org.el 878 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu 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. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. (load "org-loaddefs.el" t t t)
  74. (require 'org-macs)
  75. (require 'org-compat)
  76. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  77. ;; some places -- e.g. see the macro org-with-limited-levels.
  78. ;;
  79. ;; In Org buffers, the value of `outline-regexp' is that of
  80. ;; `org-outline-regexp'. The only function still directly relying on
  81. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  82. ;; job when `orgstruct-mode' is active.
  83. (defvar org-outline-regexp "\\*+ "
  84. "Regexp to match Org headlines.")
  85. (defvar org-outline-regexp-bol "^\\*+ "
  86. "Regexp to match Org headlines.
  87. This is similar to `org-outline-regexp' but additionally makes
  88. sure that we are at the beginning of the line.")
  89. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  90. "Matches a headline, putting stars and text into groups.
  91. Stars are put in group 1 and the trimmed body in group 2.")
  92. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  93. (when (fboundp 'defvaralias)
  94. (unless (boundp 'calendar-view-holidays-initially-flag)
  95. (defvaralias 'calendar-view-holidays-initially-flag
  96. 'view-calendar-holidays-initially))
  97. (unless (boundp 'calendar-view-diary-initially-flag)
  98. (defvaralias 'calendar-view-diary-initially-flag
  99. 'view-diary-entries-initially))
  100. (unless (boundp 'diary-fancy-buffer)
  101. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  102. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  103. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  104. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  105. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  106. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  107. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  108. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  109. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  110. (declare-function orgtbl-mode "org-table" (&optional arg))
  111. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  112. (declare-function org-beamer-mode "ox-beamer" ())
  113. (declare-function org-table-edit-field "org-table" (arg))
  114. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  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-table-align "org-table" ())
  120. (declare-function org-table-paste-rectangle "org-table" ())
  121. (declare-function org-table-maybe-eval-formula "org-table" ())
  122. (declare-function org-table-maybe-recalculate-line "org-table" ())
  123. (declare-function org-element--parse-objects "org-element"
  124. (beg end acc restriction))
  125. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  126. (declare-function org-element-contents "org-element" (element))
  127. (declare-function org-element-context "org-element" (&optional element))
  128. (declare-function org-element-interpret-data "org-element"
  129. (data &optional parent))
  130. (declare-function org-element-map "org-element"
  131. (data types fun &optional info first-match no-recursion))
  132. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  133. (declare-function org-element-parse-buffer "org-element"
  134. (&optional granularity visible-only))
  135. (declare-function org-element-property "org-element" (property element))
  136. (declare-function org-element-put-property "org-element"
  137. (element property value))
  138. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  139. (declare-function org-element--parse-objects "org-element"
  140. (beg end acc restriction))
  141. (declare-function org-element-parse-buffer "org-element"
  142. (&optional granularity visible-only))
  143. (declare-function org-element-type "org-element" (element))
  144. ;; load languages based on value of `org-babel-load-languages'
  145. (defvar org-babel-load-languages)
  146. ;;;###autoload
  147. (defun org-babel-do-load-languages (sym value)
  148. "Load the languages defined in `org-babel-load-languages'."
  149. (set-default sym value)
  150. (mapc (lambda (pair)
  151. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  152. (if active
  153. (progn
  154. (require (intern (concat "ob-" lang))))
  155. (progn
  156. (funcall 'fmakunbound
  157. (intern (concat "org-babel-execute:" lang)))
  158. (funcall 'fmakunbound
  159. (intern (concat "org-babel-expand-body:" lang)))))))
  160. org-babel-load-languages))
  161. (defcustom org-babel-load-languages '((emacs-lisp . t))
  162. "Languages which can be evaluated in Org-mode buffers.
  163. This list can be used to load support for any of the languages
  164. below, note that each language will depend on a different set of
  165. system executables and/or Emacs modes. When a language is
  166. \"loaded\", then code blocks in that language can be evaluated
  167. with `org-babel-execute-src-block' bound by default to C-c
  168. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  169. be set to remove code block evaluation from the C-c C-c
  170. keybinding. By default only Emacs Lisp (which has no
  171. requirements) is loaded."
  172. :group 'org-babel
  173. :set 'org-babel-do-load-languages
  174. :version "24.1"
  175. :type '(alist :tag "Babel Languages"
  176. :key-type
  177. (choice
  178. (const :tag "Awk" awk)
  179. (const :tag "C" C)
  180. (const :tag "R" R)
  181. (const :tag "Asymptote" asymptote)
  182. (const :tag "Calc" calc)
  183. (const :tag "Clojure" clojure)
  184. (const :tag "CSS" css)
  185. (const :tag "Ditaa" ditaa)
  186. (const :tag "Dot" dot)
  187. (const :tag "Emacs Lisp" emacs-lisp)
  188. (const :tag "Fortran" fortran)
  189. (const :tag "Gnuplot" gnuplot)
  190. (const :tag "Haskell" haskell)
  191. (const :tag "IO" io)
  192. (const :tag "Java" java)
  193. (const :tag "Javascript" js)
  194. (const :tag "LaTeX" latex)
  195. (const :tag "Ledger" ledger)
  196. (const :tag "Lilypond" lilypond)
  197. (const :tag "Lisp" lisp)
  198. (const :tag "Makefile" makefile)
  199. (const :tag "Maxima" maxima)
  200. (const :tag "Matlab" matlab)
  201. (const :tag "Mscgen" mscgen)
  202. (const :tag "Ocaml" ocaml)
  203. (const :tag "Octave" octave)
  204. (const :tag "Org" org)
  205. (const :tag "Perl" perl)
  206. (const :tag "Pico Lisp" picolisp)
  207. (const :tag "PlantUML" plantuml)
  208. (const :tag "Python" python)
  209. (const :tag "Ruby" ruby)
  210. (const :tag "Sass" sass)
  211. (const :tag "Scala" scala)
  212. (const :tag "Scheme" scheme)
  213. (const :tag "Screen" screen)
  214. (const :tag "Shell Script" sh)
  215. (const :tag "Shen" shen)
  216. (const :tag "Sql" sql)
  217. (const :tag "Sqlite" sqlite))
  218. :value-type (boolean :tag "Activate" :value t)))
  219. ;;;; Customization variables
  220. (defcustom org-clone-delete-id nil
  221. "Remove ID property of clones of a subtree.
  222. When non-nil, clones of a subtree don't inherit the ID property.
  223. Otherwise they inherit the ID property with a new unique
  224. identifier."
  225. :type 'boolean
  226. :version "24.1"
  227. :group 'org-id)
  228. ;;; Version
  229. (org-check-version)
  230. ;;;###autoload
  231. (defun org-version (&optional here full message)
  232. "Show the org-mode version in the echo area.
  233. With prefix argument HERE, insert it at point.
  234. When FULL is non-nil, use a verbose version string.
  235. When MESSAGE is non-nil, display a message with the version."
  236. (interactive "P")
  237. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  238. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  239. (load-suffixes (list ".el"))
  240. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  241. (org-trash (or
  242. (and (fboundp 'org-release) (fboundp 'org-git-version))
  243. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  244. (load-suffixes save-load-suffixes)
  245. (org-version (org-release))
  246. (git-version (org-git-version))
  247. (version (format "Org-mode version %s (%s @ %s)"
  248. org-version
  249. git-version
  250. (if org-install-dir
  251. (if (string= org-dir org-install-dir)
  252. org-install-dir
  253. (concat "mixed installation! " org-install-dir " and " org-dir))
  254. "org-loaddefs.el can not be found!")))
  255. (_version (if full version org-version)))
  256. (if (org-called-interactively-p 'interactive)
  257. (if here
  258. (insert version)
  259. (message version))
  260. (if message (message _version))
  261. _version)))
  262. (defconst org-version (org-version))
  263. ;;; Compatibility constants
  264. ;;; The custom variables
  265. (defgroup org nil
  266. "Outline-based notes management and organizer."
  267. :tag "Org"
  268. :group 'outlines
  269. :group 'calendar)
  270. (defcustom org-mode-hook nil
  271. "Mode hook for Org-mode, run after the mode was turned on."
  272. :group 'org
  273. :type 'hook)
  274. (defcustom org-load-hook nil
  275. "Hook that is run after org.el has been loaded."
  276. :group 'org
  277. :type 'hook)
  278. (defcustom org-log-buffer-setup-hook nil
  279. "Hook that is run after an Org log buffer is created."
  280. :group 'org
  281. :version "24.1"
  282. :type 'hook)
  283. (defvar org-modules) ; defined below
  284. (defvar org-modules-loaded nil
  285. "Have the modules been loaded already?")
  286. (defun org-load-modules-maybe (&optional force)
  287. "Load all extensions listed in `org-modules'."
  288. (when (or force (not org-modules-loaded))
  289. (mapc (lambda (ext)
  290. (condition-case nil (require ext)
  291. (error (message "Problems while trying to load feature `%s'" ext))))
  292. org-modules)
  293. (setq org-modules-loaded t)))
  294. (defun org-set-modules (var value)
  295. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  296. (set var value)
  297. (when (featurep 'org)
  298. (org-load-modules-maybe 'force)))
  299. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  300. "Modules that should always be loaded together with org.el.
  301. If a description starts with <C>, the file is not part of Emacs
  302. and loading it will require that you have downloaded and properly
  303. installed the Org mode distribution.
  304. You can also use this system to load external packages (i.e. neither Org
  305. core modules, nor modules from the CONTRIB directory). Just add symbols
  306. to the end of the list. If the package is called org-xyz.el, then you need
  307. to add the symbol `xyz', and the package must have a call to:
  308. \(provide 'org-xyz)
  309. For export specific modules, see also `org-export-backends'."
  310. :group 'org
  311. :set 'org-set-modules
  312. :type
  313. '(set :greedy t
  314. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  315. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  316. (const :tag " crypt: Encryption of subtrees" org-crypt)
  317. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  318. (const :tag " docview: Links to doc-view buffers" org-docview)
  319. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  320. (const :tag " id: Global IDs for identifying entries" org-id)
  321. (const :tag " info: Links to Info nodes" org-info)
  322. (const :tag " habit: Track your consistency with habits" org-habit)
  323. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  324. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  325. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  326. (const :tag " mew Links to Mew folders/messages" org-mew)
  327. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  328. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  329. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  330. (const :tag " vm: Links to VM folders/messages" org-vm)
  331. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  332. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  333. (const :tag " mouse: Additional mouse support" org-mouse)
  334. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  335. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  336. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  337. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  338. (const :tag "C collector: Collect properties into tables" org-collector)
  339. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  340. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  341. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  342. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  343. (const :tag "C eval: Include command output as text" org-eval)
  344. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  345. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  346. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  347. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  348. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  349. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  350. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  351. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  352. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  353. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  354. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  355. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  356. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  357. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  358. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  359. (const :tag "C registry: A registry for Org-mode links" org-registry)
  360. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  361. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  362. (const :tag "C secretary: Team management with org-mode" org-secretary)
  363. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  364. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  365. (const :tag "C track: Keep up with Org-mode development" org-track)
  366. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  367. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  368. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  369. (defvar org-export-registered-backends) ; From ox.el
  370. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  371. (defcustom org-export-backends '(ascii html icalendar latex)
  372. "List of export back-ends that should be always available.
  373. If a description starts with <C>, the file is not part of Emacs
  374. and loading it will require that you have downloaded and properly
  375. installed the Org mode distribution.
  376. Unlike to `org-modules', libraries in this list will not be
  377. loaded along with Org, but only once the export framework is
  378. needed.
  379. This variable needs to be set before org.el is loaded. If you
  380. need to make a change while Emacs is running, use the customize
  381. interface or run the following code, where VALUE stands for the
  382. new value of the variable, after updating it:
  383. \(progn
  384. \(setq org-export-registered-backends
  385. \(org-remove-if-not
  386. \(lambda (backend)
  387. \(or (memq backend val)
  388. \(catch 'parentp
  389. \(mapc
  390. \(lambda (b)
  391. \(and (org-export-derived-backend-p b (car backend))
  392. \(throw 'parentp t)))
  393. val)
  394. nil)))
  395. org-export-registered-backends))
  396. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  397. \(dolist (backend val)
  398. \(cond
  399. \((not (load (format \"ox-%s\" backend) t t))
  400. \(message \"Problems while trying to load export back-end `%s'\"
  401. backend))
  402. \((not (memq backend new-list)) (push backend new-list))))
  403. \(set-default var new-list)))
  404. Adding a back-end to this list will also pull the back-end it
  405. depends on, if any."
  406. :group 'org
  407. :group 'org-export
  408. :set (lambda (var val)
  409. (if (not (featurep 'ox)) (set-default var val)
  410. ;; Any back-end not required anymore (not present in VAL and not
  411. ;; a parent of any back-end in the new value) is removed from the
  412. ;; list of registered back-ends.
  413. (setq org-export-registered-backends
  414. (org-remove-if-not
  415. (lambda (backend)
  416. (or (memq backend val)
  417. (catch 'parentp
  418. (mapc
  419. (lambda (b)
  420. (and (org-export-derived-backend-p b (car backend))
  421. (throw 'parentp t)))
  422. val)
  423. nil)))
  424. org-export-registered-backends))
  425. ;; Now build NEW-LIST of both new back-ends and required
  426. ;; parents.
  427. (let ((new-list (mapcar 'car org-export-registered-backends)))
  428. (dolist (backend val)
  429. (cond
  430. ((not (load (format "ox-%s" backend) t t))
  431. (message "Problems while trying to load export back-end `%s'"
  432. backend))
  433. ((not (memq backend new-list)) (push backend new-list))))
  434. ;; Set VAR to that list with fixed dependencies.
  435. (set-default var new-list))))
  436. :type '(set :greedy t
  437. (const :tag " ascii Export buffer to ASCII format" ascii)
  438. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  439. (const :tag " html Export buffer to HTML format" html)
  440. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  441. (const :tag " latex Export buffer to LaTeX format" latex)
  442. (const :tag " man Export buffer to MAN format" man)
  443. (const :tag " md Export buffer to Markdown format" md)
  444. (const :tag " odt Export buffer to ODT format" odt)
  445. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  446. (const :tag " infojs: Set up Sebastian Rose's JavaScript org-info.js" jsinfo)
  447. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  448. (const :tag "C groff Export buffer to Groff format" groff)
  449. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  450. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  451. (eval-after-load 'ox
  452. '(mapc
  453. (lambda (backend)
  454. (condition-case nil (require (intern (format "ox-%s" backend)))
  455. (error (message "Problems while trying to load export back-end `%s'"
  456. backend))))
  457. org-export-backends))
  458. (defcustom org-support-shift-select nil
  459. "Non-nil means make shift-cursor commands select text when possible.
  460. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  461. start selecting a region, or enlarge regions started in this way.
  462. In Org-mode, in special contexts, these same keys are used for
  463. other purposes, important enough to compete with shift selection.
  464. Org tries to balance these needs by supporting `shift-select-mode'
  465. outside these special contexts, under control of this variable.
  466. The default of this variable is nil, to avoid confusing behavior. Shifted
  467. cursor keys will then execute Org commands in the following contexts:
  468. - on a headline, changing TODO state (left/right) and priority (up/down)
  469. - on a time stamp, changing the time
  470. - in a plain list item, changing the bullet type
  471. - in a property definition line, switching between allowed values
  472. - in the BEGIN line of a clock table (changing the time block).
  473. Outside these contexts, the commands will throw an error.
  474. When this variable is t and the cursor is not in a special
  475. context, Org-mode will support shift-selection for making and
  476. enlarging regions. To make this more effective, the bullet
  477. cycling will no longer happen anywhere in an item line, but only
  478. if the cursor is exactly on the bullet.
  479. If you set this variable to the symbol `always', then the keys
  480. will not be special in headlines, property lines, and item lines,
  481. to make shift selection work there as well. If this is what you
  482. want, you can use the following alternative commands: `C-c C-t'
  483. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  484. can be used to switch TODO sets, `C-c -' to cycle item bullet
  485. types, and properties can be edited by hand or in column view.
  486. However, when the cursor is on a timestamp, shift-cursor commands
  487. will still edit the time stamp - this is just too good to give up.
  488. XEmacs user should have this variable set to nil, because
  489. `shift-select-mode' is in Emacs 23 or later only."
  490. :group 'org
  491. :type '(choice
  492. (const :tag "Never" nil)
  493. (const :tag "When outside special context" t)
  494. (const :tag "Everywhere except timestamps" always)))
  495. (defcustom org-loop-over-headlines-in-active-region nil
  496. "Shall some commands act upon headlines in the active region?
  497. When set to `t', some commands will be performed in all headlines
  498. within the active region.
  499. When set to `start-level', some commands will be performed in all
  500. headlines within the active region, provided that these headlines
  501. are of the same level than the first one.
  502. When set to a string, those commands will be performed on the
  503. matching headlines within the active region. Such string must be
  504. a tags/property/todo match as it is used in the agenda tags view.
  505. The list of commands is: `org-schedule', `org-deadline',
  506. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  507. `org-archive-to-archive-sibling'. The archiving commands skip
  508. already archived entries."
  509. :type '(choice (const :tag "Don't loop" nil)
  510. (const :tag "All headlines in active region" t)
  511. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  512. (string :tag "Tags/Property/Todo matcher"))
  513. :version "24.1"
  514. :group 'org-todo
  515. :group 'org-archive)
  516. (defgroup org-startup nil
  517. "Options concerning startup of Org-mode."
  518. :tag "Org Startup"
  519. :group 'org)
  520. (defcustom org-startup-folded t
  521. "Non-nil means entering Org-mode will switch to OVERVIEW.
  522. This can also be configured on a per-file basis by adding one of
  523. the following lines anywhere in the buffer:
  524. #+STARTUP: fold (or `overview', this is equivalent)
  525. #+STARTUP: nofold (or `showall', this is equivalent)
  526. #+STARTUP: content
  527. #+STARTUP: showeverything
  528. By default, this option is ignored when Org opens agenda files
  529. for the first time. If you want the agenda to honor the startup
  530. option, set `org-agenda-inhibit-startup' to nil."
  531. :group 'org-startup
  532. :type '(choice
  533. (const :tag "nofold: show all" nil)
  534. (const :tag "fold: overview" t)
  535. (const :tag "content: all headlines" content)
  536. (const :tag "show everything, even drawers" showeverything)))
  537. (defcustom org-startup-truncated t
  538. "Non-nil means entering Org-mode will set `truncate-lines'.
  539. This is useful since some lines containing links can be very long and
  540. uninteresting. Also tables look terrible when wrapped."
  541. :group 'org-startup
  542. :type 'boolean)
  543. (defcustom org-startup-indented nil
  544. "Non-nil means turn on `org-indent-mode' on startup.
  545. This can also be configured on a per-file basis by adding one of
  546. the following lines anywhere in the buffer:
  547. #+STARTUP: indent
  548. #+STARTUP: noindent"
  549. :group 'org-structure
  550. :type '(choice
  551. (const :tag "Not" nil)
  552. (const :tag "Globally (slow on startup in large files)" t)))
  553. (defcustom org-use-sub-superscripts t
  554. "Non-nil means interpret \"_\" and \"^\" for display.
  555. When this option is turned on, you can use TeX-like syntax for sub- and
  556. superscripts. Several characters after \"_\" or \"^\" will be
  557. considered as a single item - so grouping with {} is normally not
  558. needed. For example, the following things will be parsed as single
  559. sub- or superscripts.
  560. 10^24 or 10^tau several digits will be considered 1 item.
  561. 10^-12 or 10^-tau a leading sign with digits or a word
  562. x^2-y^3 will be read as x^2 - y^3, because items are
  563. terminated by almost any nonword/nondigit char.
  564. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  565. Still, ambiguity is possible - so when in doubt use {} to enclose
  566. the sub/superscript. If you set this variable to the symbol
  567. `{}', the braces are *required* in order to trigger
  568. interpretations as sub/superscript. This can be helpful in
  569. documents that need \"_\" frequently in plain text."
  570. :group 'org-startup
  571. :version "24.1"
  572. :type '(choice
  573. (const :tag "Always interpret" t)
  574. (const :tag "Only with braces" {})
  575. (const :tag "Never interpret" nil)))
  576. (defcustom org-startup-with-beamer-mode nil
  577. "Non-nil means turn on `org-beamer-mode' on startup.
  578. This can also be configured on a per-file basis by adding one of
  579. the following lines anywhere in the buffer:
  580. #+STARTUP: beamer"
  581. :group 'org-startup
  582. :version "24.1"
  583. :type 'boolean)
  584. (defcustom org-startup-align-all-tables nil
  585. "Non-nil means align all tables when visiting a file.
  586. This is useful when the column width in tables is forced with <N> cookies
  587. in table fields. Such tables will look correct only after the first re-align.
  588. This can also be configured on a per-file basis by adding one of
  589. the following lines anywhere in the buffer:
  590. #+STARTUP: align
  591. #+STARTUP: noalign"
  592. :group 'org-startup
  593. :type 'boolean)
  594. (defcustom org-startup-with-inline-images nil
  595. "Non-nil means show inline images when loading a new Org file.
  596. This can also be configured on a per-file basis by adding one of
  597. the following lines anywhere in the buffer:
  598. #+STARTUP: inlineimages
  599. #+STARTUP: noinlineimages"
  600. :group 'org-startup
  601. :version "24.1"
  602. :type 'boolean)
  603. (defcustom org-startup-with-latex-preview nil
  604. "Non-nil means preview LaTeX fragments when loading a new Org file.
  605. This can also be configured on a per-file basis by adding one of
  606. the followinglines anywhere in the buffer:
  607. #+STARTUP: latexpreview
  608. #+STARTUP: nolatexpreview"
  609. :group 'org-startup
  610. :version "24.3"
  611. :type 'boolean)
  612. (defcustom org-insert-mode-line-in-empty-file nil
  613. "Non-nil means insert the first line setting Org-mode in empty files.
  614. When the function `org-mode' is called interactively in an empty file, this
  615. normally means that the file name does not automatically trigger Org-mode.
  616. To ensure that the file will always be in Org-mode in the future, a
  617. line enforcing Org-mode will be inserted into the buffer, if this option
  618. has been set."
  619. :group 'org-startup
  620. :type 'boolean)
  621. (defcustom org-replace-disputed-keys nil
  622. "Non-nil means use alternative key bindings for some keys.
  623. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  624. These keys are also used by other packages like shift-selection-mode'
  625. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  626. If you want to use Org-mode together with one of these other modes,
  627. or more generally if you would like to move some Org-mode commands to
  628. other keys, set this variable and configure the keys with the variable
  629. `org-disputed-keys'.
  630. This option is only relevant at load-time of Org-mode, and must be set
  631. *before* org.el is loaded. Changing it requires a restart of Emacs to
  632. become effective."
  633. :group 'org-startup
  634. :type 'boolean)
  635. (defcustom org-use-extra-keys nil
  636. "Non-nil means use extra key sequence definitions for certain commands.
  637. This happens automatically if you run XEmacs or if `window-system'
  638. is nil. This variable lets you do the same manually. You must
  639. set it before loading org.
  640. Example: on Carbon Emacs 22 running graphically, with an external
  641. keyboard on a Powerbook, the default way of setting M-left might
  642. not work for either Alt or ESC. Setting this variable will make
  643. it work for ESC."
  644. :group 'org-startup
  645. :type 'boolean)
  646. (if (fboundp 'defvaralias)
  647. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  648. (defcustom org-disputed-keys
  649. '(([(shift up)] . [(meta p)])
  650. ([(shift down)] . [(meta n)])
  651. ([(shift left)] . [(meta -)])
  652. ([(shift right)] . [(meta +)])
  653. ([(control shift right)] . [(meta shift +)])
  654. ([(control shift left)] . [(meta shift -)]))
  655. "Keys for which Org-mode and other modes compete.
  656. This is an alist, cars are the default keys, second element specifies
  657. the alternative to use when `org-replace-disputed-keys' is t.
  658. Keys can be specified in any syntax supported by `define-key'.
  659. The value of this option takes effect only at Org-mode's startup,
  660. therefore you'll have to restart Emacs to apply it after changing."
  661. :group 'org-startup
  662. :type 'alist)
  663. (defun org-key (key)
  664. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  665. Or return the original if not disputed.
  666. Also apply the translations defined in `org-xemacs-key-equivalents'."
  667. (when org-replace-disputed-keys
  668. (let* ((nkey (key-description key))
  669. (x (org-find-if (lambda (x)
  670. (equal (key-description (car x)) nkey))
  671. org-disputed-keys)))
  672. (setq key (if x (cdr x) key))))
  673. (when (featurep 'xemacs)
  674. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  675. key)
  676. (defun org-find-if (predicate seq)
  677. (catch 'exit
  678. (while seq
  679. (if (funcall predicate (car seq))
  680. (throw 'exit (car seq))
  681. (pop seq)))))
  682. (defun org-defkey (keymap key def)
  683. "Define a key, possibly translated, as returned by `org-key'."
  684. (define-key keymap (org-key key) def))
  685. (defcustom org-ellipsis nil
  686. "The ellipsis to use in the Org-mode outline.
  687. When nil, just use the standard three dots. When a string, use that instead,
  688. When a face, use the standard 3 dots, but with the specified face.
  689. The change affects only Org-mode (which will then use its own display table).
  690. Changing this requires executing `M-x org-mode' in a buffer to become
  691. effective."
  692. :group 'org-startup
  693. :type '(choice (const :tag "Default" nil)
  694. (face :tag "Face" :value org-warning)
  695. (string :tag "String" :value "...#")))
  696. (defvar org-display-table nil
  697. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  698. (defgroup org-keywords nil
  699. "Keywords in Org-mode."
  700. :tag "Org Keywords"
  701. :group 'org)
  702. (defcustom org-deadline-string "DEADLINE:"
  703. "String to mark deadline entries.
  704. A deadline is this string, followed by a time stamp. Should be a word,
  705. terminated by a colon. You can insert a schedule keyword and
  706. a timestamp with \\[org-deadline].
  707. Changes become only effective after restarting Emacs."
  708. :group 'org-keywords
  709. :type 'string)
  710. (defcustom org-scheduled-string "SCHEDULED:"
  711. "String to mark scheduled TODO entries.
  712. A schedule is this string, followed by a time stamp. Should be a word,
  713. terminated by a colon. You can insert a schedule keyword and
  714. a timestamp with \\[org-schedule].
  715. Changes become only effective after restarting Emacs."
  716. :group 'org-keywords
  717. :type 'string)
  718. (defcustom org-closed-string "CLOSED:"
  719. "String used as the prefix for timestamps logging closing a TODO entry."
  720. :group 'org-keywords
  721. :type 'string)
  722. (defcustom org-clock-string "CLOCK:"
  723. "String used as prefix for timestamps clocking work hours on an item."
  724. :group 'org-keywords
  725. :type 'string)
  726. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  727. org-scheduled-string "\\|"
  728. org-deadline-string "\\|"
  729. org-closed-string "\\|"
  730. org-clock-string "\\)")
  731. "Matches a line with planning or clock info.")
  732. (defcustom org-comment-string "COMMENT"
  733. "Entries starting with this keyword will never be exported.
  734. An entry can be toggled between COMMENT and normal with
  735. \\[org-toggle-comment].
  736. Changes become only effective after restarting Emacs."
  737. :group 'org-keywords
  738. :type 'string)
  739. (defcustom org-quote-string "QUOTE"
  740. "Entries starting with this keyword will be exported in fixed-width font.
  741. Quoting applies only to the text in the entry following the headline, and does
  742. not extend beyond the next headline, even if that is lower level.
  743. An entry can be toggled between QUOTE and normal with
  744. \\[org-toggle-fixed-width-section]."
  745. :group 'org-keywords
  746. :type 'string)
  747. (defconst org-repeat-re
  748. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  749. "Regular expression for specifying repeated events.
  750. After a match, group 1 contains the repeat expression.")
  751. (defgroup org-structure nil
  752. "Options concerning the general structure of Org-mode files."
  753. :tag "Org Structure"
  754. :group 'org)
  755. (defgroup org-reveal-location nil
  756. "Options about how to make context of a location visible."
  757. :tag "Org Reveal Location"
  758. :group 'org-structure)
  759. (defconst org-context-choice
  760. '(choice
  761. (const :tag "Always" t)
  762. (const :tag "Never" nil)
  763. (repeat :greedy t :tag "Individual contexts"
  764. (cons
  765. (choice :tag "Context"
  766. (const agenda)
  767. (const org-goto)
  768. (const occur-tree)
  769. (const tags-tree)
  770. (const link-search)
  771. (const mark-goto)
  772. (const bookmark-jump)
  773. (const isearch)
  774. (const default))
  775. (boolean))))
  776. "Contexts for the reveal options.")
  777. (defcustom org-show-hierarchy-above '((default . t))
  778. "Non-nil means show full hierarchy when revealing a location.
  779. Org-mode often shows locations in an org-mode file which might have
  780. been invisible before. When this is set, the hierarchy of headings
  781. above the exposed location is shown.
  782. Turning this off for example for sparse trees makes them very compact.
  783. Instead of t, this can also be an alist specifying this option for different
  784. contexts. Valid contexts are
  785. agenda when exposing an entry from the agenda
  786. org-goto when using the command `org-goto' on key C-c C-j
  787. occur-tree when using the command `org-occur' on key C-c /
  788. tags-tree when constructing a sparse tree based on tags matches
  789. link-search when exposing search matches associated with a link
  790. mark-goto when exposing the jump goal of a mark
  791. bookmark-jump when exposing a bookmark location
  792. isearch when exiting from an incremental search
  793. default default for all contexts not set explicitly"
  794. :group 'org-reveal-location
  795. :type org-context-choice)
  796. (defcustom org-show-following-heading '((default . nil))
  797. "Non-nil means show following heading when revealing a location.
  798. Org-mode often shows locations in an org-mode file which might have
  799. been invisible before. When this is set, the heading following the
  800. match is shown.
  801. Turning this off for example for sparse trees makes them very compact,
  802. but makes it harder to edit the location of the match. In such a case,
  803. use the command \\[org-reveal] to show more context.
  804. Instead of t, this can also be an alist specifying this option for different
  805. contexts. See `org-show-hierarchy-above' for valid contexts."
  806. :group 'org-reveal-location
  807. :type org-context-choice)
  808. (defcustom org-show-siblings '((default . nil) (isearch t))
  809. "Non-nil means show all sibling heading when revealing a location.
  810. Org-mode often shows locations in an org-mode file which might have
  811. been invisible before. When this is set, the sibling of the current entry
  812. heading are all made visible. If `org-show-hierarchy-above' is t,
  813. the same happens on each level of the hierarchy above the current entry.
  814. By default this is on for the isearch context, off for all other contexts.
  815. Turning this off for example for sparse trees makes them very compact,
  816. but makes it harder to edit the location of the match. In such a case,
  817. use the command \\[org-reveal] to show more context.
  818. Instead of t, this can also be an alist specifying this option for different
  819. contexts. See `org-show-hierarchy-above' for valid contexts."
  820. :group 'org-reveal-location
  821. :type org-context-choice)
  822. (defcustom org-show-entry-below '((default . nil))
  823. "Non-nil means show the entry below a headline when revealing a location.
  824. Org-mode often shows locations in an org-mode file which might have
  825. been invisible before. When this is set, the text below the headline that is
  826. exposed is also shown.
  827. By default this is off for all contexts.
  828. Instead of t, this can also be an alist specifying this option for different
  829. contexts. See `org-show-hierarchy-above' for valid contexts."
  830. :group 'org-reveal-location
  831. :type org-context-choice)
  832. (defcustom org-indirect-buffer-display 'other-window
  833. "How should indirect tree buffers be displayed?
  834. This applies to indirect buffers created with the commands
  835. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  836. Valid values are:
  837. current-window Display in the current window
  838. other-window Just display in another window.
  839. dedicated-frame Create one new frame, and re-use it each time.
  840. new-frame Make a new frame each time. Note that in this case
  841. previously-made indirect buffers are kept, and you need to
  842. kill these buffers yourself."
  843. :group 'org-structure
  844. :group 'org-agenda-windows
  845. :type '(choice
  846. (const :tag "In current window" current-window)
  847. (const :tag "In current frame, other window" other-window)
  848. (const :tag "Each time a new frame" new-frame)
  849. (const :tag "One dedicated frame" dedicated-frame)))
  850. (defcustom org-use-speed-commands nil
  851. "Non-nil means activate single letter commands at beginning of a headline.
  852. This may also be a function to test for appropriate locations where speed
  853. commands should be active."
  854. :group 'org-structure
  855. :type '(choice
  856. (const :tag "Never" nil)
  857. (const :tag "At beginning of headline stars" t)
  858. (function)))
  859. (defcustom org-speed-commands-user nil
  860. "Alist of additional speed commands.
  861. This list will be checked before `org-speed-commands-default'
  862. when the variable `org-use-speed-commands' is non-nil
  863. and when the cursor is at the beginning of a headline.
  864. The car if each entry is a string with a single letter, which must
  865. be assigned to `self-insert-command' in the global map.
  866. The cdr is either a command to be called interactively, a function
  867. to be called, or a form to be evaluated.
  868. An entry that is just a list with a single string will be interpreted
  869. as a descriptive headline that will be added when listing the speed
  870. commands in the Help buffer using the `?' speed command."
  871. :group 'org-structure
  872. :type '(repeat :value ("k" . ignore)
  873. (choice :value ("k" . ignore)
  874. (list :tag "Descriptive Headline" (string :tag "Headline"))
  875. (cons :tag "Letter and Command"
  876. (string :tag "Command letter")
  877. (choice
  878. (function)
  879. (sexp))))))
  880. (defgroup org-cycle nil
  881. "Options concerning visibility cycling in Org-mode."
  882. :tag "Org Cycle"
  883. :group 'org-structure)
  884. (defcustom org-cycle-skip-children-state-if-no-children t
  885. "Non-nil means skip CHILDREN state in entries that don't have any."
  886. :group 'org-cycle
  887. :type 'boolean)
  888. (defcustom org-cycle-max-level nil
  889. "Maximum level which should still be subject to visibility cycling.
  890. Levels higher than this will, for cycling, be treated as text, not a headline.
  891. When `org-odd-levels-only' is set, a value of N in this variable actually
  892. means 2N-1 stars as the limiting headline.
  893. When nil, cycle all levels.
  894. Note that the limiting level of cycling is also influenced by
  895. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  896. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  897. than its value."
  898. :group 'org-cycle
  899. :type '(choice
  900. (const :tag "No limit" nil)
  901. (integer :tag "Maximum level")))
  902. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  903. "Names of drawers. Drawers are not opened by cycling on the headline above.
  904. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  905. this:
  906. :DRAWERNAME:
  907. .....
  908. :END:
  909. The drawer \"PROPERTIES\" is special for capturing properties through
  910. the property API.
  911. Drawers can be defined on the per-file basis with a line like:
  912. #+DRAWERS: HIDDEN STATE PROPERTIES"
  913. :group 'org-structure
  914. :group 'org-cycle
  915. :type '(repeat (string :tag "Drawer Name")))
  916. (defcustom org-hide-block-startup nil
  917. "Non-nil means entering Org-mode will fold all blocks.
  918. This can also be set in on a per-file basis with
  919. #+STARTUP: hideblocks
  920. #+STARTUP: showblocks"
  921. :group 'org-startup
  922. :group 'org-cycle
  923. :type 'boolean)
  924. (defcustom org-cycle-global-at-bob nil
  925. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  926. This makes it possible to do global cycling without having to use S-TAB or
  927. \\[universal-argument] TAB. For this special case to work, the first line
  928. of the buffer must not be a headline -- it may be empty or some other text.
  929. When used in this way, `org-cycle-hook' is disabled temporarily to make
  930. sure the cursor stays at the beginning of the buffer. When this option is
  931. nil, don't do anything special at the beginning of the buffer."
  932. :group 'org-cycle
  933. :type 'boolean)
  934. (defcustom org-cycle-level-after-item/entry-creation t
  935. "Non-nil means cycle entry level or item indentation in new empty entries.
  936. When the cursor is at the end of an empty headline, i.e., with only stars
  937. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  938. and then all possible ancestor states, before returning to the original state.
  939. This makes data entry extremely fast: M-RET to create a new headline,
  940. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  941. When the cursor is at the end of an empty plain list item, one TAB will
  942. make it a subitem, two or more tabs will back up to make this an item
  943. higher up in the item hierarchy."
  944. :group 'org-cycle
  945. :type 'boolean)
  946. (defcustom org-cycle-emulate-tab t
  947. "Where should `org-cycle' emulate TAB.
  948. nil Never
  949. white Only in completely white lines
  950. whitestart Only at the beginning of lines, before the first non-white char
  951. t Everywhere except in headlines
  952. exc-hl-bol Everywhere except at the start of a headline
  953. If TAB is used in a place where it does not emulate TAB, the current subtree
  954. visibility is cycled."
  955. :group 'org-cycle
  956. :type '(choice (const :tag "Never" nil)
  957. (const :tag "Only in completely white lines" white)
  958. (const :tag "Before first char in a line" whitestart)
  959. (const :tag "Everywhere except in headlines" t)
  960. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  961. ))
  962. (defcustom org-cycle-separator-lines 2
  963. "Number of empty lines needed to keep an empty line between collapsed trees.
  964. If you leave an empty line between the end of a subtree and the following
  965. headline, this empty line is hidden when the subtree is folded.
  966. Org-mode will leave (exactly) one empty line visible if the number of
  967. empty lines is equal or larger to the number given in this variable.
  968. So the default 2 means at least 2 empty lines after the end of a subtree
  969. are needed to produce free space between a collapsed subtree and the
  970. following headline.
  971. If the number is negative, and the number of empty lines is at least -N,
  972. all empty lines are shown.
  973. Special case: when 0, never leave empty lines in collapsed view."
  974. :group 'org-cycle
  975. :type 'integer)
  976. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  977. (defcustom org-pre-cycle-hook nil
  978. "Hook that is run before visibility cycling is happening.
  979. The function(s) in this hook must accept a single argument which indicates
  980. the new state that will be set right after running this hook. The
  981. argument is a symbol. Before a global state change, it can have the values
  982. `overview', `content', or `all'. Before a local state change, it can have
  983. the values `folded', `children', or `subtree'."
  984. :group 'org-cycle
  985. :type 'hook)
  986. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  987. org-cycle-hide-drawers
  988. org-cycle-hide-inline-tasks
  989. org-cycle-show-empty-lines
  990. org-optimize-window-after-visibility-change)
  991. "Hook that is run after `org-cycle' has changed the buffer visibility.
  992. The function(s) in this hook must accept a single argument which indicates
  993. the new state that was set by the most recent `org-cycle' command. The
  994. argument is a symbol. After a global state change, it can have the values
  995. `overview', `contents', or `all'. After a local state change, it can have
  996. the values `folded', `children', or `subtree'."
  997. :group 'org-cycle
  998. :type 'hook)
  999. (defgroup org-edit-structure nil
  1000. "Options concerning structure editing in Org-mode."
  1001. :tag "Org Edit Structure"
  1002. :group 'org-structure)
  1003. (defcustom org-odd-levels-only nil
  1004. "Non-nil means skip even levels and only use odd levels for the outline.
  1005. This has the effect that two stars are being added/taken away in
  1006. promotion/demotion commands. It also influences how levels are
  1007. handled by the exporters.
  1008. Changing it requires restart of `font-lock-mode' to become effective
  1009. for fontification also in regions already fontified.
  1010. You may also set this on a per-file basis by adding one of the following
  1011. lines to the buffer:
  1012. #+STARTUP: odd
  1013. #+STARTUP: oddeven"
  1014. :group 'org-edit-structure
  1015. :group 'org-appearance
  1016. :type 'boolean)
  1017. (defcustom org-adapt-indentation t
  1018. "Non-nil means adapt indentation to outline node level.
  1019. When this variable is set, Org assumes that you write outlines by
  1020. indenting text in each node to align with the headline (after the stars).
  1021. The following issues are influenced by this variable:
  1022. - When this is set and the *entire* text in an entry is indented, the
  1023. indentation is increased by one space in a demotion command, and
  1024. decreased by one in a promotion command. If any line in the entry
  1025. body starts with text at column 0, indentation is not changed at all.
  1026. - Property drawers and planning information is inserted indented when
  1027. this variable s set. When nil, they will not be indented.
  1028. - TAB indents a line relative to context. The lines below a headline
  1029. will be indented when this variable is set.
  1030. Note that this is all about true indentation, by adding and removing
  1031. space characters. See also `org-indent.el' which does level-dependent
  1032. indentation in a virtual way, i.e. at display time in Emacs."
  1033. :group 'org-edit-structure
  1034. :type 'boolean)
  1035. (defcustom org-special-ctrl-a/e nil
  1036. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1037. When t, `C-a' will bring back the cursor to the beginning of the
  1038. headline text, i.e. after the stars and after a possible TODO
  1039. keyword. In an item, this will be the position after bullet and
  1040. check-box, if any. When the cursor is already at that position,
  1041. another `C-a' will bring it to the beginning of the line.
  1042. `C-e' will jump to the end of the headline, ignoring the presence
  1043. of tags in the headline. A second `C-e' will then jump to the
  1044. true end of the line, after any tags. This also means that, when
  1045. this variable is non-nil, `C-e' also will never jump beyond the
  1046. end of the heading of a folded section, i.e. not after the
  1047. ellipses.
  1048. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1049. normally, going to the true line boundary first. Only a directly
  1050. following, identical keypress will bring the cursor to the
  1051. special positions.
  1052. This may also be a cons cell where the behavior for `C-a' and
  1053. `C-e' is set separately."
  1054. :group 'org-edit-structure
  1055. :type '(choice
  1056. (const :tag "off" nil)
  1057. (const :tag "on: after stars/bullet and before tags first" t)
  1058. (const :tag "reversed: true line boundary first" reversed)
  1059. (cons :tag "Set C-a and C-e separately"
  1060. (choice :tag "Special C-a"
  1061. (const :tag "off" nil)
  1062. (const :tag "on: after stars/bullet first" t)
  1063. (const :tag "reversed: before stars/bullet first" reversed))
  1064. (choice :tag "Special C-e"
  1065. (const :tag "off" nil)
  1066. (const :tag "on: before tags first" t)
  1067. (const :tag "reversed: after tags first" reversed)))))
  1068. (if (fboundp 'defvaralias)
  1069. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  1070. (defcustom org-special-ctrl-k nil
  1071. "Non-nil means `C-k' will behave specially in headlines.
  1072. When nil, `C-k' will call the default `kill-line' command.
  1073. When t, the following will happen while the cursor is in the headline:
  1074. - When the cursor is at the beginning of a headline, kill the entire
  1075. line and possible the folded subtree below the line.
  1076. - When in the middle of the headline text, kill the headline up to the tags.
  1077. - When after the headline text, kill the tags."
  1078. :group 'org-edit-structure
  1079. :type 'boolean)
  1080. (defcustom org-ctrl-k-protect-subtree nil
  1081. "Non-nil means, do not delete a hidden subtree with C-k.
  1082. When set to the symbol `error', simply throw an error when C-k is
  1083. used to kill (part-of) a headline that has hidden text behind it.
  1084. Any other non-nil value will result in a query to the user, if it is
  1085. OK to kill that hidden subtree. When nil, kill without remorse."
  1086. :group 'org-edit-structure
  1087. :version "24.1"
  1088. :type '(choice
  1089. (const :tag "Do not protect hidden subtrees" nil)
  1090. (const :tag "Protect hidden subtrees with a security query" t)
  1091. (const :tag "Never kill a hidden subtree with C-k" error)))
  1092. (defcustom org-catch-invisible-edits nil
  1093. "Check if in invisible region before inserting or deleting a character.
  1094. Valid values are:
  1095. nil Do not check, so just do invisible edits.
  1096. error Throw an error and do nothing.
  1097. show Make point visible, and do the requested edit.
  1098. show-and-error Make point visible, then throw an error and abort the edit.
  1099. smart Make point visible, and do insertion/deletion if it is
  1100. adjacent to visible text and the change feels predictable.
  1101. Never delete a previously invisible character or add in the
  1102. middle or right after an invisible region. Basically, this
  1103. allows insertion and backward-delete right before ellipses.
  1104. FIXME: maybe in this case we should not even show?"
  1105. :group 'org-edit-structure
  1106. :version "24.1"
  1107. :type '(choice
  1108. (const :tag "Do not check" nil)
  1109. (const :tag "Throw error when trying to edit" error)
  1110. (const :tag "Unhide, but do not do the edit" show-and-error)
  1111. (const :tag "Show invisible part and do the edit" show)
  1112. (const :tag "Be smart and do the right thing" smart)))
  1113. (defcustom org-yank-folded-subtrees t
  1114. "Non-nil means when yanking subtrees, fold them.
  1115. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1116. it starts with a heading and all other headings in it are either children
  1117. or siblings, then fold all the subtrees. However, do this only if no
  1118. text after the yank would be swallowed into a folded tree by this action."
  1119. :group 'org-edit-structure
  1120. :type 'boolean)
  1121. (defcustom org-yank-adjusted-subtrees nil
  1122. "Non-nil means when yanking subtrees, adjust the level.
  1123. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1124. this function for details."
  1125. :group 'org-edit-structure
  1126. :type 'boolean)
  1127. (defcustom org-M-RET-may-split-line '((default . t))
  1128. "Non-nil means M-RET will split the line at the cursor position.
  1129. When nil, it will go to the end of the line before making a
  1130. new line.
  1131. You may also set this option in a different way for different
  1132. contexts. Valid contexts are:
  1133. headline when creating a new headline
  1134. item when creating a new item
  1135. table in a table field
  1136. default the value to be used for all contexts not explicitly
  1137. customized"
  1138. :group 'org-structure
  1139. :group 'org-table
  1140. :type '(choice
  1141. (const :tag "Always" t)
  1142. (const :tag "Never" nil)
  1143. (repeat :greedy t :tag "Individual contexts"
  1144. (cons
  1145. (choice :tag "Context"
  1146. (const headline)
  1147. (const item)
  1148. (const table)
  1149. (const default))
  1150. (boolean)))))
  1151. (defcustom org-insert-heading-respect-content nil
  1152. "Non-nil means insert new headings after the current subtree.
  1153. When nil, the new heading is created directly after the current line.
  1154. The commands \\[org-insert-heading-respect-content] and
  1155. \\[org-insert-todo-heading-respect-content] turn this variable on
  1156. for the duration of the command."
  1157. :group 'org-structure
  1158. :type 'boolean)
  1159. (defcustom org-blank-before-new-entry '((heading . auto)
  1160. (plain-list-item . auto))
  1161. "Should `org-insert-heading' leave a blank line before new heading/item?
  1162. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1163. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1164. which case Org will look at the surrounding headings/items and try to
  1165. make an intelligent decision whether to insert a blank line or not.
  1166. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1167. set, the setting here is ignored and no empty line is inserted, to avoid
  1168. breaking the list structure."
  1169. :group 'org-edit-structure
  1170. :type '(list
  1171. (cons (const heading)
  1172. (choice (const :tag "Never" nil)
  1173. (const :tag "Always" t)
  1174. (const :tag "Auto" auto)))
  1175. (cons (const plain-list-item)
  1176. (choice (const :tag "Never" nil)
  1177. (const :tag "Always" t)
  1178. (const :tag "Auto" auto)))))
  1179. (defcustom org-insert-heading-hook nil
  1180. "Hook being run after inserting a new heading."
  1181. :group 'org-edit-structure
  1182. :type 'hook)
  1183. (defcustom org-enable-fixed-width-editor t
  1184. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1185. This currently only means they are never auto-wrapped.
  1186. When nil, such lines will be treated like ordinary lines.
  1187. See also the QUOTE keyword."
  1188. :group 'org-edit-structure
  1189. :type 'boolean)
  1190. (defcustom org-goto-auto-isearch t
  1191. "Non-nil means typing characters in `org-goto' starts incremental search.
  1192. When nil, you can use these keybindings to navigate the buffer:
  1193. q Quit the org-goto interface
  1194. n Go to the next visible heading
  1195. p Go to the previous visible heading
  1196. f Go one heading forward on same level
  1197. b Go one heading backward on same level
  1198. u Go one heading up"
  1199. :group 'org-edit-structure
  1200. :type 'boolean)
  1201. (defgroup org-sparse-trees nil
  1202. "Options concerning sparse trees in Org-mode."
  1203. :tag "Org Sparse Trees"
  1204. :group 'org-structure)
  1205. (defcustom org-highlight-sparse-tree-matches t
  1206. "Non-nil means highlight all matches that define a sparse tree.
  1207. The highlights will automatically disappear the next time the buffer is
  1208. changed by an edit command."
  1209. :group 'org-sparse-trees
  1210. :type 'boolean)
  1211. (defcustom org-remove-highlights-with-change t
  1212. "Non-nil means any change to the buffer will remove temporary highlights.
  1213. Such highlights are created by `org-occur' and `org-clock-display'.
  1214. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1215. The highlights created by `org-preview-latex-fragment' always need
  1216. `C-c C-c' to be removed."
  1217. :group 'org-sparse-trees
  1218. :group 'org-time
  1219. :type 'boolean)
  1220. (defcustom org-occur-hook '(org-first-headline-recenter)
  1221. "Hook that is run after `org-occur' has constructed a sparse tree.
  1222. This can be used to recenter the window to show as much of the structure
  1223. as possible."
  1224. :group 'org-sparse-trees
  1225. :type 'hook)
  1226. (defgroup org-imenu-and-speedbar nil
  1227. "Options concerning imenu and speedbar in Org-mode."
  1228. :tag "Org Imenu and Speedbar"
  1229. :group 'org-structure)
  1230. (defcustom org-imenu-depth 2
  1231. "The maximum level for Imenu access to Org-mode headlines.
  1232. This also applied for speedbar access."
  1233. :group 'org-imenu-and-speedbar
  1234. :type 'integer)
  1235. (defgroup org-table nil
  1236. "Options concerning tables in Org-mode."
  1237. :tag "Org Table"
  1238. :group 'org)
  1239. (defcustom org-enable-table-editor 'optimized
  1240. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1241. When nil, such lines will be treated like ordinary lines.
  1242. When equal to the symbol `optimized', the table editor will be optimized to
  1243. do the following:
  1244. - Automatic overwrite mode in front of whitespace in table fields.
  1245. This makes the structure of the table stay in tact as long as the edited
  1246. field does not exceed the column width.
  1247. - Minimize the number of realigns. Normally, the table is aligned each time
  1248. TAB or RET are pressed to move to another field. With optimization this
  1249. happens only if changes to a field might have changed the column width.
  1250. Optimization requires replacing the functions `self-insert-command',
  1251. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1252. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1253. very good at guessing when a re-align will be necessary, but you can always
  1254. force one with \\[org-ctrl-c-ctrl-c].
  1255. If you would like to use the optimized version in Org-mode, but the
  1256. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1257. This variable can be used to turn on and off the table editor during a session,
  1258. but in order to toggle optimization, a restart is required.
  1259. See also the variable `org-table-auto-blank-field'."
  1260. :group 'org-table
  1261. :type '(choice
  1262. (const :tag "off" nil)
  1263. (const :tag "on" t)
  1264. (const :tag "on, optimized" optimized)))
  1265. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1266. (version<= emacs-version "24.1"))
  1267. "Non-nil means cluster self-insert commands for undo when possible.
  1268. If this is set, then, like in the Emacs command loop, 20 consecutive
  1269. characters will be undone together.
  1270. This is configurable, because there is some impact on typing performance."
  1271. :group 'org-table
  1272. :type 'boolean)
  1273. (defcustom org-table-tab-recognizes-table.el t
  1274. "Non-nil means TAB will automatically notice a table.el table.
  1275. When it sees such a table, it moves point into it and - if necessary -
  1276. calls `table-recognize-table'."
  1277. :group 'org-table-editing
  1278. :type 'boolean)
  1279. (defgroup org-link nil
  1280. "Options concerning links in Org-mode."
  1281. :tag "Org Link"
  1282. :group 'org)
  1283. (defvar org-link-abbrev-alist-local nil
  1284. "Buffer-local version of `org-link-abbrev-alist', which see.
  1285. The value of this is taken from the #+LINK lines.")
  1286. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1287. (defcustom org-link-abbrev-alist nil
  1288. "Alist of link abbreviations.
  1289. The car of each element is a string, to be replaced at the start of a link.
  1290. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1291. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1292. [[linkkey:tag][description]]
  1293. The 'linkkey' must be a word word, starting with a letter, followed
  1294. by letters, numbers, '-' or '_'.
  1295. If REPLACE is a string, the tag will simply be appended to create the link.
  1296. If the string contains \"%s\", the tag will be inserted there. If the string
  1297. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1298. at that point (see the function `url-hexify-string'). If the string contains
  1299. the specifier \"%(my-function)\", then the custom function `my-function' will
  1300. be invoked: this function takes the tag as its only argument and must return
  1301. a string.
  1302. REPLACE may also be a function that will be called with the tag as the
  1303. only argument to create the link, which should be returned as a string.
  1304. See the manual for examples."
  1305. :group 'org-link
  1306. :type '(repeat
  1307. (cons
  1308. (string :tag "Protocol")
  1309. (choice
  1310. (string :tag "Format")
  1311. (function)))))
  1312. (defcustom org-descriptive-links t
  1313. "Non-nil means Org will display descriptive links.
  1314. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1315. \"Org Website\", hiding the link itself and just displaying its
  1316. description. When set to `nil', Org will display the full links
  1317. literally.
  1318. You can interactively set the value of this variable by calling
  1319. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1320. :group 'org-link
  1321. :type 'boolean)
  1322. (defcustom org-link-file-path-type 'adaptive
  1323. "How the path name in file links should be stored.
  1324. Valid values are:
  1325. relative Relative to the current directory, i.e. the directory of the file
  1326. into which the link is being inserted.
  1327. absolute Absolute path, if possible with ~ for home directory.
  1328. noabbrev Absolute path, no abbreviation of home directory.
  1329. adaptive Use relative path for files in the current directory and sub-
  1330. directories of it. For other files, use an absolute path."
  1331. :group 'org-link
  1332. :type '(choice
  1333. (const relative)
  1334. (const absolute)
  1335. (const noabbrev)
  1336. (const adaptive)))
  1337. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1338. "Types of links that should be activated in Org-mode files.
  1339. This is a list of symbols, each leading to the activation of a certain link
  1340. type. In principle, it does not hurt to turn on most link types - there may
  1341. be a small gain when turning off unused link types. The types are:
  1342. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1343. angle Links in angular brackets that may contain whitespace like
  1344. <bbdb:Carsten Dominik>.
  1345. plain Plain links in normal text, no whitespace, like http://google.com.
  1346. radio Text that is matched by a radio target, see manual for details.
  1347. tag Tag settings in a headline (link to tag search).
  1348. date Time stamps (link to calendar).
  1349. footnote Footnote labels.
  1350. Changing this variable requires a restart of Emacs to become effective."
  1351. :group 'org-link
  1352. :type '(set :greedy t
  1353. (const :tag "Double bracket links" bracket)
  1354. (const :tag "Angular bracket links" angle)
  1355. (const :tag "Plain text links" plain)
  1356. (const :tag "Radio target matches" radio)
  1357. (const :tag "Tags" tag)
  1358. (const :tag "Timestamps" date)
  1359. (const :tag "Footnotes" footnote)))
  1360. (defcustom org-make-link-description-function nil
  1361. "Function to use for generating link descriptions from links.
  1362. When nil, the link location will be used. This function must take
  1363. two parameters: the first one is the link, the second one is the
  1364. description generated by `org-insert-link'. The function should
  1365. return the description to use."
  1366. :group 'org-link
  1367. :type 'function)
  1368. (defgroup org-link-store nil
  1369. "Options concerning storing links in Org-mode."
  1370. :tag "Org Store Link"
  1371. :group 'org-link)
  1372. (defcustom org-url-hexify-p t
  1373. "When non-nil, hexify URL when creating a link."
  1374. :type 'boolean
  1375. :version "24.3"
  1376. :group 'org-link-store)
  1377. (defcustom org-email-link-description-format "Email %c: %.30s"
  1378. "Format of the description part of a link to an email or usenet message.
  1379. The following %-escapes will be replaced by corresponding information:
  1380. %F full \"From\" field
  1381. %f name, taken from \"From\" field, address if no name
  1382. %T full \"To\" field
  1383. %t first name in \"To\" field, address if no name
  1384. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1385. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1386. %s subject
  1387. %d date
  1388. %m message-id.
  1389. You may use normal field width specification between the % and the letter.
  1390. This is for example useful to limit the length of the subject.
  1391. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1392. :group 'org-link-store
  1393. :type 'string)
  1394. (defcustom org-from-is-user-regexp
  1395. (let (r1 r2)
  1396. (when (and user-mail-address (not (string= user-mail-address "")))
  1397. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1398. (when (and user-full-name (not (string= user-full-name "")))
  1399. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1400. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1401. "Regexp matched against the \"From:\" header of an email or usenet message.
  1402. It should match if the message is from the user him/herself."
  1403. :group 'org-link-store
  1404. :type 'regexp)
  1405. (defcustom org-context-in-file-links t
  1406. "Non-nil means file links from `org-store-link' contain context.
  1407. A search string will be added to the file name with :: as separator and
  1408. used to find the context when the link is activated by the command
  1409. `org-open-at-point'. When this option is t, the entire active region
  1410. will be placed in the search string of the file link. If set to a
  1411. positive integer, only the first n lines of context will be stored.
  1412. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1413. negates this setting for the duration of the command."
  1414. :group 'org-link-store
  1415. :type '(choice boolean integer))
  1416. (defcustom org-keep-stored-link-after-insertion nil
  1417. "Non-nil means keep link in list for entire session.
  1418. The command `org-store-link' adds a link pointing to the current
  1419. location to an internal list. These links accumulate during a session.
  1420. The command `org-insert-link' can be used to insert links into any
  1421. Org-mode file (offering completion for all stored links). When this
  1422. option is nil, every link which has been inserted once using \\[org-insert-link]
  1423. will be removed from the list, to make completing the unused links
  1424. more efficient."
  1425. :group 'org-link-store
  1426. :type 'boolean)
  1427. (defgroup org-link-follow nil
  1428. "Options concerning following links in Org-mode."
  1429. :tag "Org Follow Link"
  1430. :group 'org-link)
  1431. (defcustom org-link-translation-function nil
  1432. "Function to translate links with different syntax to Org syntax.
  1433. This can be used to translate links created for example by the Planner
  1434. or emacs-wiki packages to Org syntax.
  1435. The function must accept two parameters, a TYPE containing the link
  1436. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1437. which is everything after the link protocol. It should return a cons
  1438. with possibly modified values of type and path.
  1439. Org contains a function for this, so if you set this variable to
  1440. `org-translate-link-from-planner', you should be able follow many
  1441. links created by planner."
  1442. :group 'org-link-follow
  1443. :type 'function)
  1444. (defcustom org-follow-link-hook nil
  1445. "Hook that is run after a link has been followed."
  1446. :group 'org-link-follow
  1447. :type 'hook)
  1448. (defcustom org-tab-follows-link nil
  1449. "Non-nil means on links TAB will follow the link.
  1450. Needs to be set before org.el is loaded.
  1451. This really should not be used, it does not make sense, and the
  1452. implementation is bad."
  1453. :group 'org-link-follow
  1454. :type 'boolean)
  1455. (defcustom org-return-follows-link nil
  1456. "Non-nil means on links RET will follow the link.
  1457. In tables, the special behavior of RET has precedence."
  1458. :group 'org-link-follow
  1459. :type 'boolean)
  1460. (defcustom org-mouse-1-follows-link
  1461. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1462. "Non-nil means mouse-1 on a link will follow the link.
  1463. A longer mouse click will still set point. Does not work on XEmacs.
  1464. Needs to be set before org.el is loaded."
  1465. :group 'org-link-follow
  1466. :type 'boolean)
  1467. (defcustom org-mark-ring-length 4
  1468. "Number of different positions to be recorded in the ring.
  1469. Changing this requires a restart of Emacs to work correctly."
  1470. :group 'org-link-follow
  1471. :type 'integer)
  1472. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1473. "Non-nil means internal links in Org files must exactly match a headline.
  1474. When nil, the link search tries to match a phrase with all words
  1475. in the search text."
  1476. :group 'org-link-follow
  1477. :version "24.1"
  1478. :type '(choice
  1479. (const :tag "Use fuzzy text search" nil)
  1480. (const :tag "Match only exact headline" t)
  1481. (const :tag "Match exact headline or query to create it"
  1482. query-to-create)))
  1483. (defcustom org-link-frame-setup
  1484. '((vm . vm-visit-folder-other-frame)
  1485. (vm-imap . vm-visit-imap-folder-other-frame)
  1486. (gnus . org-gnus-no-new-news)
  1487. (file . find-file-other-window)
  1488. (wl . wl-other-frame))
  1489. "Setup the frame configuration for following links.
  1490. When following a link with Emacs, it may often be useful to display
  1491. this link in another window or frame. This variable can be used to
  1492. set this up for the different types of links.
  1493. For VM, use any of
  1494. `vm-visit-folder'
  1495. `vm-visit-folder-other-window'
  1496. `vm-visit-folder-other-frame'
  1497. For Gnus, use any of
  1498. `gnus'
  1499. `gnus-other-frame'
  1500. `org-gnus-no-new-news'
  1501. For FILE, use any of
  1502. `find-file'
  1503. `find-file-other-window'
  1504. `find-file-other-frame'
  1505. For Wanderlust use any of
  1506. `wl'
  1507. `wl-other-frame'
  1508. For the calendar, use the variable `calendar-setup'.
  1509. For BBDB, it is currently only possible to display the matches in
  1510. another window."
  1511. :group 'org-link-follow
  1512. :type '(list
  1513. (cons (const vm)
  1514. (choice
  1515. (const vm-visit-folder)
  1516. (const vm-visit-folder-other-window)
  1517. (const vm-visit-folder-other-frame)))
  1518. (cons (const gnus)
  1519. (choice
  1520. (const gnus)
  1521. (const gnus-other-frame)
  1522. (const org-gnus-no-new-news)))
  1523. (cons (const file)
  1524. (choice
  1525. (const find-file)
  1526. (const find-file-other-window)
  1527. (const find-file-other-frame)))
  1528. (cons (const wl)
  1529. (choice
  1530. (const wl)
  1531. (const wl-other-frame)))))
  1532. (defcustom org-display-internal-link-with-indirect-buffer nil
  1533. "Non-nil means use indirect buffer to display infile links.
  1534. Activating internal links (from one location in a file to another location
  1535. in the same file) normally just jumps to the location. When the link is
  1536. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1537. is displayed in
  1538. another window. When this option is set, the other window actually displays
  1539. an indirect buffer clone of the current buffer, to avoid any visibility
  1540. changes to the current buffer."
  1541. :group 'org-link-follow
  1542. :type 'boolean)
  1543. (defcustom org-open-non-existing-files nil
  1544. "Non-nil means `org-open-file' will open non-existing files.
  1545. When nil, an error will be generated.
  1546. This variable applies only to external applications because they
  1547. might choke on non-existing files. If the link is to a file that
  1548. will be opened in Emacs, the variable is ignored."
  1549. :group 'org-link-follow
  1550. :type 'boolean)
  1551. (defcustom org-open-directory-means-index-dot-org nil
  1552. "Non-nil means a link to a directory really means to index.org.
  1553. When nil, following a directory link will run dired or open a finder/explorer
  1554. window on that directory."
  1555. :group 'org-link-follow
  1556. :type 'boolean)
  1557. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1558. "Function and arguments to call for following mailto links.
  1559. This is a list with the first element being a Lisp function, and the
  1560. remaining elements being arguments to the function. In string arguments,
  1561. %a will be replaced by the address, and %s will be replaced by the subject
  1562. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1563. :group 'org-link-follow
  1564. :type '(choice
  1565. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1566. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1567. (const :tag "message-mail" (message-mail "%a" "%s"))
  1568. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1569. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1570. "Non-nil means ask for confirmation before executing shell links.
  1571. Shell links can be dangerous: just think about a link
  1572. [[shell:rm -rf ~/*][Google Search]]
  1573. This link would show up in your Org-mode document as \"Google Search\",
  1574. but really it would remove your entire home directory.
  1575. Therefore we advise against setting this variable to nil.
  1576. Just change it to `y-or-n-p' if you want to confirm with a
  1577. single keystroke rather than having to type \"yes\"."
  1578. :group 'org-link-follow
  1579. :type '(choice
  1580. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1581. (const :tag "with y-or-n (faster)" y-or-n-p)
  1582. (const :tag "no confirmation (dangerous)" nil)))
  1583. (put 'org-confirm-shell-link-function
  1584. 'safe-local-variable
  1585. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1586. (defcustom org-confirm-shell-link-not-regexp ""
  1587. "A regexp to skip confirmation for shell links."
  1588. :group 'org-link-follow
  1589. :version "24.1"
  1590. :type 'regexp)
  1591. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1592. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1593. Elisp links can be dangerous: just think about a link
  1594. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1595. This link would show up in your Org-mode document as \"Google Search\",
  1596. but really it would remove your entire home directory.
  1597. Therefore we advise against setting this variable to nil.
  1598. Just change it to `y-or-n-p' if you want to confirm with a
  1599. single keystroke rather than having to type \"yes\"."
  1600. :group 'org-link-follow
  1601. :type '(choice
  1602. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1603. (const :tag "with y-or-n (faster)" y-or-n-p)
  1604. (const :tag "no confirmation (dangerous)" nil)))
  1605. (put 'org-confirm-shell-link-function
  1606. 'safe-local-variable
  1607. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1608. (defcustom org-confirm-elisp-link-not-regexp ""
  1609. "A regexp to skip confirmation for Elisp links."
  1610. :group 'org-link-follow
  1611. :version "24.1"
  1612. :type 'regexp)
  1613. (defconst org-file-apps-defaults-gnu
  1614. '((remote . emacs)
  1615. (system . mailcap)
  1616. (t . mailcap))
  1617. "Default file applications on a UNIX or GNU/Linux system.
  1618. See `org-file-apps'.")
  1619. (defconst org-file-apps-defaults-macosx
  1620. '((remote . emacs)
  1621. (t . "open %s")
  1622. (system . "open %s")
  1623. ("ps.gz" . "gv %s")
  1624. ("eps.gz" . "gv %s")
  1625. ("dvi" . "xdvi %s")
  1626. ("fig" . "xfig %s"))
  1627. "Default file applications on a MacOS X system.
  1628. The system \"open\" is known as a default, but we use X11 applications
  1629. for some files for which the OS does not have a good default.
  1630. See `org-file-apps'.")
  1631. (defconst org-file-apps-defaults-windowsnt
  1632. (list
  1633. '(remote . emacs)
  1634. (cons t
  1635. (list (if (featurep 'xemacs)
  1636. 'mswindows-shell-execute
  1637. 'w32-shell-execute)
  1638. "open" 'file))
  1639. (cons 'system
  1640. (list (if (featurep 'xemacs)
  1641. 'mswindows-shell-execute
  1642. 'w32-shell-execute)
  1643. "open" 'file)))
  1644. "Default file applications on a Windows NT system.
  1645. The system \"open\" is used for most files.
  1646. See `org-file-apps'.")
  1647. (defcustom org-file-apps
  1648. '(
  1649. (auto-mode . emacs)
  1650. ("\\.mm\\'" . default)
  1651. ("\\.x?html?\\'" . default)
  1652. ("\\.pdf\\'" . default)
  1653. )
  1654. "External applications for opening `file:path' items in a document.
  1655. Org-mode uses system defaults for different file types, but
  1656. you can use this variable to set the application for a given file
  1657. extension. The entries in this list are cons cells where the car identifies
  1658. files and the cdr the corresponding command. Possible values for the
  1659. file identifier are
  1660. \"string\" A string as a file identifier can be interpreted in different
  1661. ways, depending on its contents:
  1662. - Alphanumeric characters only:
  1663. Match links with this file extension.
  1664. Example: (\"pdf\" . \"evince %s\")
  1665. to open PDFs with evince.
  1666. - Regular expression: Match links where the
  1667. filename matches the regexp. If you want to
  1668. use groups here, use shy groups.
  1669. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1670. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1671. to open *.html and *.xhtml with firefox.
  1672. - Regular expression which contains (non-shy) groups:
  1673. Match links where the whole link, including \"::\", and
  1674. anything after that, matches the regexp.
  1675. In a custom command string, %1, %2, etc. are replaced with
  1676. the parts of the link that were matched by the groups.
  1677. For backwards compatibility, if a command string is given
  1678. that does not use any of the group matches, this case is
  1679. handled identically to the second one (i.e. match against
  1680. file name only).
  1681. In a custom lisp form, you can access the group matches with
  1682. (match-string n link).
  1683. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1684. to open [[file:document.pdf::5]] with evince at page 5.
  1685. `directory' Matches a directory
  1686. `remote' Matches a remote file, accessible through tramp or efs.
  1687. Remote files most likely should be visited through Emacs
  1688. because external applications cannot handle such paths.
  1689. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1690. so all files Emacs knows how to handle. Using this with
  1691. command `emacs' will open most files in Emacs. Beware that this
  1692. will also open html files inside Emacs, unless you add
  1693. (\"html\" . default) to the list as well.
  1694. t Default for files not matched by any of the other options.
  1695. `system' The system command to open files, like `open' on Windows
  1696. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1697. that will be selected if you call `C-c C-o' with a double
  1698. \\[universal-argument] \\[universal-argument] prefix.
  1699. Possible values for the command are:
  1700. `emacs' The file will be visited by the current Emacs process.
  1701. `default' Use the default application for this file type, which is the
  1702. association for t in the list, most likely in the system-specific
  1703. part.
  1704. This can be used to overrule an unwanted setting in the
  1705. system-specific variable.
  1706. `system' Use the system command for opening files, like \"open\".
  1707. This command is specified by the entry whose car is `system'.
  1708. Most likely, the system-specific version of this variable
  1709. does define this command, but you can overrule/replace it
  1710. here.
  1711. string A command to be executed by a shell; %s will be replaced
  1712. by the path to the file.
  1713. sexp A Lisp form which will be evaluated. The file path will
  1714. be available in the Lisp variable `file'.
  1715. For more examples, see the system specific constants
  1716. `org-file-apps-defaults-macosx'
  1717. `org-file-apps-defaults-windowsnt'
  1718. `org-file-apps-defaults-gnu'."
  1719. :group 'org-link-follow
  1720. :type '(repeat
  1721. (cons (choice :value ""
  1722. (string :tag "Extension")
  1723. (const :tag "System command to open files" system)
  1724. (const :tag "Default for unrecognized files" t)
  1725. (const :tag "Remote file" remote)
  1726. (const :tag "Links to a directory" directory)
  1727. (const :tag "Any files that have Emacs modes"
  1728. auto-mode))
  1729. (choice :value ""
  1730. (const :tag "Visit with Emacs" emacs)
  1731. (const :tag "Use default" default)
  1732. (const :tag "Use the system command" system)
  1733. (string :tag "Command")
  1734. (sexp :tag "Lisp form")))))
  1735. (defcustom org-doi-server-url "http://dx.doi.org/"
  1736. "The URL of the DOI server."
  1737. :type 'string
  1738. :version "24.3"
  1739. :group 'org-link-follow)
  1740. (defgroup org-refile nil
  1741. "Options concerning refiling entries in Org-mode."
  1742. :tag "Org Refile"
  1743. :group 'org)
  1744. (defcustom org-directory "~/org"
  1745. "Directory with org files.
  1746. This is just a default location to look for Org files. There is no need
  1747. at all to put your files into this directory. It is only used in the
  1748. following situations:
  1749. 1. When a capture template specifies a target file that is not an
  1750. absolute path. The path will then be interpreted relative to
  1751. `org-directory'
  1752. 2. When a capture note is filed away in an interactive way (when exiting the
  1753. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1754. with `org-directory' as the default path."
  1755. :group 'org-refile
  1756. :group 'org-remember
  1757. :group 'org-capture
  1758. :type 'directory)
  1759. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1760. "Default target for storing notes.
  1761. Used as a fall back file for org-remember.el and org-capture.el, for
  1762. templates that do not specify a target file."
  1763. :group 'org-refile
  1764. :group 'org-remember
  1765. :group 'org-capture
  1766. :type '(choice
  1767. (const :tag "Default from remember-data-file" nil)
  1768. file))
  1769. (defcustom org-goto-interface 'outline
  1770. "The default interface to be used for `org-goto'.
  1771. Allowed values are:
  1772. outline The interface shows an outline of the relevant file
  1773. and the correct heading is found by moving through
  1774. the outline or by searching with incremental search.
  1775. outline-path-completion Headlines in the current buffer are offered via
  1776. completion. This is the interface also used by
  1777. the refile command."
  1778. :group 'org-refile
  1779. :type '(choice
  1780. (const :tag "Outline" outline)
  1781. (const :tag "Outline-path-completion" outline-path-completion)))
  1782. (defcustom org-goto-max-level 5
  1783. "Maximum target level when running `org-goto' with refile interface."
  1784. :group 'org-refile
  1785. :type 'integer)
  1786. (defcustom org-reverse-note-order nil
  1787. "Non-nil means store new notes at the beginning of a file or entry.
  1788. When nil, new notes will be filed to the end of a file or entry.
  1789. This can also be a list with cons cells of regular expressions that
  1790. are matched against file names, and values."
  1791. :group 'org-remember
  1792. :group 'org-capture
  1793. :group 'org-refile
  1794. :type '(choice
  1795. (const :tag "Reverse always" t)
  1796. (const :tag "Reverse never" nil)
  1797. (repeat :tag "By file name regexp"
  1798. (cons regexp boolean))))
  1799. (defcustom org-log-refile nil
  1800. "Information to record when a task is refiled.
  1801. Possible values are:
  1802. nil Don't add anything
  1803. time Add a time stamp to the task
  1804. note Prompt for a note and add it with template `org-log-note-headings'
  1805. This option can also be set with on a per-file-basis with
  1806. #+STARTUP: nologrefile
  1807. #+STARTUP: logrefile
  1808. #+STARTUP: lognoterefile
  1809. You can have local logging settings for a subtree by setting the LOGGING
  1810. property to one or more of these keywords.
  1811. When bulk-refiling from the agenda, the value `note' is forbidden and
  1812. will temporarily be changed to `time'."
  1813. :group 'org-refile
  1814. :group 'org-progress
  1815. :version "24.1"
  1816. :type '(choice
  1817. (const :tag "No logging" nil)
  1818. (const :tag "Record timestamp" time)
  1819. (const :tag "Record timestamp with note." note)))
  1820. (defcustom org-refile-targets nil
  1821. "Targets for refiling entries with \\[org-refile].
  1822. This is a list of cons cells. Each cell contains:
  1823. - a specification of the files to be considered, either a list of files,
  1824. or a symbol whose function or variable value will be used to retrieve
  1825. a file name or a list of file names. If you use `org-agenda-files' for
  1826. that, all agenda files will be scanned for targets. Nil means consider
  1827. headings in the current buffer.
  1828. - A specification of how to find candidate refile targets. This may be
  1829. any of:
  1830. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1831. This tag has to be present in all target headlines, inheritance will
  1832. not be considered.
  1833. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1834. todo keyword.
  1835. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1836. headlines that are refiling targets.
  1837. - a cons cell (:level . N). Any headline of level N is considered a target.
  1838. Note that, when `org-odd-levels-only' is set, level corresponds to
  1839. order in hierarchy, not to the number of stars.
  1840. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1841. Note that, when `org-odd-levels-only' is set, level corresponds to
  1842. order in hierarchy, not to the number of stars.
  1843. Each element of this list generates a set of possible targets.
  1844. The union of these sets is presented (with completion) to
  1845. the user by `org-refile'.
  1846. You can set the variable `org-refile-target-verify-function' to a function
  1847. to verify each headline found by the simple criteria above.
  1848. When this variable is nil, all top-level headlines in the current buffer
  1849. are used, equivalent to the value `((nil . (:level . 1))'."
  1850. :group 'org-refile
  1851. :type '(repeat
  1852. (cons
  1853. (choice :value org-agenda-files
  1854. (const :tag "All agenda files" org-agenda-files)
  1855. (const :tag "Current buffer" nil)
  1856. (function) (variable) (file))
  1857. (choice :tag "Identify target headline by"
  1858. (cons :tag "Specific tag" (const :value :tag) (string))
  1859. (cons :tag "TODO keyword" (const :value :todo) (string))
  1860. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1861. (cons :tag "Level number" (const :value :level) (integer))
  1862. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1863. (defcustom org-refile-target-verify-function nil
  1864. "Function to verify if the headline at point should be a refile target.
  1865. The function will be called without arguments, with point at the
  1866. beginning of the headline. It should return t and leave point
  1867. where it is if the headline is a valid target for refiling.
  1868. If the target should not be selected, the function must return nil.
  1869. In addition to this, it may move point to a place from where the search
  1870. should be continued. For example, the function may decide that the entire
  1871. subtree of the current entry should be excluded and move point to the end
  1872. of the subtree."
  1873. :group 'org-refile
  1874. :type 'function)
  1875. (defcustom org-refile-use-cache nil
  1876. "Non-nil means cache refile targets to speed up the process.
  1877. The cache for a particular file will be updated automatically when
  1878. the buffer has been killed, or when any of the marker used for flagging
  1879. refile targets no longer points at a live buffer.
  1880. If you have added new entries to a buffer that might themselves be targets,
  1881. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1882. find that easier, `C-u C-u C-u C-c C-w'."
  1883. :group 'org-refile
  1884. :version "24.1"
  1885. :type 'boolean)
  1886. (defcustom org-refile-use-outline-path nil
  1887. "Non-nil means provide refile targets as paths.
  1888. So a level 3 headline will be available as level1/level2/level3.
  1889. When the value is `file', also include the file name (without directory)
  1890. into the path. In this case, you can also stop the completion after
  1891. the file name, to get entries inserted as top level in the file.
  1892. When `full-file-path', include the full file path."
  1893. :group 'org-refile
  1894. :type '(choice
  1895. (const :tag "Not" nil)
  1896. (const :tag "Yes" t)
  1897. (const :tag "Start with file name" file)
  1898. (const :tag "Start with full file path" full-file-path)))
  1899. (defcustom org-outline-path-complete-in-steps t
  1900. "Non-nil means complete the outline path in hierarchical steps.
  1901. When Org-mode uses the refile interface to select an outline path
  1902. \(see variable `org-refile-use-outline-path'), the completion of
  1903. the path can be done is a single go, or if can be done in steps down
  1904. the headline hierarchy. Going in steps is probably the best if you
  1905. do not use a special completion package like `ido' or `icicles'.
  1906. However, when using these packages, going in one step can be very
  1907. fast, while still showing the whole path to the entry."
  1908. :group 'org-refile
  1909. :type 'boolean)
  1910. (defcustom org-refile-allow-creating-parent-nodes nil
  1911. "Non-nil means allow to create new nodes as refile targets.
  1912. New nodes are then created by adding \"/new node name\" to the completion
  1913. of an existing node. When the value of this variable is `confirm',
  1914. new node creation must be confirmed by the user (recommended)
  1915. When nil, the completion must match an existing entry.
  1916. Note that, if the new heading is not seen by the criteria
  1917. listed in `org-refile-targets', multiple instances of the same
  1918. heading would be created by trying again to file under the new
  1919. heading."
  1920. :group 'org-refile
  1921. :type '(choice
  1922. (const :tag "Never" nil)
  1923. (const :tag "Always" t)
  1924. (const :tag "Prompt for confirmation" confirm)))
  1925. (defcustom org-refile-active-region-within-subtree nil
  1926. "Non-nil means also refile active region within a subtree.
  1927. By default `org-refile' doesn't allow refiling regions if they
  1928. don't contain a set of subtrees, but it might be convenient to
  1929. do so sometimes: in that case, the first line of the region is
  1930. converted to a headline before refiling."
  1931. :group 'org-refile
  1932. :version "24.1"
  1933. :type 'boolean)
  1934. (defgroup org-todo nil
  1935. "Options concerning TODO items in Org-mode."
  1936. :tag "Org TODO"
  1937. :group 'org)
  1938. (defgroup org-progress nil
  1939. "Options concerning Progress logging in Org-mode."
  1940. :tag "Org Progress"
  1941. :group 'org-time)
  1942. (defvar org-todo-interpretation-widgets
  1943. '((:tag "Sequence (cycling hits every state)" sequence)
  1944. (:tag "Type (cycling directly to DONE)" type))
  1945. "The available interpretation symbols for customizing `org-todo-keywords'.
  1946. Interested libraries should add to this list.")
  1947. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1948. "List of TODO entry keyword sequences and their interpretation.
  1949. \\<org-mode-map>This is a list of sequences.
  1950. Each sequence starts with a symbol, either `sequence' or `type',
  1951. indicating if the keywords should be interpreted as a sequence of
  1952. action steps, or as different types of TODO items. The first
  1953. keywords are states requiring action - these states will select a headline
  1954. for inclusion into the global TODO list Org-mode produces. If one of
  1955. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1956. signify that no further action is necessary. If \"|\" is not found,
  1957. the last keyword is treated as the only DONE state of the sequence.
  1958. The command \\[org-todo] cycles an entry through these states, and one
  1959. additional state where no keyword is present. For details about this
  1960. cycling, see the manual.
  1961. TODO keywords and interpretation can also be set on a per-file basis with
  1962. the special #+SEQ_TODO and #+TYP_TODO lines.
  1963. Each keyword can optionally specify a character for fast state selection
  1964. \(in combination with the variable `org-use-fast-todo-selection')
  1965. and specifiers for state change logging, using the same syntax that
  1966. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1967. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1968. indicates to record a time stamp each time this state is selected.
  1969. Each keyword may also specify if a timestamp or a note should be
  1970. recorded when entering or leaving the state, by adding additional
  1971. characters in the parenthesis after the keyword. This looks like this:
  1972. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1973. record only the time of the state change. With X and Y being either
  1974. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1975. Y when leaving the state if and only if the *target* state does not
  1976. define X. You may omit any of the fast-selection key or X or /Y,
  1977. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1978. For backward compatibility, this variable may also be just a list
  1979. of keywords. In this case the interpretation (sequence or type) will be
  1980. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1981. :group 'org-todo
  1982. :group 'org-keywords
  1983. :type '(choice
  1984. (repeat :tag "Old syntax, just keywords"
  1985. (string :tag "Keyword"))
  1986. (repeat :tag "New syntax"
  1987. (cons
  1988. (choice
  1989. :tag "Interpretation"
  1990. ;;Quick and dirty way to see
  1991. ;;`org-todo-interpretations'. This takes the
  1992. ;;place of item arguments
  1993. :convert-widget
  1994. (lambda (widget)
  1995. (widget-put widget
  1996. :args (mapcar
  1997. #'(lambda (x)
  1998. (widget-convert
  1999. (cons 'const x)))
  2000. org-todo-interpretation-widgets))
  2001. widget))
  2002. (repeat
  2003. (string :tag "Keyword"))))))
  2004. (defvar org-todo-keywords-1 nil
  2005. "All TODO and DONE keywords active in a buffer.")
  2006. (make-variable-buffer-local 'org-todo-keywords-1)
  2007. (defvar org-todo-keywords-for-agenda nil)
  2008. (defvar org-done-keywords-for-agenda nil)
  2009. (defvar org-drawers-for-agenda nil)
  2010. (defvar org-todo-keyword-alist-for-agenda nil)
  2011. (defvar org-tag-alist-for-agenda nil)
  2012. (defvar org-agenda-contributing-files nil)
  2013. (defvar org-not-done-keywords nil)
  2014. (make-variable-buffer-local 'org-not-done-keywords)
  2015. (defvar org-done-keywords nil)
  2016. (make-variable-buffer-local 'org-done-keywords)
  2017. (defvar org-todo-heads nil)
  2018. (make-variable-buffer-local 'org-todo-heads)
  2019. (defvar org-todo-sets nil)
  2020. (make-variable-buffer-local 'org-todo-sets)
  2021. (defvar org-todo-log-states nil)
  2022. (make-variable-buffer-local 'org-todo-log-states)
  2023. (defvar org-todo-kwd-alist nil)
  2024. (make-variable-buffer-local 'org-todo-kwd-alist)
  2025. (defvar org-todo-key-alist nil)
  2026. (make-variable-buffer-local 'org-todo-key-alist)
  2027. (defvar org-todo-key-trigger nil)
  2028. (make-variable-buffer-local 'org-todo-key-trigger)
  2029. (defcustom org-todo-interpretation 'sequence
  2030. "Controls how TODO keywords are interpreted.
  2031. This variable is in principle obsolete and is only used for
  2032. backward compatibility, if the interpretation of todo keywords is
  2033. not given already in `org-todo-keywords'. See that variable for
  2034. more information."
  2035. :group 'org-todo
  2036. :group 'org-keywords
  2037. :type '(choice (const sequence)
  2038. (const type)))
  2039. (defcustom org-use-fast-todo-selection t
  2040. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2041. This variable describes if and under what circumstances the cycling
  2042. mechanism for TODO keywords will be replaced by a single-key, direct
  2043. selection scheme.
  2044. When nil, fast selection is never used.
  2045. When the symbol `prefix', it will be used when `org-todo' is called
  2046. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2047. `C-u t' in an agenda buffer.
  2048. When t, fast selection is used by default. In this case, the prefix
  2049. argument forces cycling instead.
  2050. In all cases, the special interface is only used if access keys have
  2051. actually been assigned by the user, i.e. if keywords in the configuration
  2052. are followed by a letter in parenthesis, like TODO(t)."
  2053. :group 'org-todo
  2054. :type '(choice
  2055. (const :tag "Never" nil)
  2056. (const :tag "By default" t)
  2057. (const :tag "Only with C-u C-c C-t" prefix)))
  2058. (defcustom org-provide-todo-statistics t
  2059. "Non-nil means update todo statistics after insert and toggle.
  2060. ALL-HEADLINES means update todo statistics by including headlines
  2061. with no TODO keyword as well, counting them as not done.
  2062. A list of TODO keywords means the same, but skip keywords that are
  2063. not in this list.
  2064. When this is set, todo statistics is updated in the parent of the
  2065. current entry each time a todo state is changed."
  2066. :group 'org-todo
  2067. :type '(choice
  2068. (const :tag "Yes, only for TODO entries" t)
  2069. (const :tag "Yes, including all entries" 'all-headlines)
  2070. (repeat :tag "Yes, for TODOs in this list"
  2071. (string :tag "TODO keyword"))
  2072. (other :tag "No TODO statistics" nil)))
  2073. (defcustom org-hierarchical-todo-statistics t
  2074. "Non-nil means TODO statistics covers just direct children.
  2075. When nil, all entries in the subtree are considered.
  2076. This has only an effect if `org-provide-todo-statistics' is set.
  2077. To set this to nil for only a single subtree, use a COOKIE_DATA
  2078. property and include the word \"recursive\" into the value."
  2079. :group 'org-todo
  2080. :type 'boolean)
  2081. (defcustom org-after-todo-state-change-hook nil
  2082. "Hook which is run after the state of a TODO item was changed.
  2083. The new state (a string with a TODO keyword, or nil) is available in the
  2084. Lisp variable `org-state'."
  2085. :group 'org-todo
  2086. :type 'hook)
  2087. (defvar org-blocker-hook nil
  2088. "Hook for functions that are allowed to block a state change.
  2089. Functions in this hook should not modify the buffer.
  2090. Each function gets as its single argument a property list,
  2091. see `org-trigger-hook' for more information about this list.
  2092. If any of the functions in this hook returns nil, the state change
  2093. is blocked.")
  2094. (defvar org-trigger-hook nil
  2095. "Hook for functions that are triggered by a state change.
  2096. Each function gets as its single argument a property list with at
  2097. least the following elements:
  2098. (:type type-of-change :position pos-at-entry-start
  2099. :from old-state :to new-state)
  2100. Depending on the type, more properties may be present.
  2101. This mechanism is currently implemented for:
  2102. TODO state changes
  2103. ------------------
  2104. :type todo-state-change
  2105. :from previous state (keyword as a string), or nil, or a symbol
  2106. 'todo' or 'done', to indicate the general type of state.
  2107. :to new state, like in :from")
  2108. (defcustom org-enforce-todo-dependencies nil
  2109. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2110. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2111. be blocked if any prior sibling is not yet done.
  2112. Finally, if the parent is blocked because of ordered siblings of its own,
  2113. the child will also be blocked."
  2114. :set (lambda (var val)
  2115. (set var val)
  2116. (if val
  2117. (add-hook 'org-blocker-hook
  2118. 'org-block-todo-from-children-or-siblings-or-parent)
  2119. (remove-hook 'org-blocker-hook
  2120. 'org-block-todo-from-children-or-siblings-or-parent)))
  2121. :group 'org-todo
  2122. :type 'boolean)
  2123. (defcustom org-enforce-todo-checkbox-dependencies nil
  2124. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2125. When this is nil, checkboxes have no influence on switching TODO states.
  2126. When non-nil, you first need to check off all check boxes before the TODO
  2127. entry can be switched to DONE.
  2128. This variable needs to be set before org.el is loaded, and you need to
  2129. restart Emacs after a change to make the change effective. The only way
  2130. to change is while Emacs is running is through the customize interface."
  2131. :set (lambda (var val)
  2132. (set var val)
  2133. (if val
  2134. (add-hook 'org-blocker-hook
  2135. 'org-block-todo-from-checkboxes)
  2136. (remove-hook 'org-blocker-hook
  2137. 'org-block-todo-from-checkboxes)))
  2138. :group 'org-todo
  2139. :type 'boolean)
  2140. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2141. "Non-nil means inserting a TODO heading is treated as state change.
  2142. So when the command \\[org-insert-todo-heading] is used, state change
  2143. logging will apply if appropriate. When nil, the new TODO item will
  2144. be inserted directly, and no logging will take place."
  2145. :group 'org-todo
  2146. :type 'boolean)
  2147. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2148. "Non-nil means switching TODO states with S-cursor counts as state change.
  2149. This is the default behavior. However, setting this to nil allows a
  2150. convenient way to select a TODO state and bypass any logging associated
  2151. with that."
  2152. :group 'org-todo
  2153. :type 'boolean)
  2154. (defcustom org-todo-state-tags-triggers nil
  2155. "Tag changes that should be triggered by TODO state changes.
  2156. This is a list. Each entry is
  2157. (state-change (tag . flag) .......)
  2158. State-change can be a string with a state, and empty string to indicate the
  2159. state that has no TODO keyword, or it can be one of the symbols `todo'
  2160. or `done', meaning any not-done or done state, respectively."
  2161. :group 'org-todo
  2162. :group 'org-tags
  2163. :type '(repeat
  2164. (cons (choice :tag "When changing to"
  2165. (const :tag "Not-done state" todo)
  2166. (const :tag "Done state" done)
  2167. (string :tag "State"))
  2168. (repeat
  2169. (cons :tag "Tag action"
  2170. (string :tag "Tag")
  2171. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2172. (defcustom org-log-done nil
  2173. "Information to record when a task moves to the DONE state.
  2174. Possible values are:
  2175. nil Don't add anything, just change the keyword
  2176. time Add a time stamp to the task
  2177. note Prompt for a note and add it with template `org-log-note-headings'
  2178. This option can also be set with on a per-file-basis with
  2179. #+STARTUP: nologdone
  2180. #+STARTUP: logdone
  2181. #+STARTUP: lognotedone
  2182. You can have local logging settings for a subtree by setting the LOGGING
  2183. property to one or more of these keywords."
  2184. :group 'org-todo
  2185. :group 'org-progress
  2186. :type '(choice
  2187. (const :tag "No logging" nil)
  2188. (const :tag "Record CLOSED timestamp" time)
  2189. (const :tag "Record CLOSED timestamp with note." note)))
  2190. ;; Normalize old uses of org-log-done.
  2191. (cond
  2192. ((eq org-log-done t) (setq org-log-done 'time))
  2193. ((and (listp org-log-done) (memq 'done org-log-done))
  2194. (setq org-log-done 'note)))
  2195. (defcustom org-log-reschedule nil
  2196. "Information to record when the scheduling date of a tasks is modified.
  2197. Possible values are:
  2198. nil Don't add anything, just change the date
  2199. time Add a time stamp to the task
  2200. note Prompt for a note and add it with template `org-log-note-headings'
  2201. This option can also be set with on a per-file-basis with
  2202. #+STARTUP: nologreschedule
  2203. #+STARTUP: logreschedule
  2204. #+STARTUP: lognotereschedule"
  2205. :group 'org-todo
  2206. :group 'org-progress
  2207. :type '(choice
  2208. (const :tag "No logging" nil)
  2209. (const :tag "Record timestamp" time)
  2210. (const :tag "Record timestamp with note." note)))
  2211. (defcustom org-log-redeadline nil
  2212. "Information to record when the deadline date of a tasks is modified.
  2213. Possible values are:
  2214. nil Don't add anything, just change the date
  2215. time Add a time stamp to the task
  2216. note Prompt for a note and add it with template `org-log-note-headings'
  2217. This option can also be set with on a per-file-basis with
  2218. #+STARTUP: nologredeadline
  2219. #+STARTUP: logredeadline
  2220. #+STARTUP: lognoteredeadline
  2221. You can have local logging settings for a subtree by setting the LOGGING
  2222. property to one or more of these keywords."
  2223. :group 'org-todo
  2224. :group 'org-progress
  2225. :type '(choice
  2226. (const :tag "No logging" nil)
  2227. (const :tag "Record timestamp" time)
  2228. (const :tag "Record timestamp with note." note)))
  2229. (defcustom org-log-note-clock-out nil
  2230. "Non-nil means record a note when clocking out of an item.
  2231. This can also be configured on a per-file basis by adding one of
  2232. the following lines anywhere in the buffer:
  2233. #+STARTUP: lognoteclock-out
  2234. #+STARTUP: nolognoteclock-out"
  2235. :group 'org-todo
  2236. :group 'org-progress
  2237. :type 'boolean)
  2238. (defcustom org-log-done-with-time t
  2239. "Non-nil means the CLOSED time stamp will contain date and time.
  2240. When nil, only the date will be recorded."
  2241. :group 'org-progress
  2242. :type 'boolean)
  2243. (defcustom org-log-note-headings
  2244. '((done . "CLOSING NOTE %t")
  2245. (state . "State %-12s from %-12S %t")
  2246. (note . "Note taken on %t")
  2247. (reschedule . "Rescheduled from %S on %t")
  2248. (delschedule . "Not scheduled, was %S on %t")
  2249. (redeadline . "New deadline from %S on %t")
  2250. (deldeadline . "Removed deadline, was %S on %t")
  2251. (refile . "Refiled on %t")
  2252. (clock-out . ""))
  2253. "Headings for notes added to entries.
  2254. The value is an alist, with the car being a symbol indicating the note
  2255. context, and the cdr is the heading to be used. The heading may also be the
  2256. empty string.
  2257. %t in the heading will be replaced by a time stamp.
  2258. %T will be an active time stamp instead the default inactive one
  2259. %d will be replaced by a short-format time stamp.
  2260. %D will be replaced by an active short-format time stamp.
  2261. %s will be replaced by the new TODO state, in double quotes.
  2262. %S will be replaced by the old TODO state, in double quotes.
  2263. %u will be replaced by the user name.
  2264. %U will be replaced by the full user name.
  2265. In fact, it is not a good idea to change the `state' entry, because
  2266. agenda log mode depends on the format of these entries."
  2267. :group 'org-todo
  2268. :group 'org-progress
  2269. :type '(list :greedy t
  2270. (cons (const :tag "Heading when closing an item" done) string)
  2271. (cons (const :tag
  2272. "Heading when changing todo state (todo sequence only)"
  2273. state) string)
  2274. (cons (const :tag "Heading when just taking a note" note) string)
  2275. (cons (const :tag "Heading when clocking out" clock-out) string)
  2276. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2277. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2278. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2279. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2280. (cons (const :tag "Heading when refiling" refile) string)))
  2281. (unless (assq 'note org-log-note-headings)
  2282. (push '(note . "%t") org-log-note-headings))
  2283. (defcustom org-log-into-drawer nil
  2284. "Non-nil means insert state change notes and time stamps into a drawer.
  2285. When nil, state changes notes will be inserted after the headline and
  2286. any scheduling and clock lines, but not inside a drawer.
  2287. The value of this variable should be the name of the drawer to use.
  2288. LOGBOOK is proposed as the default drawer for this purpose, you can
  2289. also set this to a string to define the drawer of your choice.
  2290. A value of t is also allowed, representing \"LOGBOOK\".
  2291. A value of t or nil can also be set with on a per-file-basis with
  2292. #+STARTUP: logdrawer
  2293. #+STARTUP: nologdrawer
  2294. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2295. will be ignored.
  2296. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2297. a subtree."
  2298. :group 'org-todo
  2299. :group 'org-progress
  2300. :type '(choice
  2301. (const :tag "Not into a drawer" nil)
  2302. (const :tag "LOGBOOK" t)
  2303. (string :tag "Other")))
  2304. (if (fboundp 'defvaralias)
  2305. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2306. (defun org-log-into-drawer ()
  2307. "Return the value of `org-log-into-drawer', but let properties overrule.
  2308. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2309. used instead of the default value."
  2310. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2311. (cond
  2312. ((not p) org-log-into-drawer)
  2313. ((equal p "nil") nil)
  2314. ((equal p "t") "LOGBOOK")
  2315. (t p))))
  2316. (defcustom org-log-state-notes-insert-after-drawers nil
  2317. "Non-nil means insert state change notes after any drawers in entry.
  2318. Only the drawers that *immediately* follow the headline and the
  2319. deadline/scheduled line are skipped.
  2320. When nil, insert notes right after the heading and perhaps the line
  2321. with deadline/scheduling if present.
  2322. This variable will have no effect if `org-log-into-drawer' is
  2323. set."
  2324. :group 'org-todo
  2325. :group 'org-progress
  2326. :type 'boolean)
  2327. (defcustom org-log-states-order-reversed t
  2328. "Non-nil means the latest state note will be directly after heading.
  2329. When nil, the state change notes will be ordered according to time.
  2330. This option can also be set with on a per-file-basis with
  2331. #+STARTUP: logstatesreversed
  2332. #+STARTUP: nologstatesreversed"
  2333. :group 'org-todo
  2334. :group 'org-progress
  2335. :type 'boolean)
  2336. (defcustom org-todo-repeat-to-state nil
  2337. "The TODO state to which a repeater should return the repeating task.
  2338. By default this is the first task in a TODO sequence, or the previous state
  2339. in a TODO_TYP set. But you can specify another task here.
  2340. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2341. :group 'org-todo
  2342. :version "24.1"
  2343. :type '(choice (const :tag "Head of sequence" nil)
  2344. (string :tag "Specific state")))
  2345. (defcustom org-log-repeat 'time
  2346. "Non-nil means record moving through the DONE state when triggering repeat.
  2347. An auto-repeating task is immediately switched back to TODO when
  2348. marked DONE. If you are not logging state changes (by adding \"@\"
  2349. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2350. record a closing note, there will be no record of the task moving
  2351. through DONE. This variable forces taking a note anyway.
  2352. nil Don't force a record
  2353. time Record a time stamp
  2354. note Prompt for a note and add it with template `org-log-note-headings'
  2355. This option can also be set with on a per-file-basis with
  2356. #+STARTUP: nologrepeat
  2357. #+STARTUP: logrepeat
  2358. #+STARTUP: lognoterepeat
  2359. You can have local logging settings for a subtree by setting the LOGGING
  2360. property to one or more of these keywords."
  2361. :group 'org-todo
  2362. :group 'org-progress
  2363. :type '(choice
  2364. (const :tag "Don't force a record" nil)
  2365. (const :tag "Force recording the DONE state" time)
  2366. (const :tag "Force recording a note with the DONE state" note)))
  2367. (defgroup org-priorities nil
  2368. "Priorities in Org-mode."
  2369. :tag "Org Priorities"
  2370. :group 'org-todo)
  2371. (defcustom org-enable-priority-commands t
  2372. "Non-nil means priority commands are active.
  2373. When nil, these commands will be disabled, so that you never accidentally
  2374. set a priority."
  2375. :group 'org-priorities
  2376. :type 'boolean)
  2377. (defcustom org-highest-priority ?A
  2378. "The highest priority of TODO items. A character like ?A, ?B etc.
  2379. Must have a smaller ASCII number than `org-lowest-priority'."
  2380. :group 'org-priorities
  2381. :type 'character)
  2382. (defcustom org-lowest-priority ?C
  2383. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2384. Must have a larger ASCII number than `org-highest-priority'."
  2385. :group 'org-priorities
  2386. :type 'character)
  2387. (defcustom org-default-priority ?B
  2388. "The default priority of TODO items.
  2389. This is the priority an item gets if no explicit priority is given.
  2390. When starting to cycle on an empty priority the first step in the cycle
  2391. depends on `org-priority-start-cycle-with-default'. The resulting first
  2392. step priority must not exceed the range from `org-highest-priority' to
  2393. `org-lowest-priority' which means that `org-default-priority' has to be
  2394. in this range exclusive or inclusive the range boundaries. Else the
  2395. first step refuses to set the default and the second will fall back
  2396. to (depending on the command used) the highest or lowest priority."
  2397. :group 'org-priorities
  2398. :type 'character)
  2399. (defcustom org-priority-start-cycle-with-default t
  2400. "Non-nil means start with default priority when starting to cycle.
  2401. When this is nil, the first step in the cycle will be (depending on the
  2402. command used) one higher or lower than the default priority.
  2403. See also `org-default-priority'."
  2404. :group 'org-priorities
  2405. :type 'boolean)
  2406. (defcustom org-get-priority-function nil
  2407. "Function to extract the priority from a string.
  2408. The string is normally the headline. If this is nil Org computes the
  2409. priority from the priority cookie like [#A] in the headline. It returns
  2410. an integer, increasing by 1000 for each priority level.
  2411. The user can set a different function here, which should take a string
  2412. as an argument and return the numeric priority."
  2413. :group 'org-priorities
  2414. :version "24.1"
  2415. :type 'function)
  2416. (defgroup org-time nil
  2417. "Options concerning time stamps and deadlines in Org-mode."
  2418. :tag "Org Time"
  2419. :group 'org)
  2420. (defcustom org-insert-labeled-timestamps-at-point nil
  2421. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2422. When nil, these labeled time stamps are forces into the second line of an
  2423. entry, just after the headline. When scheduling from the global TODO list,
  2424. the time stamp will always be forced into the second line."
  2425. :group 'org-time
  2426. :type 'boolean)
  2427. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2428. "Formats for `format-time-string' which are used for time stamps.
  2429. It is not recommended to change this constant.")
  2430. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2431. "Number of minutes to round time stamps to.
  2432. These are two values, the first applies when first creating a time stamp.
  2433. The second applies when changing it with the commands `S-up' and `S-down'.
  2434. When changing the time stamp, this means that it will change in steps
  2435. of N minutes, as given by the second value.
  2436. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2437. numbers should be factors of 60, so for example 5, 10, 15.
  2438. When this is larger than 1, you can still force an exact time stamp by using
  2439. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2440. and by using a prefix arg to `S-up/down' to specify the exact number
  2441. of minutes to shift."
  2442. :group 'org-time
  2443. :get #'(lambda (var) ; Make sure both elements are there
  2444. (if (integerp (default-value var))
  2445. (list (default-value var) 5)
  2446. (default-value var)))
  2447. :type '(list
  2448. (integer :tag "when inserting times")
  2449. (integer :tag "when modifying times")))
  2450. ;; Normalize old customizations of this variable.
  2451. (when (integerp org-time-stamp-rounding-minutes)
  2452. (setq org-time-stamp-rounding-minutes
  2453. (list org-time-stamp-rounding-minutes
  2454. org-time-stamp-rounding-minutes)))
  2455. (defcustom org-display-custom-times nil
  2456. "Non-nil means overlay custom formats over all time stamps.
  2457. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2458. To turn this on on a per-file basis, insert anywhere in the file:
  2459. #+STARTUP: customtime"
  2460. :group 'org-time
  2461. :set 'set-default
  2462. :type 'sexp)
  2463. (make-variable-buffer-local 'org-display-custom-times)
  2464. (defcustom org-time-stamp-custom-formats
  2465. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2466. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2467. These are overlaid over the default ISO format if the variable
  2468. `org-display-custom-times' is set. Time like %H:%M should be at the
  2469. end of the second format. The custom formats are also honored by export
  2470. commands, if custom time display is turned on at the time of export."
  2471. :group 'org-time
  2472. :type 'sexp)
  2473. (defun org-time-stamp-format (&optional long inactive)
  2474. "Get the right format for a time string."
  2475. (let ((f (if long (cdr org-time-stamp-formats)
  2476. (car org-time-stamp-formats))))
  2477. (if inactive
  2478. (concat "[" (substring f 1 -1) "]")
  2479. f)))
  2480. (defcustom org-time-clocksum-format
  2481. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2482. "The format string used when creating CLOCKSUM lines.
  2483. This is also used when Org mode generates a time duration.
  2484. The value can be a single format string containing two
  2485. %-sequences, which will be filled with the number of hours and
  2486. minutes in that order.
  2487. Alternatively, the value can be a plist associating any of the
  2488. keys :years, :months, :weeks, :days, :hours or :minutes with
  2489. format strings. The time duration is formatted using only the
  2490. time components that are needed and concatenating the results.
  2491. If a time unit in absent, it falls back to the next smallest
  2492. unit.
  2493. The keys :require-years, :require-months, :require-days,
  2494. :require-weeks, :require-hours, :require-minutes are also
  2495. meaningful. A non-nil value for these keys indicates that the
  2496. corresponding time component should always be included, even if
  2497. its value is 0.
  2498. For example,
  2499. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2500. :require-minutes t)
  2501. means durations longer than a day will be expressed in days,
  2502. hours and minutes, and durations less than a day will always be
  2503. expressed in hours and minutes (even for durations less than an
  2504. hour).
  2505. The value
  2506. \(:days \"%dd\" :minutes \"%dm\")
  2507. means durations longer than a day will be expressed in days and
  2508. minutes, and durations less than a day will be expressed entirely
  2509. in minutes (even for durations longer than an hour)."
  2510. :group 'org-time
  2511. :group 'org-clock
  2512. :version "24.3"
  2513. :type '(choice (string :tag "Format string")
  2514. (set :tag "Plist"
  2515. (group :inline t (const :tag "Years" :years)
  2516. (string :tag "Format string"))
  2517. (group :inline t
  2518. (const :tag "Always show years" :require-years)
  2519. (const t))
  2520. (group :inline t (const :tag "Months" :months)
  2521. (string :tag "Format string"))
  2522. (group :inline t
  2523. (const :tag "Always show months" :require-months)
  2524. (const t))
  2525. (group :inline t (const :tag "Weeks" :weeks)
  2526. (string :tag "Format string"))
  2527. (group :inline t
  2528. (const :tag "Always show weeks" :require-weeks)
  2529. (const t))
  2530. (group :inline t (const :tag "Days" :days)
  2531. (string :tag "Format string"))
  2532. (group :inline t
  2533. (const :tag "Always show days" :require-days)
  2534. (const t))
  2535. (group :inline t (const :tag "Hours" :hours)
  2536. (string :tag "Format string"))
  2537. (group :inline t
  2538. (const :tag "Always show hours" :require-hours)
  2539. (const t))
  2540. (group :inline t (const :tag "Minutes" :minutes)
  2541. (string :tag "Format string"))
  2542. (group :inline t
  2543. (const :tag "Always show minutes" :require-minutes)
  2544. (const t)))))
  2545. (defcustom org-time-clocksum-use-fractional nil
  2546. "When non-nil, \\[org-clock-display] uses fractional times.
  2547. See `org-time-clocksum-format' for more on time clock formats."
  2548. :group 'org-time
  2549. :group 'org-clock
  2550. :version "24.3"
  2551. :type 'boolean)
  2552. (defcustom org-time-clocksum-use-effort-durations t
  2553. "When non-nil, \\[org-clock-display] uses effort durations.
  2554. E.g. by default, one day is considered to be a 8 hours effort,
  2555. so a task that has been clocked for 16 hours will be displayed
  2556. as during 2 days in the clock display or in the clocktable.
  2557. See `org-effort-durations' on how to set effort durations
  2558. and `org-time-clocksum-format' for more on time clock formats."
  2559. :group 'org-time
  2560. :group 'org-clock
  2561. :version "24.3"
  2562. :type 'boolean)
  2563. (defcustom org-time-clocksum-fractional-format "%.2f"
  2564. "The format string used when creating CLOCKSUM lines,
  2565. or when Org mode generates a time duration, if
  2566. `org-time-clocksum-use-fractional' is enabled.
  2567. The value can be a single format string containing one
  2568. %-sequence, which will be filled with the number of hours as
  2569. a float.
  2570. Alternatively, the value can be a plist associating any of the
  2571. keys :years, :months, :weeks, :days, :hours or :minutes with
  2572. a format string. The time duration is formatted using the
  2573. largest time unit which gives a non-zero integer part. If all
  2574. specified formats have zero integer part, the smallest time unit
  2575. is used."
  2576. :group 'org-time
  2577. :type '(choice (string :tag "Format string")
  2578. (set (group :inline t (const :tag "Years" :years)
  2579. (string :tag "Format string"))
  2580. (group :inline t (const :tag "Months" :months)
  2581. (string :tag "Format string"))
  2582. (group :inline t (const :tag "Weeks" :weeks)
  2583. (string :tag "Format string"))
  2584. (group :inline t (const :tag "Days" :days)
  2585. (string :tag "Format string"))
  2586. (group :inline t (const :tag "Hours" :hours)
  2587. (string :tag "Format string"))
  2588. (group :inline t (const :tag "Minutes" :minutes)
  2589. (string :tag "Format string")))))
  2590. (defcustom org-deadline-warning-days 14
  2591. "Number of days before expiration during which a deadline becomes active.
  2592. This variable governs the display in sparse trees and in the agenda.
  2593. When 0 or negative, it means use this number (the absolute value of it)
  2594. even if a deadline has a different individual lead time specified.
  2595. Custom commands can set this variable in the options section."
  2596. :group 'org-time
  2597. :group 'org-agenda-daily/weekly
  2598. :type 'integer)
  2599. (defcustom org-scheduled-delay-days 0
  2600. "Number of days before a scheduled item becomes active.
  2601. This variable governs the display in sparse trees and in the agenda.
  2602. The default value (i.e. 0) means: don't delay scheduled item.
  2603. When negative, it means use this number (the absolute value of it)
  2604. even if a scheduled item has a different individual delay time
  2605. specified.
  2606. Custom commands can set this variable in the options section."
  2607. :group 'org-time
  2608. :group 'org-agenda-daily/weekly
  2609. :version "24.3"
  2610. :type 'integer)
  2611. (defcustom org-read-date-prefer-future t
  2612. "Non-nil means assume future for incomplete date input from user.
  2613. This affects the following situations:
  2614. 1. The user gives a month but not a year.
  2615. For example, if it is April and you enter \"feb 2\", this will be read
  2616. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2617. 2. The user gives a day, but no month.
  2618. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2619. read this as the third of *next* month. However, if you enter \"17\",
  2620. it will be considered as *this* month.
  2621. If you set this variable to the symbol `time', then also the following
  2622. will work:
  2623. 3. If the user gives a time.
  2624. If the time is before now, it will be interpreted as tomorrow.
  2625. Currently none of this works for ISO week specifications.
  2626. When this option is nil, the current day, month and year will always be
  2627. used as defaults.
  2628. See also `org-agenda-jump-prefer-future'."
  2629. :group 'org-time
  2630. :type '(choice
  2631. (const :tag "Never" nil)
  2632. (const :tag "Check month and day" t)
  2633. (const :tag "Check month, day, and time" time)))
  2634. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2635. "Should the agenda jump command prefer the future for incomplete dates?
  2636. The default is to do the same as configured in `org-read-date-prefer-future'.
  2637. But you can also set a deviating value here.
  2638. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2639. :group 'org-agenda
  2640. :group 'org-time
  2641. :version "24.1"
  2642. :type '(choice
  2643. (const :tag "Use org-read-date-prefer-future"
  2644. org-read-date-prefer-future)
  2645. (const :tag "Never" nil)
  2646. (const :tag "Always" t)))
  2647. (defcustom org-read-date-force-compatible-dates t
  2648. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2649. Depending on the system Emacs is running on, certain dates cannot
  2650. be represented with the type used internally to represent time.
  2651. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2652. correctly. Some systems allow for earlier dates, some for later,
  2653. some for both. One way to find out it to insert any date into an
  2654. Org buffer, putting the cursor on the year and hitting S-up and
  2655. S-down to test the range.
  2656. When this variable is set to t, the date/time prompt will not let
  2657. you specify dates outside the 1970-2037 range, so it is certain that
  2658. these dates will work in whatever version of Emacs you are
  2659. running, and also that you can move a file from one Emacs implementation
  2660. to another. WHenever Org is forcing the year for you, it will display
  2661. a message and beep.
  2662. When this variable is nil, Org will check if the date is
  2663. representable in the specific Emacs implementation you are using.
  2664. If not, it will force a year, usually the current year, and beep
  2665. to remind you. Currently this setting is not recommended because
  2666. the likelihood that you will open your Org files in an Emacs that
  2667. has limited date range is not negligible.
  2668. A workaround for this problem is to use diary sexp dates for time
  2669. stamps outside of this range."
  2670. :group 'org-time
  2671. :version "24.1"
  2672. :type 'boolean)
  2673. (defcustom org-read-date-display-live t
  2674. "Non-nil means display current interpretation of date prompt live.
  2675. This display will be in an overlay, in the minibuffer."
  2676. :group 'org-time
  2677. :type 'boolean)
  2678. (defcustom org-read-date-popup-calendar t
  2679. "Non-nil means pop up a calendar when prompting for a date.
  2680. In the calendar, the date can be selected with mouse-1. However, the
  2681. minibuffer will also be active, and you can simply enter the date as well.
  2682. When nil, only the minibuffer will be available."
  2683. :group 'org-time
  2684. :type 'boolean)
  2685. (if (fboundp 'defvaralias)
  2686. (defvaralias 'org-popup-calendar-for-date-prompt
  2687. 'org-read-date-popup-calendar))
  2688. (make-obsolete-variable
  2689. 'org-read-date-minibuffer-setup-hook
  2690. "Set `org-read-date-minibuffer-local-map' instead." "24.3")
  2691. (defcustom org-read-date-minibuffer-setup-hook nil
  2692. "Hook to be used to set up keys for the date/time interface.
  2693. Add key definitions to `minibuffer-local-map', which will be a
  2694. temporary copy.
  2695. WARNING: This option is obsolete, you should use
  2696. `org-read-date-minibuffer-local-map' to set up keys."
  2697. :group 'org-time
  2698. :type 'hook)
  2699. (defcustom org-extend-today-until 0
  2700. "The hour when your day really ends. Must be an integer.
  2701. This has influence for the following applications:
  2702. - When switching the agenda to \"today\". It it is still earlier than
  2703. the time given here, the day recognized as TODAY is actually yesterday.
  2704. - When a date is read from the user and it is still before the time given
  2705. here, the current date and time will be assumed to be yesterday, 23:59.
  2706. Also, timestamps inserted in capture templates follow this rule.
  2707. IMPORTANT: This is a feature whose implementation is and likely will
  2708. remain incomplete. Really, it is only here because past midnight seems to
  2709. be the favorite working time of John Wiegley :-)"
  2710. :group 'org-time
  2711. :type 'integer)
  2712. (defcustom org-use-effective-time nil
  2713. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2714. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2715. \"effective time\" of any timestamps between midnight and 8am will be
  2716. 23:59 of the previous day."
  2717. :group 'org-time
  2718. :version "24.1"
  2719. :type 'boolean)
  2720. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2721. "When non-nil, use the last clock out time for `org-todo'.
  2722. Note that this option has precedence over the combined use of
  2723. `org-use-effective-time' and `org-extend-today-until'."
  2724. :group 'org-time
  2725. ;; :version "24.3"
  2726. :type 'boolean)
  2727. (defcustom org-edit-timestamp-down-means-later nil
  2728. "Non-nil means S-down will increase the time in a time stamp.
  2729. When nil, S-up will increase."
  2730. :group 'org-time
  2731. :type 'boolean)
  2732. (defcustom org-calendar-follow-timestamp-change t
  2733. "Non-nil means make the calendar window follow timestamp changes.
  2734. When a timestamp is modified and the calendar window is visible, it will be
  2735. moved to the new date."
  2736. :group 'org-time
  2737. :type 'boolean)
  2738. (defgroup org-tags nil
  2739. "Options concerning tags in Org-mode."
  2740. :tag "Org Tags"
  2741. :group 'org)
  2742. (defcustom org-tag-alist nil
  2743. "List of tags allowed in Org-mode files.
  2744. When this list is nil, Org-mode will base TAG input on what is already in the
  2745. buffer.
  2746. The value of this variable is an alist, the car of each entry must be a
  2747. keyword as a string, the cdr may be a character that is used to select
  2748. that tag through the fast-tag-selection interface.
  2749. See the manual for details."
  2750. :group 'org-tags
  2751. :type '(repeat
  2752. (choice
  2753. (cons (string :tag "Tag name")
  2754. (character :tag "Access char"))
  2755. (list :tag "Start radio group"
  2756. (const :startgroup)
  2757. (option (string :tag "Group description")))
  2758. (list :tag "End radio group"
  2759. (const :endgroup)
  2760. (option (string :tag "Group description")))
  2761. (const :tag "New line" (:newline)))))
  2762. (defcustom org-tag-persistent-alist nil
  2763. "List of tags that will always appear in all Org-mode files.
  2764. This is in addition to any in buffer settings or customizations
  2765. of `org-tag-alist'.
  2766. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2767. The value of this variable is an alist, the car of each entry must be a
  2768. keyword as a string, the cdr may be a character that is used to select
  2769. that tag through the fast-tag-selection interface.
  2770. See the manual for details.
  2771. To disable these tags on a per-file basis, insert anywhere in the file:
  2772. #+STARTUP: noptag"
  2773. :group 'org-tags
  2774. :type '(repeat
  2775. (choice
  2776. (cons (string :tag "Tag name")
  2777. (character :tag "Access char"))
  2778. (const :tag "Start radio group" (:startgroup))
  2779. (const :tag "End radio group" (:endgroup))
  2780. (const :tag "New line" (:newline)))))
  2781. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2782. "If non-nil, always offer completion for all tags of all agenda files.
  2783. Instead of customizing this variable directly, you might want to
  2784. set it locally for capture buffers, because there no list of
  2785. tags in that file can be created dynamically (there are none).
  2786. (add-hook 'org-capture-mode-hook
  2787. (lambda ()
  2788. (set (make-local-variable
  2789. 'org-complete-tags-always-offer-all-agenda-tags)
  2790. t)))"
  2791. :group 'org-tags
  2792. :version "24.1"
  2793. :type 'boolean)
  2794. (defvar org-file-tags nil
  2795. "List of tags that can be inherited by all entries in the file.
  2796. The tags will be inherited if the variable `org-use-tag-inheritance'
  2797. says they should be.
  2798. This variable is populated from #+FILETAGS lines.")
  2799. (defcustom org-use-fast-tag-selection 'auto
  2800. "Non-nil means use fast tag selection scheme.
  2801. This is a special interface to select and deselect tags with single keys.
  2802. When nil, fast selection is never used.
  2803. When the symbol `auto', fast selection is used if and only if selection
  2804. characters for tags have been configured, either through the variable
  2805. `org-tag-alist' or through a #+TAGS line in the buffer.
  2806. When t, fast selection is always used and selection keys are assigned
  2807. automatically if necessary."
  2808. :group 'org-tags
  2809. :type '(choice
  2810. (const :tag "Always" t)
  2811. (const :tag "Never" nil)
  2812. (const :tag "When selection characters are configured" 'auto)))
  2813. (defcustom org-fast-tag-selection-single-key nil
  2814. "Non-nil means fast tag selection exits after first change.
  2815. When nil, you have to press RET to exit it.
  2816. During fast tag selection, you can toggle this flag with `C-c'.
  2817. This variable can also have the value `expert'. In this case, the window
  2818. displaying the tags menu is not even shown, until you press C-c again."
  2819. :group 'org-tags
  2820. :type '(choice
  2821. (const :tag "No" nil)
  2822. (const :tag "Yes" t)
  2823. (const :tag "Expert" expert)))
  2824. (defvar org-fast-tag-selection-include-todo nil
  2825. "Non-nil means fast tags selection interface will also offer TODO states.
  2826. This is an undocumented feature, you should not rely on it.")
  2827. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2828. "The column to which tags should be indented in a headline.
  2829. If this number is positive, it specifies the column. If it is negative,
  2830. it means that the tags should be flushright to that column. For example,
  2831. -80 works well for a normal 80 character screen.
  2832. When 0, place tags directly after headline text, with only one space in
  2833. between."
  2834. :group 'org-tags
  2835. :type 'integer)
  2836. (defcustom org-auto-align-tags t
  2837. "Non-nil keeps tags aligned when modifying headlines.
  2838. Some operations (i.e. demoting) change the length of a headline and
  2839. therefore shift the tags around. With this option turned on, after
  2840. each such operation the tags are again aligned to `org-tags-column'."
  2841. :group 'org-tags
  2842. :type 'boolean)
  2843. (defcustom org-use-tag-inheritance t
  2844. "Non-nil means tags in levels apply also for sublevels.
  2845. When nil, only the tags directly given in a specific line apply there.
  2846. This may also be a list of tags that should be inherited, or a regexp that
  2847. matches tags that should be inherited. Additional control is possible
  2848. with the variable `org-tags-exclude-from-inheritance' which gives an
  2849. explicit list of tags to be excluded from inheritance, even if the value of
  2850. `org-use-tag-inheritance' would select it for inheritance.
  2851. If this option is t, a match early-on in a tree can lead to a large
  2852. number of matches in the subtree when constructing the agenda or creating
  2853. a sparse tree. If you only want to see the first match in a tree during
  2854. a search, check out the variable `org-tags-match-list-sublevels'."
  2855. :group 'org-tags
  2856. :type '(choice
  2857. (const :tag "Not" nil)
  2858. (const :tag "Always" t)
  2859. (repeat :tag "Specific tags" (string :tag "Tag"))
  2860. (regexp :tag "Tags matched by regexp")))
  2861. (defcustom org-tags-exclude-from-inheritance nil
  2862. "List of tags that should never be inherited.
  2863. This is a way to exclude a few tags from inheritance. For way to do
  2864. the opposite, to actively allow inheritance for selected tags,
  2865. see the variable `org-use-tag-inheritance'."
  2866. :group 'org-tags
  2867. :type '(repeat (string :tag "Tag")))
  2868. (defun org-tag-inherit-p (tag)
  2869. "Check if TAG is one that should be inherited."
  2870. (cond
  2871. ((member tag org-tags-exclude-from-inheritance) nil)
  2872. ((eq org-use-tag-inheritance t) t)
  2873. ((not org-use-tag-inheritance) nil)
  2874. ((stringp org-use-tag-inheritance)
  2875. (string-match org-use-tag-inheritance tag))
  2876. ((listp org-use-tag-inheritance)
  2877. (member tag org-use-tag-inheritance))
  2878. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2879. (defcustom org-tags-match-list-sublevels t
  2880. "Non-nil means list also sublevels of headlines matching a search.
  2881. This variable applies to tags/property searches, and also to stuck
  2882. projects because this search is based on a tags match as well.
  2883. When set to the symbol `indented', sublevels are indented with
  2884. leading dots.
  2885. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2886. the sublevels of a headline matching a tag search often also match
  2887. the same search. Listing all of them can create very long lists.
  2888. Setting this variable to nil causes subtrees of a match to be skipped.
  2889. This variable is semi-obsolete and probably should always be true. It
  2890. is better to limit inheritance to certain tags using the variables
  2891. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2892. :group 'org-tags
  2893. :type '(choice
  2894. (const :tag "No, don't list them" nil)
  2895. (const :tag "Yes, do list them" t)
  2896. (const :tag "List them, indented with leading dots" indented)))
  2897. (defcustom org-tags-sort-function nil
  2898. "When set, tags are sorted using this function as a comparator."
  2899. :group 'org-tags
  2900. :type '(choice
  2901. (const :tag "No sorting" nil)
  2902. (const :tag "Alphabetical" string<)
  2903. (const :tag "Reverse alphabetical" string>)
  2904. (function :tag "Custom function" nil)))
  2905. (defvar org-tags-history nil
  2906. "History of minibuffer reads for tags.")
  2907. (defvar org-last-tags-completion-table nil
  2908. "The last used completion table for tags.")
  2909. (defvar org-after-tags-change-hook nil
  2910. "Hook that is run after the tags in a line have changed.")
  2911. (defgroup org-properties nil
  2912. "Options concerning properties in Org-mode."
  2913. :tag "Org Properties"
  2914. :group 'org)
  2915. (defcustom org-property-format "%-10s %s"
  2916. "How property key/value pairs should be formatted by `indent-line'.
  2917. When `indent-line' hits a property definition, it will format the line
  2918. according to this format, mainly to make sure that the values are
  2919. lined-up with respect to each other."
  2920. :group 'org-properties
  2921. :type 'string)
  2922. (defcustom org-properties-postprocess-alist nil
  2923. "Alist of properties and functions to adjust inserted values.
  2924. Elements of this alist must be of the form
  2925. ([string] [function])
  2926. where [string] must be a property name and [function] must be a
  2927. lambda expression: this lambda expression must take one argument,
  2928. the value to adjust, and return the new value as a string.
  2929. For example, this element will allow the property \"Remaining\"
  2930. to be updated wrt the relation between the \"Effort\" property
  2931. and the clock summary:
  2932. ((\"Remaining\" (lambda(value)
  2933. (let ((clocksum (org-clock-sum-current-item))
  2934. (effort (org-duration-string-to-minutes
  2935. (org-entry-get (point) \"Effort\"))))
  2936. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2937. :group 'org-properties
  2938. :version "24.1"
  2939. :type '(alist :key-type (string :tag "Property")
  2940. :value-type (function :tag "Function")))
  2941. (defcustom org-use-property-inheritance nil
  2942. "Non-nil means properties apply also for sublevels.
  2943. This setting is chiefly used during property searches. Turning it on can
  2944. cause significant overhead when doing a search, which is why it is not
  2945. on by default.
  2946. When nil, only the properties directly given in the current entry count.
  2947. When t, every property is inherited. The value may also be a list of
  2948. properties that should have inheritance, or a regular expression matching
  2949. properties that should be inherited.
  2950. However, note that some special properties use inheritance under special
  2951. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2952. and the properties ending in \"_ALL\" when they are used as descriptor
  2953. for valid values of a property.
  2954. Note for programmers:
  2955. When querying an entry with `org-entry-get', you can control if inheritance
  2956. should be used. By default, `org-entry-get' looks only at the local
  2957. properties. You can request inheritance by setting the inherit argument
  2958. to t (to force inheritance) or to `selective' (to respect the setting
  2959. in this variable)."
  2960. :group 'org-properties
  2961. :type '(choice
  2962. (const :tag "Not" nil)
  2963. (const :tag "Always" t)
  2964. (repeat :tag "Specific properties" (string :tag "Property"))
  2965. (regexp :tag "Properties matched by regexp")))
  2966. (defun org-property-inherit-p (property)
  2967. "Check if PROPERTY is one that should be inherited."
  2968. (cond
  2969. ((eq org-use-property-inheritance t) t)
  2970. ((not org-use-property-inheritance) nil)
  2971. ((stringp org-use-property-inheritance)
  2972. (string-match org-use-property-inheritance property))
  2973. ((listp org-use-property-inheritance)
  2974. (member property org-use-property-inheritance))
  2975. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2976. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2977. "The default column format, if no other format has been defined.
  2978. This variable can be set on the per-file basis by inserting a line
  2979. #+COLUMNS: %25ITEM ....."
  2980. :group 'org-properties
  2981. :type 'string)
  2982. (defcustom org-columns-ellipses ".."
  2983. "The ellipses to be used when a field in column view is truncated.
  2984. When this is the empty string, as many characters as possible are shown,
  2985. but then there will be no visual indication that the field has been truncated.
  2986. When this is a string of length N, the last N characters of a truncated
  2987. field are replaced by this string. If the column is narrower than the
  2988. ellipses string, only part of the ellipses string will be shown."
  2989. :group 'org-properties
  2990. :type 'string)
  2991. (defcustom org-columns-modify-value-for-display-function nil
  2992. "Function that modifies values for display in column view.
  2993. For example, it can be used to cut out a certain part from a time stamp.
  2994. The function must take 2 arguments:
  2995. column-title The title of the column (*not* the property name)
  2996. value The value that should be modified.
  2997. The function should return the value that should be displayed,
  2998. or nil if the normal value should be used."
  2999. :group 'org-properties
  3000. :type 'function)
  3001. (defcustom org-effort-property "Effort"
  3002. "The property that is being used to keep track of effort estimates.
  3003. Effort estimates given in this property need to have the format H:MM."
  3004. :group 'org-properties
  3005. :group 'org-progress
  3006. :type '(string :tag "Property"))
  3007. (defconst org-global-properties-fixed
  3008. '(("VISIBILITY_ALL" . "folded children content all")
  3009. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3010. "List of property/value pairs that can be inherited by any entry.
  3011. These are fixed values, for the preset properties. The user variable
  3012. that can be used to add to this list is `org-global-properties'.
  3013. The entries in this list are cons cells where the car is a property
  3014. name and cdr is a string with the value. If the value represents
  3015. multiple items like an \"_ALL\" property, separate the items by
  3016. spaces.")
  3017. (defcustom org-global-properties nil
  3018. "List of property/value pairs that can be inherited by any entry.
  3019. This list will be combined with the constant `org-global-properties-fixed'.
  3020. The entries in this list are cons cells where the car is a property
  3021. name and cdr is a string with the value.
  3022. You can set buffer-local values for the same purpose in the variable
  3023. `org-file-properties' this by adding lines like
  3024. #+PROPERTY: NAME VALUE"
  3025. :group 'org-properties
  3026. :type '(repeat
  3027. (cons (string :tag "Property")
  3028. (string :tag "Value"))))
  3029. (defvar org-file-properties nil
  3030. "List of property/value pairs that can be inherited by any entry.
  3031. Valid for the current buffer.
  3032. This variable is populated from #+PROPERTY lines.")
  3033. (make-variable-buffer-local 'org-file-properties)
  3034. (defgroup org-agenda nil
  3035. "Options concerning agenda views in Org-mode."
  3036. :tag "Org Agenda"
  3037. :group 'org)
  3038. (defvar org-category nil
  3039. "Variable used by org files to set a category for agenda display.
  3040. Such files should use a file variable to set it, for example
  3041. # -*- mode: org; org-category: \"ELisp\"
  3042. or contain a special line
  3043. #+CATEGORY: ELisp
  3044. If the file does not specify a category, then file's base name
  3045. is used instead.")
  3046. (make-variable-buffer-local 'org-category)
  3047. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3048. (defcustom org-agenda-files nil
  3049. "The files to be used for agenda display.
  3050. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3051. \\[org-remove-file]. You can also use customize to edit the list.
  3052. If an entry is a directory, all files in that directory that are matched by
  3053. `org-agenda-file-regexp' will be part of the file list.
  3054. If the value of the variable is not a list but a single file name, then
  3055. the list of agenda files is actually stored and maintained in that file, one
  3056. agenda file per line. In this file paths can be given relative to
  3057. `org-directory'. Tilde expansion and environment variable substitution
  3058. are also made."
  3059. :group 'org-agenda
  3060. :type '(choice
  3061. (repeat :tag "List of files and directories" file)
  3062. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3063. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3064. "Regular expression to match files for `org-agenda-files'.
  3065. If any element in the list in that variable contains a directory instead
  3066. of a normal file, all files in that directory that are matched by this
  3067. regular expression will be included."
  3068. :group 'org-agenda
  3069. :type 'regexp)
  3070. (defcustom org-agenda-text-search-extra-files nil
  3071. "List of extra files to be searched by text search commands.
  3072. These files will be search in addition to the agenda files by the
  3073. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3074. Note that these files will only be searched for text search commands,
  3075. not for the other agenda views like todo lists, tag searches or the weekly
  3076. agenda. This variable is intended to list notes and possibly archive files
  3077. that should also be searched by these two commands.
  3078. In fact, if the first element in the list is the symbol `agenda-archives',
  3079. than all archive files of all agenda files will be added to the search
  3080. scope."
  3081. :group 'org-agenda
  3082. :type '(set :greedy t
  3083. (const :tag "Agenda Archives" agenda-archives)
  3084. (repeat :inline t (file))))
  3085. (if (fboundp 'defvaralias)
  3086. (defvaralias 'org-agenda-multi-occur-extra-files
  3087. 'org-agenda-text-search-extra-files))
  3088. (defcustom org-agenda-skip-unavailable-files nil
  3089. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3090. A nil value means to remove them, after a query, from the list."
  3091. :group 'org-agenda
  3092. :type 'boolean)
  3093. (defcustom org-calendar-to-agenda-key [?c]
  3094. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3095. The command `org-calendar-goto-agenda' will be bound to this key. The
  3096. default is the character `c' because then `c' can be used to switch back and
  3097. forth between agenda and calendar."
  3098. :group 'org-agenda
  3099. :type 'sexp)
  3100. (defcustom org-calendar-insert-diary-entry-key [?i]
  3101. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3102. This option is irrelevant until `org-agenda-diary-file' has been configured
  3103. to point to an Org-mode file. When that is the case, the command
  3104. `org-agenda-diary-entry' will be bound to the key given here, by default
  3105. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3106. if you want to continue doing this, you need to change this to a different
  3107. key."
  3108. :group 'org-agenda
  3109. :type 'sexp)
  3110. (defcustom org-agenda-diary-file 'diary-file
  3111. "File to which to add new entries with the `i' key in agenda and calendar.
  3112. When this is the symbol `diary-file', the functionality in the Emacs
  3113. calendar will be used to add entries to the `diary-file'. But when this
  3114. points to a file, `org-agenda-diary-entry' will be used instead."
  3115. :group 'org-agenda
  3116. :type '(choice
  3117. (const :tag "The standard Emacs diary file" diary-file)
  3118. (file :tag "Special Org file diary entries")))
  3119. (eval-after-load "calendar"
  3120. '(progn
  3121. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3122. 'org-calendar-goto-agenda)
  3123. (add-hook 'calendar-mode-hook
  3124. (lambda ()
  3125. (unless (eq org-agenda-diary-file 'diary-file)
  3126. (define-key calendar-mode-map
  3127. org-calendar-insert-diary-entry-key
  3128. 'org-agenda-diary-entry))))))
  3129. (defgroup org-latex nil
  3130. "Options for embedding LaTeX code into Org-mode."
  3131. :tag "Org LaTeX"
  3132. :group 'org)
  3133. (defcustom org-format-latex-options
  3134. '(:foreground default :background default :scale 1.0
  3135. :html-foreground "Black" :html-background "Transparent"
  3136. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3137. "Options for creating images from LaTeX fragments.
  3138. This is a property list with the following properties:
  3139. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3140. `default' means use the foreground of the default face.
  3141. `auto' means use the foreground from the text face.
  3142. :background the background color, or \"Transparent\".
  3143. `default' means use the background of the default face.
  3144. `auto' means use the background from the text face.
  3145. :scale a scaling factor for the size of the images, to get more pixels
  3146. :html-foreground, :html-background, :html-scale
  3147. the same numbers for HTML export.
  3148. :matchers a list indicating which matchers should be used to
  3149. find LaTeX fragments. Valid members of this list are:
  3150. \"begin\" find environments
  3151. \"$1\" find single characters surrounded by $.$
  3152. \"$\" find math expressions surrounded by $...$
  3153. \"$$\" find math expressions surrounded by $$....$$
  3154. \"\\(\" find math expressions surrounded by \\(...\\)
  3155. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3156. :group 'org-latex
  3157. :type 'plist)
  3158. (defcustom org-format-latex-signal-error t
  3159. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3160. When nil, just push out a message."
  3161. :group 'org-latex
  3162. :version "24.1"
  3163. :type 'boolean)
  3164. (defcustom org-latex-to-mathml-jar-file nil
  3165. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3166. Use this to specify additional executable file say a jar file.
  3167. When using MathToWeb as the converter, specify the full-path to
  3168. your mathtoweb.jar file."
  3169. :group 'org-latex
  3170. :version "24.1"
  3171. :type '(choice
  3172. (const :tag "None" nil)
  3173. (file :tag "JAR file" :must-match t)))
  3174. (defcustom org-latex-to-mathml-convert-command nil
  3175. "Command to convert LaTeX fragments to MathML.
  3176. Replace format-specifiers in the command as noted below and use
  3177. `shell-command' to convert LaTeX to MathML.
  3178. %j: Executable file in fully expanded form as specified by
  3179. `org-latex-to-mathml-jar-file'.
  3180. %I: Input LaTeX file in fully expanded form
  3181. %o: Output MathML file
  3182. This command is used by `org-create-math-formula'.
  3183. When using MathToWeb as the converter, set this to
  3184. \"java -jar %j -unicode -force -df %o %I\"."
  3185. :group 'org-latex
  3186. :version "24.1"
  3187. :type '(choice
  3188. (const :tag "None" nil)
  3189. (string :tag "\nShell command")))
  3190. (defcustom org-latex-create-formula-image-program 'dvipng
  3191. "Program to convert LaTeX fragments with.
  3192. dvipng Process the LaTeX fragments to dvi file, then convert
  3193. dvi files to png files using dvipng.
  3194. This will also include processing of non-math environments.
  3195. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3196. to convert pdf files to png files"
  3197. :group 'org-latex
  3198. :version "24.1"
  3199. :type '(choice
  3200. (const :tag "dvipng" dvipng)
  3201. (const :tag "imagemagick" imagemagick)))
  3202. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3203. "Path to store latex preview images.
  3204. A relative path here creates many directories relative to the
  3205. processed org files paths. An absolute path puts all preview
  3206. images at the same place."
  3207. :group 'org-latex
  3208. :version "24.3"
  3209. :type 'string)
  3210. (defun org-format-latex-mathml-available-p ()
  3211. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3212. (save-match-data
  3213. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3214. org-latex-to-mathml-convert-command)
  3215. (let ((executable (car (split-string
  3216. org-latex-to-mathml-convert-command))))
  3217. (when (executable-find executable)
  3218. (if (string-match
  3219. "%j" org-latex-to-mathml-convert-command)
  3220. (file-readable-p org-latex-to-mathml-jar-file)
  3221. t))))))
  3222. (defcustom org-format-latex-header "\\documentclass{article}
  3223. \\usepackage[usenames]{color}
  3224. \\usepackage{amsmath}
  3225. \\usepackage[mathscr]{eucal}
  3226. \\pagestyle{empty} % do not remove
  3227. \[PACKAGES]
  3228. \[DEFAULT-PACKAGES]
  3229. % The settings below are copied from fullpage.sty
  3230. \\setlength{\\textwidth}{\\paperwidth}
  3231. \\addtolength{\\textwidth}{-3cm}
  3232. \\setlength{\\oddsidemargin}{1.5cm}
  3233. \\addtolength{\\oddsidemargin}{-2.54cm}
  3234. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3235. \\setlength{\\textheight}{\\paperheight}
  3236. \\addtolength{\\textheight}{-\\headheight}
  3237. \\addtolength{\\textheight}{-\\headsep}
  3238. \\addtolength{\\textheight}{-\\footskip}
  3239. \\addtolength{\\textheight}{-3cm}
  3240. \\setlength{\\topmargin}{1.5cm}
  3241. \\addtolength{\\topmargin}{-2.54cm}"
  3242. "The document header used for processing LaTeX fragments.
  3243. It is imperative that this header make sure that no page number
  3244. appears on the page. The package defined in the variables
  3245. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3246. will either replace the placeholder \"[PACKAGES]\" in this
  3247. header, or they will be appended."
  3248. :group 'org-latex
  3249. :type 'string)
  3250. (defun org-set-packages-alist (var val)
  3251. "Set the packages alist and make sure it has 3 elements per entry."
  3252. (set var (mapcar (lambda (x)
  3253. (if (and (consp x) (= (length x) 2))
  3254. (list (car x) (nth 1 x) t)
  3255. x))
  3256. val)))
  3257. (defun org-get-packages-alist (var)
  3258. "Get the packages alist and make sure it has 3 elements per entry."
  3259. (mapcar (lambda (x)
  3260. (if (and (consp x) (= (length x) 2))
  3261. (list (car x) (nth 1 x) t)
  3262. x))
  3263. (default-value var)))
  3264. (defcustom org-latex-default-packages-alist
  3265. '(("AUTO" "inputenc" t)
  3266. ("T1" "fontenc" t)
  3267. ("" "fixltx2e" nil)
  3268. ("" "graphicx" t)
  3269. ("" "longtable" nil)
  3270. ("" "float" nil)
  3271. ("" "wrapfig" nil)
  3272. ("" "soul" t)
  3273. ("" "textcomp" t)
  3274. ("" "marvosym" t)
  3275. ("" "wasysym" t)
  3276. ("" "latexsym" t)
  3277. ("" "amssymb" t)
  3278. ("" "hyperref" nil)
  3279. "\\tolerance=1000")
  3280. "Alist of default packages to be inserted in the header.
  3281. Change this only if one of the packages here causes an
  3282. incompatibility with another package you are using.
  3283. The packages in this list are needed by one part or another of
  3284. Org mode to function properly:
  3285. - inputenc, fontenc: for basic font and character selection
  3286. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3287. symbols used for interpreting the entities in `org-entities'.
  3288. You can skip some of these packages if you don't use any of the
  3289. symbols in it.
  3290. - graphicx: for including images
  3291. - float, wrapfig: for figure placement
  3292. - longtable: for long tables
  3293. - hyperref: for cross references
  3294. Therefore you should not modify this variable unless you know
  3295. what you are doing. The one reason to change it anyway is that
  3296. you might be loading some other package that conflicts with one
  3297. of the default packages. Each cell is of the format
  3298. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3299. the package also needs to be included when compiling LaTeX
  3300. snippets into images for inclusion into non-LaTeX output."
  3301. :group 'org-latex
  3302. :group 'org-export-latex
  3303. :set 'org-set-packages-alist
  3304. :get 'org-get-packages-alist
  3305. :version "24.1"
  3306. :type '(repeat
  3307. (choice
  3308. (list :tag "options/package pair"
  3309. (string :tag "options")
  3310. (string :tag "package")
  3311. (boolean :tag "Snippet"))
  3312. (string :tag "A line of LaTeX"))))
  3313. (defcustom org-latex-packages-alist nil
  3314. "Alist of packages to be inserted in every LaTeX header.
  3315. These will be inserted after `org-latex-default-packages-alist'.
  3316. Each cell is of the format:
  3317. \(\"options\" \"package\" snippet-flag)
  3318. SNIPPET-FLAG, when t, indicates that this package is also needed
  3319. when turning LaTeX snippets into images for inclusion into
  3320. non-LaTeX output.
  3321. Make sure that you only list packages here which:
  3322. - you want in every file
  3323. - do not conflict with the setup in `org-format-latex-header'.
  3324. - do not conflict with the default packages in
  3325. `org-latex-default-packages-alist'."
  3326. :group 'org-latex
  3327. :group 'org-export-latex
  3328. :set 'org-set-packages-alist
  3329. :get 'org-get-packages-alist
  3330. :type '(repeat
  3331. (choice
  3332. (list :tag "options/package pair"
  3333. (string :tag "options")
  3334. (string :tag "package")
  3335. (boolean :tag "Snippet"))
  3336. (string :tag "A line of LaTeX"))))
  3337. (defgroup org-appearance nil
  3338. "Settings for Org-mode appearance."
  3339. :tag "Org Appearance"
  3340. :group 'org)
  3341. (defcustom org-level-color-stars-only nil
  3342. "Non-nil means fontify only the stars in each headline.
  3343. When nil, the entire headline is fontified.
  3344. Changing it requires restart of `font-lock-mode' to become effective
  3345. also in regions already fontified."
  3346. :group 'org-appearance
  3347. :type 'boolean)
  3348. (defcustom org-hide-leading-stars nil
  3349. "Non-nil means hide the first N-1 stars in a headline.
  3350. This works by using the face `org-hide' for these stars. This
  3351. face is white for a light background, and black for a dark
  3352. background. You may have to customize the face `org-hide' to
  3353. make this work.
  3354. Changing it requires restart of `font-lock-mode' to become effective
  3355. also in regions already fontified.
  3356. You may also set this on a per-file basis by adding one of the following
  3357. lines to the buffer:
  3358. #+STARTUP: hidestars
  3359. #+STARTUP: showstars"
  3360. :group 'org-appearance
  3361. :type 'boolean)
  3362. (defcustom org-hidden-keywords nil
  3363. "List of symbols corresponding to keywords to be hidden the org buffer.
  3364. For example, a value '(title) for this list will make the document's title
  3365. appear in the buffer without the initial #+TITLE: keyword."
  3366. :group 'org-appearance
  3367. :version "24.1"
  3368. :type '(set (const :tag "#+AUTHOR" author)
  3369. (const :tag "#+DATE" date)
  3370. (const :tag "#+EMAIL" email)
  3371. (const :tag "#+TITLE" title)))
  3372. (defcustom org-custom-properties nil
  3373. "List of properties (as strings) with a special meaning.
  3374. The default use of these custom properties is to let the user
  3375. hide them with `org-toggle-custom-properties-visibility'."
  3376. :group 'org-properties
  3377. :group 'org-appearance
  3378. :version "24.3"
  3379. :type '(repeat (string :tag "Property Name")))
  3380. (defcustom org-fontify-done-headline nil
  3381. "Non-nil means change the face of a headline if it is marked DONE.
  3382. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3383. When this is non-nil, the headline after the keyword is set to the
  3384. `org-headline-done' as an additional indication."
  3385. :group 'org-appearance
  3386. :type 'boolean)
  3387. (defcustom org-fontify-emphasized-text t
  3388. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3389. Changing this variable requires a restart of Emacs to take effect."
  3390. :group 'org-appearance
  3391. :type 'boolean)
  3392. (defcustom org-fontify-whole-heading-line nil
  3393. "Non-nil means fontify the whole line for headings.
  3394. This is useful when setting a background color for the
  3395. org-level-* faces."
  3396. :group 'org-appearance
  3397. :type 'boolean)
  3398. (defcustom org-highlight-latex-and-related nil
  3399. "Non-nil means highlight LaTeX related syntax in the buffer.
  3400. When non nil, the value should be a list containing any of the
  3401. following symbols:
  3402. `latex' Highlight LaTeX snippets and environments.
  3403. `script' Highlight subscript and superscript.
  3404. `entities' Highlight entities."
  3405. :group 'org-appearance
  3406. :type '(choice
  3407. (const :tag "No highlighting" nil)
  3408. (set :greedy t :tag "Highlight"
  3409. (const :tag "LaTeX snippets and environments" latex)
  3410. (const :tag "Subscript and superscript" script)
  3411. (const :tag "Entities" entities))))
  3412. (defcustom org-hide-emphasis-markers nil
  3413. "Non-nil mean font-lock should hide the emphasis marker characters."
  3414. :group 'org-appearance
  3415. :type 'boolean)
  3416. (defcustom org-pretty-entities nil
  3417. "Non-nil means show entities as UTF8 characters.
  3418. When nil, the \\name form remains in the buffer."
  3419. :group 'org-appearance
  3420. :version "24.1"
  3421. :type 'boolean)
  3422. (defcustom org-pretty-entities-include-sub-superscripts t
  3423. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3424. :group 'org-appearance
  3425. :version "24.1"
  3426. :type 'boolean)
  3427. (defvar org-emph-re nil
  3428. "Regular expression for matching emphasis.
  3429. After a match, the match groups contain these elements:
  3430. 0 The match of the full regular expression, including the characters
  3431. before and after the proper match
  3432. 1 The character before the proper match, or empty at beginning of line
  3433. 2 The proper match, including the leading and trailing markers
  3434. 3 The leading marker like * or /, indicating the type of highlighting
  3435. 4 The text between the emphasis markers, not including the markers
  3436. 5 The character after the match, empty at the end of a line")
  3437. (defvar org-verbatim-re nil
  3438. "Regular expression for matching verbatim text.")
  3439. (defvar org-emphasis-regexp-components) ; defined just below
  3440. (defvar org-emphasis-alist) ; defined just below
  3441. (defun org-set-emph-re (var val)
  3442. "Set variable and compute the emphasis regular expression."
  3443. (set var val)
  3444. (when (and (boundp 'org-emphasis-alist)
  3445. (boundp 'org-emphasis-regexp-components)
  3446. org-emphasis-alist org-emphasis-regexp-components)
  3447. (let* ((e org-emphasis-regexp-components)
  3448. (pre (car e))
  3449. (post (nth 1 e))
  3450. (border (nth 2 e))
  3451. (body (nth 3 e))
  3452. (nl (nth 4 e))
  3453. (body1 (concat body "*?"))
  3454. (markers (mapconcat 'car org-emphasis-alist ""))
  3455. (vmarkers (mapconcat
  3456. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3457. org-emphasis-alist "")))
  3458. ;; make sure special characters appear at the right position in the class
  3459. (if (string-match "\\^" markers)
  3460. (setq markers (concat (replace-match "" t t markers) "^")))
  3461. (if (string-match "-" markers)
  3462. (setq markers (concat (replace-match "" t t markers) "-")))
  3463. (if (string-match "\\^" vmarkers)
  3464. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3465. (if (string-match "-" vmarkers)
  3466. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3467. (if (> nl 0)
  3468. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3469. (int-to-string nl) "\\}")))
  3470. ;; Make the regexp
  3471. (setq org-emph-re
  3472. (concat "\\([" pre "]\\|^\\)"
  3473. "\\("
  3474. "\\([" markers "]\\)"
  3475. "\\("
  3476. "[^" border "]\\|"
  3477. "[^" border "]"
  3478. body1
  3479. "[^" border "]"
  3480. "\\)"
  3481. "\\3\\)"
  3482. "\\([" post "]\\|$\\)"))
  3483. (setq org-verbatim-re
  3484. (concat "\\([" pre "]\\|^\\)"
  3485. "\\("
  3486. "\\([" vmarkers "]\\)"
  3487. "\\("
  3488. "[^" border "]\\|"
  3489. "[^" border "]"
  3490. body1
  3491. "[^" border "]"
  3492. "\\)"
  3493. "\\3\\)"
  3494. "\\([" post "]\\|$\\)")))))
  3495. (defcustom org-emphasis-regexp-components
  3496. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3497. "Components used to build the regular expression for emphasis.
  3498. This is a list with five entries. Terminology: In an emphasis string
  3499. like \" *strong word* \", we call the initial space PREMATCH, the final
  3500. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3501. and \"trong wor\" is the body. The different components in this variable
  3502. specify what is allowed/forbidden in each part:
  3503. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3504. post Chars allowed as postmatch. End of line will be allowed too.
  3505. border The chars *forbidden* as border characters.
  3506. body-regexp A regexp like \".\" to match a body character. Don't use
  3507. non-shy groups here, and don't allow newline here.
  3508. newline The maximum number of newlines allowed in an emphasis exp.
  3509. Use customize to modify this, or restart Emacs after changing it."
  3510. :group 'org-appearance
  3511. :set 'org-set-emph-re
  3512. :type '(list
  3513. (sexp :tag "Allowed chars in pre ")
  3514. (sexp :tag "Allowed chars in post ")
  3515. (sexp :tag "Forbidden chars in border ")
  3516. (sexp :tag "Regexp for body ")
  3517. (integer :tag "number of newlines allowed")
  3518. (option (boolean :tag "Please ignore this button"))))
  3519. (defcustom org-emphasis-alist
  3520. `(("*" bold "<b>" "</b>")
  3521. ("/" italic "<i>" "</i>")
  3522. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3523. ("=" org-code "<code>" "</code>" verbatim)
  3524. ("~" org-verbatim "<code>" "</code>" verbatim)
  3525. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3526. "<del>" "</del>")
  3527. )
  3528. "Special syntax for emphasized text.
  3529. Text starting and ending with a special character will be emphasized, for
  3530. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3531. characters, the face to be used by font-lock for highlighting in Org-mode
  3532. Emacs buffers, and the HTML tags to be used for this.
  3533. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3534. Use customize to modify this, or restart Emacs after changing it."
  3535. :group 'org-appearance
  3536. :set 'org-set-emph-re
  3537. :type '(repeat
  3538. (list
  3539. (string :tag "Marker character")
  3540. (choice
  3541. (face :tag "Font-lock-face")
  3542. (plist :tag "Face property list"))
  3543. (string :tag "HTML start tag")
  3544. (string :tag "HTML end tag")
  3545. (option (const verbatim)))))
  3546. (defvar org-protecting-blocks
  3547. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3548. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3549. This is needed for font-lock setup.")
  3550. ;;; Miscellaneous options
  3551. (defgroup org-completion nil
  3552. "Completion in Org-mode."
  3553. :tag "Org Completion"
  3554. :group 'org)
  3555. (defcustom org-completion-use-ido nil
  3556. "Non-nil means use ido completion wherever possible.
  3557. Note that `ido-mode' must be active for this variable to be relevant.
  3558. If you decide to turn this variable on, you might well want to turn off
  3559. `org-outline-path-complete-in-steps'.
  3560. See also `org-completion-use-iswitchb'."
  3561. :group 'org-completion
  3562. :type 'boolean)
  3563. (defcustom org-completion-use-iswitchb nil
  3564. "Non-nil means use iswitchb completion wherever possible.
  3565. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3566. If you decide to turn this variable on, you might well want to turn off
  3567. `org-outline-path-complete-in-steps'.
  3568. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3569. :group 'org-completion
  3570. :type 'boolean)
  3571. (defcustom org-completion-fallback-command 'hippie-expand
  3572. "The expansion command called by \\[pcomplete] in normal context.
  3573. Normal means, no org-mode-specific context."
  3574. :group 'org-completion
  3575. :type 'function)
  3576. ;;; Functions and variables from their packages
  3577. ;; Declared here to avoid compiler warnings
  3578. ;; XEmacs only
  3579. (defvar outline-mode-menu-heading)
  3580. (defvar outline-mode-menu-show)
  3581. (defvar outline-mode-menu-hide)
  3582. (defvar zmacs-regions) ; XEmacs regions
  3583. ;; Emacs only
  3584. (defvar mark-active)
  3585. ;; Various packages
  3586. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3587. (declare-function calendar-forward-day "cal-move" (arg))
  3588. (declare-function calendar-goto-date "cal-move" (date))
  3589. (declare-function calendar-goto-today "cal-move" ())
  3590. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3591. (defvar calc-embedded-close-formula)
  3592. (defvar calc-embedded-open-formula)
  3593. (declare-function cdlatex-tab "ext:cdlatex" ())
  3594. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3595. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3596. (defvar font-lock-unfontify-region-function)
  3597. (declare-function iswitchb-read-buffer "iswitchb"
  3598. (prompt &optional default require-match start matches-set))
  3599. (defvar iswitchb-temp-buflist)
  3600. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3601. (defvar org-agenda-tags-todo-honor-ignore-options)
  3602. (declare-function org-agenda-skip "org-agenda" ())
  3603. (declare-function
  3604. org-agenda-format-item "org-agenda"
  3605. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3606. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3607. (declare-function org-agenda-change-all-lines "org-agenda"
  3608. (newhead hdmarker &optional fixface just-this))
  3609. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3610. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3611. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3612. (beg end))
  3613. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3614. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3615. "org-agenda" (&optional end))
  3616. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3617. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3618. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3619. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3620. (declare-function org-indent-mode "org-indent" (&optional arg))
  3621. (declare-function parse-time-string "parse-time" (string))
  3622. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3623. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3624. (defvar remember-data-file)
  3625. (defvar texmathp-why)
  3626. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3627. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3628. (defvar org-latex-regexps)
  3629. ;;; Autoload and prepare some org modules
  3630. ;; Some table stuff that needs to be defined here, because it is used
  3631. ;; by the functions setting up org-mode or checking for table context.
  3632. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3633. "Detect an org-type or table-type table.")
  3634. (defconst org-table-line-regexp "^[ \t]*|"
  3635. "Detect an org-type table line.")
  3636. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3637. "Detect an org-type table line.")
  3638. (defconst org-table-hline-regexp "^[ \t]*|-"
  3639. "Detect an org-type table hline.")
  3640. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3641. "Detect a table-type table hline.")
  3642. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3643. "Detect the first line outside a table when searching from within it.
  3644. This works for both table types.")
  3645. ;; Autoload the functions in org-table.el that are needed by functions here.
  3646. (eval-and-compile
  3647. (org-autoload "org-table"
  3648. '(org-table-begin org-table-blank-field org-table-end)))
  3649. ;;;###autoload
  3650. (defun turn-on-orgtbl ()
  3651. "Unconditionally turn on `orgtbl-mode'."
  3652. (require 'org-table)
  3653. (orgtbl-mode 1))
  3654. (defun org-at-table-p (&optional table-type)
  3655. "Return t if the cursor is inside an org-type table.
  3656. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3657. (if org-enable-table-editor
  3658. (save-excursion
  3659. (beginning-of-line 1)
  3660. (looking-at (if table-type org-table-any-line-regexp
  3661. org-table-line-regexp)))
  3662. nil))
  3663. (defsubst org-table-p () (org-at-table-p))
  3664. (defun org-at-table.el-p ()
  3665. "Return t if and only if we are at a table.el table."
  3666. (and (org-at-table-p 'any)
  3667. (save-excursion
  3668. (goto-char (org-table-begin 'any))
  3669. (looking-at org-table1-hline-regexp))))
  3670. (defun org-table-recognize-table.el ()
  3671. "If there is a table.el table nearby, recognize it and move into it."
  3672. (if org-table-tab-recognizes-table.el
  3673. (if (org-at-table.el-p)
  3674. (progn
  3675. (beginning-of-line 1)
  3676. (if (looking-at org-table-dataline-regexp)
  3677. nil
  3678. (if (looking-at org-table1-hline-regexp)
  3679. (progn
  3680. (beginning-of-line 2)
  3681. (if (looking-at org-table-any-border-regexp)
  3682. (beginning-of-line -1)))))
  3683. (if (re-search-forward "|" (org-table-end t) t)
  3684. (progn
  3685. (require 'table)
  3686. (if (table--at-cell-p (point))
  3687. t
  3688. (message "recognizing table.el table...")
  3689. (table-recognize-table)
  3690. (message "recognizing table.el table...done")))
  3691. (error "This should not happen"))
  3692. t)
  3693. nil)
  3694. nil))
  3695. (defun org-at-table-hline-p ()
  3696. "Return t if the cursor is inside a hline in a table."
  3697. (if org-enable-table-editor
  3698. (save-excursion
  3699. (beginning-of-line 1)
  3700. (looking-at org-table-hline-regexp))
  3701. nil))
  3702. (defvar org-table-clean-did-remove-column nil)
  3703. (defun org-table-map-tables (function &optional quietly)
  3704. "Apply FUNCTION to the start of all tables in the buffer."
  3705. (save-excursion
  3706. (save-restriction
  3707. (widen)
  3708. (goto-char (point-min))
  3709. (while (re-search-forward org-table-any-line-regexp nil t)
  3710. (unless quietly
  3711. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3712. (beginning-of-line 1)
  3713. (when (and (looking-at org-table-line-regexp)
  3714. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3715. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3716. (save-excursion (funcall function))
  3717. (or (looking-at org-table-line-regexp)
  3718. (forward-char 1)))
  3719. (re-search-forward org-table-any-border-regexp nil 1))))
  3720. (unless quietly (message "Mapping tables: done")))
  3721. ;; Declare and autoload functions from ox.el and al.
  3722. (declare-function org-export-get-environment "ox"
  3723. (&optional backend subtreep ext-plist))
  3724. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3725. ;; Declare and autoload functions from org-agenda.el
  3726. (eval-and-compile
  3727. (org-autoload "org-agenda"
  3728. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3729. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3730. (declare-function org-clock-update-mode-line "org-clock" ())
  3731. (declare-function org-resolve-clocks "org-clock"
  3732. (&optional also-non-dangling-p prompt last-valid))
  3733. (defvar org-clock-start-time)
  3734. (defvar org-clock-marker (make-marker)
  3735. "Marker recording the last clock-in.")
  3736. (defvar org-clock-hd-marker (make-marker)
  3737. "Marker recording the last clock-in, but the headline position.")
  3738. (defvar org-clock-heading ""
  3739. "The heading of the current clock entry.")
  3740. (defun org-clock-is-active ()
  3741. "Return non-nil if clock is currently running.
  3742. The return value is actually the clock marker."
  3743. (marker-buffer org-clock-marker))
  3744. (eval-and-compile
  3745. (org-autoload "org-clock" '(org-clock-remove-overlays
  3746. org-clock-update-time-maybe
  3747. org-clocktable-shift)))
  3748. (defun org-check-running-clock ()
  3749. "Check if the current buffer contains the running clock.
  3750. If yes, offer to stop it and to save the buffer with the changes."
  3751. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3752. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3753. (buffer-name))))
  3754. (org-clock-out)
  3755. (when (y-or-n-p "Save changed buffer?")
  3756. (save-buffer))))
  3757. (defun org-clocktable-try-shift (dir n)
  3758. "Check if this line starts a clock table, if yes, shift the time block."
  3759. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3760. (org-clocktable-shift dir n)))
  3761. ;;;###autoload
  3762. (defun org-clock-persistence-insinuate ()
  3763. "Set up hooks for clock persistence."
  3764. (require 'org-clock)
  3765. (add-hook 'org-mode-hook 'org-clock-load)
  3766. (add-hook 'kill-emacs-hook 'org-clock-save))
  3767. ;; Define the variable already here, to make sure we have it.
  3768. (defvar org-indent-mode nil
  3769. "Non-nil if Org-Indent mode is enabled.
  3770. Use the command `org-indent-mode' to change this variable.")
  3771. ;; Autoload archiving code
  3772. ;; The stuff that is needed for cycling and tags has to be defined here.
  3773. (defgroup org-archive nil
  3774. "Options concerning archiving in Org-mode."
  3775. :tag "Org Archive"
  3776. :group 'org-structure)
  3777. (defcustom org-archive-location "%s_archive::"
  3778. "The location where subtrees should be archived.
  3779. The value of this variable is a string, consisting of two parts,
  3780. separated by a double-colon. The first part is a filename and
  3781. the second part is a headline.
  3782. When the filename is omitted, archiving happens in the same file.
  3783. %s in the filename will be replaced by the current file
  3784. name (without the directory part). Archiving to a different file
  3785. is useful to keep archived entries from contributing to the
  3786. Org-mode Agenda.
  3787. The archived entries will be filed as subtrees of the specified
  3788. headline. When the headline is omitted, the subtrees are simply
  3789. filed away at the end of the file, as top-level entries. Also in
  3790. the heading you can use %s to represent the file name, this can be
  3791. useful when using the same archive for a number of different files.
  3792. Here are a few examples:
  3793. \"%s_archive::\"
  3794. If the current file is Projects.org, archive in file
  3795. Projects.org_archive, as top-level trees. This is the default.
  3796. \"::* Archived Tasks\"
  3797. Archive in the current file, under the top-level headline
  3798. \"* Archived Tasks\".
  3799. \"~/org/archive.org::\"
  3800. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3801. \"~/org/archive.org::* From %s\"
  3802. Archive in file ~/org/archive.org (absolute path), under headlines
  3803. \"From FILENAME\" where file name is the current file name.
  3804. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3805. The \"datetree/\" string is special, signifying to archive
  3806. items to the datetree. Items are placed in either the CLOSED
  3807. date of the item, or the current date if there is no CLOSED date.
  3808. The heading will be a subentry to the current date. There doesn't
  3809. need to be a heading, but there always needs to be a slash after
  3810. datetree. For example, to store archived items directly in the
  3811. datetree, use \"~/org/datetree.org::datetree/\".
  3812. \"basement::** Finished Tasks\"
  3813. Archive in file ./basement (relative path), as level 3 trees
  3814. below the level 2 heading \"** Finished Tasks\".
  3815. You may set this option on a per-file basis by adding to the buffer a
  3816. line like
  3817. #+ARCHIVE: basement::** Finished Tasks
  3818. You may also define it locally for a subtree by setting an ARCHIVE property
  3819. in the entry. If such a property is found in an entry, or anywhere up
  3820. the hierarchy, it will be used."
  3821. :group 'org-archive
  3822. :type 'string)
  3823. (defcustom org-archive-tag "ARCHIVE"
  3824. "The tag that marks a subtree as archived.
  3825. An archived subtree does not open during visibility cycling, and does
  3826. not contribute to the agenda listings.
  3827. After changing this, font-lock must be restarted in the relevant buffers to
  3828. get the proper fontification."
  3829. :group 'org-archive
  3830. :group 'org-keywords
  3831. :type 'string)
  3832. (defcustom org-agenda-skip-archived-trees t
  3833. "Non-nil means the agenda will skip any items located in archived trees.
  3834. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3835. variable is no longer recommended, you should leave it at the value t.
  3836. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3837. :group 'org-archive
  3838. :group 'org-agenda-skip
  3839. :type 'boolean)
  3840. (defcustom org-columns-skip-archived-trees t
  3841. "Non-nil means ignore archived trees when creating column view."
  3842. :group 'org-archive
  3843. :group 'org-properties
  3844. :type 'boolean)
  3845. (defcustom org-cycle-open-archived-trees nil
  3846. "Non-nil means `org-cycle' will open archived trees.
  3847. An archived tree is a tree marked with the tag ARCHIVE.
  3848. When nil, archived trees will stay folded. You can still open them with
  3849. normal outline commands like `show-all', but not with the cycling commands."
  3850. :group 'org-archive
  3851. :group 'org-cycle
  3852. :type 'boolean)
  3853. (defcustom org-sparse-tree-open-archived-trees nil
  3854. "Non-nil means sparse tree construction shows matches in archived trees.
  3855. When nil, matches in these trees are highlighted, but the trees are kept in
  3856. collapsed state."
  3857. :group 'org-archive
  3858. :group 'org-sparse-trees
  3859. :type 'boolean)
  3860. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3861. "The default date type when building a sparse tree.
  3862. When this is nil, a date is a scheduled or a deadline timestamp.
  3863. Otherwise, these types are allowed:
  3864. all: all timestamps
  3865. active: only active timestamps (<...>)
  3866. inactive: only inactive timestamps (<...)
  3867. scheduled: only scheduled timestamps
  3868. deadline: only deadline timestamps"
  3869. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3870. (const :tag "All timestamps" all)
  3871. (const :tag "Only active timestamps" active)
  3872. (const :tag "Only inactive timestamps" inactive)
  3873. (const :tag "Only scheduled timestamps" scheduled)
  3874. (const :tag "Only deadline timestamps" deadline))
  3875. :version "24.3"
  3876. :group 'org-sparse-trees)
  3877. (defun org-cycle-hide-archived-subtrees (state)
  3878. "Re-hide all archived subtrees after a visibility state change."
  3879. (when (and (not org-cycle-open-archived-trees)
  3880. (not (memq state '(overview folded))))
  3881. (save-excursion
  3882. (let* ((globalp (memq state '(contents all)))
  3883. (beg (if globalp (point-min) (point)))
  3884. (end (if globalp (point-max) (org-end-of-subtree t))))
  3885. (org-hide-archived-subtrees beg end)
  3886. (goto-char beg)
  3887. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3888. (message "%s" (substitute-command-keys
  3889. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3890. (defun org-force-cycle-archived ()
  3891. "Cycle subtree even if it is archived."
  3892. (interactive)
  3893. (setq this-command 'org-cycle)
  3894. (let ((org-cycle-open-archived-trees t))
  3895. (call-interactively 'org-cycle)))
  3896. (defun org-hide-archived-subtrees (beg end)
  3897. "Re-hide all archived subtrees after a visibility state change."
  3898. (save-excursion
  3899. (let* ((re (concat ":" org-archive-tag ":")))
  3900. (goto-char beg)
  3901. (while (re-search-forward re end t)
  3902. (when (org-at-heading-p)
  3903. (org-flag-subtree t)
  3904. (org-end-of-subtree t))))))
  3905. (declare-function outline-end-of-heading "outline" ())
  3906. (declare-function outline-flag-region "outline" (from to flag))
  3907. (defun org-flag-subtree (flag)
  3908. (save-excursion
  3909. (org-back-to-heading t)
  3910. (outline-end-of-heading)
  3911. (outline-flag-region (point)
  3912. (progn (org-end-of-subtree t) (point))
  3913. flag)))
  3914. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3915. (eval-and-compile
  3916. (org-autoload "org-archive"
  3917. '(org-add-archive-files)))
  3918. ;; Autoload Column View Code
  3919. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3920. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3921. (declare-function org-columns-compute "org-colview" (property))
  3922. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3923. '(org-columns-number-to-string
  3924. org-columns-get-format-and-top-level
  3925. org-columns-compute
  3926. org-columns-remove-overlays))
  3927. ;; Autoload ID code
  3928. (declare-function org-id-store-link "org-id")
  3929. (declare-function org-id-locations-load "org-id")
  3930. (declare-function org-id-locations-save "org-id")
  3931. (defvar org-id-track-globally)
  3932. (org-autoload "org-id"
  3933. '(org-id-new
  3934. org-id-copy
  3935. org-id-get-with-outline-path-completion
  3936. org-id-get-with-outline-drilling))
  3937. ;;; Variables for pre-computed regular expressions, all buffer local
  3938. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3939. "Matches first line of a hidden block.")
  3940. (make-variable-buffer-local 'org-drawer-regexp)
  3941. (defvar org-todo-regexp nil
  3942. "Matches any of the TODO state keywords.")
  3943. (make-variable-buffer-local 'org-todo-regexp)
  3944. (defvar org-not-done-regexp nil
  3945. "Matches any of the TODO state keywords except the last one.")
  3946. (make-variable-buffer-local 'org-not-done-regexp)
  3947. (defvar org-not-done-heading-regexp nil
  3948. "Matches a TODO headline that is not done.")
  3949. (make-variable-buffer-local 'org-not-done-regexp)
  3950. (defvar org-todo-line-regexp nil
  3951. "Matches a headline and puts TODO state into group 2 if present.")
  3952. (make-variable-buffer-local 'org-todo-line-regexp)
  3953. (defvar org-complex-heading-regexp nil
  3954. "Matches a headline and puts everything into groups:
  3955. group 1: the stars
  3956. group 2: The todo keyword, maybe
  3957. group 3: Priority cookie
  3958. group 4: True headline
  3959. group 5: Tags")
  3960. (make-variable-buffer-local 'org-complex-heading-regexp)
  3961. (defvar org-complex-heading-regexp-format nil
  3962. "Printf format to make regexp to match an exact headline.
  3963. This regexp will match the headline of any node which has the
  3964. exact headline text that is put into the format, but may have any
  3965. TODO state, priority and tags.")
  3966. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3967. (defvar org-todo-line-tags-regexp nil
  3968. "Matches a headline and puts TODO state into group 2 if present.
  3969. Also put tags into group 4 if tags are present.")
  3970. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3971. (defvar org-ds-keyword-length 12
  3972. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3973. (make-variable-buffer-local 'org-ds-keyword-length)
  3974. (defvar org-deadline-regexp nil
  3975. "Matches the DEADLINE keyword.")
  3976. (make-variable-buffer-local 'org-deadline-regexp)
  3977. (defvar org-deadline-time-regexp nil
  3978. "Matches the DEADLINE keyword together with a time stamp.")
  3979. (make-variable-buffer-local 'org-deadline-time-regexp)
  3980. (defvar org-deadline-line-regexp nil
  3981. "Matches the DEADLINE keyword and the rest of the line.")
  3982. (make-variable-buffer-local 'org-deadline-line-regexp)
  3983. (defvar org-scheduled-regexp nil
  3984. "Matches the SCHEDULED keyword.")
  3985. (make-variable-buffer-local 'org-scheduled-regexp)
  3986. (defvar org-scheduled-time-regexp nil
  3987. "Matches the SCHEDULED keyword together with a time stamp.")
  3988. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3989. (defvar org-closed-time-regexp nil
  3990. "Matches the CLOSED keyword together with a time stamp.")
  3991. (make-variable-buffer-local 'org-closed-time-regexp)
  3992. (defvar org-keyword-time-regexp nil
  3993. "Matches any of the 4 keywords, together with the time stamp.")
  3994. (make-variable-buffer-local 'org-keyword-time-regexp)
  3995. (defvar org-keyword-time-not-clock-regexp nil
  3996. "Matches any of the 3 keywords, together with the time stamp.")
  3997. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3998. (defvar org-maybe-keyword-time-regexp nil
  3999. "Matches a timestamp, possibly preceded by a keyword.")
  4000. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4001. (defvar org-all-time-keywords nil
  4002. "List of time keywords.")
  4003. (make-variable-buffer-local 'org-all-time-keywords)
  4004. (defconst org-plain-time-of-day-regexp
  4005. (concat
  4006. "\\(\\<[012]?[0-9]"
  4007. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4008. "\\(--?"
  4009. "\\(\\<[012]?[0-9]"
  4010. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4011. "\\)?")
  4012. "Regular expression to match a plain time or time range.
  4013. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4014. groups carry important information:
  4015. 0 the full match
  4016. 1 the first time, range or not
  4017. 8 the second time, if it is a range.")
  4018. (defconst org-plain-time-extension-regexp
  4019. (concat
  4020. "\\(\\<[012]?[0-9]"
  4021. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4022. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4023. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4024. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4025. groups carry important information:
  4026. 0 the full match
  4027. 7 hours of duration
  4028. 9 minutes of duration")
  4029. (defconst org-stamp-time-of-day-regexp
  4030. (concat
  4031. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4032. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4033. "\\(--?"
  4034. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4035. "Regular expression to match a timestamp time or time range.
  4036. After a match, the following groups carry important information:
  4037. 0 the full match
  4038. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4039. 2 the first time, range or not
  4040. 4 the second time, if it is a range.")
  4041. (defconst org-startup-options
  4042. '(("fold" org-startup-folded t)
  4043. ("overview" org-startup-folded t)
  4044. ("nofold" org-startup-folded nil)
  4045. ("showall" org-startup-folded nil)
  4046. ("showeverything" org-startup-folded showeverything)
  4047. ("content" org-startup-folded content)
  4048. ("indent" org-startup-indented t)
  4049. ("noindent" org-startup-indented nil)
  4050. ("hidestars" org-hide-leading-stars t)
  4051. ("showstars" org-hide-leading-stars nil)
  4052. ("odd" org-odd-levels-only t)
  4053. ("oddeven" org-odd-levels-only nil)
  4054. ("align" org-startup-align-all-tables t)
  4055. ("noalign" org-startup-align-all-tables nil)
  4056. ("inlineimages" org-startup-with-inline-images t)
  4057. ("noinlineimages" org-startup-with-inline-images nil)
  4058. ("latexpreview" org-startup-with-latex-preview t)
  4059. ("nolatexpreview" org-startup-with-latex-preview nil)
  4060. ("customtime" org-display-custom-times t)
  4061. ("logdone" org-log-done time)
  4062. ("lognotedone" org-log-done note)
  4063. ("nologdone" org-log-done nil)
  4064. ("lognoteclock-out" org-log-note-clock-out t)
  4065. ("nolognoteclock-out" org-log-note-clock-out nil)
  4066. ("logrepeat" org-log-repeat state)
  4067. ("lognoterepeat" org-log-repeat note)
  4068. ("logdrawer" org-log-into-drawer t)
  4069. ("nologdrawer" org-log-into-drawer nil)
  4070. ("logstatesreversed" org-log-states-order-reversed t)
  4071. ("nologstatesreversed" org-log-states-order-reversed nil)
  4072. ("nologrepeat" org-log-repeat nil)
  4073. ("logreschedule" org-log-reschedule time)
  4074. ("lognotereschedule" org-log-reschedule note)
  4075. ("nologreschedule" org-log-reschedule nil)
  4076. ("logredeadline" org-log-redeadline time)
  4077. ("lognoteredeadline" org-log-redeadline note)
  4078. ("nologredeadline" org-log-redeadline nil)
  4079. ("logrefile" org-log-refile time)
  4080. ("lognoterefile" org-log-refile note)
  4081. ("nologrefile" org-log-refile nil)
  4082. ("fninline" org-footnote-define-inline t)
  4083. ("nofninline" org-footnote-define-inline nil)
  4084. ("fnlocal" org-footnote-section nil)
  4085. ("fnauto" org-footnote-auto-label t)
  4086. ("fnprompt" org-footnote-auto-label nil)
  4087. ("fnconfirm" org-footnote-auto-label confirm)
  4088. ("fnplain" org-footnote-auto-label plain)
  4089. ("fnadjust" org-footnote-auto-adjust t)
  4090. ("nofnadjust" org-footnote-auto-adjust nil)
  4091. ("constcgs" constants-unit-system cgs)
  4092. ("constSI" constants-unit-system SI)
  4093. ("noptag" org-tag-persistent-alist nil)
  4094. ("hideblocks" org-hide-block-startup t)
  4095. ("nohideblocks" org-hide-block-startup nil)
  4096. ("beamer" org-startup-with-beamer-mode t)
  4097. ("entitiespretty" org-pretty-entities t)
  4098. ("entitiesplain" org-pretty-entities nil))
  4099. "Variable associated with STARTUP options for org-mode.
  4100. Each element is a list of three items: the startup options (as written
  4101. in the #+STARTUP line), the corresponding variable, and the value to set
  4102. this variable to if the option is found. An optional forth element PUSH
  4103. means to push this value onto the list in the variable.")
  4104. (defun org-update-property-plist (key val props)
  4105. "Update PROPS with KEY and VAL."
  4106. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4107. (key (if appending (substring key 0 (- (length key) 1)) key))
  4108. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4109. (previous (cdr (assoc key props))))
  4110. (if appending
  4111. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4112. (cons (cons key val) remainder))))
  4113. (defconst org-block-regexp
  4114. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4115. "Regular expression for hiding blocks.")
  4116. (defconst org-heading-keyword-regexp-format
  4117. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4118. "Printf format for a regexp matching a headline with some keyword.
  4119. This regexp will match the headline of any node which has the
  4120. exact keyword that is put into the format. The keyword isn't in
  4121. any group by default, but the stars and the body are.")
  4122. (defconst org-heading-keyword-maybe-regexp-format
  4123. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4124. "Printf format for a regexp matching a headline, possibly with some keyword.
  4125. This regexp can match any headline with the specified keyword, or
  4126. without a keyword. The keyword isn't in any group by default,
  4127. but the stars and the body are.")
  4128. (defun org-set-regexps-and-options ()
  4129. "Precompute regular expressions for current buffer."
  4130. (when (derived-mode-p 'org-mode)
  4131. (org-set-local 'org-todo-kwd-alist nil)
  4132. (org-set-local 'org-todo-key-alist nil)
  4133. (org-set-local 'org-todo-key-trigger nil)
  4134. (org-set-local 'org-todo-keywords-1 nil)
  4135. (org-set-local 'org-done-keywords nil)
  4136. (org-set-local 'org-todo-heads nil)
  4137. (org-set-local 'org-todo-sets nil)
  4138. (org-set-local 'org-todo-log-states nil)
  4139. (org-set-local 'org-file-properties nil)
  4140. (org-set-local 'org-file-tags nil)
  4141. (let ((re (org-make-options-regexp
  4142. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE" "FILETAGS"
  4143. "TAGS" "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4144. "SETUPFILE" "OPTIONS")
  4145. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4146. (splitre "[ \t]+")
  4147. (scripts org-use-sub-superscripts)
  4148. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4149. tail sep kws1 prio props ftags drawers ext-setup-or-nil setup-contents
  4150. (start 0))
  4151. (save-excursion
  4152. (save-restriction
  4153. (widen)
  4154. (goto-char (point-min))
  4155. (while (or (and ext-setup-or-nil
  4156. (string-match re ext-setup-or-nil start)
  4157. (setq start (match-end 0)))
  4158. (and (setq ext-setup-or-nil nil start 0)
  4159. (re-search-forward re nil t)))
  4160. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4161. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4162. (if (stringp value) (setq value (org-trim value)))
  4163. (cond
  4164. ((equal key "CATEGORY")
  4165. (setq cat value))
  4166. ((member key '("SEQ_TODO" "TODO"))
  4167. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4168. ((equal key "TYP_TODO")
  4169. (push (cons 'type (org-split-string value splitre)) kwds))
  4170. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4171. ;; general TODO-like setup
  4172. (push (cons (intern (downcase (match-string 1 key)))
  4173. (org-split-string value splitre)) kwds))
  4174. ((equal key "TAGS")
  4175. (setq tags (append tags (if tags '("\\n") nil)
  4176. (org-split-string value splitre))))
  4177. ((equal key "COLUMNS")
  4178. (org-set-local 'org-columns-default-format value))
  4179. ((equal key "LINK")
  4180. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4181. (push (cons (match-string 1 value)
  4182. (org-trim (match-string 2 value)))
  4183. links)))
  4184. ((equal key "PRIORITIES")
  4185. (setq prio (org-split-string value " +")))
  4186. ((equal key "PROPERTY")
  4187. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4188. (setq props (org-update-property-plist (match-string 1 value)
  4189. (match-string 2 value)
  4190. props))))
  4191. ((equal key "FILETAGS")
  4192. (when (string-match "\\S-" value)
  4193. (setq ftags
  4194. (append
  4195. ftags
  4196. (apply 'append
  4197. (mapcar (lambda (x) (org-split-string x ":"))
  4198. (org-split-string value)))))))
  4199. ((equal key "DRAWERS")
  4200. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4201. ((equal key "CONSTANTS")
  4202. (setq const (append const (org-split-string value splitre))))
  4203. ((equal key "STARTUP")
  4204. (let ((opts (org-split-string value splitre))
  4205. l var val)
  4206. (while (setq l (pop opts))
  4207. (when (setq l (assoc l org-startup-options))
  4208. (setq var (nth 1 l) val (nth 2 l))
  4209. (if (not (nth 3 l))
  4210. (set (make-local-variable var) val)
  4211. (if (not (listp (symbol-value var)))
  4212. (set (make-local-variable var) nil))
  4213. (set (make-local-variable var) (symbol-value var))
  4214. (add-to-list var val))))))
  4215. ((equal key "ARCHIVE")
  4216. (setq arch value)
  4217. (remove-text-properties 0 (length arch)
  4218. '(face t fontified t) arch))
  4219. ((equal key "OPTIONS")
  4220. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4221. (setq scripts (read (match-string 2 value)))))
  4222. ((equal key "SETUPFILE")
  4223. (setq setup-contents (org-file-contents
  4224. (expand-file-name
  4225. (org-remove-double-quotes value))
  4226. 'noerror))
  4227. (if (not ext-setup-or-nil)
  4228. (setq ext-setup-or-nil setup-contents start 0)
  4229. (setq ext-setup-or-nil
  4230. (concat (substring ext-setup-or-nil 0 start)
  4231. "\n" setup-contents "\n"
  4232. (substring ext-setup-or-nil start)))))))
  4233. ;; search for property blocks
  4234. (goto-char (point-min))
  4235. (while (re-search-forward org-block-regexp nil t)
  4236. (when (equal "PROPERTY" (upcase (match-string 1)))
  4237. (setq value (replace-regexp-in-string
  4238. "[\n\r]" " " (match-string 4)))
  4239. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4240. (setq props (org-update-property-plist (match-string 1 value)
  4241. (match-string 2 value)
  4242. props)))))))
  4243. (org-set-local 'org-use-sub-superscripts scripts)
  4244. (when cat
  4245. (org-set-local 'org-category (intern cat))
  4246. (push (cons "CATEGORY" cat) props))
  4247. (when prio
  4248. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4249. (setq prio (mapcar 'string-to-char prio))
  4250. (org-set-local 'org-highest-priority (nth 0 prio))
  4251. (org-set-local 'org-lowest-priority (nth 1 prio))
  4252. (org-set-local 'org-default-priority (nth 2 prio)))
  4253. (and props (org-set-local 'org-file-properties (nreverse props)))
  4254. (and ftags (org-set-local 'org-file-tags
  4255. (mapcar 'org-add-prop-inherited ftags)))
  4256. (and drawers (org-set-local 'org-drawers drawers))
  4257. (and arch (org-set-local 'org-archive-location arch))
  4258. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4259. ;; Process the TODO keywords
  4260. (unless kwds
  4261. ;; Use the global values as if they had been given locally.
  4262. (setq kwds (default-value 'org-todo-keywords))
  4263. (if (stringp (car kwds))
  4264. (setq kwds (list (cons org-todo-interpretation
  4265. (default-value 'org-todo-keywords)))))
  4266. (setq kwds (reverse kwds)))
  4267. (setq kwds (nreverse kwds))
  4268. (let (inter kws kw)
  4269. (while (setq kws (pop kwds))
  4270. (let ((kws (or
  4271. (run-hook-with-args-until-success
  4272. 'org-todo-setup-filter-hook kws)
  4273. kws)))
  4274. (setq inter (pop kws) sep (member "|" kws)
  4275. kws0 (delete "|" (copy-sequence kws))
  4276. kwsa nil
  4277. kws1 (mapcar
  4278. (lambda (x)
  4279. ;; 1 2
  4280. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4281. (progn
  4282. (setq kw (match-string 1 x)
  4283. key (and (match-end 2) (match-string 2 x))
  4284. log (org-extract-log-state-settings x))
  4285. (push (cons kw (and key (string-to-char key))) kwsa)
  4286. (and log (push log org-todo-log-states))
  4287. kw)
  4288. (error "Invalid TODO keyword %s" x)))
  4289. kws0)
  4290. kwsa (if kwsa (append '((:startgroup))
  4291. (nreverse kwsa)
  4292. '((:endgroup))))
  4293. hw (car kws1)
  4294. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4295. tail (list inter hw (car dws) (org-last dws))))
  4296. (add-to-list 'org-todo-heads hw 'append)
  4297. (push kws1 org-todo-sets)
  4298. (setq org-done-keywords (append org-done-keywords dws nil))
  4299. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4300. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4301. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4302. (setq org-todo-sets (nreverse org-todo-sets)
  4303. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4304. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4305. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4306. ;; Process the constants
  4307. (when const
  4308. (let (e cst)
  4309. (while (setq e (pop const))
  4310. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4311. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4312. (setq org-table-formula-constants-local cst)))
  4313. ;; Process the tags.
  4314. (when tags
  4315. (let (e tgs)
  4316. (while (setq e (pop tags))
  4317. (cond
  4318. ((equal e "{") (push '(:startgroup) tgs))
  4319. ((equal e "}") (push '(:endgroup) tgs))
  4320. ((equal e "\\n") (push '(:newline) tgs))
  4321. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4322. (push (cons (match-string 1 e)
  4323. (string-to-char (match-string 2 e)))
  4324. tgs))
  4325. (t (push (list e) tgs))))
  4326. (org-set-local 'org-tag-alist nil)
  4327. (while (setq e (pop tgs))
  4328. (or (and (stringp (car e))
  4329. (assoc (car e) org-tag-alist))
  4330. (push e org-tag-alist)))))
  4331. ;; Compute the regular expressions and other local variables.
  4332. ;; Using `org-outline-regexp-bol' would complicate them much,
  4333. ;; because of the fixed white space at the end of that string.
  4334. (if (not org-done-keywords)
  4335. (setq org-done-keywords (and org-todo-keywords-1
  4336. (list (org-last org-todo-keywords-1)))))
  4337. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4338. (length org-scheduled-string)
  4339. (length org-clock-string)
  4340. (length org-closed-string)))
  4341. org-drawer-regexp
  4342. (concat "^[ \t]*:\\("
  4343. (mapconcat 'regexp-quote org-drawers "\\|")
  4344. "\\):[ \t]*$")
  4345. org-not-done-keywords
  4346. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4347. org-todo-regexp
  4348. (concat "\\("
  4349. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4350. "\\)")
  4351. org-not-done-regexp
  4352. (concat "\\("
  4353. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4354. "\\)")
  4355. org-not-done-heading-regexp
  4356. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4357. org-todo-line-regexp
  4358. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4359. org-complex-heading-regexp
  4360. (concat "^\\(\\*+\\)"
  4361. "\\(?: +" org-todo-regexp "\\)?"
  4362. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4363. "\\(?: +\\(.*?\\)\\)??"
  4364. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4365. "[ \t]*$")
  4366. org-complex-heading-regexp-format
  4367. (concat "^\\(\\*+\\)"
  4368. "\\(?: +" org-todo-regexp "\\)?"
  4369. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4370. "\\(?: +"
  4371. ;; Stats cookies can be stuck to body.
  4372. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4373. "\\(%s\\)"
  4374. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4375. "\\)"
  4376. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4377. "[ \t]*$")
  4378. org-todo-line-tags-regexp
  4379. (concat "^\\(\\*+\\)"
  4380. "\\(?: +" org-todo-regexp "\\)?"
  4381. "\\(?: +\\(.*?\\)\\)??"
  4382. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4383. "[ \t]*$")
  4384. org-deadline-regexp (concat "\\<" org-deadline-string)
  4385. org-deadline-time-regexp
  4386. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4387. org-deadline-line-regexp
  4388. (concat "\\<\\(" org-deadline-string "\\).*")
  4389. org-scheduled-regexp
  4390. (concat "\\<" org-scheduled-string)
  4391. org-scheduled-time-regexp
  4392. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4393. org-closed-time-regexp
  4394. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4395. org-keyword-time-regexp
  4396. (concat "\\<\\(" org-scheduled-string
  4397. "\\|" org-deadline-string
  4398. "\\|" org-closed-string
  4399. "\\|" org-clock-string "\\)"
  4400. " *[[<]\\([^]>]+\\)[]>]")
  4401. org-keyword-time-not-clock-regexp
  4402. (concat "\\<\\(" org-scheduled-string
  4403. "\\|" org-deadline-string
  4404. "\\|" org-closed-string
  4405. "\\)"
  4406. " *[[<]\\([^]>]+\\)[]>]")
  4407. org-maybe-keyword-time-regexp
  4408. (concat "\\(\\<\\(" org-scheduled-string
  4409. "\\|" org-deadline-string
  4410. "\\|" org-closed-string
  4411. "\\|" org-clock-string "\\)\\)?"
  4412. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4413. org-all-time-keywords
  4414. (mapcar (lambda (w) (substring w 0 -1))
  4415. (list org-scheduled-string org-deadline-string
  4416. org-clock-string org-closed-string)))
  4417. (org-compute-latex-and-related-regexp)
  4418. (org-set-font-lock-defaults))))
  4419. (defun org-file-contents (file &optional noerror)
  4420. "Return the contents of FILE, as a string."
  4421. (if (or (not file)
  4422. (not (file-readable-p file)))
  4423. (if noerror
  4424. (progn
  4425. (message "Cannot read file \"%s\"" file)
  4426. (ding) (sit-for 2)
  4427. "")
  4428. (error "Cannot read file \"%s\"" file))
  4429. (with-temp-buffer
  4430. (insert-file-contents file)
  4431. (buffer-string))))
  4432. (defun org-extract-log-state-settings (x)
  4433. "Extract the log state setting from a TODO keyword string.
  4434. This will extract info from a string like \"WAIT(w@/!)\"."
  4435. (let (kw key log1 log2)
  4436. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4437. (setq kw (match-string 1 x)
  4438. key (and (match-end 2) (match-string 2 x))
  4439. log1 (and (match-end 3) (match-string 3 x))
  4440. log2 (and (match-end 4) (match-string 4 x)))
  4441. (and (or log1 log2)
  4442. (list kw
  4443. (and log1 (if (equal log1 "!") 'time 'note))
  4444. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4445. (defun org-remove-keyword-keys (list)
  4446. "Remove a pair of parenthesis at the end of each string in LIST."
  4447. (mapcar (lambda (x)
  4448. (if (string-match "(.*)$" x)
  4449. (substring x 0 (match-beginning 0))
  4450. x))
  4451. list))
  4452. (defun org-assign-fast-keys (alist)
  4453. "Assign fast keys to a keyword-key alist.
  4454. Respect keys that are already there."
  4455. (let (new e (alt ?0))
  4456. (while (setq e (pop alist))
  4457. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4458. (cdr e)) ;; Key already assigned.
  4459. (push e new)
  4460. (let ((clist (string-to-list (downcase (car e))))
  4461. (used (append new alist)))
  4462. (when (= (car clist) ?@)
  4463. (pop clist))
  4464. (while (and clist (rassoc (car clist) used))
  4465. (pop clist))
  4466. (unless clist
  4467. (while (rassoc alt used)
  4468. (incf alt)))
  4469. (push (cons (car e) (or (car clist) alt)) new))))
  4470. (nreverse new)))
  4471. ;;; Some variables used in various places
  4472. (defvar org-window-configuration nil
  4473. "Used in various places to store a window configuration.")
  4474. (defvar org-selected-window nil
  4475. "Used in various places to store a window configuration.")
  4476. (defvar org-finish-function nil
  4477. "Function to be called when `C-c C-c' is used.
  4478. This is for getting out of special buffers like capture.")
  4479. ;; FIXME: Occasionally check by commenting these, to make sure
  4480. ;; no other functions uses these, forgetting to let-bind them.
  4481. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4482. (defvar org-last-state)
  4483. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4484. ;; Defined somewhere in this file, but used before definition.
  4485. (defvar org-entities) ;; defined in org-entities.el
  4486. (defvar org-struct-menu)
  4487. (defvar org-org-menu)
  4488. (defvar org-tbl-menu)
  4489. ;;;; Define the Org-mode
  4490. ;; We use a before-change function to check if a table might need
  4491. ;; an update.
  4492. (defvar org-table-may-need-update t
  4493. "Indicates that a table might need an update.
  4494. This variable is set by `org-before-change-function'.
  4495. `org-table-align' sets it back to nil.")
  4496. (defun org-before-change-function (beg end)
  4497. "Every change indicates that a table might need an update."
  4498. (setq org-table-may-need-update t))
  4499. (defvar org-mode-map)
  4500. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4501. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4502. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4503. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4504. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4505. (defvar org-table-buffer-is-an nil)
  4506. (defvar bidi-paragraph-direction)
  4507. (defvar buffer-face-mode-face)
  4508. (require 'outline)
  4509. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4510. (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"))
  4511. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4512. ;; Other stuff we need.
  4513. (require 'time-date)
  4514. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4515. (require 'easymenu)
  4516. (require 'overlay)
  4517. ;; (require 'org-macs) moved higher up in the file before it is first used
  4518. (require 'org-entities)
  4519. ;; (require 'org-compat) moved higher up in the file before it is first used
  4520. (require 'org-faces)
  4521. (require 'org-list)
  4522. (require 'org-pcomplete)
  4523. (require 'org-src)
  4524. (require 'org-footnote)
  4525. (require 'org-macro)
  4526. ;; babel
  4527. (require 'ob)
  4528. ;;;###autoload
  4529. (define-derived-mode org-mode outline-mode "Org"
  4530. "Outline-based notes management and organizer, alias
  4531. \"Carsten's outline-mode for keeping track of everything.\"
  4532. Org-mode develops organizational tasks around a NOTES file which
  4533. contains information about projects as plain text. Org-mode is
  4534. implemented on top of outline-mode, which is ideal to keep the content
  4535. of large files well structured. It supports ToDo items, deadlines and
  4536. time stamps, which magically appear in the diary listing of the Emacs
  4537. calendar. Tables are easily created with a built-in table editor.
  4538. Plain text URL-like links connect to websites, emails (VM), Usenet
  4539. messages (Gnus), BBDB entries, and any files related to the project.
  4540. For printing and sharing of notes, an Org-mode file (or a part of it)
  4541. can be exported as a structured ASCII or HTML file.
  4542. The following commands are available:
  4543. \\{org-mode-map}"
  4544. ;; Get rid of Outline menus, they are not needed
  4545. ;; Need to do this here because define-derived-mode sets up
  4546. ;; the keymap so late. Still, it is a waste to call this each time
  4547. ;; we switch another buffer into org-mode.
  4548. (if (featurep 'xemacs)
  4549. (when (boundp 'outline-mode-menu-heading)
  4550. ;; Assume this is Greg's port, it uses easymenu
  4551. (easy-menu-remove outline-mode-menu-heading)
  4552. (easy-menu-remove outline-mode-menu-show)
  4553. (easy-menu-remove outline-mode-menu-hide))
  4554. (define-key org-mode-map [menu-bar headings] 'undefined)
  4555. (define-key org-mode-map [menu-bar hide] 'undefined)
  4556. (define-key org-mode-map [menu-bar show] 'undefined))
  4557. (org-load-modules-maybe)
  4558. (easy-menu-add org-org-menu)
  4559. (easy-menu-add org-tbl-menu)
  4560. (org-install-agenda-files-menu)
  4561. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4562. (add-to-invisibility-spec '(org-cwidth))
  4563. (add-to-invisibility-spec '(org-hide-block . t))
  4564. (when (featurep 'xemacs)
  4565. (org-set-local 'line-move-ignore-invisible t))
  4566. (org-set-local 'outline-regexp org-outline-regexp)
  4567. (org-set-local 'outline-level 'org-outline-level)
  4568. (setq bidi-paragraph-direction 'left-to-right)
  4569. (when (and org-ellipsis
  4570. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4571. (fboundp 'make-glyph-code))
  4572. (unless org-display-table
  4573. (setq org-display-table (make-display-table)))
  4574. (set-display-table-slot
  4575. org-display-table 4
  4576. (vconcat (mapcar
  4577. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4578. org-ellipsis)))
  4579. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4580. (setq buffer-display-table org-display-table))
  4581. (org-set-regexps-and-options)
  4582. (when (and org-tag-faces (not org-tags-special-faces-re))
  4583. ;; tag faces set outside customize.... force initialization.
  4584. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4585. ;; Calc embedded
  4586. (org-set-local 'calc-embedded-open-mode "# ")
  4587. ;; Modify a few syntax entries
  4588. (modify-syntax-entry ?< "(")
  4589. (modify-syntax-entry ?> ")")
  4590. (modify-syntax-entry ?{ "(")
  4591. (modify-syntax-entry ?} ")")
  4592. (modify-syntax-entry ?@ "w")
  4593. (modify-syntax-entry ?\" "\"")
  4594. (if org-startup-truncated (setq truncate-lines t))
  4595. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4596. (org-set-local 'font-lock-unfontify-region-function
  4597. 'org-unfontify-region)
  4598. ;; Activate before-change-function
  4599. (org-set-local 'org-table-may-need-update t)
  4600. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4601. 'local)
  4602. ;; Check for running clock before killing a buffer
  4603. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4604. ;; Initialize macros templates.
  4605. (org-macro-initialize-templates)
  4606. ;; Initialize radio targets.
  4607. (org-update-radio-target-regexp)
  4608. ;; Indentation.
  4609. (org-set-local 'indent-line-function 'org-indent-line)
  4610. (org-set-local 'indent-region-function 'org-indent-region)
  4611. ;; Filling and auto-filling.
  4612. (org-setup-filling)
  4613. ;; Comments.
  4614. (org-setup-comments-handling)
  4615. ;; Beginning/end of defun
  4616. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4617. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4618. ;; Next error for sparse trees
  4619. (org-set-local 'next-error-function 'org-occur-next-match)
  4620. ;; Make sure dependence stuff works reliably, even for users who set it
  4621. ;; too late :-(
  4622. (if org-enforce-todo-dependencies
  4623. (add-hook 'org-blocker-hook
  4624. 'org-block-todo-from-children-or-siblings-or-parent)
  4625. (remove-hook 'org-blocker-hook
  4626. 'org-block-todo-from-children-or-siblings-or-parent))
  4627. (if org-enforce-todo-checkbox-dependencies
  4628. (add-hook 'org-blocker-hook
  4629. 'org-block-todo-from-checkboxes)
  4630. (remove-hook 'org-blocker-hook
  4631. 'org-block-todo-from-checkboxes))
  4632. ;; Align options lines
  4633. (org-set-local
  4634. 'align-mode-rules-list
  4635. '((org-in-buffer-settings
  4636. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4637. (modes . '(org-mode)))))
  4638. ;; Imenu
  4639. (org-set-local 'imenu-create-index-function
  4640. 'org-imenu-get-tree)
  4641. ;; Make isearch reveal context
  4642. (if (or (featurep 'xemacs)
  4643. (not (boundp 'outline-isearch-open-invisible-function)))
  4644. ;; Emacs 21 and XEmacs make use of the hook
  4645. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4646. ;; Emacs 22 deals with this through a special variable
  4647. (org-set-local 'outline-isearch-open-invisible-function
  4648. (lambda (&rest ignore) (org-show-context 'isearch))))
  4649. ;; Setup the pcomplete hooks
  4650. (set (make-local-variable 'pcomplete-command-completion-function)
  4651. 'org-pcomplete-initial)
  4652. (set (make-local-variable 'pcomplete-command-name-function)
  4653. 'org-command-at-point)
  4654. (set (make-local-variable 'pcomplete-default-completion-function)
  4655. 'ignore)
  4656. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4657. 'org-parse-arguments)
  4658. (set (make-local-variable 'pcomplete-termination-string) "")
  4659. (when (>= emacs-major-version 23)
  4660. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4661. ;; If empty file that did not turn on org-mode automatically, make it to.
  4662. (if (and org-insert-mode-line-in-empty-file
  4663. (org-called-interactively-p 'any)
  4664. (= (point-min) (point-max)))
  4665. (insert "# -*- mode: org -*-\n\n"))
  4666. (unless org-inhibit-startup
  4667. (org-unmodified
  4668. (and org-startup-with-beamer-mode (org-beamer-mode))
  4669. (when org-startup-align-all-tables
  4670. (org-table-map-tables 'org-table-align 'quietly))
  4671. (when org-startup-with-inline-images
  4672. (org-display-inline-images))
  4673. (when org-startup-with-latex-preview
  4674. (org-preview-latex-fragment))
  4675. (unless org-inhibit-startup-visibility-stuff
  4676. (org-set-startup-visibility))))
  4677. ;; Try to set org-hide correctly
  4678. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4679. (defvar org-mode-transpose-word-syntax-table
  4680. (let ((st (make-syntax-table)))
  4681. (mapc (lambda(c) (modify-syntax-entry
  4682. (string-to-char (car c)) "w p" st))
  4683. org-emphasis-alist)
  4684. st))
  4685. (when (fboundp 'abbrev-table-put)
  4686. (abbrev-table-put org-mode-abbrev-table
  4687. :parents (list text-mode-abbrev-table)))
  4688. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4689. (defun org-find-invisible-foreground ()
  4690. (let ((candidates (remove
  4691. "unspecified-bg"
  4692. (nconc
  4693. (list (face-background 'default)
  4694. (face-background 'org-default))
  4695. (mapcar
  4696. (lambda (alist)
  4697. (when (boundp alist)
  4698. (cdr (assoc 'background-color (symbol-value alist)))))
  4699. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4700. (list (face-foreground 'org-hide))))))
  4701. (car (remove nil candidates))))
  4702. (defun org-current-time (&optional rounding-minutes past)
  4703. "Current time, possibly rounded to ROUNDING-MINUTES.
  4704. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4705. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4706. the rounding returns a past time."
  4707. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4708. (car org-time-stamp-rounding-minutes)))
  4709. (time (decode-time)) res)
  4710. (if (< r 1)
  4711. (current-time)
  4712. (setq res
  4713. (apply 'encode-time
  4714. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4715. (nthcdr 2 time))))
  4716. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4717. (seconds-to-time (- (org-float-time res) (* r 60)))
  4718. res))))
  4719. (defun org-today ()
  4720. "Return today date, considering `org-extend-today-until'."
  4721. (time-to-days
  4722. (time-subtract (current-time)
  4723. (list 0 (* 3600 org-extend-today-until) 0))))
  4724. ;;;; Font-Lock stuff, including the activators
  4725. (defvar org-mouse-map (make-sparse-keymap))
  4726. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4727. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4728. (when org-mouse-1-follows-link
  4729. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4730. (when org-tab-follows-link
  4731. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4732. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4733. (require 'font-lock)
  4734. (defconst org-non-link-chars "]\t\n\r<>")
  4735. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4736. "shell" "elisp" "doi" "message"))
  4737. (defvar org-link-types-re nil
  4738. "Matches a link that has a url-like prefix like \"http:\"")
  4739. (defvar org-link-re-with-space nil
  4740. "Matches a link with spaces, optional angular brackets around it.")
  4741. (defvar org-link-re-with-space2 nil
  4742. "Matches a link with spaces, optional angular brackets around it.")
  4743. (defvar org-link-re-with-space3 nil
  4744. "Matches a link with spaces, only for internal part in bracket links.")
  4745. (defvar org-angle-link-re nil
  4746. "Matches link with angular brackets, spaces are allowed.")
  4747. (defvar org-plain-link-re nil
  4748. "Matches plain link, without spaces.")
  4749. (defvar org-bracket-link-regexp nil
  4750. "Matches a link in double brackets.")
  4751. (defvar org-bracket-link-analytic-regexp nil
  4752. "Regular expression used to analyze links.
  4753. Here is what the match groups contain after a match:
  4754. 1: http:
  4755. 2: http
  4756. 3: path
  4757. 4: [desc]
  4758. 5: desc")
  4759. (defvar org-bracket-link-analytic-regexp++ nil
  4760. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4761. (defvar org-any-link-re nil
  4762. "Regular expression matching any link.")
  4763. (defconst org-match-sexp-depth 3
  4764. "Number of stacked braces for sub/superscript matching.")
  4765. (defun org-create-multibrace-regexp (left right n)
  4766. "Create a regular expression which will match a balanced sexp.
  4767. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4768. as single character strings.
  4769. The regexp returned will match the entire expression including the
  4770. delimiters. It will also define a single group which contains the
  4771. match except for the outermost delimiters. The maximum depth of
  4772. stacked delimiters is N. Escaping delimiters is not possible."
  4773. (let* ((nothing (concat "[^" left right "]*?"))
  4774. (or "\\|")
  4775. (re nothing)
  4776. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4777. (while (> n 1)
  4778. (setq n (1- n)
  4779. re (concat re or next)
  4780. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4781. (concat left "\\(" re "\\)" right)))
  4782. (defvar org-match-substring-regexp
  4783. (concat
  4784. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4785. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4786. "\\|"
  4787. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4788. "\\|"
  4789. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4790. "The regular expression matching a sub- or superscript.")
  4791. (defvar org-match-substring-with-braces-regexp
  4792. (concat
  4793. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4794. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4795. "\\)")
  4796. "The regular expression matching a sub- or superscript, forcing braces.")
  4797. (defun org-make-link-regexps ()
  4798. "Update the link regular expressions.
  4799. This should be called after the variable `org-link-types' has changed."
  4800. (setq org-link-types-re
  4801. (concat
  4802. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4803. org-link-re-with-space
  4804. (concat
  4805. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4806. "\\([^" org-non-link-chars " ]"
  4807. "[^" org-non-link-chars "]*"
  4808. "[^" org-non-link-chars " ]\\)>?")
  4809. org-link-re-with-space2
  4810. (concat
  4811. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4812. "\\([^" org-non-link-chars " ]"
  4813. "[^\t\n\r]*"
  4814. "[^" org-non-link-chars " ]\\)>?")
  4815. org-link-re-with-space3
  4816. (concat
  4817. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4818. "\\([^" org-non-link-chars " ]"
  4819. "[^\t\n\r]*\\)")
  4820. org-angle-link-re
  4821. (concat
  4822. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4823. "\\([^" org-non-link-chars " ]"
  4824. "[^" org-non-link-chars "]*"
  4825. "\\)>")
  4826. org-plain-link-re
  4827. (concat
  4828. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4829. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4830. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4831. org-bracket-link-regexp
  4832. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4833. org-bracket-link-analytic-regexp
  4834. (concat
  4835. "\\[\\["
  4836. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4837. "\\([^]]+\\)"
  4838. "\\]"
  4839. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4840. "\\]")
  4841. org-bracket-link-analytic-regexp++
  4842. (concat
  4843. "\\[\\["
  4844. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4845. "\\([^]]+\\)"
  4846. "\\]"
  4847. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4848. "\\]")
  4849. org-any-link-re
  4850. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4851. org-angle-link-re "\\)\\|\\("
  4852. org-plain-link-re "\\)")))
  4853. (org-make-link-regexps)
  4854. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4855. "Regular expression for fast time stamp matching.")
  4856. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4857. "Regular expression for fast time stamp matching.")
  4858. (defconst org-ts-regexp0
  4859. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4860. "Regular expression matching time strings for analysis.
  4861. This one does not require the space after the date, so it can be used
  4862. on a string that terminates immediately after the date.")
  4863. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4864. "Regular expression matching time strings for analysis.")
  4865. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4866. "Regular expression matching time stamps, with groups.")
  4867. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4868. "Regular expression matching time stamps (also [..]), with groups.")
  4869. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4870. "Regular expression matching a time stamp range.")
  4871. (defconst org-tr-regexp-both
  4872. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4873. "Regular expression matching a time stamp range.")
  4874. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4875. org-ts-regexp "\\)?")
  4876. "Regular expression matching a time stamp or time stamp range.")
  4877. (defconst org-tsr-regexp-both
  4878. (concat "[^][]" ;; Don't activate dates in links
  4879. org-ts-regexp-both "\\(--?-?"
  4880. org-ts-regexp-both "\\)?")
  4881. "Regular expression matching a time stamp or time stamp range.
  4882. The time stamps may be either active or inactive.")
  4883. (defvar org-emph-face nil)
  4884. (defun org-do-emphasis-faces (limit)
  4885. "Run through the buffer and add overlays to emphasized strings."
  4886. (let (rtn a)
  4887. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4888. (if (not (= (char-after (match-beginning 3))
  4889. (char-after (match-beginning 4))))
  4890. (progn
  4891. (setq rtn t)
  4892. (setq a (assoc (match-string 3) org-emphasis-alist))
  4893. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4894. 'face
  4895. (nth 1 a))
  4896. (and (nth 4 a)
  4897. (org-remove-flyspell-overlays-in
  4898. (match-beginning 0) (match-end 0)))
  4899. (add-text-properties (match-beginning 2) (match-end 2)
  4900. '(font-lock-multiline t org-emphasis t))
  4901. (when org-hide-emphasis-markers
  4902. (add-text-properties (match-end 4) (match-beginning 5)
  4903. '(invisible org-link))
  4904. (add-text-properties (match-beginning 3) (match-end 3)
  4905. '(invisible org-link)))))
  4906. (backward-char 1))
  4907. rtn))
  4908. (defun org-emphasize (&optional char)
  4909. "Insert or change an emphasis, i.e. a font like bold or italic.
  4910. If there is an active region, change that region to a new emphasis.
  4911. If there is no region, just insert the marker characters and position
  4912. the cursor between them.
  4913. CHAR should be either the marker character, or the first character of the
  4914. HTML tag associated with that emphasis. If CHAR is a space, the means
  4915. to remove the emphasis of the selected region.
  4916. If char is not given (for example in an interactive call) it
  4917. will be prompted for."
  4918. (interactive)
  4919. (let ((eal org-emphasis-alist) e det
  4920. (erc org-emphasis-regexp-components)
  4921. (prompt "")
  4922. (string "") beg end move tag c s)
  4923. (if (org-region-active-p)
  4924. (setq beg (region-beginning) end (region-end)
  4925. string (buffer-substring beg end))
  4926. (setq move t))
  4927. (while (setq e (pop eal))
  4928. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4929. c (aref tag 0))
  4930. (push (cons c (string-to-char (car e))) det)
  4931. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4932. (substring tag 1)))))
  4933. (setq det (nreverse det))
  4934. (unless char
  4935. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4936. (setq char (read-char-exclusive)))
  4937. (setq char (or (cdr (assoc char det)) char))
  4938. (if (equal char ?\ )
  4939. (setq s "" move nil)
  4940. (unless (assoc (char-to-string char) org-emphasis-alist)
  4941. (error "No such emphasis marker: \"%c\"" char))
  4942. (setq s (char-to-string char)))
  4943. (while (and (> (length string) 1)
  4944. (equal (substring string 0 1) (substring string -1))
  4945. (assoc (substring string 0 1) org-emphasis-alist))
  4946. (setq string (substring string 1 -1)))
  4947. (setq string (concat s string s))
  4948. (if beg (delete-region beg end))
  4949. (unless (or (bolp)
  4950. (string-match (concat "[" (nth 0 erc) "\n]")
  4951. (char-to-string (char-before (point)))))
  4952. (insert " "))
  4953. (unless (or (eobp)
  4954. (string-match (concat "[" (nth 1 erc) "\n]")
  4955. (char-to-string (char-after (point)))))
  4956. (insert " ") (backward-char 1))
  4957. (insert string)
  4958. (and move (backward-char 1))))
  4959. (defconst org-nonsticky-props
  4960. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4961. (defsubst org-rear-nonsticky-at (pos)
  4962. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4963. (defun org-activate-plain-links (limit)
  4964. "Run through the buffer and add overlays to links."
  4965. (catch 'exit
  4966. (let (f hl)
  4967. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4968. (not (org-in-src-block-p)))
  4969. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4970. (setq f (get-text-property (match-beginning 0) 'face))
  4971. (setq hl (org-match-string-no-properties 0))
  4972. (if (or (eq f 'org-tag)
  4973. (and (listp f) (memq 'org-tag f)))
  4974. nil
  4975. (add-text-properties (match-beginning 0) (match-end 0)
  4976. (list 'mouse-face 'highlight
  4977. 'face 'org-link
  4978. 'htmlize-link `(:uri ,hl)
  4979. 'keymap org-mouse-map))
  4980. (org-rear-nonsticky-at (match-end 0)))
  4981. t))))
  4982. (defun org-activate-code (limit)
  4983. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4984. (progn
  4985. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4986. (remove-text-properties (match-beginning 0) (match-end 0)
  4987. '(display t invisible t intangible t))
  4988. t)))
  4989. (defcustom org-src-fontify-natively nil
  4990. "When non-nil, fontify code in code blocks."
  4991. :type 'boolean
  4992. :version "24.1"
  4993. :group 'org-appearance
  4994. :group 'org-babel)
  4995. (defcustom org-allow-promoting-top-level-subtree nil
  4996. "When non-nil, allow promoting a top level subtree.
  4997. The leading star of the top level headline will be replaced
  4998. by a #."
  4999. :type 'boolean
  5000. :version "24.1"
  5001. :group 'org-appearance)
  5002. (defun org-fontify-meta-lines-and-blocks (limit)
  5003. (condition-case nil
  5004. (org-fontify-meta-lines-and-blocks-1 limit)
  5005. (error (message "org-mode fontification error"))))
  5006. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5007. "Fontify #+ lines and blocks, in the correct ways."
  5008. (let ((case-fold-search t))
  5009. (if (re-search-forward
  5010. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5011. limit t)
  5012. (let ((beg (match-beginning 0))
  5013. (block-start (match-end 0))
  5014. (block-end nil)
  5015. (lang (match-string 7))
  5016. (beg1 (line-beginning-position 2))
  5017. (dc1 (downcase (match-string 2)))
  5018. (dc3 (downcase (match-string 3)))
  5019. end end1 quoting block-type ovl)
  5020. (cond
  5021. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5022. ;; a single line of backend-specific content
  5023. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5024. (remove-text-properties (match-beginning 0) (match-end 0)
  5025. '(display t invisible t intangible t))
  5026. (add-text-properties (match-beginning 1) (match-end 3)
  5027. '(font-lock-fontified t face org-meta-line))
  5028. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5029. '(font-lock-fontified t face org-block))
  5030. ; for backend-specific code
  5031. t)
  5032. ((and (match-end 4) (equal dc3 "+begin"))
  5033. ;; Truly a block
  5034. (setq block-type (downcase (match-string 5))
  5035. quoting (member block-type org-protecting-blocks))
  5036. (when (re-search-forward
  5037. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5038. nil t) ;; on purpose, we look further than LIMIT
  5039. (setq end (min (point-max) (match-end 0))
  5040. end1 (min (point-max) (1- (match-beginning 0))))
  5041. (setq block-end (match-beginning 0))
  5042. (when quoting
  5043. (remove-text-properties beg end
  5044. '(display t invisible t intangible t)))
  5045. (add-text-properties
  5046. beg end
  5047. '(font-lock-fontified t font-lock-multiline t))
  5048. (add-text-properties beg beg1 '(face org-meta-line))
  5049. (add-text-properties end1 (min (point-max) (1+ end))
  5050. '(face org-meta-line)) ; for end_src
  5051. (cond
  5052. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5053. (org-src-font-lock-fontify-block lang block-start block-end)
  5054. ;; remove old background overlays
  5055. (mapc (lambda (ov)
  5056. (if (eq (overlay-get ov 'face) 'org-block-background)
  5057. (delete-overlay ov)))
  5058. (overlays-at (/ (+ beg1 block-end) 2)))
  5059. ;; add a background overlay
  5060. (setq ovl (make-overlay beg1 block-end))
  5061. (overlay-put ovl 'face 'org-block-background)
  5062. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5063. (quoting
  5064. (add-text-properties beg1 (min (point-max) (1+ end1))
  5065. '(face org-block))) ; end of source block
  5066. ((not org-fontify-quote-and-verse-blocks))
  5067. ((string= block-type "quote")
  5068. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5069. ((string= block-type "verse")
  5070. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5071. (add-text-properties beg beg1 '(face org-block-begin-line))
  5072. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5073. '(face org-block-end-line))
  5074. t))
  5075. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5076. (add-text-properties
  5077. beg (match-end 3)
  5078. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5079. '(font-lock-fontified t invisible t)
  5080. '(font-lock-fontified t face org-document-info-keyword)))
  5081. (add-text-properties
  5082. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5083. (if (string-equal dc1 "+title:")
  5084. '(font-lock-fontified t face org-document-title)
  5085. '(font-lock-fontified t face org-document-info))))
  5086. ((or (equal dc1 "+results")
  5087. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5088. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5089. "+call:" "+header:" "+headers:" "+name:"))
  5090. (and (match-end 4) (equal dc3 "+attr")))
  5091. (add-text-properties
  5092. beg (match-end 0)
  5093. '(font-lock-fontified t face org-meta-line))
  5094. t)
  5095. ((member dc3 '(" " ""))
  5096. (add-text-properties
  5097. beg (match-end 0)
  5098. '(font-lock-fontified t face font-lock-comment-face)))
  5099. ((not (member (char-after beg) '(?\ ?\t)))
  5100. ;; just any other in-buffer setting, but not indented
  5101. (add-text-properties
  5102. beg (match-end 0)
  5103. '(font-lock-fontified t face org-meta-line))
  5104. t)
  5105. (t nil))))))
  5106. (defun org-activate-angle-links (limit)
  5107. "Run through the buffer and add overlays to links."
  5108. (if (and (re-search-forward org-angle-link-re limit t)
  5109. (not (org-in-src-block-p)))
  5110. (progn
  5111. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5112. (add-text-properties (match-beginning 0) (match-end 0)
  5113. (list 'mouse-face 'highlight
  5114. 'keymap org-mouse-map))
  5115. (org-rear-nonsticky-at (match-end 0))
  5116. t)))
  5117. (defun org-activate-footnote-links (limit)
  5118. "Run through the buffer and add overlays to footnotes."
  5119. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5120. (when fn
  5121. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5122. (org-remove-flyspell-overlays-in beg end)
  5123. (add-text-properties beg end
  5124. (list 'mouse-face 'highlight
  5125. 'keymap org-mouse-map
  5126. 'help-echo
  5127. (if (= (point-at-bol) beg)
  5128. "Footnote definition"
  5129. "Footnote reference")
  5130. 'font-lock-fontified t
  5131. 'font-lock-multiline t
  5132. 'face 'org-footnote))))))
  5133. (defun org-activate-bracket-links (limit)
  5134. "Run through the buffer and add overlays to bracketed links."
  5135. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5136. (not (org-in-src-block-p)))
  5137. (let* ((hl (org-match-string-no-properties 1))
  5138. (help (concat "LINK: " hl))
  5139. ;; FIXME: Above we should remove the escapes. But that
  5140. ;; requires another match, protecting match data, a lot
  5141. ;; of overhead for font-lock.
  5142. (ip (org-maybe-intangible
  5143. (list 'invisible 'org-link
  5144. 'keymap org-mouse-map 'mouse-face 'highlight
  5145. 'font-lock-multiline t 'help-echo help
  5146. 'htmlize-link `(:uri ,hl))))
  5147. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5148. 'font-lock-multiline t 'help-echo help
  5149. 'htmlize-link `(:uri ,hl))))
  5150. ;; We need to remove the invisible property here. Table narrowing
  5151. ;; may have made some of this invisible.
  5152. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5153. (remove-text-properties (match-beginning 0) (match-end 0)
  5154. '(invisible nil))
  5155. (if (match-end 3)
  5156. (progn
  5157. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5158. (org-rear-nonsticky-at (match-beginning 3))
  5159. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5160. (org-rear-nonsticky-at (match-end 3))
  5161. (add-text-properties (match-end 3) (match-end 0) ip)
  5162. (org-rear-nonsticky-at (match-end 0)))
  5163. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5164. (org-rear-nonsticky-at (match-beginning 1))
  5165. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5166. (org-rear-nonsticky-at (match-end 1))
  5167. (add-text-properties (match-end 1) (match-end 0) ip)
  5168. (org-rear-nonsticky-at (match-end 0)))
  5169. t)))
  5170. (defun org-activate-dates (limit)
  5171. "Run through the buffer and add overlays to dates."
  5172. (if (re-search-forward org-tsr-regexp-both limit t)
  5173. (progn
  5174. (org-remove-flyspell-overlays-in (1+ (match-beginning 0)) (match-end 0))
  5175. (add-text-properties (1+ (match-beginning 0)) (match-end 0)
  5176. (list 'mouse-face 'highlight
  5177. 'keymap org-mouse-map))
  5178. (org-rear-nonsticky-at (match-end 0))
  5179. (when org-display-custom-times
  5180. (if (match-end 3)
  5181. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5182. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5183. t)))
  5184. (defvar org-target-link-regexp nil
  5185. "Regular expression matching radio targets in plain text.")
  5186. (make-variable-buffer-local 'org-target-link-regexp)
  5187. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5188. "Regular expression matching a link target.")
  5189. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5190. "Regular expression matching a radio target.")
  5191. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5192. "Regular expression matching any target.")
  5193. (defun org-activate-target-links (limit)
  5194. "Run through the buffer and add overlays to target matches."
  5195. (when org-target-link-regexp
  5196. (let ((case-fold-search t))
  5197. (if (re-search-forward org-target-link-regexp limit t)
  5198. (progn
  5199. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5200. (add-text-properties (match-beginning 0) (match-end 0)
  5201. (list 'mouse-face 'highlight
  5202. 'keymap org-mouse-map
  5203. 'help-echo "Radio target link"
  5204. 'org-linked-text t))
  5205. (org-rear-nonsticky-at (match-end 0))
  5206. t)))))
  5207. (defun org-update-radio-target-regexp ()
  5208. "Find all radio targets in this file and update the regular expression."
  5209. (interactive)
  5210. (when (memq 'radio org-activate-links)
  5211. (setq org-target-link-regexp
  5212. (org-make-target-link-regexp (org-all-targets 'radio)))
  5213. (org-restart-font-lock)))
  5214. (defun org-hide-wide-columns (limit)
  5215. (let (s e)
  5216. (setq s (text-property-any (point) (or limit (point-max))
  5217. 'org-cwidth t))
  5218. (when s
  5219. (setq e (next-single-property-change s 'org-cwidth))
  5220. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5221. (goto-char e)
  5222. t)))
  5223. (defvar org-latex-and-related-regexp nil
  5224. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5225. (defvar org-match-substring-regexp)
  5226. (defvar org-match-substring-with-braces-regexp)
  5227. (defun org-compute-latex-and-related-regexp ()
  5228. "Compute regular expression for LaTeX, entities and sub/superscript.
  5229. Result depends on variable `org-highlight-latex-and-related'."
  5230. (org-set-local
  5231. 'org-latex-and-related-regexp
  5232. (let* ((re-sub
  5233. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5234. ((eq org-use-sub-superscripts '{})
  5235. (list org-match-substring-with-braces-regexp))
  5236. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5237. (re-latex
  5238. (when (memq 'latex org-highlight-latex-and-related)
  5239. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5240. (delq nil
  5241. (mapcar (lambda (x)
  5242. (and (member (car x) matchers) (nth 1 x)))
  5243. org-latex-regexps)))))
  5244. (re-entities
  5245. (when (memq 'entities org-highlight-latex-and-related)
  5246. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5247. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5248. (defun org-do-latex-and-related (limit)
  5249. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5250. LIMIT bounds the search for syntax to highlight. Stop at first
  5251. highlighted object, if any. Return t if some highlighting was
  5252. done, nil otherwise."
  5253. (when org-highlight-latex-and-related
  5254. (catch 'found
  5255. (while (re-search-forward org-latex-and-related-regexp limit t)
  5256. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5257. 'face))
  5258. '(org-code org-verbatim underline))
  5259. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5260. '(?_ ?^))
  5261. 1
  5262. 0)))
  5263. (font-lock-prepend-text-property
  5264. (+ offset (match-beginning 0)) (match-end 0)
  5265. 'face 'org-latex-and-related)
  5266. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5267. '(font-lock-multiline t)))
  5268. (throw 'found t)))
  5269. nil)))
  5270. (defun org-restart-font-lock ()
  5271. "Restart `font-lock-mode', to force refontification."
  5272. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5273. (font-lock-mode -1)
  5274. (font-lock-mode 1)))
  5275. (defun org-all-targets (&optional radio)
  5276. "Return a list of all targets in this file.
  5277. When optional argument RADIO is non-nil, only find radio
  5278. targets."
  5279. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5280. (save-excursion
  5281. (goto-char (point-min))
  5282. (while (re-search-forward re nil t)
  5283. ;; Make sure point is really within the object.
  5284. (backward-char)
  5285. (let ((obj (org-element-context)))
  5286. (when (memq (org-element-type obj) '(radio-target target))
  5287. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5288. rtn)))
  5289. (defun org-make-target-link-regexp (targets)
  5290. "Make regular expression matching all strings in TARGETS.
  5291. The regular expression finds the targets also if there is a line break
  5292. between words."
  5293. (and targets
  5294. (concat
  5295. "\\<\\("
  5296. (mapconcat
  5297. (lambda (x)
  5298. (setq x (regexp-quote x))
  5299. (while (string-match " +" x)
  5300. (setq x (replace-match "\\s-+" t t x)))
  5301. x)
  5302. targets
  5303. "\\|")
  5304. "\\)\\>")))
  5305. (defun org-activate-tags (limit)
  5306. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5307. (progn
  5308. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5309. (add-text-properties (match-beginning 1) (match-end 1)
  5310. (list 'mouse-face 'highlight
  5311. 'keymap org-mouse-map))
  5312. (org-rear-nonsticky-at (match-end 1))
  5313. t)))
  5314. (defun org-outline-level ()
  5315. "Compute the outline level of the heading at point.
  5316. If this is called at a normal headline, the level is the number of stars.
  5317. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5318. (save-excursion
  5319. (if (not (condition-case nil
  5320. (org-back-to-heading t)
  5321. (error nil)))
  5322. 0
  5323. (looking-at org-outline-regexp)
  5324. (1- (- (match-end 0) (match-beginning 0))))))
  5325. (defvar org-font-lock-keywords nil)
  5326. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5327. "Regular expression matching a property line.")
  5328. (defvar org-font-lock-hook nil
  5329. "Functions to be called for special font lock stuff.")
  5330. (defvar org-font-lock-set-keywords-hook nil
  5331. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5332. This is called after `org-font-lock-extra-keywords' is defined, but before
  5333. it is installed to be used by font lock. This can be useful if something
  5334. needs to be inserted at a specific position in the font-lock sequence.")
  5335. (defun org-font-lock-hook (limit)
  5336. "Run `org-font-lock-hook' within LIMIT."
  5337. (run-hook-with-args 'org-font-lock-hook limit))
  5338. (defun org-set-font-lock-defaults ()
  5339. "Set font lock defaults for the current buffer."
  5340. (let* ((em org-fontify-emphasized-text)
  5341. (lk org-activate-links)
  5342. (org-font-lock-extra-keywords
  5343. (list
  5344. ;; Call the hook
  5345. '(org-font-lock-hook)
  5346. ;; Headlines
  5347. `(,(if org-fontify-whole-heading-line
  5348. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5349. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5350. (1 (org-get-level-face 1))
  5351. (2 (org-get-level-face 2))
  5352. (3 (org-get-level-face 3)))
  5353. ;; Table lines
  5354. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5355. (1 'org-table t))
  5356. ;; Table internals
  5357. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5358. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5359. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5360. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5361. ;; Drawers
  5362. (list org-drawer-regexp '(0 'org-special-keyword t))
  5363. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5364. ;; Properties
  5365. (list org-property-re
  5366. '(1 'org-special-keyword t)
  5367. '(3 'org-property-value t))
  5368. ;; Links
  5369. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5370. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5371. (if (memq 'plain lk) '(org-activate-plain-links))
  5372. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5373. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5374. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5375. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5376. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5377. '(org-hide-wide-columns (0 nil append))
  5378. ;; TODO keyword
  5379. (list (format org-heading-keyword-regexp-format
  5380. org-todo-regexp)
  5381. '(2 (org-get-todo-face 2) t))
  5382. ;; DONE
  5383. (if org-fontify-done-headline
  5384. (list (format org-heading-keyword-regexp-format
  5385. (concat
  5386. "\\(?:"
  5387. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5388. "\\)"))
  5389. '(2 'org-headline-done t))
  5390. nil)
  5391. ;; Priorities
  5392. '(org-font-lock-add-priority-faces)
  5393. ;; Tags
  5394. '(org-font-lock-add-tag-faces)
  5395. ;; Special keywords
  5396. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5397. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5398. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5399. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5400. ;; Emphasis
  5401. (if em
  5402. (if (featurep 'xemacs)
  5403. '(org-do-emphasis-faces (0 nil append))
  5404. '(org-do-emphasis-faces)))
  5405. ;; Checkboxes
  5406. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5407. 1 'org-checkbox prepend)
  5408. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5409. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5410. (0 (org-get-checkbox-statistics-face) t)))
  5411. ;; Description list items
  5412. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5413. 1 'org-list-dt prepend)
  5414. ;; ARCHIVEd headings
  5415. (list (concat
  5416. org-outline-regexp-bol
  5417. "\\(.*:" org-archive-tag ":.*\\)")
  5418. '(1 'org-archived prepend))
  5419. ;; Specials
  5420. '(org-do-latex-and-related)
  5421. '(org-fontify-entities)
  5422. '(org-raise-scripts)
  5423. ;; Code
  5424. '(org-activate-code (1 'org-code t))
  5425. ;; COMMENT
  5426. (list (format org-heading-keyword-regexp-format
  5427. (concat "\\("
  5428. org-comment-string "\\|" org-quote-string
  5429. "\\)"))
  5430. '(2 'org-special-keyword t))
  5431. ;; Blocks and meta lines
  5432. '(org-fontify-meta-lines-and-blocks)
  5433. )))
  5434. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5435. (run-hooks 'org-font-lock-set-keywords-hook)
  5436. ;; Now set the full font-lock-keywords
  5437. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5438. (org-set-local 'font-lock-defaults
  5439. '(org-font-lock-keywords t nil nil backward-paragraph))
  5440. (kill-local-variable 'font-lock-keywords) nil))
  5441. (defun org-toggle-pretty-entities ()
  5442. "Toggle the composition display of entities as UTF8 characters."
  5443. (interactive)
  5444. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5445. (org-restart-font-lock)
  5446. (if org-pretty-entities
  5447. (message "Entities are displayed as UTF8 characters")
  5448. (save-restriction
  5449. (widen)
  5450. (org-decompose-region (point-min) (point-max))
  5451. (message "Entities are displayed plain"))))
  5452. (defvar org-custom-properties-overlays nil
  5453. "List of overlays used for custom properties.")
  5454. (make-variable-buffer-local 'org-custom-properties-overlays)
  5455. (defun org-toggle-custom-properties-visibility ()
  5456. "Display or hide properties in `org-custom-properties'."
  5457. (interactive)
  5458. (if org-custom-properties-overlays
  5459. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5460. (setq org-custom-properties-overlays nil))
  5461. (unless (not org-custom-properties)
  5462. (save-excursion
  5463. (save-restriction
  5464. (widen)
  5465. (goto-char (point-min))
  5466. (while (re-search-forward org-property-re nil t)
  5467. (mapc (lambda(p)
  5468. (when (equal p (substring (match-string 1) 1 -1))
  5469. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5470. (overlay-put o 'invisible t)
  5471. (overlay-put o 'org-custom-property t)
  5472. (push o org-custom-properties-overlays))))
  5473. org-custom-properties)))))))
  5474. (defun org-fontify-entities (limit)
  5475. "Find an entity to fontify."
  5476. (let (ee)
  5477. (when org-pretty-entities
  5478. (catch 'match
  5479. (while (re-search-forward
  5480. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5481. limit t)
  5482. (if (and (not (org-in-indented-comment-line))
  5483. (setq ee (org-entity-get (match-string 1)))
  5484. (= (length (nth 6 ee)) 1))
  5485. (let*
  5486. ((end (if (equal (match-string 2) "{}")
  5487. (match-end 2)
  5488. (match-end 1))))
  5489. (add-text-properties
  5490. (match-beginning 0) end
  5491. (list 'font-lock-fontified t))
  5492. (compose-region (match-beginning 0) end
  5493. (nth 6 ee) nil)
  5494. (backward-char 1)
  5495. (throw 'match t))))
  5496. nil))))
  5497. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5498. "Fontify string S like in Org-mode."
  5499. (with-temp-buffer
  5500. (insert s)
  5501. (let ((org-odd-levels-only odd-levels))
  5502. (org-mode)
  5503. (font-lock-fontify-buffer)
  5504. (buffer-string))))
  5505. (defvar org-m nil)
  5506. (defvar org-l nil)
  5507. (defvar org-f nil)
  5508. (defun org-get-level-face (n)
  5509. "Get the right face for match N in font-lock matching of headlines."
  5510. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5511. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5512. (if org-cycle-level-faces
  5513. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5514. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5515. (cond
  5516. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5517. ((eq n 2) org-f)
  5518. (t (if org-level-color-stars-only nil org-f))))
  5519. (defun org-get-todo-face (kwd)
  5520. "Get the right face for a TODO keyword KWD.
  5521. If KWD is a number, get the corresponding match group."
  5522. (if (numberp kwd) (setq kwd (match-string kwd)))
  5523. (or (org-face-from-face-or-color
  5524. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5525. (and (member kwd org-done-keywords) 'org-done)
  5526. 'org-todo))
  5527. (defun org-face-from-face-or-color (context inherit face-or-color)
  5528. "Create a face list that inherits INHERIT, but sets the foreground color.
  5529. When FACE-OR-COLOR is not a string, just return it."
  5530. (if (stringp face-or-color)
  5531. (list :inherit inherit
  5532. (cdr (assoc context org-faces-easy-properties))
  5533. face-or-color)
  5534. face-or-color))
  5535. (defun org-font-lock-add-tag-faces (limit)
  5536. "Add the special tag faces."
  5537. (when (and org-tag-faces org-tags-special-faces-re)
  5538. (while (re-search-forward org-tags-special-faces-re limit t)
  5539. (add-text-properties (match-beginning 1) (match-end 1)
  5540. (list 'face (org-get-tag-face 1)
  5541. 'font-lock-fontified t))
  5542. (backward-char 1))))
  5543. (defun org-font-lock-add-priority-faces (limit)
  5544. "Add the special priority faces."
  5545. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5546. (when (save-match-data (org-at-heading-p))
  5547. (add-text-properties
  5548. (match-beginning 0) (match-end 0)
  5549. (list 'face (or (org-face-from-face-or-color
  5550. 'priority 'org-priority
  5551. (cdr (assoc (char-after (match-beginning 1))
  5552. org-priority-faces)))
  5553. 'org-priority)
  5554. 'font-lock-fontified t)))))
  5555. (defun org-get-tag-face (kwd)
  5556. "Get the right face for a TODO keyword KWD.
  5557. If KWD is a number, get the corresponding match group."
  5558. (if (numberp kwd) (setq kwd (match-string kwd)))
  5559. (or (org-face-from-face-or-color
  5560. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5561. 'org-tag))
  5562. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5563. "Remove fontification and activation overlays from links."
  5564. (font-lock-default-unfontify-region beg end)
  5565. (let* ((buffer-undo-list t)
  5566. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5567. (inhibit-modification-hooks t)
  5568. deactivate-mark buffer-file-name buffer-file-truename)
  5569. (org-decompose-region beg end)
  5570. (remove-text-properties beg end
  5571. '(mouse-face t keymap t org-linked-text t
  5572. invisible t intangible t
  5573. org-no-flyspell t org-emphasis t))
  5574. (org-remove-font-lock-display-properties beg end)))
  5575. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5576. ((raise 0.3) (height 0.7))
  5577. ((raise -0.5))
  5578. ((raise 0.5)))
  5579. "Display properties for showing superscripts and subscripts.")
  5580. (defun org-remove-font-lock-display-properties (beg end)
  5581. "Remove specific display properties that have been added by font lock.
  5582. The will remove the raise properties that are used to show superscripts
  5583. and subscripts."
  5584. (let (next prop)
  5585. (while (< beg end)
  5586. (setq next (next-single-property-change beg 'display nil end)
  5587. prop (get-text-property beg 'display))
  5588. (if (member prop org-script-display)
  5589. (put-text-property beg next 'display nil))
  5590. (setq beg next))))
  5591. (defun org-raise-scripts (limit)
  5592. "Add raise properties to sub/superscripts."
  5593. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5594. (if (re-search-forward
  5595. (if (eq org-use-sub-superscripts t)
  5596. org-match-substring-regexp
  5597. org-match-substring-with-braces-regexp)
  5598. limit t)
  5599. (let* ((pos (point)) table-p comment-p
  5600. (mpos (match-beginning 3))
  5601. (emph-p (get-text-property mpos 'org-emphasis))
  5602. (link-p (get-text-property mpos 'mouse-face))
  5603. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5604. (goto-char (point-at-bol))
  5605. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5606. comment-p (org-looking-at-p "[ \t]*#"))
  5607. (goto-char pos)
  5608. ;; FIXME: Should we go back one character here, for a_b^c
  5609. ;; (goto-char (1- pos)) ;????????????????????
  5610. (if (or comment-p emph-p link-p keyw-p)
  5611. t
  5612. (put-text-property (match-beginning 3) (match-end 0)
  5613. 'display
  5614. (if (equal (char-after (match-beginning 2)) ?^)
  5615. (nth (if table-p 3 1) org-script-display)
  5616. (nth (if table-p 2 0) org-script-display)))
  5617. (add-text-properties (match-beginning 2) (match-end 2)
  5618. (list 'invisible t
  5619. 'org-dwidth t 'org-dwidth-n 1))
  5620. (if (and (eq (char-after (match-beginning 3)) ?{)
  5621. (eq (char-before (match-end 3)) ?}))
  5622. (progn
  5623. (add-text-properties
  5624. (match-beginning 3) (1+ (match-beginning 3))
  5625. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5626. (add-text-properties
  5627. (1- (match-end 3)) (match-end 3)
  5628. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5629. t)))))
  5630. ;;;; Visibility cycling, including org-goto and indirect buffer
  5631. ;;; Cycling
  5632. (defvar org-cycle-global-status nil)
  5633. (make-variable-buffer-local 'org-cycle-global-status)
  5634. (put 'org-cycle-global-status 'org-state t)
  5635. (defvar org-cycle-subtree-status nil)
  5636. (make-variable-buffer-local 'org-cycle-subtree-status)
  5637. (put 'org-cycle-subtree-status 'org-state t)
  5638. (defvar org-inlinetask-min-level)
  5639. ;;;###autoload
  5640. (defun org-cycle (&optional arg)
  5641. "TAB-action and visibility cycling for Org-mode.
  5642. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5643. is outline visibility cycling, but it also invokes other actions
  5644. in special contexts.
  5645. - When this function is called with a prefix argument, rotate the entire
  5646. buffer through 3 states (global cycling)
  5647. 1. OVERVIEW: Show only top-level headlines.
  5648. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5649. 3. SHOW ALL: Show everything.
  5650. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5651. determined by the variable `org-startup-folded', and by any VISIBILITY
  5652. properties in the buffer.
  5653. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5654. including any drawers.
  5655. - When inside a table, re-align the table and move to the next field.
  5656. - When point is at the beginning of a headline, rotate the subtree started
  5657. by this line through 3 different states (local cycling)
  5658. 1. FOLDED: Only the main headline is shown.
  5659. 2. CHILDREN: The main headline and the direct children are shown.
  5660. From this state, you can move to one of the children
  5661. and zoom in further.
  5662. 3. SUBTREE: Show the entire subtree, including body text.
  5663. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5664. - When point is at the beginning of an empty headline and the variable
  5665. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5666. of the headline by demoting and promoting it to likely levels. This
  5667. speeds up creation document structure by pressing TAB once or several
  5668. times right after creating a new headline.
  5669. - When there is a numeric prefix, go up to a heading with level ARG, do
  5670. a `show-subtree' and return to the previous cursor position. If ARG
  5671. is negative, go up that many levels.
  5672. - When point is not at the beginning of a headline, execute the global
  5673. binding for TAB, which is re-indenting the line. See the option
  5674. `org-cycle-emulate-tab' for details.
  5675. - Special case: if point is at the beginning of the buffer and there is
  5676. no headline in line 1, this function will act as if called with prefix arg
  5677. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5678. But only if also the variable `org-cycle-global-at-bob' is t."
  5679. (interactive "P")
  5680. (org-load-modules-maybe)
  5681. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5682. (and org-cycle-level-after-item/entry-creation
  5683. (or (org-cycle-level)
  5684. (org-cycle-item-indentation))))
  5685. (let* (message-log-max ; Don't populate the *Messages* buffer
  5686. (limit-level
  5687. (or org-cycle-max-level
  5688. (and (boundp 'org-inlinetask-min-level)
  5689. org-inlinetask-min-level
  5690. (1- org-inlinetask-min-level))))
  5691. (nstars (and limit-level
  5692. (if org-odd-levels-only
  5693. (and limit-level (1- (* limit-level 2)))
  5694. limit-level)))
  5695. (org-outline-regexp
  5696. (if (not (derived-mode-p 'org-mode))
  5697. outline-regexp
  5698. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5699. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5700. (not (looking-at org-outline-regexp))))
  5701. (org-cycle-hook
  5702. (if bob-special
  5703. (delq 'org-optimize-window-after-visibility-change
  5704. (copy-sequence org-cycle-hook))
  5705. org-cycle-hook))
  5706. (pos (point)))
  5707. (if (or bob-special (equal arg '(4)))
  5708. ;; special case: use global cycling
  5709. (setq arg t))
  5710. (cond
  5711. ((equal arg '(16))
  5712. (setq last-command 'dummy)
  5713. (org-set-startup-visibility)
  5714. (message "Startup visibility, plus VISIBILITY properties"))
  5715. ((equal arg '(64))
  5716. (show-all)
  5717. (message "Entire buffer visible, including drawers"))
  5718. ;; Table: enter it or move to the next field.
  5719. ((org-at-table-p 'any)
  5720. (if (org-at-table.el-p)
  5721. (message "Use C-c ' to edit table.el tables")
  5722. (if arg (org-table-edit-field t)
  5723. (org-table-justify-field-maybe)
  5724. (call-interactively 'org-table-next-field))))
  5725. ((run-hook-with-args-until-success
  5726. 'org-tab-after-check-for-table-hook))
  5727. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5728. ((eq arg t) (org-cycle-internal-global))
  5729. ;; Drawers: delegate to `org-flag-drawer'.
  5730. ((and org-drawers org-drawer-regexp
  5731. (save-excursion
  5732. (beginning-of-line 1)
  5733. (looking-at org-drawer-regexp)))
  5734. (org-flag-drawer ; toggle block visibility
  5735. (not (get-char-property (match-end 0) 'invisible))))
  5736. ;; Show-subtree, ARG levels up from here.
  5737. ((integerp arg)
  5738. (save-excursion
  5739. (org-back-to-heading)
  5740. (outline-up-heading (if (< arg 0) (- arg)
  5741. (- (funcall outline-level) arg)))
  5742. (org-show-subtree)))
  5743. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5744. ((and (featurep 'org-inlinetask)
  5745. (org-inlinetask-at-task-p)
  5746. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5747. (org-inlinetask-toggle-visibility))
  5748. ((org-try-cdlatex-tab))
  5749. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5750. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5751. (save-excursion (beginning-of-line 1)
  5752. (looking-at org-outline-regexp)))
  5753. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5754. (org-cycle-internal-local))
  5755. ;; From there: TAB emulation and template completion.
  5756. (buffer-read-only (org-back-to-heading))
  5757. ((run-hook-with-args-until-success
  5758. 'org-tab-after-check-for-cycling-hook))
  5759. ((org-try-structure-completion))
  5760. ((run-hook-with-args-until-success
  5761. 'org-tab-before-tab-emulation-hook))
  5762. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5763. (or (not (bolp))
  5764. (not (looking-at org-outline-regexp))))
  5765. (call-interactively (global-key-binding "\t")))
  5766. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5767. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5768. (or (and (eq org-cycle-emulate-tab 'white)
  5769. (= (match-end 0) (point-at-eol)))
  5770. (and (eq org-cycle-emulate-tab 'whitestart)
  5771. (>= (match-end 0) pos))))
  5772. t
  5773. (eq org-cycle-emulate-tab t))
  5774. (call-interactively (global-key-binding "\t")))
  5775. (t (save-excursion
  5776. (org-back-to-heading)
  5777. (org-cycle)))))))
  5778. (defun org-cycle-internal-global ()
  5779. "Do the global cycling action."
  5780. ;; Hack to avoid display of messages for .org attachments in Gnus
  5781. (let (message-log-max ; Don't populate the *Messages* buffer
  5782. (ga (string-match "\\*fontification" (buffer-name))))
  5783. (cond
  5784. ((and (eq last-command this-command)
  5785. (eq org-cycle-global-status 'overview))
  5786. ;; We just created the overview - now do table of contents
  5787. ;; This can be slow in very large buffers, so indicate action
  5788. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5789. (unless ga (message "CONTENTS..."))
  5790. (org-content)
  5791. (unless ga (message "CONTENTS...done"))
  5792. (setq org-cycle-global-status 'contents)
  5793. (run-hook-with-args 'org-cycle-hook 'contents))
  5794. ((and (eq last-command this-command)
  5795. (eq org-cycle-global-status 'contents))
  5796. ;; We just showed the table of contents - now show everything
  5797. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5798. (show-all)
  5799. (unless ga (message "SHOW ALL"))
  5800. (setq org-cycle-global-status 'all)
  5801. (run-hook-with-args 'org-cycle-hook 'all))
  5802. (t
  5803. ;; Default action: go to overview
  5804. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5805. (org-overview)
  5806. (unless ga (message "OVERVIEW"))
  5807. (setq org-cycle-global-status 'overview)
  5808. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5809. (defun org-cycle-internal-local ()
  5810. "Do the local cycling action."
  5811. (let (message-log-max ; Don't populate the *Messages* buffer
  5812. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5813. ;; First, determine end of headline (EOH), end of subtree or item
  5814. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5815. (save-excursion
  5816. (if (org-at-item-p)
  5817. (progn
  5818. (beginning-of-line)
  5819. (setq struct (org-list-struct))
  5820. (setq eoh (point-at-eol))
  5821. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5822. (setq has-children (org-list-has-child-p (point) struct)))
  5823. (org-back-to-heading)
  5824. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5825. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5826. (setq has-children
  5827. (or (save-excursion
  5828. (let ((level (funcall outline-level)))
  5829. (outline-next-heading)
  5830. (and (org-at-heading-p t)
  5831. (> (funcall outline-level) level))))
  5832. (save-excursion
  5833. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5834. ;; Determine end invisible part of buffer (EOL)
  5835. (beginning-of-line 2)
  5836. ;; XEmacs doesn't have `next-single-char-property-change'
  5837. (if (featurep 'xemacs)
  5838. (while (and (not (eobp)) ;; this is like `next-line'
  5839. (get-char-property (1- (point)) 'invisible))
  5840. (beginning-of-line 2))
  5841. (while (and (not (eobp)) ;; this is like `next-line'
  5842. (get-char-property (1- (point)) 'invisible))
  5843. (goto-char (next-single-char-property-change (point) 'invisible))
  5844. (and (eolp) (beginning-of-line 2))))
  5845. (setq eol (point)))
  5846. ;; Find out what to do next and set `this-command'
  5847. (cond
  5848. ((= eos eoh)
  5849. ;; Nothing is hidden behind this heading
  5850. (unless (org-before-first-heading-p)
  5851. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5852. (message "EMPTY ENTRY")
  5853. (setq org-cycle-subtree-status nil)
  5854. (save-excursion
  5855. (goto-char eos)
  5856. (outline-next-heading)
  5857. (if (outline-invisible-p) (org-flag-heading nil))))
  5858. ((and (or (>= eol eos)
  5859. (not (string-match "\\S-" (buffer-substring eol eos))))
  5860. (or has-children
  5861. (not (setq children-skipped
  5862. org-cycle-skip-children-state-if-no-children))))
  5863. ;; Entire subtree is hidden in one line: children view
  5864. (unless (org-before-first-heading-p)
  5865. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5866. (if (org-at-item-p)
  5867. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5868. (org-show-entry)
  5869. (org-with-limited-levels (show-children))
  5870. ;; FIXME: This slows down the func way too much.
  5871. ;; How keep drawers hidden in subtree anyway?
  5872. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5873. ;; (org-cycle-hide-drawers 'subtree))
  5874. ;; Fold every list in subtree to top-level items.
  5875. (when (eq org-cycle-include-plain-lists 'integrate)
  5876. (save-excursion
  5877. (org-back-to-heading)
  5878. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5879. (beginning-of-line 1)
  5880. (let* ((struct (org-list-struct))
  5881. (prevs (org-list-prevs-alist struct))
  5882. (end (org-list-get-bottom-point struct)))
  5883. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5884. (org-list-get-all-items (point) struct prevs))
  5885. (goto-char end))))))
  5886. (message "CHILDREN")
  5887. (save-excursion
  5888. (goto-char eos)
  5889. (outline-next-heading)
  5890. (if (outline-invisible-p) (org-flag-heading nil)))
  5891. (setq org-cycle-subtree-status 'children)
  5892. (unless (org-before-first-heading-p)
  5893. (run-hook-with-args 'org-cycle-hook 'children)))
  5894. ((or children-skipped
  5895. (and (eq last-command this-command)
  5896. (eq org-cycle-subtree-status 'children)))
  5897. ;; We just showed the children, or no children are there,
  5898. ;; now show everything.
  5899. (unless (org-before-first-heading-p)
  5900. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5901. (outline-flag-region eoh eos nil)
  5902. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5903. (setq org-cycle-subtree-status 'subtree)
  5904. (unless (org-before-first-heading-p)
  5905. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5906. (t
  5907. ;; Default action: hide the subtree.
  5908. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5909. (outline-flag-region eoh eos t)
  5910. (message "FOLDED")
  5911. (setq org-cycle-subtree-status 'folded)
  5912. (unless (org-before-first-heading-p)
  5913. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5914. ;;;###autoload
  5915. (defun org-global-cycle (&optional arg)
  5916. "Cycle the global visibility. For details see `org-cycle'.
  5917. With \\[universal-argument] prefix arg, switch to startup visibility.
  5918. With a numeric prefix, show all headlines up to that level."
  5919. (interactive "P")
  5920. (let ((org-cycle-include-plain-lists
  5921. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5922. (cond
  5923. ((integerp arg)
  5924. (show-all)
  5925. (hide-sublevels arg)
  5926. (setq org-cycle-global-status 'contents))
  5927. ((equal arg '(4))
  5928. (org-set-startup-visibility)
  5929. (message "Startup visibility, plus VISIBILITY properties."))
  5930. (t
  5931. (org-cycle '(4))))))
  5932. (defun org-set-startup-visibility ()
  5933. "Set the visibility required by startup options and properties."
  5934. (cond
  5935. ((eq org-startup-folded t)
  5936. (org-cycle '(4)))
  5937. ((eq org-startup-folded 'content)
  5938. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5939. (org-cycle '(4)) (org-cycle '(4)))))
  5940. (unless (eq org-startup-folded 'showeverything)
  5941. (if org-hide-block-startup (org-hide-block-all))
  5942. (org-set-visibility-according-to-property 'no-cleanup)
  5943. (org-cycle-hide-archived-subtrees 'all)
  5944. (org-cycle-hide-drawers 'all)
  5945. (org-cycle-show-empty-lines t)))
  5946. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5947. "Switch subtree visibilities according to :VISIBILITY: property."
  5948. (interactive)
  5949. (let (org-show-entry-below state)
  5950. (save-excursion
  5951. (goto-char (point-min))
  5952. (while (re-search-forward
  5953. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5954. nil t)
  5955. (setq state (match-string 1))
  5956. (save-excursion
  5957. (org-back-to-heading t)
  5958. (hide-subtree)
  5959. (org-reveal)
  5960. (cond
  5961. ((equal state '("fold" "folded"))
  5962. (hide-subtree))
  5963. ((equal state "children")
  5964. (org-show-hidden-entry)
  5965. (show-children))
  5966. ((equal state "content")
  5967. (save-excursion
  5968. (save-restriction
  5969. (org-narrow-to-subtree)
  5970. (org-content))))
  5971. ((member state '("all" "showall"))
  5972. (show-subtree)))))
  5973. (unless no-cleanup
  5974. (org-cycle-hide-archived-subtrees 'all)
  5975. (org-cycle-hide-drawers 'all)
  5976. (org-cycle-show-empty-lines 'all)))))
  5977. ;; This function uses outline-regexp instead of the more fundamental
  5978. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5979. ;; buffers, where outline-regexp is needed.
  5980. (defun org-overview ()
  5981. "Switch to overview mode, showing only top-level headlines.
  5982. Really, this shows all headlines with level equal or greater than the level
  5983. of the first headline in the buffer. This is important, because if the
  5984. first headline is not level one, then (hide-sublevels 1) gives confusing
  5985. results."
  5986. (interactive)
  5987. (let ((level (save-excursion
  5988. (goto-char (point-min))
  5989. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5990. (progn
  5991. (goto-char (match-beginning 0))
  5992. (funcall outline-level))))))
  5993. (and level (hide-sublevels level))))
  5994. (defun org-content (&optional arg)
  5995. "Show all headlines in the buffer, like a table of contents.
  5996. With numerical argument N, show content up to level N."
  5997. (interactive "P")
  5998. (save-excursion
  5999. ;; Visit all headings and show their offspring
  6000. (and (integerp arg) (org-overview))
  6001. (goto-char (point-max))
  6002. (catch 'exit
  6003. (while (and (progn (condition-case nil
  6004. (outline-previous-visible-heading 1)
  6005. (error (goto-char (point-min))))
  6006. t)
  6007. (looking-at org-outline-regexp))
  6008. (if (integerp arg)
  6009. (show-children (1- arg))
  6010. (show-branches))
  6011. (if (bobp) (throw 'exit nil))))))
  6012. (defun org-optimize-window-after-visibility-change (state)
  6013. "Adjust the window after a change in outline visibility.
  6014. This function is the default value of the hook `org-cycle-hook'."
  6015. (when (get-buffer-window (current-buffer))
  6016. (cond
  6017. ((eq state 'content) nil)
  6018. ((eq state 'all) nil)
  6019. ((eq state 'folded) nil)
  6020. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6021. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6022. (defun org-remove-empty-overlays-at (pos)
  6023. "Remove outline overlays that do not contain non-white stuff."
  6024. (mapc
  6025. (lambda (o)
  6026. (and (eq 'outline (overlay-get o 'invisible))
  6027. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6028. (overlay-end o))))
  6029. (delete-overlay o)))
  6030. (overlays-at pos)))
  6031. (defun org-clean-visibility-after-subtree-move ()
  6032. "Fix visibility issues after moving a subtree."
  6033. ;; First, find a reasonable region to look at:
  6034. ;; Start two siblings above, end three below
  6035. (let* ((beg (save-excursion
  6036. (and (org-get-last-sibling)
  6037. (org-get-last-sibling))
  6038. (point)))
  6039. (end (save-excursion
  6040. (and (org-get-next-sibling)
  6041. (org-get-next-sibling)
  6042. (org-get-next-sibling))
  6043. (if (org-at-heading-p)
  6044. (point-at-eol)
  6045. (point))))
  6046. (level (looking-at "\\*+"))
  6047. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6048. (save-excursion
  6049. (save-restriction
  6050. (narrow-to-region beg end)
  6051. (when re
  6052. ;; Properly fold already folded siblings
  6053. (goto-char (point-min))
  6054. (while (re-search-forward re nil t)
  6055. (if (and (not (outline-invisible-p))
  6056. (save-excursion
  6057. (goto-char (point-at-eol)) (outline-invisible-p)))
  6058. (hide-entry))))
  6059. (org-cycle-show-empty-lines 'overview)
  6060. (org-cycle-hide-drawers 'overview)))))
  6061. (defun org-cycle-show-empty-lines (state)
  6062. "Show empty lines above all visible headlines.
  6063. The region to be covered depends on STATE when called through
  6064. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6065. entire buffer covered. Note that an empty line is only shown if there
  6066. are at least `org-cycle-separator-lines' empty lines before the headline."
  6067. (when (not (= org-cycle-separator-lines 0))
  6068. (save-excursion
  6069. (let* ((n (abs org-cycle-separator-lines))
  6070. (re (cond
  6071. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6072. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6073. (t (let ((ns (number-to-string (- n 2))))
  6074. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6075. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6076. beg end b e)
  6077. (cond
  6078. ((memq state '(overview contents t))
  6079. (setq beg (point-min) end (point-max)))
  6080. ((memq state '(children folded))
  6081. (setq beg (point) end (progn (org-end-of-subtree t t)
  6082. (beginning-of-line 2)
  6083. (point)))))
  6084. (when beg
  6085. (goto-char beg)
  6086. (while (re-search-forward re end t)
  6087. (unless (get-char-property (match-end 1) 'invisible)
  6088. (setq e (match-end 1))
  6089. (if (< org-cycle-separator-lines 0)
  6090. (setq b (save-excursion
  6091. (goto-char (match-beginning 0))
  6092. (org-back-over-empty-lines)
  6093. (if (save-excursion
  6094. (goto-char (max (point-min) (1- (point))))
  6095. (org-at-heading-p))
  6096. (1- (point))
  6097. (point))))
  6098. (setq b (match-beginning 1)))
  6099. (outline-flag-region b e nil)))))))
  6100. ;; Never hide empty lines at the end of the file.
  6101. (save-excursion
  6102. (goto-char (point-max))
  6103. (outline-previous-heading)
  6104. (outline-end-of-heading)
  6105. (if (and (looking-at "[ \t\n]+")
  6106. (= (match-end 0) (point-max)))
  6107. (outline-flag-region (point) (match-end 0) nil))))
  6108. (defun org-show-empty-lines-in-parent ()
  6109. "Move to the parent and re-show empty lines before visible headlines."
  6110. (save-excursion
  6111. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6112. (org-cycle-show-empty-lines context))))
  6113. (defun org-files-list ()
  6114. "Return `org-agenda-files' list, plus all open org-mode files.
  6115. This is useful for operations that need to scan all of a user's
  6116. open and agenda-wise Org files."
  6117. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6118. (dolist (buf (buffer-list))
  6119. (with-current-buffer buf
  6120. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6121. (let ((file (expand-file-name (buffer-file-name))))
  6122. (unless (member file files)
  6123. (push file files))))))
  6124. files))
  6125. (defsubst org-entry-beginning-position ()
  6126. "Return the beginning position of the current entry."
  6127. (save-excursion (outline-back-to-heading t) (point)))
  6128. (defsubst org-entry-end-position ()
  6129. "Return the end position of the current entry."
  6130. (save-excursion (outline-next-heading) (point)))
  6131. (defun org-cycle-hide-drawers (state)
  6132. "Re-hide all drawers after a visibility state change."
  6133. (when (and (derived-mode-p 'org-mode)
  6134. (not (memq state '(overview folded contents))))
  6135. (save-excursion
  6136. (let* ((globalp (memq state '(contents all)))
  6137. (beg (if globalp (point-min) (point)))
  6138. (end (if globalp (point-max)
  6139. (if (eq state 'children)
  6140. (save-excursion (outline-next-heading) (point))
  6141. (org-end-of-subtree t)))))
  6142. (goto-char beg)
  6143. (while (re-search-forward org-drawer-regexp end t)
  6144. (org-flag-drawer t))))))
  6145. (defun org-cycle-hide-inline-tasks (state)
  6146. "Re-hide inline task when switching to 'contents visibility state."
  6147. (when (and (eq state 'contents)
  6148. (boundp 'org-inlinetask-min-level)
  6149. org-inlinetask-min-level)
  6150. (hide-sublevels (1- org-inlinetask-min-level))))
  6151. (defun org-flag-drawer (flag)
  6152. "When FLAG is non-nil, hide the drawer we are within.
  6153. Otherwise make it visible."
  6154. (save-excursion
  6155. (beginning-of-line 1)
  6156. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6157. (let ((b (match-end 0)))
  6158. (if (re-search-forward
  6159. "^[ \t]*:END:"
  6160. (save-excursion (outline-next-heading) (point)) t)
  6161. (outline-flag-region b (point-at-eol) flag)
  6162. (error ":END: line missing at position %s" b))))))
  6163. (defun org-subtree-end-visible-p ()
  6164. "Is the end of the current subtree visible?"
  6165. (pos-visible-in-window-p
  6166. (save-excursion (org-end-of-subtree t) (point))))
  6167. (defun org-first-headline-recenter (&optional N)
  6168. "Move cursor to the first headline and recenter the headline.
  6169. Optional argument N means put the headline into the Nth line of the window."
  6170. (goto-char (point-min))
  6171. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6172. (beginning-of-line)
  6173. (recenter (prefix-numeric-value N))))
  6174. ;;; Saving and restoring visibility
  6175. (defun org-outline-overlay-data (&optional use-markers)
  6176. "Return a list of the locations of all outline overlays.
  6177. These are overlays with the `invisible' property value `outline'.
  6178. The return value is a list of cons cells, with start and stop
  6179. positions for each overlay.
  6180. If USE-MARKERS is set, return the positions as markers."
  6181. (let (beg end)
  6182. (save-excursion
  6183. (save-restriction
  6184. (widen)
  6185. (delq nil
  6186. (mapcar (lambda (o)
  6187. (when (eq (overlay-get o 'invisible) 'outline)
  6188. (setq beg (overlay-start o)
  6189. end (overlay-end o))
  6190. (and beg end (> end beg)
  6191. (if use-markers
  6192. (cons (move-marker (make-marker) beg)
  6193. (move-marker (make-marker) end))
  6194. (cons beg end)))))
  6195. (overlays-in (point-min) (point-max))))))))
  6196. (defun org-set-outline-overlay-data (data)
  6197. "Create visibility overlays for all positions in DATA.
  6198. DATA should have been made by `org-outline-overlay-data'."
  6199. (let (o)
  6200. (save-excursion
  6201. (save-restriction
  6202. (widen)
  6203. (show-all)
  6204. (mapc (lambda (c)
  6205. (outline-flag-region (car c) (cdr c) t))
  6206. data)))))
  6207. ;;; Folding of blocks
  6208. (defvar org-hide-block-overlays nil
  6209. "Overlays hiding blocks.")
  6210. (make-variable-buffer-local 'org-hide-block-overlays)
  6211. (defun org-block-map (function &optional start end)
  6212. "Call FUNCTION at the head of all source blocks in the current buffer.
  6213. Optional arguments START and END can be used to limit the range."
  6214. (let ((start (or start (point-min)))
  6215. (end (or end (point-max))))
  6216. (save-excursion
  6217. (goto-char start)
  6218. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6219. (save-excursion
  6220. (save-match-data
  6221. (goto-char (match-beginning 0))
  6222. (funcall function)))))))
  6223. (defun org-hide-block-toggle-all ()
  6224. "Toggle the visibility of all blocks in the current buffer."
  6225. (org-block-map #'org-hide-block-toggle))
  6226. (defun org-hide-block-all ()
  6227. "Fold all blocks in the current buffer."
  6228. (interactive)
  6229. (org-show-block-all)
  6230. (org-block-map #'org-hide-block-toggle-maybe))
  6231. (defun org-show-block-all ()
  6232. "Unfold all blocks in the current buffer."
  6233. (interactive)
  6234. (mapc 'delete-overlay org-hide-block-overlays)
  6235. (setq org-hide-block-overlays nil))
  6236. (defun org-hide-block-toggle-maybe ()
  6237. "Toggle visibility of block at point."
  6238. (interactive)
  6239. (let ((case-fold-search t))
  6240. (if (save-excursion
  6241. (beginning-of-line 1)
  6242. (looking-at org-block-regexp))
  6243. (progn (org-hide-block-toggle)
  6244. t) ;; to signal that we took action
  6245. nil))) ;; to signal that we did not
  6246. (defun org-hide-block-toggle (&optional force)
  6247. "Toggle the visibility of the current block."
  6248. (interactive)
  6249. (save-excursion
  6250. (beginning-of-line)
  6251. (if (re-search-forward org-block-regexp nil t)
  6252. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6253. (end (match-end 0)) ;; end of entire body
  6254. ov)
  6255. (if (memq t (mapcar (lambda (overlay)
  6256. (eq (overlay-get overlay 'invisible)
  6257. 'org-hide-block))
  6258. (overlays-at start)))
  6259. (if (or (not force) (eq force 'off))
  6260. (mapc (lambda (ov)
  6261. (when (member ov org-hide-block-overlays)
  6262. (setq org-hide-block-overlays
  6263. (delq ov org-hide-block-overlays)))
  6264. (when (eq (overlay-get ov 'invisible)
  6265. 'org-hide-block)
  6266. (delete-overlay ov)))
  6267. (overlays-at start)))
  6268. (setq ov (make-overlay start end))
  6269. (overlay-put ov 'invisible 'org-hide-block)
  6270. ;; make the block accessible to isearch
  6271. (overlay-put
  6272. ov 'isearch-open-invisible
  6273. (lambda (ov)
  6274. (when (member ov org-hide-block-overlays)
  6275. (setq org-hide-block-overlays
  6276. (delq ov org-hide-block-overlays)))
  6277. (when (eq (overlay-get ov 'invisible)
  6278. 'org-hide-block)
  6279. (delete-overlay ov))))
  6280. (push ov org-hide-block-overlays)))
  6281. (error "Not looking at a source block"))))
  6282. ;; org-tab-after-check-for-cycling-hook
  6283. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6284. ;; Remove overlays when changing major mode
  6285. (add-hook 'org-mode-hook
  6286. (lambda () (org-add-hook 'change-major-mode-hook
  6287. 'org-show-block-all 'append 'local)))
  6288. ;;; Org-goto
  6289. (defvar org-goto-window-configuration nil)
  6290. (defvar org-goto-marker nil)
  6291. (defvar org-goto-map)
  6292. (defun org-goto-map ()
  6293. "Set the keymap `org-goto'."
  6294. (setq org-goto-map
  6295. (let ((map (make-sparse-keymap)))
  6296. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6297. mouse-drag-region universal-argument org-occur))
  6298. cmd)
  6299. (while (setq cmd (pop cmds))
  6300. (substitute-key-definition cmd cmd map global-map)))
  6301. (suppress-keymap map)
  6302. (org-defkey map "\C-m" 'org-goto-ret)
  6303. (org-defkey map [(return)] 'org-goto-ret)
  6304. (org-defkey map [(left)] 'org-goto-left)
  6305. (org-defkey map [(right)] 'org-goto-right)
  6306. (org-defkey map [(control ?g)] 'org-goto-quit)
  6307. (org-defkey map "\C-i" 'org-cycle)
  6308. (org-defkey map [(tab)] 'org-cycle)
  6309. (org-defkey map [(down)] 'outline-next-visible-heading)
  6310. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6311. (if org-goto-auto-isearch
  6312. (if (fboundp 'define-key-after)
  6313. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6314. nil)
  6315. (org-defkey map "q" 'org-goto-quit)
  6316. (org-defkey map "n" 'outline-next-visible-heading)
  6317. (org-defkey map "p" 'outline-previous-visible-heading)
  6318. (org-defkey map "f" 'outline-forward-same-level)
  6319. (org-defkey map "b" 'outline-backward-same-level)
  6320. (org-defkey map "u" 'outline-up-heading))
  6321. (org-defkey map "/" 'org-occur)
  6322. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6323. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6324. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6325. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6326. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6327. map)))
  6328. (defconst org-goto-help
  6329. "Browse buffer copy, to find location or copy text.%s
  6330. RET=jump to location C-g=quit and return to previous location
  6331. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6332. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6333. ;; FIXME: Docstring does not mention both interfaces
  6334. (defun org-goto (&optional alternative-interface)
  6335. "Look up a different location in the current file, keeping current visibility.
  6336. When you want look-up or go to a different location in a
  6337. document, the fastest way is often to fold the entire buffer and
  6338. then dive into the tree. This method has the disadvantage, that
  6339. the previous location will be folded, which may not be what you
  6340. want.
  6341. This command works around this by showing a copy of the current
  6342. buffer in an indirect buffer, in overview mode. You can dive
  6343. into the tree in that copy, use org-occur and incremental search
  6344. to find a location. When pressing RET or `Q', the command
  6345. returns to the original buffer in which the visibility is still
  6346. unchanged. After RET it will also jump to the location selected
  6347. in the indirect buffer and expose the headline hierarchy above.
  6348. With a prefix argument, use the alternative interface: e.g. if
  6349. `org-goto-interface' is 'outline use 'outline-path-completion."
  6350. (interactive "P")
  6351. (org-goto-map)
  6352. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6353. (org-refile-use-outline-path t)
  6354. (org-refile-target-verify-function nil)
  6355. (interface
  6356. (if (not alternative-interface)
  6357. org-goto-interface
  6358. (if (eq org-goto-interface 'outline)
  6359. 'outline-path-completion
  6360. 'outline)))
  6361. (org-goto-start-pos (point))
  6362. (selected-point
  6363. (if (eq interface 'outline)
  6364. (car (org-get-location (current-buffer) org-goto-help))
  6365. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6366. (org-refile-check-position pa)
  6367. (nth 3 pa)))))
  6368. (if selected-point
  6369. (progn
  6370. (org-mark-ring-push org-goto-start-pos)
  6371. (goto-char selected-point)
  6372. (if (or (outline-invisible-p) (org-invisible-p2))
  6373. (org-show-context 'org-goto)))
  6374. (message "Quit"))))
  6375. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6376. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6377. (defvar org-goto-local-auto-isearch-map) ; defined below
  6378. (defun org-get-location (buf help)
  6379. "Let the user select a location in the Org-mode buffer BUF.
  6380. This function uses a recursive edit. It returns the selected position
  6381. or nil."
  6382. (org-no-popups
  6383. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6384. (isearch-hide-immediately nil)
  6385. (isearch-search-fun-function
  6386. (lambda () 'org-goto-local-search-headings))
  6387. (org-goto-selected-point org-goto-exit-command))
  6388. (save-excursion
  6389. (save-window-excursion
  6390. (delete-other-windows)
  6391. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6392. (org-pop-to-buffer-same-window
  6393. (condition-case nil
  6394. (make-indirect-buffer (current-buffer) "*org-goto*")
  6395. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6396. (with-output-to-temp-buffer "*Org Help*"
  6397. (princ (format help (if org-goto-auto-isearch
  6398. " Just type for auto-isearch."
  6399. " n/p/f/b/u to navigate, q to quit."))))
  6400. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6401. (setq buffer-read-only nil)
  6402. (let ((org-startup-truncated t)
  6403. (org-startup-folded nil)
  6404. (org-startup-align-all-tables nil))
  6405. (org-mode)
  6406. (org-overview))
  6407. (setq buffer-read-only t)
  6408. (if (and (boundp 'org-goto-start-pos)
  6409. (integer-or-marker-p org-goto-start-pos))
  6410. (let ((org-show-hierarchy-above t)
  6411. (org-show-siblings t)
  6412. (org-show-following-heading t))
  6413. (goto-char org-goto-start-pos)
  6414. (and (outline-invisible-p) (org-show-context)))
  6415. (goto-char (point-min)))
  6416. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6417. (message "Select location and press RET")
  6418. (use-local-map org-goto-map)
  6419. (recursive-edit)))
  6420. (kill-buffer "*org-goto*")
  6421. (cons org-goto-selected-point org-goto-exit-command))))
  6422. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6423. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6424. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6425. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6426. (defun org-goto-local-search-headings (string bound noerror)
  6427. "Search and make sure that any matches are in headlines."
  6428. (catch 'return
  6429. (while (if isearch-forward
  6430. (search-forward string bound noerror)
  6431. (search-backward string bound noerror))
  6432. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6433. (and (member :headline context)
  6434. (not (member :tags context))))
  6435. (throw 'return (point))))))
  6436. (defun org-goto-local-auto-isearch ()
  6437. "Start isearch."
  6438. (interactive)
  6439. (goto-char (point-min))
  6440. (let ((keys (this-command-keys)))
  6441. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6442. (isearch-mode t)
  6443. (isearch-process-search-char (string-to-char keys)))))
  6444. (defun org-goto-ret (&optional arg)
  6445. "Finish `org-goto' by going to the new location."
  6446. (interactive "P")
  6447. (setq org-goto-selected-point (point)
  6448. org-goto-exit-command 'return)
  6449. (throw 'exit nil))
  6450. (defun org-goto-left ()
  6451. "Finish `org-goto' by going to the new location."
  6452. (interactive)
  6453. (if (org-at-heading-p)
  6454. (progn
  6455. (beginning-of-line 1)
  6456. (setq org-goto-selected-point (point)
  6457. org-goto-exit-command 'left)
  6458. (throw 'exit nil))
  6459. (error "Not on a heading")))
  6460. (defun org-goto-right ()
  6461. "Finish `org-goto' by going to the new location."
  6462. (interactive)
  6463. (if (org-at-heading-p)
  6464. (progn
  6465. (setq org-goto-selected-point (point)
  6466. org-goto-exit-command 'right)
  6467. (throw 'exit nil))
  6468. (error "Not on a heading")))
  6469. (defun org-goto-quit ()
  6470. "Finish `org-goto' without cursor motion."
  6471. (interactive)
  6472. (setq org-goto-selected-point nil)
  6473. (setq org-goto-exit-command 'quit)
  6474. (throw 'exit nil))
  6475. ;;; Indirect buffer display of subtrees
  6476. (defvar org-indirect-dedicated-frame nil
  6477. "This is the frame being used for indirect tree display.")
  6478. (defvar org-last-indirect-buffer nil)
  6479. (defun org-tree-to-indirect-buffer (&optional arg)
  6480. "Create indirect buffer and narrow it to current subtree.
  6481. With a numerical prefix ARG, go up to this level and then take that tree.
  6482. If ARG is negative, go up that many levels.
  6483. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6484. indirect buffer previously made with this command, to avoid proliferation of
  6485. indirect buffers. However, when you call the command with a \
  6486. \\[universal-argument] prefix, or
  6487. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6488. is kept so that you can work with several indirect buffers at the same time.
  6489. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6490. \\[universal-argument] prefix also
  6491. requests that a new frame be made for the new buffer, so that the dedicated
  6492. frame is not changed."
  6493. (interactive "P")
  6494. (let ((cbuf (current-buffer))
  6495. (cwin (selected-window))
  6496. (pos (point))
  6497. beg end level heading ibuf)
  6498. (save-excursion
  6499. (org-back-to-heading t)
  6500. (when (numberp arg)
  6501. (setq level (org-outline-level))
  6502. (if (< arg 0) (setq arg (+ level arg)))
  6503. (while (> (setq level (org-outline-level)) arg)
  6504. (org-up-heading-safe)))
  6505. (setq beg (point)
  6506. heading (org-get-heading))
  6507. (org-end-of-subtree t t)
  6508. (if (org-at-heading-p) (backward-char 1))
  6509. (setq end (point)))
  6510. (if (and (buffer-live-p org-last-indirect-buffer)
  6511. (not (eq org-indirect-buffer-display 'new-frame))
  6512. (not arg))
  6513. (kill-buffer org-last-indirect-buffer))
  6514. (setq ibuf (org-get-indirect-buffer cbuf)
  6515. org-last-indirect-buffer ibuf)
  6516. (cond
  6517. ((or (eq org-indirect-buffer-display 'new-frame)
  6518. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6519. (select-frame (make-frame))
  6520. (delete-other-windows)
  6521. (org-pop-to-buffer-same-window ibuf)
  6522. (org-set-frame-title heading))
  6523. ((eq org-indirect-buffer-display 'dedicated-frame)
  6524. (raise-frame
  6525. (select-frame (or (and org-indirect-dedicated-frame
  6526. (frame-live-p org-indirect-dedicated-frame)
  6527. org-indirect-dedicated-frame)
  6528. (setq org-indirect-dedicated-frame (make-frame)))))
  6529. (delete-other-windows)
  6530. (org-pop-to-buffer-same-window ibuf)
  6531. (org-set-frame-title (concat "Indirect: " heading)))
  6532. ((eq org-indirect-buffer-display 'current-window)
  6533. (org-pop-to-buffer-same-window ibuf))
  6534. ((eq org-indirect-buffer-display 'other-window)
  6535. (pop-to-buffer ibuf))
  6536. (t (error "Invalid value")))
  6537. (if (featurep 'xemacs)
  6538. (save-excursion (org-mode) (turn-on-font-lock)))
  6539. (narrow-to-region beg end)
  6540. (show-all)
  6541. (goto-char pos)
  6542. (run-hook-with-args 'org-cycle-hook 'all)
  6543. (and (window-live-p cwin) (select-window cwin))))
  6544. (defun org-get-indirect-buffer (&optional buffer)
  6545. (setq buffer (or buffer (current-buffer)))
  6546. (let ((n 1) (base (buffer-name buffer)) bname)
  6547. (while (buffer-live-p
  6548. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6549. (setq n (1+ n)))
  6550. (condition-case nil
  6551. (make-indirect-buffer buffer bname 'clone)
  6552. (error (make-indirect-buffer buffer bname)))))
  6553. (defun org-set-frame-title (title)
  6554. "Set the title of the current frame to the string TITLE."
  6555. ;; FIXME: how to name a single frame in XEmacs???
  6556. (unless (featurep 'xemacs)
  6557. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6558. ;;;; Structure editing
  6559. ;;; Inserting headlines
  6560. (defun org-previous-line-empty-p ()
  6561. (save-excursion
  6562. (and (not (bobp))
  6563. (or (beginning-of-line 0) t)
  6564. (save-match-data
  6565. (looking-at "[ \t]*$")))))
  6566. (defun org-insert-heading (&optional force-heading invisible-ok)
  6567. "Insert a new heading or item with same depth at point.
  6568. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6569. If point is at the beginning of a headline, insert a sibling before the
  6570. current headline. If point is not at the beginning, split the line,
  6571. create the new headline with the text in the current line after point
  6572. \(but see also the variable `org-M-RET-may-split-line').
  6573. With a double prefix arg, force the heading to be inserted at the
  6574. end of the parent subtree.
  6575. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6576. This is important for non-interactive uses of the command."
  6577. (interactive "P")
  6578. (if (or (= (buffer-size) 0)
  6579. (and (not (save-excursion
  6580. (and (ignore-errors (org-back-to-heading invisible-ok))
  6581. (org-at-heading-p))))
  6582. (or force-heading (not (org-in-item-p)))))
  6583. (progn
  6584. (insert "\n* ")
  6585. (run-hooks 'org-insert-heading-hook))
  6586. (when (or force-heading (not (org-insert-item)))
  6587. (let* ((empty-line-p nil)
  6588. (level nil)
  6589. (on-heading (org-at-heading-p))
  6590. (head (save-excursion
  6591. (condition-case nil
  6592. (progn
  6593. (org-back-to-heading invisible-ok)
  6594. (when (and (not on-heading)
  6595. (featurep 'org-inlinetask)
  6596. (integerp org-inlinetask-min-level)
  6597. (>= (length (match-string 0))
  6598. org-inlinetask-min-level))
  6599. ;; Find a heading level before the inline task
  6600. (while (and (setq level (org-up-heading-safe))
  6601. (>= level org-inlinetask-min-level)))
  6602. (if (org-at-heading-p)
  6603. (org-back-to-heading invisible-ok)
  6604. (error "This should not happen")))
  6605. (setq empty-line-p (org-previous-line-empty-p))
  6606. (match-string 0))
  6607. (error "*"))))
  6608. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6609. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6610. pos hide-previous previous-pos)
  6611. (cond
  6612. ((and (org-at-heading-p) (bolp)
  6613. (or (bobp)
  6614. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6615. ;; insert before the current line
  6616. (open-line (if blank 2 1)))
  6617. ((and (bolp)
  6618. (not org-insert-heading-respect-content)
  6619. (or (bobp)
  6620. (save-excursion
  6621. (backward-char 1) (not (outline-invisible-p)))))
  6622. ;; insert right here
  6623. nil)
  6624. (t
  6625. ;; somewhere in the line
  6626. (save-excursion
  6627. (setq previous-pos (point-at-bol))
  6628. (end-of-line)
  6629. (setq hide-previous (outline-invisible-p)))
  6630. (and org-insert-heading-respect-content (org-show-subtree))
  6631. (let ((split
  6632. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6633. (save-excursion
  6634. (let ((p (point)))
  6635. (goto-char (point-at-bol))
  6636. (and (looking-at org-complex-heading-regexp)
  6637. (match-beginning 4)
  6638. (> p (match-beginning 4)))))))
  6639. tags pos)
  6640. (cond
  6641. (org-insert-heading-respect-content
  6642. (if (not (equal force-heading '(16)))
  6643. (progn
  6644. (org-end-of-subtree nil t)
  6645. (and (looking-at "^\\*") (backward-char 1))
  6646. (while (and (not (bobp))
  6647. (member (char-before) '(?\ ?\t ?\n)))
  6648. (backward-delete-char 1)))
  6649. (org-up-heading-safe)
  6650. (org-end-of-subtree nil t))
  6651. (when (featurep 'org-inlinetask)
  6652. (while (and (not (eobp))
  6653. (looking-at "\\(\\*+\\)[ \t]+")
  6654. (>= (length (match-string 1))
  6655. org-inlinetask-min-level))
  6656. (org-end-of-subtree nil t)))
  6657. (or (bolp) (newline))
  6658. (or (org-previous-line-empty-p)
  6659. (and blank (newline)))
  6660. (open-line 1))
  6661. ((org-at-heading-p)
  6662. (when hide-previous
  6663. (show-children)
  6664. (org-show-entry))
  6665. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6666. (setq tags (and (match-end 2) (match-string 2)))
  6667. (and (match-end 1)
  6668. (delete-region (match-beginning 1) (match-end 1)))
  6669. (setq pos (point-at-bol))
  6670. (or split (end-of-line 1))
  6671. (delete-horizontal-space)
  6672. (if (string-match "\\`\\*+\\'"
  6673. (buffer-substring (point-at-bol) (point)))
  6674. (insert " "))
  6675. (newline (if blank 2 1))
  6676. (when tags
  6677. (save-excursion
  6678. (goto-char pos)
  6679. (end-of-line 1)
  6680. (insert " " tags)
  6681. (org-set-tags nil 'align))))
  6682. (t
  6683. (or split (end-of-line 1))
  6684. (newline (if blank 2 1)))))))
  6685. (insert head) (just-one-space)
  6686. (setq pos (point))
  6687. (end-of-line 1)
  6688. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6689. (when (and org-insert-heading-respect-content hide-previous)
  6690. (save-excursion
  6691. (goto-char previous-pos)
  6692. (hide-subtree)))
  6693. (run-hooks 'org-insert-heading-hook)))))
  6694. (defun org-get-heading (&optional no-tags no-todo)
  6695. "Return the heading of the current entry, without the stars.
  6696. When NO-TAGS is non-nil, don't include tags.
  6697. When NO-TODO is non-nil, don't include TODO keywords."
  6698. (save-excursion
  6699. (org-back-to-heading t)
  6700. (cond
  6701. ((and no-tags no-todo)
  6702. (looking-at org-complex-heading-regexp)
  6703. (match-string 4))
  6704. (no-tags
  6705. (looking-at (concat org-outline-regexp
  6706. "\\(.*?\\)"
  6707. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6708. (match-string 1))
  6709. (no-todo
  6710. (looking-at org-todo-line-regexp)
  6711. (match-string 3))
  6712. (t (looking-at org-heading-regexp)
  6713. (match-string 2)))))
  6714. (defvar orgstruct-mode) ; defined below
  6715. (defun org-heading-components ()
  6716. "Return the components of the current heading.
  6717. This is a list with the following elements:
  6718. - the level as an integer
  6719. - the reduced level, different if `org-odd-levels-only' is set.
  6720. - the TODO keyword, or nil
  6721. - the priority character, like ?A, or nil if no priority is given
  6722. - the headline text itself, or the tags string if no headline text
  6723. - the tags string, or nil."
  6724. (save-excursion
  6725. (org-back-to-heading t)
  6726. (if (let (case-fold-search)
  6727. (looking-at
  6728. (if orgstruct-mode
  6729. org-heading-regexp
  6730. org-complex-heading-regexp)))
  6731. (if orgstruct-mode
  6732. (list (length (match-string 1))
  6733. (org-reduced-level (length (match-string 1)))
  6734. nil
  6735. nil
  6736. (match-string 2)
  6737. nil)
  6738. (list (length (match-string 1))
  6739. (org-reduced-level (length (match-string 1)))
  6740. (org-match-string-no-properties 2)
  6741. (and (match-end 3) (aref (match-string 3) 2))
  6742. (org-match-string-no-properties 4)
  6743. (org-match-string-no-properties 5))))))
  6744. (defun org-get-entry ()
  6745. "Get the entry text, after heading, entire subtree."
  6746. (save-excursion
  6747. (org-back-to-heading t)
  6748. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6749. (defun org-insert-heading-after-current ()
  6750. "Insert a new heading with same level as current, after current subtree."
  6751. (interactive)
  6752. (org-back-to-heading)
  6753. (org-insert-heading)
  6754. (org-move-subtree-down)
  6755. (end-of-line 1))
  6756. (defun org-insert-heading-respect-content (invisible-ok)
  6757. "Insert heading with `org-insert-heading-respect-content' set to t."
  6758. (interactive "P")
  6759. (let ((org-insert-heading-respect-content t))
  6760. (org-insert-heading t invisible-ok)))
  6761. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6762. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6763. (interactive "P")
  6764. (let ((org-insert-heading-respect-content t))
  6765. (org-insert-todo-heading force-state t)))
  6766. (defun org-insert-todo-heading (arg &optional force-heading)
  6767. "Insert a new heading with the same level and TODO state as current heading.
  6768. If the heading has no TODO state, or if the state is DONE, use the first
  6769. state (TODO by default). Also one prefix arg, force first state. With two
  6770. prefix args, force inserting at the end of the parent subtree."
  6771. (interactive "P")
  6772. (when (or force-heading (not (org-insert-item 'checkbox)))
  6773. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6774. force-heading))
  6775. (save-excursion
  6776. (org-back-to-heading)
  6777. (outline-previous-heading)
  6778. (looking-at org-todo-line-regexp))
  6779. (let*
  6780. ((new-mark-x
  6781. (if (or arg
  6782. (not (match-beginning 2))
  6783. (member (match-string 2) org-done-keywords))
  6784. (car org-todo-keywords-1)
  6785. (match-string 2)))
  6786. (new-mark
  6787. (or
  6788. (run-hook-with-args-until-success
  6789. 'org-todo-get-default-hook new-mark-x nil)
  6790. new-mark-x)))
  6791. (beginning-of-line 1)
  6792. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6793. (if org-treat-insert-todo-heading-as-state-change
  6794. (org-todo new-mark)
  6795. (insert new-mark " "))))
  6796. (when org-provide-todo-statistics
  6797. (org-update-parent-todo-statistics))))
  6798. (defun org-insert-subheading (arg)
  6799. "Insert a new subheading and demote it.
  6800. Works for outline headings and for plain lists alike."
  6801. (interactive "P")
  6802. (org-insert-heading arg)
  6803. (cond
  6804. ((org-at-heading-p) (org-do-demote))
  6805. ((org-at-item-p) (org-indent-item))))
  6806. (defun org-insert-todo-subheading (arg)
  6807. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6808. Works for outline headings and for plain lists alike."
  6809. (interactive "P")
  6810. (org-insert-todo-heading arg)
  6811. (cond
  6812. ((org-at-heading-p) (org-do-demote))
  6813. ((org-at-item-p) (org-indent-item))))
  6814. ;;; Promotion and Demotion
  6815. (defvar org-after-demote-entry-hook nil
  6816. "Hook run after an entry has been demoted.
  6817. The cursor will be at the beginning of the entry.
  6818. When a subtree is being demoted, the hook will be called for each node.")
  6819. (defvar org-after-promote-entry-hook nil
  6820. "Hook run after an entry has been promoted.
  6821. The cursor will be at the beginning of the entry.
  6822. When a subtree is being promoted, the hook will be called for each node.")
  6823. (defun org-promote-subtree ()
  6824. "Promote the entire subtree.
  6825. See also `org-promote'."
  6826. (interactive)
  6827. (save-excursion
  6828. (org-with-limited-levels (org-map-tree 'org-promote)))
  6829. (org-fix-position-after-promote))
  6830. (defun org-demote-subtree ()
  6831. "Demote the entire subtree. See `org-demote'.
  6832. See also `org-promote'."
  6833. (interactive)
  6834. (save-excursion
  6835. (org-with-limited-levels (org-map-tree 'org-demote)))
  6836. (org-fix-position-after-promote))
  6837. (defun org-do-promote ()
  6838. "Promote the current heading higher up the tree.
  6839. If the region is active in `transient-mark-mode', promote all headings
  6840. in the region."
  6841. (interactive)
  6842. (save-excursion
  6843. (if (org-region-active-p)
  6844. (org-map-region 'org-promote (region-beginning) (region-end))
  6845. (org-promote)))
  6846. (org-fix-position-after-promote))
  6847. (defun org-do-demote ()
  6848. "Demote the current heading lower down the tree.
  6849. If the region is active in `transient-mark-mode', demote all headings
  6850. in the region."
  6851. (interactive)
  6852. (save-excursion
  6853. (if (org-region-active-p)
  6854. (org-map-region 'org-demote (region-beginning) (region-end))
  6855. (org-demote)))
  6856. (org-fix-position-after-promote))
  6857. (defun org-fix-position-after-promote ()
  6858. "Make sure that after pro/demotion cursor position is right."
  6859. (let ((pos (point)))
  6860. (when (save-excursion
  6861. (beginning-of-line 1)
  6862. (looking-at org-todo-line-regexp)
  6863. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6864. (cond ((eobp) (insert " "))
  6865. ((eolp) (insert " "))
  6866. ((equal (char-after) ?\ ) (forward-char 1))))))
  6867. (defun org-current-level ()
  6868. "Return the level of the current entry, or nil if before the first headline.
  6869. The level is the number of stars at the beginning of the headline."
  6870. (save-excursion
  6871. (org-with-limited-levels
  6872. (if (ignore-errors (org-back-to-heading t))
  6873. (funcall outline-level)))))
  6874. (defun org-get-previous-line-level ()
  6875. "Return the outline depth of the last headline before the current line.
  6876. Returns 0 for the first headline in the buffer, and nil if before the
  6877. first headline."
  6878. (let ((current-level (org-current-level))
  6879. (prev-level (when (> (line-number-at-pos) 1)
  6880. (save-excursion
  6881. (beginning-of-line 0)
  6882. (org-current-level)))))
  6883. (cond ((null current-level) nil) ; Before first headline
  6884. ((null prev-level) 0) ; At first headline
  6885. (prev-level))))
  6886. (defun org-reduced-level (l)
  6887. "Compute the effective level of a heading.
  6888. This takes into account the setting of `org-odd-levels-only'."
  6889. (cond
  6890. ((zerop l) 0)
  6891. (org-odd-levels-only (1+ (floor (/ l 2))))
  6892. (t l)))
  6893. (defun org-level-increment ()
  6894. "Return the number of stars that will be added or removed at a
  6895. time to headlines when structure editing, based on the value of
  6896. `org-odd-levels-only'."
  6897. (if org-odd-levels-only 2 1))
  6898. (defun org-get-valid-level (level &optional change)
  6899. "Rectify a level change under the influence of `org-odd-levels-only'
  6900. LEVEL is a current level, CHANGE is by how much the level should be
  6901. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6902. even level numbers will become the next higher odd number."
  6903. (if org-odd-levels-only
  6904. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6905. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6906. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6907. (max 1 (+ level (or change 0)))))
  6908. (if (boundp 'define-obsolete-function-alias)
  6909. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6910. (define-obsolete-function-alias 'org-get-legal-level
  6911. 'org-get-valid-level)
  6912. (define-obsolete-function-alias 'org-get-legal-level
  6913. 'org-get-valid-level "23.1")))
  6914. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6915. ;; ̀org-with-limited-levels'
  6916. (defun org-promote ()
  6917. "Promote the current heading higher up the tree.
  6918. If the region is active in `transient-mark-mode', promote all headings
  6919. in the region."
  6920. (org-back-to-heading t)
  6921. (let* ((level (save-match-data (funcall outline-level)))
  6922. (after-change-functions (remove 'flyspell-after-change-function
  6923. after-change-functions))
  6924. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6925. (diff (abs (- level (length up-head) -1))))
  6926. (cond ((and (= level 1) org-called-with-limited-levels
  6927. org-allow-promoting-top-level-subtree)
  6928. (replace-match "# " nil t))
  6929. ((= level 1)
  6930. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6931. (t (replace-match up-head nil t)))
  6932. ;; Fixup tag positioning
  6933. (unless (= level 1)
  6934. (and org-auto-align-tags (org-set-tags nil t))
  6935. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6936. (run-hooks 'org-after-promote-entry-hook)))
  6937. (defun org-demote ()
  6938. "Demote the current heading lower down the tree.
  6939. If the region is active in `transient-mark-mode', demote all headings
  6940. in the region."
  6941. (org-back-to-heading t)
  6942. (let* ((level (save-match-data (funcall outline-level)))
  6943. (after-change-functions (remove 'flyspell-after-change-function
  6944. after-change-functions))
  6945. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6946. (diff (abs (- level (length down-head) -1))))
  6947. (replace-match down-head nil t)
  6948. ;; Fixup tag positioning
  6949. (and org-auto-align-tags (org-set-tags nil t))
  6950. (if org-adapt-indentation (org-fixup-indentation diff))
  6951. (run-hooks 'org-after-demote-entry-hook)))
  6952. (defun org-cycle-level ()
  6953. "Cycle the level of an empty headline through possible states.
  6954. This goes first to child, then to parent, level, then up the hierarchy.
  6955. After top level, it switches back to sibling level."
  6956. (interactive)
  6957. (let ((org-adapt-indentation nil))
  6958. (when (org-point-at-end-of-empty-headline)
  6959. (setq this-command 'org-cycle-level) ; Only needed for caching
  6960. (let ((cur-level (org-current-level))
  6961. (prev-level (org-get-previous-line-level)))
  6962. (cond
  6963. ;; If first headline in file, promote to top-level.
  6964. ((= prev-level 0)
  6965. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6966. do (org-do-promote)))
  6967. ;; If same level as prev, demote one.
  6968. ((= prev-level cur-level)
  6969. (org-do-demote))
  6970. ;; If parent is top-level, promote to top level if not already.
  6971. ((= prev-level 1)
  6972. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6973. do (org-do-promote)))
  6974. ;; If top-level, return to prev-level.
  6975. ((= cur-level 1)
  6976. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6977. do (org-do-demote)))
  6978. ;; If less than prev-level, promote one.
  6979. ((< cur-level prev-level)
  6980. (org-do-promote))
  6981. ;; If deeper than prev-level, promote until higher than
  6982. ;; prev-level.
  6983. ((> cur-level prev-level)
  6984. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6985. do (org-do-promote))))
  6986. t))))
  6987. (defun org-map-tree (fun)
  6988. "Call FUN for every heading underneath the current one."
  6989. (org-back-to-heading)
  6990. (let ((level (funcall outline-level)))
  6991. (save-excursion
  6992. (funcall fun)
  6993. (while (and (progn
  6994. (outline-next-heading)
  6995. (> (funcall outline-level) level))
  6996. (not (eobp)))
  6997. (funcall fun)))))
  6998. (defun org-map-region (fun beg end)
  6999. "Call FUN for every heading between BEG and END."
  7000. (let ((org-ignore-region t))
  7001. (save-excursion
  7002. (setq end (copy-marker end))
  7003. (goto-char beg)
  7004. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7005. (< (point) end))
  7006. (funcall fun))
  7007. (while (and (progn
  7008. (outline-next-heading)
  7009. (< (point) end))
  7010. (not (eobp)))
  7011. (funcall fun)))))
  7012. (defvar org-property-end-re) ; silence byte-compiler
  7013. (defun org-fixup-indentation (diff)
  7014. "Change the indentation in the current entry by DIFF.
  7015. However, if any line in the current entry has no indentation, or if it
  7016. would end up with no indentation after the change, nothing at all is done."
  7017. (save-excursion
  7018. (let ((end (save-excursion (outline-next-heading)
  7019. (point-marker)))
  7020. (prohibit (if (> diff 0)
  7021. "^\\S-"
  7022. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7023. col)
  7024. (unless (save-excursion (end-of-line 1)
  7025. (re-search-forward prohibit end t))
  7026. (while (and (< (point) end)
  7027. (re-search-forward "^[ \t]+" end t))
  7028. (goto-char (match-end 0))
  7029. (setq col (current-column))
  7030. (if (< diff 0) (replace-match ""))
  7031. (org-indent-to-column (+ diff col))))
  7032. (move-marker end nil))))
  7033. (defun org-convert-to-odd-levels ()
  7034. "Convert an org-mode file with all levels allowed to one with odd levels.
  7035. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7036. level 5 etc."
  7037. (interactive)
  7038. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7039. (let ((outline-level 'org-outline-level)
  7040. (org-odd-levels-only nil) n)
  7041. (save-excursion
  7042. (goto-char (point-min))
  7043. (while (re-search-forward "^\\*\\*+ " nil t)
  7044. (setq n (- (length (match-string 0)) 2))
  7045. (while (>= (setq n (1- n)) 0)
  7046. (org-demote))
  7047. (end-of-line 1))))))
  7048. (defun org-convert-to-oddeven-levels ()
  7049. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7050. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7051. file contains a section with an even level, conversion would
  7052. destroy the structure of the file. An error is signaled in this
  7053. case."
  7054. (interactive)
  7055. (goto-char (point-min))
  7056. ;; First check if there are no even levels
  7057. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7058. (org-show-context t)
  7059. (error "Not all levels are odd in this file. Conversion not possible"))
  7060. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7061. (let ((outline-regexp org-outline-regexp)
  7062. (outline-level 'org-outline-level)
  7063. (org-odd-levels-only nil) n)
  7064. (save-excursion
  7065. (goto-char (point-min))
  7066. (while (re-search-forward "^\\*\\*+ " nil t)
  7067. (setq n (/ (1- (length (match-string 0))) 2))
  7068. (while (>= (setq n (1- n)) 0)
  7069. (org-promote))
  7070. (end-of-line 1))))))
  7071. (defun org-tr-level (n)
  7072. "Make N odd if required."
  7073. (if org-odd-levels-only (1+ (/ n 2)) n))
  7074. ;;; Vertical tree motion, cutting and pasting of subtrees
  7075. (defun org-move-subtree-up (&optional arg)
  7076. "Move the current subtree up past ARG headlines of the same level."
  7077. (interactive "p")
  7078. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7079. (defun org-move-subtree-down (&optional arg)
  7080. "Move the current subtree down past ARG headlines of the same level."
  7081. (interactive "p")
  7082. (setq arg (prefix-numeric-value arg))
  7083. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7084. 'org-get-last-sibling))
  7085. (ins-point (make-marker))
  7086. (cnt (abs arg))
  7087. (col (current-column))
  7088. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7089. ;; Select the tree
  7090. (org-back-to-heading)
  7091. (setq beg0 (point))
  7092. (save-excursion
  7093. (setq ne-beg (org-back-over-empty-lines))
  7094. (setq beg (point)))
  7095. (save-match-data
  7096. (save-excursion (outline-end-of-heading)
  7097. (setq folded (outline-invisible-p)))
  7098. (outline-end-of-subtree))
  7099. (outline-next-heading)
  7100. (setq ne-end (org-back-over-empty-lines))
  7101. (setq end (point))
  7102. (goto-char beg0)
  7103. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7104. ;; include less whitespace
  7105. (save-excursion
  7106. (goto-char beg)
  7107. (forward-line (- ne-beg ne-end))
  7108. (setq beg (point))))
  7109. ;; Find insertion point, with error handling
  7110. (while (> cnt 0)
  7111. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7112. (progn (goto-char beg0)
  7113. (user-error "Cannot move past superior level or buffer limit")))
  7114. (setq cnt (1- cnt)))
  7115. (if (> arg 0)
  7116. ;; Moving forward - still need to move over subtree
  7117. (progn (org-end-of-subtree t t)
  7118. (save-excursion
  7119. (org-back-over-empty-lines)
  7120. (or (bolp) (newline)))))
  7121. (setq ne-ins (org-back-over-empty-lines))
  7122. (move-marker ins-point (point))
  7123. (setq txt (buffer-substring beg end))
  7124. (org-save-markers-in-region beg end)
  7125. (delete-region beg end)
  7126. (org-remove-empty-overlays-at beg)
  7127. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7128. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7129. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7130. (let ((bbb (point)))
  7131. (insert-before-markers txt)
  7132. (org-reinstall-markers-in-region bbb)
  7133. (move-marker ins-point bbb))
  7134. (or (bolp) (insert "\n"))
  7135. (setq ins-end (point))
  7136. (goto-char ins-point)
  7137. (org-skip-whitespace)
  7138. (when (and (< arg 0)
  7139. (org-first-sibling-p)
  7140. (> ne-ins ne-beg))
  7141. ;; Move whitespace back to beginning
  7142. (save-excursion
  7143. (goto-char ins-end)
  7144. (let ((kill-whole-line t))
  7145. (kill-line (- ne-ins ne-beg)) (point)))
  7146. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7147. (move-marker ins-point nil)
  7148. (if folded
  7149. (hide-subtree)
  7150. (org-show-entry)
  7151. (show-children)
  7152. (org-cycle-hide-drawers 'children))
  7153. (org-clean-visibility-after-subtree-move)
  7154. ;; move back to the initial column we were at
  7155. (move-to-column col)))
  7156. (defvar org-subtree-clip ""
  7157. "Clipboard for cut and paste of subtrees.
  7158. This is actually only a copy of the kill, because we use the normal kill
  7159. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7160. (defvar org-subtree-clip-folded nil
  7161. "Was the last copied subtree folded?
  7162. This is used to fold the tree back after pasting.")
  7163. (defun org-cut-subtree (&optional n)
  7164. "Cut the current subtree into the clipboard.
  7165. With prefix arg N, cut this many sequential subtrees.
  7166. This is a short-hand for marking the subtree and then cutting it."
  7167. (interactive "p")
  7168. (org-copy-subtree n 'cut))
  7169. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7170. "Cut the current subtree into the clipboard.
  7171. With prefix arg N, cut this many sequential subtrees.
  7172. This is a short-hand for marking the subtree and then copying it.
  7173. If CUT is non-nil, actually cut the subtree.
  7174. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7175. of some markers in the region, even if CUT is non-nil. This is
  7176. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7177. (interactive "p")
  7178. (let (beg end folded (beg0 (point)))
  7179. (if (org-called-interactively-p 'any)
  7180. (org-back-to-heading nil) ; take what looks like a subtree
  7181. (org-back-to-heading t)) ; take what is really there
  7182. (setq beg (point))
  7183. (skip-chars-forward " \t\r\n")
  7184. (save-match-data
  7185. (if nosubtrees
  7186. (outline-next-heading)
  7187. (save-excursion (outline-end-of-heading)
  7188. (setq folded (outline-invisible-p)))
  7189. (condition-case nil
  7190. (org-forward-heading-same-level (1- n) t)
  7191. (error nil))
  7192. (org-end-of-subtree t t)))
  7193. (setq end (point))
  7194. (goto-char beg0)
  7195. (when (> end beg)
  7196. (setq org-subtree-clip-folded folded)
  7197. (when (or cut force-store-markers)
  7198. (org-save-markers-in-region beg end))
  7199. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7200. (setq org-subtree-clip (current-kill 0))
  7201. (message "%s: Subtree(s) with %d characters"
  7202. (if cut "Cut" "Copied")
  7203. (length org-subtree-clip)))))
  7204. (defun org-paste-subtree (&optional level tree for-yank)
  7205. "Paste the clipboard as a subtree, with modification of headline level.
  7206. The entire subtree is promoted or demoted in order to match a new headline
  7207. level.
  7208. If the cursor is at the beginning of a headline, the same level as
  7209. that headline is used to paste the tree.
  7210. If not, the new level is derived from the *visible* headings
  7211. before and after the insertion point, and taken to be the inferior headline
  7212. level of the two. So if the previous visible heading is level 3 and the
  7213. next is level 4 (or vice versa), level 4 will be used for insertion.
  7214. This makes sure that the subtree remains an independent subtree and does
  7215. not swallow low level entries.
  7216. You can also force a different level, either by using a numeric prefix
  7217. argument, or by inserting the heading marker by hand. For example, if the
  7218. cursor is after \"*****\", then the tree will be shifted to level 5.
  7219. If optional TREE is given, use this text instead of the kill ring.
  7220. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7221. move back over whitespace before inserting, and move point to the end of
  7222. the inserted text when done."
  7223. (interactive "P")
  7224. (setq tree (or tree (and kill-ring (current-kill 0))))
  7225. (unless (org-kill-is-subtree-p tree)
  7226. (error "%s"
  7227. (substitute-command-keys
  7228. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7229. (org-with-limited-levels
  7230. (let* ((visp (not (outline-invisible-p)))
  7231. (txt tree)
  7232. (^re_ "\\(\\*+\\)[ \t]*")
  7233. (old-level (if (string-match org-outline-regexp-bol txt)
  7234. (- (match-end 0) (match-beginning 0) 1)
  7235. -1))
  7236. (force-level (cond (level (prefix-numeric-value level))
  7237. ((and (looking-at "[ \t]*$")
  7238. (string-match
  7239. "^\\*+$" (buffer-substring
  7240. (point-at-bol) (point))))
  7241. (- (match-end 1) (match-beginning 1)))
  7242. ((and (bolp)
  7243. (looking-at org-outline-regexp))
  7244. (- (match-end 0) (point) 1))))
  7245. (previous-level (save-excursion
  7246. (condition-case nil
  7247. (progn
  7248. (outline-previous-visible-heading 1)
  7249. (if (looking-at ^re_)
  7250. (- (match-end 0) (match-beginning 0) 1)
  7251. 1))
  7252. (error 1))))
  7253. (next-level (save-excursion
  7254. (condition-case nil
  7255. (progn
  7256. (or (looking-at org-outline-regexp)
  7257. (outline-next-visible-heading 1))
  7258. (if (looking-at ^re_)
  7259. (- (match-end 0) (match-beginning 0) 1)
  7260. 1))
  7261. (error 1))))
  7262. (new-level (or force-level (max previous-level next-level)))
  7263. (shift (if (or (= old-level -1)
  7264. (= new-level -1)
  7265. (= old-level new-level))
  7266. 0
  7267. (- new-level old-level)))
  7268. (delta (if (> shift 0) -1 1))
  7269. (func (if (> shift 0) 'org-demote 'org-promote))
  7270. (org-odd-levels-only nil)
  7271. beg end newend)
  7272. ;; Remove the forced level indicator
  7273. (if force-level
  7274. (delete-region (point-at-bol) (point)))
  7275. ;; Paste
  7276. (beginning-of-line (if (bolp) 1 2))
  7277. (setq beg (point))
  7278. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7279. (insert-before-markers txt)
  7280. (unless (string-match "\n\\'" txt) (insert "\n"))
  7281. (setq newend (point))
  7282. (org-reinstall-markers-in-region beg)
  7283. (setq end (point))
  7284. (goto-char beg)
  7285. (skip-chars-forward " \t\n\r")
  7286. (setq beg (point))
  7287. (if (and (outline-invisible-p) visp)
  7288. (save-excursion (outline-show-heading)))
  7289. ;; Shift if necessary
  7290. (unless (= shift 0)
  7291. (save-restriction
  7292. (narrow-to-region beg end)
  7293. (while (not (= shift 0))
  7294. (org-map-region func (point-min) (point-max))
  7295. (setq shift (+ delta shift)))
  7296. (goto-char (point-min))
  7297. (setq newend (point-max))))
  7298. (when (or (org-called-interactively-p 'interactive) for-yank)
  7299. (message "Clipboard pasted as level %d subtree" new-level))
  7300. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7301. kill-ring
  7302. (eq org-subtree-clip (current-kill 0))
  7303. org-subtree-clip-folded)
  7304. ;; The tree was folded before it was killed/copied
  7305. (hide-subtree))
  7306. (and for-yank (goto-char newend)))))
  7307. (defun org-kill-is-subtree-p (&optional txt)
  7308. "Check if the current kill is an outline subtree, or a set of trees.
  7309. Returns nil if kill does not start with a headline, or if the first
  7310. headline level is not the largest headline level in the tree.
  7311. So this will actually accept several entries of equal levels as well,
  7312. which is OK for `org-paste-subtree'.
  7313. If optional TXT is given, check this string instead of the current kill."
  7314. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7315. (re (org-get-limited-outline-regexp))
  7316. (^re (concat "^" re))
  7317. (start-level (and kill
  7318. (string-match
  7319. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7320. kill)
  7321. (- (match-end 2) (match-beginning 2) 1)))
  7322. (start (1+ (or (match-beginning 2) -1))))
  7323. (if (not start-level)
  7324. (progn
  7325. nil) ;; does not even start with a heading
  7326. (catch 'exit
  7327. (while (setq start (string-match ^re kill (1+ start)))
  7328. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7329. (throw 'exit nil)))
  7330. t))))
  7331. (defvar org-markers-to-move nil
  7332. "Markers that should be moved with a cut-and-paste operation.
  7333. Those markers are stored together with their positions relative to
  7334. the start of the region.")
  7335. (defun org-save-markers-in-region (beg end)
  7336. "Check markers in region.
  7337. If these markers are between BEG and END, record their position relative
  7338. to BEG, so that after moving the block of text, we can put the markers back
  7339. into place.
  7340. This function gets called just before an entry or tree gets cut from the
  7341. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7342. called immediately, to move the markers with the entries."
  7343. (setq org-markers-to-move nil)
  7344. (when (featurep 'org-clock)
  7345. (org-clock-save-markers-for-cut-and-paste beg end))
  7346. (when (featurep 'org-agenda)
  7347. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7348. (defun org-check-and-save-marker (marker beg end)
  7349. "Check if MARKER is between BEG and END.
  7350. If yes, remember the marker and the distance to BEG."
  7351. (when (and (marker-buffer marker)
  7352. (equal (marker-buffer marker) (current-buffer)))
  7353. (if (and (>= marker beg) (< marker end))
  7354. (push (cons marker (- marker beg)) org-markers-to-move))))
  7355. (defun org-reinstall-markers-in-region (beg)
  7356. "Move all remembered markers to their position relative to BEG."
  7357. (mapc (lambda (x)
  7358. (move-marker (car x) (+ beg (cdr x))))
  7359. org-markers-to-move)
  7360. (setq org-markers-to-move nil))
  7361. (defun org-narrow-to-subtree ()
  7362. "Narrow buffer to the current subtree."
  7363. (interactive)
  7364. (save-excursion
  7365. (save-match-data
  7366. (org-with-limited-levels
  7367. (narrow-to-region
  7368. (progn (org-back-to-heading t) (point))
  7369. (progn (org-end-of-subtree t t)
  7370. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7371. (point)))))))
  7372. (defun org-narrow-to-block ()
  7373. "Narrow buffer to the current block."
  7374. (interactive)
  7375. (let* ((case-fold-search t)
  7376. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7377. "^[ \t]*#\\+end_.*")))
  7378. (if blockp
  7379. (narrow-to-region (car blockp) (cdr blockp))
  7380. (error "Not in a block"))))
  7381. (eval-when-compile
  7382. (defvar org-property-drawer-re))
  7383. (defvar org-property-start-re) ;; defined below
  7384. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7385. "Clone the task (subtree) at point N times.
  7386. The clones will be inserted as siblings.
  7387. In interactive use, the user will be prompted for the number of
  7388. clones to be produced, and for a time SHIFT, which may be a
  7389. repeater as used in time stamps, for example `+3d'.
  7390. When a valid repeater is given and the entry contains any time
  7391. stamps, the clones will become a sequence in time, with time
  7392. stamps in the subtree shifted for each clone produced. If SHIFT
  7393. is nil or the empty string, time stamps will be left alone. The
  7394. ID property of the original subtree is removed.
  7395. If the original subtree did contain time stamps with a repeater,
  7396. the following will happen:
  7397. - the repeater will be removed in each clone
  7398. - an additional clone will be produced, with the current, unshifted
  7399. date(s) in the entry.
  7400. - the original entry will be placed *after* all the clones, with
  7401. repeater intact.
  7402. - the start days in the repeater in the original entry will be shifted
  7403. to past the last clone.
  7404. In this way you can spell out a number of instances of a repeating task,
  7405. and still retain the repeater to cover future instances of the task."
  7406. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7407. (let (beg end template task idprop
  7408. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7409. (drawer-re org-drawer-regexp))
  7410. (if (not (and (integerp n) (> n 0)))
  7411. (error "Invalid number of replications %s" n))
  7412. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7413. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7414. shift)))
  7415. (error "Invalid shift specification %s" shift))
  7416. (when doshift
  7417. (setq shift-n (string-to-number (match-string 1 shift))
  7418. shift-what (cdr (assoc (match-string 2 shift)
  7419. '(("d" . day) ("w" . week)
  7420. ("m" . month) ("y" . year))))))
  7421. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7422. (setq nmin 1 nmax n)
  7423. (org-back-to-heading t)
  7424. (setq beg (point))
  7425. (setq idprop (org-entry-get nil "ID"))
  7426. (org-end-of-subtree t t)
  7427. (or (bolp) (insert "\n"))
  7428. (setq end (point))
  7429. (setq template (buffer-substring beg end))
  7430. (when (and doshift
  7431. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7432. (delete-region beg end)
  7433. (setq end beg)
  7434. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7435. (goto-char end)
  7436. (loop for n from nmin to nmax do
  7437. ;; prepare clone
  7438. (with-temp-buffer
  7439. (insert template)
  7440. (org-mode)
  7441. (goto-char (point-min))
  7442. (org-show-subtree)
  7443. (and idprop (if org-clone-delete-id
  7444. (org-entry-delete nil "ID")
  7445. (org-id-get-create t)))
  7446. (unless (= n 0)
  7447. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7448. (kill-whole-line))
  7449. (goto-char (point-min))
  7450. (while (re-search-forward drawer-re nil t)
  7451. (mapc (lambda (d)
  7452. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7453. (goto-char (point-min))
  7454. (when doshift
  7455. (while (re-search-forward org-ts-regexp-both nil t)
  7456. (org-timestamp-change (* n shift-n) shift-what))
  7457. (unless (= n n-no-remove)
  7458. (goto-char (point-min))
  7459. (while (re-search-forward org-ts-regexp nil t)
  7460. (save-excursion
  7461. (goto-char (match-beginning 0))
  7462. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7463. (delete-region (match-beginning 1) (match-end 1)))))))
  7464. (setq task (buffer-string)))
  7465. (insert task))
  7466. (goto-char beg)))
  7467. ;;; Outline Sorting
  7468. (defun org-sort (with-case)
  7469. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7470. Optional argument WITH-CASE means sort case-sensitively."
  7471. (interactive "P")
  7472. (cond
  7473. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7474. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7475. (t
  7476. (org-call-with-arg 'org-sort-entries with-case))))
  7477. (defun org-sort-remove-invisible (s)
  7478. "Remove invisible links from string S."
  7479. (remove-text-properties 0 (length s) org-rm-props s)
  7480. (while (string-match org-bracket-link-regexp s)
  7481. (setq s (replace-match (if (match-end 2)
  7482. (match-string 3 s)
  7483. (match-string 1 s)) t t s)))
  7484. (let ((st (format " %s " s)))
  7485. (while (string-match org-emph-re st)
  7486. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7487. (setq s (substring st 1 -1)))
  7488. s)
  7489. (defvar org-priority-regexp) ; defined later in the file
  7490. (defvar org-after-sorting-entries-or-items-hook nil
  7491. "Hook that is run after a bunch of entries or items have been sorted.
  7492. When children are sorted, the cursor is in the parent line when this
  7493. hook gets called. When a region or a plain list is sorted, the cursor
  7494. will be in the first entry of the sorted region/list.")
  7495. (defun org-sort-entries
  7496. (&optional with-case sorting-type getkey-func compare-func property)
  7497. "Sort entries on a certain level of an outline tree.
  7498. If there is an active region, the entries in the region are sorted.
  7499. Else, if the cursor is before the first entry, sort the top-level items.
  7500. Else, the children of the entry at point are sorted.
  7501. Sorting can be alphabetically, numerically, by date/time as given by
  7502. a time stamp, by a property, by priority order, or by a custom function.
  7503. The command prompts for the sorting type unless it has been given to the
  7504. function through the SORTING-TYPE argument, which needs to be a character,
  7505. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7506. precise meaning of each character:
  7507. n Numerically, by converting the beginning of the entry/item to a number.
  7508. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7509. o By order of TODO keywords.
  7510. t By date/time, either the first active time stamp in the entry, or, if
  7511. none exist, by the first inactive one.
  7512. s By the scheduled date/time.
  7513. d By deadline date/time.
  7514. c By creation time, which is assumed to be the first inactive time stamp
  7515. at the beginning of a line.
  7516. p By priority according to the cookie.
  7517. r By the value of a property.
  7518. Capital letters will reverse the sort order.
  7519. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7520. called with point at the beginning of the record. It must return either
  7521. a string or a number that should serve as the sorting key for that record.
  7522. Comparing entries ignores case by default. However, with an optional argument
  7523. WITH-CASE, the sorting considers case as well.
  7524. Sorting is done against the visible part of the headlines, it ignores hidden
  7525. links."
  7526. (interactive "P")
  7527. (let ((case-func (if with-case 'identity 'downcase))
  7528. (cmstr
  7529. ;; The clock marker is lost when using `sort-subr', let's
  7530. ;; store the clocking string.
  7531. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7532. (save-excursion
  7533. (goto-char org-clock-marker)
  7534. (looking-back "^.*") (match-string-no-properties 0))))
  7535. start beg end stars re re2
  7536. txt what tmp)
  7537. ;; Find beginning and end of region to sort
  7538. (cond
  7539. ((org-region-active-p)
  7540. ;; we will sort the region
  7541. (setq end (region-end)
  7542. what "region")
  7543. (goto-char (region-beginning))
  7544. (if (not (org-at-heading-p)) (outline-next-heading))
  7545. (setq start (point)))
  7546. ((or (org-at-heading-p)
  7547. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7548. ;; we will sort the children of the current headline
  7549. (org-back-to-heading)
  7550. (setq start (point)
  7551. end (progn (org-end-of-subtree t t)
  7552. (or (bolp) (insert "\n"))
  7553. (org-back-over-empty-lines)
  7554. (point))
  7555. what "children")
  7556. (goto-char start)
  7557. (show-subtree)
  7558. (outline-next-heading))
  7559. (t
  7560. ;; we will sort the top-level entries in this file
  7561. (goto-char (point-min))
  7562. (or (org-at-heading-p) (outline-next-heading))
  7563. (setq start (point))
  7564. (goto-char (point-max))
  7565. (beginning-of-line 1)
  7566. (when (looking-at ".*?\\S-")
  7567. ;; File ends in a non-white line
  7568. (end-of-line 1)
  7569. (insert "\n"))
  7570. (setq end (point-max))
  7571. (setq what "top-level")
  7572. (goto-char start)
  7573. (show-all)))
  7574. (setq beg (point))
  7575. (if (>= beg end) (error "Nothing to sort"))
  7576. (looking-at "\\(\\*+\\)")
  7577. (setq stars (match-string 1)
  7578. re (concat "^" (regexp-quote stars) " +")
  7579. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7580. txt (buffer-substring beg end))
  7581. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7582. (if (and (not (equal stars "*")) (string-match re2 txt))
  7583. (error "Region to sort contains a level above the first entry"))
  7584. (unless sorting-type
  7585. (message
  7586. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7587. [t]ime [s]cheduled [d]eadline [c]reated
  7588. A/N/P/R/O/F/T/S/D/C means reversed:"
  7589. what)
  7590. (setq sorting-type (read-char-exclusive))
  7591. (unless getkey-func
  7592. (and (= (downcase sorting-type) ?f)
  7593. (setq getkey-func
  7594. (org-icompleting-read "Sort using function: "
  7595. obarray 'fboundp t nil nil))
  7596. (setq getkey-func (intern getkey-func))))
  7597. (and (= (downcase sorting-type) ?r)
  7598. (not property)
  7599. (setq property
  7600. (org-icompleting-read "Property: "
  7601. (mapcar 'list (org-buffer-property-keys t))
  7602. nil t))))
  7603. (message "Sorting entries...")
  7604. (save-restriction
  7605. (narrow-to-region start end)
  7606. (let ((dcst (downcase sorting-type))
  7607. (case-fold-search nil)
  7608. (now (current-time)))
  7609. (sort-subr
  7610. (/= dcst sorting-type)
  7611. ;; This function moves to the beginning character of the "record" to
  7612. ;; be sorted.
  7613. (lambda nil
  7614. (if (re-search-forward re nil t)
  7615. (goto-char (match-beginning 0))
  7616. (goto-char (point-max))))
  7617. ;; This function moves to the last character of the "record" being
  7618. ;; sorted.
  7619. (lambda nil
  7620. (save-match-data
  7621. (condition-case nil
  7622. (outline-forward-same-level 1)
  7623. (error
  7624. (goto-char (point-max))))))
  7625. ;; This function returns the value that gets sorted against.
  7626. (lambda nil
  7627. (cond
  7628. ((= dcst ?n)
  7629. (if (looking-at org-complex-heading-regexp)
  7630. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7631. nil))
  7632. ((= dcst ?a)
  7633. (if (looking-at org-complex-heading-regexp)
  7634. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7635. nil))
  7636. ((= dcst ?t)
  7637. (let ((end (save-excursion (outline-next-heading) (point))))
  7638. (if (or (re-search-forward org-ts-regexp end t)
  7639. (re-search-forward org-ts-regexp-both end t))
  7640. (org-time-string-to-seconds (match-string 0))
  7641. (org-float-time now))))
  7642. ((= dcst ?c)
  7643. (let ((end (save-excursion (outline-next-heading) (point))))
  7644. (if (re-search-forward
  7645. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7646. end t)
  7647. (org-time-string-to-seconds (match-string 0))
  7648. (org-float-time now))))
  7649. ((= dcst ?s)
  7650. (let ((end (save-excursion (outline-next-heading) (point))))
  7651. (if (re-search-forward org-scheduled-time-regexp end t)
  7652. (org-time-string-to-seconds (match-string 1))
  7653. (org-float-time now))))
  7654. ((= dcst ?d)
  7655. (let ((end (save-excursion (outline-next-heading) (point))))
  7656. (if (re-search-forward org-deadline-time-regexp end t)
  7657. (org-time-string-to-seconds (match-string 1))
  7658. (org-float-time now))))
  7659. ((= dcst ?p)
  7660. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7661. (string-to-char (match-string 2))
  7662. org-default-priority))
  7663. ((= dcst ?r)
  7664. (or (org-entry-get nil property) ""))
  7665. ((= dcst ?o)
  7666. (if (looking-at org-complex-heading-regexp)
  7667. (- 9999 (length (member (match-string 2)
  7668. org-todo-keywords-1)))))
  7669. ((= dcst ?f)
  7670. (if getkey-func
  7671. (progn
  7672. (setq tmp (funcall getkey-func))
  7673. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7674. tmp)
  7675. (error "Invalid key function `%s'" getkey-func)))
  7676. (t (error "Invalid sorting type `%c'" sorting-type))))
  7677. nil
  7678. (cond
  7679. ((= dcst ?a) 'string<)
  7680. ((= dcst ?f) compare-func)
  7681. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7682. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7683. ;; Reset the clock marker if needed
  7684. (when cmstr
  7685. (save-excursion
  7686. (goto-char start)
  7687. (search-forward cmstr nil t)
  7688. (move-marker org-clock-marker (point))))
  7689. (message "Sorting entries...done")))
  7690. (defun org-do-sort (table what &optional with-case sorting-type)
  7691. "Sort TABLE of WHAT according to SORTING-TYPE.
  7692. The user will be prompted for the SORTING-TYPE if the call to this
  7693. function does not specify it. WHAT is only for the prompt, to indicate
  7694. what is being sorted. The sorting key will be extracted from
  7695. the car of the elements of the table.
  7696. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7697. (unless sorting-type
  7698. (message
  7699. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7700. what)
  7701. (setq sorting-type (read-char-exclusive)))
  7702. (let ((dcst (downcase sorting-type))
  7703. extractfun comparefun)
  7704. ;; Define the appropriate functions
  7705. (cond
  7706. ((= dcst ?n)
  7707. (setq extractfun 'string-to-number
  7708. comparefun (if (= dcst sorting-type) '< '>)))
  7709. ((= dcst ?a)
  7710. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7711. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7712. comparefun (if (= dcst sorting-type)
  7713. 'string<
  7714. (lambda (a b) (and (not (string< a b))
  7715. (not (string= a b)))))))
  7716. ((= dcst ?t)
  7717. (setq extractfun
  7718. (lambda (x)
  7719. (if (or (string-match org-ts-regexp x)
  7720. (string-match org-ts-regexp-both x))
  7721. (org-float-time
  7722. (org-time-string-to-time (match-string 0 x)))
  7723. 0))
  7724. comparefun (if (= dcst sorting-type) '< '>)))
  7725. (t (error "Invalid sorting type `%c'" sorting-type)))
  7726. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7727. table)
  7728. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7729. ;;; The orgstruct minor mode
  7730. ;; Define a minor mode which can be used in other modes in order to
  7731. ;; integrate the org-mode structure editing commands.
  7732. ;; This is really a hack, because the org-mode structure commands use
  7733. ;; keys which normally belong to the major mode. Here is how it
  7734. ;; works: The minor mode defines all the keys necessary to operate the
  7735. ;; structure commands, but wraps the commands into a function which
  7736. ;; tests if the cursor is currently at a headline or a plain list
  7737. ;; item. If that is the case, the structure command is used,
  7738. ;; temporarily setting many Org-mode variables like regular
  7739. ;; expressions for filling etc. However, when any of those keys is
  7740. ;; used at a different location, function uses `key-binding' to look
  7741. ;; up if the key has an associated command in another currently active
  7742. ;; keymap (minor modes, major mode, global), and executes that
  7743. ;; command. There might be problems if any of the keys is otherwise
  7744. ;; used as a prefix key.
  7745. (defcustom orgstruct-heading-prefix-regexp ""
  7746. "Regexp that matches the custom prefix of Org headlines in
  7747. orgstruct(++)-mode."
  7748. :group 'org
  7749. :type 'string)
  7750. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7751. (defcustom orgstruct-setup-hook nil
  7752. "Hook run after orgstruct-mode-map is filled."
  7753. :group 'org
  7754. :type 'hook)
  7755. (defvar orgstruct-initialized nil)
  7756. (defvar org-local-vars nil
  7757. "List of local variables, for use by `orgstruct-mode'.")
  7758. ;;;###autoload
  7759. (define-minor-mode orgstruct-mode
  7760. "Toggle the minor mode `orgstruct-mode'.
  7761. This mode is for using Org-mode structure commands in other
  7762. modes. The following keys behave as if Org-mode were active, if
  7763. the cursor is on a headline, or on a plain list item (both as
  7764. defined by Org-mode)."
  7765. nil " OrgStruct" (make-sparse-keymap)
  7766. (when orgstruct-mode
  7767. (org-load-modules-maybe)
  7768. (unless orgstruct-initialized
  7769. (orgstruct-setup)
  7770. (setq orgstruct-initialized t))))
  7771. ;;;###autoload
  7772. (defun turn-on-orgstruct ()
  7773. "Unconditionally turn on `orgstruct-mode'."
  7774. (orgstruct-mode 1))
  7775. (defvar org-fb-vars nil)
  7776. (make-variable-buffer-local 'org-fb-vars)
  7777. (defun orgstruct++-mode (&optional arg)
  7778. "Toggle `orgstruct-mode', the enhanced version of it.
  7779. In addition to setting orgstruct-mode, this also exports all
  7780. indentation and autofilling variables from org-mode into the
  7781. buffer. It will also recognize item context in multiline items."
  7782. (interactive "P")
  7783. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7784. (if (< arg 1)
  7785. (progn (orgstruct-mode -1)
  7786. (mapc (lambda(v)
  7787. (org-set-local (car v)
  7788. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7789. org-fb-vars))
  7790. (orgstruct-mode 1)
  7791. (setq org-fb-vars nil)
  7792. (let (var val)
  7793. (mapc
  7794. (lambda (x)
  7795. (when (string-match
  7796. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7797. (symbol-name (car x)))
  7798. (setq var (car x) val (nth 1 x))
  7799. (push (list var `(quote ,(eval var))) org-fb-vars)
  7800. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7801. org-local-vars)
  7802. (org-set-local 'orgstruct-is-++ t))))
  7803. (defvar orgstruct-is-++ nil
  7804. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7805. (make-variable-buffer-local 'orgstruct-is-++)
  7806. ;;;###autoload
  7807. (defun turn-on-orgstruct++ ()
  7808. "Unconditionally turn on `orgstruct++-mode'."
  7809. (orgstruct++-mode 1))
  7810. (defun orgstruct-error ()
  7811. "Error when there is no default binding for a structure key."
  7812. (interactive)
  7813. (error "This key has no function outside structure elements"))
  7814. (defun orgstruct-setup ()
  7815. "Setup orgstruct keymap."
  7816. (dolist (f
  7817. '("org-meta"
  7818. "org-shiftmeta"
  7819. org-shifttab
  7820. org-backward-element
  7821. org-backward-heading-same-level
  7822. org-ctrl-c-ret
  7823. org-cycle
  7824. org-forward-heading-same-level
  7825. org-insert-heading
  7826. org-insert-heading-respect-content
  7827. org-kill-note-or-show-branches
  7828. org-mark-subtree
  7829. org-narrow-to-subtree
  7830. org-promote-subtree
  7831. org-reveal
  7832. org-show-subtree
  7833. org-sort
  7834. org-up-element
  7835. outline-demote
  7836. outline-next-visible-heading
  7837. outline-previous-visible-heading
  7838. outline-promote
  7839. outline-up-heading
  7840. show-children))
  7841. (dolist (f (if (stringp f)
  7842. (let ((flist))
  7843. (dolist (postfix
  7844. '("-return" "tab" "left" "right" "up" "down")
  7845. flist)
  7846. (let ((f (intern (concat f postfix))))
  7847. (when (fboundp f)
  7848. (push f flist)))))
  7849. (list f)))
  7850. (dolist (binding (nconc (where-is-internal f org-mode-map)
  7851. (where-is-internal f outline-mode-map)))
  7852. ;; TODO use local-function-key-map
  7853. (dolist (rep '(("<tab>" . "TAB")
  7854. ("<return>" . "RET")
  7855. ("<escape>" . "ESC")
  7856. ("<delete>" . "DEL")))
  7857. (setq binding (read-kbd-macro (replace-regexp-in-string
  7858. (regexp-quote (car rep))
  7859. (cdr rep)
  7860. (key-description binding)))))
  7861. (let ((key (lookup-key orgstruct-mode-map binding)))
  7862. (when (or (not key) (numberp key))
  7863. (condition-case nil
  7864. (org-defkey orgstruct-mode-map
  7865. binding
  7866. (orgstruct-make-binding f binding))
  7867. (error nil)))))))
  7868. (run-hooks 'orgstruct-setup-hook))
  7869. (defun orgstruct-make-binding (fun key)
  7870. "Create a function for binding in the structure minor mode.
  7871. FUN is the command to call inside a table. KEY is the key that
  7872. should be checked in for a command to execute outside of tables."
  7873. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  7874. (let ((nname name)
  7875. (i 0))
  7876. (while (fboundp (intern nname))
  7877. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  7878. (setq name (intern nname)))
  7879. (eval
  7880. `(defun ,name (arg)
  7881. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  7882. "Outside of structure, run the binding of `"
  7883. (key-description key) "'.")
  7884. (interactive "p")
  7885. (unless
  7886. (let* ((org-heading-regexp
  7887. (concat "^"
  7888. orgstruct-heading-prefix-regexp
  7889. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  7890. (org-outline-regexp
  7891. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  7892. (org-outline-regexp-bol
  7893. (concat "^" org-outline-regexp))
  7894. (outline-regexp org-outline-regexp)
  7895. (outline-heading-end-regexp "\n")
  7896. (outline-level 'outline-level)
  7897. (outline-heading-alist))
  7898. (when (org-context-p 'headline 'item
  7899. ,(when (memq fun '(org-insert-heading))
  7900. '(when orgstruct-is-++
  7901. 'item-body)))
  7902. (org-run-like-in-org-mode ',fun)
  7903. t))
  7904. (let* ((orgstruct-mode)
  7905. (binding (key-binding ,key)))
  7906. (if (keymapp binding)
  7907. (set-temporary-overlay-map binding)
  7908. (call-interactively
  7909. (or binding 'orgstruct-error)))))))
  7910. name))
  7911. (defun org-contextualize-keys (alist contexts)
  7912. "Return valid elements in ALIST depending on CONTEXTS.
  7913. `org-agenda-custom-commands' or `org-capture-templates' are the
  7914. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7915. or `org-capture-templates-contexts' are the associated contexts
  7916. definitions."
  7917. (let ((contexts
  7918. ;; normalize contexts
  7919. (mapcar
  7920. (lambda(c) (cond ((listp (cadr c))
  7921. (list (car c) (car c) (cadr c)))
  7922. ((string= "" (cadr c))
  7923. (list (car c) (car c) (caddr c)))
  7924. (t c))) contexts))
  7925. (a alist) c r s)
  7926. ;; loop over all commands or templates
  7927. (while (setq c (pop a))
  7928. (let (vrules repl)
  7929. (cond
  7930. ((not (assoc (car c) contexts))
  7931. (push c r))
  7932. ((and (assoc (car c) contexts)
  7933. (setq vrules (org-contextualize-validate-key
  7934. (car c) contexts)))
  7935. (mapc (lambda (vr)
  7936. (when (not (equal (car vr) (cadr vr)))
  7937. (setq repl vr))) vrules)
  7938. (if (not repl) (push c r)
  7939. (push (cadr repl) s)
  7940. (push
  7941. (cons (car c)
  7942. (cdr (or (assoc (cadr repl) alist)
  7943. (error "Undefined key `%s' as contextual replacement for `%s'"
  7944. (cadr repl) (car c)))))
  7945. r))))))
  7946. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7947. (delq
  7948. nil
  7949. (delete-dups
  7950. (mapcar (lambda (x)
  7951. (let ((tpl (car x)))
  7952. (when (not (delq
  7953. nil
  7954. (mapcar (lambda(y)
  7955. (equal y tpl)) s))) x)))
  7956. (reverse r))))))
  7957. (defun org-contextualize-validate-key (key contexts)
  7958. "Check CONTEXTS for agenda or capture KEY."
  7959. (let (r rr res)
  7960. (while (setq r (pop contexts))
  7961. (mapc
  7962. (lambda (rr)
  7963. (when
  7964. (and (equal key (car r))
  7965. (if (functionp rr) (funcall rr)
  7966. (or (and (eq (car rr) 'in-file)
  7967. (buffer-file-name)
  7968. (string-match (cdr rr) (buffer-file-name)))
  7969. (and (eq (car rr) 'in-mode)
  7970. (string-match (cdr rr) (symbol-name major-mode)))
  7971. (and (eq (car rr) 'in-buffer)
  7972. (string-match (cdr rr) (buffer-name)))
  7973. (when (and (eq (car rr) 'not-in-file)
  7974. (buffer-file-name))
  7975. (not (string-match (cdr rr) (buffer-file-name))))
  7976. (when (eq (car rr) 'not-in-mode)
  7977. (not (string-match (cdr rr) (symbol-name major-mode))))
  7978. (when (eq (car rr) 'not-in-buffer)
  7979. (not (string-match (cdr rr) (buffer-name)))))))
  7980. (push r res)))
  7981. (car (last r))))
  7982. (delete-dups (delq nil res))))
  7983. (defun org-context-p (&rest contexts)
  7984. "Check if local context is any of CONTEXTS.
  7985. Possible values in the list of contexts are `table', `headline', and `item'."
  7986. (let ((pos (point)))
  7987. (goto-char (point-at-bol))
  7988. (prog1 (or (and (memq 'table contexts)
  7989. (looking-at "[ \t]*|"))
  7990. (and (memq 'headline contexts)
  7991. (looking-at org-outline-regexp))
  7992. (and (memq 'item contexts)
  7993. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7994. (and (memq 'item-body contexts)
  7995. (org-in-item-p)))
  7996. (goto-char pos))))
  7997. (defun org-get-local-variables ()
  7998. "Return a list of all local variables in an Org mode buffer."
  7999. (let (varlist)
  8000. (with-current-buffer (get-buffer-create "*Org tmp*")
  8001. (erase-buffer)
  8002. (org-mode)
  8003. (setq varlist (buffer-local-variables)))
  8004. (kill-buffer "*Org tmp*")
  8005. (delq nil
  8006. (mapcar
  8007. (lambda (x)
  8008. (setq x
  8009. (if (symbolp x)
  8010. (list x)
  8011. (list (car x) (cdr x))))
  8012. (if (and (not (get (car x) 'org-state))
  8013. (string-match
  8014. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8015. (symbol-name (car x))))
  8016. x nil))
  8017. varlist))))
  8018. (defun org-clone-local-variables (from-buffer &optional regexp)
  8019. "Clone local variables from FROM-BUFFER.
  8020. Optional argument REGEXP selects variables to clone."
  8021. (mapc
  8022. (lambda (pair)
  8023. (and (symbolp (car pair))
  8024. (or (null regexp)
  8025. (string-match regexp (symbol-name (car pair))))
  8026. (set (make-local-variable (car pair))
  8027. (cdr pair))))
  8028. (buffer-local-variables from-buffer)))
  8029. ;;;###autoload
  8030. (defun org-run-like-in-org-mode (cmd)
  8031. "Run a command, pretending that the current buffer is in Org-mode.
  8032. This will temporarily bind local variables that are typically bound in
  8033. Org-mode to the values they have in Org-mode, and then interactively
  8034. call CMD."
  8035. (org-load-modules-maybe)
  8036. (unless org-local-vars
  8037. (setq org-local-vars (org-get-local-variables)))
  8038. (let (binds)
  8039. (dolist (var org-local-vars)
  8040. (when (or (not (boundp (car var)))
  8041. (eq (symbol-value (car var))
  8042. (default-value (car var))))
  8043. (push (list (car var) `(quote ,(cadr var))) binds)))
  8044. (eval `(let ,binds
  8045. (call-interactively (quote ,cmd))))))
  8046. ;;;; Archiving
  8047. (defun org-get-category (&optional pos force-refresh)
  8048. "Get the category applying to position POS."
  8049. (save-match-data
  8050. (if force-refresh (org-refresh-category-properties))
  8051. (let ((pos (or pos (point))))
  8052. (or (get-text-property pos 'org-category)
  8053. (progn (org-refresh-category-properties)
  8054. (get-text-property pos 'org-category))))))
  8055. (defun org-refresh-category-properties ()
  8056. "Refresh category text properties in the buffer."
  8057. (let ((case-fold-search t)
  8058. (inhibit-read-only t)
  8059. (def-cat (cond
  8060. ((null org-category)
  8061. (if buffer-file-name
  8062. (file-name-sans-extension
  8063. (file-name-nondirectory buffer-file-name))
  8064. "???"))
  8065. ((symbolp org-category) (symbol-name org-category))
  8066. (t org-category)))
  8067. beg end cat pos optionp)
  8068. (with-silent-modifications
  8069. (save-excursion
  8070. (save-restriction
  8071. (widen)
  8072. (goto-char (point-min))
  8073. (put-text-property (point) (point-max) 'org-category def-cat)
  8074. (while (re-search-forward
  8075. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8076. (setq pos (match-end 0)
  8077. optionp (equal (char-after (match-beginning 0)) ?#)
  8078. cat (org-trim (match-string 2)))
  8079. (if optionp
  8080. (setq beg (point-at-bol) end (point-max))
  8081. (org-back-to-heading t)
  8082. (setq beg (point) end (org-end-of-subtree t t)))
  8083. (put-text-property beg end 'org-category cat)
  8084. (put-text-property beg end 'org-category-position beg)
  8085. (goto-char pos)))))))
  8086. (defun org-refresh-properties (dprop tprop)
  8087. "Refresh buffer text properties.
  8088. DPROP is the drawer property and TPROP is the corresponding text
  8089. property to set."
  8090. (let ((case-fold-search t)
  8091. (inhibit-read-only t) p)
  8092. (with-silent-modifications
  8093. (save-excursion
  8094. (save-restriction
  8095. (widen)
  8096. (goto-char (point-min))
  8097. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8098. (setq p (org-match-string-no-properties 1))
  8099. (save-excursion
  8100. (org-back-to-heading t)
  8101. (put-text-property
  8102. (point-at-bol) (point-at-eol) tprop p))))))))
  8103. ;;;; Link Stuff
  8104. ;;; Link abbreviations
  8105. (defun org-link-expand-abbrev (link)
  8106. "Apply replacements as defined in `org-link-abbrev-alist'."
  8107. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8108. (let* ((key (match-string 1 link))
  8109. (as (or (assoc key org-link-abbrev-alist-local)
  8110. (assoc key org-link-abbrev-alist)))
  8111. (tag (and (match-end 2) (match-string 3 link)))
  8112. rpl)
  8113. (if (not as)
  8114. link
  8115. (setq rpl (cdr as))
  8116. (cond
  8117. ((symbolp rpl) (funcall rpl tag))
  8118. ((string-match "%(\\([^)]+\\))" rpl)
  8119. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  8120. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8121. ((string-match "%h" rpl)
  8122. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8123. (t (concat rpl tag)))))
  8124. link))
  8125. ;;; Storing and inserting links
  8126. (defvar org-insert-link-history nil
  8127. "Minibuffer history for links inserted with `org-insert-link'.")
  8128. (defvar org-stored-links nil
  8129. "Contains the links stored with `org-store-link'.")
  8130. (defvar org-store-link-plist nil
  8131. "Plist with info about the most recently link created with `org-store-link'.")
  8132. (defvar org-link-protocols nil
  8133. "Link protocols added to Org-mode using `org-add-link-type'.")
  8134. (defvar org-store-link-functions nil
  8135. "List of functions that are called to create and store a link.
  8136. Each function will be called in turn until one returns a non-nil
  8137. value. Each function should check if it is responsible for creating
  8138. this link (for example by looking at the major mode).
  8139. If not, it must exit and return nil.
  8140. If yes, it should return a non-nil value after a calling
  8141. `org-store-link-props' with a list of properties and values.
  8142. Special properties are:
  8143. :type The link prefix, like \"http\". This must be given.
  8144. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8145. This is obligatory as well.
  8146. :description Optional default description for the second pair
  8147. of brackets in an Org-mode link. The user can still change
  8148. this when inserting this link into an Org-mode buffer.
  8149. In addition to these, any additional properties can be specified
  8150. and then used in capture templates.")
  8151. (defun org-add-link-type (type &optional follow export)
  8152. "Add TYPE to the list of `org-link-types'.
  8153. Re-compute all regular expressions depending on `org-link-types'
  8154. FOLLOW and EXPORT are two functions.
  8155. FOLLOW should take the link path as the single argument and do whatever
  8156. is necessary to follow the link, for example find a file or display
  8157. a mail message.
  8158. EXPORT should format the link path for export to one of the export formats.
  8159. It should be a function accepting three arguments:
  8160. path the path of the link, the text after the prefix (like \"http:\")
  8161. desc the description of the link, if any, or a description added by
  8162. org-export-normalize-links if there is none
  8163. format the export format, a symbol like `html' or `latex' or `ascii'..
  8164. The function may use the FORMAT information to return different values
  8165. depending on the format. The return value will be put literally into
  8166. the exported file. If the return value is nil, this means Org should
  8167. do what it normally does with links which do not have EXPORT defined.
  8168. Org-mode has a built-in default for exporting links. If you are happy with
  8169. this default, there is no need to define an export function for the link
  8170. type. For a simple example of an export function, see `org-bbdb.el'."
  8171. (add-to-list 'org-link-types type t)
  8172. (org-make-link-regexps)
  8173. (if (assoc type org-link-protocols)
  8174. (setcdr (assoc type org-link-protocols) (list follow export))
  8175. (push (list type follow export) org-link-protocols)))
  8176. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8177. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8178. ;;;###autoload
  8179. (defun org-store-link (arg)
  8180. "\\<org-mode-map>Store an org-link to the current location.
  8181. This link is added to `org-stored-links' and can later be inserted
  8182. into an org-buffer with \\[org-insert-link].
  8183. For some link types, a prefix arg is interpreted.
  8184. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8185. For file links, arg negates `org-context-in-file-links'.
  8186. A double prefix arg force skipping storing functions that are not
  8187. part of Org's core."
  8188. (interactive "P")
  8189. (org-load-modules-maybe)
  8190. (setq org-store-link-plist nil) ; reset
  8191. (org-with-limited-levels
  8192. (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
  8193. (cond
  8194. ((and (not (equal arg '(16)))
  8195. (setq sfuns
  8196. (delq
  8197. nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
  8198. org-store-link-functions))
  8199. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8200. (or (and (cdr sfuns)
  8201. (funcall (intern
  8202. (completing-read "Which function for creating the link? "
  8203. sfunsn t (car sfunsn)))))
  8204. (funcall (caar sfuns)))
  8205. (setq link (plist-get org-store-link-plist :link)
  8206. desc (or (plist-get org-store-link-plist :description) link))))
  8207. ((org-src-edit-buffer-p)
  8208. (let (label gc)
  8209. (while (or (not label)
  8210. (save-excursion
  8211. (save-restriction
  8212. (widen)
  8213. (goto-char (point-min))
  8214. (re-search-forward
  8215. (regexp-quote (format org-coderef-label-format label))
  8216. nil t))))
  8217. (when label (message "Label exists already") (sit-for 2))
  8218. (setq label (read-string "Code line label: " label)))
  8219. (end-of-line 1)
  8220. (setq link (format org-coderef-label-format label))
  8221. (setq gc (- 79 (length link)))
  8222. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8223. (insert link)
  8224. (setq link (concat "(" label ")") desc nil)))
  8225. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8226. ;; We are in the agenda, link to referenced location
  8227. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8228. (get-text-property (point) 'org-marker))))
  8229. (when m
  8230. (org-with-point-at m
  8231. (setq agenda-link
  8232. (if (org-called-interactively-p 'any)
  8233. (call-interactively 'org-store-link)
  8234. (org-store-link nil)))))))
  8235. ((eq major-mode 'calendar-mode)
  8236. (let ((cd (calendar-cursor-to-date)))
  8237. (setq link
  8238. (format-time-string
  8239. (car org-time-stamp-formats)
  8240. (apply 'encode-time
  8241. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8242. nil nil nil))))
  8243. (org-store-link-props :type "calendar" :date cd)))
  8244. ((eq major-mode 'help-mode)
  8245. (setq link (concat "help:" (save-excursion
  8246. (goto-char (point-min))
  8247. (looking-at "^[^ ]+")
  8248. (match-string 0))))
  8249. (org-store-link-props :type "help"))
  8250. ((eq major-mode 'w3-mode)
  8251. (setq cpltxt (if (and (buffer-name)
  8252. (not (string-match "Untitled" (buffer-name))))
  8253. (buffer-name)
  8254. (url-view-url t))
  8255. link (url-view-url t))
  8256. (org-store-link-props :type "w3" :url (url-view-url t)))
  8257. ((setq search (run-hook-with-args-until-success
  8258. 'org-create-file-search-functions))
  8259. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8260. "::" search))
  8261. (setq cpltxt (or description link)))
  8262. ((eq major-mode 'image-mode)
  8263. (setq cpltxt (concat "file:"
  8264. (abbreviate-file-name buffer-file-name))
  8265. link cpltxt)
  8266. (org-store-link-props :type "image" :file buffer-file-name))
  8267. ((eq major-mode 'dired-mode)
  8268. ;; link to the file in the current line
  8269. (let ((file (dired-get-filename nil t)))
  8270. (setq file (if file
  8271. (abbreviate-file-name
  8272. (expand-file-name (dired-get-filename nil t)))
  8273. ;; otherwise, no file so use current directory.
  8274. default-directory))
  8275. (setq cpltxt (concat "file:" file)
  8276. link cpltxt)))
  8277. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8278. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8279. (cond
  8280. ((org-in-regexp "<<\\(.*?\\)>>")
  8281. (setq cpltxt
  8282. (concat "file:"
  8283. (abbreviate-file-name
  8284. (buffer-file-name (buffer-base-buffer)))
  8285. "::" (match-string 1))
  8286. link cpltxt))
  8287. ((and (featurep 'org-id)
  8288. (or (eq org-id-link-to-org-use-id t)
  8289. (and (org-called-interactively-p 'any)
  8290. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8291. (and (eq org-id-link-to-org-use-id
  8292. 'create-if-interactive-and-no-custom-id)
  8293. (not custom-id))))
  8294. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8295. ;; We can make a link using the ID.
  8296. (setq link (condition-case nil
  8297. (prog1 (org-id-store-link)
  8298. (setq desc (plist-get org-store-link-plist :description)))
  8299. (error
  8300. ;; probably before first headline, link to file only
  8301. (concat "file:"
  8302. (abbreviate-file-name
  8303. (buffer-file-name (buffer-base-buffer))))))))
  8304. (t
  8305. ;; Just link to current headline
  8306. (setq cpltxt (concat "file:"
  8307. (abbreviate-file-name
  8308. (buffer-file-name (buffer-base-buffer)))))
  8309. ;; Add a context search string
  8310. (when (org-xor org-context-in-file-links arg)
  8311. (let* ((ee (org-element-at-point))
  8312. (et (org-element-type ee))
  8313. (ev (plist-get (cadr ee) :value)))
  8314. (setq txt (cond
  8315. ((org-at-heading-p) nil)
  8316. ((eq et 'keyword) ev)
  8317. ((org-region-active-p)
  8318. (buffer-substring (region-beginning) (region-end)))))
  8319. (when (or (null txt) (string-match "\\S-" txt))
  8320. (setq cpltxt
  8321. (concat cpltxt "::"
  8322. (condition-case nil
  8323. (org-make-org-heading-search-string txt)
  8324. (error "")))
  8325. desc (or (and (eq et 'keyword) ev)
  8326. (nth 4 (ignore-errors (org-heading-components)))
  8327. "NONE")))))
  8328. (if (string-match "::\\'" cpltxt)
  8329. (setq cpltxt (substring cpltxt 0 -2)))
  8330. (setq link cpltxt))))
  8331. ((buffer-file-name (buffer-base-buffer))
  8332. ;; Just link to this file here.
  8333. (setq cpltxt (concat "file:"
  8334. (abbreviate-file-name
  8335. (buffer-file-name (buffer-base-buffer)))))
  8336. ;; Add a context string
  8337. (when (org-xor org-context-in-file-links arg)
  8338. (setq txt (if (org-region-active-p)
  8339. (buffer-substring (region-beginning) (region-end))
  8340. (buffer-substring (point-at-bol) (point-at-eol))))
  8341. ;; Only use search option if there is some text.
  8342. (when (string-match "\\S-" txt)
  8343. (setq cpltxt
  8344. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8345. desc "NONE")))
  8346. (setq link cpltxt))
  8347. ((org-called-interactively-p 'interactive)
  8348. (user-error "No method for storing a link from this buffer"))
  8349. (t (setq link nil)))
  8350. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8351. (setq link (or link cpltxt)
  8352. desc (or desc cpltxt))
  8353. (cond ((equal desc "NONE") (setq desc nil))
  8354. ((string-match org-bracket-link-regexp desc)
  8355. (setq desc (replace-regexp-in-string
  8356. org-bracket-link-regexp
  8357. (concat "\\3" (if (equal (length (match-string 0 desc))
  8358. (length desc)) "*" "")) desc))))
  8359. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8360. (progn
  8361. (setq org-stored-links
  8362. (cons (list link desc) org-stored-links))
  8363. (message "Stored: %s" (or desc link))
  8364. (when custom-id
  8365. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8366. "::#" custom-id))
  8367. (setq org-stored-links
  8368. (cons (list link desc) org-stored-links))))
  8369. (or agenda-link (and link (org-make-link-string link desc)))))))
  8370. (defun org-store-link-props (&rest plist)
  8371. "Store link properties, extract names and addresses."
  8372. (let (x adr)
  8373. (when (setq x (plist-get plist :from))
  8374. (setq adr (mail-extract-address-components x))
  8375. (setq plist (plist-put plist :fromname (car adr)))
  8376. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8377. (when (setq x (plist-get plist :to))
  8378. (setq adr (mail-extract-address-components x))
  8379. (setq plist (plist-put plist :toname (car adr)))
  8380. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8381. (let ((from (plist-get plist :from))
  8382. (to (plist-get plist :to)))
  8383. (when (and from to org-from-is-user-regexp)
  8384. (setq plist
  8385. (plist-put plist :fromto
  8386. (if (string-match org-from-is-user-regexp from)
  8387. (concat "to %t")
  8388. (concat "from %f"))))))
  8389. (setq org-store-link-plist plist))
  8390. (defun org-add-link-props (&rest plist)
  8391. "Add these properties to the link property list."
  8392. (let (key value)
  8393. (while plist
  8394. (setq key (pop plist) value (pop plist))
  8395. (setq org-store-link-plist
  8396. (plist-put org-store-link-plist key value)))))
  8397. (defun org-email-link-description (&optional fmt)
  8398. "Return the description part of an email link.
  8399. This takes information from `org-store-link-plist' and formats it
  8400. according to FMT (default from `org-email-link-description-format')."
  8401. (setq fmt (or fmt org-email-link-description-format))
  8402. (let* ((p org-store-link-plist)
  8403. (to (plist-get p :toaddress))
  8404. (from (plist-get p :fromaddress))
  8405. (table
  8406. (list
  8407. (cons "%c" (plist-get p :fromto))
  8408. (cons "%F" (plist-get p :from))
  8409. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8410. (cons "%T" (plist-get p :to))
  8411. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8412. (cons "%s" (plist-get p :subject))
  8413. (cons "%d" (plist-get p :date))
  8414. (cons "%m" (plist-get p :message-id)))))
  8415. (when (string-match "%c" fmt)
  8416. ;; Check if the user wrote this message
  8417. (if (and org-from-is-user-regexp from to
  8418. (save-match-data (string-match org-from-is-user-regexp from)))
  8419. (setq fmt (replace-match "to %t" t t fmt))
  8420. (setq fmt (replace-match "from %f" t t fmt))))
  8421. (org-replace-escapes fmt table)))
  8422. (defun org-make-org-heading-search-string (&optional string)
  8423. "Make search string for the current headline or STRING."
  8424. (let ((s (or string
  8425. (and (derived-mode-p 'org-mode)
  8426. (save-excursion
  8427. (org-back-to-heading t)
  8428. (org-element-property :raw-value (org-element-at-point))))))
  8429. (lines org-context-in-file-links))
  8430. (or string (setq s (concat "*" s))) ; Add * for headlines
  8431. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8432. (when (and string (integerp lines) (> lines 0))
  8433. (let ((slines (org-split-string s "\n")))
  8434. (when (< lines (length slines))
  8435. (setq s (mapconcat
  8436. 'identity
  8437. (reverse (nthcdr (- (length slines) lines)
  8438. (reverse slines))) "\n")))))
  8439. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8440. (defun org-make-link-string (link &optional description)
  8441. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8442. (unless (string-match "\\S-" link)
  8443. (error "Empty link"))
  8444. (when (and description
  8445. (stringp description)
  8446. (not (string-match "\\S-" description)))
  8447. (setq description nil))
  8448. (when (stringp description)
  8449. ;; Remove brackets from the description, they are fatal.
  8450. (while (string-match "\\[" description)
  8451. (setq description (replace-match "{" t t description)))
  8452. (while (string-match "\\]" description)
  8453. (setq description (replace-match "}" t t description))))
  8454. (when (equal link description)
  8455. ;; No description needed, it is identical
  8456. (setq description nil))
  8457. (when (and (not description)
  8458. (not (string-match (org-image-file-name-regexp) link))
  8459. (not (equal link (org-link-escape link))))
  8460. (setq description (org-extract-attributes link)))
  8461. (setq link
  8462. (cond ((string-match (org-image-file-name-regexp) link) link)
  8463. ((string-match org-link-types-re link)
  8464. (concat (match-string 1 link)
  8465. (org-link-escape (substring link (match-end 1)))))
  8466. (t (org-link-escape link))))
  8467. (concat "[[" link "]"
  8468. (if description (concat "[" description "]") "")
  8469. "]"))
  8470. (defconst org-link-escape-chars
  8471. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8472. "List of characters that should be escaped in link.
  8473. This is the list that is used for internal purposes.")
  8474. (defconst org-link-escape-chars-browser
  8475. '(?\ )
  8476. "List of escapes for characters that are problematic in links.
  8477. This is the list that is used before handing over to the browser.")
  8478. (defun org-link-escape (text &optional table merge)
  8479. "Return percent escaped representation of TEXT.
  8480. TEXT is a string with the text to escape.
  8481. Optional argument TABLE is a list with characters that should be
  8482. escaped. When nil, `org-link-escape-chars' is used.
  8483. If optional argument MERGE is set, merge TABLE into
  8484. `org-link-escape-chars'."
  8485. (cond
  8486. ((and table merge)
  8487. (mapc (lambda (defchr)
  8488. (unless (member defchr table)
  8489. (setq table (cons defchr table)))) org-link-escape-chars))
  8490. ((null table)
  8491. (setq table org-link-escape-chars)))
  8492. (mapconcat
  8493. (lambda (char)
  8494. (if (or (member char table)
  8495. (and (or (< char 32) (= char 37) (> char 126))
  8496. org-url-hexify-p))
  8497. (mapconcat (lambda (sequence-element)
  8498. (format "%%%.2X" sequence-element))
  8499. (or (encode-coding-char char 'utf-8)
  8500. (error "Unable to percent escape character: %s"
  8501. (char-to-string char))) "")
  8502. (char-to-string char))) text ""))
  8503. (defun org-link-unescape (str)
  8504. "Unhex hexified Unicode strings as returned from the JavaScript function
  8505. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8506. (unless (and (null str) (string= "" str))
  8507. (let ((pos 0) (case-fold-search t) unhexed)
  8508. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8509. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8510. (setq str (replace-match unhexed t t str))
  8511. (setq pos (+ pos (length unhexed))))))
  8512. str)
  8513. (defun org-link-unescape-compound (hex)
  8514. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8515. Note: this function also decodes single byte encodings like
  8516. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8517. (save-match-data
  8518. (let* ((bytes (cdr (split-string hex "%")))
  8519. (ret "")
  8520. (eat 0)
  8521. (sum 0))
  8522. (while bytes
  8523. (let* ((val (string-to-number (pop bytes) 16))
  8524. (shift-xor
  8525. (if (= 0 eat)
  8526. (cond
  8527. ((>= val 252) (cons 6 252))
  8528. ((>= val 248) (cons 5 248))
  8529. ((>= val 240) (cons 4 240))
  8530. ((>= val 224) (cons 3 224))
  8531. ((>= val 192) (cons 2 192))
  8532. (t (cons 0 0)))
  8533. (cons 6 128))))
  8534. (if (>= val 192) (setq eat (car shift-xor)))
  8535. (setq val (logxor val (cdr shift-xor)))
  8536. (setq sum (+ (lsh sum (car shift-xor)) val))
  8537. (if (> eat 0) (setq eat (- eat 1)))
  8538. (cond
  8539. ((= 0 eat) ;multi byte
  8540. (setq ret (concat ret (org-char-to-string sum)))
  8541. (setq sum 0))
  8542. ((not bytes) ; single byte(s)
  8543. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8544. )) ;; end (while bytes
  8545. ret )))
  8546. (defun org-link-unescape-single-byte-sequence (hex)
  8547. "Unhexify hex-encoded single byte character sequences."
  8548. (mapconcat (lambda (byte)
  8549. (char-to-string (string-to-number byte 16)))
  8550. (cdr (split-string hex "%")) ""))
  8551. (defun org-xor (a b)
  8552. "Exclusive or."
  8553. (if a (not b) b))
  8554. (defun org-fixup-message-id-for-http (s)
  8555. "Replace special characters in a message id, so it can be used in an http query."
  8556. (when (string-match "%" s)
  8557. (setq s (mapconcat (lambda (c)
  8558. (if (eq c ?%)
  8559. "%25"
  8560. (char-to-string c)))
  8561. s "")))
  8562. (while (string-match "<" s)
  8563. (setq s (replace-match "%3C" t t s)))
  8564. (while (string-match ">" s)
  8565. (setq s (replace-match "%3E" t t s)))
  8566. (while (string-match "@" s)
  8567. (setq s (replace-match "%40" t t s)))
  8568. s)
  8569. (defun org-link-prettify (link)
  8570. "Return a human-readable representation of LINK.
  8571. The car of LINK must be a raw link the cdr of LINK must be either
  8572. a link description or nil."
  8573. (let ((desc (or (cadr link) "<no description>")))
  8574. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8575. "<" (car link) ">")))
  8576. ;;;###autoload
  8577. (defun org-insert-link-global ()
  8578. "Insert a link like Org-mode does.
  8579. This command can be called in any mode to insert a link in Org-mode syntax."
  8580. (interactive)
  8581. (org-load-modules-maybe)
  8582. (org-run-like-in-org-mode 'org-insert-link))
  8583. (defun org-insert-all-links (&optional keep)
  8584. "Insert all links in `org-stored-links'."
  8585. (interactive "P")
  8586. (let ((links (copy-sequence org-stored-links)) l)
  8587. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8588. (insert "- ")
  8589. (org-insert-link nil (car l) (cadr l))
  8590. (insert "\n"))))
  8591. (defun org-link-fontify-links-to-this-file ()
  8592. "Fontify links to the current file in `org-stored-links'."
  8593. (let ((f (buffer-file-name)) a b)
  8594. (setq a (mapcar (lambda(l)
  8595. (let ((ll (car l)))
  8596. (when (and (string-match "^file:\\(.+\\)::" ll)
  8597. (equal f (expand-file-name (match-string 1 ll))))
  8598. ll)))
  8599. org-stored-links))
  8600. (when (featurep 'org-id)
  8601. (setq b (mapcar (lambda(l)
  8602. (let ((ll (car l)))
  8603. (when (and (string-match "^id:\\(.+\\)$" ll)
  8604. (equal f (expand-file-name
  8605. (or (org-id-find-id-file
  8606. (match-string 1 ll)) ""))))
  8607. ll)))
  8608. org-stored-links)))
  8609. (mapcar (lambda(l)
  8610. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8611. (delq nil (append a b)))))
  8612. (defvar org-link-links-in-this-file nil)
  8613. (defun org-insert-link (&optional complete-file link-location default-description)
  8614. "Insert a link. At the prompt, enter the link.
  8615. Completion can be used to insert any of the link protocol prefixes like
  8616. http or ftp in use.
  8617. The history can be used to select a link previously stored with
  8618. `org-store-link'. When the empty string is entered (i.e. if you just
  8619. press RET at the prompt), the link defaults to the most recently
  8620. stored link. As SPC triggers completion in the minibuffer, you need to
  8621. use M-SPC or C-q SPC to force the insertion of a space character.
  8622. You will also be prompted for a description, and if one is given, it will
  8623. be displayed in the buffer instead of the link.
  8624. If there is already a link at point, this command will allow you to edit link
  8625. and description parts.
  8626. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8627. be selected using completion. The path to the file will be relative to the
  8628. current directory if the file is in the current directory or a subdirectory.
  8629. Otherwise, the link will be the absolute path as completed in the minibuffer
  8630. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8631. option `org-link-file-path-type'.
  8632. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8633. the current directory or below.
  8634. With three \\[universal-argument] prefixes, negate the meaning of
  8635. `org-keep-stored-link-after-insertion'.
  8636. If `org-make-link-description-function' is non-nil, this function will be
  8637. called with the link target, and the result will be the default
  8638. link description.
  8639. If the LINK-LOCATION parameter is non-nil, this value will be
  8640. used as the link location instead of reading one interactively.
  8641. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8642. be used as the default description."
  8643. (interactive "P")
  8644. (let* ((wcf (current-window-configuration))
  8645. (origbuf (current-buffer))
  8646. (region (if (org-region-active-p)
  8647. (buffer-substring (region-beginning) (region-end))))
  8648. (remove (and region (list (region-beginning) (region-end))))
  8649. (desc region)
  8650. tmphist ; byte-compile incorrectly complains about this
  8651. (link link-location)
  8652. (abbrevs org-link-abbrev-alist-local)
  8653. entry file all-prefixes auto-desc)
  8654. (cond
  8655. (link-location) ; specified by arg, just use it.
  8656. ((org-in-regexp org-bracket-link-regexp 1)
  8657. ;; We do have a link at point, and we are going to edit it.
  8658. (setq remove (list (match-beginning 0) (match-end 0)))
  8659. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8660. (setq link (read-string "Link: "
  8661. (org-link-unescape
  8662. (org-match-string-no-properties 1)))))
  8663. ((or (org-in-regexp org-angle-link-re)
  8664. (org-in-regexp org-plain-link-re))
  8665. ;; Convert to bracket link
  8666. (setq remove (list (match-beginning 0) (match-end 0))
  8667. link (read-string "Link: "
  8668. (org-remove-angle-brackets (match-string 0)))))
  8669. ((member complete-file '((4) (16)))
  8670. ;; Completing read for file names.
  8671. (setq link (org-file-complete-link complete-file)))
  8672. (t
  8673. ;; Read link, with completion for stored links.
  8674. (org-link-fontify-links-to-this-file)
  8675. (org-switch-to-buffer-other-window "*Org Links*")
  8676. (with-current-buffer "*Org Links*"
  8677. (erase-buffer)
  8678. (insert "Insert a link.
  8679. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8680. (when org-stored-links
  8681. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8682. (insert (mapconcat 'org-link-prettify
  8683. (reverse org-stored-links) "\n")))
  8684. (goto-char (point-min)))
  8685. (let ((cw (selected-window)))
  8686. (select-window (get-buffer-window "*Org Links*" 'visible))
  8687. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8688. (unless (pos-visible-in-window-p (point-max))
  8689. (org-fit-window-to-buffer))
  8690. (and (window-live-p cw) (select-window cw)))
  8691. ;; Fake a link history, containing the stored links.
  8692. (setq tmphist (append (mapcar 'car org-stored-links)
  8693. org-insert-link-history))
  8694. (setq all-prefixes (append (mapcar 'car abbrevs)
  8695. (mapcar 'car org-link-abbrev-alist)
  8696. org-link-types))
  8697. (unwind-protect
  8698. (progn
  8699. (setq link
  8700. (org-completing-read
  8701. "Link: "
  8702. (append
  8703. (mapcar (lambda (x) (concat x ":"))
  8704. all-prefixes)
  8705. (mapcar 'car org-stored-links))
  8706. nil nil nil
  8707. 'tmphist
  8708. (caar org-stored-links)))
  8709. (if (not (string-match "\\S-" link))
  8710. (error "No link selected"))
  8711. (mapc (lambda(l)
  8712. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8713. org-stored-links)
  8714. (if (or (member link all-prefixes)
  8715. (and (equal ":" (substring link -1))
  8716. (member (substring link 0 -1) all-prefixes)
  8717. (setq link (substring link 0 -1))))
  8718. (setq link (with-current-buffer origbuf
  8719. (org-link-try-special-completion link)))))
  8720. (set-window-configuration wcf)
  8721. (kill-buffer "*Org Links*"))
  8722. (setq entry (assoc link org-stored-links))
  8723. (or entry (push link org-insert-link-history))
  8724. (setq desc (or desc (nth 1 entry)))))
  8725. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8726. (not org-keep-stored-link-after-insertion))
  8727. (setq org-stored-links (delq (assoc link org-stored-links)
  8728. org-stored-links)))
  8729. (if (string-match org-plain-link-re link)
  8730. ;; URL-like link, normalize the use of angular brackets.
  8731. (setq link (org-remove-angle-brackets link)))
  8732. ;; Check if we are linking to the current file with a search
  8733. ;; option If yes, simplify the link by using only the search
  8734. ;; option.
  8735. (when (and buffer-file-name
  8736. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8737. (let* ((path (match-string 1 link))
  8738. (case-fold-search nil)
  8739. (search (match-string 2 link)))
  8740. (save-match-data
  8741. (if (equal (file-truename buffer-file-name) (file-truename path))
  8742. ;; We are linking to this same file, with a search option
  8743. (setq link search)))))
  8744. ;; Check if we can/should use a relative path. If yes, simplify the link
  8745. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8746. (let* ((type (match-string 1 link))
  8747. (path (match-string 2 link))
  8748. (origpath path)
  8749. (case-fold-search nil))
  8750. (cond
  8751. ((or (eq org-link-file-path-type 'absolute)
  8752. (equal complete-file '(16)))
  8753. (setq path (abbreviate-file-name (expand-file-name path))))
  8754. ((eq org-link-file-path-type 'noabbrev)
  8755. (setq path (expand-file-name path)))
  8756. ((eq org-link-file-path-type 'relative)
  8757. (setq path (file-relative-name path)))
  8758. (t
  8759. (save-match-data
  8760. (if (string-match (concat "^" (regexp-quote
  8761. (expand-file-name
  8762. (file-name-as-directory
  8763. default-directory))))
  8764. (expand-file-name path))
  8765. ;; We are linking a file with relative path name.
  8766. (setq path (substring (expand-file-name path)
  8767. (match-end 0)))
  8768. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8769. (setq link (concat type path))
  8770. (if (equal desc origpath)
  8771. (setq desc path))))
  8772. (if org-make-link-description-function
  8773. (setq desc
  8774. (or (condition-case nil
  8775. (funcall org-make-link-description-function link desc)
  8776. (error (progn (message "Can't get link description from `%s'"
  8777. (symbol-name org-make-link-description-function))
  8778. (sit-for 2) nil)))
  8779. (read-string "Description: " default-description)))
  8780. (if default-description (setq desc default-description)
  8781. (setq desc (or (and auto-desc desc)
  8782. (read-string "Description: " desc)))))
  8783. (unless (string-match "\\S-" desc) (setq desc nil))
  8784. (if remove (apply 'delete-region remove))
  8785. (insert (org-make-link-string link desc))))
  8786. (defun org-link-try-special-completion (type)
  8787. "If there is completion support for link type TYPE, offer it."
  8788. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8789. (if (functionp fun)
  8790. (funcall fun)
  8791. (read-string "Link (no completion support): " (concat type ":")))))
  8792. (defun org-file-complete-link (&optional arg)
  8793. "Create a file link using completion."
  8794. (let (file link)
  8795. (setq file (org-iread-file-name "File: "))
  8796. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8797. (pwd1 (file-name-as-directory (abbreviate-file-name
  8798. (expand-file-name ".")))))
  8799. (cond
  8800. ((equal arg '(16))
  8801. (setq link (concat
  8802. "file:"
  8803. (abbreviate-file-name (expand-file-name file)))))
  8804. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8805. (setq link (concat "file:" (match-string 1 file))))
  8806. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8807. (expand-file-name file))
  8808. (setq link (concat
  8809. "file:" (match-string 1 (expand-file-name file)))))
  8810. (t (setq link (concat "file:" file)))))
  8811. link))
  8812. (defun org-iread-file-name (&rest args)
  8813. "Read-file-name using `ido-mode' speedup if available.
  8814. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8815. See `read-file-name' for a description of parameters."
  8816. (org-without-partial-completion
  8817. (if (and org-completion-use-ido
  8818. (fboundp 'ido-read-file-name)
  8819. (boundp 'ido-mode) ido-mode
  8820. (listp (second args)))
  8821. (let ((ido-enter-matching-directory nil))
  8822. (apply 'ido-read-file-name args))
  8823. (apply 'read-file-name args))))
  8824. (defun org-completing-read (&rest args)
  8825. "Completing-read with SPACE being a normal character."
  8826. (let ((enable-recursive-minibuffers t)
  8827. (minibuffer-local-completion-map
  8828. (copy-keymap minibuffer-local-completion-map)))
  8829. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8830. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8831. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8832. (apply 'org-icompleting-read args)))
  8833. (defun org-completing-read-no-i (&rest args)
  8834. (let (org-completion-use-ido org-completion-use-iswitchb)
  8835. (apply 'org-completing-read args)))
  8836. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8837. "Use iswitch as a completing-read replacement to choose from choices.
  8838. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8839. from."
  8840. (let* ((iswitchb-use-virtual-buffers nil)
  8841. (iswitchb-make-buflist-hook
  8842. (lambda ()
  8843. (setq iswitchb-temp-buflist choices))))
  8844. (iswitchb-read-buffer prompt)))
  8845. (defun org-icompleting-read (&rest args)
  8846. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8847. (org-without-partial-completion
  8848. (if (and org-completion-use-ido
  8849. (fboundp 'ido-completing-read)
  8850. (boundp 'ido-mode) ido-mode
  8851. (listp (second args)))
  8852. (let ((ido-enter-matching-directory nil))
  8853. (apply 'ido-completing-read (concat (car args))
  8854. (if (consp (car (nth 1 args)))
  8855. (mapcar 'car (nth 1 args))
  8856. (nth 1 args))
  8857. (cddr args)))
  8858. (if (and org-completion-use-iswitchb
  8859. (boundp 'iswitchb-mode) iswitchb-mode
  8860. (listp (second args)))
  8861. (apply 'org-iswitchb-completing-read (concat (car args))
  8862. (if (consp (car (nth 1 args)))
  8863. (mapcar 'car (nth 1 args))
  8864. (nth 1 args))
  8865. (cddr args))
  8866. (apply 'completing-read args)))))
  8867. (defun org-extract-attributes (s)
  8868. "Extract the attributes cookie from a string and set as text property."
  8869. (let (a attr (start 0) key value)
  8870. (save-match-data
  8871. (when (string-match "{{\\([^}]+\\)}}$" s)
  8872. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8873. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8874. (setq key (match-string 1 a) value (match-string 2 a)
  8875. start (match-end 0)
  8876. attr (plist-put attr (intern key) value))))
  8877. (org-add-props s nil 'org-attr attr))
  8878. s))
  8879. (defun org-extract-attributes-from-string (tag)
  8880. (let (key value attr)
  8881. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8882. (setq key (match-string 1 tag) value (match-string 2 tag)
  8883. tag (replace-match "" t t tag)
  8884. attr (plist-put attr (intern key) value)))
  8885. (cons tag attr)))
  8886. (defun org-attributes-to-string (plist)
  8887. "Format a property list into an HTML attribute list."
  8888. (let ((s "") key value)
  8889. (while plist
  8890. (setq key (pop plist) value (pop plist))
  8891. (and value
  8892. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8893. s))
  8894. ;;; Opening/following a link
  8895. (defvar org-link-search-failed nil)
  8896. (defvar org-open-link-functions nil
  8897. "Hook for functions finding a plain text link.
  8898. These functions must take a single argument, the link content.
  8899. They will be called for links that look like [[link text][description]]
  8900. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8901. like a filename (e.g. \"./blue.png\").
  8902. These functions will be called *before* Org attempts to resolve the
  8903. link by doing text searches in the current buffer - so if you want a
  8904. link \"[[target]]\" to still find \"<<target>>\", your function should
  8905. handle this as a special case.
  8906. When the function does handle the link, it must return a non-nil value.
  8907. If it decides that it is not responsible for this link, it must return
  8908. nil to indicate that that Org-mode can continue with other options
  8909. like exact and fuzzy text search.")
  8910. (defun org-next-link (&optional search-backward)
  8911. "Move forward to the next link.
  8912. If the link is in hidden text, expose it."
  8913. (interactive "P")
  8914. (when (and org-link-search-failed (eq this-command last-command))
  8915. (goto-char (point-min))
  8916. (message "Link search wrapped back to beginning of buffer"))
  8917. (setq org-link-search-failed nil)
  8918. (let* ((pos (point))
  8919. (ct (org-context))
  8920. (a (assoc :link ct))
  8921. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  8922. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  8923. ((looking-at org-any-link-re)
  8924. ;; Don't stay stuck at link without an org-link face
  8925. (forward-char (if search-backward -1 1))))
  8926. (if (funcall srch-fun org-any-link-re nil t)
  8927. (progn
  8928. (goto-char (match-beginning 0))
  8929. (if (outline-invisible-p) (org-show-context)))
  8930. (goto-char pos)
  8931. (setq org-link-search-failed t)
  8932. (message "No further link found"))))
  8933. (defun org-previous-link ()
  8934. "Move backward to the previous link.
  8935. If the link is in hidden text, expose it."
  8936. (interactive)
  8937. (funcall 'org-next-link t))
  8938. (defun org-translate-link (s)
  8939. "Translate a link string if a translation function has been defined."
  8940. (if (and org-link-translation-function
  8941. (fboundp org-link-translation-function)
  8942. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8943. (progn
  8944. (setq s (funcall org-link-translation-function
  8945. (match-string 1 s) (match-string 2 s)))
  8946. (concat (car s) ":" (cdr s)))
  8947. s))
  8948. (defun org-translate-link-from-planner (type path)
  8949. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8950. This is still an experimental function, your mileage may vary."
  8951. (cond
  8952. ((member type '("http" "https" "news" "ftp"))
  8953. ;; standard Internet links are the same.
  8954. nil)
  8955. ((and (equal type "irc") (string-match "^//" path))
  8956. ;; Planner has two / at the beginning of an irc link, we have 1.
  8957. ;; We should have zero, actually....
  8958. (setq path (substring path 1)))
  8959. ((and (equal type "lisp") (string-match "^/" path))
  8960. ;; Planner has a slash, we do not.
  8961. (setq type "elisp" path (substring path 1)))
  8962. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8963. ;; A typical message link. Planner has the id after the final slash,
  8964. ;; we separate it with a hash mark
  8965. (setq path (concat (match-string 1 path) "#"
  8966. (org-remove-angle-brackets (match-string 2 path)))))
  8967. )
  8968. (cons type path))
  8969. (defun org-find-file-at-mouse (ev)
  8970. "Open file link or URL at mouse."
  8971. (interactive "e")
  8972. (mouse-set-point ev)
  8973. (org-open-at-point 'in-emacs))
  8974. (defun org-open-at-mouse (ev)
  8975. "Open file link or URL at mouse.
  8976. See the docstring of `org-open-file' for details."
  8977. (interactive "e")
  8978. (mouse-set-point ev)
  8979. (if (eq major-mode 'org-agenda-mode)
  8980. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8981. (org-open-at-point))
  8982. (defvar org-window-config-before-follow-link nil
  8983. "The window configuration before following a link.
  8984. This is saved in case the need arises to restore it.")
  8985. (defvar org-open-link-marker (make-marker)
  8986. "Marker pointing to the location where `org-open-at-point; was called.")
  8987. ;;;###autoload
  8988. (defun org-open-at-point-global ()
  8989. "Follow a link like Org-mode does.
  8990. This command can be called in any mode to follow a link that has
  8991. Org-mode syntax."
  8992. (interactive)
  8993. (org-run-like-in-org-mode 'org-open-at-point))
  8994. ;;;###autoload
  8995. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8996. "Open a link in the string S, as if it was in Org-mode."
  8997. (interactive "sLink: \nP")
  8998. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8999. (with-temp-buffer
  9000. (let ((org-inhibit-startup (not reference-buffer)))
  9001. (org-mode)
  9002. (insert s)
  9003. (goto-char (point-min))
  9004. (when reference-buffer
  9005. (setq org-link-abbrev-alist-local
  9006. (with-current-buffer reference-buffer
  9007. org-link-abbrev-alist-local)))
  9008. (org-open-at-point arg reference-buffer)))))
  9009. (defvar org-open-at-point-functions nil
  9010. "Hook that is run when following a link at point.
  9011. Functions in this hook must return t if they identify and follow
  9012. a link at point. If they don't find anything interesting at point,
  9013. they must return nil.")
  9014. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9015. (defun org-open-at-point (&optional arg reference-buffer)
  9016. "Open link at or after point.
  9017. If there is no link at point, this function will search forward up to
  9018. the end of the current line.
  9019. Normally, files will be opened by an appropriate application. If the
  9020. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9021. With a double prefix argument, try to open outside of Emacs, in the
  9022. application the system uses for this file type."
  9023. (interactive "P")
  9024. ;; if in a code block, then open the block's results
  9025. (unless (call-interactively #'org-babel-open-src-block-result)
  9026. (org-load-modules-maybe)
  9027. (move-marker org-open-link-marker (point))
  9028. (setq org-window-config-before-follow-link (current-window-configuration))
  9029. (org-remove-occur-highlights nil nil t)
  9030. (cond
  9031. ((and (org-at-heading-p)
  9032. (not (org-at-timestamp-p t))
  9033. (not (org-in-regexp
  9034. (concat org-plain-link-re "\\|"
  9035. org-bracket-link-regexp "\\|"
  9036. org-angle-link-re "\\|"
  9037. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9038. (not (get-text-property (point) 'org-linked-text)))
  9039. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9040. (lk0 (car lkall))
  9041. (lk (if (stringp lk0) (list lk0) lk0))
  9042. (lkend (cdr lkall)))
  9043. (mapcar (lambda(l)
  9044. (search-forward l nil lkend)
  9045. (goto-char (match-beginning 0))
  9046. (org-open-at-point))
  9047. lk))
  9048. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9049. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9050. ((and (org-at-timestamp-p t)
  9051. (not (org-in-regexp org-bracket-link-regexp)))
  9052. (org-follow-timestamp-link))
  9053. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9054. (not (org-in-regexp org-any-link-re)))
  9055. (org-footnote-action))
  9056. (t
  9057. (let (type path link line search (pos (point)))
  9058. (catch 'match
  9059. (save-excursion
  9060. (skip-chars-forward "^]\n\r")
  9061. (when (org-in-regexp org-bracket-link-regexp 1)
  9062. (setq link (org-extract-attributes
  9063. (org-link-unescape (org-match-string-no-properties 1))))
  9064. (while (string-match " *\n *" link)
  9065. (setq link (replace-match " " t t link)))
  9066. (setq link (org-link-expand-abbrev link))
  9067. (cond
  9068. ((or (file-name-absolute-p link)
  9069. (string-match "^\\.\\.?/" link))
  9070. (setq type "file" path link))
  9071. ((string-match org-link-re-with-space3 link)
  9072. (setq type (match-string 1 link) path (match-string 2 link)))
  9073. ((string-match "^help:+\\(.+\\)" link)
  9074. (setq type "help" path (match-string 1 link)))
  9075. (t (setq type "thisfile" path link)))
  9076. (throw 'match t)))
  9077. (when (get-text-property (point) 'org-linked-text)
  9078. (setq type "thisfile"
  9079. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9080. (1+ (point)) (point))
  9081. path (buffer-substring
  9082. (or (previous-single-property-change pos 'org-linked-text)
  9083. (point-min))
  9084. (or (next-single-property-change pos 'org-linked-text)
  9085. (point-max))))
  9086. (throw 'match t))
  9087. (save-excursion
  9088. (when (or (org-in-regexp org-angle-link-re)
  9089. (let ((match (org-in-regexp org-plain-link-re)))
  9090. ;; Check a plain link is not within a bracket link
  9091. (and match
  9092. (save-excursion
  9093. (progn
  9094. (goto-char (car match))
  9095. (not (org-in-regexp org-bracket-link-regexp))))))
  9096. (let ((line_ending (save-excursion (end-of-line) (point))))
  9097. ;; We are in a line before a plain or bracket link
  9098. (or (re-search-forward org-plain-link-re line_ending t)
  9099. (re-search-forward org-bracket-link-regexp line_ending t))))
  9100. (setq type (match-string 1)
  9101. path (org-link-unescape (match-string 2)))
  9102. (throw 'match t)))
  9103. (save-excursion
  9104. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9105. (setq type "tags"
  9106. path (match-string 1))
  9107. (while (string-match ":" path)
  9108. (setq path (replace-match "+" t t path)))
  9109. (throw 'match t)))
  9110. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9111. (setq type "tree-match"
  9112. path (match-string 1))
  9113. (throw 'match t)))
  9114. (unless path
  9115. (user-error "No link found"))
  9116. ;; switch back to reference buffer
  9117. ;; needed when if called in a temporary buffer through
  9118. ;; org-open-link-from-string
  9119. (with-current-buffer (or reference-buffer (current-buffer))
  9120. ;; Remove any trailing spaces in path
  9121. (if (string-match " +\\'" path)
  9122. (setq path (replace-match "" t t path)))
  9123. (if (and org-link-translation-function
  9124. (fboundp org-link-translation-function))
  9125. ;; Check if we need to translate the link
  9126. (let ((tmp (funcall org-link-translation-function type path)))
  9127. (setq type (car tmp) path (cdr tmp))))
  9128. (cond
  9129. ((assoc type org-link-protocols)
  9130. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9131. ((equal type "help")
  9132. (let ((f-or-v (intern path)))
  9133. (cond ((fboundp f-or-v)
  9134. (describe-function f-or-v))
  9135. ((boundp f-or-v)
  9136. (describe-variable f-or-v))
  9137. (t (error "Not a known function or variable")))))
  9138. ((equal type "mailto")
  9139. (let ((cmd (car org-link-mailto-program))
  9140. (args (cdr org-link-mailto-program)) args1
  9141. (address path) (subject "") a)
  9142. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9143. (setq address (match-string 1 path)
  9144. subject (org-link-escape (match-string 2 path))))
  9145. (while args
  9146. (cond
  9147. ((not (stringp (car args))) (push (pop args) args1))
  9148. (t (setq a (pop args))
  9149. (if (string-match "%a" a)
  9150. (setq a (replace-match address t t a)))
  9151. (if (string-match "%s" a)
  9152. (setq a (replace-match subject t t a)))
  9153. (push a args1))))
  9154. (apply cmd (nreverse args1))))
  9155. ((member type '("http" "https" "ftp" "news"))
  9156. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9157. (org-link-escape
  9158. path org-link-escape-chars-browser)
  9159. path))))
  9160. ((string= type "doi")
  9161. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9162. (org-link-escape
  9163. path org-link-escape-chars-browser)
  9164. path))))
  9165. ((member type '("message"))
  9166. (browse-url (concat type ":" path)))
  9167. ((string= type "tags")
  9168. (org-tags-view arg path))
  9169. ((string= type "tree-match")
  9170. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9171. ((string= type "file")
  9172. (if (string-match "::\\([0-9]+\\)\\'" path)
  9173. (setq line (string-to-number (match-string 1 path))
  9174. path (substring path 0 (match-beginning 0)))
  9175. (if (string-match "::\\(.+\\)\\'" path)
  9176. (setq search (match-string 1 path)
  9177. path (substring path 0 (match-beginning 0)))))
  9178. (if (string-match "[*?{]" (file-name-nondirectory path))
  9179. (dired path)
  9180. (org-open-file path arg line search)))
  9181. ((string= type "shell")
  9182. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9183. (cmd path))
  9184. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9185. (string-match org-confirm-shell-link-not-regexp cmd))
  9186. (not org-confirm-shell-link-function)
  9187. (funcall org-confirm-shell-link-function
  9188. (format "Execute \"%s\" in shell? "
  9189. (org-add-props cmd nil
  9190. 'face 'org-warning))))
  9191. (progn
  9192. (message "Executing %s" cmd)
  9193. (shell-command cmd buf)
  9194. (if (featurep 'midnight)
  9195. (setq clean-buffer-list-kill-buffer-names
  9196. (cons buf clean-buffer-list-kill-buffer-names))))
  9197. (error "Abort"))))
  9198. ((string= type "elisp")
  9199. (let ((cmd path))
  9200. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9201. (string-match org-confirm-elisp-link-not-regexp cmd))
  9202. (not org-confirm-elisp-link-function)
  9203. (funcall org-confirm-elisp-link-function
  9204. (format "Execute \"%s\" as elisp? "
  9205. (org-add-props cmd nil
  9206. 'face 'org-warning))))
  9207. (message "%s => %s" cmd
  9208. (if (equal (string-to-char cmd) ?\()
  9209. (eval (read cmd))
  9210. (call-interactively (read cmd))))
  9211. (error "Abort"))))
  9212. ((and (string= type "thisfile")
  9213. (run-hook-with-args-until-success
  9214. 'org-open-link-functions path)))
  9215. ((string= type "thisfile")
  9216. (if arg
  9217. (switch-to-buffer-other-window
  9218. (org-get-buffer-for-internal-link (current-buffer)))
  9219. (org-mark-ring-push))
  9220. (let ((cmd `(org-link-search
  9221. ,path
  9222. ,(cond ((equal arg '(4)) ''occur)
  9223. ((equal arg '(16)) ''org-occur))
  9224. ,pos)))
  9225. (condition-case nil (let ((org-link-search-inhibit-query t))
  9226. (eval cmd))
  9227. (error (progn (widen) (eval cmd))))))
  9228. (t (browse-url-at-point)))))))
  9229. (move-marker org-open-link-marker nil)
  9230. (run-hook-with-args 'org-follow-link-hook)))
  9231. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9232. "Offer links in the current entry and return the selected link.
  9233. If there is only one link, return it.
  9234. If NTH is an integer, return the NTH link found.
  9235. If ZERO is a string, check also this string for a link, and if
  9236. there is one, return it."
  9237. (with-current-buffer buffer
  9238. (save-excursion
  9239. (save-restriction
  9240. (widen)
  9241. (goto-char marker)
  9242. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9243. "\\(" org-angle-link-re "\\)\\|"
  9244. "\\(" org-plain-link-re "\\)"))
  9245. (cnt ?0)
  9246. (in-emacs (if (integerp nth) nil nth))
  9247. have-zero end links link c)
  9248. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9249. (push (match-string 0 zero) links)
  9250. (setq cnt (1- cnt) have-zero t))
  9251. (save-excursion
  9252. (org-back-to-heading t)
  9253. (setq end (save-excursion (outline-next-heading) (point)))
  9254. (while (re-search-forward re end t)
  9255. (push (match-string 0) links))
  9256. (setq links (org-uniquify (reverse links))))
  9257. (cond
  9258. ((null links)
  9259. (message "No links"))
  9260. ((equal (length links) 1)
  9261. (setq link (car links)))
  9262. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9263. (setq link (nth (if have-zero nth (1- nth)) links)))
  9264. (t ; we have to select a link
  9265. (save-excursion
  9266. (save-window-excursion
  9267. (delete-other-windows)
  9268. (with-output-to-temp-buffer "*Select Link*"
  9269. (mapc (lambda (l)
  9270. (if (not (string-match org-bracket-link-regexp l))
  9271. (princ (format "[%c] %s\n" (incf cnt)
  9272. (org-remove-angle-brackets l)))
  9273. (if (match-end 3)
  9274. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9275. (match-string 3 l) (match-string 1 l)))
  9276. (princ (format "[%c] %s\n" (incf cnt)
  9277. (match-string 1 l))))))
  9278. links))
  9279. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9280. (message "Select link to open, RET to open all:")
  9281. (setq c (read-char-exclusive))
  9282. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9283. (when (equal c ?q) (error "Abort"))
  9284. (if (equal c ?\C-m)
  9285. (setq link links)
  9286. (setq nth (- c ?0))
  9287. (if have-zero (setq nth (1+ nth)))
  9288. (unless (and (integerp nth) (>= (length links) nth))
  9289. (error "Invalid link selection"))
  9290. (setq link (nth (1- nth) links)))))
  9291. (cons link end))))))
  9292. ;; Add special file links that specify the way of opening
  9293. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9294. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9295. (defun org-open-file-with-system (path)
  9296. "Open file at PATH using the system way of opening it."
  9297. (org-open-file path 'system))
  9298. (defun org-open-file-with-emacs (path)
  9299. "Open file at PATH in Emacs."
  9300. (org-open-file path 'emacs))
  9301. ;;; File search
  9302. (defvar org-create-file-search-functions nil
  9303. "List of functions to construct the right search string for a file link.
  9304. These functions are called in turn with point at the location to
  9305. which the link should point.
  9306. A function in the hook should first test if it would like to
  9307. handle this file type, for example by checking the `major-mode'
  9308. or the file extension. If it decides not to handle this file, it
  9309. should just return nil to give other functions a chance. If it
  9310. does handle the file, it must return the search string to be used
  9311. when following the link. The search string will be part of the
  9312. file link, given after a double colon, and `org-open-at-point'
  9313. will automatically search for it. If special measures must be
  9314. taken to make the search successful, another function should be
  9315. added to the companion hook `org-execute-file-search-functions',
  9316. which see.
  9317. A function in this hook may also use `setq' to set the variable
  9318. `description' to provide a suggestion for the descriptive text to
  9319. be used for this link when it gets inserted into an Org-mode
  9320. buffer with \\[org-insert-link].")
  9321. (defvar org-execute-file-search-functions nil
  9322. "List of functions to execute a file search triggered by a link.
  9323. Functions added to this hook must accept a single argument, the
  9324. search string that was part of the file link, the part after the
  9325. double colon. The function must first check if it would like to
  9326. handle this search, for example by checking the `major-mode' or
  9327. the file extension. If it decides not to handle this search, it
  9328. should just return nil to give other functions a chance. If it
  9329. does handle the search, it must return a non-nil value to keep
  9330. other functions from trying.
  9331. Each function can access the current prefix argument through the
  9332. variable `current-prefix-argument'. Note that a single prefix is
  9333. used to force opening a link in Emacs, so it may be good to only
  9334. use a numeric or double prefix to guide the search function.
  9335. In case this is needed, a function in this hook can also restore
  9336. the window configuration before `org-open-at-point' was called using:
  9337. (set-window-configuration org-window-config-before-follow-link)")
  9338. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9339. (defun org-link-search (s &optional type avoid-pos stealth)
  9340. "Search for a link search option.
  9341. If S is surrounded by forward slashes, it is interpreted as a
  9342. regular expression. In org-mode files, this will create an `org-occur'
  9343. sparse tree. In ordinary files, `occur' will be used to list matches.
  9344. If the current buffer is in `dired-mode', grep will be used to search
  9345. in all files. If AVOID-POS is given, ignore matches near that position.
  9346. When optional argument STEALTH is non-nil, do not modify
  9347. visibility around point, thus ignoring
  9348. `org-show-hierarchy-above', `org-show-following-heading' and
  9349. `org-show-siblings' variables."
  9350. (let ((case-fold-search t)
  9351. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9352. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9353. (append '(("") (" ") ("\t") ("\n"))
  9354. org-emphasis-alist)
  9355. "\\|") "\\)"))
  9356. (pos (point))
  9357. (pre nil) (post nil)
  9358. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9359. (cond
  9360. ;; First check if there are any special search functions
  9361. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9362. ;; Now try the builtin stuff
  9363. ((and (equal (string-to-char s0) ?#)
  9364. (> (length s0) 1)
  9365. (save-excursion
  9366. (goto-char (point-min))
  9367. (and
  9368. (re-search-forward
  9369. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9370. (setq type 'dedicated
  9371. pos (match-beginning 0))))
  9372. ;; There is an exact target for this
  9373. (goto-char pos)
  9374. (org-back-to-heading t)))
  9375. ((save-excursion
  9376. (goto-char (point-min))
  9377. (and
  9378. (re-search-forward
  9379. (concat "<<" (regexp-quote s0) ">>") nil t)
  9380. (setq type 'dedicated
  9381. pos (match-beginning 0))))
  9382. ;; There is an exact target for this
  9383. (goto-char pos))
  9384. ((save-excursion
  9385. (goto-char (point-min))
  9386. (and
  9387. (re-search-forward
  9388. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9389. (setq type 'dedicated pos (match-beginning 0))))
  9390. ;; Found an invisible target.
  9391. (goto-char pos))
  9392. ((save-excursion
  9393. (goto-char (point-min))
  9394. (and
  9395. (re-search-forward
  9396. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9397. (setq type 'dedicated pos (match-beginning 0))))
  9398. ;; Found an element with a matching #+name affiliated keyword.
  9399. (goto-char pos))
  9400. ((and (string-match "^(\\(.*\\))$" s0)
  9401. (save-excursion
  9402. (goto-char (point-min))
  9403. (and
  9404. (re-search-forward
  9405. (concat "[^[]" (regexp-quote
  9406. (format org-coderef-label-format
  9407. (match-string 1 s0))))
  9408. nil t)
  9409. (setq type 'dedicated
  9410. pos (1+ (match-beginning 0))))))
  9411. ;; There is a coderef target for this
  9412. (goto-char pos))
  9413. ((string-match "^/\\(.*\\)/$" s)
  9414. ;; A regular expression
  9415. (cond
  9416. ((derived-mode-p 'org-mode)
  9417. (org-occur (match-string 1 s)))
  9418. (t (org-do-occur (match-string 1 s)))))
  9419. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9420. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9421. (goto-char (point-min))
  9422. (cond
  9423. ((let (case-fold-search)
  9424. (re-search-forward (format org-complex-heading-regexp-format
  9425. (regexp-quote s))
  9426. nil t))
  9427. ;; OK, found a match
  9428. (setq type 'dedicated)
  9429. (goto-char (match-beginning 0)))
  9430. ((and (not org-link-search-inhibit-query)
  9431. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9432. (y-or-n-p "No match - create this as a new heading? "))
  9433. (goto-char (point-max))
  9434. (or (bolp) (newline))
  9435. (insert "* " s "\n")
  9436. (beginning-of-line 0))
  9437. (t
  9438. (goto-char pos)
  9439. (error "No match"))))
  9440. (t
  9441. ;; A normal search string
  9442. (when (equal (string-to-char s) ?*)
  9443. ;; Anchor on headlines, post may include tags.
  9444. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9445. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9446. s (substring s 1)))
  9447. (remove-text-properties
  9448. 0 (length s)
  9449. '(face nil mouse-face nil keymap nil fontified nil) s)
  9450. ;; Make a series of regular expressions to find a match
  9451. (setq words (org-split-string s "[ \n\r\t]+")
  9452. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9453. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9454. "\\)" markers)
  9455. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9456. re2a (concat "[ \t\r\n]" re2a_)
  9457. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9458. re4 (concat "[^a-zA-Z_]" re4_)
  9459. re1 (concat pre re2 post)
  9460. re3 (concat pre (if pre re4_ re4) post)
  9461. re5 (concat pre ".*" re4)
  9462. re2 (concat pre re2)
  9463. re2a (concat pre (if pre re2a_ re2a))
  9464. re4 (concat pre (if pre re4_ re4))
  9465. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9466. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9467. re5 "\\)"
  9468. ))
  9469. (cond
  9470. ((eq type 'org-occur) (org-occur reall))
  9471. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9472. (t (goto-char (point-min))
  9473. (setq type 'fuzzy)
  9474. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9475. (org-search-not-self 1 re1 nil t)
  9476. (org-search-not-self 1 re2 nil t)
  9477. (org-search-not-self 1 re2a nil t)
  9478. (org-search-not-self 1 re3 nil t)
  9479. (org-search-not-self 1 re4 nil t)
  9480. (org-search-not-self 1 re5 nil t)
  9481. )
  9482. (goto-char (match-beginning 1))
  9483. (goto-char pos)
  9484. (error "No match"))))))
  9485. (and (derived-mode-p 'org-mode)
  9486. (not stealth)
  9487. (org-show-context 'link-search))
  9488. type))
  9489. (defun org-search-not-self (group &rest args)
  9490. "Execute `re-search-forward', but only accept matches that do not
  9491. enclose the position of `org-open-link-marker'."
  9492. (let ((m org-open-link-marker))
  9493. (catch 'exit
  9494. (while (apply 're-search-forward args)
  9495. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9496. (goto-char (match-end group))
  9497. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9498. (> (match-beginning 0) (marker-position m))
  9499. (< (match-end 0) (marker-position m)))
  9500. (save-match-data
  9501. (or (not (org-in-regexp
  9502. org-bracket-link-analytic-regexp 1))
  9503. (not (match-end 4)) ; no description
  9504. (and (<= (match-beginning 4) (point))
  9505. (>= (match-end 4) (point))))))
  9506. (throw 'exit (point))))))))
  9507. (defun org-get-buffer-for-internal-link (buffer)
  9508. "Return a buffer to be used for displaying the link target of internal links."
  9509. (cond
  9510. ((not org-display-internal-link-with-indirect-buffer)
  9511. buffer)
  9512. ((string-match "(Clone)$" (buffer-name buffer))
  9513. (message "Buffer is already a clone, not making another one")
  9514. ;; we also do not modify visibility in this case
  9515. buffer)
  9516. (t ; make a new indirect buffer for displaying the link
  9517. (let* ((bn (buffer-name buffer))
  9518. (ibn (concat bn "(Clone)"))
  9519. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9520. (with-current-buffer ib (org-overview))
  9521. ib))))
  9522. (defun org-do-occur (regexp &optional cleanup)
  9523. "Call the Emacs command `occur'.
  9524. If CLEANUP is non-nil, remove the printout of the regular expression
  9525. in the *Occur* buffer. This is useful if the regex is long and not useful
  9526. to read."
  9527. (occur regexp)
  9528. (when cleanup
  9529. (let ((cwin (selected-window)) win beg end)
  9530. (when (setq win (get-buffer-window "*Occur*"))
  9531. (select-window win))
  9532. (goto-char (point-min))
  9533. (when (re-search-forward "match[a-z]+" nil t)
  9534. (setq beg (match-end 0))
  9535. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9536. (setq end (1- (match-beginning 0)))))
  9537. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9538. (goto-char (point-min))
  9539. (select-window cwin))))
  9540. ;;; The mark ring for links jumps
  9541. (defvar org-mark-ring nil
  9542. "Mark ring for positions before jumps in Org-mode.")
  9543. (defvar org-mark-ring-last-goto nil
  9544. "Last position in the mark ring used to go back.")
  9545. ;; Fill and close the ring
  9546. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9547. (loop for i from 1 to org-mark-ring-length do
  9548. (push (make-marker) org-mark-ring))
  9549. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9550. org-mark-ring)
  9551. (defun org-mark-ring-push (&optional pos buffer)
  9552. "Put the current position or POS into the mark ring and rotate it."
  9553. (interactive)
  9554. (setq pos (or pos (point)))
  9555. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9556. (move-marker (car org-mark-ring)
  9557. (or pos (point))
  9558. (or buffer (current-buffer)))
  9559. (message "%s"
  9560. (substitute-command-keys
  9561. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9562. (defun org-mark-ring-goto (&optional n)
  9563. "Jump to the previous position in the mark ring.
  9564. With prefix arg N, jump back that many stored positions. When
  9565. called several times in succession, walk through the entire ring.
  9566. Org-mode commands jumping to a different position in the current file,
  9567. or to another Org-mode file, automatically push the old position
  9568. onto the ring."
  9569. (interactive "p")
  9570. (let (p m)
  9571. (if (eq last-command this-command)
  9572. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9573. (setq p org-mark-ring))
  9574. (setq org-mark-ring-last-goto p)
  9575. (setq m (car p))
  9576. (org-pop-to-buffer-same-window (marker-buffer m))
  9577. (goto-char m)
  9578. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9579. (defun org-remove-angle-brackets (s)
  9580. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9581. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9582. s)
  9583. (defun org-add-angle-brackets (s)
  9584. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9585. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9586. s)
  9587. (defun org-remove-double-quotes (s)
  9588. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9589. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9590. s)
  9591. ;;; Following specific links
  9592. (defun org-follow-timestamp-link ()
  9593. "Open an agenda view for the time-stamp date/range at point."
  9594. (cond
  9595. ((org-at-date-range-p t)
  9596. (let ((org-agenda-start-on-weekday)
  9597. (t1 (match-string 1))
  9598. (t2 (match-string 2)) tt1 tt2)
  9599. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9600. tt2 (time-to-days (org-time-string-to-time t2)))
  9601. (let ((org-agenda-buffer-tmp-name
  9602. (format "*Org Agenda(a:%s)"
  9603. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9604. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9605. ((org-at-timestamp-p t)
  9606. (let ((org-agenda-buffer-tmp-name
  9607. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9608. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9609. (substring (match-string 1) 0 10)))
  9610. 1)))
  9611. (t (error "This should not happen"))))
  9612. ;;; Following file links
  9613. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9614. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9615. (declare-function mailcap-mime-info
  9616. "mailcap" (string &optional request no-decode))
  9617. (defvar org-wait nil)
  9618. (defun org-open-file (path &optional in-emacs line search)
  9619. "Open the file at PATH.
  9620. First, this expands any special file name abbreviations. Then the
  9621. configuration variable `org-file-apps' is checked if it contains an
  9622. entry for this file type, and if yes, the corresponding command is launched.
  9623. If no application is found, Emacs simply visits the file.
  9624. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9625. With a double \\[universal-argument] \\[universal-argument] \
  9626. prefix arg, Org tries to avoid opening in Emacs
  9627. and to use an external application to visit the file.
  9628. Optional LINE specifies a line to go to, optional SEARCH a string
  9629. to search for. If LINE or SEARCH is given, the file will be
  9630. opened in Emacs, unless an entry from org-file-apps that makes
  9631. use of groups in a regexp matches.
  9632. If you want to change the way frames are used when following a
  9633. link, please customize `org-link-frame-setup'.
  9634. If the file does not exist, an error is thrown."
  9635. (let* ((file (if (equal path "")
  9636. buffer-file-name
  9637. (substitute-in-file-name (expand-file-name path))))
  9638. (file-apps (append org-file-apps (org-default-apps)))
  9639. (apps (org-remove-if
  9640. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9641. (apps-dlink (org-remove-if-not
  9642. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9643. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9644. (dirp (if remp nil (file-directory-p file)))
  9645. (file (if (and dirp org-open-directory-means-index-dot-org)
  9646. (concat (file-name-as-directory file) "index.org")
  9647. file))
  9648. (a-m-a-p (assq 'auto-mode apps))
  9649. (dfile (downcase file))
  9650. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9651. (link (cond ((and (eq line nil)
  9652. (eq search nil))
  9653. file)
  9654. (line
  9655. (concat file "::" (number-to-string line)))
  9656. (search
  9657. (concat file "::" search))))
  9658. (dlink (downcase link))
  9659. (old-buffer (current-buffer))
  9660. (old-pos (point))
  9661. (old-mode major-mode)
  9662. ext cmd link-match-data)
  9663. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9664. (setq ext (match-string 1 dfile))
  9665. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9666. (setq ext (match-string 1 dfile))))
  9667. (cond
  9668. ((member in-emacs '((16) system))
  9669. (setq cmd (cdr (assoc 'system apps))))
  9670. (in-emacs (setq cmd 'emacs))
  9671. (t
  9672. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9673. (and dirp (cdr (assoc 'directory apps)))
  9674. ; first, try matching against apps-dlink
  9675. ; if we get a match here, store the match data for later
  9676. (let ((match (assoc-default dlink apps-dlink
  9677. 'string-match)))
  9678. (if match
  9679. (progn (setq link-match-data (match-data))
  9680. match)
  9681. (progn (setq in-emacs (or in-emacs line search))
  9682. nil))) ; if we have no match in apps-dlink,
  9683. ; always open the file in emacs if line or search
  9684. ; is given (for backwards compatibility)
  9685. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9686. 'string-match)
  9687. (cdr (assoc ext apps))
  9688. (cdr (assoc t apps))))))
  9689. (when (eq cmd 'system)
  9690. (setq cmd (cdr (assoc 'system apps))))
  9691. (when (eq cmd 'default)
  9692. (setq cmd (cdr (assoc t apps))))
  9693. (when (eq cmd 'mailcap)
  9694. (require 'mailcap)
  9695. (mailcap-parse-mailcaps)
  9696. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9697. (command (mailcap-mime-info mime-type)))
  9698. (if (stringp command)
  9699. (setq cmd command)
  9700. (setq cmd 'emacs))))
  9701. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9702. (not (file-exists-p file))
  9703. (not org-open-non-existing-files))
  9704. (error "No such file: %s" file))
  9705. (cond
  9706. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9707. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9708. (while (string-match "['\"]%s['\"]" cmd)
  9709. (setq cmd (replace-match "%s" t t cmd)))
  9710. (while (string-match "%s" cmd)
  9711. (setq cmd (replace-match
  9712. (save-match-data
  9713. (shell-quote-argument
  9714. (convert-standard-filename file)))
  9715. t t cmd)))
  9716. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9717. (save-match-data
  9718. (let ((match-index 1)
  9719. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9720. (set-match-data link-match-data)
  9721. (while (<= match-index number-of-groups)
  9722. (let ((regex (concat "%" (number-to-string match-index)))
  9723. (replace-with (match-string match-index dlink)))
  9724. (while (string-match regex cmd)
  9725. (setq cmd (replace-match replace-with t t cmd))))
  9726. (setq match-index (+ match-index 1)))))
  9727. (save-window-excursion
  9728. (message "Running %s...done" cmd)
  9729. (start-process-shell-command cmd nil cmd)
  9730. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9731. ((or (stringp cmd)
  9732. (eq cmd 'emacs))
  9733. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9734. (widen)
  9735. (if line (org-goto-line line)
  9736. (if search (org-link-search search))))
  9737. ((consp cmd)
  9738. (let ((file (convert-standard-filename file)))
  9739. (save-match-data
  9740. (set-match-data link-match-data)
  9741. (eval cmd))))
  9742. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9743. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9744. (or (not (equal old-buffer (current-buffer)))
  9745. (not (equal old-pos (point))))
  9746. (org-mark-ring-push old-pos old-buffer))))
  9747. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9748. "This function returns non-nil if `entry' uses a regular
  9749. expression which should be matched against the whole link by
  9750. org-open-file.
  9751. It assumes that is the case when the entry uses a regular
  9752. expression which has at least one grouping construct and the
  9753. action is either a lisp form or a command string containing
  9754. '%1', i.e. using at least one subexpression match as a
  9755. parameter."
  9756. (let ((selector (car entry))
  9757. (action (cdr entry)))
  9758. (if (stringp selector)
  9759. (and (> (regexp-opt-depth selector) 0)
  9760. (or (and (stringp action)
  9761. (string-match "%[0-9]" action))
  9762. (consp action)))
  9763. nil)))
  9764. (defun org-default-apps ()
  9765. "Return the default applications for this operating system."
  9766. (cond
  9767. ((eq system-type 'darwin)
  9768. org-file-apps-defaults-macosx)
  9769. ((eq system-type 'windows-nt)
  9770. org-file-apps-defaults-windowsnt)
  9771. (t org-file-apps-defaults-gnu)))
  9772. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9773. "Convert extensions to regular expressions in the cars of LIST.
  9774. Also, weed out any non-string entries, because the return value is used
  9775. only for regexp matching.
  9776. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9777. point to the symbol `emacs', indicating that the file should
  9778. be opened in Emacs."
  9779. (append
  9780. (delq nil
  9781. (mapcar (lambda (x)
  9782. (if (not (stringp (car x)))
  9783. nil
  9784. (if (string-match "\\W" (car x))
  9785. x
  9786. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9787. list))
  9788. (if add-auto-mode
  9789. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9790. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9791. (defun org-file-remote-p (file)
  9792. "Test whether FILE specifies a location on a remote system.
  9793. Return non-nil if the location is indeed remote.
  9794. For example, the filename \"/user@host:/foo\" specifies a location
  9795. on the system \"/user@host:\"."
  9796. (cond ((fboundp 'file-remote-p)
  9797. (file-remote-p file))
  9798. ((fboundp 'tramp-handle-file-remote-p)
  9799. (tramp-handle-file-remote-p file))
  9800. ((and (boundp 'ange-ftp-name-format)
  9801. (string-match (car ange-ftp-name-format) file))
  9802. t)))
  9803. ;;;; Refiling
  9804. (defun org-get-org-file ()
  9805. "Read a filename, with default directory `org-directory'."
  9806. (let ((default (or org-default-notes-file remember-data-file)))
  9807. (read-file-name (format "File name [%s]: " default)
  9808. (file-name-as-directory org-directory)
  9809. default)))
  9810. (defun org-notes-order-reversed-p ()
  9811. "Check if the current file should receive notes in reversed order."
  9812. (cond
  9813. ((not org-reverse-note-order) nil)
  9814. ((eq t org-reverse-note-order) t)
  9815. ((not (listp org-reverse-note-order)) nil)
  9816. (t (catch 'exit
  9817. (let ((all org-reverse-note-order)
  9818. entry)
  9819. (while (setq entry (pop all))
  9820. (if (string-match (car entry) buffer-file-name)
  9821. (throw 'exit (cdr entry))))
  9822. nil)))))
  9823. (defvar org-refile-target-table nil
  9824. "The list of refile targets, created by `org-refile'.")
  9825. (defvar org-agenda-new-buffers nil
  9826. "Buffers created to visit agenda files.")
  9827. (defvar org-refile-cache nil
  9828. "Cache for refile targets.")
  9829. (defvar org-refile-markers nil
  9830. "All the markers used for caching refile locations.")
  9831. (defun org-refile-marker (pos)
  9832. "Get a new refile marker, but only if caching is in use."
  9833. (if (not org-refile-use-cache)
  9834. pos
  9835. (let ((m (make-marker)))
  9836. (move-marker m pos)
  9837. (push m org-refile-markers)
  9838. m)))
  9839. (defun org-refile-cache-clear ()
  9840. "Clear the refile cache and disable all the markers."
  9841. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9842. (setq org-refile-markers nil)
  9843. (setq org-refile-cache nil)
  9844. (message "Refile cache has been cleared"))
  9845. (defun org-refile-cache-check-set (set)
  9846. "Check if all the markers in the cache still have live buffers."
  9847. (let (marker)
  9848. (catch 'exit
  9849. (while (and set (setq marker (nth 3 (pop set))))
  9850. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9851. (when (and marker (null (marker-buffer marker)))
  9852. (message "not found") (sit-for 3)
  9853. (throw 'exit nil)))
  9854. t)))
  9855. (defun org-refile-cache-put (set &rest identifiers)
  9856. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9857. (let* ((key (sha1 (prin1-to-string identifiers)))
  9858. (entry (assoc key org-refile-cache)))
  9859. (if entry
  9860. (setcdr entry set)
  9861. (push (cons key set) org-refile-cache))))
  9862. (defun org-refile-cache-get (&rest identifiers)
  9863. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9864. (cond
  9865. ((not org-refile-cache) nil)
  9866. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9867. (t
  9868. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9869. org-refile-cache))))
  9870. (and set (org-refile-cache-check-set set) set)))))
  9871. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9872. "Produce a table with refile targets."
  9873. (let ((case-fold-search nil)
  9874. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9875. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9876. targets tgs txt re files f desc descre fast-path-p level pos0)
  9877. (message "Getting targets...")
  9878. (with-current-buffer (or default-buffer (current-buffer))
  9879. (while (setq entry (pop entries))
  9880. (setq files (car entry) desc (cdr entry))
  9881. (setq fast-path-p nil)
  9882. (cond
  9883. ((null files) (setq files (list (current-buffer))))
  9884. ((eq files 'org-agenda-files)
  9885. (setq files (org-agenda-files 'unrestricted)))
  9886. ((and (symbolp files) (fboundp files))
  9887. (setq files (funcall files)))
  9888. ((and (symbolp files) (boundp files))
  9889. (setq files (symbol-value files))))
  9890. (if (stringp files) (setq files (list files)))
  9891. (cond
  9892. ((eq (car desc) :tag)
  9893. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9894. ((eq (car desc) :todo)
  9895. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9896. ((eq (car desc) :regexp)
  9897. (setq descre (cdr desc)))
  9898. ((eq (car desc) :level)
  9899. (setq descre (concat "^\\*\\{" (number-to-string
  9900. (if org-odd-levels-only
  9901. (1- (* 2 (cdr desc)))
  9902. (cdr desc)))
  9903. "\\}[ \t]")))
  9904. ((eq (car desc) :maxlevel)
  9905. (setq fast-path-p t)
  9906. (setq descre (concat "^\\*\\{1," (number-to-string
  9907. (if org-odd-levels-only
  9908. (1- (* 2 (cdr desc)))
  9909. (cdr desc)))
  9910. "\\}[ \t]")))
  9911. (t (error "Bad refiling target description %s" desc)))
  9912. (while (setq f (pop files))
  9913. (with-current-buffer
  9914. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9915. (or
  9916. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9917. (progn
  9918. (if (bufferp f) (setq f (buffer-file-name
  9919. (buffer-base-buffer f))))
  9920. (setq f (and f (expand-file-name f)))
  9921. (if (eq org-refile-use-outline-path 'file)
  9922. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9923. (save-excursion
  9924. (save-restriction
  9925. (widen)
  9926. (goto-char (point-min))
  9927. (while (re-search-forward descre nil t)
  9928. (goto-char (setq pos0 (point-at-bol)))
  9929. (catch 'next
  9930. (when org-refile-target-verify-function
  9931. (save-match-data
  9932. (or (funcall org-refile-target-verify-function)
  9933. (throw 'next t))))
  9934. (when (and (looking-at org-complex-heading-regexp)
  9935. (not (member (match-string 4) excluded-entries))
  9936. (match-string 4))
  9937. (setq level (org-reduced-level
  9938. (- (match-end 1) (match-beginning 1)))
  9939. txt (org-link-display-format (match-string 4))
  9940. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9941. re (format org-complex-heading-regexp-format
  9942. (regexp-quote (match-string 4))))
  9943. (when org-refile-use-outline-path
  9944. (setq txt (mapconcat
  9945. 'org-protect-slash
  9946. (append
  9947. (if (eq org-refile-use-outline-path
  9948. 'file)
  9949. (list (file-name-nondirectory
  9950. (buffer-file-name
  9951. (buffer-base-buffer))))
  9952. (if (eq org-refile-use-outline-path
  9953. 'full-file-path)
  9954. (list (buffer-file-name
  9955. (buffer-base-buffer)))))
  9956. (org-get-outline-path fast-path-p
  9957. level txt)
  9958. (list txt))
  9959. "/")))
  9960. (push (list txt f re (org-refile-marker (point)))
  9961. tgs)))
  9962. (when (= (point) pos0)
  9963. ;; verification function has not moved point
  9964. (goto-char (point-at-eol))))))))
  9965. (when org-refile-use-cache
  9966. (org-refile-cache-put tgs (buffer-file-name) descre))
  9967. (setq targets (append tgs targets))
  9968. ))))
  9969. (message "Getting targets...done")
  9970. (nreverse targets)))
  9971. (defun org-protect-slash (s)
  9972. (while (string-match "/" s)
  9973. (setq s (replace-match "\\" t t s)))
  9974. s)
  9975. (defvar org-olpa (make-vector 20 nil))
  9976. (defun org-get-outline-path (&optional fastp level heading)
  9977. "Return the outline path to the current entry, as a list.
  9978. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9979. routine which makes outline path derivations for an entire file,
  9980. avoiding backtracing. Refile target collection makes use of that."
  9981. (if fastp
  9982. (progn
  9983. (if (> level 19)
  9984. (error "Outline path failure, more than 19 levels"))
  9985. (loop for i from level upto 19 do
  9986. (aset org-olpa i nil))
  9987. (prog1
  9988. (delq nil (append org-olpa nil))
  9989. (aset org-olpa level heading)))
  9990. (let (rtn case-fold-search)
  9991. (save-excursion
  9992. (save-restriction
  9993. (widen)
  9994. (while (org-up-heading-safe)
  9995. (when (looking-at org-complex-heading-regexp)
  9996. (push (org-match-string-no-properties 4) rtn)))
  9997. rtn)))))
  9998. (defun org-format-outline-path (path &optional width prefix separator)
  9999. "Format the outline path PATH for display.
  10000. WIDTH is the maximum number of characters that is available.
  10001. PREFIX is a prefix to be included in the returned string,
  10002. such as the file name.
  10003. SEPARATOR is inserted between the different parts of the path,
  10004. the default is \"/\"."
  10005. (setq width (or width 79))
  10006. (if prefix (setq width (- width (length prefix))))
  10007. (if (not path)
  10008. (or prefix "")
  10009. (let* ((nsteps (length path))
  10010. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10011. (maxwidth (if (<= total-width width)
  10012. 10000 ;; everything fits
  10013. ;; we need to shorten the level headings
  10014. (/ (- width nsteps) nsteps)))
  10015. (org-odd-levels-only nil)
  10016. (n 0)
  10017. (total (1+ (length prefix))))
  10018. (setq maxwidth (max maxwidth 10))
  10019. (concat prefix
  10020. (if prefix (or separator "/"))
  10021. (mapconcat
  10022. (lambda (h)
  10023. (setq n (1+ n))
  10024. (if (and (= n nsteps) (< maxwidth 10000))
  10025. (setq maxwidth (- total-width total)))
  10026. (if (< (length h) maxwidth)
  10027. (progn (setq total (+ total (length h) 1)) h)
  10028. (setq h (substring h 0 (- maxwidth 2))
  10029. total (+ total maxwidth 1))
  10030. (if (string-match "[ \t]+\\'" h)
  10031. (setq h (substring h 0 (match-beginning 0))))
  10032. (setq h (concat h "..")))
  10033. (org-add-props h nil 'face
  10034. (nth (% (1- n) org-n-level-faces)
  10035. org-level-faces))
  10036. h)
  10037. path (or separator "/"))))))
  10038. (defun org-display-outline-path (&optional file current separator just-return-string)
  10039. "Display the current outline path in the echo area.
  10040. If FILE is non-nil, prepend the output with the file name.
  10041. If CURRENT is non-nil, append the current heading to the output.
  10042. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10043. the different parts of the path and defaults to \"/\".
  10044. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10045. (interactive "P")
  10046. (let* (case-fold-search
  10047. message-log-max ; Don't populate the *Messages* buffer
  10048. (bfn (buffer-file-name (buffer-base-buffer)))
  10049. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10050. res)
  10051. (if current (setq path (append path
  10052. (save-excursion
  10053. (org-back-to-heading t)
  10054. (if (looking-at org-complex-heading-regexp)
  10055. (list (match-string 4)))))))
  10056. (setq res
  10057. (org-format-outline-path
  10058. path
  10059. (1- (frame-width))
  10060. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10061. separator))
  10062. (if just-return-string
  10063. (org-no-properties res)
  10064. (message "%s" res))))
  10065. (defvar org-refile-history nil
  10066. "History for refiling operations.")
  10067. (defvar org-after-refile-insert-hook nil
  10068. "Hook run after `org-refile' has inserted its stuff at the new location.
  10069. Note that this is still *before* the stuff will be removed from
  10070. the *old* location.")
  10071. (defvar org-capture-last-stored-marker)
  10072. (defvar org-refile-keep nil
  10073. "Non-nil means `org-refile' will copy instead of refile.")
  10074. (defun org-copy ()
  10075. "Like `org-refile', but copy."
  10076. (interactive)
  10077. (let ((org-refile-keep t))
  10078. (funcall 'org-refile nil nil nil "Copy")))
  10079. (defun org-refile (&optional goto default-buffer rfloc msg)
  10080. "Move the entry or entries at point to another heading.
  10081. The list of target headings is compiled using the information in
  10082. `org-refile-targets', which see.
  10083. At the target location, the entry is filed as a subitem of the target
  10084. heading. Depending on `org-reverse-note-order', the new subitem will
  10085. either be the first or the last subitem.
  10086. If there is an active region, all entries in that region will be moved.
  10087. However, the region must fulfill the requirement that the first heading
  10088. is the first one sets the top-level of the moved text - at most siblings
  10089. below it are allowed.
  10090. With prefix arg GOTO, the command will only visit the target location
  10091. and not actually move anything.
  10092. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10093. go to the location where the last refiling operation has put the subtree.
  10094. With a prefix argument of `2', refile to the running clock.
  10095. RFLOC can be a refile location obtained in a different way.
  10096. MSG is a string to replace \"Refile\" in the default prompt with
  10097. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10098. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10099. If you are using target caching (see `org-refile-use-cache'),
  10100. you have to clear the target cache in order to find new targets.
  10101. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10102. prefix argument (`C-u C-u C-u C-c C-w')."
  10103. (interactive "P")
  10104. (if (member goto '(0 (64)))
  10105. (org-refile-cache-clear)
  10106. (let* ((actionmsg (or msg "Refile"))
  10107. (cbuf (current-buffer))
  10108. (regionp (org-region-active-p))
  10109. (region-start (and regionp (region-beginning)))
  10110. (region-end (and regionp (region-end)))
  10111. (region-length (and regionp (- region-end region-start)))
  10112. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10113. pos it nbuf file re level reversed)
  10114. (setq last-command nil)
  10115. (when regionp
  10116. (goto-char region-start)
  10117. (or (bolp) (goto-char (point-at-bol)))
  10118. (setq region-start (point))
  10119. (unless (or (org-kill-is-subtree-p
  10120. (buffer-substring region-start region-end))
  10121. (prog1 org-refile-active-region-within-subtree
  10122. (org-toggle-heading)))
  10123. (error "The region is not a (sequence of) subtree(s)")))
  10124. (if (equal goto '(16))
  10125. (org-refile-goto-last-stored)
  10126. (when (or
  10127. (and (equal goto 2)
  10128. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10129. (prog1
  10130. (setq it (list (or org-clock-heading "running clock")
  10131. (buffer-file-name
  10132. (marker-buffer org-clock-hd-marker))
  10133. ""
  10134. (marker-position org-clock-hd-marker)))
  10135. (setq goto nil)))
  10136. (setq it (or rfloc
  10137. (let (heading-text)
  10138. (save-excursion
  10139. (unless goto
  10140. (org-back-to-heading t)
  10141. (setq heading-text
  10142. (nth 4 (org-heading-components))))
  10143. (org-refile-get-location
  10144. (cond (goto "Goto")
  10145. (regionp (concat actionmsg " region to"))
  10146. (t (concat actionmsg " subtree \""
  10147. heading-text "\" to")))
  10148. default-buffer
  10149. (and (not (equal '(4) goto))
  10150. org-refile-allow-creating-parent-nodes)
  10151. goto))))))
  10152. (setq file (nth 1 it)
  10153. re (nth 2 it)
  10154. pos (nth 3 it))
  10155. (if (and (not goto)
  10156. pos
  10157. (equal (buffer-file-name) file)
  10158. (if regionp
  10159. (and (>= pos region-start)
  10160. (<= pos region-end))
  10161. (and (>= pos (point))
  10162. (< pos (save-excursion
  10163. (org-end-of-subtree t t))))))
  10164. (error "Cannot refile to position inside the tree or region"))
  10165. (setq nbuf (or (find-buffer-visiting file)
  10166. (find-file-noselect file)))
  10167. (if goto
  10168. (progn
  10169. (org-pop-to-buffer-same-window nbuf)
  10170. (goto-char pos)
  10171. (org-show-context 'org-goto))
  10172. (if regionp
  10173. (progn
  10174. (org-kill-new (buffer-substring region-start region-end))
  10175. (org-save-markers-in-region region-start region-end))
  10176. (org-copy-subtree 1 nil t))
  10177. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10178. (find-file-noselect file)))
  10179. (setq reversed (org-notes-order-reversed-p))
  10180. (save-excursion
  10181. (save-restriction
  10182. (widen)
  10183. (if pos
  10184. (progn
  10185. (goto-char pos)
  10186. (looking-at org-outline-regexp)
  10187. (setq level (org-get-valid-level (funcall outline-level) 1))
  10188. (goto-char
  10189. (if reversed
  10190. (or (outline-next-heading) (point-max))
  10191. (or (save-excursion (org-get-next-sibling))
  10192. (org-end-of-subtree t t)
  10193. (point-max)))))
  10194. (setq level 1)
  10195. (if (not reversed)
  10196. (goto-char (point-max))
  10197. (goto-char (point-min))
  10198. (or (outline-next-heading) (goto-char (point-max)))))
  10199. (if (not (bolp)) (newline))
  10200. (org-paste-subtree level)
  10201. (when org-log-refile
  10202. (org-add-log-setup 'refile nil nil 'findpos
  10203. org-log-refile)
  10204. (unless (eq org-log-refile 'note)
  10205. (save-excursion (org-add-log-note))))
  10206. (and org-auto-align-tags
  10207. (let ((org-loop-over-headlines-in-active-region nil))
  10208. (org-set-tags nil t)))
  10209. (with-demoted-errors
  10210. (bookmark-set "org-refile-last-stored"))
  10211. ;; If we are refiling for capture, make sure that the
  10212. ;; last-capture pointers point here
  10213. (when (org-bound-and-true-p org-refile-for-capture)
  10214. (with-demoted-errors
  10215. (bookmark-set "org-capture-last-stored-marker"))
  10216. (move-marker org-capture-last-stored-marker (point)))
  10217. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10218. (run-hooks 'org-after-refile-insert-hook))))
  10219. (unless org-refile-keep
  10220. (if regionp
  10221. (delete-region (point) (+ (point) region-length))
  10222. (org-cut-subtree)))
  10223. (when (featurep 'org-inlinetask)
  10224. (org-inlinetask-remove-END-maybe))
  10225. (setq org-markers-to-move nil)
  10226. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10227. (defun org-refile-goto-last-stored ()
  10228. "Go to the location where the last refile was stored."
  10229. (interactive)
  10230. (bookmark-jump "org-refile-last-stored")
  10231. (message "This is the location of the last refile"))
  10232. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10233. no-exclude)
  10234. "Prompt the user for a refile location, using PROMPT.
  10235. PROMPT should not be suffixed with a colon and a space, because
  10236. this function appends the default value from
  10237. `org-refile-history' automatically, if that is not empty.
  10238. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10239. this is used for the GOTO interface."
  10240. (let ((org-refile-targets org-refile-targets)
  10241. (org-refile-use-outline-path org-refile-use-outline-path)
  10242. excluded-entries)
  10243. (when (and (derived-mode-p 'org-mode)
  10244. (not org-refile-use-cache)
  10245. (not no-exclude))
  10246. (org-map-tree
  10247. (lambda()
  10248. (setq excluded-entries
  10249. (append excluded-entries (list (org-get-heading t t)))))))
  10250. (setq org-refile-target-table
  10251. (org-refile-get-targets default-buffer excluded-entries)))
  10252. (unless org-refile-target-table
  10253. (error "No refile targets"))
  10254. (let* ((cbuf (current-buffer))
  10255. (partial-completion-mode nil)
  10256. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10257. (cfunc (if (and org-refile-use-outline-path
  10258. org-outline-path-complete-in-steps)
  10259. 'org-olpath-completing-read
  10260. 'org-icompleting-read))
  10261. (extra (if org-refile-use-outline-path "/" ""))
  10262. (cbnex (concat (buffer-name) extra))
  10263. (filename (and cfn (expand-file-name cfn)))
  10264. (tbl (mapcar
  10265. (lambda (x)
  10266. (if (and (not (member org-refile-use-outline-path
  10267. '(file full-file-path)))
  10268. (not (equal filename (nth 1 x))))
  10269. (cons (concat (car x) extra " ("
  10270. (file-name-nondirectory (nth 1 x)) ")")
  10271. (cdr x))
  10272. (cons (concat (car x) extra) (cdr x))))
  10273. org-refile-target-table))
  10274. (completion-ignore-case t)
  10275. cdef
  10276. (prompt (concat prompt
  10277. (or (and (car org-refile-history)
  10278. (concat " (default " (car org-refile-history) ")"))
  10279. (and (assoc cbnex tbl) (setq cdef cbnex)
  10280. (concat " (default " cbnex ")"))) ": "))
  10281. pa answ parent-target child parent old-hist)
  10282. (setq old-hist org-refile-history)
  10283. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10284. nil 'org-refile-history (or cdef (car org-refile-history))))
  10285. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10286. (org-refile-check-position pa)
  10287. (if pa
  10288. (progn
  10289. (when (or (not org-refile-history)
  10290. (not (eq old-hist org-refile-history))
  10291. (not (equal (car pa) (car org-refile-history))))
  10292. (setq org-refile-history
  10293. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10294. org-refile-history
  10295. (cdr org-refile-history))))
  10296. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10297. (pop org-refile-history)))
  10298. pa)
  10299. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10300. (progn
  10301. (setq parent (match-string 1 answ)
  10302. child (match-string 2 answ))
  10303. (setq parent-target (or (assoc parent tbl)
  10304. (assoc (concat parent "/") tbl)))
  10305. (when (and parent-target
  10306. (or (eq new-nodes t)
  10307. (and (eq new-nodes 'confirm)
  10308. (y-or-n-p (format "Create new node \"%s\"? "
  10309. child)))))
  10310. (org-refile-new-child parent-target child)))
  10311. (error "Invalid target location")))))
  10312. (declare-function org-string-nw-p "org-macs" (s))
  10313. (defun org-refile-check-position (refile-pointer)
  10314. "Check if the refile pointer matches the headline to which it points."
  10315. (let* ((file (nth 1 refile-pointer))
  10316. (re (nth 2 refile-pointer))
  10317. (pos (nth 3 refile-pointer))
  10318. buffer)
  10319. (if (and (not (markerp pos)) (not file))
  10320. (error "Please save the buffer to a file before refiling")
  10321. (when (org-string-nw-p re)
  10322. (setq buffer (if (markerp pos)
  10323. (marker-buffer pos)
  10324. (or (find-buffer-visiting file)
  10325. (find-file-noselect file))))
  10326. (with-current-buffer buffer
  10327. (save-excursion
  10328. (save-restriction
  10329. (widen)
  10330. (goto-char pos)
  10331. (beginning-of-line 1)
  10332. (unless (org-looking-at-p re)
  10333. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10334. (defun org-refile-new-child (parent-target child)
  10335. "Use refile target PARENT-TARGET to add new CHILD below it."
  10336. (unless parent-target
  10337. (error "Cannot find parent for new node"))
  10338. (let ((file (nth 1 parent-target))
  10339. (pos (nth 3 parent-target))
  10340. level)
  10341. (with-current-buffer (or (find-buffer-visiting file)
  10342. (find-file-noselect file))
  10343. (save-excursion
  10344. (save-restriction
  10345. (widen)
  10346. (if pos
  10347. (goto-char pos)
  10348. (goto-char (point-max))
  10349. (if (not (bolp)) (newline)))
  10350. (when (looking-at org-outline-regexp)
  10351. (setq level (funcall outline-level))
  10352. (org-end-of-subtree t t))
  10353. (org-back-over-empty-lines)
  10354. (insert "\n" (make-string
  10355. (if pos (org-get-valid-level level 1) 1) ?*)
  10356. " " child "\n")
  10357. (beginning-of-line 0)
  10358. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10359. (defun org-olpath-completing-read (prompt collection &rest args)
  10360. "Read an outline path like a file name."
  10361. (let ((thetable collection)
  10362. (org-completion-use-ido nil) ; does not work with ido.
  10363. (org-completion-use-iswitchb nil)) ; or iswitchb
  10364. (apply
  10365. 'org-icompleting-read prompt
  10366. (lambda (string predicate &optional flag)
  10367. (let (rtn r f (l (length string)))
  10368. (cond
  10369. ((eq flag nil)
  10370. ;; try completion
  10371. (try-completion string thetable))
  10372. ((eq flag t)
  10373. ;; all-completions
  10374. (setq rtn (all-completions string thetable predicate))
  10375. (mapcar
  10376. (lambda (x)
  10377. (setq r (substring x l))
  10378. (if (string-match " ([^)]*)$" x)
  10379. (setq f (match-string 0 x))
  10380. (setq f ""))
  10381. (if (string-match "/" r)
  10382. (concat string (substring r 0 (match-end 0)) f)
  10383. x))
  10384. rtn))
  10385. ((eq flag 'lambda)
  10386. ;; exact match?
  10387. (assoc string thetable)))))
  10388. args)))
  10389. ;;;; Dynamic blocks
  10390. (defun org-find-dblock (name)
  10391. "Find the first dynamic block with name NAME in the buffer.
  10392. If not found, stay at current position and return nil."
  10393. (let ((case-fold-search t) pos)
  10394. (save-excursion
  10395. (goto-char (point-min))
  10396. (setq pos (and (re-search-forward
  10397. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10398. (match-beginning 0))))
  10399. (if pos (goto-char pos))
  10400. pos))
  10401. (defconst org-dblock-start-re
  10402. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10403. "Matches the start line of a dynamic block, with parameters.")
  10404. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10405. "Matches the end of a dynamic block.")
  10406. (defun org-create-dblock (plist)
  10407. "Create a dynamic block section, with parameters taken from PLIST.
  10408. PLIST must contain a :name entry which is used as name of the block."
  10409. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10410. (end-of-line 1)
  10411. (newline))
  10412. (let ((col (current-column))
  10413. (name (plist-get plist :name)))
  10414. (insert "#+BEGIN: " name)
  10415. (while plist
  10416. (if (eq (car plist) :name)
  10417. (setq plist (cddr plist))
  10418. (insert " " (prin1-to-string (pop plist)))))
  10419. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10420. (beginning-of-line -2)))
  10421. (defun org-prepare-dblock ()
  10422. "Prepare dynamic block for refresh.
  10423. This empties the block, puts the cursor at the insert position and returns
  10424. the property list including an extra property :name with the block name."
  10425. (unless (looking-at org-dblock-start-re)
  10426. (error "Not at a dynamic block"))
  10427. (let* ((begdel (1+ (match-end 0)))
  10428. (name (org-no-properties (match-string 1)))
  10429. (params (append (list :name name)
  10430. (read (concat "(" (match-string 3) ")")))))
  10431. (save-excursion
  10432. (beginning-of-line 1)
  10433. (skip-chars-forward " \t")
  10434. (setq params (plist-put params :indentation-column (current-column))))
  10435. (unless (re-search-forward org-dblock-end-re nil t)
  10436. (error "Dynamic block not terminated"))
  10437. (setq params
  10438. (append params
  10439. (list :content (buffer-substring
  10440. begdel (match-beginning 0)))))
  10441. (delete-region begdel (match-beginning 0))
  10442. (goto-char begdel)
  10443. (open-line 1)
  10444. params))
  10445. (defun org-map-dblocks (&optional command)
  10446. "Apply COMMAND to all dynamic blocks in the current buffer.
  10447. If COMMAND is not given, use `org-update-dblock'."
  10448. (let ((cmd (or command 'org-update-dblock)))
  10449. (save-excursion
  10450. (goto-char (point-min))
  10451. (while (re-search-forward org-dblock-start-re nil t)
  10452. (goto-char (match-beginning 0))
  10453. (save-excursion
  10454. (condition-case nil
  10455. (funcall cmd)
  10456. (error (message "Error during update of dynamic block"))))
  10457. (unless (re-search-forward org-dblock-end-re nil t)
  10458. (error "Dynamic block not terminated"))))))
  10459. (defun org-dblock-update (&optional arg)
  10460. "User command for updating dynamic blocks.
  10461. Update the dynamic block at point. With prefix ARG, update all dynamic
  10462. blocks in the buffer."
  10463. (interactive "P")
  10464. (if arg
  10465. (org-update-all-dblocks)
  10466. (or (looking-at org-dblock-start-re)
  10467. (org-beginning-of-dblock))
  10468. (org-update-dblock)))
  10469. (defun org-update-dblock ()
  10470. "Update the dynamic block at point.
  10471. This means to empty the block, parse for parameters and then call
  10472. the correct writing function."
  10473. (interactive)
  10474. (save-window-excursion
  10475. (let* ((pos (point))
  10476. (line (org-current-line))
  10477. (params (org-prepare-dblock))
  10478. (name (plist-get params :name))
  10479. (indent (plist-get params :indentation-column))
  10480. (cmd (intern (concat "org-dblock-write:" name))))
  10481. (message "Updating dynamic block `%s' at line %d..." name line)
  10482. (funcall cmd params)
  10483. (message "Updating dynamic block `%s' at line %d...done" name line)
  10484. (goto-char pos)
  10485. (when (and indent (> indent 0))
  10486. (setq indent (make-string indent ?\ ))
  10487. (save-excursion
  10488. (org-beginning-of-dblock)
  10489. (forward-line 1)
  10490. (while (not (looking-at org-dblock-end-re))
  10491. (insert indent)
  10492. (beginning-of-line 2))
  10493. (when (looking-at org-dblock-end-re)
  10494. (and (looking-at "[ \t]+")
  10495. (replace-match ""))
  10496. (insert indent)))))))
  10497. (defun org-beginning-of-dblock ()
  10498. "Find the beginning of the dynamic block at point.
  10499. Error if there is no such block at point."
  10500. (let ((pos (point))
  10501. beg)
  10502. (end-of-line 1)
  10503. (if (and (re-search-backward org-dblock-start-re nil t)
  10504. (setq beg (match-beginning 0))
  10505. (re-search-forward org-dblock-end-re nil t)
  10506. (> (match-end 0) pos))
  10507. (goto-char beg)
  10508. (goto-char pos)
  10509. (error "Not in a dynamic block"))))
  10510. (defun org-update-all-dblocks ()
  10511. "Update all dynamic blocks in the buffer.
  10512. This function can be used in a hook."
  10513. (interactive)
  10514. (when (derived-mode-p 'org-mode)
  10515. (org-map-dblocks 'org-update-dblock)))
  10516. ;;;; Completion
  10517. (defun org-get-export-keywords ()
  10518. "Return a list of all currently understood export keywords.
  10519. Export keywords include options, block names, attributes and
  10520. keywords relative to each registered export back-end."
  10521. (delq nil
  10522. (let (keywords)
  10523. (mapc
  10524. (lambda (back-end)
  10525. (let ((props (cdr back-end)))
  10526. ;; Back-end name (for keywords, like #+LATEX:)
  10527. (push (upcase (symbol-name (car back-end))) keywords)
  10528. ;; Back-end options.
  10529. (mapc (lambda (option) (push (cadr option) keywords))
  10530. (plist-get (cdr back-end) :options-alist))))
  10531. (org-bound-and-true-p org-export-registered-backends))
  10532. keywords)))
  10533. (defconst org-options-keywords
  10534. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10535. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10536. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10537. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10538. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:" "INFOJS_OPT:"))
  10539. (defcustom org-structure-template-alist
  10540. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10541. "<src lang=\"?\">\n\n</src>")
  10542. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10543. "<example>\n?\n</example>")
  10544. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10545. "<quote>\n?\n</quote>")
  10546. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10547. "<verse>\n?\n</verse>")
  10548. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10549. "<verbatim>\n?\n</verbatim>")
  10550. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10551. "<center>\n?\n</center>")
  10552. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10553. "<literal style=\"latex\">\n?\n</literal>")
  10554. ("L" "#+LaTeX: "
  10555. "<literal style=\"latex\">?</literal>")
  10556. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10557. "<literal style=\"html\">\n?\n</literal>")
  10558. ("H" "#+HTML: "
  10559. "<literal style=\"html\">?</literal>")
  10560. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10561. ("A" "#+ASCII: ")
  10562. ("i" "#+INDEX: ?"
  10563. "#+INDEX: ?")
  10564. ("I" "#+INCLUDE: %file ?"
  10565. "<include file=%file markup=\"?\">"))
  10566. "Structure completion elements.
  10567. This is a list of abbreviation keys and values. The value gets inserted
  10568. if you type `<' followed by the key and then press the completion key,
  10569. usually `M-TAB'. %file will be replaced by a file name after prompting
  10570. for the file using completion. The cursor will be placed at the position
  10571. of the `?` in the template.
  10572. There are two templates for each key, the first uses the original Org syntax,
  10573. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10574. the default when the /org-mtags.el/ module has been loaded. See also the
  10575. variable `org-mtags-prefer-muse-templates'."
  10576. :group 'org-completion
  10577. :type '(repeat
  10578. (string :tag "Key")
  10579. (string :tag "Template")
  10580. (string :tag "Muse Template")))
  10581. (defun org-try-structure-completion ()
  10582. "Try to complete a structure template before point.
  10583. This looks for strings like \"<e\" on an otherwise empty line and
  10584. expands them."
  10585. (let ((l (buffer-substring (point-at-bol) (point)))
  10586. a)
  10587. (when (and (looking-at "[ \t]*$")
  10588. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10589. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10590. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10591. (match-beginning 1)) a)
  10592. t)))
  10593. (defun org-complete-expand-structure-template (start cell)
  10594. "Expand a structure template."
  10595. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10596. (rpl (nth (if musep 2 1) cell))
  10597. (ind ""))
  10598. (delete-region start (point))
  10599. (when (string-match "\\`#\\+" rpl)
  10600. (cond
  10601. ((bolp))
  10602. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10603. (setq ind (buffer-substring (point-at-bol) (point))))
  10604. (t (newline))))
  10605. (setq start (point))
  10606. (if (string-match "%file" rpl)
  10607. (setq rpl (replace-match
  10608. (concat
  10609. "\""
  10610. (save-match-data
  10611. (abbreviate-file-name (read-file-name "Include file: ")))
  10612. "\"")
  10613. t t rpl)))
  10614. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10615. (concat "\n" ind)))
  10616. (insert rpl)
  10617. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10618. ;;;; TODO, DEADLINE, Comments
  10619. (defun org-toggle-comment ()
  10620. "Change the COMMENT state of an entry."
  10621. (interactive)
  10622. (save-excursion
  10623. (org-back-to-heading)
  10624. (let (case-fold-search)
  10625. (cond
  10626. ((looking-at (format org-heading-keyword-regexp-format
  10627. org-comment-string))
  10628. (goto-char (match-end 1))
  10629. (looking-at (concat " +" org-comment-string))
  10630. (replace-match "" t t)
  10631. (when (eolp) (insert " ")))
  10632. ((looking-at org-outline-regexp)
  10633. (goto-char (match-end 0))
  10634. (insert org-comment-string " "))))))
  10635. (defvar org-last-todo-state-is-todo nil
  10636. "This is non-nil when the last TODO state change led to a TODO state.
  10637. If the last change removed the TODO tag or switched to DONE, then
  10638. this is nil.")
  10639. (defvar org-setting-tags nil) ; dynamically skipped
  10640. (defvar org-todo-setup-filter-hook nil
  10641. "Hook for functions that pre-filter todo specs.
  10642. Each function takes a todo spec and returns either nil or the spec
  10643. transformed into canonical form." )
  10644. (defvar org-todo-get-default-hook nil
  10645. "Hook for functions that get a default item for todo.
  10646. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10647. nil or a string to be used for the todo mark." )
  10648. (defvar org-agenda-headline-snapshot-before-repeat)
  10649. (defun org-current-effective-time ()
  10650. "Return current time adjusted for `org-extend-today-until' variable."
  10651. (let* ((ct (org-current-time))
  10652. (dct (decode-time ct))
  10653. (ct1
  10654. (cond
  10655. (org-use-last-clock-out-time-as-effective-time
  10656. (or (org-clock-get-last-clock-out-time) ct))
  10657. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10658. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10659. (t ct))))
  10660. ct1))
  10661. (defun org-todo-yesterday (&optional arg)
  10662. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10663. (interactive "P")
  10664. (if (eq major-mode 'org-agenda-mode)
  10665. (apply 'org-agenda-todo-yesterday arg)
  10666. (let* ((hour (third (decode-time
  10667. (org-current-time))))
  10668. (org-extend-today-until (1+ hour)))
  10669. (org-todo arg))))
  10670. (defvar org-block-entry-blocking ""
  10671. "First entry preventing the TODO state change.")
  10672. (defun org-todo (&optional arg)
  10673. "Change the TODO state of an item.
  10674. The state of an item is given by a keyword at the start of the heading,
  10675. like
  10676. *** TODO Write paper
  10677. *** DONE Call mom
  10678. The different keywords are specified in the variable `org-todo-keywords'.
  10679. By default the available states are \"TODO\" and \"DONE\".
  10680. So for this example: when the item starts with TODO, it is changed to DONE.
  10681. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10682. DONE are present, add TODO at the beginning of the heading.
  10683. With \\[universal-argument] prefix arg, use completion to determine the new \
  10684. state.
  10685. With numeric prefix arg, switch to that state.
  10686. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10687. keywords (nextset).
  10688. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10689. With a numeric prefix arg of 0, inhibit note taking for the change.
  10690. For calling through lisp, arg is also interpreted in the following way:
  10691. 'none -> empty state
  10692. \"\"(empty string) -> switch to empty state
  10693. 'done -> switch to DONE
  10694. 'nextset -> switch to the next set of keywords
  10695. 'previousset -> switch to the previous set of keywords
  10696. \"WAITING\" -> switch to the specified keyword, but only if it
  10697. really is a member of `org-todo-keywords'."
  10698. (interactive "P")
  10699. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10700. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10701. 'region-start-level 'region))
  10702. org-loop-over-headlines-in-active-region)
  10703. (org-map-entries
  10704. `(org-todo ,arg)
  10705. org-loop-over-headlines-in-active-region
  10706. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10707. (if (equal arg '(16)) (setq arg 'nextset))
  10708. (let ((org-blocker-hook org-blocker-hook)
  10709. commentp
  10710. case-fold-search)
  10711. (when (equal arg '(64))
  10712. (setq arg nil org-blocker-hook nil))
  10713. (when (and org-blocker-hook
  10714. (or org-inhibit-blocking
  10715. (org-entry-get nil "NOBLOCKING")))
  10716. (setq org-blocker-hook nil))
  10717. (save-excursion
  10718. (catch 'exit
  10719. (org-back-to-heading t)
  10720. (when (looking-at (concat "^\\*+ " org-comment-string))
  10721. (org-toggle-comment)
  10722. (setq commentp t))
  10723. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10724. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10725. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10726. (let* ((match-data (match-data))
  10727. (startpos (point-at-bol))
  10728. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10729. (org-log-done org-log-done)
  10730. (org-log-repeat org-log-repeat)
  10731. (org-todo-log-states org-todo-log-states)
  10732. (org-inhibit-logging
  10733. (if (equal arg 0)
  10734. (progn (setq arg nil) 'note) org-inhibit-logging))
  10735. (this (match-string 1))
  10736. (hl-pos (match-beginning 0))
  10737. (head (org-get-todo-sequence-head this))
  10738. (ass (assoc head org-todo-kwd-alist))
  10739. (interpret (nth 1 ass))
  10740. (done-word (nth 3 ass))
  10741. (final-done-word (nth 4 ass))
  10742. (org-last-state (or this ""))
  10743. (completion-ignore-case t)
  10744. (member (member this org-todo-keywords-1))
  10745. (tail (cdr member))
  10746. (org-state (cond
  10747. ((and org-todo-key-trigger
  10748. (or (and (equal arg '(4))
  10749. (eq org-use-fast-todo-selection 'prefix))
  10750. (and (not arg) org-use-fast-todo-selection
  10751. (not (eq org-use-fast-todo-selection
  10752. 'prefix)))))
  10753. ;; Use fast selection
  10754. (org-fast-todo-selection))
  10755. ((and (equal arg '(4))
  10756. (or (not org-use-fast-todo-selection)
  10757. (not org-todo-key-trigger)))
  10758. ;; Read a state with completion
  10759. (org-icompleting-read
  10760. "State: " (mapcar (lambda(x) (list x))
  10761. org-todo-keywords-1)
  10762. nil t))
  10763. ((eq arg 'right)
  10764. (if this
  10765. (if tail (car tail) nil)
  10766. (car org-todo-keywords-1)))
  10767. ((eq arg 'left)
  10768. (if (equal member org-todo-keywords-1)
  10769. nil
  10770. (if this
  10771. (nth (- (length org-todo-keywords-1)
  10772. (length tail) 2)
  10773. org-todo-keywords-1)
  10774. (org-last org-todo-keywords-1))))
  10775. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10776. (setq arg nil))) ; hack to fall back to cycling
  10777. (arg
  10778. ;; user or caller requests a specific state
  10779. (cond
  10780. ((equal arg "") nil)
  10781. ((eq arg 'none) nil)
  10782. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10783. ((eq arg 'nextset)
  10784. (or (car (cdr (member head org-todo-heads)))
  10785. (car org-todo-heads)))
  10786. ((eq arg 'previousset)
  10787. (let ((org-todo-heads (reverse org-todo-heads)))
  10788. (or (car (cdr (member head org-todo-heads)))
  10789. (car org-todo-heads))))
  10790. ((car (member arg org-todo-keywords-1)))
  10791. ((stringp arg)
  10792. (error "State `%s' not valid in this file" arg))
  10793. ((nth (1- (prefix-numeric-value arg))
  10794. org-todo-keywords-1))))
  10795. ((null member) (or head (car org-todo-keywords-1)))
  10796. ((equal this final-done-word) nil) ;; -> make empty
  10797. ((null tail) nil) ;; -> first entry
  10798. ((memq interpret '(type priority))
  10799. (if (eq this-command last-command)
  10800. (car tail)
  10801. (if (> (length tail) 0)
  10802. (or done-word (car org-done-keywords))
  10803. nil)))
  10804. (t
  10805. (car tail))))
  10806. (org-state (or
  10807. (run-hook-with-args-until-success
  10808. 'org-todo-get-default-hook org-state org-last-state)
  10809. org-state))
  10810. (next (if org-state (concat " " org-state " ") " "))
  10811. (change-plist (list :type 'todo-state-change :from this :to org-state
  10812. :position startpos))
  10813. dolog now-done-p)
  10814. (when org-blocker-hook
  10815. (setq org-last-todo-state-is-todo
  10816. (not (member this org-done-keywords)))
  10817. (unless (save-excursion
  10818. (save-match-data
  10819. (org-with-wide-buffer
  10820. (run-hook-with-args-until-failure
  10821. 'org-blocker-hook change-plist))))
  10822. (if (org-called-interactively-p 'interactive)
  10823. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  10824. this org-state org-block-entry-blocking)
  10825. ;; fail silently
  10826. (message "TODO state change from %s to %s blocked (by \"%s\")"
  10827. this org-state org-block-entry-blocking)
  10828. (throw 'exit nil))))
  10829. (store-match-data match-data)
  10830. (replace-match next t t)
  10831. (unless (pos-visible-in-window-p hl-pos)
  10832. (message "TODO state changed to %s" (org-trim next)))
  10833. (unless head
  10834. (setq head (org-get-todo-sequence-head org-state)
  10835. ass (assoc head org-todo-kwd-alist)
  10836. interpret (nth 1 ass)
  10837. done-word (nth 3 ass)
  10838. final-done-word (nth 4 ass)))
  10839. (when (memq arg '(nextset previousset))
  10840. (message "Keyword-Set %d/%d: %s"
  10841. (- (length org-todo-sets) -1
  10842. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10843. (length org-todo-sets)
  10844. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10845. (setq org-last-todo-state-is-todo
  10846. (not (member org-state org-done-keywords)))
  10847. (setq now-done-p (and (member org-state org-done-keywords)
  10848. (not (member this org-done-keywords))))
  10849. (and logging (org-local-logging logging))
  10850. (when (and (or org-todo-log-states org-log-done)
  10851. (not (eq org-inhibit-logging t))
  10852. (not (memq arg '(nextset previousset))))
  10853. ;; we need to look at recording a time and note
  10854. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10855. (nth 2 (assoc this org-todo-log-states))))
  10856. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10857. (setq dolog 'time))
  10858. (when (and org-state
  10859. (member org-state org-not-done-keywords)
  10860. (not (member this org-not-done-keywords)))
  10861. ;; This is now a todo state and was not one before
  10862. ;; If there was a CLOSED time stamp, get rid of it.
  10863. (org-add-planning-info nil nil 'closed))
  10864. (when (and now-done-p org-log-done)
  10865. ;; It is now done, and it was not done before
  10866. (org-add-planning-info 'closed (org-current-effective-time))
  10867. (if (and (not dolog) (eq 'note org-log-done))
  10868. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10869. (when (and org-state dolog)
  10870. ;; This is a non-nil state, and we need to log it
  10871. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10872. ;; Fixup tag positioning
  10873. (org-todo-trigger-tag-changes org-state)
  10874. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10875. (when org-provide-todo-statistics
  10876. (org-update-parent-todo-statistics))
  10877. (run-hooks 'org-after-todo-state-change-hook)
  10878. (if (and arg (not (member org-state org-done-keywords)))
  10879. (setq head (org-get-todo-sequence-head org-state)))
  10880. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10881. ;; Do we need to trigger a repeat?
  10882. (when now-done-p
  10883. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10884. ;; This is for the agenda, take a snapshot of the headline.
  10885. (save-match-data
  10886. (setq org-agenda-headline-snapshot-before-repeat
  10887. (org-get-heading))))
  10888. (org-auto-repeat-maybe org-state))
  10889. ;; Fixup cursor location if close to the keyword
  10890. (if (and (outline-on-heading-p)
  10891. (not (bolp))
  10892. (save-excursion (beginning-of-line 1)
  10893. (looking-at org-todo-line-regexp))
  10894. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10895. (progn
  10896. (goto-char (or (match-end 2) (match-end 1)))
  10897. (and (looking-at " ") (just-one-space))))
  10898. (when org-trigger-hook
  10899. (save-excursion
  10900. (run-hook-with-args 'org-trigger-hook change-plist)))
  10901. (when commentp (org-toggle-comment))))))))
  10902. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10903. "Block turning an entry into a TODO, using the hierarchy.
  10904. This checks whether the current task should be blocked from state
  10905. changes. Such blocking occurs when:
  10906. 1. The task has children which are not all in a completed state.
  10907. 2. A task has a parent with the property :ORDERED:, and there
  10908. are siblings prior to the current task with incomplete
  10909. status.
  10910. 3. The parent of the task is blocked because it has siblings that should
  10911. be done first, or is child of a block grandparent TODO entry."
  10912. (if (not org-enforce-todo-dependencies)
  10913. t ; if locally turned off don't block
  10914. (catch 'dont-block
  10915. ;; If this is not a todo state change, or if this entry is already DONE,
  10916. ;; do not block
  10917. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10918. (member (plist-get change-plist :from)
  10919. (cons 'done org-done-keywords))
  10920. (member (plist-get change-plist :to)
  10921. (cons 'todo org-not-done-keywords))
  10922. (not (plist-get change-plist :to)))
  10923. (throw 'dont-block t))
  10924. ;; If this task has children, and any are undone, it's blocked
  10925. (save-excursion
  10926. (org-back-to-heading t)
  10927. (let ((this-level (funcall outline-level)))
  10928. (outline-next-heading)
  10929. (let ((child-level (funcall outline-level)))
  10930. (while (and (not (eobp))
  10931. (> child-level this-level))
  10932. ;; this todo has children, check whether they are all
  10933. ;; completed
  10934. (if (and (not (org-entry-is-done-p))
  10935. (org-entry-is-todo-p))
  10936. (progn (setq org-block-entry-blocking (org-get-heading))
  10937. (throw 'dont-block nil)))
  10938. (outline-next-heading)
  10939. (setq child-level (funcall outline-level))))))
  10940. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10941. ;; any previous siblings are undone, it's blocked
  10942. (save-excursion
  10943. (org-back-to-heading t)
  10944. (let* ((pos (point))
  10945. (parent-pos (and (org-up-heading-safe) (point))))
  10946. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10947. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10948. (forward-line 1)
  10949. (re-search-forward org-not-done-heading-regexp pos t))
  10950. (setq org-block-entry-blocking (match-string 0))
  10951. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10952. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10953. (while t
  10954. (goto-char parent-pos)
  10955. (if (not (looking-at org-not-done-heading-regexp))
  10956. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10957. (setq pos (point))
  10958. (setq parent-pos (and (org-up-heading-safe) (point)))
  10959. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10960. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10961. (forward-line 1)
  10962. (re-search-forward org-not-done-heading-regexp pos t)
  10963. (setq org-block-entry-blocking (org-get-heading)))
  10964. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10965. (defcustom org-track-ordered-property-with-tag nil
  10966. "Should the ORDERED property also be shown as a tag?
  10967. The ORDERED property decides if an entry should require subtasks to be
  10968. completed in sequence. Since a property is not very visible, setting
  10969. this option means that toggling the ORDERED property with the command
  10970. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10971. not relevant for the behavior, but it makes things more visible.
  10972. Note that toggling the tag with tags commands will not change the property
  10973. and therefore not influence behavior!
  10974. This can be t, meaning the tag ORDERED should be used, It can also be a
  10975. string to select a different tag for this task."
  10976. :group 'org-todo
  10977. :type '(choice
  10978. (const :tag "No tracking" nil)
  10979. (const :tag "Track with ORDERED tag" t)
  10980. (string :tag "Use other tag")))
  10981. (defun org-toggle-ordered-property ()
  10982. "Toggle the ORDERED property of the current entry.
  10983. For better visibility, you can track the value of this property with a tag.
  10984. See variable `org-track-ordered-property-with-tag'."
  10985. (interactive)
  10986. (let* ((t1 org-track-ordered-property-with-tag)
  10987. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10988. (save-excursion
  10989. (org-back-to-heading)
  10990. (if (org-entry-get nil "ORDERED")
  10991. (progn
  10992. (org-delete-property "ORDERED" "PROPERTIES")
  10993. (and tag (org-toggle-tag tag 'off))
  10994. (message "Subtasks can be completed in arbitrary order"))
  10995. (org-entry-put nil "ORDERED" "t")
  10996. (and tag (org-toggle-tag tag 'on))
  10997. (message "Subtasks must be completed in sequence")))))
  10998. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10999. (defun org-block-todo-from-checkboxes (change-plist)
  11000. "Block turning an entry into a TODO, using checkboxes.
  11001. This checks whether the current task should be blocked from state
  11002. changes because there are unchecked boxes in this entry."
  11003. (if (not org-enforce-todo-checkbox-dependencies)
  11004. t ; if locally turned off don't block
  11005. (catch 'dont-block
  11006. ;; If this is not a todo state change, or if this entry is already DONE,
  11007. ;; do not block
  11008. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11009. (member (plist-get change-plist :from)
  11010. (cons 'done org-done-keywords))
  11011. (member (plist-get change-plist :to)
  11012. (cons 'todo org-not-done-keywords))
  11013. (not (plist-get change-plist :to)))
  11014. (throw 'dont-block t))
  11015. ;; If this task has checkboxes that are not checked, it's blocked
  11016. (save-excursion
  11017. (org-back-to-heading t)
  11018. (let ((beg (point)) end)
  11019. (outline-next-heading)
  11020. (setq end (point))
  11021. (goto-char beg)
  11022. (if (org-list-search-forward
  11023. (concat (org-item-beginning-re)
  11024. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11025. "\\[[- ]\\]")
  11026. end t)
  11027. (progn
  11028. (if (boundp 'org-blocked-by-checkboxes)
  11029. (setq org-blocked-by-checkboxes t))
  11030. (throw 'dont-block nil)))))
  11031. t))) ; do not block
  11032. (defun org-entry-blocked-p ()
  11033. "Is the current entry blocked?"
  11034. (with-silent-modifications
  11035. (if (org-entry-get nil "NOBLOCKING")
  11036. nil ;; Never block this entry
  11037. (not (run-hook-with-args-until-failure
  11038. 'org-blocker-hook
  11039. (list :type 'todo-state-change
  11040. :position (point)
  11041. :from 'todo
  11042. :to 'done))))))
  11043. (defun org-update-statistics-cookies (all)
  11044. "Update the statistics cookie, either from TODO or from checkboxes.
  11045. This should be called with the cursor in a line with a statistics cookie."
  11046. (interactive "P")
  11047. (if all
  11048. (progn
  11049. (org-update-checkbox-count 'all)
  11050. (org-map-entries 'org-update-parent-todo-statistics))
  11051. (if (not (org-at-heading-p))
  11052. (org-update-checkbox-count)
  11053. (let ((pos (point-marker))
  11054. end l1 l2)
  11055. (ignore-errors (org-back-to-heading t))
  11056. (if (not (org-at-heading-p))
  11057. (org-update-checkbox-count)
  11058. (setq l1 (org-outline-level))
  11059. (setq end (save-excursion
  11060. (outline-next-heading)
  11061. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11062. (point)))
  11063. (if (and (save-excursion
  11064. (re-search-forward
  11065. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11066. (not (save-excursion (re-search-forward
  11067. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11068. (org-update-checkbox-count)
  11069. (if (and l2 (> l2 l1))
  11070. (progn
  11071. (goto-char end)
  11072. (org-update-parent-todo-statistics))
  11073. (goto-char pos)
  11074. (beginning-of-line 1)
  11075. (while (re-search-forward
  11076. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11077. (point-at-eol) t)
  11078. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11079. (goto-char pos)
  11080. (move-marker pos nil)))))
  11081. (defvar org-entry-property-inherited-from) ;; defined below
  11082. (defun org-update-parent-todo-statistics ()
  11083. "Update any statistics cookie in the parent of the current headline.
  11084. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11085. the entire subtree and this will travel up the hierarchy and update
  11086. statistics everywhere."
  11087. (let* ((prop (save-excursion (org-up-heading-safe)
  11088. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11089. (recursive (or (not org-hierarchical-todo-statistics)
  11090. (and prop (string-match "\\<recursive\\>" prop))))
  11091. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11092. 0))
  11093. (first t)
  11094. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11095. level ltoggle l1 new ndel
  11096. (cnt-all 0) (cnt-done 0) is-percent kwd
  11097. checkbox-beg ov ovs ove cookie-present)
  11098. (catch 'exit
  11099. (save-excursion
  11100. (beginning-of-line 1)
  11101. (setq ltoggle (funcall outline-level))
  11102. ;; Three situations are to consider:
  11103. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11104. ;; to the top-level ancestor on the headline;
  11105. ;; 2. If parent has "recursive" property, repeat up to the
  11106. ;; headline setting that property, taking inheritance into
  11107. ;; account;
  11108. ;; 3. Else, move up to direct parent and proceed only once.
  11109. (while (and (setq level (org-up-heading-safe))
  11110. (or recursive first)
  11111. (>= (point) lim))
  11112. (setq first nil cookie-present nil)
  11113. (unless (and level
  11114. (not (string-match
  11115. "\\<checkbox\\>"
  11116. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11117. "")))))
  11118. (throw 'exit nil))
  11119. (while (re-search-forward box-re (point-at-eol) t)
  11120. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11121. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11122. (save-match-data
  11123. (unless (outline-next-heading) (throw 'exit nil))
  11124. (while (and (looking-at org-complex-heading-regexp)
  11125. (> (setq l1 (length (match-string 1))) level))
  11126. (setq kwd (and (or recursive (= l1 ltoggle))
  11127. (match-string 2)))
  11128. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11129. (and (listp org-provide-todo-statistics)
  11130. (or (member kwd org-provide-todo-statistics)
  11131. (member kwd org-done-keywords))))
  11132. (setq cnt-all (1+ cnt-all))
  11133. (if (eq org-provide-todo-statistics t)
  11134. (and kwd (setq cnt-all (1+ cnt-all)))))
  11135. (and (member kwd org-done-keywords)
  11136. (setq cnt-done (1+ cnt-done)))
  11137. (outline-next-heading)))
  11138. (setq new
  11139. (if is-percent
  11140. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11141. (format "[%d/%d]" cnt-done cnt-all))
  11142. ndel (- (match-end 0) checkbox-beg))
  11143. ;; handle overlays when updating cookie from column view
  11144. (when (setq ov (car (overlays-at checkbox-beg)))
  11145. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11146. (delete-overlay ov))
  11147. (goto-char checkbox-beg)
  11148. (insert new)
  11149. (delete-region (point) (+ (point) ndel))
  11150. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11151. (when ov (move-overlay ov ovs ove)))
  11152. (when cookie-present
  11153. (run-hook-with-args 'org-after-todo-statistics-hook
  11154. cnt-done (- cnt-all cnt-done))))))
  11155. (run-hooks 'org-todo-statistics-hook)))
  11156. (defvar org-after-todo-statistics-hook nil
  11157. "Hook that is called after a TODO statistics cookie has been updated.
  11158. Each function is called with two arguments: the number of not-done entries
  11159. and the number of done entries.
  11160. For example, the following function, when added to this hook, will switch
  11161. an entry to DONE when all children are done, and back to TODO when new
  11162. entries are set to a TODO status. Note that this hook is only called
  11163. when there is a statistics cookie in the headline!
  11164. (defun org-summary-todo (n-done n-not-done)
  11165. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11166. (let (org-log-done org-log-states) ; turn off logging
  11167. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11168. ")
  11169. (defvar org-todo-statistics-hook nil
  11170. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11171. This hook runs even if there is no statistics cookie present, in which case
  11172. `org-after-todo-statistics-hook' would not run.")
  11173. (defun org-todo-trigger-tag-changes (state)
  11174. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11175. (let ((l org-todo-state-tags-triggers)
  11176. changes)
  11177. (when (or (not state) (equal state ""))
  11178. (setq changes (append changes (cdr (assoc "" l)))))
  11179. (when (and (stringp state) (> (length state) 0))
  11180. (setq changes (append changes (cdr (assoc state l)))))
  11181. (when (member state org-not-done-keywords)
  11182. (setq changes (append changes (cdr (assoc 'todo l)))))
  11183. (when (member state org-done-keywords)
  11184. (setq changes (append changes (cdr (assoc 'done l)))))
  11185. (dolist (c changes)
  11186. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11187. (defun org-local-logging (value)
  11188. "Get logging settings from a property VALUE."
  11189. (let* (words w a)
  11190. ;; directly set the variables, they are already local.
  11191. (setq org-log-done nil
  11192. org-log-repeat nil
  11193. org-todo-log-states nil)
  11194. (setq words (org-split-string value))
  11195. (while (setq w (pop words))
  11196. (cond
  11197. ((setq a (assoc w org-startup-options))
  11198. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11199. (set (nth 1 a) (nth 2 a))))
  11200. ((setq a (org-extract-log-state-settings w))
  11201. (and (member (car a) org-todo-keywords-1)
  11202. (push a org-todo-log-states)))))))
  11203. (defun org-get-todo-sequence-head (kwd)
  11204. "Return the head of the TODO sequence to which KWD belongs.
  11205. If KWD is not set, check if there is a text property remembering the
  11206. right sequence."
  11207. (let (p)
  11208. (cond
  11209. ((not kwd)
  11210. (or (get-text-property (point-at-bol) 'org-todo-head)
  11211. (progn
  11212. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11213. nil (point-at-eol)))
  11214. (get-text-property p 'org-todo-head))))
  11215. ((not (member kwd org-todo-keywords-1))
  11216. (car org-todo-keywords-1))
  11217. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11218. (defun org-fast-todo-selection ()
  11219. "Fast TODO keyword selection with single keys.
  11220. Returns the new TODO keyword, or nil if no state change should occur."
  11221. (let* ((fulltable org-todo-key-alist)
  11222. (done-keywords org-done-keywords) ;; needed for the faces.
  11223. (maxlen (apply 'max (mapcar
  11224. (lambda (x)
  11225. (if (stringp (car x)) (string-width (car x)) 0))
  11226. fulltable)))
  11227. (expert nil)
  11228. (fwidth (+ maxlen 3 1 3))
  11229. (ncol (/ (- (window-width) 4) fwidth))
  11230. tg cnt e c tbl
  11231. groups ingroup)
  11232. (save-excursion
  11233. (save-window-excursion
  11234. (if expert
  11235. (set-buffer (get-buffer-create " *Org todo*"))
  11236. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11237. (erase-buffer)
  11238. (org-set-local 'org-done-keywords done-keywords)
  11239. (setq tbl fulltable cnt 0)
  11240. (while (setq e (pop tbl))
  11241. (cond
  11242. ((equal e '(:startgroup))
  11243. (push '() groups) (setq ingroup t)
  11244. (when (not (= cnt 0))
  11245. (setq cnt 0)
  11246. (insert "\n"))
  11247. (insert "{ "))
  11248. ((equal e '(:endgroup))
  11249. (setq ingroup nil cnt 0)
  11250. (insert "}\n"))
  11251. ((equal e '(:newline))
  11252. (when (not (= cnt 0))
  11253. (setq cnt 0)
  11254. (insert "\n")
  11255. (setq e (car tbl))
  11256. (while (equal (car tbl) '(:newline))
  11257. (insert "\n")
  11258. (setq tbl (cdr tbl)))))
  11259. (t
  11260. (setq tg (car e) c (cdr e))
  11261. (if ingroup (push tg (car groups)))
  11262. (setq tg (org-add-props tg nil 'face
  11263. (org-get-todo-face tg)))
  11264. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11265. (insert "[" c "] " tg (make-string
  11266. (- fwidth 4 (length tg)) ?\ ))
  11267. (when (= (setq cnt (1+ cnt)) ncol)
  11268. (insert "\n")
  11269. (if ingroup (insert " "))
  11270. (setq cnt 0)))))
  11271. (insert "\n")
  11272. (goto-char (point-min))
  11273. (if (not expert) (org-fit-window-to-buffer))
  11274. (message "[a-z..]:Set [SPC]:clear")
  11275. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11276. (cond
  11277. ((or (= c ?\C-g)
  11278. (and (= c ?q) (not (rassoc c fulltable))))
  11279. (setq quit-flag t))
  11280. ((= c ?\ ) nil)
  11281. ((setq e (rassoc c fulltable) tg (car e))
  11282. tg)
  11283. (t (setq quit-flag t)))))))
  11284. (defun org-entry-is-todo-p ()
  11285. (member (org-get-todo-state) org-not-done-keywords))
  11286. (defun org-entry-is-done-p ()
  11287. (member (org-get-todo-state) org-done-keywords))
  11288. (defun org-get-todo-state ()
  11289. (save-excursion
  11290. (org-back-to-heading t)
  11291. (and (looking-at org-todo-line-regexp)
  11292. (match-end 2)
  11293. (match-string 2))))
  11294. (defun org-at-date-range-p (&optional inactive-ok)
  11295. "Is the cursor inside a date range?"
  11296. (interactive)
  11297. (save-excursion
  11298. (catch 'exit
  11299. (let ((pos (point)))
  11300. (skip-chars-backward "^[<\r\n")
  11301. (skip-chars-backward "<[")
  11302. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11303. (>= (match-end 0) pos)
  11304. (throw 'exit t))
  11305. (skip-chars-backward "^<[\r\n")
  11306. (skip-chars-backward "<[")
  11307. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11308. (>= (match-end 0) pos)
  11309. (throw 'exit t)))
  11310. nil)))
  11311. (defun org-get-repeat (&optional tagline)
  11312. "Check if there is a deadline/schedule with repeater in this entry."
  11313. (save-match-data
  11314. (save-excursion
  11315. (org-back-to-heading t)
  11316. (and (re-search-forward (if tagline
  11317. (concat tagline "\\s-*" org-repeat-re)
  11318. org-repeat-re)
  11319. (org-entry-end-position) t)
  11320. (match-string-no-properties 1)))))
  11321. (defvar org-last-changed-timestamp)
  11322. (defvar org-last-inserted-timestamp)
  11323. (defvar org-log-post-message)
  11324. (defvar org-log-note-purpose)
  11325. (defvar org-log-note-how)
  11326. (defvar org-log-note-extra)
  11327. (defun org-auto-repeat-maybe (done-word)
  11328. "Check if the current headline contains a repeated deadline/schedule.
  11329. If yes, set TODO state back to what it was and change the base date
  11330. of repeating deadline/scheduled time stamps to new date.
  11331. This function is run automatically after each state change to a DONE state."
  11332. ;; last-state is dynamically scoped into this function
  11333. (let* ((repeat (org-get-repeat))
  11334. (aa (assoc org-last-state org-todo-kwd-alist))
  11335. (interpret (nth 1 aa))
  11336. (head (nth 2 aa))
  11337. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11338. (msg "Entry repeats: ")
  11339. (org-log-done nil)
  11340. (org-todo-log-states nil)
  11341. re type n what ts time to-state)
  11342. (when repeat
  11343. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11344. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11345. org-todo-repeat-to-state))
  11346. (unless (and to-state (member to-state org-todo-keywords-1))
  11347. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11348. (org-todo to-state)
  11349. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11350. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11351. (org-time-stamp-format t t))))
  11352. (when org-log-repeat
  11353. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11354. (memq 'org-add-log-note post-command-hook))
  11355. ;; OK, we are already setup for some record
  11356. (if (eq org-log-repeat 'note)
  11357. ;; make sure we take a note, not only a time stamp
  11358. (setq org-log-note-how 'note))
  11359. ;; Set up for taking a record
  11360. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11361. org-last-state
  11362. 'findpos org-log-repeat)))
  11363. (org-back-to-heading t)
  11364. (org-add-planning-info nil nil 'closed)
  11365. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11366. org-deadline-time-regexp "\\)\\|\\("
  11367. org-ts-regexp "\\)"))
  11368. (while (re-search-forward
  11369. re (save-excursion (outline-next-heading) (point)) t)
  11370. (setq type (if (match-end 1) org-scheduled-string
  11371. (if (match-end 3) org-deadline-string "Plain:"))
  11372. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11373. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11374. (setq n (string-to-number (match-string 2 ts))
  11375. what (match-string 3 ts))
  11376. (if (equal what "w") (setq n (* n 7) what "d"))
  11377. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11378. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11379. ;; Preparation, see if we need to modify the start date for the change
  11380. (when (match-end 1)
  11381. (setq time (save-match-data (org-time-string-to-time ts)))
  11382. (cond
  11383. ((equal (match-string 1 ts) ".")
  11384. ;; Shift starting date to today
  11385. (org-timestamp-change
  11386. (- (org-today) (time-to-days time))
  11387. 'day))
  11388. ((equal (match-string 1 ts) "+")
  11389. (let ((nshiftmax 10) (nshift 0))
  11390. (while (or (= nshift 0)
  11391. (<= (time-to-days time)
  11392. (time-to-days (current-time))))
  11393. (when (= (incf nshift) nshiftmax)
  11394. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11395. (error "Abort")))
  11396. (org-timestamp-change n (cdr (assoc what whata)))
  11397. (org-at-timestamp-p t)
  11398. (setq ts (match-string 1))
  11399. (setq time (save-match-data (org-time-string-to-time ts)))))
  11400. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11401. ;; rematch, so that we have everything in place for the real shift
  11402. (org-at-timestamp-p t)
  11403. (setq ts (match-string 1))
  11404. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11405. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11406. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11407. (setq org-log-post-message msg)
  11408. (message "%s" msg))))
  11409. (defun org-show-todo-tree (arg)
  11410. "Make a compact tree which shows all headlines marked with TODO.
  11411. The tree will show the lines where the regexp matches, and all higher
  11412. headlines above the match.
  11413. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11414. With a numeric prefix N, construct a sparse tree for the Nth element
  11415. of `org-todo-keywords-1'."
  11416. (interactive "P")
  11417. (let ((case-fold-search nil)
  11418. (kwd-re
  11419. (cond ((null arg) org-not-done-regexp)
  11420. ((equal arg '(4))
  11421. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11422. (mapcar 'list org-todo-keywords-1))))
  11423. (concat "\\("
  11424. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11425. "\\)\\>")))
  11426. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11427. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11428. org-todo-keywords-1)))
  11429. (t (error "Invalid prefix argument: %s" arg)))))
  11430. (message "%d TODO entries found"
  11431. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11432. (defun org-deadline (&optional arg time)
  11433. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11434. With one universal prefix argument, remove any deadline from the item.
  11435. With two universal prefix arguments, prompt for a warning delay.
  11436. With argument TIME, set the deadline at the corresponding date. TIME
  11437. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11438. (interactive "P")
  11439. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11440. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11441. 'region-start-level 'region))
  11442. org-loop-over-headlines-in-active-region)
  11443. (org-map-entries
  11444. `(org-deadline ',arg ,time)
  11445. org-loop-over-headlines-in-active-region
  11446. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11447. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11448. (repeater (and old-date
  11449. (string-match
  11450. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11451. old-date)
  11452. (match-string 1 old-date))))
  11453. (cond
  11454. ((equal arg '(4))
  11455. (when (and old-date org-log-redeadline)
  11456. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11457. org-log-redeadline))
  11458. (org-remove-timestamp-with-keyword org-deadline-string)
  11459. (message "Item no longer has a deadline."))
  11460. ((equal arg '(16))
  11461. (save-excursion
  11462. (if (re-search-forward
  11463. org-deadline-time-regexp
  11464. (save-excursion (outline-next-heading) (point)) t)
  11465. (let* ((rpl0 (match-string 1))
  11466. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11467. (replace-match
  11468. (concat org-deadline-string
  11469. " <" rpl
  11470. (format " -%dd" (abs
  11471. (- (time-to-days
  11472. (save-match-data
  11473. (org-read-date nil t nil "Warn starting from")))
  11474. (time-to-days nil))))
  11475. ">") t t))
  11476. (user-error "No deadline information to update"))))
  11477. (t
  11478. (org-add-planning-info 'deadline time 'closed)
  11479. (when (and old-date org-log-redeadline
  11480. (not (equal old-date
  11481. (substring org-last-inserted-timestamp 1 -1))))
  11482. (org-add-log-setup 'redeadline nil old-date 'findpos
  11483. org-log-redeadline))
  11484. (when repeater
  11485. (save-excursion
  11486. (org-back-to-heading t)
  11487. (when (re-search-forward (concat org-deadline-string " "
  11488. org-last-inserted-timestamp)
  11489. (save-excursion
  11490. (outline-next-heading) (point)) t)
  11491. (goto-char (1- (match-end 0)))
  11492. (insert " " repeater)
  11493. (setq org-last-inserted-timestamp
  11494. (concat (substring org-last-inserted-timestamp 0 -1)
  11495. " " repeater
  11496. (substring org-last-inserted-timestamp -1))))))
  11497. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11498. (defun org-schedule (&optional arg time)
  11499. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11500. With one universal prefix argument, remove any scheduling date from the item.
  11501. With two universal prefix arguments, prompt for a delay cookie.
  11502. With argument TIME, scheduled at the corresponding date. TIME can
  11503. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11504. (interactive "P")
  11505. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11506. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11507. 'region-start-level 'region))
  11508. org-loop-over-headlines-in-active-region)
  11509. (org-map-entries
  11510. `(org-schedule ',arg ,time)
  11511. org-loop-over-headlines-in-active-region
  11512. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11513. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11514. (repeater (and old-date
  11515. (string-match
  11516. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11517. old-date)
  11518. (match-string 1 old-date))))
  11519. (cond
  11520. ((equal arg '(4))
  11521. (progn
  11522. (when (and old-date org-log-reschedule)
  11523. (org-add-log-setup 'delschedule nil old-date 'findpos
  11524. org-log-reschedule))
  11525. (org-remove-timestamp-with-keyword org-scheduled-string)
  11526. (message "Item is no longer scheduled.")))
  11527. ((equal arg '(16))
  11528. (save-excursion
  11529. (if (re-search-forward
  11530. org-scheduled-time-regexp
  11531. (save-excursion (outline-next-heading) (point)) t)
  11532. (let* ((rpl0 (match-string 1))
  11533. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11534. (replace-match
  11535. (concat org-scheduled-string
  11536. " <" rpl
  11537. (format " -%dd" (abs
  11538. (- (time-to-days
  11539. (save-match-data
  11540. (org-read-date nil t nil "Delay until")))
  11541. (time-to-days nil))))
  11542. ">") t t))
  11543. (user-error "No scheduled information to update"))))
  11544. (t
  11545. (org-add-planning-info 'scheduled time 'closed)
  11546. (when (and old-date org-log-reschedule
  11547. (not (equal old-date
  11548. (substring org-last-inserted-timestamp 1 -1))))
  11549. (org-add-log-setup 'reschedule nil old-date 'findpos
  11550. org-log-reschedule))
  11551. (when repeater
  11552. (save-excursion
  11553. (org-back-to-heading t)
  11554. (when (re-search-forward (concat org-scheduled-string " "
  11555. org-last-inserted-timestamp)
  11556. (save-excursion
  11557. (outline-next-heading) (point)) t)
  11558. (goto-char (1- (match-end 0)))
  11559. (insert " " repeater)
  11560. (setq org-last-inserted-timestamp
  11561. (concat (substring org-last-inserted-timestamp 0 -1)
  11562. " " repeater
  11563. (substring org-last-inserted-timestamp -1))))))
  11564. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11565. (defun org-get-scheduled-time (pom &optional inherit)
  11566. "Get the scheduled time as a time tuple, of a format suitable
  11567. for calling org-schedule with, or if there is no scheduling,
  11568. returns nil."
  11569. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11570. (when time
  11571. (apply 'encode-time (org-parse-time-string time)))))
  11572. (defun org-get-deadline-time (pom &optional inherit)
  11573. "Get the deadline as a time tuple, of a format suitable for
  11574. calling org-deadline with, or if there is no scheduling, returns
  11575. nil."
  11576. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11577. (when time
  11578. (apply 'encode-time (org-parse-time-string time)))))
  11579. (defun org-remove-timestamp-with-keyword (keyword)
  11580. "Remove all time stamps with KEYWORD in the current entry."
  11581. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11582. beg)
  11583. (save-excursion
  11584. (org-back-to-heading t)
  11585. (setq beg (point))
  11586. (outline-next-heading)
  11587. (while (re-search-backward re beg t)
  11588. (replace-match "")
  11589. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11590. (equal (char-before) ?\ ))
  11591. (backward-delete-char 1)
  11592. (if (string-match "^[ \t]*$" (buffer-substring
  11593. (point-at-bol) (point-at-eol)))
  11594. (delete-region (point-at-bol)
  11595. (min (point-max) (1+ (point-at-eol))))))))))
  11596. (defun org-add-planning-info (what &optional time &rest remove)
  11597. "Insert new timestamp with keyword in the line directly after the headline.
  11598. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11599. If non is given, the user is prompted for a date.
  11600. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11601. be removed."
  11602. (interactive)
  11603. (let (org-time-was-given org-end-time-was-given ts
  11604. end default-time default-input)
  11605. (catch 'exit
  11606. (when (and (memq what '(scheduled deadline))
  11607. (or (not time)
  11608. (and (stringp time)
  11609. (string-match "^[-+]+[0-9]" time))))
  11610. ;; Try to get a default date/time from existing timestamp
  11611. (save-excursion
  11612. (org-back-to-heading t)
  11613. (setq end (save-excursion (outline-next-heading) (point)))
  11614. (when (re-search-forward (if (eq what 'scheduled)
  11615. org-scheduled-time-regexp
  11616. org-deadline-time-regexp)
  11617. end t)
  11618. (setq ts (match-string 1)
  11619. default-time
  11620. (apply 'encode-time (org-parse-time-string ts))
  11621. default-input (and ts (org-get-compact-tod ts))))))
  11622. (when what
  11623. (setq time
  11624. (if (stringp time)
  11625. ;; This is a string (relative or absolute), set proper date
  11626. (apply 'encode-time
  11627. (org-read-date-analyze
  11628. time default-time (decode-time default-time)))
  11629. ;; If necessary, get the time from the user
  11630. (or time (org-read-date nil 'to-time nil nil
  11631. default-time default-input)))))
  11632. (when (and org-insert-labeled-timestamps-at-point
  11633. (member what '(scheduled deadline)))
  11634. (insert
  11635. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11636. (org-insert-time-stamp time org-time-was-given
  11637. nil nil nil (list org-end-time-was-given))
  11638. (setq what nil))
  11639. (save-excursion
  11640. (save-restriction
  11641. (let (col list elt ts buffer-invisibility-spec)
  11642. (org-back-to-heading t)
  11643. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11644. (goto-char (match-end 1))
  11645. (setq col (current-column))
  11646. (goto-char (match-end 0))
  11647. (if (eobp) (insert "\n") (forward-char 1))
  11648. (when (and (not what)
  11649. (not (looking-at
  11650. (concat "[ \t]*"
  11651. org-keyword-time-not-clock-regexp))))
  11652. ;; Nothing to add, nothing to remove...... :-)
  11653. (throw 'exit nil))
  11654. (if (and (not (looking-at org-outline-regexp))
  11655. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11656. "[^\r\n]*"))
  11657. (not (equal (match-string 1) org-clock-string)))
  11658. (narrow-to-region (match-beginning 0) (match-end 0))
  11659. (insert-before-markers "\n")
  11660. (backward-char 1)
  11661. (narrow-to-region (point) (point))
  11662. (and org-adapt-indentation (org-indent-to-column col)))
  11663. ;; Check if we have to remove something.
  11664. (setq list (cons what remove))
  11665. (while list
  11666. (setq elt (pop list))
  11667. (when (or (and (eq elt 'scheduled)
  11668. (re-search-forward org-scheduled-time-regexp nil t))
  11669. (and (eq elt 'deadline)
  11670. (re-search-forward org-deadline-time-regexp nil t))
  11671. (and (eq elt 'closed)
  11672. (re-search-forward org-closed-time-regexp nil t)))
  11673. (replace-match "")
  11674. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11675. (and (looking-at "[ \t]+") (replace-match ""))
  11676. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11677. (when what
  11678. (insert
  11679. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11680. (cond ((eq what 'scheduled) org-scheduled-string)
  11681. ((eq what 'deadline) org-deadline-string)
  11682. ((eq what 'closed) org-closed-string))
  11683. " ")
  11684. (setq ts (org-insert-time-stamp
  11685. time
  11686. (or org-time-was-given
  11687. (and (eq what 'closed) org-log-done-with-time))
  11688. (eq what 'closed)
  11689. nil nil (list org-end-time-was-given)))
  11690. (insert
  11691. (if (not (or (bolp) (eq (char-before) ?\ )
  11692. (memq (char-after) '(32 10))
  11693. (eobp))) " " ""))
  11694. (end-of-line 1))
  11695. (goto-char (point-min))
  11696. (widen)
  11697. (if (and (looking-at "[ \t]*\n")
  11698. (equal (char-before) ?\n))
  11699. (delete-region (1- (point)) (point-at-eol)))
  11700. ts))))))
  11701. (defvar org-log-note-marker (make-marker))
  11702. (defvar org-log-note-purpose nil)
  11703. (defvar org-log-note-state nil)
  11704. (defvar org-log-note-previous-state nil)
  11705. (defvar org-log-note-how nil)
  11706. (defvar org-log-note-extra nil)
  11707. (defvar org-log-note-window-configuration nil)
  11708. (defvar org-log-note-return-to (make-marker))
  11709. (defvar org-log-note-effective-time nil
  11710. "Remembered current time so that dynamically scoped
  11711. `org-extend-today-until' affects tha timestamps in state change
  11712. log")
  11713. (defvar org-log-post-message nil
  11714. "Message to be displayed after a log note has been stored.
  11715. The auto-repeater uses this.")
  11716. (defun org-add-note ()
  11717. "Add a note to the current entry.
  11718. This is done in the same way as adding a state change note."
  11719. (interactive)
  11720. (org-add-log-setup 'note nil nil 'findpos nil))
  11721. (defvar org-property-end-re)
  11722. (defun org-add-log-setup (&optional purpose state prev-state
  11723. findpos how extra)
  11724. "Set up the post command hook to take a note.
  11725. If this is about to TODO state change, the new state is expected in STATE.
  11726. When FINDPOS is non-nil, find the correct position for the note in
  11727. the current entry. If not, assume that it can be inserted at point.
  11728. HOW is an indicator what kind of note should be created.
  11729. EXTRA is additional text that will be inserted into the notes buffer."
  11730. (let* ((org-log-into-drawer (org-log-into-drawer))
  11731. (drawer (cond ((stringp org-log-into-drawer)
  11732. org-log-into-drawer)
  11733. (org-log-into-drawer "LOGBOOK"))))
  11734. (save-restriction
  11735. (save-excursion
  11736. (when findpos
  11737. (org-back-to-heading t)
  11738. (narrow-to-region (point) (save-excursion
  11739. (outline-next-heading) (point)))
  11740. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11741. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11742. "[^\r\n]*\\)?"))
  11743. (goto-char (match-end 0))
  11744. (cond
  11745. (drawer
  11746. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11747. nil t)
  11748. (progn
  11749. (goto-char (match-end 0))
  11750. (or org-log-states-order-reversed
  11751. (and (re-search-forward org-property-end-re nil t)
  11752. (goto-char (1- (match-beginning 0))))))
  11753. (insert "\n:" drawer ":\n:END:")
  11754. (beginning-of-line 0)
  11755. (org-indent-line)
  11756. (beginning-of-line 2)
  11757. (org-indent-line)
  11758. (end-of-line 0)))
  11759. ((and org-log-state-notes-insert-after-drawers
  11760. (save-excursion
  11761. (forward-line) (looking-at org-drawer-regexp)))
  11762. (forward-line)
  11763. (while (looking-at org-drawer-regexp)
  11764. (goto-char (match-end 0))
  11765. (re-search-forward org-property-end-re (point-max) t)
  11766. (forward-line))
  11767. (forward-line -1)))
  11768. (unless org-log-states-order-reversed
  11769. (and (= (char-after) ?\n) (forward-char 1))
  11770. (org-skip-over-state-notes)
  11771. (skip-chars-backward " \t\n\r")))
  11772. (move-marker org-log-note-marker (point))
  11773. (setq org-log-note-purpose purpose
  11774. org-log-note-state state
  11775. org-log-note-previous-state prev-state
  11776. org-log-note-how how
  11777. org-log-note-extra extra
  11778. org-log-note-effective-time (org-current-effective-time))
  11779. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11780. (defun org-skip-over-state-notes ()
  11781. "Skip past the list of State notes in an entry."
  11782. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11783. (when (ignore-errors (goto-char (org-in-item-p)))
  11784. (let* ((struct (org-list-struct))
  11785. (prevs (org-list-prevs-alist struct)))
  11786. (while (looking-at "[ \t]*- State")
  11787. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11788. (org-list-get-item-end (point) struct)))))))
  11789. (defun org-add-log-note (&optional purpose)
  11790. "Pop up a window for taking a note, and add this note later at point."
  11791. (remove-hook 'post-command-hook 'org-add-log-note)
  11792. (setq org-log-note-window-configuration (current-window-configuration))
  11793. (delete-other-windows)
  11794. (move-marker org-log-note-return-to (point))
  11795. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11796. (goto-char org-log-note-marker)
  11797. (org-switch-to-buffer-other-window "*Org Note*")
  11798. (erase-buffer)
  11799. (if (memq org-log-note-how '(time state))
  11800. (let (current-prefix-arg) (org-store-log-note))
  11801. (let ((org-inhibit-startup t)) (org-mode))
  11802. (insert (format "# Insert note for %s.
  11803. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11804. (cond
  11805. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11806. ((eq org-log-note-purpose 'done) "closed todo item")
  11807. ((eq org-log-note-purpose 'state)
  11808. (format "state change from \"%s\" to \"%s\""
  11809. (or org-log-note-previous-state "")
  11810. (or org-log-note-state "")))
  11811. ((eq org-log-note-purpose 'reschedule)
  11812. "rescheduling")
  11813. ((eq org-log-note-purpose 'delschedule)
  11814. "no longer scheduled")
  11815. ((eq org-log-note-purpose 'redeadline)
  11816. "changing deadline")
  11817. ((eq org-log-note-purpose 'deldeadline)
  11818. "removing deadline")
  11819. ((eq org-log-note-purpose 'refile)
  11820. "refiling")
  11821. ((eq org-log-note-purpose 'note)
  11822. "this entry")
  11823. (t (error "This should not happen")))))
  11824. (if org-log-note-extra (insert org-log-note-extra))
  11825. (org-set-local 'org-finish-function 'org-store-log-note)
  11826. (run-hooks 'org-log-buffer-setup-hook)))
  11827. (defvar org-note-abort nil) ; dynamically scoped
  11828. (defun org-store-log-note ()
  11829. "Finish taking a log note, and insert it to where it belongs."
  11830. (let ((txt (buffer-string)))
  11831. (kill-buffer (current-buffer))
  11832. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11833. lines ind bul)
  11834. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11835. (setq txt (replace-match "" t t txt)))
  11836. (if (string-match "\\s-+\\'" txt)
  11837. (setq txt (replace-match "" t t txt)))
  11838. (setq lines (org-split-string txt "\n"))
  11839. (when (and note (string-match "\\S-" note))
  11840. (setq note
  11841. (org-replace-escapes
  11842. note
  11843. (list (cons "%u" (user-login-name))
  11844. (cons "%U" user-full-name)
  11845. (cons "%t" (format-time-string
  11846. (org-time-stamp-format 'long 'inactive)
  11847. org-log-note-effective-time))
  11848. (cons "%T" (format-time-string
  11849. (org-time-stamp-format 'long nil)
  11850. org-log-note-effective-time))
  11851. (cons "%d" (format-time-string
  11852. (org-time-stamp-format nil 'inactive)
  11853. org-log-note-effective-time))
  11854. (cons "%D" (format-time-string
  11855. (org-time-stamp-format nil nil)
  11856. org-log-note-effective-time))
  11857. (cons "%s" (if org-log-note-state
  11858. (concat "\"" org-log-note-state "\"")
  11859. ""))
  11860. (cons "%S" (if org-log-note-previous-state
  11861. (concat "\"" org-log-note-previous-state "\"")
  11862. "\"\"")))))
  11863. (if lines (setq note (concat note " \\\\")))
  11864. (push note lines))
  11865. (when (or current-prefix-arg org-note-abort)
  11866. (when org-log-into-drawer
  11867. (org-remove-empty-drawer-at
  11868. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11869. org-log-note-marker))
  11870. (setq lines nil))
  11871. (when lines
  11872. (with-current-buffer (marker-buffer org-log-note-marker)
  11873. (save-excursion
  11874. (goto-char org-log-note-marker)
  11875. (move-marker org-log-note-marker nil)
  11876. (end-of-line 1)
  11877. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11878. (setq ind (save-excursion
  11879. (if (ignore-errors (goto-char (org-in-item-p)))
  11880. (let ((struct (org-list-struct)))
  11881. (org-list-get-ind
  11882. (org-list-get-top-point struct) struct))
  11883. (skip-chars-backward " \r\t\n")
  11884. (cond
  11885. ((and (org-at-heading-p)
  11886. org-adapt-indentation)
  11887. (1+ (org-current-level)))
  11888. ((org-at-heading-p) 0)
  11889. (t (org-get-indentation))))))
  11890. (setq bul (org-list-bullet-string "-"))
  11891. (org-indent-line-to ind)
  11892. (insert bul (pop lines))
  11893. (let ((ind-body (+ (length bul) ind)))
  11894. (while lines
  11895. (insert "\n")
  11896. (org-indent-line-to ind-body)
  11897. (insert (pop lines))))
  11898. (message "Note stored")
  11899. (org-back-to-heading t)
  11900. (org-cycle-hide-drawers 'children))))))
  11901. (set-window-configuration org-log-note-window-configuration)
  11902. (with-current-buffer (marker-buffer org-log-note-return-to)
  11903. (goto-char org-log-note-return-to))
  11904. (move-marker org-log-note-return-to nil)
  11905. (and org-log-post-message (message "%s" org-log-post-message)))
  11906. (defun org-remove-empty-drawer-at (drawer pos)
  11907. "Remove an empty drawer DRAWER at position POS.
  11908. POS may also be a marker."
  11909. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11910. (save-excursion
  11911. (save-restriction
  11912. (widen)
  11913. (goto-char pos)
  11914. (if (org-in-regexp
  11915. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11916. (replace-match ""))))))
  11917. (defvar org-ts-type nil)
  11918. (defun org-sparse-tree (&optional arg type)
  11919. "Create a sparse tree, prompt for the details.
  11920. This command can create sparse trees. You first need to select the type
  11921. of match used to create the tree:
  11922. t Show all TODO entries.
  11923. T Show entries with a specific TODO keyword.
  11924. m Show entries selected by a tags/property match.
  11925. p Enter a property name and its value (both with completion on existing
  11926. names/values) and show entries with that property.
  11927. r Show entries matching a regular expression (`/' can be used as well).
  11928. b Show deadlines and scheduled items before a date.
  11929. a Show deadlines and scheduled items after a date.
  11930. d Show deadlines due within `org-deadline-warning-days'.
  11931. D Show deadlines and scheduled items between a date range."
  11932. (interactive "P")
  11933. (let (ans kwd value ts-type)
  11934. (setq type (or type org-sparse-tree-default-date-type))
  11935. (setq org-ts-type type)
  11936. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range\n [c]ycle through date types: %s"
  11937. (cond ((eq type 'all) "all timestamps")
  11938. ((eq type 'scheduled) "only scheduled")
  11939. ((eq type 'deadline) "only deadline")
  11940. ((eq type 'active) "only active timestamps")
  11941. ((eq type 'inactive) "only inactive timestamps")
  11942. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11943. (t "scheduled/deadline")))
  11944. (setq ans (read-char-exclusive))
  11945. (cond
  11946. ((equal ans ?c)
  11947. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11948. ((equal ans ?d)
  11949. (call-interactively 'org-check-deadlines))
  11950. ((equal ans ?b)
  11951. (call-interactively 'org-check-before-date))
  11952. ((equal ans ?a)
  11953. (call-interactively 'org-check-after-date))
  11954. ((equal ans ?D)
  11955. (call-interactively 'org-check-dates-range))
  11956. ((equal ans ?t)
  11957. (call-interactively 'org-show-todo-tree))
  11958. ((equal ans ?T)
  11959. (org-show-todo-tree '(4)))
  11960. ((member ans '(?T ?m))
  11961. (call-interactively 'org-match-sparse-tree))
  11962. ((member ans '(?p ?P))
  11963. (setq kwd (org-icompleting-read "Property: "
  11964. (mapcar 'list (org-buffer-property-keys))))
  11965. (setq value (org-icompleting-read "Value: "
  11966. (mapcar 'list (org-property-values kwd))))
  11967. (unless (string-match "\\`{.*}\\'" value)
  11968. (setq value (concat "\"" value "\"")))
  11969. (org-match-sparse-tree arg (concat kwd "=" value)))
  11970. ((member ans '(?r ?R ?/))
  11971. (call-interactively 'org-occur))
  11972. (t (error "No such sparse tree command \"%c\"" ans)))))
  11973. (defvar org-occur-highlights nil
  11974. "List of overlays used for occur matches.")
  11975. (make-variable-buffer-local 'org-occur-highlights)
  11976. (defvar org-occur-parameters nil
  11977. "Parameters of the active org-occur calls.
  11978. This is a list, each call to org-occur pushes as cons cell,
  11979. containing the regular expression and the callback, onto the list.
  11980. The list can contain several entries if `org-occur' has been called
  11981. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11982. will only contain one set of parameters. When the highlights are
  11983. removed (for example with `C-c C-c', or with the next edit (depending
  11984. on `org-remove-highlights-with-change'), this variable is emptied
  11985. as well.")
  11986. (make-variable-buffer-local 'org-occur-parameters)
  11987. (defun org-occur (regexp &optional keep-previous callback)
  11988. "Make a compact tree which shows all matches of REGEXP.
  11989. The tree will show the lines where the regexp matches, and all higher
  11990. headlines above the match. It will also show the heading after the match,
  11991. to make sure editing the matching entry is easy.
  11992. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11993. call to `org-occur' will be kept, to allow stacking of calls to this
  11994. command.
  11995. If CALLBACK is non-nil, it is a function which is called to confirm
  11996. that the match should indeed be shown."
  11997. (interactive "sRegexp: \nP")
  11998. (when (equal regexp "")
  11999. (error "Regexp cannot be empty"))
  12000. (unless keep-previous
  12001. (org-remove-occur-highlights nil nil t))
  12002. (push (cons regexp callback) org-occur-parameters)
  12003. (let ((cnt 0))
  12004. (save-excursion
  12005. (goto-char (point-min))
  12006. (if (or (not keep-previous) ; do not want to keep
  12007. (not org-occur-highlights)) ; no previous matches
  12008. ;; hide everything
  12009. (org-overview))
  12010. (while (re-search-forward regexp nil t)
  12011. (when (or (not callback)
  12012. (save-match-data (funcall callback)))
  12013. (setq cnt (1+ cnt))
  12014. (when org-highlight-sparse-tree-matches
  12015. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12016. (org-show-context 'occur-tree))))
  12017. (when org-remove-highlights-with-change
  12018. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12019. nil 'local))
  12020. (unless org-sparse-tree-open-archived-trees
  12021. (org-hide-archived-subtrees (point-min) (point-max)))
  12022. (run-hooks 'org-occur-hook)
  12023. (if (org-called-interactively-p 'interactive)
  12024. (message "%d match(es) for regexp %s" cnt regexp))
  12025. cnt))
  12026. (defun org-occur-next-match (&optional n reset)
  12027. "Function for `next-error-function' to find sparse tree matches.
  12028. N is the number of matches to move, when negative move backwards.
  12029. RESET is entirely ignored - this function always goes back to the
  12030. starting point when no match is found."
  12031. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12032. (search-func (if (< n 0)
  12033. 'previous-single-char-property-change
  12034. 'next-single-char-property-change))
  12035. (n (abs n))
  12036. (pos (point))
  12037. p1)
  12038. (catch 'exit
  12039. (while (setq p1 (funcall search-func (point) 'org-type))
  12040. (when (equal p1 limit)
  12041. (goto-char pos)
  12042. (error "No more matches"))
  12043. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12044. (setq n (1- n))
  12045. (when (= n 0)
  12046. (goto-char p1)
  12047. (throw 'exit (point))))
  12048. (goto-char p1))
  12049. (goto-char p1)
  12050. (error "No more matches"))))
  12051. (defun org-show-context (&optional key)
  12052. "Make sure point and context are visible.
  12053. How much context is shown depends upon the variables
  12054. `org-show-hierarchy-above', `org-show-following-heading',
  12055. `org-show-entry-below' and `org-show-siblings'."
  12056. (let ((heading-p (org-at-heading-p t))
  12057. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12058. (following-p (org-get-alist-option org-show-following-heading key))
  12059. (entry-p (org-get-alist-option org-show-entry-below key))
  12060. (siblings-p (org-get-alist-option org-show-siblings key)))
  12061. (catch 'exit
  12062. ;; Show heading or entry text
  12063. (if (and heading-p (not entry-p))
  12064. (org-flag-heading nil) ; only show the heading
  12065. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12066. (org-show-hidden-entry))) ; show entire entry
  12067. (when following-p
  12068. ;; Show next sibling, or heading below text
  12069. (save-excursion
  12070. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12071. (org-flag-heading nil))))
  12072. (when siblings-p (org-show-siblings))
  12073. (when hierarchy-p
  12074. ;; show all higher headings, possibly with siblings
  12075. (save-excursion
  12076. (while (and (condition-case nil
  12077. (progn (org-up-heading-all 1) t)
  12078. (error nil))
  12079. (not (bobp)))
  12080. (org-flag-heading nil)
  12081. (when siblings-p (org-show-siblings))))))))
  12082. (defvar org-reveal-start-hook nil
  12083. "Hook run before revealing a location.")
  12084. (defun org-reveal (&optional siblings)
  12085. "Show current entry, hierarchy above it, and the following headline.
  12086. This can be used to show a consistent set of context around locations
  12087. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12088. not t for the search context.
  12089. With optional argument SIBLINGS, on each level of the hierarchy all
  12090. siblings are shown. This repairs the tree structure to what it would
  12091. look like when opened with hierarchical calls to `org-cycle'.
  12092. With double optional argument \\[universal-argument] \\[universal-argument], \
  12093. go to the parent and show the
  12094. entire tree."
  12095. (interactive "P")
  12096. (run-hooks 'org-reveal-start-hook)
  12097. (let ((org-show-hierarchy-above t)
  12098. (org-show-following-heading t)
  12099. (org-show-siblings (if siblings t org-show-siblings)))
  12100. (org-show-context nil))
  12101. (when (equal siblings '(16))
  12102. (save-excursion
  12103. (when (org-up-heading-safe)
  12104. (org-show-subtree)
  12105. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12106. (defun org-highlight-new-match (beg end)
  12107. "Highlight from BEG to END and mark the highlight is an occur headline."
  12108. (let ((ov (make-overlay beg end)))
  12109. (overlay-put ov 'face 'secondary-selection)
  12110. (overlay-put ov 'org-type 'org-occur)
  12111. (push ov org-occur-highlights)))
  12112. (defun org-remove-occur-highlights (&optional beg end noremove)
  12113. "Remove the occur highlights from the buffer.
  12114. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12115. from the `before-change-functions' in the current buffer."
  12116. (interactive)
  12117. (unless org-inhibit-highlight-removal
  12118. (mapc 'delete-overlay org-occur-highlights)
  12119. (setq org-occur-highlights nil)
  12120. (setq org-occur-parameters nil)
  12121. (unless noremove
  12122. (remove-hook 'before-change-functions
  12123. 'org-remove-occur-highlights 'local))))
  12124. ;;;; Priorities
  12125. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12126. "Regular expression matching the priority indicator.")
  12127. (defvar org-remove-priority-next-time nil)
  12128. (defun org-priority-up ()
  12129. "Increase the priority of the current item."
  12130. (interactive)
  12131. (org-priority 'up))
  12132. (defun org-priority-down ()
  12133. "Decrease the priority of the current item."
  12134. (interactive)
  12135. (org-priority 'down))
  12136. (defun org-priority (&optional action show)
  12137. "Change the priority of an item.
  12138. ACTION can be `set', `up', `down', or a character."
  12139. (interactive "P")
  12140. (if (equal action '(4))
  12141. (org-show-priority)
  12142. (unless org-enable-priority-commands
  12143. (error "Priority commands are disabled"))
  12144. (setq action (or action 'set))
  12145. (let (current new news have remove)
  12146. (save-excursion
  12147. (org-back-to-heading t)
  12148. (if (looking-at org-priority-regexp)
  12149. (setq current (string-to-char (match-string 2))
  12150. have t))
  12151. (cond
  12152. ((eq action 'remove)
  12153. (setq remove t new ?\ ))
  12154. ((or (eq action 'set)
  12155. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12156. (if (not (eq action 'set))
  12157. (setq new action)
  12158. (message "Priority %c-%c, SPC to remove: "
  12159. org-highest-priority org-lowest-priority)
  12160. (save-match-data
  12161. (setq new (read-char-exclusive))))
  12162. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12163. (= (upcase org-lowest-priority) org-lowest-priority))
  12164. (setq new (upcase new)))
  12165. (cond ((equal new ?\ ) (setq remove t))
  12166. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12167. (error "Priority must be between `%c' and `%c'"
  12168. org-highest-priority org-lowest-priority))))
  12169. ((eq action 'up)
  12170. (setq new (if have
  12171. (1- current) ; normal cycling
  12172. ;; last priority was empty
  12173. (if (eq last-command this-command)
  12174. org-lowest-priority ; wrap around empty to lowest
  12175. ;; default
  12176. (if org-priority-start-cycle-with-default
  12177. org-default-priority
  12178. (1- org-default-priority))))))
  12179. ((eq action 'down)
  12180. (setq new (if have
  12181. (1+ current) ; normal cycling
  12182. ;; last priority was empty
  12183. (if (eq last-command this-command)
  12184. org-highest-priority ; wrap around empty to highest
  12185. ;; default
  12186. (if org-priority-start-cycle-with-default
  12187. org-default-priority
  12188. (1+ org-default-priority))))))
  12189. (t (error "Invalid action")))
  12190. (if (or (< (upcase new) org-highest-priority)
  12191. (> (upcase new) org-lowest-priority))
  12192. (if (and (memq action '(up down))
  12193. (not have) (not (eq last-command this-command)))
  12194. ;; `new' is from default priority
  12195. (error
  12196. "The default can not be set, see `org-default-priority' why")
  12197. ;; normal cycling: `new' is beyond highest/lowest priority
  12198. ;; and is wrapped around to the empty priority
  12199. (setq remove t)))
  12200. (setq news (format "%c" new))
  12201. (if have
  12202. (if remove
  12203. (replace-match "" t t nil 1)
  12204. (replace-match news t t nil 2))
  12205. (if remove
  12206. (error "No priority cookie found in line")
  12207. (let ((case-fold-search nil))
  12208. (looking-at org-todo-line-regexp))
  12209. (if (match-end 2)
  12210. (progn
  12211. (goto-char (match-end 2))
  12212. (insert " [#" news "]"))
  12213. (goto-char (match-beginning 3))
  12214. (insert "[#" news "] "))))
  12215. (org-preserve-lc (org-set-tags nil 'align)))
  12216. (if remove
  12217. (message "Priority removed")
  12218. (message "Priority of current item set to %s" news)))))
  12219. (defun org-show-priority ()
  12220. "Show the priority of the current item.
  12221. This priority is composed of the main priority given with the [#A] cookies,
  12222. and by additional input from the age of a schedules or deadline entry."
  12223. (interactive)
  12224. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12225. (org-get-at-bol 'priority)
  12226. (save-excursion
  12227. (save-match-data
  12228. (beginning-of-line)
  12229. (and (looking-at org-heading-regexp)
  12230. (org-get-priority (match-string 0))))))))
  12231. (message "Priority is %d" (if pri pri -1000))))
  12232. (defun org-get-priority (s)
  12233. "Find priority cookie and return priority."
  12234. (save-match-data
  12235. (if (functionp org-get-priority-function)
  12236. (funcall org-get-priority-function)
  12237. (if (not (string-match org-priority-regexp s))
  12238. (* 1000 (- org-lowest-priority org-default-priority))
  12239. (* 1000 (- org-lowest-priority
  12240. (string-to-char (match-string 2 s))))))))
  12241. ;;;; Tags
  12242. (defvar org-agenda-archives-mode)
  12243. (defvar org-map-continue-from nil
  12244. "Position from where mapping should continue.
  12245. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12246. (defvar org-scanner-tags nil
  12247. "The current tag list while the tags scanner is running.")
  12248. (defvar org-trust-scanner-tags nil
  12249. "Should `org-get-tags-at' use the tags for the scanner.
  12250. This is for internal dynamical scoping only.
  12251. When this is non-nil, the function `org-get-tags-at' will return the value
  12252. of `org-scanner-tags' instead of building the list by itself. This
  12253. can lead to large speed-ups when the tags scanner is used in a file with
  12254. many entries, and when the list of tags is retrieved, for example to
  12255. obtain a list of properties. Building the tags list for each entry in such
  12256. a file becomes an N^2 operation - but with this variable set, it scales
  12257. as N.")
  12258. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12259. "Sca headline tags with inheritance and produce output ACTION.
  12260. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12261. or `agenda' to produce an entry list for an agenda view. It can also be
  12262. a Lisp form or a function that should be called at each matched headline, in
  12263. this case the return value is a list of all return values from these calls.
  12264. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12265. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12266. only lines with a not-done TODO keyword are included in the output.
  12267. This should be the same variable that was scoped into
  12268. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12269. START-LEVEL can be a string with asterisks, reducing the scope to
  12270. headlines matching this string."
  12271. (require 'org-agenda)
  12272. (let* ((re (concat "^"
  12273. (if start-level
  12274. ;; Get the correct level to match
  12275. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12276. org-outline-regexp)
  12277. " *\\(\\<\\("
  12278. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12279. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12280. (props (list 'face 'default
  12281. 'done-face 'org-agenda-done
  12282. 'undone-face 'default
  12283. 'mouse-face 'highlight
  12284. 'org-not-done-regexp org-not-done-regexp
  12285. 'org-todo-regexp org-todo-regexp
  12286. 'org-complex-heading-regexp org-complex-heading-regexp
  12287. 'help-echo
  12288. (format "mouse-2 or RET jump to org file %s"
  12289. (abbreviate-file-name
  12290. (or (buffer-file-name (buffer-base-buffer))
  12291. (buffer-name (buffer-base-buffer)))))))
  12292. (case-fold-search nil)
  12293. (org-map-continue-from nil)
  12294. lspos tags tags-list
  12295. (tags-alist (list (cons 0 org-file-tags)))
  12296. (llast 0) rtn rtn1 level category i txt
  12297. todo marker entry priority)
  12298. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12299. (setq action (list 'lambda nil action)))
  12300. (save-excursion
  12301. (goto-char (point-min))
  12302. (when (eq action 'sparse-tree)
  12303. (org-overview)
  12304. (org-remove-occur-highlights))
  12305. (while (re-search-forward re nil t)
  12306. (setq org-map-continue-from nil)
  12307. (catch :skip
  12308. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12309. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12310. (goto-char (setq lspos (match-beginning 0)))
  12311. (setq level (org-reduced-level (org-outline-level))
  12312. category (org-get-category))
  12313. (setq i llast llast level)
  12314. ;; remove tag lists from same and sublevels
  12315. (while (>= i level)
  12316. (when (setq entry (assoc i tags-alist))
  12317. (setq tags-alist (delete entry tags-alist)))
  12318. (setq i (1- i)))
  12319. ;; add the next tags
  12320. (when tags
  12321. (setq tags (org-split-string tags ":")
  12322. tags-alist
  12323. (cons (cons level tags) tags-alist)))
  12324. ;; compile tags for current headline
  12325. (setq tags-list
  12326. (if org-use-tag-inheritance
  12327. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12328. tags)
  12329. org-scanner-tags tags-list)
  12330. (when org-use-tag-inheritance
  12331. (setcdr (car tags-alist)
  12332. (mapcar (lambda (x)
  12333. (setq x (copy-sequence x))
  12334. (org-add-prop-inherited x))
  12335. (cdar tags-alist))))
  12336. (when (and tags org-use-tag-inheritance
  12337. (or (not (eq t org-use-tag-inheritance))
  12338. org-tags-exclude-from-inheritance))
  12339. ;; selective inheritance, remove uninherited ones
  12340. (setcdr (car tags-alist)
  12341. (org-remove-uninherited-tags (cdar tags-alist))))
  12342. (when (and
  12343. ;; eval matcher only when the todo condition is OK
  12344. (and (or (not todo-only) (member todo org-not-done-keywords))
  12345. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12346. (eval matcher)))
  12347. ;; Call the skipper, but return t if it does not skip,
  12348. ;; so that the `and' form continues evaluating
  12349. (progn
  12350. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12351. t)
  12352. ;; Check if timestamps are deselecting this entry
  12353. (or (not todo-only)
  12354. (and (member todo org-not-done-keywords)
  12355. (or (not org-agenda-tags-todo-honor-ignore-options)
  12356. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12357. ;; select this headline
  12358. (cond
  12359. ((eq action 'sparse-tree)
  12360. (and org-highlight-sparse-tree-matches
  12361. (org-get-heading) (match-end 0)
  12362. (org-highlight-new-match
  12363. (match-beginning 1) (match-end 1)))
  12364. (org-show-context 'tags-tree))
  12365. ((eq action 'agenda)
  12366. (setq txt (org-agenda-format-item
  12367. ""
  12368. (concat
  12369. (if (eq org-tags-match-list-sublevels 'indented)
  12370. (make-string (1- level) ?.) "")
  12371. (org-get-heading))
  12372. level category
  12373. tags-list)
  12374. priority (org-get-priority txt))
  12375. (goto-char lspos)
  12376. (setq marker (org-agenda-new-marker))
  12377. (org-add-props txt props
  12378. 'org-marker marker 'org-hd-marker marker 'org-category category
  12379. 'todo-state todo
  12380. 'priority priority 'type "tagsmatch")
  12381. (push txt rtn))
  12382. ((functionp action)
  12383. (setq org-map-continue-from nil)
  12384. (save-excursion
  12385. (setq rtn1 (funcall action))
  12386. (push rtn1 rtn)))
  12387. (t (error "Invalid action")))
  12388. ;; if we are to skip sublevels, jump to end of subtree
  12389. (unless org-tags-match-list-sublevels
  12390. (org-end-of-subtree t)
  12391. (backward-char 1))))
  12392. ;; Get the correct position from where to continue
  12393. (if org-map-continue-from
  12394. (goto-char org-map-continue-from)
  12395. (and (= (point) lspos) (end-of-line 1)))))
  12396. (when (and (eq action 'sparse-tree)
  12397. (not org-sparse-tree-open-archived-trees))
  12398. (org-hide-archived-subtrees (point-min) (point-max)))
  12399. (nreverse rtn)))
  12400. (defun org-remove-uninherited-tags (tags)
  12401. "Remove all tags that are not inherited from the list TAGS."
  12402. (cond
  12403. ((eq org-use-tag-inheritance t)
  12404. (if org-tags-exclude-from-inheritance
  12405. (org-delete-all org-tags-exclude-from-inheritance tags)
  12406. tags))
  12407. ((not org-use-tag-inheritance) nil)
  12408. ((stringp org-use-tag-inheritance)
  12409. (delq nil (mapcar
  12410. (lambda (x)
  12411. (if (and (string-match org-use-tag-inheritance x)
  12412. (not (member x org-tags-exclude-from-inheritance)))
  12413. x nil))
  12414. tags)))
  12415. ((listp org-use-tag-inheritance)
  12416. (delq nil (mapcar
  12417. (lambda (x)
  12418. (if (member x org-use-tag-inheritance) x nil))
  12419. tags)))))
  12420. (defun org-match-sparse-tree (&optional todo-only match)
  12421. "Create a sparse tree according to tags string MATCH.
  12422. MATCH can contain positive and negative selection of tags, like
  12423. \"+WORK+URGENT-WITHBOSS\".
  12424. If optional argument TODO-ONLY is non-nil, only select lines that are
  12425. also TODO lines."
  12426. (interactive "P")
  12427. (org-agenda-prepare-buffers (list (current-buffer)))
  12428. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12429. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12430. (defvar org-cached-props nil)
  12431. (defun org-cached-entry-get (pom property)
  12432. (if (or (eq t org-use-property-inheritance)
  12433. (and (stringp org-use-property-inheritance)
  12434. (string-match org-use-property-inheritance property))
  12435. (and (listp org-use-property-inheritance)
  12436. (member property org-use-property-inheritance)))
  12437. ;; Caching is not possible, check it directly
  12438. (org-entry-get pom property 'inherit)
  12439. ;; Get all properties, so that we can do complicated checks easily
  12440. (cdr (assoc property (or org-cached-props
  12441. (setq org-cached-props
  12442. (org-entry-properties pom)))))))
  12443. (defun org-global-tags-completion-table (&optional files)
  12444. "Return the list of all tags in all agenda buffer/files.
  12445. Optional FILES argument is a list of files which can be used
  12446. instead of the agenda files."
  12447. (save-excursion
  12448. (org-uniquify
  12449. (delq nil
  12450. (apply 'append
  12451. (mapcar
  12452. (lambda (file)
  12453. (set-buffer (find-file-noselect file))
  12454. (append (org-get-buffer-tags)
  12455. (mapcar (lambda (x) (if (stringp (car-safe x))
  12456. (list (car-safe x)) nil))
  12457. org-tag-alist)))
  12458. (if (and files (car files))
  12459. files
  12460. (org-agenda-files))))))))
  12461. (defun org-make-tags-matcher (match)
  12462. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12463. The variable `todo-only' is scoped dynamically into this function.
  12464. It will be set to t if the matcher restricts matching to TODO entries,
  12465. otherwise will not be touched.
  12466. Returns a cons of the selection string MATCH and the constructed
  12467. lisp form implementing the matcher. The matcher is to be evaluated
  12468. at an Org entry, with point on the headline, and returns t if the
  12469. entry matches the selection string MATCH. The returned lisp form
  12470. references two variables with information about the entry, which
  12471. must be bound around the form's evaluation: todo, the TODO keyword
  12472. at the entry (or nil of none); and tags-list, the list of all tags
  12473. at the entry including inherited ones. Additionally, the category
  12474. of the entry (if any) must be specified as the text property
  12475. 'org-category on the headline.
  12476. See also `org-scan-tags'.
  12477. "
  12478. (declare (special todo-only))
  12479. (unless (boundp 'todo-only)
  12480. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12481. (unless match
  12482. ;; Get a new match request, with completion
  12483. (let ((org-last-tags-completion-table
  12484. (org-global-tags-completion-table)))
  12485. (setq match (org-completing-read-no-i
  12486. "Match: " 'org-tags-completion-function nil nil nil
  12487. 'org-tags-history))))
  12488. ;; Parse the string and create a lisp form
  12489. (let ((match0 match)
  12490. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12491. minus tag mm
  12492. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12493. orterms term orlist re-p str-p level-p level-op time-p
  12494. prop-p pn pv po gv rest)
  12495. (if (string-match "/+" match)
  12496. ;; match contains also a todo-matching request
  12497. (progn
  12498. (setq tagsmatch (substring match 0 (match-beginning 0))
  12499. todomatch (substring match (match-end 0)))
  12500. (if (string-match "^!" todomatch)
  12501. (setq todo-only t todomatch (substring todomatch 1)))
  12502. (if (string-match "^\\s-*$" todomatch)
  12503. (setq todomatch nil)))
  12504. ;; only matching tags
  12505. (setq tagsmatch match todomatch nil))
  12506. ;; Make the tags matcher
  12507. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12508. (setq tagsmatcher t)
  12509. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12510. (while (setq term (pop orterms))
  12511. (while (and (equal (substring term -1) "\\") orterms)
  12512. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12513. (while (string-match re term)
  12514. (setq rest (substring term (match-end 0))
  12515. minus (and (match-end 1)
  12516. (equal (match-string 1 term) "-"))
  12517. tag (save-match-data (replace-regexp-in-string
  12518. "\\\\-" "-"
  12519. (match-string 2 term)))
  12520. re-p (equal (string-to-char tag) ?{)
  12521. level-p (match-end 4)
  12522. prop-p (match-end 5)
  12523. mm (cond
  12524. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12525. (level-p
  12526. (setq level-op (org-op-to-function (match-string 3 term)))
  12527. `(,level-op level ,(string-to-number
  12528. (match-string 4 term))))
  12529. (prop-p
  12530. (setq pn (match-string 5 term)
  12531. po (match-string 6 term)
  12532. pv (match-string 7 term)
  12533. re-p (equal (string-to-char pv) ?{)
  12534. str-p (equal (string-to-char pv) ?\")
  12535. time-p (save-match-data
  12536. (string-match "^\"[[<].*[]>]\"$" pv))
  12537. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12538. (if time-p (setq pv (org-matcher-time pv)))
  12539. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12540. (cond
  12541. ((equal pn "CATEGORY")
  12542. (setq gv '(get-text-property (point) 'org-category)))
  12543. ((equal pn "TODO")
  12544. (setq gv 'todo))
  12545. (t
  12546. (setq gv `(org-cached-entry-get nil ,pn))))
  12547. (if re-p
  12548. (if (eq po 'org<>)
  12549. `(not (string-match ,pv (or ,gv "")))
  12550. `(string-match ,pv (or ,gv "")))
  12551. (if str-p
  12552. `(,po (or ,gv "") ,pv)
  12553. `(,po (string-to-number (or ,gv ""))
  12554. ,(string-to-number pv) ))))
  12555. (t `(member ,tag tags-list)))
  12556. mm (if minus (list 'not mm) mm)
  12557. term rest)
  12558. (push mm tagsmatcher))
  12559. (push (if (> (length tagsmatcher) 1)
  12560. (cons 'and tagsmatcher)
  12561. (car tagsmatcher))
  12562. orlist)
  12563. (setq tagsmatcher nil))
  12564. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12565. (setq tagsmatcher
  12566. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12567. ;; Make the todo matcher
  12568. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12569. (setq todomatcher t)
  12570. (setq orterms (org-split-string todomatch "|") orlist nil)
  12571. (while (setq term (pop orterms))
  12572. (while (string-match re term)
  12573. (setq minus (and (match-end 1)
  12574. (equal (match-string 1 term) "-"))
  12575. kwd (match-string 2 term)
  12576. re-p (equal (string-to-char kwd) ?{)
  12577. term (substring term (match-end 0))
  12578. mm (if re-p
  12579. `(string-match ,(substring kwd 1 -1) todo)
  12580. (list 'equal 'todo kwd))
  12581. mm (if minus (list 'not mm) mm))
  12582. (push mm todomatcher))
  12583. (push (if (> (length todomatcher) 1)
  12584. (cons 'and todomatcher)
  12585. (car todomatcher))
  12586. orlist)
  12587. (setq todomatcher nil))
  12588. (setq todomatcher (if (> (length orlist) 1)
  12589. (cons 'or orlist) (car orlist))))
  12590. ;; Return the string and lisp forms of the matcher
  12591. (setq matcher (if todomatcher
  12592. (list 'and tagsmatcher todomatcher)
  12593. tagsmatcher))
  12594. (when todo-only
  12595. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12596. matcher)))
  12597. (cons match0 matcher)))
  12598. (defun org-op-to-function (op &optional stringp)
  12599. "Turn an operator into the appropriate function."
  12600. (setq op
  12601. (cond
  12602. ((equal op "<" ) '(< string< org-time<))
  12603. ((equal op ">" ) '(> org-string> org-time>))
  12604. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12605. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12606. ((member op '("=" "==")) '(= string= org-time=))
  12607. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12608. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12609. (defun org<> (a b) (not (= a b)))
  12610. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12611. (defun org-string>= (a b) (not (string< a b)))
  12612. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12613. (defun org-string<> (a b) (not (string= a b)))
  12614. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12615. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12616. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12617. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12618. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12619. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12620. (defun org-2ft (s)
  12621. "Convert S to a floating point time.
  12622. If S is already a number, just return it. If it is a string, parse
  12623. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12624. (cond
  12625. ((numberp s) s)
  12626. ((stringp s)
  12627. (condition-case nil
  12628. (float-time (apply 'encode-time (org-parse-time-string s)))
  12629. (error 0.)))
  12630. (t 0.)))
  12631. (defun org-time-today ()
  12632. "Time in seconds today at 0:00.
  12633. Returns the float number of seconds since the beginning of the
  12634. epoch to the beginning of today (00:00)."
  12635. (float-time (apply 'encode-time
  12636. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12637. (defun org-matcher-time (s)
  12638. "Interpret a time comparison value."
  12639. (save-match-data
  12640. (cond
  12641. ((string= s "<now>") (float-time))
  12642. ((string= s "<today>") (org-time-today))
  12643. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12644. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12645. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12646. (+ (org-time-today)
  12647. (* (string-to-number (match-string 1 s))
  12648. (cdr (assoc (match-string 2 s)
  12649. '(("d" . 86400.0) ("w" . 604800.0)
  12650. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12651. (t (org-2ft s)))))
  12652. (defun org-match-any-p (re list)
  12653. "Does re match any element of list?"
  12654. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12655. (delq nil list))
  12656. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12657. (defvar org-tags-overlay (make-overlay 1 1))
  12658. (org-detach-overlay org-tags-overlay)
  12659. (defun org-get-local-tags-at (&optional pos)
  12660. "Get a list of tags defined in the current headline."
  12661. (org-get-tags-at pos 'local))
  12662. (defun org-get-local-tags ()
  12663. "Get a list of tags defined in the current headline."
  12664. (org-get-tags-at nil 'local))
  12665. (defun org-get-tags-at (&optional pos local)
  12666. "Get a list of all headline tags applicable at POS.
  12667. POS defaults to point. If tags are inherited, the list contains
  12668. the targets in the same sequence as the headlines appear, i.e.
  12669. the tags of the current headline come last.
  12670. When LOCAL is non-nil, only return tags from the current headline,
  12671. ignore inherited ones."
  12672. (interactive)
  12673. (if (and org-trust-scanner-tags
  12674. (or (not pos) (equal pos (point)))
  12675. (not local))
  12676. org-scanner-tags
  12677. (let (tags ltags lastpos parent)
  12678. (save-excursion
  12679. (save-restriction
  12680. (widen)
  12681. (goto-char (or pos (point)))
  12682. (save-match-data
  12683. (catch 'done
  12684. (condition-case nil
  12685. (progn
  12686. (org-back-to-heading t)
  12687. (while (not (equal lastpos (point)))
  12688. (setq lastpos (point))
  12689. (when (looking-at
  12690. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12691. (setq ltags (org-split-string
  12692. (org-match-string-no-properties 1) ":"))
  12693. (when parent
  12694. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12695. (setq tags (append
  12696. (if parent
  12697. (org-remove-uninherited-tags ltags)
  12698. ltags)
  12699. tags)))
  12700. (or org-use-tag-inheritance (throw 'done t))
  12701. (if local (throw 'done t))
  12702. (or (org-up-heading-safe) (error nil))
  12703. (setq parent t)))
  12704. (error nil)))))
  12705. (if local
  12706. tags
  12707. (reverse (delete-dups
  12708. (reverse (append
  12709. (org-remove-uninherited-tags
  12710. org-file-tags) tags)))))))))
  12711. (defun org-add-prop-inherited (s)
  12712. (add-text-properties 0 (length s) '(inherited t) s)
  12713. s)
  12714. (defun org-toggle-tag (tag &optional onoff)
  12715. "Toggle the tag TAG for the current line.
  12716. If ONOFF is `on' or `off', don't toggle but set to this state."
  12717. (let (res current)
  12718. (save-excursion
  12719. (org-back-to-heading t)
  12720. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12721. (point-at-eol) t)
  12722. (progn
  12723. (setq current (match-string 1))
  12724. (replace-match ""))
  12725. (setq current ""))
  12726. (setq current (nreverse (org-split-string current ":")))
  12727. (cond
  12728. ((eq onoff 'on)
  12729. (setq res t)
  12730. (or (member tag current) (push tag current)))
  12731. ((eq onoff 'off)
  12732. (or (not (member tag current)) (setq current (delete tag current))))
  12733. (t (if (member tag current)
  12734. (setq current (delete tag current))
  12735. (setq res t)
  12736. (push tag current))))
  12737. (end-of-line 1)
  12738. (if current
  12739. (progn
  12740. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12741. (org-set-tags nil t))
  12742. (delete-horizontal-space))
  12743. (run-hooks 'org-after-tags-change-hook))
  12744. res))
  12745. (defun org-align-tags-here (to-col)
  12746. ;; Assumes that this is a headline
  12747. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12748. (beginning-of-line 1)
  12749. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12750. (< pos (match-beginning 2)))
  12751. (progn
  12752. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12753. (goto-char (match-beginning 1))
  12754. (insert " ")
  12755. (delete-region (point) (1+ (match-beginning 2)))
  12756. (setq ncol (max (current-column)
  12757. (1+ col)
  12758. (if (> to-col 0)
  12759. to-col
  12760. (- (abs to-col) tags-l))))
  12761. (setq p (point))
  12762. (insert (make-string (- ncol (current-column)) ?\ ))
  12763. (setq ncol (current-column))
  12764. (when indent-tabs-mode (tabify p (point-at-eol)))
  12765. (org-move-to-column (min ncol col) t))
  12766. (goto-char pos))))
  12767. (defun org-set-tags-command (&optional arg just-align)
  12768. "Call the set-tags command for the current entry."
  12769. (interactive "P")
  12770. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12771. (org-set-tags arg just-align)
  12772. (save-excursion
  12773. (unless (and (org-region-active-p)
  12774. org-loop-over-headlines-in-active-region)
  12775. (org-back-to-heading t))
  12776. (org-set-tags arg just-align))))
  12777. (defun org-set-tags-to (data)
  12778. "Set the tags of the current entry to DATA, replacing the current tags.
  12779. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12780. If DATA is nil or the empty string, any tags will be removed."
  12781. (interactive "sTags: ")
  12782. (setq data
  12783. (cond
  12784. ((eq data nil) "")
  12785. ((equal data "") "")
  12786. ((stringp data)
  12787. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12788. ":"))
  12789. ((listp data)
  12790. (concat ":" (mapconcat 'identity data ":") ":"))))
  12791. (when data
  12792. (save-excursion
  12793. (org-back-to-heading t)
  12794. (when (looking-at org-complex-heading-regexp)
  12795. (if (match-end 5)
  12796. (progn
  12797. (goto-char (match-beginning 5))
  12798. (insert data)
  12799. (delete-region (point) (point-at-eol))
  12800. (org-set-tags nil 'align))
  12801. (goto-char (point-at-eol))
  12802. (insert " " data)
  12803. (org-set-tags nil 'align)))
  12804. (beginning-of-line 1)
  12805. (if (looking-at ".*?\\([ \t]+\\)$")
  12806. (delete-region (match-beginning 1) (match-end 1))))))
  12807. (defun org-align-all-tags ()
  12808. "Align the tags i all headings."
  12809. (interactive)
  12810. (save-excursion
  12811. (or (ignore-errors (org-back-to-heading t))
  12812. (outline-next-heading))
  12813. (if (org-at-heading-p)
  12814. (org-set-tags t)
  12815. (message "No headings"))))
  12816. (defvar org-indent-indentation-per-level)
  12817. (defun org-set-tags (&optional arg just-align)
  12818. "Set the tags for the current headline.
  12819. With prefix ARG, realign all tags in headings in the current buffer."
  12820. (interactive "P")
  12821. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12822. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12823. 'region-start-level 'region))
  12824. org-loop-over-headlines-in-active-region)
  12825. (org-map-entries
  12826. ;; We don't use ARG and JUST-ALIGN here these args are not
  12827. ;; useful when looping over headlines
  12828. `(org-set-tags)
  12829. org-loop-over-headlines-in-active-region
  12830. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12831. (let* ((re org-outline-regexp-bol)
  12832. (current (unless arg (org-get-tags-string)))
  12833. (col (current-column))
  12834. (org-setting-tags t)
  12835. table current-tags inherited-tags ; computed below when needed
  12836. tags p0 c0 c1 rpl di tc level)
  12837. (if arg
  12838. (save-excursion
  12839. (goto-char (point-min))
  12840. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12841. (while (re-search-forward re nil t)
  12842. (org-set-tags nil t)
  12843. (end-of-line 1)))
  12844. (message "All tags realigned to column %d" org-tags-column))
  12845. (if just-align
  12846. (setq tags current)
  12847. ;; Get a new set of tags from the user
  12848. (save-excursion
  12849. (setq table (append org-tag-persistent-alist
  12850. (or org-tag-alist (org-get-buffer-tags))
  12851. (and
  12852. org-complete-tags-always-offer-all-agenda-tags
  12853. (org-global-tags-completion-table
  12854. (org-agenda-files))))
  12855. org-last-tags-completion-table table
  12856. current-tags (org-split-string current ":")
  12857. inherited-tags (nreverse
  12858. (nthcdr (length current-tags)
  12859. (nreverse (org-get-tags-at))))
  12860. tags
  12861. (if (or (eq t org-use-fast-tag-selection)
  12862. (and org-use-fast-tag-selection
  12863. (delq nil (mapcar 'cdr table))))
  12864. (org-fast-tag-selection
  12865. current-tags inherited-tags table
  12866. (if org-fast-tag-selection-include-todo
  12867. org-todo-key-alist))
  12868. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12869. (org-trim
  12870. (org-icompleting-read "Tags: "
  12871. 'org-tags-completion-function
  12872. nil nil current 'org-tags-history))))))
  12873. (while (string-match "[-+&]+" tags)
  12874. ;; No boolean logic, just a list
  12875. (setq tags (replace-match ":" t t tags))))
  12876. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12877. (if org-tags-sort-function
  12878. (setq tags (mapconcat 'identity
  12879. (sort (org-split-string
  12880. tags (org-re "[^[:alnum:]_@#%]+"))
  12881. org-tags-sort-function) ":")))
  12882. (if (string-match "\\`[\t ]*\\'" tags)
  12883. (setq tags "")
  12884. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12885. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12886. ;; Insert new tags at the correct column
  12887. (beginning-of-line 1)
  12888. (setq level (or (and (looking-at org-outline-regexp)
  12889. (- (match-end 0) (point) 1))
  12890. 1))
  12891. (cond
  12892. ((and (equal current "") (equal tags "")))
  12893. ((re-search-forward
  12894. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12895. (point-at-eol) t)
  12896. (if (equal tags "")
  12897. (setq rpl "")
  12898. (goto-char (match-beginning 0))
  12899. (setq c0 (current-column)
  12900. ;; compute offset for the case of org-indent-mode active
  12901. di (if org-indent-mode
  12902. (* (1- org-indent-indentation-per-level) (1- level))
  12903. 0)
  12904. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12905. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12906. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12907. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12908. (replace-match rpl t t)
  12909. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12910. tags)
  12911. (t (error "Tags alignment failed")))
  12912. (org-move-to-column col)
  12913. (unless just-align
  12914. (run-hooks 'org-after-tags-change-hook))))))
  12915. (defun org-change-tag-in-region (beg end tag off)
  12916. "Add or remove TAG for each entry in the region.
  12917. This works in the agenda, and also in an org-mode buffer."
  12918. (interactive
  12919. (list (region-beginning) (region-end)
  12920. (let ((org-last-tags-completion-table
  12921. (if (derived-mode-p 'org-mode)
  12922. (org-get-buffer-tags)
  12923. (org-global-tags-completion-table))))
  12924. (org-icompleting-read
  12925. "Tag: " 'org-tags-completion-function nil nil nil
  12926. 'org-tags-history))
  12927. (progn
  12928. (message "[s]et or [r]emove? ")
  12929. (equal (read-char-exclusive) ?r))))
  12930. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12931. (let ((agendap (equal major-mode 'org-agenda-mode))
  12932. l1 l2 m buf pos newhead (cnt 0))
  12933. (goto-char end)
  12934. (setq l2 (1- (org-current-line)))
  12935. (goto-char beg)
  12936. (setq l1 (org-current-line))
  12937. (loop for l from l1 to l2 do
  12938. (org-goto-line l)
  12939. (setq m (get-text-property (point) 'org-hd-marker))
  12940. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12941. (and agendap m))
  12942. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12943. pos (if agendap m (point)))
  12944. (with-current-buffer buf
  12945. (save-excursion
  12946. (save-restriction
  12947. (goto-char pos)
  12948. (setq cnt (1+ cnt))
  12949. (org-toggle-tag tag (if off 'off 'on))
  12950. (setq newhead (org-get-heading)))))
  12951. (and agendap (org-agenda-change-all-lines newhead m))))
  12952. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12953. (defun org-tags-completion-function (string predicate &optional flag)
  12954. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12955. (confirm (lambda (x) (stringp (car x)))))
  12956. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12957. (setq s1 (match-string 1 string)
  12958. s2 (match-string 2 string))
  12959. (setq s1 "" s2 string))
  12960. (cond
  12961. ((eq flag nil)
  12962. ;; try completion
  12963. (setq rtn (try-completion s2 ctable confirm))
  12964. (if (stringp rtn)
  12965. (setq rtn
  12966. (concat s1 s2 (substring rtn (length s2))
  12967. (if (and org-add-colon-after-tag-completion
  12968. (assoc rtn ctable))
  12969. ":" ""))))
  12970. rtn)
  12971. ((eq flag t)
  12972. ;; all-completions
  12973. (all-completions s2 ctable confirm)
  12974. )
  12975. ((eq flag 'lambda)
  12976. ;; exact match?
  12977. (assoc s2 ctable)))
  12978. ))
  12979. (defun org-fast-tag-insert (kwd tags face &optional end)
  12980. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12981. (insert (format "%-12s" (concat kwd ":"))
  12982. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12983. (or end "")))
  12984. (defun org-fast-tag-show-exit (flag)
  12985. (save-excursion
  12986. (org-goto-line 3)
  12987. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12988. (replace-match ""))
  12989. (when flag
  12990. (end-of-line 1)
  12991. (org-move-to-column (- (window-width) 19) t)
  12992. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12993. (defun org-set-current-tags-overlay (current prefix)
  12994. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12995. (if (featurep 'xemacs)
  12996. (org-overlay-display org-tags-overlay (concat prefix s)
  12997. 'secondary-selection)
  12998. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12999. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13000. (defvar org-last-tag-selection-key nil)
  13001. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13002. "Fast tag selection with single keys.
  13003. CURRENT is the current list of tags in the headline, INHERITED is the
  13004. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13005. possibly with grouping information. TODO-TABLE is a similar table with
  13006. TODO keywords, should these have keys assigned to them.
  13007. If the keys are nil, a-z are automatically assigned.
  13008. Returns the new tags string, or nil to not change the current settings."
  13009. (let* ((fulltable (append table todo-table))
  13010. (maxlen (apply 'max (mapcar
  13011. (lambda (x)
  13012. (if (stringp (car x)) (string-width (car x)) 0))
  13013. fulltable)))
  13014. (buf (current-buffer))
  13015. (expert (eq org-fast-tag-selection-single-key 'expert))
  13016. (buffer-tags nil)
  13017. (fwidth (+ maxlen 3 1 3))
  13018. (ncol (/ (- (window-width) 4) fwidth))
  13019. (i-face 'org-done)
  13020. (c-face 'org-todo)
  13021. tg cnt e c char c1 c2 ntable tbl rtn
  13022. ov-start ov-end ov-prefix
  13023. (exit-after-next org-fast-tag-selection-single-key)
  13024. (done-keywords org-done-keywords)
  13025. groups ingroup)
  13026. (save-excursion
  13027. (beginning-of-line 1)
  13028. (if (looking-at
  13029. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13030. (setq ov-start (match-beginning 1)
  13031. ov-end (match-end 1)
  13032. ov-prefix "")
  13033. (setq ov-start (1- (point-at-eol))
  13034. ov-end (1+ ov-start))
  13035. (skip-chars-forward "^\n\r")
  13036. (setq ov-prefix
  13037. (concat
  13038. (buffer-substring (1- (point)) (point))
  13039. (if (> (current-column) org-tags-column)
  13040. " "
  13041. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13042. (move-overlay org-tags-overlay ov-start ov-end)
  13043. (save-window-excursion
  13044. (if expert
  13045. (set-buffer (get-buffer-create " *Org tags*"))
  13046. (delete-other-windows)
  13047. (split-window-vertically)
  13048. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13049. (erase-buffer)
  13050. (org-set-local 'org-done-keywords done-keywords)
  13051. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13052. (org-fast-tag-insert "Current" current c-face "\n\n")
  13053. (org-fast-tag-show-exit exit-after-next)
  13054. (org-set-current-tags-overlay current ov-prefix)
  13055. (setq tbl fulltable char ?a cnt 0)
  13056. (while (setq e (pop tbl))
  13057. (cond
  13058. ((equal (car e) :startgroup)
  13059. (push '() groups) (setq ingroup t)
  13060. (when (not (= cnt 0))
  13061. (setq cnt 0)
  13062. (insert "\n"))
  13063. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13064. ((equal (car e) :endgroup)
  13065. (setq ingroup nil cnt 0)
  13066. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13067. ((equal e '(:newline))
  13068. (when (not (= cnt 0))
  13069. (setq cnt 0)
  13070. (insert "\n")
  13071. (setq e (car tbl))
  13072. (while (equal (car tbl) '(:newline))
  13073. (insert "\n")
  13074. (setq tbl (cdr tbl)))))
  13075. (t
  13076. (setq tg (copy-sequence (car e)) c2 nil)
  13077. (if (cdr e)
  13078. (setq c (cdr e))
  13079. ;; automatically assign a character.
  13080. (setq c1 (string-to-char
  13081. (downcase (substring
  13082. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13083. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13084. (while (or (rassoc char ntable) (rassoc char table))
  13085. (setq char (1+ char)))
  13086. (setq c2 c1))
  13087. (setq c (or c2 char)))
  13088. (if ingroup (push tg (car groups)))
  13089. (setq tg (org-add-props tg nil 'face
  13090. (cond
  13091. ((not (assoc tg table))
  13092. (org-get-todo-face tg))
  13093. ((member tg current) c-face)
  13094. ((member tg inherited) i-face))))
  13095. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13096. (insert "[" c "] " tg (make-string
  13097. (- fwidth 4 (length tg)) ?\ ))
  13098. (push (cons tg c) ntable)
  13099. (when (= (setq cnt (1+ cnt)) ncol)
  13100. (insert "\n")
  13101. (if ingroup (insert " "))
  13102. (setq cnt 0)))))
  13103. (setq ntable (nreverse ntable))
  13104. (insert "\n")
  13105. (goto-char (point-min))
  13106. (if (not expert) (org-fit-window-to-buffer))
  13107. (setq rtn
  13108. (catch 'exit
  13109. (while t
  13110. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13111. (if (not groups) "no " "")
  13112. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13113. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13114. (setq org-last-tag-selection-key c)
  13115. (cond
  13116. ((= c ?\r) (throw 'exit t))
  13117. ((= c ?!)
  13118. (setq groups (not groups))
  13119. (goto-char (point-min))
  13120. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13121. ((= c ?\C-c)
  13122. (if (not expert)
  13123. (org-fast-tag-show-exit
  13124. (setq exit-after-next (not exit-after-next)))
  13125. (setq expert nil)
  13126. (delete-other-windows)
  13127. (set-window-buffer (split-window-vertically) " *Org tags*")
  13128. (org-switch-to-buffer-other-window " *Org tags*")
  13129. (org-fit-window-to-buffer)))
  13130. ((or (= c ?\C-g)
  13131. (and (= c ?q) (not (rassoc c ntable))))
  13132. (org-detach-overlay org-tags-overlay)
  13133. (setq quit-flag t))
  13134. ((= c ?\ )
  13135. (setq current nil)
  13136. (if exit-after-next (setq exit-after-next 'now)))
  13137. ((= c ?\t)
  13138. (condition-case nil
  13139. (setq tg (org-icompleting-read
  13140. "Tag: "
  13141. (or buffer-tags
  13142. (with-current-buffer buf
  13143. (org-get-buffer-tags)))))
  13144. (quit (setq tg "")))
  13145. (when (string-match "\\S-" tg)
  13146. (add-to-list 'buffer-tags (list tg))
  13147. (if (member tg current)
  13148. (setq current (delete tg current))
  13149. (push tg current)))
  13150. (if exit-after-next (setq exit-after-next 'now)))
  13151. ((setq e (rassoc c todo-table) tg (car e))
  13152. (with-current-buffer buf
  13153. (save-excursion (org-todo tg)))
  13154. (if exit-after-next (setq exit-after-next 'now)))
  13155. ((setq e (rassoc c ntable) tg (car e))
  13156. (if (member tg current)
  13157. (setq current (delete tg current))
  13158. (loop for g in groups do
  13159. (if (member tg g)
  13160. (mapc (lambda (x)
  13161. (setq current (delete x current)))
  13162. g)))
  13163. (push tg current))
  13164. (if exit-after-next (setq exit-after-next 'now))))
  13165. ;; Create a sorted list
  13166. (setq current
  13167. (sort current
  13168. (lambda (a b)
  13169. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13170. (if (eq exit-after-next 'now) (throw 'exit t))
  13171. (goto-char (point-min))
  13172. (beginning-of-line 2)
  13173. (delete-region (point) (point-at-eol))
  13174. (org-fast-tag-insert "Current" current c-face)
  13175. (org-set-current-tags-overlay current ov-prefix)
  13176. (while (re-search-forward
  13177. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13178. (setq tg (match-string 1))
  13179. (add-text-properties
  13180. (match-beginning 1) (match-end 1)
  13181. (list 'face
  13182. (cond
  13183. ((member tg current) c-face)
  13184. ((member tg inherited) i-face)
  13185. (t (get-text-property (match-beginning 1) 'face))))))
  13186. (goto-char (point-min)))))
  13187. (org-detach-overlay org-tags-overlay)
  13188. (if rtn
  13189. (mapconcat 'identity current ":")
  13190. nil))))
  13191. (defun org-get-tags-string ()
  13192. "Get the TAGS string in the current headline."
  13193. (unless (org-at-heading-p t)
  13194. (error "Not on a heading"))
  13195. (save-excursion
  13196. (beginning-of-line 1)
  13197. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13198. (org-match-string-no-properties 1)
  13199. "")))
  13200. (defun org-get-tags ()
  13201. "Get the list of tags specified in the current headline."
  13202. (org-split-string (org-get-tags-string) ":"))
  13203. (defun org-get-buffer-tags ()
  13204. "Get a table of all tags used in the buffer, for completion."
  13205. (let (tags)
  13206. (save-excursion
  13207. (goto-char (point-min))
  13208. (while (re-search-forward
  13209. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13210. (when (equal (char-after (point-at-bol 0)) ?*)
  13211. (mapc (lambda (x) (add-to-list 'tags x))
  13212. (org-split-string (org-match-string-no-properties 1) ":")))))
  13213. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13214. (mapcar 'list tags)))
  13215. ;;;; The mapping API
  13216. (defun org-map-entries (func &optional match scope &rest skip)
  13217. "Call FUNC at each headline selected by MATCH in SCOPE.
  13218. FUNC is a function or a lisp form. The function will be called without
  13219. arguments, with the cursor positioned at the beginning of the headline.
  13220. The return values of all calls to the function will be collected and
  13221. returned as a list.
  13222. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13223. does not need to preserve point. After evaluation, the cursor will be
  13224. moved to the end of the line (presumably of the headline of the
  13225. processed entry) and search continues from there. Under some
  13226. circumstances, this may not produce the wanted results. For example,
  13227. if you have removed (e.g. archived) the current (sub)tree it could
  13228. mean that the next entry will be skipped entirely. In such cases, you
  13229. can specify the position from where search should continue by making
  13230. FUNC set the variable `org-map-continue-from' to the desired buffer
  13231. position.
  13232. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13233. Only headlines that are matched by this query will be considered during
  13234. the iteration. When MATCH is nil or t, all headlines will be
  13235. visited by the iteration.
  13236. SCOPE determines the scope of this command. It can be any of:
  13237. nil The current buffer, respecting the restriction if any
  13238. tree The subtree started with the entry at point
  13239. region The entries within the active region, if any
  13240. region-start-level
  13241. The entries within the active region, but only those at
  13242. the same level than the first one.
  13243. file The current buffer, without restriction
  13244. file-with-archives
  13245. The current buffer, and any archives associated with it
  13246. agenda All agenda files
  13247. agenda-with-archives
  13248. All agenda files with any archive files associated with them
  13249. \(file1 file2 ...)
  13250. If this is a list, all files in the list will be scanned
  13251. The remaining args are treated as settings for the skipping facilities of
  13252. the scanner. The following items can be given here:
  13253. archive skip trees with the archive tag.
  13254. comment skip trees with the COMMENT keyword
  13255. function or Emacs Lisp form:
  13256. will be used as value for `org-agenda-skip-function', so whenever
  13257. the function returns t, FUNC will not be called for that
  13258. entry and search will continue from the point where the
  13259. function leaves it.
  13260. If your function needs to retrieve the tags including inherited tags
  13261. at the *current* entry, you can use the value of the variable
  13262. `org-scanner-tags' which will be much faster than getting the value
  13263. with `org-get-tags-at'. If your function gets properties with
  13264. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13265. to t around the call to `org-entry-properties' to get the same speedup.
  13266. Note that if your function moves around to retrieve tags and properties at
  13267. a *different* entry, you cannot use these techniques."
  13268. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13269. (not (org-region-active-p)))
  13270. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13271. (org-agenda-skip-archived-trees (memq 'archive skip))
  13272. (org-agenda-skip-comment-trees (memq 'comment skip))
  13273. (org-agenda-skip-function
  13274. (car (org-delete-all '(comment archive) skip)))
  13275. (org-tags-match-list-sublevels t)
  13276. (start-level (eq scope 'region-start-level))
  13277. matcher file res
  13278. org-todo-keywords-for-agenda
  13279. org-done-keywords-for-agenda
  13280. org-todo-keyword-alist-for-agenda
  13281. org-drawers-for-agenda
  13282. org-tag-alist-for-agenda
  13283. todo-only)
  13284. (cond
  13285. ((eq match t) (setq matcher t))
  13286. ((eq match nil) (setq matcher t))
  13287. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13288. (save-excursion
  13289. (save-restriction
  13290. (cond ((eq scope 'tree)
  13291. (org-back-to-heading t)
  13292. (org-narrow-to-subtree)
  13293. (setq scope nil))
  13294. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13295. (org-region-active-p))
  13296. ;; If needed, set start-level to a string like "2"
  13297. (when start-level
  13298. (save-excursion
  13299. (goto-char (region-beginning))
  13300. (unless (org-at-heading-p) (outline-next-heading))
  13301. (setq start-level (org-current-level))))
  13302. (narrow-to-region (region-beginning)
  13303. (save-excursion
  13304. (goto-char (region-end))
  13305. (unless (and (bolp) (org-at-heading-p))
  13306. (outline-next-heading))
  13307. (point)))
  13308. (setq scope nil)))
  13309. (if (not scope)
  13310. (progn
  13311. (org-agenda-prepare-buffers
  13312. (list (buffer-file-name (current-buffer))))
  13313. (setq res (org-scan-tags func matcher todo-only start-level)))
  13314. ;; Get the right scope
  13315. (cond
  13316. ((and scope (listp scope) (symbolp (car scope)))
  13317. (setq scope (eval scope)))
  13318. ((eq scope 'agenda)
  13319. (setq scope (org-agenda-files t)))
  13320. ((eq scope 'agenda-with-archives)
  13321. (setq scope (org-agenda-files t))
  13322. (setq scope (org-add-archive-files scope)))
  13323. ((eq scope 'file)
  13324. (setq scope (list (buffer-file-name))))
  13325. ((eq scope 'file-with-archives)
  13326. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13327. (org-agenda-prepare-buffers scope)
  13328. (while (setq file (pop scope))
  13329. (with-current-buffer (org-find-base-buffer-visiting file)
  13330. (save-excursion
  13331. (save-restriction
  13332. (widen)
  13333. (goto-char (point-min))
  13334. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13335. res)))
  13336. ;;;; Properties
  13337. ;;; Setting and retrieving properties
  13338. (defconst org-special-properties
  13339. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13340. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13341. "The special properties valid in Org-mode.
  13342. These are properties that are not defined in the property drawer,
  13343. but in some other way.")
  13344. (defconst org-default-properties
  13345. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13346. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13347. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13348. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13349. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13350. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13351. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13352. "Some properties that are used by Org-mode for various purposes.
  13353. Being in this list makes sure that they are offered for completion.")
  13354. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13355. "Regular expression matching the first line of a property drawer.")
  13356. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13357. "Regular expression matching the last line of a property drawer.")
  13358. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13359. "Regular expression matching the first line of a property drawer.")
  13360. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13361. "Regular expression matching the first line of a property drawer.")
  13362. (defconst org-property-drawer-re
  13363. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13364. org-property-end-re "\\)\n?")
  13365. "Matches an entire property drawer.")
  13366. (defconst org-clock-drawer-re
  13367. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13368. org-property-end-re "\\)\n?")
  13369. "Matches an entire clock drawer.")
  13370. (defsubst org-re-property (property)
  13371. "Return a regexp matching a PROPERTY line.
  13372. Match group 1 will be set to the value."
  13373. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13374. (defsubst org-re-property-keyword (property)
  13375. "Return a regexp matching a PROPERTY line, possibly with no
  13376. value for the property."
  13377. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13378. (defun org-property-action ()
  13379. "Do an action on properties."
  13380. (interactive)
  13381. (let (c)
  13382. (org-at-property-p)
  13383. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13384. (setq c (read-char-exclusive))
  13385. (cond
  13386. ((equal c ?s)
  13387. (call-interactively 'org-set-property))
  13388. ((equal c ?d)
  13389. (call-interactively 'org-delete-property))
  13390. ((equal c ?D)
  13391. (call-interactively 'org-delete-property-globally))
  13392. ((equal c ?c)
  13393. (call-interactively 'org-compute-property-at-point))
  13394. (t (error "No such property action %c" c)))))
  13395. (defun org-inc-effort ()
  13396. "Increment the value of the effort property in the current entry."
  13397. (interactive)
  13398. (org-set-effort nil t))
  13399. (defvar org-clock-effort) ;; Defined in org-clock.el
  13400. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13401. (defun org-set-effort (&optional value increment)
  13402. "Set the effort property of the current entry.
  13403. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13404. 10th allowed value.
  13405. When INCREMENT is non-nil, set the property to the next allowed value."
  13406. (interactive "P")
  13407. (if (equal value 0) (setq value 10))
  13408. (let* ((completion-ignore-case t)
  13409. (prop org-effort-property)
  13410. (cur (org-entry-get nil prop))
  13411. (allowed (org-property-get-allowed-values nil prop 'table))
  13412. (existing (mapcar 'list (org-property-values prop)))
  13413. (heading (nth 4 (org-heading-components)))
  13414. rpl
  13415. (val (cond
  13416. ((stringp value) value)
  13417. ((and allowed (integerp value))
  13418. (or (car (nth (1- value) allowed))
  13419. (car (org-last allowed))))
  13420. ((and allowed increment)
  13421. (or (caadr (member (list cur) allowed))
  13422. (error "Allowed effort values are not set")))
  13423. (allowed
  13424. (message "Select 1-9,0, [RET%s]: %s"
  13425. (if cur (concat "=" cur) "")
  13426. (mapconcat 'car allowed " "))
  13427. (setq rpl (read-char-exclusive))
  13428. (if (equal rpl ?\r)
  13429. cur
  13430. (setq rpl (- rpl ?0))
  13431. (if (equal rpl 0) (setq rpl 10))
  13432. (if (and (> rpl 0) (<= rpl (length allowed)))
  13433. (car (nth (1- rpl) allowed))
  13434. (org-completing-read "Effort: " allowed nil))))
  13435. (t
  13436. (let (org-completion-use-ido org-completion-use-iswitchb)
  13437. (org-completing-read
  13438. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13439. (concat "[" cur "]") "")
  13440. ": ")
  13441. existing nil nil "" nil cur))))))
  13442. (unless (equal (org-entry-get nil prop) val)
  13443. (org-entry-put nil prop val))
  13444. (save-excursion
  13445. (org-back-to-heading t)
  13446. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13447. (when (string= heading org-clock-current-task)
  13448. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13449. (org-clock-update-mode-line))
  13450. (message "%s is now %s" prop val)))
  13451. (defun org-at-property-p ()
  13452. "Is cursor inside a property drawer?"
  13453. (save-excursion
  13454. (beginning-of-line 1)
  13455. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13456. (save-match-data ;; Used by calling procedures
  13457. (let ((p (point))
  13458. (range (unless (org-before-first-heading-p)
  13459. (org-get-property-block))))
  13460. (and range (<= (car range) p) (< p (cdr range))))))))
  13461. (defun org-get-property-block (&optional beg end force)
  13462. "Return the (beg . end) range of the body of the property drawer.
  13463. BEG and END are the beginning and end of the current subtree, or of
  13464. the part before the first headline. If they are not given, they will
  13465. be found. If the drawer does not exist and FORCE is non-nil, create
  13466. the drawer."
  13467. (catch 'exit
  13468. (save-excursion
  13469. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13470. (progn (org-back-to-heading t) (point))))
  13471. (end (or end (and (not (outline-next-heading)) (point-max))
  13472. (point))))
  13473. (goto-char beg)
  13474. (if (re-search-forward org-property-start-re end t)
  13475. (setq beg (1+ (match-end 0)))
  13476. (if force
  13477. (save-excursion
  13478. (org-insert-property-drawer)
  13479. (setq end (progn (outline-next-heading) (point))))
  13480. (throw 'exit nil))
  13481. (goto-char beg)
  13482. (if (re-search-forward org-property-start-re end t)
  13483. (setq beg (1+ (match-end 0)))))
  13484. (if (re-search-forward org-property-end-re end t)
  13485. (setq end (match-beginning 0))
  13486. (or force (throw 'exit nil))
  13487. (goto-char beg)
  13488. (setq end beg)
  13489. (org-indent-line)
  13490. (insert ":END:\n"))
  13491. (cons beg end)))))
  13492. (defun org-entry-properties (&optional pom which specific)
  13493. "Get all properties of the entry at point-or-marker POM.
  13494. This includes the TODO keyword, the tags, time strings for deadline,
  13495. scheduled, and clocking, and any additional properties defined in the
  13496. entry. The return value is an alist, keys may occur multiple times
  13497. if the property key was used several times.
  13498. POM may also be nil, in which case the current entry is used.
  13499. If WHICH is nil or `all', get all properties. If WHICH is
  13500. `special' or `standard', only get that subclass. If WHICH
  13501. is a string only get exactly this property. SPECIFIC can be a string, the
  13502. specific property we are interested in. Specifying it can speed
  13503. things up because then unnecessary parsing is avoided."
  13504. (setq which (or which 'all))
  13505. (org-with-point-at pom
  13506. (let ((clockstr (substring org-clock-string 0 -1))
  13507. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13508. (case-fold-search nil)
  13509. beg end range props sum-props key key1 value string clocksum clocksumt)
  13510. (save-excursion
  13511. (when (condition-case nil
  13512. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13513. (error nil))
  13514. (setq beg (point))
  13515. (setq sum-props (get-text-property (point) 'org-summaries))
  13516. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13517. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13518. (outline-next-heading)
  13519. (setq end (point))
  13520. (when (memq which '(all special))
  13521. ;; Get the special properties, like TODO and tags
  13522. (goto-char beg)
  13523. (when (and (or (not specific) (string= specific "TODO"))
  13524. (looking-at org-todo-line-regexp) (match-end 2))
  13525. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13526. (when (and (or (not specific) (string= specific "PRIORITY"))
  13527. (looking-at org-priority-regexp))
  13528. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13529. (when (or (not specific) (string= specific "FILE"))
  13530. (push (cons "FILE" buffer-file-name) props))
  13531. (when (and (or (not specific) (string= specific "TAGS"))
  13532. (setq value (org-get-tags-string))
  13533. (string-match "\\S-" value))
  13534. (push (cons "TAGS" value) props))
  13535. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13536. (setq value (org-get-tags-at)))
  13537. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13538. ":"))
  13539. props))
  13540. (when (or (not specific) (string= specific "BLOCKED"))
  13541. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13542. (when (or (not specific)
  13543. (member specific
  13544. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13545. "TIMESTAMP" "TIMESTAMP_IA")))
  13546. (catch 'match
  13547. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13548. (setq key (if (match-end 1)
  13549. (substring (org-match-string-no-properties 1)
  13550. 0 -1))
  13551. string (if (equal key clockstr)
  13552. (org-trim
  13553. (buffer-substring-no-properties
  13554. (match-beginning 3) (goto-char
  13555. (point-at-eol))))
  13556. (substring (org-match-string-no-properties 3)
  13557. 1 -1)))
  13558. ;; Get the correct property name from the key. This is
  13559. ;; necessary if the user has configured time keywords.
  13560. (setq key1 (concat key ":"))
  13561. (cond
  13562. ((not key)
  13563. (setq key
  13564. (if (= (char-after (match-beginning 3)) ?\[)
  13565. "TIMESTAMP_IA" "TIMESTAMP")))
  13566. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13567. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13568. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13569. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13570. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13571. (progn
  13572. (push (cons key string) props)
  13573. ;; no need to search further if match is found
  13574. (throw 'match t))
  13575. (when (or (equal key "CLOCK") (not (assoc key props)))
  13576. (push (cons key string) props)))))))
  13577. (when (memq which '(all standard))
  13578. ;; Get the standard properties, like :PROP: ...
  13579. (setq range (org-get-property-block beg end))
  13580. (when range
  13581. (goto-char (car range))
  13582. (while (re-search-forward
  13583. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13584. (cdr range) t)
  13585. (setq key (org-match-string-no-properties 1)
  13586. value (org-trim (or (org-match-string-no-properties 2) "")))
  13587. (unless (member key excluded)
  13588. (push (cons key (or value "")) props)))))
  13589. (if clocksum
  13590. (push (cons "CLOCKSUM"
  13591. (org-columns-number-to-string (/ (float clocksum) 60.)
  13592. 'add_times))
  13593. props))
  13594. (if clocksumt
  13595. (push (cons "CLOCKSUM_T"
  13596. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13597. 'add_times))
  13598. props))
  13599. (unless (assoc "CATEGORY" props)
  13600. (push (cons "CATEGORY" (org-get-category)) props))
  13601. (append sum-props (nreverse props)))))))
  13602. (defun org-entry-get (pom property &optional inherit literal-nil)
  13603. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13604. If INHERIT is non-nil and the entry does not have the property,
  13605. then also check higher levels of the hierarchy.
  13606. If INHERIT is the symbol `selective', use inheritance only if the setting
  13607. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13608. If the property is present but empty, the return value is the empty string.
  13609. If the property is not present at all, nil is returned.
  13610. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13611. do not interpret it as the list atom nil. This is used for inheritance
  13612. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13613. (org-with-point-at pom
  13614. (if (and inherit (if (eq inherit 'selective)
  13615. (org-property-inherit-p property)
  13616. t))
  13617. (org-entry-get-with-inheritance property literal-nil)
  13618. (if (member property org-special-properties)
  13619. ;; We need a special property. Use `org-entry-properties' to
  13620. ;; retrieve it, but specify the wanted property
  13621. (cdr (assoc property (org-entry-properties nil 'special property)))
  13622. (let ((range (org-get-property-block)))
  13623. (when (and range (not (eq (car range) (cdr range))))
  13624. (let* ((props (list (or (assoc property org-file-properties)
  13625. (assoc property org-global-properties)
  13626. (assoc property org-global-properties-fixed))))
  13627. (ap (lambda (key)
  13628. (when (re-search-forward
  13629. (org-re-property key) (cdr range) t)
  13630. (setq props
  13631. (org-update-property-plist
  13632. key
  13633. (if (match-end 1)
  13634. (org-match-string-no-properties 1) "")
  13635. props)))))
  13636. val)
  13637. (goto-char (car range))
  13638. (funcall ap property)
  13639. (goto-char (car range))
  13640. (while (funcall ap (concat property "+")))
  13641. (setq val (cdr (assoc property props)))
  13642. (when val (if literal-nil val (org-not-nil val))))))))))
  13643. (defun org-property-or-variable-value (var &optional inherit)
  13644. "Check if there is a property fixing the value of VAR.
  13645. If yes, return this value. If not, return the current value of the variable."
  13646. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13647. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13648. (read prop)
  13649. (symbol-value var))))
  13650. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13651. "Delete the property PROPERTY from entry at point-or-marker POM.
  13652. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13653. an empty drawer to delete."
  13654. (org-with-point-at pom
  13655. (if (member property org-special-properties)
  13656. nil ; cannot delete these properties.
  13657. (let ((range (org-get-property-block)))
  13658. (if (and range
  13659. (goto-char (car range))
  13660. (re-search-forward
  13661. (org-re-property property)
  13662. (cdr range) t))
  13663. (progn
  13664. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13665. (and delete-empty-drawer
  13666. (org-remove-empty-drawer-at
  13667. delete-empty-drawer (car range)))
  13668. t)
  13669. nil)))))
  13670. ;; Multi-values properties are properties that contain multiple values
  13671. ;; These values are assumed to be single words, separated by whitespace.
  13672. (defun org-entry-add-to-multivalued-property (pom property value)
  13673. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13674. (let* ((old (org-entry-get pom property))
  13675. (values (and old (org-split-string old "[ \t]"))))
  13676. (setq value (org-entry-protect-space value))
  13677. (unless (member value values)
  13678. (setq values (cons value values))
  13679. (org-entry-put pom property
  13680. (mapconcat 'identity values " ")))))
  13681. (defun org-entry-remove-from-multivalued-property (pom property value)
  13682. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13683. (let* ((old (org-entry-get pom property))
  13684. (values (and old (org-split-string old "[ \t]"))))
  13685. (setq value (org-entry-protect-space value))
  13686. (when (member value values)
  13687. (setq values (delete value values))
  13688. (org-entry-put pom property
  13689. (mapconcat 'identity values " ")))))
  13690. (defun org-entry-member-in-multivalued-property (pom property value)
  13691. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13692. (let* ((old (org-entry-get pom property))
  13693. (values (and old (org-split-string old "[ \t]"))))
  13694. (setq value (org-entry-protect-space value))
  13695. (member value values)))
  13696. (defun org-entry-get-multivalued-property (pom property)
  13697. "Return a list of values in a multivalued property."
  13698. (let* ((value (org-entry-get pom property))
  13699. (values (and value (org-split-string value "[ \t]"))))
  13700. (mapcar 'org-entry-restore-space values)))
  13701. (defun org-entry-put-multivalued-property (pom property &rest values)
  13702. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13703. VALUES should be a list of strings. Spaces will be protected."
  13704. (org-entry-put pom property
  13705. (mapconcat 'org-entry-protect-space values " "))
  13706. (let* ((value (org-entry-get pom property))
  13707. (values (and value (org-split-string value "[ \t]"))))
  13708. (mapcar 'org-entry-restore-space values)))
  13709. (defun org-entry-protect-space (s)
  13710. "Protect spaces and newline in string S."
  13711. (while (string-match " " s)
  13712. (setq s (replace-match "%20" t t s)))
  13713. (while (string-match "\n" s)
  13714. (setq s (replace-match "%0A" t t s)))
  13715. s)
  13716. (defun org-entry-restore-space (s)
  13717. "Restore spaces and newline in string S."
  13718. (while (string-match "%20" s)
  13719. (setq s (replace-match " " t t s)))
  13720. (while (string-match "%0A" s)
  13721. (setq s (replace-match "\n" t t s)))
  13722. s)
  13723. (defvar org-entry-property-inherited-from (make-marker)
  13724. "Marker pointing to the entry from where a property was inherited.
  13725. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13726. location of the entry where the inheritance search matched. If there was
  13727. no match, the marker will point nowhere.
  13728. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13729. is set.")
  13730. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13731. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13732. The search will stop at the first ancestor which has the property defined.
  13733. If the value found is \"nil\", return nil to show that the property
  13734. should be considered as undefined (this is the meaning of nil here).
  13735. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13736. (move-marker org-entry-property-inherited-from nil)
  13737. (let (tmp)
  13738. (save-excursion
  13739. (save-restriction
  13740. (widen)
  13741. (catch 'ex
  13742. (while t
  13743. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13744. (or (ignore-errors (org-back-to-heading t))
  13745. (goto-char (point-min)))
  13746. (move-marker org-entry-property-inherited-from (point))
  13747. (throw 'ex tmp))
  13748. (or (ignore-errors (org-up-heading-safe))
  13749. (throw 'ex nil))))))
  13750. (setq tmp (or tmp
  13751. (cdr (assoc property org-file-properties))
  13752. (cdr (assoc property org-global-properties))
  13753. (cdr (assoc property org-global-properties-fixed))))
  13754. (if literal-nil tmp (org-not-nil tmp))))
  13755. (defvar org-property-changed-functions nil
  13756. "Hook called when the value of a property has changed.
  13757. Each hook function should accept two arguments, the name of the property
  13758. and the new value.")
  13759. (defun org-entry-put (pom property value)
  13760. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13761. (org-with-point-at pom
  13762. (org-back-to-heading t)
  13763. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13764. range)
  13765. (cond
  13766. ((equal property "TODO")
  13767. (when (and (stringp value) (string-match "\\S-" value)
  13768. (not (member value org-todo-keywords-1)))
  13769. (error "\"%s\" is not a valid TODO state" value))
  13770. (if (or (not value)
  13771. (not (string-match "\\S-" value)))
  13772. (setq value 'none))
  13773. (org-todo value)
  13774. (org-set-tags nil 'align))
  13775. ((equal property "PRIORITY")
  13776. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13777. (string-to-char value) ?\ ))
  13778. (org-set-tags nil 'align))
  13779. ((equal property "CLOCKSUM")
  13780. (if (not (re-search-forward
  13781. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13782. (error "Cannot find a clock log")
  13783. (goto-char (- (match-end 1) 2))
  13784. (cond
  13785. ((eq value 'earlier) (org-timestamp-down))
  13786. ((eq value 'later) (org-timestamp-up)))
  13787. (org-clock-sum-current-item)))
  13788. ((equal property "SCHEDULED")
  13789. (if (re-search-forward org-scheduled-time-regexp end t)
  13790. (cond
  13791. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13792. ((eq value 'later) (org-timestamp-change 1 'day))
  13793. (t (call-interactively 'org-schedule)))
  13794. (call-interactively 'org-schedule)))
  13795. ((equal property "DEADLINE")
  13796. (if (re-search-forward org-deadline-time-regexp end t)
  13797. (cond
  13798. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13799. ((eq value 'later) (org-timestamp-change 1 'day))
  13800. (t (call-interactively 'org-deadline)))
  13801. (call-interactively 'org-deadline)))
  13802. ((member property org-special-properties)
  13803. (error "The %s property can not yet be set with `org-entry-put'"
  13804. property))
  13805. (t ; a non-special property
  13806. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13807. (setq range (org-get-property-block beg end 'force))
  13808. (goto-char (car range))
  13809. (if (re-search-forward
  13810. (org-re-property-keyword property) (cdr range) t)
  13811. (progn
  13812. (delete-region (match-beginning 0) (match-end 0))
  13813. (goto-char (match-beginning 0)))
  13814. (goto-char (cdr range))
  13815. (insert "\n")
  13816. (backward-char 1)
  13817. (org-indent-line))
  13818. (insert ":" property ":")
  13819. (and value (insert " " value))
  13820. (org-indent-line)))))
  13821. (run-hook-with-args 'org-property-changed-functions property value)))
  13822. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13823. "Get all property keys in the current buffer.
  13824. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13825. like tags and TODO state.
  13826. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13827. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13828. and others.
  13829. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13830. formats in the current buffer."
  13831. (let (rtn range cfmt s p)
  13832. (save-excursion
  13833. (save-restriction
  13834. (widen)
  13835. (goto-char (point-min))
  13836. (while (re-search-forward org-property-start-re nil t)
  13837. (setq range (org-get-property-block))
  13838. (goto-char (car range))
  13839. (while (re-search-forward
  13840. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13841. (cdr range) t)
  13842. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13843. (outline-next-heading))))
  13844. (when include-specials
  13845. (setq rtn (append org-special-properties rtn)))
  13846. (when include-defaults
  13847. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13848. (add-to-list 'rtn org-effort-property))
  13849. (when include-columns
  13850. (save-excursion
  13851. (save-restriction
  13852. (widen)
  13853. (goto-char (point-min))
  13854. (while (re-search-forward
  13855. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13856. nil t)
  13857. (setq cfmt (match-string 2) s 0)
  13858. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13859. cfmt s)
  13860. (setq s (match-end 0)
  13861. p (match-string 1 cfmt))
  13862. (unless (or (equal p "ITEM")
  13863. (member p org-special-properties))
  13864. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13865. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13866. (defun org-property-values (key)
  13867. "Return a list of all values of property KEY in the current buffer."
  13868. (save-excursion
  13869. (save-restriction
  13870. (widen)
  13871. (goto-char (point-min))
  13872. (let ((re (org-re-property key))
  13873. values)
  13874. (while (re-search-forward re nil t)
  13875. (add-to-list 'values (org-trim (match-string 1))))
  13876. (delete "" values)))))
  13877. (defun org-insert-property-drawer ()
  13878. "Insert a property drawer into the current entry."
  13879. (org-back-to-heading t)
  13880. (looking-at org-outline-regexp)
  13881. (let ((indent (if org-adapt-indentation
  13882. (- (match-end 0) (match-beginning 0))
  13883. 0))
  13884. (beg (point))
  13885. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13886. end hiddenp)
  13887. (outline-next-heading)
  13888. (setq end (point))
  13889. (goto-char beg)
  13890. (while (re-search-forward re end t))
  13891. (setq hiddenp (outline-invisible-p))
  13892. (end-of-line 1)
  13893. (and (equal (char-after) ?\n) (forward-char 1))
  13894. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13895. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13896. ;; just skip this line
  13897. (beginning-of-line 2)
  13898. ;; Drawer start, find the end
  13899. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13900. (beginning-of-line 1)))
  13901. (org-skip-over-state-notes)
  13902. (skip-chars-backward " \t\n\r")
  13903. (if (eq (char-before) ?*) (forward-char 1))
  13904. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13905. (beginning-of-line 0)
  13906. (org-indent-to-column indent)
  13907. (beginning-of-line 2)
  13908. (org-indent-to-column indent)
  13909. (beginning-of-line 0)
  13910. (if hiddenp
  13911. (save-excursion
  13912. (org-back-to-heading t)
  13913. (hide-entry))
  13914. (org-flag-drawer t))))
  13915. (defun org-insert-drawer (&optional arg drawer)
  13916. "Insert a drawer at point.
  13917. Optional argument DRAWER, when non-nil, is a string representing
  13918. drawer's name. Otherwise, the user is prompted for a name.
  13919. If a region is active, insert the drawer around that region
  13920. instead.
  13921. Point is left between drawer's boundaries."
  13922. (interactive "P")
  13923. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13924. "LOGBOOK"))
  13925. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13926. ;; internally by Org. They are meant to be inserted
  13927. ;; automatically.
  13928. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13929. ;; Remove system drawers from list. Note: For some reason,
  13930. ;; `org-completing-read' ignores the predicate while
  13931. ;; `completing-read' handles it fine.
  13932. (drawer (if arg "PROPERTIES"
  13933. (or drawer
  13934. (completing-read
  13935. "Drawer: " org-drawers
  13936. (lambda (d) (not (member d system-drawers))))))))
  13937. (cond
  13938. ;; With C-u, fall back on `org-insert-property-drawer'
  13939. (arg (org-insert-property-drawer))
  13940. ;; With an active region, insert a drawer at point.
  13941. ((not (org-region-active-p))
  13942. (progn
  13943. (unless (bolp) (insert "\n"))
  13944. (insert (format ":%s:\n\n:END:\n" drawer))
  13945. (forward-line -2)))
  13946. ;; Otherwise, insert the drawer at point
  13947. (t
  13948. (let ((rbeg (region-beginning))
  13949. (rend (copy-marker (region-end))))
  13950. (unwind-protect
  13951. (progn
  13952. (goto-char rbeg)
  13953. (beginning-of-line)
  13954. (when (save-excursion
  13955. (re-search-forward org-outline-regexp-bol rend t))
  13956. (error "Drawers cannot contain headlines"))
  13957. ;; Position point at the beginning of the first
  13958. ;; non-blank line in region. Insert drawer's opening
  13959. ;; there, then indent it.
  13960. (org-skip-whitespace)
  13961. (beginning-of-line)
  13962. (insert ":" drawer ":\n")
  13963. (forward-line -1)
  13964. (indent-for-tab-command)
  13965. ;; Move point to the beginning of the first blank line
  13966. ;; after the last non-blank line in region. Insert
  13967. ;; drawer's closing, then indent it.
  13968. (goto-char rend)
  13969. (skip-chars-backward " \r\t\n")
  13970. (insert "\n:END:")
  13971. (deactivate-mark t)
  13972. (indent-for-tab-command)
  13973. (unless (eolp) (insert "\n")))
  13974. ;; Clear marker, whatever the outcome of insertion is.
  13975. (set-marker rend nil)))))))
  13976. (defvar org-property-set-functions-alist nil
  13977. "Property set function alist.
  13978. Each entry should have the following format:
  13979. (PROPERTY . READ-FUNCTION)
  13980. The read function will be called with the same argument as
  13981. `org-completing-read'.")
  13982. (defun org-set-property-function (property)
  13983. "Get the function that should be used to set PROPERTY.
  13984. This is computed according to `org-property-set-functions-alist'."
  13985. (or (cdr (assoc property org-property-set-functions-alist))
  13986. 'org-completing-read))
  13987. (defun org-read-property-value (property)
  13988. "Read PROPERTY value from user."
  13989. (let* ((completion-ignore-case t)
  13990. (allowed (org-property-get-allowed-values nil property 'table))
  13991. (cur (org-entry-get nil property))
  13992. (prompt (concat property " value"
  13993. (if (and cur (string-match "\\S-" cur))
  13994. (concat " [" cur "]") "") ": "))
  13995. (set-function (org-set-property-function property))
  13996. (val (if allowed
  13997. (funcall set-function prompt allowed nil
  13998. (not (get-text-property 0 'org-unrestricted
  13999. (caar allowed))))
  14000. (let (org-completion-use-ido org-completion-use-iswitchb)
  14001. (funcall set-function prompt
  14002. (mapcar 'list (org-property-values property))
  14003. nil nil "" nil cur)))))
  14004. (if (equal val "")
  14005. cur
  14006. val)))
  14007. (defvar org-last-set-property nil)
  14008. (defvar org-last-set-property-value nil)
  14009. (defun org-read-property-name ()
  14010. "Read a property name."
  14011. (let* ((completion-ignore-case t)
  14012. (keys (org-buffer-property-keys nil t t))
  14013. (default-prop (or (save-excursion
  14014. (save-match-data
  14015. (beginning-of-line)
  14016. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14017. (null (string= (match-string 1) "END"))
  14018. (match-string 1))))
  14019. org-last-set-property))
  14020. (property (org-icompleting-read
  14021. (concat "Property"
  14022. (if default-prop (concat " [" default-prop "]") "")
  14023. ": ")
  14024. (mapcar 'list keys)
  14025. nil nil nil nil
  14026. default-prop)))
  14027. (if (member property keys)
  14028. property
  14029. (or (cdr (assoc (downcase property)
  14030. (mapcar (lambda (x) (cons (downcase x) x))
  14031. keys)))
  14032. property))))
  14033. (defun org-set-property-and-value (use-last)
  14034. "Allow to set [PROPERTY]: [value] direction from prompt.
  14035. When use-default, don't even ask, just use the last
  14036. \"[PROPERTY]: [value]\" string from the history."
  14037. (interactive "P")
  14038. (let* ((completion-ignore-case t)
  14039. (pv (or (and use-last org-last-set-property-value)
  14040. (org-completing-read
  14041. "Enter a \"[Property]: [value]\" pair: "
  14042. nil nil nil nil nil
  14043. org-last-set-property-value)))
  14044. prop val)
  14045. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14046. (setq prop (match-string 1 pv)
  14047. val (match-string 2 pv))
  14048. (org-set-property prop val))))
  14049. (defun org-set-property (property value)
  14050. "In the current entry, set PROPERTY to VALUE.
  14051. When called interactively, this will prompt for a property name, offering
  14052. completion on existing and default properties. And then it will prompt
  14053. for a value, offering completion either on allowed values (via an inherited
  14054. xxx_ALL property) or on existing values in other instances of this property
  14055. in the current file."
  14056. (interactive (list nil nil))
  14057. (let* ((property (or property (org-read-property-name)))
  14058. (value (or value (org-read-property-value property)))
  14059. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14060. (setq org-last-set-property property)
  14061. (setq org-last-set-property-value (concat property ": " value))
  14062. ;; Possibly postprocess the inserted value:
  14063. (when fn (setq value (funcall fn value)))
  14064. (unless (equal (org-entry-get nil property) value)
  14065. (org-entry-put nil property value))))
  14066. (defun org-delete-property (property &optional delete-empty-drawer)
  14067. "In the current entry, delete PROPERTY.
  14068. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14069. an empty drawer to delete."
  14070. (interactive
  14071. (let* ((completion-ignore-case t)
  14072. (prop (org-icompleting-read "Property: "
  14073. (org-entry-properties nil 'standard))))
  14074. (list prop)))
  14075. (message "Property %s %s" property
  14076. (if (org-entry-delete nil property delete-empty-drawer)
  14077. "deleted"
  14078. "was not present in the entry")))
  14079. (defun org-delete-property-globally (property)
  14080. "Remove PROPERTY globally, from all entries."
  14081. (interactive
  14082. (let* ((completion-ignore-case t)
  14083. (prop (org-icompleting-read
  14084. "Globally remove property: "
  14085. (mapcar 'list (org-buffer-property-keys)))))
  14086. (list prop)))
  14087. (save-excursion
  14088. (save-restriction
  14089. (widen)
  14090. (goto-char (point-min))
  14091. (let ((cnt 0))
  14092. (while (re-search-forward
  14093. (org-re-property property)
  14094. nil t)
  14095. (setq cnt (1+ cnt))
  14096. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14097. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14098. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14099. (defun org-compute-property-at-point ()
  14100. "Compute the property at point.
  14101. This looks for an enclosing column format, extracts the operator and
  14102. then applies it to the property in the column format's scope."
  14103. (interactive)
  14104. (unless (org-at-property-p)
  14105. (error "Not at a property"))
  14106. (let ((prop (org-match-string-no-properties 2)))
  14107. (org-columns-get-format-and-top-level)
  14108. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14109. (error "No operator defined for property %s" prop))
  14110. (org-columns-compute prop)))
  14111. (defvar org-property-allowed-value-functions nil
  14112. "Hook for functions supplying allowed values for a specific property.
  14113. The functions must take a single argument, the name of the property, and
  14114. return a flat list of allowed values. If \":ETC\" is one of
  14115. the values, this means that these values are intended as defaults for
  14116. completion, but that other values should be allowed too.
  14117. The functions must return nil if they are not responsible for this
  14118. property.")
  14119. (defun org-property-get-allowed-values (pom property &optional table)
  14120. "Get allowed values for the property PROPERTY.
  14121. When TABLE is non-nil, return an alist that can directly be used for
  14122. completion."
  14123. (let (vals)
  14124. (cond
  14125. ((equal property "TODO")
  14126. (setq vals (org-with-point-at pom
  14127. (append org-todo-keywords-1 '("")))))
  14128. ((equal property "PRIORITY")
  14129. (let ((n org-lowest-priority))
  14130. (while (>= n org-highest-priority)
  14131. (push (char-to-string n) vals)
  14132. (setq n (1- n)))))
  14133. ((member property org-special-properties))
  14134. ((setq vals (run-hook-with-args-until-success
  14135. 'org-property-allowed-value-functions property)))
  14136. (t
  14137. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14138. (when (and vals (string-match "\\S-" vals))
  14139. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14140. (setq vals (mapcar (lambda (x)
  14141. (cond ((stringp x) x)
  14142. ((numberp x) (number-to-string x))
  14143. ((symbolp x) (symbol-name x))
  14144. (t "???")))
  14145. vals)))))
  14146. (when (member ":ETC" vals)
  14147. (setq vals (remove ":ETC" vals))
  14148. (org-add-props (car vals) '(org-unrestricted t)))
  14149. (if table (mapcar 'list vals) vals)))
  14150. (defun org-property-previous-allowed-value (&optional previous)
  14151. "Switch to the next allowed value for this property."
  14152. (interactive)
  14153. (org-property-next-allowed-value t))
  14154. (defun org-property-next-allowed-value (&optional previous)
  14155. "Switch to the next allowed value for this property."
  14156. (interactive)
  14157. (unless (org-at-property-p)
  14158. (error "Not at a property"))
  14159. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14160. (key (match-string 2))
  14161. (value (match-string 3))
  14162. (allowed (or (org-property-get-allowed-values (point) key)
  14163. (and (member value '("[ ]" "[-]" "[X]"))
  14164. '("[ ]" "[X]"))))
  14165. (heading (save-match-data (nth 4 (org-heading-components))))
  14166. nval)
  14167. (unless allowed
  14168. (error "Allowed values for this property have not been defined"))
  14169. (if previous (setq allowed (reverse allowed)))
  14170. (if (member value allowed)
  14171. (setq nval (car (cdr (member value allowed)))))
  14172. (setq nval (or nval (car allowed)))
  14173. (if (equal nval value)
  14174. (error "Only one allowed value for this property"))
  14175. (org-at-property-p)
  14176. (replace-match (concat " :" key ": " nval) t t)
  14177. (org-indent-line)
  14178. (beginning-of-line 1)
  14179. (skip-chars-forward " \t")
  14180. (when (equal prop org-effort-property)
  14181. (save-excursion
  14182. (org-back-to-heading t)
  14183. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14184. (when (string= org-clock-current-task heading)
  14185. (setq org-clock-effort nval)
  14186. (org-clock-update-mode-line)))
  14187. (run-hook-with-args 'org-property-changed-functions key nval)))
  14188. (defun org-find-olp (path &optional this-buffer)
  14189. "Return a marker pointing to the entry at outline path OLP.
  14190. If anything goes wrong, throw an error.
  14191. You can wrap this call to catch the error like this:
  14192. (condition-case msg
  14193. (org-mobile-locate-entry (match-string 4))
  14194. (error (nth 1 msg)))
  14195. The return value will then be either a string with the error message,
  14196. or a marker if everything is OK.
  14197. If THIS-BUFFER is set, the outline path does not contain a file,
  14198. only headings."
  14199. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14200. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14201. (level 1)
  14202. (lmin 1)
  14203. (lmax 1)
  14204. limit re end found pos heading cnt flevel)
  14205. (unless buffer (error "File not found :%s" file))
  14206. (with-current-buffer buffer
  14207. (save-excursion
  14208. (save-restriction
  14209. (widen)
  14210. (setq limit (point-max))
  14211. (goto-char (point-min))
  14212. (while (setq heading (pop path))
  14213. (setq re (format org-complex-heading-regexp-format
  14214. (regexp-quote heading)))
  14215. (setq cnt 0 pos (point))
  14216. (while (re-search-forward re end t)
  14217. (setq level (- (match-end 1) (match-beginning 1)))
  14218. (if (and (>= level lmin) (<= level lmax))
  14219. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14220. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14221. lmax heading))
  14222. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14223. lmax heading))
  14224. (goto-char found)
  14225. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14226. (setq end (save-excursion (org-end-of-subtree t t))))
  14227. (when (org-at-heading-p)
  14228. (point-marker)))))))
  14229. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14230. "Find node HEADING in BUFFER.
  14231. Return a marker to the heading if it was found, or nil if not.
  14232. If POS-ONLY is set, return just the position instead of a marker.
  14233. The heading text must match exact, but it may have a TODO keyword,
  14234. a priority cookie and tags in the standard locations."
  14235. (with-current-buffer (or buffer (current-buffer))
  14236. (save-excursion
  14237. (save-restriction
  14238. (widen)
  14239. (goto-char (point-min))
  14240. (let (case-fold-search)
  14241. (if (re-search-forward
  14242. (format org-complex-heading-regexp-format
  14243. (regexp-quote heading)) nil t)
  14244. (if pos-only
  14245. (match-beginning 0)
  14246. (move-marker (make-marker) (match-beginning 0)))))))))
  14247. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14248. "Find Org node headline HEADING in all .org files in directory DIR.
  14249. When the target headline is found, return a marker to this location."
  14250. (let ((files (directory-files (or dir default-directory)
  14251. nil "\\`[^.#].*\\.org\\'"))
  14252. file visiting m buffer)
  14253. (catch 'found
  14254. (while (setq file (pop files))
  14255. (message "trying %s" file)
  14256. (setq visiting (org-find-base-buffer-visiting file))
  14257. (setq buffer (or visiting (find-file-noselect file)))
  14258. (setq m (org-find-exact-headline-in-buffer
  14259. heading buffer))
  14260. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14261. (and m (throw 'found m))))))
  14262. (defun org-find-entry-with-id (ident)
  14263. "Locate the entry that contains the ID property with exact value IDENT.
  14264. IDENT can be a string, a symbol or a number, this function will search for
  14265. the string representation of it.
  14266. Return the position where this entry starts, or nil if there is no such entry."
  14267. (interactive "sID: ")
  14268. (let ((id (cond
  14269. ((stringp ident) ident)
  14270. ((symbol-name ident) (symbol-name ident))
  14271. ((numberp ident) (number-to-string ident))
  14272. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14273. (case-fold-search nil))
  14274. (save-excursion
  14275. (save-restriction
  14276. (widen)
  14277. (goto-char (point-min))
  14278. (when (re-search-forward
  14279. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14280. nil t)
  14281. (org-back-to-heading t)
  14282. (point))))))
  14283. ;;;; Timestamps
  14284. (defvar org-last-changed-timestamp nil)
  14285. (defvar org-last-inserted-timestamp nil
  14286. "The last time stamp inserted with `org-insert-time-stamp'.")
  14287. (defvar org-time-was-given) ; dynamically scoped parameter
  14288. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14289. (defvar org-ts-what) ; dynamically scoped parameter
  14290. (defun org-time-stamp (arg &optional inactive)
  14291. "Prompt for a date/time and insert a time stamp.
  14292. If the user specifies a time like HH:MM or if this command is
  14293. called with at least one prefix argument, the time stamp contains
  14294. the date and the time. Otherwise, only the date is be included.
  14295. All parts of a date not specified by the user is filled in from
  14296. the current date/time. So if you just press return without
  14297. typing anything, the time stamp will represent the current
  14298. date/time.
  14299. If there is already a timestamp at the cursor, it will be
  14300. modified.
  14301. With two universal prefix arguments, insert an active timestamp
  14302. with the current time without prompting the user."
  14303. (interactive "P")
  14304. (let* ((ts nil)
  14305. (default-time
  14306. ;; Default time is either today, or, when entering a range,
  14307. ;; the range start.
  14308. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14309. (save-excursion
  14310. (re-search-backward
  14311. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14312. (- (point) 20) t)))
  14313. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14314. (current-time)))
  14315. (default-input (and ts (org-get-compact-tod ts)))
  14316. (repeater (save-excursion
  14317. (save-match-data
  14318. (beginning-of-line)
  14319. (when (re-search-forward
  14320. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14321. (save-excursion (progn (end-of-line) (point))) t)
  14322. (match-string 0)))))
  14323. org-time-was-given org-end-time-was-given time)
  14324. (cond
  14325. ((and (org-at-timestamp-p t)
  14326. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14327. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14328. (insert "--")
  14329. (setq time (let ((this-command this-command))
  14330. (org-read-date arg 'totime nil nil
  14331. default-time default-input inactive)))
  14332. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14333. ((org-at-timestamp-p t)
  14334. (setq time (let ((this-command this-command))
  14335. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14336. (when (org-at-timestamp-p t) ; just to get the match data
  14337. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14338. (replace-match "")
  14339. (setq org-last-changed-timestamp
  14340. (org-insert-time-stamp
  14341. time (or org-time-was-given arg)
  14342. inactive nil nil (list org-end-time-was-given)))
  14343. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14344. (setq org-last-changed-timestamp
  14345. (concat (substring org-last-inserted-timestamp 0 -1)
  14346. " " repeater ">"))))
  14347. (message "Timestamp updated"))
  14348. ((equal arg '(16))
  14349. (org-insert-time-stamp (current-time) t))
  14350. (t
  14351. (setq time (let ((this-command this-command))
  14352. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14353. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14354. nil nil (list org-end-time-was-given))))))
  14355. ;; FIXME: can we use this for something else, like computing time differences?
  14356. (defun org-get-compact-tod (s)
  14357. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14358. (let* ((t1 (match-string 1 s))
  14359. (h1 (string-to-number (match-string 2 s)))
  14360. (m1 (string-to-number (match-string 3 s)))
  14361. (t2 (and (match-end 4) (match-string 5 s)))
  14362. (h2 (and t2 (string-to-number (match-string 6 s))))
  14363. (m2 (and t2 (string-to-number (match-string 7 s))))
  14364. dh dm)
  14365. (if (not t2)
  14366. t1
  14367. (setq dh (- h2 h1) dm (- m2 m1))
  14368. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14369. (concat t1 "+" (number-to-string dh)
  14370. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14371. (defun org-time-stamp-inactive (&optional arg)
  14372. "Insert an inactive time stamp.
  14373. An inactive time stamp is enclosed in square brackets instead of angle
  14374. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14375. does not link to the calendar and cannot be changed with the S-cursor keys.
  14376. So these are more for recording a certain time/date."
  14377. (interactive "P")
  14378. (org-time-stamp arg 'inactive))
  14379. (defvar org-date-ovl (make-overlay 1 1))
  14380. (overlay-put org-date-ovl 'face 'org-date-selected)
  14381. (org-detach-overlay org-date-ovl)
  14382. (defvar org-ans1) ; dynamically scoped parameter
  14383. (defvar org-ans2) ; dynamically scoped parameter
  14384. (defvar org-plain-time-of-day-regexp) ; defined below
  14385. (defvar org-overriding-default-time nil) ; dynamically scoped
  14386. (defvar org-read-date-overlay nil)
  14387. (defvar org-dcst nil) ; dynamically scoped
  14388. (defvar org-read-date-history nil)
  14389. (defvar org-read-date-final-answer nil)
  14390. (defvar org-read-date-analyze-futurep nil)
  14391. (defvar org-read-date-analyze-forced-year nil)
  14392. (defvar org-read-date-inactive)
  14393. (defvar org-read-date-minibuffer-local-map
  14394. (let ((map (make-sparse-keymap)))
  14395. (set-keymap-parent map minibuffer-local-map)
  14396. (org-defkey map (kbd ".")
  14397. (lambda () (interactive)
  14398. (org-eval-in-calendar '(calendar-goto-today))))
  14399. (org-defkey map [(meta shift left)]
  14400. (lambda () (interactive)
  14401. (org-eval-in-calendar '(calendar-backward-month 1))))
  14402. (org-defkey map [(meta shift right)]
  14403. (lambda () (interactive)
  14404. (org-eval-in-calendar '(calendar-forward-month 1))))
  14405. (org-defkey map [(meta shift up)]
  14406. (lambda () (interactive)
  14407. (org-eval-in-calendar '(calendar-backward-year 1))))
  14408. (org-defkey map [(meta shift down)]
  14409. (lambda () (interactive)
  14410. (org-eval-in-calendar '(calendar-forward-year 1))))
  14411. (org-defkey map [?\e (shift left)]
  14412. (lambda () (interactive)
  14413. (org-eval-in-calendar '(calendar-backward-month 1))))
  14414. (org-defkey map [?\e (shift right)]
  14415. (lambda () (interactive)
  14416. (org-eval-in-calendar '(calendar-forward-month 1))))
  14417. (org-defkey map [?\e (shift up)]
  14418. (lambda () (interactive)
  14419. (org-eval-in-calendar '(calendar-backward-year 1))))
  14420. (org-defkey map [?\e (shift down)]
  14421. (lambda () (interactive)
  14422. (org-eval-in-calendar '(calendar-forward-year 1))))
  14423. (org-defkey map [(shift up)]
  14424. (lambda () (interactive)
  14425. (org-eval-in-calendar '(calendar-backward-week 1))))
  14426. (org-defkey map [(shift down)]
  14427. (lambda () (interactive)
  14428. (org-eval-in-calendar '(calendar-forward-week 1))))
  14429. (org-defkey map [(shift left)]
  14430. (lambda () (interactive)
  14431. (org-eval-in-calendar '(calendar-backward-day 1))))
  14432. (org-defkey map [(shift right)]
  14433. (lambda () (interactive)
  14434. (org-eval-in-calendar '(calendar-forward-day 1))))
  14435. (org-defkey map "!"
  14436. (lambda () (interactive)
  14437. (org-eval-in-calendar '(diary-view-entries))
  14438. (message "")))
  14439. (org-defkey map ">"
  14440. (lambda () (interactive)
  14441. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14442. (org-defkey map "<"
  14443. (lambda () (interactive)
  14444. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14445. (org-defkey map "\C-v"
  14446. (lambda () (interactive)
  14447. (org-eval-in-calendar
  14448. '(calendar-scroll-left-three-months 1))))
  14449. (org-defkey map "\M-v"
  14450. (lambda () (interactive)
  14451. (org-eval-in-calendar
  14452. '(calendar-scroll-right-three-months 1))))
  14453. map)
  14454. "Keymap for minibuffer commands when using `org-read-date'.")
  14455. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14456. default-time default-input inactive)
  14457. "Read a date, possibly a time, and make things smooth for the user.
  14458. The prompt will suggest to enter an ISO date, but you can also enter anything
  14459. which will at least partially be understood by `parse-time-string'.
  14460. Unrecognized parts of the date will default to the current day, month, year,
  14461. hour and minute. If this command is called to replace a timestamp at point,
  14462. or to enter the second timestamp of a range, the default time is taken
  14463. from the existing stamp. Furthermore, the command prefers the future,
  14464. so if you are giving a date where the year is not given, and the day-month
  14465. combination is already past in the current year, it will assume you
  14466. mean next year. For details, see the manual. A few examples:
  14467. 3-2-5 --> 2003-02-05
  14468. feb 15 --> currentyear-02-15
  14469. 2/15 --> currentyear-02-15
  14470. sep 12 9 --> 2009-09-12
  14471. 12:45 --> today 12:45
  14472. 22 sept 0:34 --> currentyear-09-22 0:34
  14473. 12 --> currentyear-currentmonth-12
  14474. Fri --> nearest Friday (today or later)
  14475. etc.
  14476. Furthermore you can specify a relative date by giving, as the *first* thing
  14477. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14478. change in days weeks, months, years.
  14479. With a single plus or minus, the date is relative to today. With a double
  14480. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14481. +4d --> four days from today
  14482. +4 --> same as above
  14483. +2w --> two weeks from today
  14484. ++5 --> five days from default date
  14485. The function understands only English month and weekday abbreviations.
  14486. While prompting, a calendar is popped up - you can also select the
  14487. date with the mouse (button 1). The calendar shows a period of three
  14488. months. To scroll it to other months, use the keys `>' and `<'.
  14489. If you don't like the calendar, turn it off with
  14490. \(setq org-read-date-popup-calendar nil)
  14491. With optional argument TO-TIME, the date will immediately be converted
  14492. to an internal time.
  14493. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14494. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14495. still enter a time, and this function will inform the calling routine
  14496. about this change. The calling routine may then choose to change the
  14497. format used to insert the time stamp into the buffer to include the time.
  14498. With optional argument FROM-STRING, read from this string instead from
  14499. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14500. the time/date that is used for everything that is not specified by the
  14501. user."
  14502. (require 'parse-time)
  14503. (let* ((org-time-stamp-rounding-minutes
  14504. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14505. (org-dcst org-display-custom-times)
  14506. (ct (org-current-time))
  14507. (org-def (or org-overriding-default-time default-time ct))
  14508. (org-defdecode (decode-time org-def))
  14509. (dummy (progn
  14510. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14511. (setcar (nthcdr 2 org-defdecode) -1)
  14512. (setcar (nthcdr 1 org-defdecode) 59)
  14513. (setq org-def (apply 'encode-time org-defdecode)
  14514. org-defdecode (decode-time org-def)))))
  14515. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14516. (calendar-frame-setup nil)
  14517. (calendar-setup nil)
  14518. (calendar-move-hook nil)
  14519. (calendar-view-diary-initially-flag nil)
  14520. (calendar-view-holidays-initially-flag nil)
  14521. (timestr (format-time-string
  14522. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14523. (prompt (concat (if prompt (concat prompt " ") "")
  14524. (format "Date+time [%s]: " timestr)))
  14525. ans (org-ans0 "") org-ans1 org-ans2 final)
  14526. (cond
  14527. (from-string (setq ans from-string))
  14528. (org-read-date-popup-calendar
  14529. (save-excursion
  14530. (save-window-excursion
  14531. (calendar)
  14532. (org-eval-in-calendar '(setq cursor-type nil) t)
  14533. (unwind-protect
  14534. (progn
  14535. (calendar-forward-day (- (time-to-days org-def)
  14536. (calendar-absolute-from-gregorian
  14537. (calendar-current-date))))
  14538. (org-eval-in-calendar nil t)
  14539. (let* ((old-map (current-local-map))
  14540. (map (copy-keymap calendar-mode-map))
  14541. (minibuffer-local-map
  14542. (copy-keymap org-read-date-minibuffer-local-map)))
  14543. (org-defkey map (kbd "RET") 'org-calendar-select)
  14544. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14545. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14546. (unwind-protect
  14547. (progn
  14548. (use-local-map map)
  14549. (setq org-read-date-inactive inactive)
  14550. (add-hook 'post-command-hook 'org-read-date-display)
  14551. (setq org-ans0 (read-string prompt default-input
  14552. 'org-read-date-history nil))
  14553. ;; org-ans0: from prompt
  14554. ;; org-ans1: from mouse click
  14555. ;; org-ans2: from calendar motion
  14556. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14557. (remove-hook 'post-command-hook 'org-read-date-display)
  14558. (use-local-map old-map)
  14559. (when org-read-date-overlay
  14560. (delete-overlay org-read-date-overlay)
  14561. (setq org-read-date-overlay nil)))))
  14562. (bury-buffer "*Calendar*")))))
  14563. (t ; Naked prompt only
  14564. (unwind-protect
  14565. (setq ans (read-string prompt default-input
  14566. 'org-read-date-history timestr))
  14567. (when org-read-date-overlay
  14568. (delete-overlay org-read-date-overlay)
  14569. (setq org-read-date-overlay nil)))))
  14570. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14571. (when org-read-date-analyze-forced-year
  14572. (message "Year was forced into %s"
  14573. (if org-read-date-force-compatible-dates
  14574. "compatible range (1970-2037)"
  14575. "range representable on this machine"))
  14576. (ding))
  14577. ;; One round trip to get rid of 34th of August and stuff like that....
  14578. (setq final (decode-time (apply 'encode-time final)))
  14579. (setq org-read-date-final-answer ans)
  14580. (if to-time
  14581. (apply 'encode-time final)
  14582. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14583. (format "%04d-%02d-%02d %02d:%02d"
  14584. (nth 5 final) (nth 4 final) (nth 3 final)
  14585. (nth 2 final) (nth 1 final))
  14586. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14587. (defvar org-def)
  14588. (defvar org-defdecode)
  14589. (defvar org-with-time)
  14590. (defun org-read-date-display ()
  14591. "Display the current date prompt interpretation in the minibuffer."
  14592. (when org-read-date-display-live
  14593. (when org-read-date-overlay
  14594. (delete-overlay org-read-date-overlay))
  14595. (when (minibufferp (current-buffer))
  14596. (save-excursion
  14597. (end-of-line 1)
  14598. (while (not (equal (buffer-substring
  14599. (max (point-min) (- (point) 4)) (point))
  14600. " "))
  14601. (insert " ")))
  14602. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14603. " " (or org-ans1 org-ans2)))
  14604. (org-end-time-was-given nil)
  14605. (f (org-read-date-analyze ans org-def org-defdecode))
  14606. (fmts (if org-dcst
  14607. org-time-stamp-custom-formats
  14608. org-time-stamp-formats))
  14609. (fmt (if (or org-with-time
  14610. (and (boundp 'org-time-was-given) org-time-was-given))
  14611. (cdr fmts)
  14612. (car fmts)))
  14613. (txt (format-time-string fmt (apply 'encode-time f)))
  14614. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14615. (txt (concat "=> " txt)))
  14616. (when (and org-end-time-was-given
  14617. (string-match org-plain-time-of-day-regexp txt))
  14618. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14619. org-end-time-was-given
  14620. (substring txt (match-end 0)))))
  14621. (when org-read-date-analyze-futurep
  14622. (setq txt (concat txt " (=>F)")))
  14623. (setq org-read-date-overlay
  14624. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14625. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14626. (defun org-read-date-analyze (ans org-def org-defdecode)
  14627. "Analyze the combined answer of the date prompt."
  14628. ;; FIXME: cleanup and comment
  14629. (let ((nowdecode (decode-time (current-time)))
  14630. delta deltan deltaw deltadef year month day
  14631. hour minute second wday pm h2 m2 tl wday1
  14632. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14633. (setq org-read-date-analyze-futurep nil
  14634. org-read-date-analyze-forced-year nil)
  14635. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14636. (setq ans "+0"))
  14637. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14638. (setq ans (replace-match "" t t ans)
  14639. deltan (car delta)
  14640. deltaw (nth 1 delta)
  14641. deltadef (nth 2 delta)))
  14642. ;; Check if there is an iso week date in there. If yes, store the
  14643. ;; info and postpone interpreting it until the rest of the parsing
  14644. ;; is done.
  14645. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14646. (setq iso-year (if (match-end 1)
  14647. (org-small-year-to-year
  14648. (string-to-number (match-string 1 ans))))
  14649. iso-weekday (if (match-end 3)
  14650. (string-to-number (match-string 3 ans)))
  14651. iso-week (string-to-number (match-string 2 ans)))
  14652. (setq ans (replace-match "" t t ans)))
  14653. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14654. (when (string-match
  14655. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14656. (setq year (if (match-end 2)
  14657. (string-to-number (match-string 2 ans))
  14658. (progn (setq kill-year t)
  14659. (string-to-number (format-time-string "%Y"))))
  14660. month (string-to-number (match-string 3 ans))
  14661. day (string-to-number (match-string 4 ans)))
  14662. (if (< year 100) (setq year (+ 2000 year)))
  14663. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14664. t nil ans)))
  14665. ;; Help matching dotted european dates
  14666. (when (string-match
  14667. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14668. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14669. (setq kill-year t)
  14670. (string-to-number (format-time-string "%Y")))
  14671. day (string-to-number (match-string 1 ans))
  14672. month (string-to-number (match-string 2 ans))
  14673. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14674. t nil ans)))
  14675. ;; Help matching american dates, like 5/30 or 5/30/7
  14676. (when (string-match
  14677. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14678. (setq year (if (match-end 4)
  14679. (string-to-number (match-string 4 ans))
  14680. (progn (setq kill-year t)
  14681. (string-to-number (format-time-string "%Y"))))
  14682. month (string-to-number (match-string 1 ans))
  14683. day (string-to-number (match-string 2 ans)))
  14684. (if (< year 100) (setq year (+ 2000 year)))
  14685. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14686. t nil ans)))
  14687. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14688. ;; If there is a time with am/pm, and *no* time without it, we convert
  14689. ;; so that matching will be successful.
  14690. (loop for i from 1 to 2 do ; twice, for end time as well
  14691. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14692. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14693. (setq hour (string-to-number (match-string 1 ans))
  14694. minute (if (match-end 3)
  14695. (string-to-number (match-string 3 ans))
  14696. 0)
  14697. pm (equal ?p
  14698. (string-to-char (downcase (match-string 4 ans)))))
  14699. (if (and (= hour 12) (not pm))
  14700. (setq hour 0)
  14701. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14702. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14703. t t ans))))
  14704. ;; Check if a time range is given as a duration
  14705. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14706. (setq hour (string-to-number (match-string 1 ans))
  14707. h2 (+ hour (string-to-number (match-string 3 ans)))
  14708. minute (string-to-number (match-string 2 ans))
  14709. m2 (+ minute (if (match-end 5) (string-to-number
  14710. (match-string 5 ans))0)))
  14711. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14712. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14713. t t ans)))
  14714. ;; Check if there is a time range
  14715. (when (boundp 'org-end-time-was-given)
  14716. (setq org-time-was-given nil)
  14717. (when (and (string-match org-plain-time-of-day-regexp ans)
  14718. (match-end 8))
  14719. (setq org-end-time-was-given (match-string 8 ans))
  14720. (setq ans (concat (substring ans 0 (match-beginning 7))
  14721. (substring ans (match-end 7))))))
  14722. (setq tl (parse-time-string ans)
  14723. day (or (nth 3 tl) (nth 3 org-defdecode))
  14724. month (or (nth 4 tl)
  14725. (if (and org-read-date-prefer-future
  14726. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14727. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14728. (nth 4 org-defdecode)))
  14729. year (or (and (not kill-year) (nth 5 tl))
  14730. (if (and org-read-date-prefer-future
  14731. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14732. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14733. (nth 5 org-defdecode)))
  14734. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14735. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14736. second (or (nth 0 tl) 0)
  14737. wday (nth 6 tl))
  14738. (when (and (eq org-read-date-prefer-future 'time)
  14739. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14740. (equal day (nth 3 nowdecode))
  14741. (equal month (nth 4 nowdecode))
  14742. (equal year (nth 5 nowdecode))
  14743. (nth 2 tl)
  14744. (or (< (nth 2 tl) (nth 2 nowdecode))
  14745. (and (= (nth 2 tl) (nth 2 nowdecode))
  14746. (nth 1 tl)
  14747. (< (nth 1 tl) (nth 1 nowdecode)))))
  14748. (setq day (1+ day)
  14749. futurep t))
  14750. ;; Special date definitions below
  14751. (cond
  14752. (iso-week
  14753. ;; There was an iso week
  14754. (require 'cal-iso)
  14755. (setq futurep nil)
  14756. (setq year (or iso-year year)
  14757. day (or iso-weekday wday 1)
  14758. wday nil ; to make sure that the trigger below does not match
  14759. iso-date (calendar-gregorian-from-absolute
  14760. (calendar-absolute-from-iso
  14761. (list iso-week day year))))
  14762. ; FIXME: Should we also push ISO weeks into the future?
  14763. ; (when (and org-read-date-prefer-future
  14764. ; (not iso-year)
  14765. ; (< (calendar-absolute-from-gregorian iso-date)
  14766. ; (time-to-days (current-time))))
  14767. ; (setq year (1+ year)
  14768. ; iso-date (calendar-gregorian-from-absolute
  14769. ; (calendar-absolute-from-iso
  14770. ; (list iso-week day year)))))
  14771. (setq month (car iso-date)
  14772. year (nth 2 iso-date)
  14773. day (nth 1 iso-date)))
  14774. (deltan
  14775. (setq futurep nil)
  14776. (unless deltadef
  14777. (let ((now (decode-time (current-time))))
  14778. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14779. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14780. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14781. ((equal deltaw "m") (setq month (+ month deltan)))
  14782. ((equal deltaw "y") (setq year (+ year deltan)))))
  14783. ((and wday (not (nth 3 tl)))
  14784. ;; Weekday was given, but no day, so pick that day in the week
  14785. ;; on or after the derived date.
  14786. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14787. (unless (equal wday wday1)
  14788. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14789. (if (and (boundp 'org-time-was-given)
  14790. (nth 2 tl))
  14791. (setq org-time-was-given t))
  14792. (if (< year 100) (setq year (+ 2000 year)))
  14793. ;; Check of the date is representable
  14794. (if org-read-date-force-compatible-dates
  14795. (progn
  14796. (if (< year 1970)
  14797. (setq year 1970 org-read-date-analyze-forced-year t))
  14798. (if (> year 2037)
  14799. (setq year 2037 org-read-date-analyze-forced-year t)))
  14800. (condition-case nil
  14801. (ignore (encode-time second minute hour day month year))
  14802. (error
  14803. (setq year (nth 5 org-defdecode))
  14804. (setq org-read-date-analyze-forced-year t))))
  14805. (setq org-read-date-analyze-futurep futurep)
  14806. (list second minute hour day month year)))
  14807. (defvar parse-time-weekdays)
  14808. (defun org-read-date-get-relative (s today default)
  14809. "Check string S for special relative date string.
  14810. TODAY and DEFAULT are internal times, for today and for a default.
  14811. Return shift list (N what def-flag)
  14812. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14813. N is the number of WHATs to shift.
  14814. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14815. the DEFAULT date rather than TODAY."
  14816. (require 'parse-time)
  14817. (when (and
  14818. (string-match
  14819. (concat
  14820. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14821. "\\([0-9]+\\)?"
  14822. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14823. "\\([ \t]\\|$\\)") s)
  14824. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14825. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14826. (string-to-char (substring (match-string 1 s) -1))
  14827. ?+))
  14828. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14829. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14830. (what (if (match-end 3) (match-string 3 s) "d"))
  14831. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14832. (date (if rel default today))
  14833. (wday (nth 6 (decode-time date)))
  14834. delta)
  14835. (if wday1
  14836. (progn
  14837. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14838. (if (= dir ?-) (setq delta (- delta 7)))
  14839. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14840. (list delta "d" rel))
  14841. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14842. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14843. "Turn a user-specified date into the internal representation.
  14844. The internal representation needed by the calendar is (month day year).
  14845. This is a wrapper to handle the brain-dead convention in calendar that
  14846. user function argument order change dependent on argument order."
  14847. (if (boundp 'calendar-date-style)
  14848. (cond
  14849. ((eq calendar-date-style 'american)
  14850. (list arg1 arg2 arg3))
  14851. ((eq calendar-date-style 'european)
  14852. (list arg2 arg1 arg3))
  14853. ((eq calendar-date-style 'iso)
  14854. (list arg2 arg3 arg1)))
  14855. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14856. (if (org-bound-and-true-p european-calendar-style)
  14857. (list arg2 arg1 arg3)
  14858. (list arg1 arg2 arg3)))))
  14859. (defun org-eval-in-calendar (form &optional keepdate)
  14860. "Eval FORM in the calendar window and return to current window.
  14861. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14862. otherwise stick to the current value of `org-ans2'."
  14863. (let ((sf (selected-frame))
  14864. (sw (selected-window)))
  14865. (select-window (get-buffer-window "*Calendar*" t))
  14866. (eval form)
  14867. (when (and (not keepdate) (calendar-cursor-to-date))
  14868. (let* ((date (calendar-cursor-to-date))
  14869. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14870. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14871. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14872. (select-window sw)
  14873. (org-select-frame-set-input-focus sf)))
  14874. (defun org-calendar-select ()
  14875. "Return to `org-read-date' with the date currently selected.
  14876. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14877. (interactive)
  14878. (when (calendar-cursor-to-date)
  14879. (let* ((date (calendar-cursor-to-date))
  14880. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14881. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14882. (if (active-minibuffer-window) (exit-minibuffer))))
  14883. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14884. "Insert a date stamp for the date given by the internal TIME.
  14885. WITH-HM means use the stamp format that includes the time of the day.
  14886. INACTIVE means use square brackets instead of angular ones, so that the
  14887. stamp will not contribute to the agenda.
  14888. PRE and POST are optional strings to be inserted before and after the
  14889. stamp.
  14890. The command returns the inserted time stamp."
  14891. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14892. stamp)
  14893. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14894. (insert-before-markers (or pre ""))
  14895. (when (listp extra)
  14896. (setq extra (car extra))
  14897. (if (and (stringp extra)
  14898. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14899. (setq extra (format "-%02d:%02d"
  14900. (string-to-number (match-string 1 extra))
  14901. (string-to-number (match-string 2 extra))))
  14902. (setq extra nil)))
  14903. (when extra
  14904. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14905. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14906. (insert-before-markers (or post ""))
  14907. (setq org-last-inserted-timestamp stamp)))
  14908. (defun org-toggle-time-stamp-overlays ()
  14909. "Toggle the use of custom time stamp formats."
  14910. (interactive)
  14911. (setq org-display-custom-times (not org-display-custom-times))
  14912. (unless org-display-custom-times
  14913. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14914. (while (setq p (next-single-property-change p 'display))
  14915. (if (and (get-text-property p 'display)
  14916. (eq (get-text-property p 'face) 'org-date))
  14917. (remove-text-properties
  14918. p (setq p (next-single-property-change p 'display))
  14919. '(display t))))
  14920. (set-buffer-modified-p bmp)))
  14921. (if (featurep 'xemacs)
  14922. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14923. (org-restart-font-lock)
  14924. (setq org-table-may-need-update t)
  14925. (if org-display-custom-times
  14926. (message "Time stamps are overlaid with custom format")
  14927. (message "Time stamp overlays removed")))
  14928. (defun org-display-custom-time (beg end)
  14929. "Overlay modified time stamp format over timestamp between BEG and END."
  14930. (let* ((ts (buffer-substring beg end))
  14931. t1 w1 with-hm tf time str w2 (off 0))
  14932. (save-match-data
  14933. (setq t1 (org-parse-time-string ts t))
  14934. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14935. (setq off (- (match-end 0) (match-beginning 0)))))
  14936. (setq end (- end off))
  14937. (setq w1 (- end beg)
  14938. with-hm (and (nth 1 t1) (nth 2 t1))
  14939. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14940. time (org-fix-decoded-time t1)
  14941. str (org-add-props
  14942. (format-time-string
  14943. (substring tf 1 -1) (apply 'encode-time time))
  14944. nil 'mouse-face 'highlight)
  14945. w2 (length str))
  14946. (if (not (= w2 w1))
  14947. (add-text-properties (1+ beg) (+ 2 beg)
  14948. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14949. (if (featurep 'xemacs)
  14950. (progn
  14951. (put-text-property beg end 'invisible t)
  14952. (put-text-property beg end 'end-glyph (make-glyph str)))
  14953. (put-text-property beg end 'display str))))
  14954. (defun org-translate-time (string)
  14955. "Translate all timestamps in STRING to custom format.
  14956. But do this only if the variable `org-display-custom-times' is set."
  14957. (when org-display-custom-times
  14958. (save-match-data
  14959. (let* ((start 0)
  14960. (re org-ts-regexp-both)
  14961. t1 with-hm inactive tf time str beg end)
  14962. (while (setq start (string-match re string start))
  14963. (setq beg (match-beginning 0)
  14964. end (match-end 0)
  14965. t1 (save-match-data
  14966. (org-parse-time-string (substring string beg end) t))
  14967. with-hm (and (nth 1 t1) (nth 2 t1))
  14968. inactive (equal (substring string beg (1+ beg)) "[")
  14969. tf (funcall (if with-hm 'cdr 'car)
  14970. org-time-stamp-custom-formats)
  14971. time (org-fix-decoded-time t1)
  14972. str (format-time-string
  14973. (concat
  14974. (if inactive "[" "<") (substring tf 1 -1)
  14975. (if inactive "]" ">"))
  14976. (apply 'encode-time time))
  14977. string (replace-match str t t string)
  14978. start (+ start (length str)))))))
  14979. string)
  14980. (defun org-fix-decoded-time (time)
  14981. "Set 0 instead of nil for the first 6 elements of time.
  14982. Don't touch the rest."
  14983. (let ((n 0))
  14984. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14985. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.3")
  14986. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  14987. "Difference between TIMESTAMP-STRING and now in days.
  14988. If SECONDS is non-nil, return the difference in seconds."
  14989. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  14990. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  14991. (funcall fdiff (current-time)))))
  14992. (defun org-deadline-close (timestamp-string &optional ndays)
  14993. "Is the time in TIMESTAMP-STRING close to the current date?"
  14994. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14995. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  14996. (not (org-entry-is-done-p))))
  14997. (defun org-get-wdays (ts &optional delay zero-delay)
  14998. "Get the deadline lead time appropriate for timestring TS.
  14999. When DELAY is non-nil, get the delay time for scheduled items
  15000. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15001. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15002. don't try to find the delay cookie in the scheduled timestamp."
  15003. (let ((tv (if delay org-scheduled-delay-days
  15004. org-deadline-warning-days)))
  15005. (cond
  15006. ((or (and delay (< tv 0))
  15007. (and delay zero-delay (<= tv 0))
  15008. (and (not delay) (<= tv 0)))
  15009. ;; Enforce this value no matter what
  15010. (- tv))
  15011. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15012. ;; lead time is specified.
  15013. (floor (* (string-to-number (match-string 1 ts))
  15014. (cdr (assoc (match-string 2 ts)
  15015. '(("d" . 1) ("w" . 7)
  15016. ("m" . 30.4) ("y" . 365.25)
  15017. ("h" . 0.041667)))))))
  15018. ;; go for the default.
  15019. (t tv))))
  15020. (defun org-calendar-select-mouse (ev)
  15021. "Return to `org-read-date' with the date currently selected.
  15022. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15023. (interactive "e")
  15024. (mouse-set-point ev)
  15025. (when (calendar-cursor-to-date)
  15026. (let* ((date (calendar-cursor-to-date))
  15027. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15028. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15029. (if (active-minibuffer-window) (exit-minibuffer))))
  15030. (defun org-check-deadlines (ndays)
  15031. "Check if there are any deadlines due or past due.
  15032. A deadline is considered due if it happens within `org-deadline-warning-days'
  15033. days from today's date. If the deadline appears in an entry marked DONE,
  15034. it is not shown. The prefix arg NDAYS can be used to test that many
  15035. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15036. (interactive "P")
  15037. (let* ((org-warn-days
  15038. (cond
  15039. ((equal ndays '(4)) 100000)
  15040. (ndays (prefix-numeric-value ndays))
  15041. (t (abs org-deadline-warning-days))))
  15042. (case-fold-search nil)
  15043. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15044. (callback
  15045. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15046. (message "%d deadlines past-due or due within %d days"
  15047. (org-occur regexp nil callback)
  15048. org-warn-days)))
  15049. (defsubst org-re-timestamp (type)
  15050. "Return a regexp for timestamp TYPE.
  15051. Allowed values for TYPE are:
  15052. all: all timestamps
  15053. active: only active timestamps (<...>)
  15054. inactive: only inactive timestamps ([...])
  15055. scheduled: only scheduled timestamps
  15056. deadline: only deadline timestamps
  15057. When TYPE is nil, fall back on returning a regexp that matches
  15058. both scheduled and deadline timestamps."
  15059. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15060. ((eq type 'active) org-ts-regexp)
  15061. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15062. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15063. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15064. ((eq type 'scheduled-or-deadline)
  15065. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15066. (defun org-check-before-date (date)
  15067. "Check if there are deadlines or scheduled entries before DATE."
  15068. (interactive (list (org-read-date)))
  15069. (let ((case-fold-search nil)
  15070. (regexp (org-re-timestamp org-ts-type))
  15071. (callback
  15072. (lambda () (time-less-p
  15073. (org-time-string-to-time (match-string 1))
  15074. (org-time-string-to-time date)))))
  15075. (message "%d entries before %s"
  15076. (org-occur regexp nil callback) date)))
  15077. (defun org-check-after-date (date)
  15078. "Check if there are deadlines or scheduled entries after DATE."
  15079. (interactive (list (org-read-date)))
  15080. (let ((case-fold-search nil)
  15081. (regexp (org-re-timestamp org-ts-type))
  15082. (callback
  15083. (lambda () (not
  15084. (time-less-p
  15085. (org-time-string-to-time (match-string 1))
  15086. (org-time-string-to-time date))))))
  15087. (message "%d entries after %s"
  15088. (org-occur regexp nil callback) date)))
  15089. (defun org-check-dates-range (start-date end-date)
  15090. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15091. (interactive (list (org-read-date nil nil nil "Range starts")
  15092. (org-read-date nil nil nil "Range end")))
  15093. (let ((case-fold-search nil)
  15094. (regexp (org-re-timestamp org-ts-type))
  15095. (callback
  15096. (lambda ()
  15097. (let ((match (match-string 1)))
  15098. (and
  15099. (not (time-less-p
  15100. (org-time-string-to-time match)
  15101. (org-time-string-to-time start-date)))
  15102. (time-less-p
  15103. (org-time-string-to-time match)
  15104. (org-time-string-to-time end-date)))))))
  15105. (message "%d entries between %s and %s"
  15106. (org-occur regexp nil callback) start-date end-date)))
  15107. (defun org-evaluate-time-range (&optional to-buffer)
  15108. "Evaluate a time range by computing the difference between start and end.
  15109. Normally the result is just printed in the echo area, but with prefix arg
  15110. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15111. If the time range is actually in a table, the result is inserted into the
  15112. next column.
  15113. For time difference computation, a year is assumed to be exactly 365
  15114. days in order to avoid rounding problems."
  15115. (interactive "P")
  15116. (or
  15117. (org-clock-update-time-maybe)
  15118. (save-excursion
  15119. (unless (org-at-date-range-p t)
  15120. (goto-char (point-at-bol))
  15121. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15122. (if (not (org-at-date-range-p t))
  15123. (error "Not at a time-stamp range, and none found in current line")))
  15124. (let* ((ts1 (match-string 1))
  15125. (ts2 (match-string 2))
  15126. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15127. (match-end (match-end 0))
  15128. (time1 (org-time-string-to-time ts1))
  15129. (time2 (org-time-string-to-time ts2))
  15130. (t1 (org-float-time time1))
  15131. (t2 (org-float-time time2))
  15132. (diff (abs (- t2 t1)))
  15133. (negative (< (- t2 t1) 0))
  15134. ;; (ys (floor (* 365 24 60 60)))
  15135. (ds (* 24 60 60))
  15136. (hs (* 60 60))
  15137. (fy "%dy %dd %02d:%02d")
  15138. (fy1 "%dy %dd")
  15139. (fd "%dd %02d:%02d")
  15140. (fd1 "%dd")
  15141. (fh "%02d:%02d")
  15142. y d h m align)
  15143. (if havetime
  15144. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15145. y 0
  15146. d (floor (/ diff ds)) diff (mod diff ds)
  15147. h (floor (/ diff hs)) diff (mod diff hs)
  15148. m (floor (/ diff 60)))
  15149. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15150. y 0
  15151. d (floor (+ (/ diff ds) 0.5))
  15152. h 0 m 0))
  15153. (if (not to-buffer)
  15154. (message "%s" (org-make-tdiff-string y d h m))
  15155. (if (org-at-table-p)
  15156. (progn
  15157. (goto-char match-end)
  15158. (setq align t)
  15159. (and (looking-at " *|") (goto-char (match-end 0))))
  15160. (goto-char match-end))
  15161. (if (looking-at
  15162. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15163. (replace-match ""))
  15164. (if negative (insert " -"))
  15165. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15166. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15167. (insert " " (format fh h m))))
  15168. (if align (org-table-align))
  15169. (message "Time difference inserted")))))
  15170. (defun org-make-tdiff-string (y d h m)
  15171. (let ((fmt "")
  15172. (l nil))
  15173. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15174. l (push y l)))
  15175. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15176. l (push d l)))
  15177. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15178. l (push h l)))
  15179. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15180. l (push m l)))
  15181. (apply 'format fmt (nreverse l))))
  15182. (defun org-time-string-to-time (s &optional buffer pos)
  15183. "Convert a timestamp string into internal time."
  15184. (condition-case errdata
  15185. (apply 'encode-time (org-parse-time-string s))
  15186. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15187. s (if (not (and buffer pos))
  15188. ""
  15189. (format " at %d in buffer `%s'" pos buffer))
  15190. (cdr errdata)))))
  15191. (defun org-time-string-to-seconds (s)
  15192. "Convert a timestamp string to a number of seconds."
  15193. (org-float-time (org-time-string-to-time s)))
  15194. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15195. "Convert a time stamp to an absolute day number.
  15196. If there is a specifier for a cyclic time stamp, get the closest
  15197. date to DAYNR.
  15198. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15199. The variable date is bound by the calendar when this is called."
  15200. (cond
  15201. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15202. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15203. daynr
  15204. (+ daynr 1000)))
  15205. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15206. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15207. (time-to-days (current-time))) (match-string 0 s)
  15208. prefer show-all))
  15209. (t (time-to-days
  15210. (condition-case errdata
  15211. (apply 'encode-time (org-parse-time-string s))
  15212. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15213. s (if (not (and buffer pos))
  15214. ""
  15215. (format " at %d in buffer `%s'" pos buffer))
  15216. (cdr errdata))))))))
  15217. (defun org-days-to-iso-week (days)
  15218. "Return the iso week number."
  15219. (require 'cal-iso)
  15220. (car (calendar-iso-from-absolute days)))
  15221. (defun org-small-year-to-year (year)
  15222. "Convert 2-digit years into 4-digit years.
  15223. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15224. The year 2000 cannot be abbreviated. Any year larger than 99
  15225. is returned unchanged."
  15226. (if (< year 38)
  15227. (setq year (+ 2000 year))
  15228. (if (< year 100)
  15229. (setq year (+ 1900 year))))
  15230. year)
  15231. (defun org-time-from-absolute (d)
  15232. "Return the time corresponding to date D.
  15233. D may be an absolute day number, or a calendar-type list (month day year)."
  15234. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15235. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15236. (defun org-calendar-holiday ()
  15237. "List of holidays, for Diary display in Org-mode."
  15238. (require 'holidays)
  15239. (let ((hl (funcall
  15240. (if (fboundp 'calendar-check-holidays)
  15241. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15242. (if hl (mapconcat 'identity hl "; "))))
  15243. (defun org-diary-sexp-entry (sexp entry date)
  15244. "Process a SEXP diary ENTRY for DATE."
  15245. (require 'diary-lib)
  15246. (let ((result (if calendar-debug-sexp
  15247. (let ((stack-trace-on-error t))
  15248. (eval (car (read-from-string sexp))))
  15249. (condition-case nil
  15250. (eval (car (read-from-string sexp)))
  15251. (error
  15252. (beep)
  15253. (message "Bad sexp at line %d in %s: %s"
  15254. (org-current-line)
  15255. (buffer-file-name) sexp)
  15256. (sleep-for 2))))))
  15257. (cond ((stringp result) (split-string result "; "))
  15258. ((and (consp result)
  15259. (not (consp (cdr result)))
  15260. (stringp (cdr result))) (cdr result))
  15261. ((and (consp result)
  15262. (stringp (car result))) result)
  15263. (result entry))))
  15264. (defun org-diary-to-ical-string (frombuf)
  15265. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15266. This uses the icalendar.el library."
  15267. (let* ((tmpdir (if (featurep 'xemacs)
  15268. (temp-directory)
  15269. temporary-file-directory))
  15270. (tmpfile (make-temp-name
  15271. (expand-file-name "orgics" tmpdir)))
  15272. buf rtn b e)
  15273. (with-current-buffer frombuf
  15274. (icalendar-export-region (point-min) (point-max) tmpfile)
  15275. (setq buf (find-buffer-visiting tmpfile))
  15276. (set-buffer buf)
  15277. (goto-char (point-min))
  15278. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15279. (setq b (match-beginning 0)))
  15280. (goto-char (point-max))
  15281. (if (re-search-backward "^END:VEVENT" nil t)
  15282. (setq e (match-end 0)))
  15283. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15284. (kill-buffer buf)
  15285. (delete-file tmpfile)
  15286. rtn))
  15287. (defun org-closest-date (start current change prefer show-all)
  15288. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15289. When PREFER is `past', return a date that is either CURRENT or past.
  15290. When PREFER is `future', return a date that is either CURRENT or future.
  15291. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15292. ;; Make the proper lists from the dates
  15293. (catch 'exit
  15294. (let ((a1 '(("h" . hour)
  15295. ("d" . day)
  15296. ("w" . week)
  15297. ("m" . month)
  15298. ("y" . year)))
  15299. (shour (nth 2 (org-parse-time-string start)))
  15300. dn dw sday cday n1 n2 n0
  15301. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15302. (setq start (org-date-to-gregorian start)
  15303. current (org-date-to-gregorian
  15304. (if show-all
  15305. current
  15306. (time-to-days (current-time))))
  15307. sday (calendar-absolute-from-gregorian start)
  15308. cday (calendar-absolute-from-gregorian current))
  15309. (if (<= cday sday) (throw 'exit sday))
  15310. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15311. (setq dn (string-to-number (match-string 1 change))
  15312. dw (cdr (assoc (match-string 2 change) a1)))
  15313. (error "Invalid change specifier: %s" change))
  15314. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15315. (cond
  15316. ((eq dw 'hour)
  15317. (let ((missing-hours
  15318. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15319. dn)))
  15320. (setq n1 (if (zerop missing-hours) cday
  15321. (- cday (1+ (floor (/ missing-hours 24)))))
  15322. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15323. ((eq dw 'day)
  15324. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15325. n2 (+ n1 dn)))
  15326. ((eq dw 'year)
  15327. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15328. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15329. (setq date1 (list m d y1)
  15330. n1 (calendar-absolute-from-gregorian date1)
  15331. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15332. n2 (calendar-absolute-from-gregorian date2)))
  15333. ((eq dw 'month)
  15334. ;; approx number of month between the two dates
  15335. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15336. ;; How often does dn fit in there?
  15337. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15338. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15339. m (+ m nm)
  15340. ny (floor (/ m 12))
  15341. y (+ y ny)
  15342. m (- m (* ny 12)))
  15343. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15344. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15345. (setq m2 (+ m dn) y2 y)
  15346. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15347. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15348. (while (<= n2 cday)
  15349. (setq n1 n2 m m2 y y2)
  15350. (setq m2 (+ m dn) y2 y)
  15351. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15352. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15353. ;; Make sure n1 is the earlier date
  15354. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15355. (if show-all
  15356. (cond
  15357. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15358. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15359. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15360. (cond
  15361. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15362. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15363. (t (if (= cday n1) n1 n2)))))))
  15364. (defun org-date-to-gregorian (date)
  15365. "Turn any specification of DATE into a Gregorian date for the calendar."
  15366. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15367. ((and (listp date) (= (length date) 3)) date)
  15368. ((stringp date)
  15369. (setq date (org-parse-time-string date))
  15370. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15371. ((listp date)
  15372. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15373. (defun org-parse-time-string (s &optional nodefault)
  15374. "Parse the standard Org-mode time string.
  15375. This should be a lot faster than the normal `parse-time-string'.
  15376. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15377. hour and minute fields will be nil if not given."
  15378. (cond ((string-match org-ts-regexp0 s)
  15379. (list 0
  15380. (if (or (match-beginning 8) (not nodefault))
  15381. (string-to-number (or (match-string 8 s) "0")))
  15382. (if (or (match-beginning 7) (not nodefault))
  15383. (string-to-number (or (match-string 7 s) "0")))
  15384. (string-to-number (match-string 4 s))
  15385. (string-to-number (match-string 3 s))
  15386. (string-to-number (match-string 2 s))
  15387. nil nil nil))
  15388. ((string-match "^<[^>]+>$" s)
  15389. (decode-time (seconds-to-time (org-matcher-time s))))
  15390. (t (error "Not a standard Org-mode time string: %s" s))))
  15391. (defun org-timestamp-up (&optional arg)
  15392. "Increase the date item at the cursor by one.
  15393. If the cursor is on the year, change the year. If it is on the month,
  15394. the day or the time, change that.
  15395. With prefix ARG, change by that many units."
  15396. (interactive "p")
  15397. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15398. (defun org-timestamp-down (&optional arg)
  15399. "Decrease the date item at the cursor by one.
  15400. If the cursor is on the year, change the year. If it is on the month,
  15401. the day or the time, change that.
  15402. With prefix ARG, change by that many units."
  15403. (interactive "p")
  15404. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15405. (defun org-timestamp-up-day (&optional arg)
  15406. "Increase the date in the time stamp by one day.
  15407. With prefix ARG, change that many days."
  15408. (interactive "p")
  15409. (if (and (not (org-at-timestamp-p t))
  15410. (org-at-heading-p))
  15411. (org-todo 'up)
  15412. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15413. (defun org-timestamp-down-day (&optional arg)
  15414. "Decrease the date in the time stamp by one day.
  15415. With prefix ARG, change that many days."
  15416. (interactive "p")
  15417. (if (and (not (org-at-timestamp-p t))
  15418. (org-at-heading-p))
  15419. (org-todo 'down)
  15420. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15421. (defun org-at-timestamp-p (&optional inactive-ok)
  15422. "Determine if the cursor is in or at a timestamp."
  15423. (interactive)
  15424. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15425. (pos (point))
  15426. (ans (or (looking-at tsr)
  15427. (save-excursion
  15428. (skip-chars-backward "^[<\n\r\t")
  15429. (if (> (point) (point-min)) (backward-char 1))
  15430. (and (looking-at tsr)
  15431. (> (- (match-end 0) pos) -1))))))
  15432. (and ans
  15433. (boundp 'org-ts-what)
  15434. (setq org-ts-what
  15435. (cond
  15436. ((= pos (match-beginning 0)) 'bracket)
  15437. ;; Point is considered to be "on the bracket" whether
  15438. ;; it's really on it or right after it.
  15439. ((= pos (1- (match-end 0))) 'bracket)
  15440. ((= pos (match-end 0)) 'after)
  15441. ((org-pos-in-match-range pos 2) 'year)
  15442. ((org-pos-in-match-range pos 3) 'month)
  15443. ((org-pos-in-match-range pos 7) 'hour)
  15444. ((org-pos-in-match-range pos 8) 'minute)
  15445. ((or (org-pos-in-match-range pos 4)
  15446. (org-pos-in-match-range pos 5)) 'day)
  15447. ((and (> pos (or (match-end 8) (match-end 5)))
  15448. (< pos (match-end 0)))
  15449. (- pos (or (match-end 8) (match-end 5))))
  15450. (t 'day))))
  15451. ans))
  15452. (defun org-toggle-timestamp-type ()
  15453. "Toggle the type (<active> or [inactive]) of a time stamp."
  15454. (interactive)
  15455. (when (org-at-timestamp-p t)
  15456. (let ((beg (match-beginning 0)) (end (match-end 0))
  15457. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15458. (save-excursion
  15459. (goto-char beg)
  15460. (while (re-search-forward "[][<>]" end t)
  15461. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15462. t t)))
  15463. (message "Timestamp is now %sactive"
  15464. (if (equal (char-after beg) ?<) "" "in")))))
  15465. (defun org-at-clock-log-p nil
  15466. "Is the cursor on the clock log line?"
  15467. (save-excursion
  15468. (move-beginning-of-line 1)
  15469. (looking-at "^[ \t]*CLOCK:")))
  15470. (defvar org-clock-history) ; defined in org-clock.el
  15471. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15472. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15473. "Change the date in the time stamp at point.
  15474. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15475. `year', `minute', `second'. If WHAT is not given, the cursor position
  15476. in the timestamp determines what will be changed.
  15477. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15478. (let ((origin (point)) origin-cat
  15479. with-hm inactive
  15480. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15481. org-ts-what
  15482. extra rem
  15483. ts time time0 fixnext clrgx)
  15484. (if (not (org-at-timestamp-p t))
  15485. (error "Not at a timestamp"))
  15486. (if (and (not what) (eq org-ts-what 'bracket))
  15487. (org-toggle-timestamp-type)
  15488. ;; Point isn't on brackets. Remember the part of the time-stamp
  15489. ;; the point was in. Indeed, size of time-stamps may change,
  15490. ;; but point must be kept in the same category nonetheless.
  15491. (setq origin-cat org-ts-what)
  15492. (if (and (not what) (not (eq org-ts-what 'day))
  15493. org-display-custom-times
  15494. (get-text-property (point) 'display)
  15495. (not (get-text-property (1- (point)) 'display)))
  15496. (setq org-ts-what 'day))
  15497. (setq org-ts-what (or what org-ts-what)
  15498. inactive (= (char-after (match-beginning 0)) ?\[)
  15499. ts (match-string 0))
  15500. (replace-match "")
  15501. (when (string-match
  15502. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15503. ts)
  15504. (setq extra (match-string 1 ts))
  15505. (if suppress-tmp-delay
  15506. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15507. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15508. (setq with-hm t))
  15509. (setq time0 (org-parse-time-string ts))
  15510. (when (and updown
  15511. (eq org-ts-what 'minute)
  15512. (not current-prefix-arg))
  15513. ;; This looks like s-up and s-down. Change by one rounding step.
  15514. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15515. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15516. (setcar (cdr time0) (+ (nth 1 time0)
  15517. (if (> n 0) (- rem) (- dm rem))))))
  15518. (setq time
  15519. (encode-time (or (car time0) 0)
  15520. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15521. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15522. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15523. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15524. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15525. (nthcdr 6 time0)))
  15526. (when (and (member org-ts-what '(hour minute))
  15527. extra
  15528. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15529. (setq extra (org-modify-ts-extra
  15530. extra
  15531. (if (eq org-ts-what 'hour) 2 5)
  15532. n dm)))
  15533. (when (integerp org-ts-what)
  15534. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15535. (if (eq what 'calendar)
  15536. (let ((cal-date (org-get-date-from-calendar)))
  15537. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15538. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15539. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15540. (setcar time0 (or (car time0) 0))
  15541. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15542. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15543. (setq time (apply 'encode-time time0))))
  15544. ;; Insert the new time-stamp, and ensure point stays in the same
  15545. ;; category as before (i.e. not after the last position in that
  15546. ;; category).
  15547. (let ((pos (point)))
  15548. ;; Stay before inserted string. `save-excursion' is of no use.
  15549. (setq org-last-changed-timestamp
  15550. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15551. (goto-char pos))
  15552. (save-match-data
  15553. (looking-at org-ts-regexp3)
  15554. (goto-char (cond
  15555. ;; `day' category ends before `hour' if any, or at
  15556. ;; the end of the day name.
  15557. ((eq origin-cat 'day)
  15558. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15559. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15560. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15561. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15562. ;; `year' and `month' have both fixed size: point
  15563. ;; couldn't have moved into another part.
  15564. (t origin))))
  15565. ;; Update clock if on a CLOCK line.
  15566. (org-clock-update-time-maybe)
  15567. ;; Maybe adjust the closest clock in `org-clock-history'
  15568. (when org-clock-adjust-closest
  15569. (if (not (and (org-at-clock-log-p)
  15570. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15571. org-clock-history))))))
  15572. (message "No clock to adjust")
  15573. (cond ((save-excursion ; fix previous clock?
  15574. (re-search-backward org-ts-regexp0 nil t)
  15575. (org-looking-back (concat org-clock-string " \\[")))
  15576. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15577. ((save-excursion ; fix next clock?
  15578. (re-search-backward org-ts-regexp0 nil t)
  15579. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15580. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15581. (save-window-excursion
  15582. ;; Find closest clock to point, adjust the previous/next one in history
  15583. (let* ((p (save-excursion (org-back-to-heading t)))
  15584. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15585. (clfixnth
  15586. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15587. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15588. (if (not clfixpos)
  15589. (message "No clock to adjust")
  15590. (save-excursion
  15591. (org-goto-marker-or-bmk clfixpos)
  15592. (org-show-subtree)
  15593. (when (re-search-forward clrgx nil t)
  15594. (goto-char (match-beginning 1))
  15595. (let (org-clock-adjust-closest)
  15596. (org-timestamp-change n org-ts-what updown))
  15597. (message "Clock adjusted in %s for heading: %s"
  15598. (file-name-nondirectory (buffer-file-name))
  15599. (org-get-heading t t)))))))))
  15600. ;; Try to recenter the calendar window, if any.
  15601. (if (and org-calendar-follow-timestamp-change
  15602. (get-buffer-window "*Calendar*" t)
  15603. (memq org-ts-what '(day month year)))
  15604. (org-recenter-calendar (time-to-days time))))))
  15605. (defun org-modify-ts-extra (s pos n dm)
  15606. "Change the different parts of the lead-time and repeat fields in timestamp."
  15607. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15608. ng h m new rem)
  15609. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15610. (cond
  15611. ((or (org-pos-in-match-range pos 2)
  15612. (org-pos-in-match-range pos 3))
  15613. (setq m (string-to-number (match-string 3 s))
  15614. h (string-to-number (match-string 2 s)))
  15615. (if (org-pos-in-match-range pos 2)
  15616. (setq h (+ h n))
  15617. (setq n (* dm (org-no-warnings (signum n))))
  15618. (when (not (= 0 (setq rem (% m dm))))
  15619. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15620. (setq m (+ m n)))
  15621. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15622. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15623. (setq h (min 24 (max 0 h)))
  15624. (setq ng 1 new (format "-%02d:%02d" h m)))
  15625. ((org-pos-in-match-range pos 6)
  15626. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15627. ((org-pos-in-match-range pos 5)
  15628. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15629. ((org-pos-in-match-range pos 9)
  15630. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15631. ((org-pos-in-match-range pos 8)
  15632. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15633. (when ng
  15634. (setq s (concat
  15635. (substring s 0 (match-beginning ng))
  15636. new
  15637. (substring s (match-end ng))))))
  15638. s))
  15639. (defun org-recenter-calendar (date)
  15640. "If the calendar is visible, recenter it to DATE."
  15641. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15642. (when cwin
  15643. (let ((calendar-move-hook nil))
  15644. (with-selected-window cwin
  15645. (calendar-goto-date (if (listp date) date
  15646. (calendar-gregorian-from-absolute date))))))))
  15647. (defun org-goto-calendar (&optional arg)
  15648. "Go to the Emacs calendar at the current date.
  15649. If there is a time stamp in the current line, go to that date.
  15650. A prefix ARG can be used to force the current date."
  15651. (interactive "P")
  15652. (let ((tsr org-ts-regexp) diff
  15653. (calendar-move-hook nil)
  15654. (calendar-view-holidays-initially-flag nil)
  15655. (calendar-view-diary-initially-flag nil))
  15656. (if (or (org-at-timestamp-p)
  15657. (save-excursion
  15658. (beginning-of-line 1)
  15659. (looking-at (concat ".*" tsr))))
  15660. (let ((d1 (time-to-days (current-time)))
  15661. (d2 (time-to-days
  15662. (org-time-string-to-time (match-string 1)))))
  15663. (setq diff (- d2 d1))))
  15664. (calendar)
  15665. (calendar-goto-today)
  15666. (if (and diff (not arg)) (calendar-forward-day diff))))
  15667. (defun org-get-date-from-calendar ()
  15668. "Return a list (month day year) of date at point in calendar."
  15669. (with-current-buffer "*Calendar*"
  15670. (save-match-data
  15671. (calendar-cursor-to-date))))
  15672. (defun org-date-from-calendar ()
  15673. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15674. If there is already a time stamp at the cursor position, update it."
  15675. (interactive)
  15676. (if (org-at-timestamp-p t)
  15677. (org-timestamp-change 0 'calendar)
  15678. (let ((cal-date (org-get-date-from-calendar)))
  15679. (org-insert-time-stamp
  15680. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15681. (defcustom org-effort-durations
  15682. `(("h" . 60)
  15683. ("d" . ,(* 60 8))
  15684. ("w" . ,(* 60 8 5))
  15685. ("m" . ,(* 60 8 5 4))
  15686. ("y" . ,(* 60 8 5 40)))
  15687. "Conversion factor to minutes for an effort modifier.
  15688. Each entry has the form (MODIFIER . MINUTES).
  15689. In an effort string, a number followed by MODIFIER is multiplied
  15690. by the specified number of MINUTES to obtain an effort in
  15691. minutes.
  15692. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15693. effort string \"2hours\" is equivalent to 120 minutes."
  15694. :group 'org-agenda
  15695. :version "24.1"
  15696. :type '(alist :key-type (string :tag "Modifier")
  15697. :value-type (number :tag "Minutes")))
  15698. (defun org-minutes-to-clocksum-string (m)
  15699. "Format number of minutes as a clocksum string.
  15700. The format is determined by `org-time-clocksum-format',
  15701. `org-time-clocksum-use-fractional' and
  15702. `org-time-clocksum-fractional-format' and
  15703. `org-time-clocksum-use-effort-durations'."
  15704. (let ((clocksum "") h d w mo y fmt n)
  15705. (setq h (if org-time-clocksum-use-effort-durations
  15706. (cdr (assoc "h" org-effort-durations)) 60)
  15707. d (if org-time-clocksum-use-effort-durations
  15708. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  15709. w (if org-time-clocksum-use-effort-durations
  15710. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  15711. mo (if org-time-clocksum-use-effort-durations
  15712. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  15713. y (if org-time-clocksum-use-effort-durations
  15714. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  15715. ;; fractional format
  15716. (if org-time-clocksum-use-fractional
  15717. (cond
  15718. ;; single format string
  15719. ((stringp org-time-clocksum-fractional-format)
  15720. (format org-time-clocksum-fractional-format (/ m (float h))))
  15721. ;; choice of fractional formats for different time units
  15722. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15723. (> (/ (truncate m) (* y d h)) 0))
  15724. (format fmt (/ m (* y d (float h)))))
  15725. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15726. (> (/ (truncate m) (* mo d h)) 0))
  15727. (format fmt (/ m (* mo d (float h)))))
  15728. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15729. (> (/ (truncate m) (* w d h)) 0))
  15730. (format fmt (/ m (* w d (float h)))))
  15731. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15732. (> (/ (truncate m) (* d h)) 0))
  15733. (format fmt (/ m (* d (float h)))))
  15734. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15735. (> (/ (truncate m) h) 0))
  15736. (format fmt (/ m (float h))))
  15737. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  15738. (format fmt m))
  15739. ;; fall back to smallest time unit with a format
  15740. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15741. (format fmt (/ m (float h))))
  15742. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15743. (format fmt (/ m (* d (float h)))))
  15744. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15745. (format fmt (/ m (* w d (float h)))))
  15746. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15747. (format fmt (/ m (* mo d (float h)))))
  15748. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15749. (format fmt (/ m (* y d (float h))))))
  15750. ;; standard (non-fractional) format, with single format string
  15751. (if (stringp org-time-clocksum-format)
  15752. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  15753. ;; separate formats components
  15754. (and (setq fmt (plist-get org-time-clocksum-format :years))
  15755. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  15756. (plist-get org-time-clocksum-format :require-years))
  15757. (setq clocksum (concat clocksum (format fmt n))
  15758. m (- m (* n y d h))))
  15759. (and (setq fmt (plist-get org-time-clocksum-format :months))
  15760. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  15761. (plist-get org-time-clocksum-format :require-months))
  15762. (setq clocksum (concat clocksum (format fmt n))
  15763. m (- m (* n mo d h))))
  15764. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  15765. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  15766. (plist-get org-time-clocksum-format :require-weeks))
  15767. (setq clocksum (concat clocksum (format fmt n))
  15768. m (- m (* n w d h))))
  15769. (and (setq fmt (plist-get org-time-clocksum-format :days))
  15770. (or (> (setq n (/ (truncate m) (* d h))) 0)
  15771. (plist-get org-time-clocksum-format :require-days))
  15772. (setq clocksum (concat clocksum (format fmt n))
  15773. m (- m (* n d h))))
  15774. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  15775. (or (> (setq n (/ (truncate m) h)) 0)
  15776. (plist-get org-time-clocksum-format :require-hours))
  15777. (setq clocksum (concat clocksum (format fmt n))
  15778. m (- m (* n h))))
  15779. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  15780. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  15781. (setq clocksum (concat clocksum (format fmt m))))
  15782. ;; return formatted time duration
  15783. clocksum))))
  15784. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  15785. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  15786. "Org mode version 8.0")
  15787. (defun org-hours-to-clocksum-string (n)
  15788. (org-minutes-to-clocksum-string (* n 60)))
  15789. (defun org-hh:mm-string-to-minutes (s)
  15790. "Convert a string H:MM to a number of minutes.
  15791. If the string is just a number, interpret it as minutes.
  15792. In fact, the first hh:mm or number in the string will be taken,
  15793. there can be extra stuff in the string.
  15794. If no number is found, the return value is 0."
  15795. (cond
  15796. ((integerp s) s)
  15797. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15798. (+ (* (string-to-number (match-string 1 s)) 60)
  15799. (string-to-number (match-string 2 s))))
  15800. ((string-match "\\([0-9]+\\)" s)
  15801. (string-to-number (match-string 1 s)))
  15802. (t 0)))
  15803. (defcustom org-image-actual-width t
  15804. "Should we use the actual width of images when inlining them?
  15805. When set to `t', always use the image width.
  15806. When set to a number, use imagemagick (when available) to set
  15807. the image's width to this value.
  15808. When set to a number in a list, try to get the width from the
  15809. #+ATTR.* keyword if it matches a width specification like
  15810. width=\"[0-9]+\", and fall back on that number if none is found.
  15811. When set to nil, try to get the width from an #+ATTR.* keyword
  15812. and fall back on the original width if none is found.
  15813. This requires Emacs >= 24.1, build with imagemagick support."
  15814. :group 'org-appearance
  15815. :version "24.3"
  15816. :type '(choice
  15817. (const :tag "Use the image width" t)
  15818. (integer :tag "Use a number of pixels")
  15819. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15820. (const :tag "Use #+ATTR* or don't resize" nil)))
  15821. (defcustom org-agenda-inhibit-startup t
  15822. "Inhibit startup when preparing agenda buffers.
  15823. When this variable is `t' (the default), the initialization of
  15824. the Org agenda buffers is inhibited: e.g. the visibility state
  15825. is not set, the tables are not re-aligned, etc."
  15826. :type 'boolean
  15827. :version "24.3"
  15828. :group 'org-agenda)
  15829. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15830. "Convert a duration string S to minutes.
  15831. A bare number is interpreted as minutes, modifiers can be set by
  15832. customizing `org-effort-durations' (which see).
  15833. Entries containing a colon are interpreted as H:MM by
  15834. `org-hh:mm-string-to-minutes'."
  15835. (let ((result 0)
  15836. (re (concat "\\([0-9.]+\\) *\\("
  15837. (regexp-opt (mapcar 'car org-effort-durations))
  15838. "\\)")))
  15839. (while (string-match re s)
  15840. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15841. (string-to-number (match-string 1 s))))
  15842. (setq s (replace-match "" nil t s)))
  15843. (setq result (floor result))
  15844. (incf result (org-hh:mm-string-to-minutes s))
  15845. (if output-to-string (number-to-string result) result)))
  15846. ;;;; Files
  15847. (defun org-save-all-org-buffers ()
  15848. "Save all Org-mode buffers without user confirmation."
  15849. (interactive)
  15850. (message "Saving all Org-mode buffers...")
  15851. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15852. (when (featurep 'org-id) (org-id-locations-save))
  15853. (message "Saving all Org-mode buffers... done"))
  15854. (defun org-revert-all-org-buffers ()
  15855. "Revert all Org-mode buffers.
  15856. Prompt for confirmation when there are unsaved changes.
  15857. Be sure you know what you are doing before letting this function
  15858. overwrite your changes.
  15859. This function is useful in a setup where one tracks org files
  15860. with a version control system, to revert on one machine after pulling
  15861. changes from another. I believe the procedure must be like this:
  15862. 1. M-x org-save-all-org-buffers
  15863. 2. Pull changes from the other machine, resolve conflicts
  15864. 3. M-x org-revert-all-org-buffers"
  15865. (interactive)
  15866. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15867. (error "Abort"))
  15868. (save-excursion
  15869. (save-window-excursion
  15870. (mapc
  15871. (lambda (b)
  15872. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15873. (with-current-buffer b buffer-file-name))
  15874. (org-pop-to-buffer-same-window b)
  15875. (revert-buffer t 'no-confirm)))
  15876. (buffer-list))
  15877. (when (and (featurep 'org-id) org-id-track-globally)
  15878. (org-id-locations-load)))))
  15879. ;;;; Agenda files
  15880. ;;;###autoload
  15881. (defun org-switchb (&optional arg)
  15882. "Switch between Org buffers.
  15883. With one prefix argument, restrict available buffers to files.
  15884. With two prefix arguments, restrict available buffers to agenda files.
  15885. Defaults to `iswitchb' for buffer name completion.
  15886. Set `org-completion-use-ido' to make it use ido instead."
  15887. (interactive "P")
  15888. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15889. ((equal arg '(16)) (org-buffer-list 'agenda))
  15890. (t (org-buffer-list))))
  15891. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15892. (org-completion-use-ido org-completion-use-ido))
  15893. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15894. (setq org-completion-use-iswitchb t))
  15895. (org-pop-to-buffer-same-window
  15896. (org-icompleting-read "Org buffer: "
  15897. (mapcar 'list (mapcar 'buffer-name blist))
  15898. nil t))))
  15899. ;;; Define some older names previously used for this functionality
  15900. ;;;###autoload
  15901. (defalias 'org-ido-switchb 'org-switchb)
  15902. ;;;###autoload
  15903. (defalias 'org-iswitchb 'org-switchb)
  15904. (defun org-buffer-list (&optional predicate exclude-tmp)
  15905. "Return a list of Org buffers.
  15906. PREDICATE can be `export', `files' or `agenda'.
  15907. export restrict the list to Export buffers.
  15908. files restrict the list to buffers visiting Org files.
  15909. agenda restrict the list to buffers visiting agenda files.
  15910. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15911. (let* ((bfn nil)
  15912. (agenda-files (and (eq predicate 'agenda)
  15913. (mapcar 'file-truename (org-agenda-files t))))
  15914. (filter
  15915. (cond
  15916. ((eq predicate 'files)
  15917. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15918. ((eq predicate 'export)
  15919. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15920. ((eq predicate 'agenda)
  15921. (lambda (b)
  15922. (with-current-buffer b
  15923. (and (derived-mode-p 'org-mode)
  15924. (setq bfn (buffer-file-name b))
  15925. (member (file-truename bfn) agenda-files)))))
  15926. (t (lambda (b) (with-current-buffer b
  15927. (or (derived-mode-p 'org-mode)
  15928. (string-match "\*Org .*Export"
  15929. (buffer-name b)))))))))
  15930. (delq nil
  15931. (mapcar
  15932. (lambda(b)
  15933. (if (and (funcall filter b)
  15934. (or (not exclude-tmp)
  15935. (not (string-match "tmp" (buffer-name b)))))
  15936. b
  15937. nil))
  15938. (buffer-list)))))
  15939. (defun org-agenda-files (&optional unrestricted archives)
  15940. "Get the list of agenda files.
  15941. Optional UNRESTRICTED means return the full list even if a restriction
  15942. is currently in place.
  15943. When ARCHIVES is t, include all archive files that are really being
  15944. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15945. `org-agenda-archives-mode' is t."
  15946. (let ((files
  15947. (cond
  15948. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15949. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15950. ((listp org-agenda-files) org-agenda-files)
  15951. (t (error "Invalid value of `org-agenda-files'")))))
  15952. (setq files (apply 'append
  15953. (mapcar (lambda (f)
  15954. (if (file-directory-p f)
  15955. (directory-files
  15956. f t org-agenda-file-regexp)
  15957. (list f)))
  15958. files)))
  15959. (when org-agenda-skip-unavailable-files
  15960. (setq files (delq nil
  15961. (mapcar (function
  15962. (lambda (file)
  15963. (and (file-readable-p file) file)))
  15964. files))))
  15965. (when (or (eq archives t)
  15966. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15967. (setq files (org-add-archive-files files)))
  15968. files))
  15969. (defun org-agenda-file-p (&optional file)
  15970. "Return non-nil, if FILE is an agenda file.
  15971. If FILE is omitted, use the file associated with the current
  15972. buffer."
  15973. (member (or file (buffer-file-name))
  15974. (org-agenda-files t)))
  15975. (defun org-edit-agenda-file-list ()
  15976. "Edit the list of agenda files.
  15977. Depending on setup, this either uses customize to edit the variable
  15978. `org-agenda-files', or it visits the file that is holding the list. In the
  15979. latter case, the buffer is set up in a way that saving it automatically kills
  15980. the buffer and restores the previous window configuration."
  15981. (interactive)
  15982. (if (stringp org-agenda-files)
  15983. (let ((cw (current-window-configuration)))
  15984. (find-file org-agenda-files)
  15985. (org-set-local 'org-window-configuration cw)
  15986. (org-add-hook 'after-save-hook
  15987. (lambda ()
  15988. (set-window-configuration
  15989. (prog1 org-window-configuration
  15990. (kill-buffer (current-buffer))))
  15991. (org-install-agenda-files-menu)
  15992. (message "New agenda file list installed"))
  15993. nil 'local)
  15994. (message "%s" (substitute-command-keys
  15995. "Edit list and finish with \\[save-buffer]")))
  15996. (customize-variable 'org-agenda-files)))
  15997. (defun org-store-new-agenda-file-list (list)
  15998. "Set new value for the agenda file list and save it correctly."
  15999. (if (stringp org-agenda-files)
  16000. (let ((fe (org-read-agenda-file-list t)) b u)
  16001. (while (setq b (find-buffer-visiting org-agenda-files))
  16002. (kill-buffer b))
  16003. (with-temp-file org-agenda-files
  16004. (insert
  16005. (mapconcat
  16006. (lambda (f) ;; Keep un-expanded entries.
  16007. (if (setq u (assoc f fe))
  16008. (cdr u)
  16009. f))
  16010. list "\n")
  16011. "\n")))
  16012. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16013. (org-insert-mode-line-in-empty-file nil))
  16014. (setq org-agenda-files list)
  16015. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16016. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16017. "Read the list of agenda files from a file.
  16018. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16019. filenames, used by `org-store-new-agenda-file-list' to write back
  16020. un-expanded file names."
  16021. (when (file-directory-p org-agenda-files)
  16022. (error "`org-agenda-files' cannot be a single directory"))
  16023. (when (stringp org-agenda-files)
  16024. (with-temp-buffer
  16025. (insert-file-contents org-agenda-files)
  16026. (mapcar
  16027. (lambda (f)
  16028. (let ((e (expand-file-name (substitute-in-file-name f)
  16029. org-directory)))
  16030. (if pair-with-expansion
  16031. (cons e f)
  16032. e)))
  16033. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16034. ;;;###autoload
  16035. (defun org-cycle-agenda-files ()
  16036. "Cycle through the files in `org-agenda-files'.
  16037. If the current buffer visits an agenda file, find the next one in the list.
  16038. If the current buffer does not, find the first agenda file."
  16039. (interactive)
  16040. (let* ((fs (org-agenda-files t))
  16041. (files (append fs (list (car fs))))
  16042. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16043. file)
  16044. (unless files (error "No agenda files"))
  16045. (catch 'exit
  16046. (while (setq file (pop files))
  16047. (if (equal (file-truename file) tcf)
  16048. (when (car files)
  16049. (find-file (car files))
  16050. (throw 'exit t))))
  16051. (find-file (car fs)))
  16052. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16053. (defun org-agenda-file-to-front (&optional to-end)
  16054. "Move/add the current file to the top of the agenda file list.
  16055. If the file is not present in the list, it is added to the front. If it is
  16056. present, it is moved there. With optional argument TO-END, add/move to the
  16057. end of the list."
  16058. (interactive "P")
  16059. (let ((org-agenda-skip-unavailable-files nil)
  16060. (file-alist (mapcar (lambda (x)
  16061. (cons (file-truename x) x))
  16062. (org-agenda-files t)))
  16063. (ctf (file-truename
  16064. (or buffer-file-name
  16065. (error "Please save the current buffer to a file"))))
  16066. x had)
  16067. (setq x (assoc ctf file-alist) had x)
  16068. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16069. (if to-end
  16070. (setq file-alist (append (delq x file-alist) (list x)))
  16071. (setq file-alist (cons x (delq x file-alist))))
  16072. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16073. (org-install-agenda-files-menu)
  16074. (message "File %s to %s of agenda file list"
  16075. (if had "moved" "added") (if to-end "end" "front"))))
  16076. (defun org-remove-file (&optional file)
  16077. "Remove current file from the list of files in variable `org-agenda-files'.
  16078. These are the files which are being checked for agenda entries.
  16079. Optional argument FILE means use this file instead of the current."
  16080. (interactive)
  16081. (let* ((org-agenda-skip-unavailable-files nil)
  16082. (file (or file buffer-file-name
  16083. (error "Current buffer does not visit a file")))
  16084. (true-file (file-truename file))
  16085. (afile (abbreviate-file-name file))
  16086. (files (delq nil (mapcar
  16087. (lambda (x)
  16088. (if (equal true-file
  16089. (file-truename x))
  16090. nil x))
  16091. (org-agenda-files t)))))
  16092. (if (not (= (length files) (length (org-agenda-files t))))
  16093. (progn
  16094. (org-store-new-agenda-file-list files)
  16095. (org-install-agenda-files-menu)
  16096. (message "Removed file: %s" afile))
  16097. (message "File was not in list: %s (not removed)" afile))))
  16098. (defun org-file-menu-entry (file)
  16099. (vector file (list 'find-file file) t))
  16100. (defun org-check-agenda-file (file)
  16101. "Make sure FILE exists. If not, ask user what to do."
  16102. (when (not (file-exists-p file))
  16103. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16104. (abbreviate-file-name file))
  16105. (let ((r (downcase (read-char-exclusive))))
  16106. (cond
  16107. ((equal r ?r)
  16108. (org-remove-file file)
  16109. (throw 'nextfile t))
  16110. (t (error "Abort"))))))
  16111. (defun org-get-agenda-file-buffer (file)
  16112. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16113. it to the list of buffers which might be released later."
  16114. (let ((buf (org-find-base-buffer-visiting file)))
  16115. (if buf
  16116. buf ; just return it
  16117. ;; Make a new buffer and remember it
  16118. (setq buf (find-file-noselect file))
  16119. (if buf (push buf org-agenda-new-buffers))
  16120. buf)))
  16121. (defun org-release-buffers (blist)
  16122. "Release all buffers in list, asking the user for confirmation when needed.
  16123. When a buffer is unmodified, it is just killed. When modified, it is saved
  16124. \(if the user agrees) and then killed."
  16125. (let (buf file)
  16126. (while (setq buf (pop blist))
  16127. (setq file (buffer-file-name buf))
  16128. (when (and (buffer-modified-p buf)
  16129. file
  16130. (y-or-n-p (format "Save file %s? " file)))
  16131. (with-current-buffer buf (save-buffer)))
  16132. (kill-buffer buf))))
  16133. (defun org-agenda-prepare-buffers (files)
  16134. "Create buffers for all agenda files, protect archived trees and comments."
  16135. (interactive)
  16136. (let ((pa '(:org-archived t))
  16137. (pc '(:org-comment t))
  16138. (pall '(:org-archived t :org-comment t))
  16139. (inhibit-read-only t)
  16140. (org-inhibit-startup org-agenda-inhibit-startup)
  16141. (rea (concat ":" org-archive-tag ":"))
  16142. file re)
  16143. (save-excursion
  16144. (save-restriction
  16145. (while (setq file (pop files))
  16146. (catch 'nextfile
  16147. (if (bufferp file)
  16148. (set-buffer file)
  16149. (org-check-agenda-file file)
  16150. (set-buffer (org-get-agenda-file-buffer file)))
  16151. (widen)
  16152. (org-refresh-category-properties)
  16153. (org-refresh-properties org-effort-property 'org-effort)
  16154. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16155. (setq org-todo-keywords-for-agenda
  16156. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16157. (setq org-done-keywords-for-agenda
  16158. (append org-done-keywords-for-agenda org-done-keywords))
  16159. (setq org-todo-keyword-alist-for-agenda
  16160. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16161. (setq org-drawers-for-agenda
  16162. (append org-drawers-for-agenda org-drawers))
  16163. (setq org-tag-alist-for-agenda
  16164. (append org-tag-alist-for-agenda org-tag-alist))
  16165. (with-silent-modifications
  16166. (save-excursion
  16167. (remove-text-properties (point-min) (point-max) pall)
  16168. (when org-agenda-skip-archived-trees
  16169. (goto-char (point-min))
  16170. (while (re-search-forward rea nil t)
  16171. (if (org-at-heading-p t)
  16172. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16173. (goto-char (point-min))
  16174. (setq re (format org-heading-keyword-regexp-format
  16175. org-comment-string))
  16176. (while (re-search-forward re nil t)
  16177. (add-text-properties
  16178. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16179. (setq org-todo-keywords-for-agenda
  16180. (org-uniquify org-todo-keywords-for-agenda))
  16181. (setq org-todo-keyword-alist-for-agenda
  16182. (org-uniquify org-todo-keyword-alist-for-agenda)
  16183. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  16184. ;;;; CDLaTeX minor mode
  16185. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16186. "Keymap for the minor `org-cdlatex-mode'.")
  16187. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16188. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16189. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16190. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16191. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16192. (defvar org-cdlatex-texmathp-advice-is-done nil
  16193. "Flag remembering if we have applied the advice to texmathp already.")
  16194. (define-minor-mode org-cdlatex-mode
  16195. "Toggle the minor `org-cdlatex-mode'.
  16196. This mode supports entering LaTeX environment and math in LaTeX fragments
  16197. in Org-mode.
  16198. \\{org-cdlatex-mode-map}"
  16199. nil " OCDL" nil
  16200. (when org-cdlatex-mode
  16201. (require 'cdlatex)
  16202. (run-hooks 'cdlatex-mode-hook)
  16203. (cdlatex-compute-tables))
  16204. (unless org-cdlatex-texmathp-advice-is-done
  16205. (setq org-cdlatex-texmathp-advice-is-done t)
  16206. (defadvice texmathp (around org-math-always-on activate)
  16207. "Always return t in org-mode buffers.
  16208. This is because we want to insert math symbols without dollars even outside
  16209. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16210. an embedded LaTeX fragment, let texmathp do its job.
  16211. \\[org-cdlatex-mode-map]"
  16212. (interactive)
  16213. (let (p)
  16214. (cond
  16215. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16216. ((eq this-command 'cdlatex-math-symbol)
  16217. (setq ad-return-value t
  16218. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16219. (t
  16220. (let ((p (org-inside-LaTeX-fragment-p)))
  16221. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16222. (setq ad-return-value t
  16223. texmathp-why '("Org-mode embedded math" . 0))
  16224. (if p ad-do-it)))))))))
  16225. (defun turn-on-org-cdlatex ()
  16226. "Unconditionally turn on `org-cdlatex-mode'."
  16227. (org-cdlatex-mode 1))
  16228. (defun org-try-cdlatex-tab ()
  16229. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16230. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16231. - inside a LaTeX fragment, or
  16232. - after the first word in a line, where an abbreviation expansion could
  16233. insert a LaTeX environment."
  16234. (when org-cdlatex-mode
  16235. (cond
  16236. ;; Before any word on the line: No expansion possible.
  16237. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16238. ;; Just after first word on the line: Expand it. Make sure it
  16239. ;; cannot happen on headlines, though.
  16240. ((save-excursion
  16241. (skip-chars-backward "a-zA-Z0-9*")
  16242. (skip-chars-backward " \t")
  16243. (and (bolp) (not (org-at-heading-p))))
  16244. (cdlatex-tab) t)
  16245. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16246. (defun org-cdlatex-underscore-caret (&optional arg)
  16247. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16248. Revert to the normal definition outside of these fragments."
  16249. (interactive "P")
  16250. (if (org-inside-LaTeX-fragment-p)
  16251. (call-interactively 'cdlatex-sub-superscript)
  16252. (let (org-cdlatex-mode)
  16253. (call-interactively (key-binding (vector last-input-event))))))
  16254. (defun org-cdlatex-math-modify (&optional arg)
  16255. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16256. Revert to the normal definition outside of these fragments."
  16257. (interactive "P")
  16258. (if (org-inside-LaTeX-fragment-p)
  16259. (call-interactively 'cdlatex-math-modify)
  16260. (let (org-cdlatex-mode)
  16261. (call-interactively (key-binding (vector last-input-event))))))
  16262. ;;;; LaTeX fragments
  16263. (defvar org-latex-regexps
  16264. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16265. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16266. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16267. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16268. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16269. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16270. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16271. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16272. "Regular expressions for matching embedded LaTeX.")
  16273. (defun org-inside-LaTeX-fragment-p ()
  16274. "Test if point is inside a LaTeX fragment.
  16275. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16276. sequence appearing also before point.
  16277. Even though the matchers for math are configurable, this function assumes
  16278. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16279. delimiters are skipped when they have been removed by customization.
  16280. The return value is nil, or a cons cell with the delimiter and the
  16281. position of this delimiter.
  16282. This function does a reasonably good job, but can locally be fooled by
  16283. for example currency specifications. For example it will assume being in
  16284. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16285. fragments that are properly closed, but during editing, we have to live
  16286. with the uncertainty caused by missing closing delimiters. This function
  16287. looks only before point, not after."
  16288. (catch 'exit
  16289. (let ((pos (point))
  16290. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16291. (lim (progn
  16292. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16293. (point)))
  16294. dd-on str (start 0) m re)
  16295. (goto-char pos)
  16296. (when dodollar
  16297. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16298. re (nth 1 (assoc "$" org-latex-regexps)))
  16299. (while (string-match re str start)
  16300. (cond
  16301. ((= (match-end 0) (length str))
  16302. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16303. ((= (match-end 0) (- (length str) 5))
  16304. (throw 'exit nil))
  16305. (t (setq start (match-end 0))))))
  16306. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16307. (goto-char pos)
  16308. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16309. (and (match-beginning 2) (throw 'exit nil))
  16310. ;; count $$
  16311. (while (re-search-backward "\\$\\$" lim t)
  16312. (setq dd-on (not dd-on)))
  16313. (goto-char pos)
  16314. (if dd-on (cons "$$" m))))))
  16315. (defun org-inside-latex-macro-p ()
  16316. "Is point inside a LaTeX macro or its arguments?"
  16317. (save-match-data
  16318. (org-in-regexp
  16319. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16320. (defvar org-latex-fragment-image-overlays nil
  16321. "List of overlays carrying the images of latex fragments.")
  16322. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16323. (defun org-remove-latex-fragment-image-overlays ()
  16324. "Remove all overlays with LaTeX fragment images in current buffer."
  16325. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16326. (setq org-latex-fragment-image-overlays nil))
  16327. (defun org-preview-latex-fragment (&optional subtree)
  16328. "Preview the LaTeX fragment at point, or all locally or globally.
  16329. If the cursor is in a LaTeX fragment, create the image and overlay
  16330. it over the source code. If there is no fragment at point, display
  16331. all fragments in the current text, from one headline to the next. With
  16332. prefix SUBTREE, display all fragments in the current subtree. With a
  16333. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16334. the cursor is before the first headline,
  16335. display all fragments in the buffer.
  16336. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16337. (interactive "P")
  16338. (unless buffer-file-name
  16339. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16340. (org-remove-latex-fragment-image-overlays)
  16341. (save-excursion
  16342. (save-restriction
  16343. (let (beg end at msg)
  16344. (cond
  16345. ((or (equal subtree '(16))
  16346. (not (save-excursion
  16347. (re-search-backward org-outline-regexp-bol nil t))))
  16348. (setq beg (point-min) end (point-max)
  16349. msg "Creating images for buffer...%s"))
  16350. ((equal subtree '(4))
  16351. (org-back-to-heading)
  16352. (setq beg (point) end (org-end-of-subtree t)
  16353. msg "Creating images for subtree...%s"))
  16354. (t
  16355. (if (setq at (org-inside-LaTeX-fragment-p))
  16356. (goto-char (max (point-min) (- (cdr at) 2)))
  16357. (org-back-to-heading))
  16358. (setq beg (point) end (progn (outline-next-heading) (point))
  16359. msg (if at "Creating image...%s"
  16360. "Creating images for entry...%s"))))
  16361. (message msg "")
  16362. (narrow-to-region beg end)
  16363. (goto-char beg)
  16364. (org-format-latex
  16365. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16366. (file-name-nondirectory
  16367. buffer-file-name)))
  16368. default-directory 'overlays msg at 'forbuffer
  16369. org-latex-create-formula-image-program)
  16370. (message msg "done. Use `C-c C-c' to remove images.")))))
  16371. (defun org-format-latex (prefix &optional dir overlays msg at
  16372. forbuffer processing-type)
  16373. "Replace LaTeX fragments with links to an image, and produce images.
  16374. Some of the options can be changed using the variable
  16375. `org-format-latex-options'."
  16376. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16377. (let* ((prefixnodir (file-name-nondirectory prefix))
  16378. (absprefix (expand-file-name prefix dir))
  16379. (todir (file-name-directory absprefix))
  16380. (opt org-format-latex-options)
  16381. (optnew org-format-latex-options)
  16382. (matchers (plist-get opt :matchers))
  16383. (re-list org-latex-regexps)
  16384. (cnt 0) txt hash link beg end re e checkdir
  16385. string
  16386. m n block-type block linkfile movefile ov)
  16387. ;; Check the different regular expressions
  16388. (while (setq e (pop re-list))
  16389. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16390. block (if block-type "\n\n" ""))
  16391. (when (member m matchers)
  16392. (goto-char (point-min))
  16393. (while (re-search-forward re nil t)
  16394. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16395. (or (not overlays)
  16396. (not (eq (get-char-property (match-beginning n)
  16397. 'org-overlay-type)
  16398. 'org-latex-overlay))))
  16399. (cond
  16400. ((eq processing-type 'verbatim))
  16401. ((eq processing-type 'mathjax)
  16402. ;; Prepare for MathJax processing.
  16403. (setq string (match-string n))
  16404. (when (member m '("$" "$1"))
  16405. (save-excursion
  16406. (delete-region (match-beginning n) (match-end n))
  16407. (goto-char (match-beginning n))
  16408. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16409. ((or (eq processing-type 'dvipng)
  16410. (eq processing-type 'imagemagick))
  16411. ;; Process to an image.
  16412. (setq txt (match-string n)
  16413. beg (match-beginning n) end (match-end n)
  16414. cnt (1+ cnt))
  16415. (let ((face (face-at-point))
  16416. (fg (plist-get opt :foreground))
  16417. (bg (plist-get opt :background))
  16418. ;; Ensure full list is printed.
  16419. print-length print-level)
  16420. (when forbuffer
  16421. ;; Get the colors from the face at point.
  16422. (goto-char beg)
  16423. (when (eq fg 'auto)
  16424. (setq fg (face-attribute face :foreground nil 'default)))
  16425. (when (eq bg 'auto)
  16426. (setq bg (face-attribute face :background nil 'default)))
  16427. (setq optnew (copy-sequence opt))
  16428. (plist-put optnew :foreground fg)
  16429. (plist-put optnew :background bg))
  16430. (setq hash (sha1 (prin1-to-string
  16431. (list org-format-latex-header
  16432. org-latex-default-packages-alist
  16433. org-latex-packages-alist
  16434. org-format-latex-options
  16435. forbuffer txt fg bg)))
  16436. linkfile (format "%s_%s.png" prefix hash)
  16437. movefile (format "%s_%s.png" absprefix hash)))
  16438. (setq link (concat block "[[file:" linkfile "]]" block))
  16439. (if msg (message msg cnt))
  16440. (goto-char beg)
  16441. (unless checkdir ; Ensure the directory exists.
  16442. (setq checkdir t)
  16443. (or (file-directory-p todir) (make-directory todir t)))
  16444. (unless (file-exists-p movefile)
  16445. (org-create-formula-image
  16446. txt movefile optnew forbuffer processing-type))
  16447. (if overlays
  16448. (progn
  16449. (mapc (lambda (o)
  16450. (if (eq (overlay-get o 'org-overlay-type)
  16451. 'org-latex-overlay)
  16452. (delete-overlay o)))
  16453. (overlays-in beg end))
  16454. (setq ov (make-overlay beg end))
  16455. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16456. (if (featurep 'xemacs)
  16457. (progn
  16458. (overlay-put ov 'invisible t)
  16459. (overlay-put
  16460. ov 'end-glyph
  16461. (make-glyph (vector 'png :file movefile))))
  16462. (overlay-put
  16463. ov 'display
  16464. (list 'image :type 'png :file movefile :ascent 'center)))
  16465. (push ov org-latex-fragment-image-overlays)
  16466. (goto-char end))
  16467. (delete-region beg end)
  16468. (insert (org-add-props link
  16469. (list 'org-latex-src
  16470. (replace-regexp-in-string
  16471. "\"" "" txt)
  16472. 'org-latex-src-embed-type
  16473. (if block-type 'paragraph 'character))))))
  16474. ((eq processing-type 'mathml)
  16475. ;; Process to MathML
  16476. (unless (save-match-data (org-format-latex-mathml-available-p))
  16477. (error "LaTeX to MathML converter not configured"))
  16478. (setq txt (match-string n)
  16479. beg (match-beginning n) end (match-end n)
  16480. cnt (1+ cnt))
  16481. (if msg (message msg cnt))
  16482. (goto-char beg)
  16483. (delete-region beg end)
  16484. (insert (org-format-latex-as-mathml
  16485. txt block-type prefix dir)))
  16486. (t
  16487. (error "Unknown conversion type %s for latex fragments"
  16488. processing-type)))))))))
  16489. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16490. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16491. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16492. sucessful, return the portion between \"<math...> </math>\"
  16493. elements otherwise return nil. When MATHML-FILE is specified,
  16494. write the results in to that file. When invoked as an
  16495. interactive command, prompt for LATEX-FRAG, with initial value
  16496. set to the current active region and echo the results for user
  16497. inspection."
  16498. (interactive (list (let ((frag (when (org-region-active-p)
  16499. (buffer-substring-no-properties
  16500. (region-beginning) (region-end)))))
  16501. (read-string "LaTeX Fragment: " frag nil frag))))
  16502. (unless latex-frag (error "Invalid latex-frag"))
  16503. (let* ((tmp-in-file (file-relative-name
  16504. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16505. (ignore (write-region latex-frag nil tmp-in-file))
  16506. (tmp-out-file (file-relative-name
  16507. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16508. (cmd (format-spec
  16509. org-latex-to-mathml-convert-command
  16510. `((?j . ,(shell-quote-argument
  16511. (expand-file-name org-latex-to-mathml-jar-file)))
  16512. (?I . ,(shell-quote-argument tmp-in-file))
  16513. (?o . ,(shell-quote-argument tmp-out-file)))))
  16514. mathml shell-command-output)
  16515. (when (org-called-interactively-p 'any)
  16516. (unless (org-format-latex-mathml-available-p)
  16517. (error "LaTeX to MathML converter not configured")))
  16518. (message "Running %s" cmd)
  16519. (setq shell-command-output (shell-command-to-string cmd))
  16520. (setq mathml
  16521. (when (file-readable-p tmp-out-file)
  16522. (with-current-buffer (find-file-noselect tmp-out-file t)
  16523. (goto-char (point-min))
  16524. (when (re-search-forward
  16525. (concat
  16526. (regexp-quote
  16527. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16528. "\\(.\\|\n\\)*"
  16529. (regexp-quote "</math>")) nil t)
  16530. (prog1 (match-string 0) (kill-buffer))))))
  16531. (cond
  16532. (mathml
  16533. (setq mathml
  16534. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16535. (when mathml-file
  16536. (write-region mathml nil mathml-file))
  16537. (when (org-called-interactively-p 'any)
  16538. (message mathml)))
  16539. ((message "LaTeX to MathML conversion failed")
  16540. (message shell-command-output)))
  16541. (delete-file tmp-in-file)
  16542. (when (file-exists-p tmp-out-file)
  16543. (delete-file tmp-out-file))
  16544. mathml))
  16545. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16546. prefix &optional dir)
  16547. "Use `org-create-math-formula' but check local cache first."
  16548. (let* ((absprefix (expand-file-name prefix dir))
  16549. (print-length nil) (print-level nil)
  16550. (formula-id (concat
  16551. "formula-"
  16552. (sha1
  16553. (prin1-to-string
  16554. (list latex-frag
  16555. org-latex-to-mathml-convert-command)))))
  16556. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16557. (formula-cache-dir (file-name-directory formula-cache)))
  16558. (unless (file-directory-p formula-cache-dir)
  16559. (make-directory formula-cache-dir t))
  16560. (unless (file-exists-p formula-cache)
  16561. (org-create-math-formula latex-frag formula-cache))
  16562. (if (file-exists-p formula-cache)
  16563. ;; Successful conversion. Return the link to MathML file.
  16564. (org-add-props
  16565. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16566. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16567. 'org-latex-src-embed-type (if latex-frag-type
  16568. 'paragraph 'character)))
  16569. ;; Failed conversion. Return the LaTeX fragment verbatim
  16570. latex-frag)))
  16571. (defun org-create-formula-image (string tofile options buffer &optional type)
  16572. "Create an image from LaTeX source using dvipng or convert.
  16573. This function calls either `org-create-formula-image-with-dvipng'
  16574. or `org-create-formula-image-with-imagemagick' depending on the
  16575. value of `org-latex-create-formula-image-program' or on the value
  16576. of the optional TYPE variable.
  16577. Note: ultimately these two function should be combined as they
  16578. share a good deal of logic."
  16579. (org-check-external-command
  16580. "latex" "needed to convert LaTeX fragments to images")
  16581. (funcall
  16582. (case (or type org-latex-create-formula-image-program)
  16583. ('dvipng
  16584. (org-check-external-command
  16585. "dvipng" "needed to convert LaTeX fragments to images")
  16586. #'org-create-formula-image-with-dvipng)
  16587. ('imagemagick
  16588. (org-check-external-command
  16589. "convert" "you need to install imagemagick")
  16590. #'org-create-formula-image-with-imagemagick)
  16591. (t (error
  16592. "invalid value of `org-latex-create-formula-image-program'")))
  16593. string tofile options buffer))
  16594. ;; This function borrows from Ganesh Swami's latex2png.el
  16595. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16596. "This calls dvipng."
  16597. (let* ((tmpdir (if (featurep 'xemacs)
  16598. (temp-directory)
  16599. temporary-file-directory))
  16600. (texfilebase (make-temp-name
  16601. (expand-file-name "orgtex" tmpdir)))
  16602. (texfile (concat texfilebase ".tex"))
  16603. (dvifile (concat texfilebase ".dvi"))
  16604. (pngfile (concat texfilebase ".png"))
  16605. (fnh (if (featurep 'xemacs)
  16606. (font-height (face-font 'default))
  16607. (face-attribute 'default :height nil)))
  16608. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16609. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16610. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16611. "Black"))
  16612. (bg (or (plist-get options (if buffer :background :html-background))
  16613. "Transparent")))
  16614. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16615. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16616. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16617. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16618. (with-temp-file texfile
  16619. (require 'ox-latex)
  16620. (insert (org-latex-guess-inputenc
  16621. (org-splice-latex-header
  16622. org-format-latex-header
  16623. org-latex-default-packages-alist
  16624. org-latex-packages-alist t)))
  16625. (insert "\n\\begin{document}\n" string "\n\\end{document}\n"))
  16626. (let ((dir default-directory))
  16627. (condition-case nil
  16628. (progn
  16629. (cd tmpdir)
  16630. (call-process "latex" nil nil nil texfile))
  16631. (error nil))
  16632. (cd dir))
  16633. (if (not (file-exists-p dvifile))
  16634. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16635. (condition-case nil
  16636. (if (featurep 'xemacs)
  16637. (call-process "dvipng" nil nil nil
  16638. "-fg" fg "-bg" bg
  16639. "-T" "tight"
  16640. "-o" pngfile
  16641. dvifile)
  16642. (call-process "dvipng" nil nil nil
  16643. "-fg" fg "-bg" bg
  16644. "-D" dpi
  16645. ;;"-x" scale "-y" scale
  16646. "-T" "tight"
  16647. "-o" pngfile
  16648. dvifile))
  16649. (error nil))
  16650. (if (not (file-exists-p pngfile))
  16651. (if org-format-latex-signal-error
  16652. (error "Failed to create png file from %s" texfile)
  16653. (message "Failed to create png file from %s" texfile)
  16654. nil)
  16655. ;; Use the requested file name and clean up
  16656. (copy-file pngfile tofile 'replace)
  16657. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16658. (if (file-exists-p (concat texfilebase e))
  16659. (delete-file (concat texfilebase e))))
  16660. pngfile))))
  16661. (defvar org-latex-pdf-process) ; From ox-latex.el
  16662. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16663. "This calls convert, which is included into imagemagick."
  16664. (let* ((tmpdir (if (featurep 'xemacs)
  16665. (temp-directory)
  16666. temporary-file-directory))
  16667. (texfilebase (make-temp-name
  16668. (expand-file-name "orgtex" tmpdir)))
  16669. (texfile (concat texfilebase ".tex"))
  16670. (pdffile (concat texfilebase ".pdf"))
  16671. (pngfile (concat texfilebase ".png"))
  16672. (fnh (if (featurep 'xemacs)
  16673. (font-height (face-font 'default))
  16674. (face-attribute 'default :height nil)))
  16675. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16676. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16677. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16678. "black"))
  16679. (bg (or (plist-get options (if buffer :background :html-background))
  16680. "white")))
  16681. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16682. (setq fg (org-latex-color-format fg)))
  16683. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16684. (setq bg (org-latex-color-format
  16685. (if (string= bg "Transparent") "white" bg))))
  16686. (with-temp-file texfile
  16687. (require 'ox-latex)
  16688. (insert (org-latex-guess-inputenc
  16689. (org-splice-latex-header
  16690. org-format-latex-header
  16691. org-latex-default-packages-alist
  16692. org-latex-packages-alist t)))
  16693. (insert "\n\\begin{document}\n"
  16694. "\\definecolor{fg}{rgb}{" fg "}\n"
  16695. "\\definecolor{bg}{rgb}{" bg "}\n"
  16696. "\n\\pagecolor{bg}\n"
  16697. "\n{\\color{fg}\n"
  16698. string
  16699. "\n}\n"
  16700. "\n\\end{document}\n"))
  16701. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16702. (condition-case nil
  16703. (progn
  16704. (cd tmpdir)
  16705. (setq cmds org-latex-pdf-process)
  16706. (while cmds
  16707. (setq latex-frags-cmds (pop cmds))
  16708. (if (listp latex-frags-cmds)
  16709. (setq cmds nil)
  16710. (setq latex-frags-cmds (list (car org-latex-pdf-process)))))
  16711. (while latex-frags-cmds
  16712. (setq cmd (pop latex-frags-cmds))
  16713. (while (string-match "%b" cmd)
  16714. (setq cmd (replace-match
  16715. (save-match-data
  16716. (shell-quote-argument texfile))
  16717. t t cmd)))
  16718. (while (string-match "%f" cmd)
  16719. (setq cmd (replace-match
  16720. (save-match-data
  16721. (shell-quote-argument
  16722. (file-name-nondirectory texfile)))
  16723. t t cmd)))
  16724. (while (string-match "%o" cmd)
  16725. (setq cmd (replace-match
  16726. (save-match-data
  16727. (shell-quote-argument
  16728. (file-name-directory texfile)))
  16729. t t cmd)))
  16730. (setq cmd (split-string cmd))
  16731. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16732. (error nil))
  16733. (cd dir))
  16734. (if (not (file-exists-p pdffile))
  16735. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16736. (condition-case nil
  16737. (if (featurep 'xemacs)
  16738. (call-process "convert" nil nil nil
  16739. "-density" "96"
  16740. "-trim"
  16741. "-antialias"
  16742. pdffile
  16743. "-quality" "100"
  16744. ;; "-sharpen" "0x1.0"
  16745. pngfile)
  16746. (call-process "convert" nil nil nil
  16747. "-density" dpi
  16748. "-trim"
  16749. "-antialias"
  16750. pdffile
  16751. "-quality" "100"
  16752. ;; "-sharpen" "0x1.0"
  16753. pngfile))
  16754. (error nil))
  16755. (if (not (file-exists-p pngfile))
  16756. (if org-format-latex-signal-error
  16757. (error "Failed to create png file from %s" texfile)
  16758. (message "Failed to create png file from %s" texfile)
  16759. nil)
  16760. ;; Use the requested file name and clean up
  16761. (copy-file pngfile tofile 'replace)
  16762. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16763. (if (file-exists-p (concat texfilebase e))
  16764. (delete-file (concat texfilebase e))))
  16765. pngfile))))
  16766. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16767. "Fill a LaTeX header template TPL.
  16768. In the template, the following place holders will be recognized:
  16769. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16770. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16771. [PACKAGES] \\usepackage statements for PKG
  16772. [NO-PACKAGES] do not include PKG
  16773. [EXTRA] the string EXTRA
  16774. [NO-EXTRA] do not include EXTRA
  16775. For backward compatibility, if both the positive and the negative place
  16776. holder is missing, the positive one (without the \"NO-\") will be
  16777. assumed to be present at the end of the template.
  16778. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16779. EXTRA is a string.
  16780. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16781. (let (rpl (end ""))
  16782. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16783. (setq rpl (if (or (match-end 1) (not def-pkg))
  16784. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16785. tpl (replace-match rpl t t tpl))
  16786. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16787. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16788. (setq rpl (if (or (match-end 1) (not pkg))
  16789. "" (org-latex-packages-to-string pkg snippets-p t))
  16790. tpl (replace-match rpl t t tpl))
  16791. (if pkg (setq end
  16792. (concat end "\n"
  16793. (org-latex-packages-to-string pkg snippets-p)))))
  16794. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16795. (setq rpl (if (or (match-end 1) (not extra))
  16796. "" (concat extra "\n"))
  16797. tpl (replace-match rpl t t tpl))
  16798. (if (and extra (string-match "\\S-" extra))
  16799. (setq end (concat end "\n" extra))))
  16800. (if (string-match "\\S-" end)
  16801. (concat tpl "\n" end)
  16802. tpl)))
  16803. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16804. "Turn an alist of packages into a string with the \\usepackage macros."
  16805. (setq pkg (mapconcat (lambda(p)
  16806. (cond
  16807. ((stringp p) p)
  16808. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16809. (format "%% Package %s omitted" (cadr p)))
  16810. ((equal "" (car p))
  16811. (format "\\usepackage{%s}" (cadr p)))
  16812. (t
  16813. (format "\\usepackage[%s]{%s}"
  16814. (car p) (cadr p)))))
  16815. pkg
  16816. "\n"))
  16817. (if newline (concat pkg "\n") pkg))
  16818. (defun org-dvipng-color (attr)
  16819. "Return a RGB color specification for dvipng."
  16820. (apply 'format "rgb %s %s %s"
  16821. (mapcar 'org-normalize-color
  16822. (if (featurep 'xemacs)
  16823. (color-rgb-components
  16824. (face-property 'default
  16825. (cond ((eq attr :foreground) 'foreground)
  16826. ((eq attr :background) 'background))))
  16827. (color-values (face-attribute 'default attr nil))))))
  16828. (defun org-dvipng-color-format (color-name)
  16829. "Convert COLOR-NAME to a RGB color value for dvipng."
  16830. (apply 'format "rgb %s %s %s"
  16831. (mapcar 'org-normalize-color
  16832. (color-values color-name))))
  16833. (defun org-latex-color (attr)
  16834. "Return a RGB color for the LaTeX color package."
  16835. (apply 'format "%s,%s,%s"
  16836. (mapcar 'org-normalize-color
  16837. (if (featurep 'xemacs)
  16838. (color-rgb-components
  16839. (face-property 'default
  16840. (cond ((eq attr :foreground) 'foreground)
  16841. ((eq attr :background) 'background))))
  16842. (color-values (face-attribute 'default attr nil))))))
  16843. (defun org-latex-color-format (color-name)
  16844. "Convert COLOR-NAME to a RGB color value."
  16845. (apply 'format "%s,%s,%s"
  16846. (mapcar 'org-normalize-color
  16847. (color-values color-name))))
  16848. (defun org-normalize-color (value)
  16849. "Return string to be used as color value for an RGB component."
  16850. (format "%g" (/ value 65535.0)))
  16851. ;; Image display
  16852. (defvar org-inline-image-overlays nil)
  16853. (make-variable-buffer-local 'org-inline-image-overlays)
  16854. (defun org-toggle-inline-images (&optional include-linked)
  16855. "Toggle the display of inline images.
  16856. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16857. (interactive "P")
  16858. (if org-inline-image-overlays
  16859. (progn
  16860. (org-remove-inline-images)
  16861. (message "Inline image display turned off"))
  16862. (org-display-inline-images include-linked)
  16863. (if (and (org-called-interactively-p)
  16864. org-inline-image-overlays)
  16865. (message "%d images displayed inline"
  16866. (length org-inline-image-overlays))
  16867. (message "No images to display inline"))))
  16868. (defun org-redisplay-inline-images ()
  16869. "Refresh the display of inline images."
  16870. (interactive)
  16871. (if (not org-inline-image-overlays)
  16872. (org-toggle-inline-images)
  16873. (org-toggle-inline-images)
  16874. (org-toggle-inline-images)))
  16875. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16876. "Display inline images.
  16877. Normally only links without a description part are inlined, because this
  16878. is how it will work for export. When INCLUDE-LINKED is set, also links
  16879. with a description part will be inlined. This can be nice for a quick
  16880. look at those images, but it does not reflect what exported files will look
  16881. like.
  16882. When REFRESH is set, refresh existing images between BEG and END.
  16883. This will create new image displays only if necessary.
  16884. BEG and END default to the buffer boundaries."
  16885. (interactive "P")
  16886. (unless refresh
  16887. (org-remove-inline-images)
  16888. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16889. (save-excursion
  16890. (save-restriction
  16891. (widen)
  16892. (setq beg (or beg (point-min)) end (or end (point-max)))
  16893. (goto-char beg)
  16894. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16895. (substring (org-image-file-name-regexp) 0 -2)
  16896. "\\)\\]" (if include-linked "" "\\]")))
  16897. old file ov img type attrwidth width)
  16898. (while (re-search-forward re end t)
  16899. (setq old (get-char-property-and-overlay (match-beginning 1)
  16900. 'org-image-overlay)
  16901. file (expand-file-name
  16902. (concat (or (match-string 3) "") (match-string 4))))
  16903. (when (image-type-available-p 'imagemagick)
  16904. (setq attrwidth (if (or (listp org-image-actual-width)
  16905. (null org-image-actual-width))
  16906. (save-excursion
  16907. (save-match-data
  16908. (when (re-search-backward
  16909. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16910. (save-excursion
  16911. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  16912. (string-to-number (match-string 1))))))
  16913. width (cond ((eq org-image-actual-width t) nil)
  16914. ((null org-image-actual-width) attrwidth)
  16915. ((numberp org-image-actual-width)
  16916. org-image-actual-width)
  16917. ((listp org-image-actual-width)
  16918. (or attrwidth (car org-image-actual-width))))
  16919. type (if width 'imagemagick)))
  16920. (when (file-exists-p file)
  16921. (if (and (car-safe old) refresh)
  16922. (image-refresh (overlay-get (cdr old) 'display))
  16923. (setq img (save-match-data (create-image file type nil :width width)))
  16924. (when img
  16925. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16926. (overlay-put ov 'display img)
  16927. (overlay-put ov 'face 'default)
  16928. (overlay-put ov 'org-image-overlay t)
  16929. (overlay-put ov 'modification-hooks
  16930. (list 'org-display-inline-remove-overlay))
  16931. (push ov org-inline-image-overlays)))))))))
  16932. (define-obsolete-function-alias
  16933. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16934. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16935. "Remove inline-display overlay if a corresponding region is modified."
  16936. (let ((inhibit-modification-hooks t))
  16937. (when (and ov after)
  16938. (delete ov org-inline-image-overlays)
  16939. (delete-overlay ov))))
  16940. (defun org-remove-inline-images ()
  16941. "Remove inline display of images."
  16942. (interactive)
  16943. (mapc 'delete-overlay org-inline-image-overlays)
  16944. (setq org-inline-image-overlays nil))
  16945. ;;;; Key bindings
  16946. ;; Outline functions from `outline-mode-prefix-map'
  16947. ;; that can be remapped in Org:
  16948. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16949. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16950. (define-key org-mode-map [remap outline-forward-same-level]
  16951. 'org-forward-heading-same-level)
  16952. (define-key org-mode-map [remap outline-backward-same-level]
  16953. 'org-backward-heading-same-level)
  16954. (define-key org-mode-map [remap show-branches]
  16955. 'org-kill-note-or-show-branches)
  16956. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16957. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16958. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16959. ;; Outline functions from `outline-mode-prefix-map' that can not
  16960. ;; be remapped in Org:
  16961. ;;
  16962. ;; - the column "key binding" shows whether the Outline function is still
  16963. ;; available in Org mode on the same key that it has been bound to in
  16964. ;; Outline mode:
  16965. ;; - "overridden": key used for a different functionality in Org mode
  16966. ;; - else: key still bound to the same Outline function in Org mode
  16967. ;;
  16968. ;; | Outline function | key binding | Org replacement |
  16969. ;; |------------------------------------+-------------+-----------------------|
  16970. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16971. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16972. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16973. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16974. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16975. ;; | `show-entry' | overridden | no replacement |
  16976. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16977. ;; | `show-branches' | `C-c C-k' | still same function |
  16978. ;; | `show-subtree' | overridden | visibility cycling |
  16979. ;; | `show-all' | overridden | no replacement |
  16980. ;; | `hide-subtree' | overridden | visibility cycling |
  16981. ;; | `hide-body' | overridden | no replacement |
  16982. ;; | `hide-entry' | overridden | visibility cycling |
  16983. ;; | `hide-leaves' | overridden | no replacement |
  16984. ;; | `hide-sublevels' | overridden | no replacement |
  16985. ;; | `hide-other' | overridden | no replacement |
  16986. ;; Make `C-c C-x' a prefix key
  16987. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16988. ;; TAB key with modifiers
  16989. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16990. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16991. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16992. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16993. ;; The following line is necessary under Suse GNU/Linux
  16994. (unless (featurep 'xemacs)
  16995. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16996. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16997. (define-key org-mode-map [backtab] 'org-shifttab)
  16998. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16999. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17000. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17001. ;; Cursor keys with modifiers
  17002. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17003. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17004. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17005. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17006. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17007. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17008. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17009. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17010. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17011. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17012. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17013. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17014. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17015. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17016. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17017. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17018. ;; Babel keys
  17019. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17020. (mapc (lambda (pair)
  17021. (define-key org-babel-map (car pair) (cdr pair)))
  17022. org-babel-key-bindings)
  17023. ;;; Extra keys for tty access.
  17024. ;; We only set them when really needed because otherwise the
  17025. ;; menus don't show the simple keys
  17026. (when (or org-use-extra-keys
  17027. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17028. (not window-system))
  17029. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17030. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17031. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17032. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17033. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17034. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17035. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17036. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17037. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17038. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17039. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17040. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17041. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17042. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17043. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17044. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17045. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17046. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17047. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17048. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17049. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17050. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17051. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17052. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17053. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17054. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17055. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17056. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17057. ;; All the other keys
  17058. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17059. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17060. (if (boundp 'narrow-map)
  17061. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17062. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17063. (if (boundp 'narrow-map)
  17064. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17065. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17066. (if (boundp 'narrow-map)
  17067. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17068. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17069. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17070. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17071. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17072. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17073. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17074. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17075. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17076. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17077. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17078. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17079. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17080. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17081. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17082. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17083. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17084. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17085. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17086. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17087. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17088. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17089. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17090. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17091. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17092. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17093. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17094. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17095. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17096. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17097. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17098. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17099. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17100. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17101. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17102. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17103. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17104. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17105. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17106. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17107. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17108. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17109. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17110. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17111. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17112. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17113. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17114. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17115. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17116. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17117. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17118. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17119. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17120. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17121. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17122. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17123. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17124. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17125. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17126. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17127. (org-defkey org-mode-map "\C-m" 'org-return)
  17128. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17129. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17130. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17131. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17132. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17133. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17134. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17135. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17136. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17137. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17138. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17139. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17140. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17141. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17142. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17143. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17144. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17145. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17146. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17147. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17148. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17149. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17150. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17151. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17152. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17153. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17154. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17155. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17156. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17157. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17158. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17159. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17160. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17161. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17162. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17163. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17164. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17165. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17166. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17167. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17168. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17169. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17170. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17171. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17172. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17173. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17174. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17175. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17176. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17177. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17178. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17179. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17180. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17181. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17182. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17183. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17184. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17185. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17186. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17187. (when (featurep 'xemacs)
  17188. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17189. (defconst org-speed-commands-default
  17190. '(
  17191. ("Outline Navigation")
  17192. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17193. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17194. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17195. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17196. ("F" . org-next-block)
  17197. ("B" . org-previous-block)
  17198. ("u" . (org-speed-move-safe 'outline-up-heading))
  17199. ("j" . org-goto)
  17200. ("g" . (org-refile t))
  17201. ("Outline Visibility")
  17202. ("c" . org-cycle)
  17203. ("C" . org-shifttab)
  17204. (" " . org-display-outline-path)
  17205. ("=" . org-columns)
  17206. ("Outline Structure Editing")
  17207. ("U" . org-shiftmetaup)
  17208. ("D" . org-shiftmetadown)
  17209. ("r" . org-metaright)
  17210. ("l" . org-metaleft)
  17211. ("R" . org-shiftmetaright)
  17212. ("L" . org-shiftmetaleft)
  17213. ("i" . (progn (forward-char 1) (call-interactively
  17214. 'org-insert-heading-respect-content)))
  17215. ("^" . org-sort)
  17216. ("w" . org-refile)
  17217. ("a" . org-archive-subtree-default-with-confirmation)
  17218. ("@" . org-mark-subtree)
  17219. ("#" . org-toggle-comment)
  17220. ("Clock Commands")
  17221. ("I" . org-clock-in)
  17222. ("O" . org-clock-out)
  17223. ("Meta Data Editing")
  17224. ("t" . org-todo)
  17225. ("," . (org-priority))
  17226. ("0" . (org-priority ?\ ))
  17227. ("1" . (org-priority ?A))
  17228. ("2" . (org-priority ?B))
  17229. ("3" . (org-priority ?C))
  17230. (":" . org-set-tags-command)
  17231. ("e" . org-set-effort)
  17232. ("E" . org-inc-effort)
  17233. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17234. (org-entry-put (point) "APPT_WARNTIME" m)))
  17235. ("Agenda Views etc")
  17236. ("v" . org-agenda)
  17237. ("/" . org-sparse-tree)
  17238. ("Misc")
  17239. ("o" . org-open-at-point)
  17240. ("?" . org-speed-command-help)
  17241. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17242. (">" . (org-agenda-remove-restriction-lock))
  17243. )
  17244. "The default speed commands.")
  17245. (defun org-print-speed-command (e)
  17246. (if (> (length (car e)) 1)
  17247. (progn
  17248. (princ "\n")
  17249. (princ (car e))
  17250. (princ "\n")
  17251. (princ (make-string (length (car e)) ?-))
  17252. (princ "\n"))
  17253. (princ (car e))
  17254. (princ " ")
  17255. (if (symbolp (cdr e))
  17256. (princ (symbol-name (cdr e)))
  17257. (prin1 (cdr e)))
  17258. (princ "\n")))
  17259. (defun org-speed-command-help ()
  17260. "Show the available speed commands."
  17261. (interactive)
  17262. (if (not org-use-speed-commands)
  17263. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17264. (with-output-to-temp-buffer "*Help*"
  17265. (princ "User-defined Speed commands\n===========================\n")
  17266. (mapc 'org-print-speed-command org-speed-commands-user)
  17267. (princ "\n")
  17268. (princ "Built-in Speed commands\n=======================\n")
  17269. (mapc 'org-print-speed-command org-speed-commands-default))
  17270. (with-current-buffer "*Help*"
  17271. (setq truncate-lines t))))
  17272. (defun org-speed-move-safe (cmd)
  17273. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17274. If not, return to the original position and throw an error."
  17275. (interactive)
  17276. (let ((pos (point)))
  17277. (call-interactively cmd)
  17278. (unless (and (bolp) (org-at-heading-p))
  17279. (goto-char pos)
  17280. (error "Boundary reached while executing %s" cmd))))
  17281. (defvar org-self-insert-command-undo-counter 0)
  17282. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17283. (defvar org-speed-command nil)
  17284. (define-obsolete-function-alias
  17285. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17286. (defun org-speed-command-activate (keys)
  17287. "Hook for activating single-letter speed commands.
  17288. `org-speed-commands-default' specifies a minimal command set.
  17289. Use `org-speed-commands-user' for further customization."
  17290. (when (or (and (bolp) (looking-at org-outline-regexp))
  17291. (and (functionp org-use-speed-commands)
  17292. (funcall org-use-speed-commands)))
  17293. (cdr (assoc keys (append org-speed-commands-user
  17294. org-speed-commands-default)))))
  17295. (define-obsolete-function-alias
  17296. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17297. (defun org-babel-speed-command-activate (keys)
  17298. "Hook for activating single-letter code block commands."
  17299. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17300. (cdr (assoc keys org-babel-key-bindings))))
  17301. (defcustom org-speed-command-hook
  17302. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17303. "Hook for activating speed commands at strategic locations.
  17304. Hook functions are called in sequence until a valid handler is
  17305. found.
  17306. Each hook takes a single argument, a user-pressed command key
  17307. which is also a `self-insert-command' from the global map.
  17308. Within the hook, examine the cursor position and the command key
  17309. and return nil or a valid handler as appropriate. Handler could
  17310. be one of an interactive command, a function, or a form.
  17311. Set `org-use-speed-commands' to non-nil value to enable this
  17312. hook. The default setting is `org-speed-command-activate'."
  17313. :group 'org-structure
  17314. :version "24.1"
  17315. :type 'hook)
  17316. (defun org-self-insert-command (N)
  17317. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17318. If the cursor is in a table looking at whitespace, the whitespace is
  17319. overwritten, and the table is not marked as requiring realignment."
  17320. (interactive "p")
  17321. (org-check-before-invisible-edit 'insert)
  17322. (cond
  17323. ((and org-use-speed-commands
  17324. (setq org-speed-command
  17325. (run-hook-with-args-until-success
  17326. 'org-speed-command-hook (this-command-keys))))
  17327. (cond
  17328. ((commandp org-speed-command)
  17329. (setq this-command org-speed-command)
  17330. (call-interactively org-speed-command))
  17331. ((functionp org-speed-command)
  17332. (funcall org-speed-command))
  17333. ((and org-speed-command (listp org-speed-command))
  17334. (eval org-speed-command))
  17335. (t (let (org-use-speed-commands)
  17336. (call-interactively 'org-self-insert-command)))))
  17337. ((and
  17338. (org-table-p)
  17339. (progn
  17340. ;; check if we blank the field, and if that triggers align
  17341. (and (featurep 'org-table) org-table-auto-blank-field
  17342. (member last-command
  17343. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17344. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17345. ;; got extra space, this field does not determine column width
  17346. (let (org-table-may-need-update) (org-table-blank-field))
  17347. ;; no extra space, this field may determine column width
  17348. (org-table-blank-field)))
  17349. t)
  17350. (eq N 1)
  17351. (looking-at "[^|\n]* |"))
  17352. (let (org-table-may-need-update)
  17353. (goto-char (1- (match-end 0)))
  17354. (backward-delete-char 1)
  17355. (goto-char (match-beginning 0))
  17356. (self-insert-command N)))
  17357. (t
  17358. (setq org-table-may-need-update t)
  17359. (self-insert-command N)
  17360. (org-fix-tags-on-the-fly)
  17361. (if org-self-insert-cluster-for-undo
  17362. (if (not (eq last-command 'org-self-insert-command))
  17363. (setq org-self-insert-command-undo-counter 1)
  17364. (if (>= org-self-insert-command-undo-counter 20)
  17365. (setq org-self-insert-command-undo-counter 1)
  17366. (and (> org-self-insert-command-undo-counter 0)
  17367. buffer-undo-list (listp buffer-undo-list)
  17368. (not (cadr buffer-undo-list)) ; remove nil entry
  17369. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17370. (setq org-self-insert-command-undo-counter
  17371. (1+ org-self-insert-command-undo-counter))))))))
  17372. (defun org-check-before-invisible-edit (kind)
  17373. "Check is editing if kind KIND would be dangerous with invisible text around.
  17374. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17375. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17376. (if (and org-catch-invisible-edits
  17377. (or (not (boundp 'visible-mode)) (not visible-mode))
  17378. (or (get-char-property (point) 'invisible)
  17379. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17380. ;; OK, we need to take a closer look
  17381. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17382. (invisible-before-point (if (bobp) nil (get-char-property
  17383. (1- (point)) 'invisible)))
  17384. (border-and-ok-direction
  17385. (or
  17386. ;; Check if we are acting predictably before invisible text
  17387. (and invisible-at-point (not invisible-before-point)
  17388. (memq kind '(insert delete-backward)))
  17389. ;; Check if we are acting predictably after invisible text
  17390. ;; This works not well, and I have turned it off. It seems
  17391. ;; better to always show and stop after invisible text.
  17392. ;; (and (not invisible-at-point) invisible-before-point
  17393. ;; (memq kind '(insert delete)))
  17394. )))
  17395. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17396. (memq invisible-before-point '(outline org-hide-block t)))
  17397. (if (eq org-catch-invisible-edits 'error)
  17398. (error "Editing in invisible areas is prohibited - make visible first"))
  17399. (if (and org-custom-properties-overlays
  17400. (y-or-n-p "Display invisible properties in this buffer? "))
  17401. (org-toggle-custom-properties-visibility)
  17402. ;; Make the area visible
  17403. (save-excursion
  17404. (if invisible-before-point
  17405. (goto-char (previous-single-char-property-change
  17406. (point) 'invisible)))
  17407. (org-cycle))
  17408. (cond
  17409. ((eq org-catch-invisible-edits 'show)
  17410. ;; That's it, we do the edit after showing
  17411. (message
  17412. "Unfolding invisible region around point before editing")
  17413. (sit-for 1))
  17414. ((and (eq org-catch-invisible-edits 'smart)
  17415. border-and-ok-direction)
  17416. (message "Unfolding invisible region around point before editing"))
  17417. (t
  17418. ;; Don't do the edit, make the user repeat it in full visibility
  17419. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17420. (defun org-fix-tags-on-the-fly ()
  17421. (when (and (equal (char-after (point-at-bol)) ?*)
  17422. (org-at-heading-p))
  17423. (org-align-tags-here org-tags-column)))
  17424. (defun org-delete-backward-char (N)
  17425. "Like `delete-backward-char', insert whitespace at field end in tables.
  17426. When deleting backwards, in tables this function will insert whitespace in
  17427. front of the next \"|\" separator, to keep the table aligned. The table will
  17428. still be marked for re-alignment if the field did fill the entire column,
  17429. because, in this case the deletion might narrow the column."
  17430. (interactive "p")
  17431. (save-match-data
  17432. (org-check-before-invisible-edit 'delete-backward)
  17433. (if (and (org-table-p)
  17434. (eq N 1)
  17435. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17436. (looking-at ".*?|"))
  17437. (let ((pos (point))
  17438. (noalign (looking-at "[^|\n\r]* |"))
  17439. (c org-table-may-need-update))
  17440. (backward-delete-char N)
  17441. (if (not overwrite-mode)
  17442. (progn
  17443. (skip-chars-forward "^|")
  17444. (insert " ")
  17445. (goto-char (1- pos))))
  17446. ;; noalign: if there were two spaces at the end, this field
  17447. ;; does not determine the width of the column.
  17448. (if noalign (setq org-table-may-need-update c)))
  17449. (backward-delete-char N)
  17450. (org-fix-tags-on-the-fly))))
  17451. (defun org-delete-char (N)
  17452. "Like `delete-char', but insert whitespace at field end in tables.
  17453. When deleting characters, in tables this function will insert whitespace in
  17454. front of the next \"|\" separator, to keep the table aligned. The table will
  17455. still be marked for re-alignment if the field did fill the entire column,
  17456. because, in this case the deletion might narrow the column."
  17457. (interactive "p")
  17458. (save-match-data
  17459. (org-check-before-invisible-edit 'delete)
  17460. (if (and (org-table-p)
  17461. (not (bolp))
  17462. (not (= (char-after) ?|))
  17463. (eq N 1))
  17464. (if (looking-at ".*?|")
  17465. (let ((pos (point))
  17466. (noalign (looking-at "[^|\n\r]* |"))
  17467. (c org-table-may-need-update))
  17468. (replace-match (concat
  17469. (substring (match-string 0) 1 -1)
  17470. " |"))
  17471. (goto-char pos)
  17472. ;; noalign: if there were two spaces at the end, this field
  17473. ;; does not determine the width of the column.
  17474. (if noalign (setq org-table-may-need-update c)))
  17475. (delete-char N))
  17476. (delete-char N)
  17477. (org-fix-tags-on-the-fly))))
  17478. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17479. (put 'org-self-insert-command 'delete-selection t)
  17480. (put 'orgtbl-self-insert-command 'delete-selection t)
  17481. (put 'org-delete-char 'delete-selection 'supersede)
  17482. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17483. (put 'org-yank 'delete-selection 'yank)
  17484. ;; Make `flyspell-mode' delay after some commands
  17485. (put 'org-self-insert-command 'flyspell-delayed t)
  17486. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17487. (put 'org-delete-char 'flyspell-delayed t)
  17488. (put 'org-delete-backward-char 'flyspell-delayed t)
  17489. ;; Make pabbrev-mode expand after org-mode commands
  17490. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17491. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17492. ;; How to do this: Measure non-white length of current string
  17493. ;; If equal to column width, we should realign.
  17494. (defun org-remap (map &rest commands)
  17495. "In MAP, remap the functions given in COMMANDS.
  17496. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17497. (let (new old)
  17498. (while commands
  17499. (setq old (pop commands) new (pop commands))
  17500. (if (fboundp 'command-remapping)
  17501. (org-defkey map (vector 'remap old) new)
  17502. (substitute-key-definition old new map global-map)))))
  17503. (defun org-transpose-words ()
  17504. "Transpose words for Org.
  17505. This uses the `org-mode-transpose-word-syntax-table' syntax
  17506. table, which interprets characters in `org-emphasis-alist' as
  17507. word constituants."
  17508. (interactive)
  17509. (with-syntax-table org-mode-transpose-word-syntax-table
  17510. (call-interactively 'transpose-words)))
  17511. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17512. (when (eq org-enable-table-editor 'optimized)
  17513. ;; If the user wants maximum table support, we need to hijack
  17514. ;; some standard editing functions
  17515. (org-remap org-mode-map
  17516. 'self-insert-command 'org-self-insert-command
  17517. 'delete-char 'org-delete-char
  17518. 'delete-backward-char 'org-delete-backward-char)
  17519. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17520. (defvar org-ctrl-c-ctrl-c-hook nil
  17521. "Hook for functions attaching themselves to `C-c C-c'.
  17522. This can be used to add additional functionality to the C-c C-c
  17523. key which executes context-dependent commands. This hook is run
  17524. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17525. run after the last test.
  17526. Each function will be called with no arguments. The function
  17527. must check if the context is appropriate for it to act. If yes,
  17528. it should do its thing and then return a non-nil value. If the
  17529. context is wrong, just do nothing and return nil.")
  17530. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17531. "Hook for functions attaching themselves to `C-c C-c'.
  17532. This can be used to add additional functionality to the C-c C-c
  17533. key which executes context-dependent commands. This hook is run
  17534. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17535. before the first test.
  17536. Each function will be called with no arguments. The function
  17537. must check if the context is appropriate for it to act. If yes,
  17538. it should do its thing and then return a non-nil value. If the
  17539. context is wrong, just do nothing and return nil.")
  17540. (defvar org-tab-first-hook nil
  17541. "Hook for functions to attach themselves to TAB.
  17542. See `org-ctrl-c-ctrl-c-hook' for more information.
  17543. This hook runs as the first action when TAB is pressed, even before
  17544. `org-cycle' messes around with the `outline-regexp' to cater for
  17545. inline tasks and plain list item folding.
  17546. If any function in this hook returns t, any other actions that
  17547. would have been caused by TAB (such as table field motion or visibility
  17548. cycling) will not occur.")
  17549. (defvar org-tab-after-check-for-table-hook nil
  17550. "Hook for functions to attach themselves to TAB.
  17551. See `org-ctrl-c-ctrl-c-hook' for more information.
  17552. This hook runs after it has been established that the cursor is not in a
  17553. table, but before checking if the cursor is in a headline or if global cycling
  17554. should be done.
  17555. If any function in this hook returns t, not other actions like visibility
  17556. cycling will be done.")
  17557. (defvar org-tab-after-check-for-cycling-hook nil
  17558. "Hook for functions to attach themselves to TAB.
  17559. See `org-ctrl-c-ctrl-c-hook' for more information.
  17560. This hook runs after it has been established that not table field motion and
  17561. not visibility should be done because of current context. This is probably
  17562. the place where a package like yasnippets can hook in.")
  17563. (defvar org-tab-before-tab-emulation-hook nil
  17564. "Hook for functions to attach themselves to TAB.
  17565. See `org-ctrl-c-ctrl-c-hook' for more information.
  17566. This hook runs after every other options for TAB have been exhausted, but
  17567. before indentation and \t insertion takes place.")
  17568. (defvar org-metaleft-hook nil
  17569. "Hook for functions attaching themselves to `M-left'.
  17570. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17571. (defvar org-metaright-hook nil
  17572. "Hook for functions attaching themselves to `M-right'.
  17573. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17574. (defvar org-metaup-hook nil
  17575. "Hook for functions attaching themselves to `M-up'.
  17576. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17577. (defvar org-metadown-hook nil
  17578. "Hook for functions attaching themselves to `M-down'.
  17579. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17580. (defvar org-shiftmetaleft-hook nil
  17581. "Hook for functions attaching themselves to `M-S-left'.
  17582. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17583. (defvar org-shiftmetaright-hook nil
  17584. "Hook for functions attaching themselves to `M-S-right'.
  17585. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17586. (defvar org-shiftmetaup-hook nil
  17587. "Hook for functions attaching themselves to `M-S-up'.
  17588. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17589. (defvar org-shiftmetadown-hook nil
  17590. "Hook for functions attaching themselves to `M-S-down'.
  17591. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17592. (defvar org-metareturn-hook nil
  17593. "Hook for functions attaching themselves to `M-RET'.
  17594. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17595. (defvar org-shiftup-hook nil
  17596. "Hook for functions attaching themselves to `S-up'.
  17597. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17598. (defvar org-shiftup-final-hook nil
  17599. "Hook for functions attaching themselves to `S-up'.
  17600. This one runs after all other options except shift-select have been excluded.
  17601. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17602. (defvar org-shiftdown-hook nil
  17603. "Hook for functions attaching themselves to `S-down'.
  17604. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17605. (defvar org-shiftdown-final-hook nil
  17606. "Hook for functions attaching themselves to `S-down'.
  17607. This one runs after all other options except shift-select have been excluded.
  17608. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17609. (defvar org-shiftleft-hook nil
  17610. "Hook for functions attaching themselves to `S-left'.
  17611. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17612. (defvar org-shiftleft-final-hook nil
  17613. "Hook for functions attaching themselves to `S-left'.
  17614. This one runs after all other options except shift-select have been excluded.
  17615. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17616. (defvar org-shiftright-hook nil
  17617. "Hook for functions attaching themselves to `S-right'.
  17618. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17619. (defvar org-shiftright-final-hook nil
  17620. "Hook for functions attaching themselves to `S-right'.
  17621. This one runs after all other options except shift-select have been excluded.
  17622. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17623. (defun org-modifier-cursor-error ()
  17624. "Throw an error, a modified cursor command was applied in wrong context."
  17625. (error "This command is active in special context like tables, headlines or items"))
  17626. (defun org-shiftselect-error ()
  17627. "Throw an error because Shift-Cursor command was applied in wrong context."
  17628. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17629. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17630. (error "This command works only in special context like headlines or timestamps")))
  17631. (defun org-call-for-shift-select (cmd)
  17632. (let ((this-command-keys-shift-translated t))
  17633. (call-interactively cmd)))
  17634. (defun org-shifttab (&optional arg)
  17635. "Global visibility cycling or move to previous table field.
  17636. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17637. on context.
  17638. See the individual commands for more information."
  17639. (interactive "P")
  17640. (cond
  17641. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17642. ((integerp arg)
  17643. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17644. (message "Content view to level: %d" arg)
  17645. (org-content (prefix-numeric-value arg2))
  17646. (setq org-cycle-global-status 'overview)))
  17647. (t (call-interactively 'org-global-cycle))))
  17648. (defun org-shiftmetaleft ()
  17649. "Promote subtree or delete table column.
  17650. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17651. `org-table-delete-column', depending on context. See the
  17652. individual commands for more information."
  17653. (interactive)
  17654. (cond
  17655. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17656. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17657. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17658. ((if (not (org-region-active-p)) (org-at-item-p)
  17659. (save-excursion (goto-char (region-beginning))
  17660. (org-at-item-p)))
  17661. (call-interactively 'org-outdent-item-tree))
  17662. (t (org-modifier-cursor-error))))
  17663. (defun org-shiftmetaright ()
  17664. "Demote subtree or insert table column.
  17665. Calls `org-demote-subtree', `org-indent-item-tree', or
  17666. `org-table-insert-column', depending on context. See the
  17667. individual commands for more information."
  17668. (interactive)
  17669. (cond
  17670. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17671. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17672. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17673. ((if (not (org-region-active-p)) (org-at-item-p)
  17674. (save-excursion (goto-char (region-beginning))
  17675. (org-at-item-p)))
  17676. (call-interactively 'org-indent-item-tree))
  17677. (t (org-modifier-cursor-error))))
  17678. (defun org-shiftmetaup (&optional arg)
  17679. "Move subtree up or kill table row.
  17680. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17681. `org-move-item-up' or `org-timestamp-up', depending on context.
  17682. See the individual commands for more information."
  17683. (interactive "P")
  17684. (cond
  17685. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17686. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17687. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17688. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17689. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17690. (call-interactively 'org-timestamp-up)))
  17691. (t (call-interactively 'org-drag-line-backward))))
  17692. (defun org-shiftmetadown (&optional arg)
  17693. "Move subtree down or insert table row.
  17694. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17695. `org-move-item-down' or `org-timestamp-up', depending on context.
  17696. See the individual commands for more information."
  17697. (interactive "P")
  17698. (cond
  17699. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17700. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17701. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17702. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17703. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17704. (call-interactively 'org-timestamp-down)))
  17705. (t (call-interactively 'org-drag-line-forward))))
  17706. (defsubst org-hidden-tree-error ()
  17707. (error
  17708. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17709. (defun org-metaleft (&optional arg)
  17710. "Promote heading or move table column to left.
  17711. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17712. With no specific context, calls the Emacs default `backward-word'.
  17713. See the individual commands for more information."
  17714. (interactive "P")
  17715. (cond
  17716. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17717. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17718. ((org-with-limited-levels
  17719. (or (org-at-heading-p)
  17720. (and (org-region-active-p)
  17721. (save-excursion
  17722. (goto-char (region-beginning))
  17723. (org-at-heading-p)))))
  17724. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17725. (call-interactively 'org-do-promote))
  17726. ;; At an inline task.
  17727. ((org-at-heading-p)
  17728. (call-interactively 'org-inlinetask-promote))
  17729. ((or (org-at-item-p)
  17730. (and (org-region-active-p)
  17731. (save-excursion
  17732. (goto-char (region-beginning))
  17733. (org-at-item-p))))
  17734. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17735. (call-interactively 'org-outdent-item))
  17736. (t (call-interactively 'backward-word))))
  17737. (defun org-metaright (&optional arg)
  17738. "Demote a subtree, a list item or move table column to right.
  17739. In front of a drawer or a block keyword, indent it correctly.
  17740. With no specific context, calls the Emacs default `forward-word'.
  17741. See the individual commands for more information."
  17742. (interactive "P")
  17743. (cond
  17744. ((run-hook-with-args-until-success 'org-metaright-hook))
  17745. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17746. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17747. ((org-at-block-p) (call-interactively 'org-indent-block))
  17748. ((org-with-limited-levels
  17749. (or (org-at-heading-p)
  17750. (and (org-region-active-p)
  17751. (save-excursion
  17752. (goto-char (region-beginning))
  17753. (org-at-heading-p)))))
  17754. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17755. (call-interactively 'org-do-demote))
  17756. ;; At an inline task.
  17757. ((org-at-heading-p)
  17758. (call-interactively 'org-inlinetask-demote))
  17759. ((or (org-at-item-p)
  17760. (and (org-region-active-p)
  17761. (save-excursion
  17762. (goto-char (region-beginning))
  17763. (org-at-item-p))))
  17764. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17765. (call-interactively 'org-indent-item))
  17766. (t (call-interactively 'forward-word))))
  17767. (defun org-check-for-hidden (what)
  17768. "Check if there are hidden headlines/items in the current visual line.
  17769. WHAT can be either `headlines' or `items'. If the current line is
  17770. an outline or item heading and it has a folded subtree below it,
  17771. this function returns t, nil otherwise."
  17772. (let ((re (cond
  17773. ((eq what 'headlines) org-outline-regexp-bol)
  17774. ((eq what 'items) (org-item-beginning-re))
  17775. (t (error "This should not happen"))))
  17776. beg end)
  17777. (save-excursion
  17778. (catch 'exit
  17779. (unless (org-region-active-p)
  17780. (setq beg (point-at-bol))
  17781. (beginning-of-line 2)
  17782. (while (and (not (eobp)) ;; this is like `next-line'
  17783. (get-char-property (1- (point)) 'invisible))
  17784. (beginning-of-line 2))
  17785. (setq end (point))
  17786. (goto-char beg)
  17787. (goto-char (point-at-eol))
  17788. (setq end (max end (point)))
  17789. (while (re-search-forward re end t)
  17790. (if (get-char-property (match-beginning 0) 'invisible)
  17791. (throw 'exit t))))
  17792. nil))))
  17793. (defun org-metaup (&optional arg)
  17794. "Move subtree up or move table row up.
  17795. Calls `org-move-subtree-up' or `org-table-move-row' or
  17796. `org-move-item-up', depending on context. See the individual commands
  17797. for more information."
  17798. (interactive "P")
  17799. (cond
  17800. ((run-hook-with-args-until-success 'org-metaup-hook))
  17801. ((org-region-active-p)
  17802. (let* ((a (min (region-beginning) (region-end)))
  17803. (b (1- (max (region-beginning) (region-end))))
  17804. (c (save-excursion (goto-char a)
  17805. (move-beginning-of-line 0)))
  17806. (d (save-excursion (goto-char a)
  17807. (move-end-of-line 0) (point))))
  17808. (transpose-regions a b c d)
  17809. (goto-char c)))
  17810. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17811. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17812. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17813. (t (org-drag-element-backward))))
  17814. (defun org-metadown (&optional arg)
  17815. "Move subtree down or move table row down.
  17816. Calls `org-move-subtree-down' or `org-table-move-row' or
  17817. `org-move-item-down', depending on context. See the individual
  17818. commands for more information."
  17819. (interactive "P")
  17820. (cond
  17821. ((run-hook-with-args-until-success 'org-metadown-hook))
  17822. ((org-region-active-p)
  17823. (let* ((a (min (region-beginning) (region-end)))
  17824. (b (max (region-beginning) (region-end)))
  17825. (c (save-excursion (goto-char b)
  17826. (move-beginning-of-line 1)))
  17827. (d (save-excursion (goto-char b)
  17828. (move-end-of-line 1) (1+ (point)))))
  17829. (transpose-regions a b c d)
  17830. (goto-char d)))
  17831. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17832. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17833. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17834. (t (org-drag-element-forward))))
  17835. (defun org-shiftup (&optional arg)
  17836. "Increase item in timestamp or increase priority of current headline.
  17837. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17838. depending on context. See the individual commands for more information."
  17839. (interactive "P")
  17840. (cond
  17841. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17842. ((and org-support-shift-select (org-region-active-p))
  17843. (org-call-for-shift-select 'previous-line))
  17844. ((org-at-timestamp-p t)
  17845. (call-interactively (if org-edit-timestamp-down-means-later
  17846. 'org-timestamp-down 'org-timestamp-up)))
  17847. ((and (not (eq org-support-shift-select 'always))
  17848. org-enable-priority-commands
  17849. (org-at-heading-p))
  17850. (call-interactively 'org-priority-up))
  17851. ((and (not org-support-shift-select) (org-at-item-p))
  17852. (call-interactively 'org-previous-item))
  17853. ((org-clocktable-try-shift 'up arg))
  17854. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17855. (org-support-shift-select
  17856. (org-call-for-shift-select 'previous-line))
  17857. (t (org-shiftselect-error))))
  17858. (defun org-shiftdown (&optional arg)
  17859. "Decrease item in timestamp or decrease priority of current headline.
  17860. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17861. depending on context. See the individual commands for more information."
  17862. (interactive "P")
  17863. (cond
  17864. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17865. ((and org-support-shift-select (org-region-active-p))
  17866. (org-call-for-shift-select 'next-line))
  17867. ((org-at-timestamp-p t)
  17868. (call-interactively (if org-edit-timestamp-down-means-later
  17869. 'org-timestamp-up 'org-timestamp-down)))
  17870. ((and (not (eq org-support-shift-select 'always))
  17871. org-enable-priority-commands
  17872. (org-at-heading-p))
  17873. (call-interactively 'org-priority-down))
  17874. ((and (not org-support-shift-select) (org-at-item-p))
  17875. (call-interactively 'org-next-item))
  17876. ((org-clocktable-try-shift 'down arg))
  17877. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17878. (org-support-shift-select
  17879. (org-call-for-shift-select 'next-line))
  17880. (t (org-shiftselect-error))))
  17881. (defun org-shiftright (&optional arg)
  17882. "Cycle the thing at point or in the current line, depending on context.
  17883. Depending on context, this does one of the following:
  17884. - switch a timestamp at point one day into the future
  17885. - on a headline, switch to the next TODO keyword.
  17886. - on an item, switch entire list to the next bullet type
  17887. - on a property line, switch to the next allowed value
  17888. - on a clocktable definition line, move time block into the future"
  17889. (interactive "P")
  17890. (cond
  17891. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17892. ((and org-support-shift-select (org-region-active-p))
  17893. (org-call-for-shift-select 'forward-char))
  17894. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17895. ((and (not (eq org-support-shift-select 'always))
  17896. (org-at-heading-p))
  17897. (let ((org-inhibit-logging
  17898. (not org-treat-S-cursor-todo-selection-as-state-change))
  17899. (org-inhibit-blocking
  17900. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17901. (org-call-with-arg 'org-todo 'right)))
  17902. ((or (and org-support-shift-select
  17903. (not (eq org-support-shift-select 'always))
  17904. (org-at-item-bullet-p))
  17905. (and (not org-support-shift-select) (org-at-item-p)))
  17906. (org-call-with-arg 'org-cycle-list-bullet nil))
  17907. ((and (not (eq org-support-shift-select 'always))
  17908. (org-at-property-p))
  17909. (call-interactively 'org-property-next-allowed-value))
  17910. ((org-clocktable-try-shift 'right arg))
  17911. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17912. (org-support-shift-select
  17913. (org-call-for-shift-select 'forward-char))
  17914. (t (org-shiftselect-error))))
  17915. (defun org-shiftleft (&optional arg)
  17916. "Cycle the thing at point or in the current line, depending on context.
  17917. Depending on context, this does one of the following:
  17918. - switch a timestamp at point one day into the past
  17919. - on a headline, switch to the previous TODO keyword.
  17920. - on an item, switch entire list to the previous bullet type
  17921. - on a property line, switch to the previous allowed value
  17922. - on a clocktable definition line, move time block into the past"
  17923. (interactive "P")
  17924. (cond
  17925. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17926. ((and org-support-shift-select (org-region-active-p))
  17927. (org-call-for-shift-select 'backward-char))
  17928. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17929. ((and (not (eq org-support-shift-select 'always))
  17930. (org-at-heading-p))
  17931. (let ((org-inhibit-logging
  17932. (not org-treat-S-cursor-todo-selection-as-state-change))
  17933. (org-inhibit-blocking
  17934. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17935. (org-call-with-arg 'org-todo 'left)))
  17936. ((or (and org-support-shift-select
  17937. (not (eq org-support-shift-select 'always))
  17938. (org-at-item-bullet-p))
  17939. (and (not org-support-shift-select) (org-at-item-p)))
  17940. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17941. ((and (not (eq org-support-shift-select 'always))
  17942. (org-at-property-p))
  17943. (call-interactively 'org-property-previous-allowed-value))
  17944. ((org-clocktable-try-shift 'left arg))
  17945. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17946. (org-support-shift-select
  17947. (org-call-for-shift-select 'backward-char))
  17948. (t (org-shiftselect-error))))
  17949. (defun org-shiftcontrolright ()
  17950. "Switch to next TODO set."
  17951. (interactive)
  17952. (cond
  17953. ((and org-support-shift-select (org-region-active-p))
  17954. (org-call-for-shift-select 'forward-word))
  17955. ((and (not (eq org-support-shift-select 'always))
  17956. (org-at-heading-p))
  17957. (org-call-with-arg 'org-todo 'nextset))
  17958. (org-support-shift-select
  17959. (org-call-for-shift-select 'forward-word))
  17960. (t (org-shiftselect-error))))
  17961. (defun org-shiftcontrolleft ()
  17962. "Switch to previous TODO set."
  17963. (interactive)
  17964. (cond
  17965. ((and org-support-shift-select (org-region-active-p))
  17966. (org-call-for-shift-select 'backward-word))
  17967. ((and (not (eq org-support-shift-select 'always))
  17968. (org-at-heading-p))
  17969. (org-call-with-arg 'org-todo 'previousset))
  17970. (org-support-shift-select
  17971. (org-call-for-shift-select 'backward-word))
  17972. (t (org-shiftselect-error))))
  17973. (defun org-shiftcontrolup (&optional n)
  17974. "Change timestamps synchronously up in CLOCK log lines.
  17975. Optional argument N tells to change by that many units."
  17976. (interactive "P")
  17977. (cond ((and (not org-support-shift-select)
  17978. (org-at-clock-log-p)
  17979. (org-at-timestamp-p t))
  17980. (org-clock-timestamps-up n))
  17981. (t (org-shiftselect-error))))
  17982. (defun org-shiftcontroldown (&optional n)
  17983. "Change timestamps synchronously down in CLOCK log lines.
  17984. Optional argument N tells to change by that many units."
  17985. (interactive "P")
  17986. (cond ((and (not org-support-shift-select)
  17987. (org-at-clock-log-p)
  17988. (org-at-timestamp-p t))
  17989. (org-clock-timestamps-down n))
  17990. (t (org-shiftselect-error))))
  17991. (defun org-ctrl-c-ret ()
  17992. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17993. (interactive)
  17994. (cond
  17995. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17996. (t (call-interactively 'org-insert-heading))))
  17997. (defun org-find-visible ()
  17998. (let ((s (point)))
  17999. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18000. (get-char-property s 'invisible)))
  18001. s))
  18002. (defun org-find-invisible ()
  18003. (let ((s (point)))
  18004. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18005. (not (get-char-property s 'invisible))))
  18006. s))
  18007. (defun org-copy-visible (beg end)
  18008. "Copy the visible parts of the region."
  18009. (interactive "r")
  18010. (let (snippets s)
  18011. (save-excursion
  18012. (save-restriction
  18013. (narrow-to-region beg end)
  18014. (setq s (goto-char (point-min)))
  18015. (while (not (= (point) (point-max)))
  18016. (goto-char (org-find-invisible))
  18017. (push (buffer-substring s (point)) snippets)
  18018. (setq s (goto-char (org-find-visible))))))
  18019. (kill-new (apply 'concat (nreverse snippets)))))
  18020. (defun org-copy-special ()
  18021. "Copy region in table or copy current subtree.
  18022. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18023. See the individual commands for more information."
  18024. (interactive)
  18025. (call-interactively
  18026. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18027. (defun org-cut-special ()
  18028. "Cut region in table or cut current subtree.
  18029. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18030. See the individual commands for more information."
  18031. (interactive)
  18032. (call-interactively
  18033. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18034. (defun org-paste-special (arg)
  18035. "Paste rectangular region into table, or past subtree relative to level.
  18036. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18037. See the individual commands for more information."
  18038. (interactive "P")
  18039. (if (org-at-table-p)
  18040. (org-table-paste-rectangle)
  18041. (org-paste-subtree arg)))
  18042. (defsubst org-in-fixed-width-region-p ()
  18043. "Is point in a fixed-width region?"
  18044. (save-match-data
  18045. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18046. (defun org-edit-special (&optional arg)
  18047. "Call a special editor for the element at point.
  18048. When at a table, call the formula editor with `org-table-edit-formulas'.
  18049. When in a source code block, call `org-edit-src-code'.
  18050. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18051. When at an #+INCLUDE keyword, visit the included file.
  18052. On a link, call `ffap' to visit the link at point.
  18053. Otherwise, return a user error."
  18054. (interactive)
  18055. (let ((element (org-element-at-point)))
  18056. (assert (not buffer-read-only) nil
  18057. "Buffer is read-only: %s" (buffer-name))
  18058. (case (org-element-type element)
  18059. (src-block
  18060. (if (not arg) (org-edit-src-code)
  18061. (let* ((info (org-babel-get-src-block-info))
  18062. (lang (nth 0 info))
  18063. (params (nth 2 info))
  18064. (session (cdr (assq :session params))))
  18065. (if (not session) (org-edit-src-code)
  18066. ;; At a src-block with a session and function called with
  18067. ;; an ARG: switch to the buffer related to the inferior
  18068. ;; process.
  18069. (funcall (intern (concat "org-babel-prep-session:" lang))
  18070. session params)))))
  18071. (keyword
  18072. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18073. (find-file
  18074. (org-remove-double-quotes
  18075. (car (org-split-string (org-element-property :value element)))))
  18076. (user-error "No special environment to edit here")))
  18077. (table
  18078. (if (eq (org-element-property :type element) 'table.el)
  18079. (org-edit-src-code)
  18080. (call-interactively 'org-table-edit-formulas)))
  18081. ;; Only Org tables contain `table-row' type elements.
  18082. (table-row (call-interactively 'org-table-edit-formulas))
  18083. ((example-block export-block) (org-edit-src-code))
  18084. (fixed-width (org-edit-fixed-width-region))
  18085. (otherwise
  18086. ;; No notable element at point. Though, we may be at a link,
  18087. ;; which is an object. Thus, scan deeper.
  18088. (if (eq (org-element-type (org-element-context element)) 'link)
  18089. (call-interactively 'ffap)
  18090. (user-error "No special environment to edit here"))))))
  18091. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18092. (defun org-ctrl-c-ctrl-c (&optional arg)
  18093. "Set tags in headline, or update according to changed information at point.
  18094. This command does many different things, depending on context:
  18095. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18096. this is what we do.
  18097. - If the cursor is on a statistics cookie, update it.
  18098. - If the cursor is in a headline, prompt for tags and insert them
  18099. into the current line, aligned to `org-tags-column'. When called
  18100. with prefix arg, realign all tags in the current buffer.
  18101. - If the cursor is in one of the special #+KEYWORD lines, this
  18102. triggers scanning the buffer for these lines and updating the
  18103. information.
  18104. - If the cursor is inside a table, realign the table. This command
  18105. works even if the automatic table editor has been turned off.
  18106. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18107. the entire table.
  18108. - If the cursor is at a footnote reference or definition, jump to
  18109. the corresponding definition or references, respectively.
  18110. - If the cursor is a the beginning of a dynamic block, update it.
  18111. - If the current buffer is a capture buffer, close note and file it.
  18112. - If the cursor is on a <<<target>>>, update radio targets and
  18113. corresponding links in this buffer.
  18114. - If the cursor is on a numbered item in a plain list, renumber the
  18115. ordered list.
  18116. - If the cursor is on a checkbox, toggle it.
  18117. - If the cursor is on a code block, evaluate it. The variable
  18118. `org-confirm-babel-evaluate' can be used to control prompting
  18119. before code block evaluation, by default every code block
  18120. evaluation requires confirmation. Code block evaluation can be
  18121. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18122. (interactive "P")
  18123. (cond
  18124. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18125. org-occur-highlights
  18126. org-latex-fragment-image-overlays)
  18127. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18128. (org-remove-occur-highlights)
  18129. (org-remove-latex-fragment-image-overlays)
  18130. (message "Temporary highlights/overlays removed from current buffer"))
  18131. ((and (local-variable-p 'org-finish-function (current-buffer))
  18132. (fboundp org-finish-function))
  18133. (funcall org-finish-function))
  18134. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18135. (t
  18136. (let* ((context (org-element-context)) (type (org-element-type context)))
  18137. ;; Test if point is within a blank line.
  18138. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18139. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18140. (user-error "C-c C-c can do nothing useful at this location"))
  18141. ;; For convenience: at the first line of a paragraph on the
  18142. ;; same line as an item, apply function on that item instead.
  18143. (when (eq type 'paragraph)
  18144. (let ((parent (org-element-property :parent context)))
  18145. (when (and (eq (org-element-type parent) 'item)
  18146. (= (point-at-bol) (org-element-property :begin parent)))
  18147. (setq context parent type 'item))))
  18148. ;; Act according to type of element or object at point.
  18149. (case type
  18150. (clock (org-clock-update-time-maybe))
  18151. (dynamic-block
  18152. (save-excursion
  18153. (goto-char (org-element-property :post-affiliated context))
  18154. (org-update-dblock)))
  18155. (footnote-definition
  18156. (goto-char (org-element-property :post-affiliated context))
  18157. (call-interactively 'org-footnote-action))
  18158. (footnote-reference (call-interactively 'org-footnote-action))
  18159. ((headline inlinetask)
  18160. (save-excursion (goto-char (org-element-property :begin context))
  18161. (call-interactively 'org-set-tags)))
  18162. (item
  18163. ;; At an item: a double C-u set checkbox to "[-]"
  18164. ;; unconditionally, whereas a single one will toggle its
  18165. ;; presence. Without an universal argument, if the item
  18166. ;; has a checkbox, toggle it. Otherwise repair the list.
  18167. (let* ((box (org-element-property :checkbox context))
  18168. (struct (org-element-property :structure context))
  18169. (old-struct (copy-tree struct))
  18170. (parents (org-list-parents-alist struct))
  18171. (prevs (org-list-prevs-alist struct))
  18172. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18173. (org-list-set-checkbox
  18174. (org-element-property :begin context) struct
  18175. (cond ((equal arg '(16)) "[-]")
  18176. ((and (not box) (equal arg '(4))) "[ ]")
  18177. ((or (not box) (equal arg '(4))) nil)
  18178. ((eq box 'on) "[ ]")
  18179. (t "[X]")))
  18180. ;; Mimic `org-list-write-struct' but with grabbing
  18181. ;; a return value from `org-list-struct-fix-box'.
  18182. (org-list-struct-fix-ind struct parents 2)
  18183. (org-list-struct-fix-item-end struct)
  18184. (org-list-struct-fix-bul struct prevs)
  18185. (org-list-struct-fix-ind struct parents)
  18186. (let ((block-item
  18187. (org-list-struct-fix-box struct parents prevs orderedp)))
  18188. (if (and box (equal struct old-struct))
  18189. (if (equal arg '(16))
  18190. (message "Checkboxes already reset")
  18191. (user-error "Cannot toggle this checkbox: %s"
  18192. (if (eq box 'on)
  18193. "all subitems checked"
  18194. "unchecked subitems")))
  18195. (org-list-struct-apply-struct struct old-struct)
  18196. (org-update-checkbox-count-maybe))
  18197. (when block-item
  18198. (message "Checkboxes were removed due to empty box at line %d"
  18199. (org-current-line block-item))))))
  18200. (keyword
  18201. (let ((org-inhibit-startup-visibility-stuff t)
  18202. (org-startup-align-all-tables nil))
  18203. (when (boundp 'org-table-coordinate-overlays)
  18204. (mapc 'delete-overlay org-table-coordinate-overlays)
  18205. (setq org-table-coordinate-overlays nil))
  18206. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18207. (message "Local setup has been refreshed"))
  18208. (plain-list
  18209. ;; At a plain list, with a double C-u argument, set
  18210. ;; checkboxes of each item to "[-]", whereas a single one
  18211. ;; will toggle their presence according to the state of the
  18212. ;; first item in the list. Without an argument, repair the
  18213. ;; list.
  18214. (let* ((begin (org-element-property :contents-begin context))
  18215. (struct (org-element-property :structure context))
  18216. (old-struct (copy-tree struct))
  18217. (first-box (save-excursion
  18218. (goto-char begin)
  18219. (looking-at org-list-full-item-re)
  18220. (match-string-no-properties 3)))
  18221. (new-box (cond ((equal arg '(16)) "[-]")
  18222. ((equal arg '(4)) (unless first-box "[ ]"))
  18223. ((equal first-box "[X]") "[ ]")
  18224. (t "[X]"))))
  18225. (cond
  18226. (arg
  18227. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18228. (org-list-get-all-items
  18229. begin struct (org-list-prevs-alist struct))))
  18230. ((and first-box (eq (point) begin))
  18231. ;; For convenience, when point is at bol on the first
  18232. ;; item of the list and no argument is provided, simply
  18233. ;; toggle checkbox of that item, if any.
  18234. (org-list-set-checkbox begin struct new-box)))
  18235. (org-list-write-struct
  18236. struct (org-list-parents-alist struct) old-struct)
  18237. (org-update-checkbox-count-maybe)
  18238. (save-excursion (goto-char begin) (org-list-send-list 'maybe))))
  18239. ((property-drawer node-property)
  18240. (call-interactively 'org-property-action))
  18241. ((radio-target target)
  18242. (call-interactively 'org-update-radio-target-regexp))
  18243. (statistics-cookie
  18244. (call-interactively 'org-update-statistics-cookies))
  18245. ((table table-cell table-row)
  18246. ;; At a table, recalculate every field and align it. Also
  18247. ;; send the table if necessary. If the table has
  18248. ;; a `table.el' type, just give up. At a table row or
  18249. ;; cell, maybe recalculate line but always align table.
  18250. (if (eq (org-element-property :type context) 'table.el)
  18251. (message "Use C-c ' to edit table.el tables")
  18252. (let ((org-enable-table-editor t))
  18253. (if (or (eq type 'table)
  18254. ;; Check if point is at a TBLFM line.
  18255. (and (eq type 'table-row)
  18256. (= (point) (org-element-property :end context))))
  18257. (save-excursion
  18258. (goto-char (org-element-property :contents-begin context))
  18259. (org-call-with-arg 'org-table-recalculate (or arg t))
  18260. (orgtbl-send-table 'maybe))
  18261. (org-table-maybe-eval-formula)
  18262. (cond (arg (call-interactively 'org-table-recalculate))
  18263. ((org-table-maybe-recalculate-line))
  18264. (t (org-table-align)))))))
  18265. (timestamp (org-timestamp-change 0 'day))
  18266. (otherwise
  18267. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18268. (user-error
  18269. "C-c C-c can do nothing useful at this location")))))))))
  18270. (defun org-mode-restart ()
  18271. "Restart Org-mode, to scan again for special lines.
  18272. Also updates the keyword regular expressions."
  18273. (interactive)
  18274. (org-mode)
  18275. (message "Org-mode restarted"))
  18276. (defun org-kill-note-or-show-branches ()
  18277. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18278. (interactive)
  18279. (if (not org-finish-function)
  18280. (progn
  18281. (hide-subtree)
  18282. (call-interactively 'show-branches))
  18283. (let ((org-note-abort t))
  18284. (funcall org-finish-function))))
  18285. (defun org-return (&optional indent)
  18286. "Goto next table row or insert a newline.
  18287. Calls `org-table-next-row' or `newline', depending on context.
  18288. See the individual commands for more information."
  18289. (interactive)
  18290. (let (org-ts-what)
  18291. (cond
  18292. ((or (bobp) (org-in-src-block-p))
  18293. (if indent (newline-and-indent) (newline)))
  18294. ((org-at-table-p)
  18295. (org-table-justify-field-maybe)
  18296. (call-interactively 'org-table-next-row))
  18297. ;; when `newline-and-indent' is called within a list, make sure
  18298. ;; text moved stays inside the item.
  18299. ((and (org-in-item-p) indent)
  18300. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18301. (progn
  18302. (save-match-data (newline))
  18303. (org-indent-line-to (length (match-string 0))))
  18304. (let ((ind (org-get-indentation)))
  18305. (newline)
  18306. (if (org-looking-back org-list-end-re)
  18307. (org-indent-line)
  18308. (org-indent-line-to ind)))))
  18309. ((and org-return-follows-link
  18310. (org-at-timestamp-p t)
  18311. (not (eq org-ts-what 'after)))
  18312. (org-follow-timestamp-link))
  18313. ((and org-return-follows-link
  18314. (let ((tprop (get-text-property (point) 'face)))
  18315. (or (eq tprop 'org-link)
  18316. (and (listp tprop) (memq 'org-link tprop)))))
  18317. (call-interactively 'org-open-at-point))
  18318. ((and (org-at-heading-p)
  18319. (looking-at
  18320. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18321. (org-show-entry)
  18322. (end-of-line 1)
  18323. (newline))
  18324. (t (if indent (newline-and-indent) (newline))))))
  18325. (defun org-return-indent ()
  18326. "Goto next table row or insert a newline and indent.
  18327. Calls `org-table-next-row' or `newline-and-indent', depending on
  18328. context. See the individual commands for more information."
  18329. (interactive)
  18330. (org-return t))
  18331. (defun org-ctrl-c-star ()
  18332. "Compute table, or change heading status of lines.
  18333. Calls `org-table-recalculate' or `org-toggle-heading',
  18334. depending on context."
  18335. (interactive)
  18336. (cond
  18337. ((org-at-table-p)
  18338. (call-interactively 'org-table-recalculate))
  18339. (t
  18340. ;; Convert all lines in region to list items
  18341. (call-interactively 'org-toggle-heading))))
  18342. (defun org-ctrl-c-minus ()
  18343. "Insert separator line in table or modify bullet status of line.
  18344. Also turns a plain line or a region of lines into list items.
  18345. Calls `org-table-insert-hline', `org-toggle-item', or
  18346. `org-cycle-list-bullet', depending on context."
  18347. (interactive)
  18348. (cond
  18349. ((org-at-table-p)
  18350. (call-interactively 'org-table-insert-hline))
  18351. ((org-region-active-p)
  18352. (call-interactively 'org-toggle-item))
  18353. ((org-in-item-p)
  18354. (call-interactively 'org-cycle-list-bullet))
  18355. (t
  18356. (call-interactively 'org-toggle-item))))
  18357. (defun org-toggle-item (arg)
  18358. "Convert headings or normal lines to items, items to normal lines.
  18359. If there is no active region, only the current line is considered.
  18360. If the first non blank line in the region is a headline, convert
  18361. all headlines to items, shifting text accordingly.
  18362. If it is an item, convert all items to normal lines.
  18363. If it is normal text, change region into an item. With a prefix
  18364. argument ARG, change each line in region into an item."
  18365. (interactive "P")
  18366. (let ((shift-text
  18367. (function
  18368. ;; Shift text in current section to IND, from point to END.
  18369. ;; The function leaves point to END line.
  18370. (lambda (ind end)
  18371. (let ((min-i 1000) (end (copy-marker end)))
  18372. ;; First determine the minimum indentation (MIN-I) of
  18373. ;; the text.
  18374. (save-excursion
  18375. (catch 'exit
  18376. (while (< (point) end)
  18377. (let ((i (org-get-indentation)))
  18378. (cond
  18379. ;; Skip blank lines and inline tasks.
  18380. ((looking-at "^[ \t]*$"))
  18381. ((looking-at org-outline-regexp-bol))
  18382. ;; We can't find less than 0 indentation.
  18383. ((zerop i) (throw 'exit (setq min-i 0)))
  18384. ((< i min-i) (setq min-i i))))
  18385. (forward-line))))
  18386. ;; Then indent each line so that a line indented to
  18387. ;; MIN-I becomes indented to IND. Ignore blank lines
  18388. ;; and inline tasks in the process.
  18389. (let ((delta (- ind min-i)))
  18390. (while (< (point) end)
  18391. (unless (or (looking-at "^[ \t]*$")
  18392. (looking-at org-outline-regexp-bol))
  18393. (org-indent-line-to (+ (org-get-indentation) delta)))
  18394. (forward-line)))))))
  18395. (skip-blanks
  18396. (function
  18397. ;; Return beginning of first non-blank line, starting from
  18398. ;; line at POS.
  18399. (lambda (pos)
  18400. (save-excursion
  18401. (goto-char pos)
  18402. (skip-chars-forward " \r\t\n")
  18403. (point-at-bol)))))
  18404. beg end)
  18405. ;; Determine boundaries of changes.
  18406. (if (org-region-active-p)
  18407. (setq beg (funcall skip-blanks (region-beginning))
  18408. end (copy-marker (region-end)))
  18409. (setq beg (funcall skip-blanks (point-at-bol))
  18410. end (copy-marker (point-at-eol))))
  18411. ;; Depending on the starting line, choose an action on the text
  18412. ;; between BEG and END.
  18413. (org-with-limited-levels
  18414. (save-excursion
  18415. (goto-char beg)
  18416. (cond
  18417. ;; Case 1. Start at an item: de-itemize. Note that it only
  18418. ;; happens when a region is active: `org-ctrl-c-minus'
  18419. ;; would call `org-cycle-list-bullet' otherwise.
  18420. ((org-at-item-p)
  18421. (while (< (point) end)
  18422. (when (org-at-item-p)
  18423. (skip-chars-forward " \t")
  18424. (delete-region (point) (match-end 0)))
  18425. (forward-line)))
  18426. ;; Case 2. Start at an heading: convert to items.
  18427. ((org-at-heading-p)
  18428. (let* ((bul (org-list-bullet-string "-"))
  18429. (bul-len (length bul))
  18430. ;; Indentation of the first heading. It should be
  18431. ;; relative to the indentation of its parent, if any.
  18432. (start-ind (save-excursion
  18433. (cond
  18434. ((not org-adapt-indentation) 0)
  18435. ((not (outline-previous-heading)) 0)
  18436. (t (length (match-string 0))))))
  18437. ;; Level of first heading. Further headings will be
  18438. ;; compared to it to determine hierarchy in the list.
  18439. (ref-level (org-reduced-level (org-outline-level))))
  18440. (while (< (point) end)
  18441. (let* ((level (org-reduced-level (org-outline-level)))
  18442. (delta (max 0 (- level ref-level))))
  18443. ;; If current headline is less indented than the first
  18444. ;; one, set it as reference, in order to preserve
  18445. ;; subtrees.
  18446. (when (< level ref-level) (setq ref-level level))
  18447. (replace-match bul t t)
  18448. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18449. ;; Ensure all text down to END (or SECTION-END) belongs
  18450. ;; to the newly created item.
  18451. (let ((section-end (save-excursion
  18452. (or (outline-next-heading) (point)))))
  18453. (forward-line)
  18454. (funcall shift-text
  18455. (+ start-ind (* (1+ delta) bul-len))
  18456. (min end section-end)))))))
  18457. ;; Case 3. Normal line with ARG: turn each non-item line into
  18458. ;; an item.
  18459. (arg
  18460. (while (< (point) end)
  18461. (unless (or (org-at-heading-p) (org-at-item-p))
  18462. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18463. (replace-match
  18464. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18465. (forward-line)))
  18466. ;; Case 4. Normal line without ARG: make the first line of
  18467. ;; region an item, and shift indentation of others
  18468. ;; lines to set them as item's body.
  18469. (t (let* ((bul (org-list-bullet-string "-"))
  18470. (bul-len (length bul))
  18471. (ref-ind (org-get-indentation)))
  18472. (skip-chars-forward " \t")
  18473. (insert bul)
  18474. (forward-line)
  18475. (while (< (point) end)
  18476. ;; Ensure that lines less indented than first one
  18477. ;; still get included in item body.
  18478. (funcall shift-text
  18479. (+ ref-ind bul-len)
  18480. (min end (save-excursion (or (outline-next-heading)
  18481. (point)))))
  18482. (forward-line)))))))))
  18483. (defun org-toggle-heading (&optional nstars)
  18484. "Convert headings to normal text, or items or text to headings.
  18485. If there is no active region, only the current line is considered.
  18486. With a \\[universal-argument] prefix, convert the whole list at
  18487. point into heading.
  18488. In a region:
  18489. - If the first non blank line is a headline, remove the stars
  18490. from all headlines in the region.
  18491. - If it is a normal line turn each and every normal line (i.e. not an
  18492. heading or an item) in the region into a heading.
  18493. - If it is a plain list item, turn all plain list items into headings.
  18494. When converting a line into a heading, the number of stars is chosen
  18495. such that the lines become children of the current entry. However,
  18496. when a prefix argument is given, its value determines the number of
  18497. stars to add."
  18498. (interactive "P")
  18499. (let ((skip-blanks
  18500. (function
  18501. ;; Return beginning of first non-blank line, starting from
  18502. ;; line at POS.
  18503. (lambda (pos)
  18504. (save-excursion
  18505. (goto-char pos)
  18506. (while (org-at-comment-p) (forward-line))
  18507. (skip-chars-forward " \r\t\n")
  18508. (point-at-bol)))))
  18509. beg end toggled)
  18510. ;; Determine boundaries of changes. If a universal prefix has
  18511. ;; been given, put the list in a region. If region ends at a bol,
  18512. ;; do not consider the last line to be in the region.
  18513. (when (and current-prefix-arg (org-at-item-p))
  18514. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  18515. (org-mark-element))
  18516. (if (org-region-active-p)
  18517. (setq beg (funcall skip-blanks (region-beginning))
  18518. end (copy-marker (save-excursion
  18519. (goto-char (region-end))
  18520. (if (bolp) (point) (point-at-eol)))))
  18521. (setq beg (funcall skip-blanks (point-at-bol))
  18522. end (copy-marker (point-at-eol))))
  18523. ;; Ensure inline tasks don't count as headings.
  18524. (org-with-limited-levels
  18525. (save-excursion
  18526. (goto-char beg)
  18527. (cond
  18528. ;; Case 1. Started at an heading: de-star headings.
  18529. ((org-at-heading-p)
  18530. (while (< (point) end)
  18531. (when (org-at-heading-p t)
  18532. (looking-at org-outline-regexp) (replace-match "")
  18533. (setq toggled t))
  18534. (forward-line)))
  18535. ;; Case 2. Started at an item: change items into headlines.
  18536. ;; One star will be added by `org-list-to-subtree'.
  18537. ((org-at-item-p)
  18538. (let* ((stars (make-string
  18539. (if nstars
  18540. ;; subtract the star that will be added again by
  18541. ;; `org-list-to-subtree'
  18542. (1- (prefix-numeric-value current-prefix-arg))
  18543. (or (org-current-level) 0))
  18544. ?*))
  18545. (add-stars
  18546. (cond (nstars "") ; stars from prefix only
  18547. ((equal stars "") "") ; before first heading
  18548. (org-odd-levels-only "*") ; inside heading, odd
  18549. (t "")))) ; inside heading, oddeven
  18550. (while (< (point) end)
  18551. (when (org-at-item-p)
  18552. ;; Pay attention to cases when region ends before list.
  18553. (let* ((struct (org-list-struct))
  18554. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18555. (save-restriction
  18556. (narrow-to-region (point) list-end)
  18557. (insert
  18558. (org-list-to-subtree
  18559. (org-list-parse-list t)
  18560. '(:istart (concat stars add-stars (funcall get-stars depth))
  18561. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18562. (setq toggled t))
  18563. (forward-line))))
  18564. ;; Case 3. Started at normal text: make every line an heading,
  18565. ;; skipping headlines and items.
  18566. (t (let* ((stars (make-string
  18567. (if nstars
  18568. (prefix-numeric-value current-prefix-arg)
  18569. (or (org-current-level) 0))
  18570. ?*))
  18571. (add-stars
  18572. (cond (nstars "") ; stars from prefix only
  18573. ((equal stars "") "*") ; before first heading
  18574. (org-odd-levels-only "**") ; inside heading, odd
  18575. (t "*"))) ; inside heading, oddeven
  18576. (rpl (concat stars add-stars " ")))
  18577. (while (< (point) end)
  18578. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18579. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18580. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18581. (forward-line)))))))
  18582. (unless toggled (message "Cannot toggle heading from here"))))
  18583. (defun org-meta-return (&optional arg)
  18584. "Insert a new heading or wrap a region in a table.
  18585. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18586. See the individual commands for more information."
  18587. (interactive "P")
  18588. (cond
  18589. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18590. ((or (org-at-drawer-p) (org-at-property-p))
  18591. (newline-and-indent))
  18592. ((org-at-table-p)
  18593. (call-interactively 'org-table-wrap-region))
  18594. (t (call-interactively 'org-insert-heading))))
  18595. ;;; Menu entries
  18596. (defsubst org-in-subtree-not-table-p ()
  18597. "Are we in a subtree and not in a table?"
  18598. (and (not (org-before-first-heading-p))
  18599. (not (org-at-table-p))))
  18600. ;; Define the Org-mode menus
  18601. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18602. '("Tbl"
  18603. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18604. ["Next Field" org-cycle (org-at-table-p)]
  18605. ["Previous Field" org-shifttab (org-at-table-p)]
  18606. ["Next Row" org-return (org-at-table-p)]
  18607. "--"
  18608. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18609. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18610. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18611. "--"
  18612. ("Column"
  18613. ["Move Column Left" org-metaleft (org-at-table-p)]
  18614. ["Move Column Right" org-metaright (org-at-table-p)]
  18615. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18616. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18617. ("Row"
  18618. ["Move Row Up" org-metaup (org-at-table-p)]
  18619. ["Move Row Down" org-metadown (org-at-table-p)]
  18620. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18621. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18622. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18623. "--"
  18624. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18625. ("Rectangle"
  18626. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18627. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18628. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18629. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18630. "--"
  18631. ("Calculate"
  18632. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18633. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18634. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18635. "--"
  18636. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18637. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18638. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18639. "--"
  18640. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18641. "--"
  18642. ["Sum Column/Rectangle" org-table-sum
  18643. (or (org-at-table-p) (org-region-active-p))]
  18644. ["Which Column?" org-table-current-column (org-at-table-p)])
  18645. ["Debug Formulas"
  18646. org-table-toggle-formula-debugger
  18647. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18648. ["Show Col/Row Numbers"
  18649. org-table-toggle-coordinate-overlays
  18650. :style toggle
  18651. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18652. "--"
  18653. ["Create" org-table-create (and (not (org-at-table-p))
  18654. org-enable-table-editor)]
  18655. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18656. ["Import from File" org-table-import (not (org-at-table-p))]
  18657. ["Export to File" org-table-export (org-at-table-p)]
  18658. "--"
  18659. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18660. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18661. '("Org"
  18662. ("Show/Hide"
  18663. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18664. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18665. ["Sparse Tree..." org-sparse-tree t]
  18666. ["Reveal Context" org-reveal t]
  18667. ["Show All" show-all t]
  18668. "--"
  18669. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18670. "--"
  18671. ["New Heading" org-insert-heading t]
  18672. ("Navigate Headings"
  18673. ["Up" outline-up-heading t]
  18674. ["Next" outline-next-visible-heading t]
  18675. ["Previous" outline-previous-visible-heading t]
  18676. ["Next Same Level" outline-forward-same-level t]
  18677. ["Previous Same Level" outline-backward-same-level t]
  18678. "--"
  18679. ["Jump" org-goto t])
  18680. ("Edit Structure"
  18681. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18682. "--"
  18683. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18684. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18685. "--"
  18686. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18687. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18688. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18689. "--"
  18690. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18691. "--"
  18692. ["Copy visible text" org-copy-visible t]
  18693. "--"
  18694. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18695. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18696. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18697. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18698. "--"
  18699. ["Sort Region/Children" org-sort t]
  18700. "--"
  18701. ["Convert to odd levels" org-convert-to-odd-levels t]
  18702. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18703. ("Editing"
  18704. ["Emphasis..." org-emphasize t]
  18705. ["Edit Source Example" org-edit-special t]
  18706. "--"
  18707. ["Footnote new/jump" org-footnote-action t]
  18708. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18709. ("Archive"
  18710. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18711. "--"
  18712. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18713. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18714. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18715. )
  18716. "--"
  18717. ("Hyperlinks"
  18718. ["Store Link (Global)" org-store-link t]
  18719. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18720. ["Insert Link" org-insert-link t]
  18721. ["Follow Link" org-open-at-point t]
  18722. "--"
  18723. ["Next link" org-next-link t]
  18724. ["Previous link" org-previous-link t]
  18725. "--"
  18726. ["Descriptive Links"
  18727. org-toggle-link-display
  18728. :style radio
  18729. :selected org-descriptive-links
  18730. ]
  18731. ["Literal Links"
  18732. org-toggle-link-display
  18733. :style radio
  18734. :selected (not org-descriptive-links)])
  18735. "--"
  18736. ("TODO Lists"
  18737. ["TODO/DONE/-" org-todo t]
  18738. ("Select keyword"
  18739. ["Next keyword" org-shiftright (org-at-heading-p)]
  18740. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18741. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18742. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18743. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18744. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18745. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18746. "--"
  18747. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18748. :selected org-enforce-todo-dependencies :style toggle :active t]
  18749. "Settings for tree at point"
  18750. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18751. :selected (org-entry-get nil "ORDERED")
  18752. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18753. ["Do Children parallel" org-toggle-ordered-property :style radio
  18754. :selected (not (org-entry-get nil "ORDERED"))
  18755. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18756. "--"
  18757. ["Set Priority" org-priority t]
  18758. ["Priority Up" org-shiftup t]
  18759. ["Priority Down" org-shiftdown t]
  18760. "--"
  18761. ["Get news from all feeds" org-feed-update-all t]
  18762. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18763. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18764. ("TAGS and Properties"
  18765. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18766. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18767. "--"
  18768. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18769. ["Column view of properties" org-columns t]
  18770. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18771. ("Dates and Scheduling"
  18772. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18773. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18774. ("Change Date"
  18775. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18776. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18777. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18778. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18779. ["Compute Time Range" org-evaluate-time-range t]
  18780. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18781. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18782. "--"
  18783. ["Custom time format" org-toggle-time-stamp-overlays
  18784. :style radio :selected org-display-custom-times]
  18785. "--"
  18786. ["Goto Calendar" org-goto-calendar t]
  18787. ["Date from Calendar" org-date-from-calendar t]
  18788. "--"
  18789. ["Start/Restart Timer" org-timer-start t]
  18790. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18791. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18792. ["Insert Timer String" org-timer t]
  18793. ["Insert Timer Item" org-timer-item t])
  18794. ("Logging work"
  18795. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18796. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18797. ["Clock out" org-clock-out t]
  18798. ["Clock cancel" org-clock-cancel t]
  18799. "--"
  18800. ["Mark as default task" org-clock-mark-default-task t]
  18801. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18802. ["Goto running clock" org-clock-goto t]
  18803. "--"
  18804. ["Display times" org-clock-display t]
  18805. ["Create clock table" org-clock-report t]
  18806. "--"
  18807. ["Record DONE time"
  18808. (progn (setq org-log-done (not org-log-done))
  18809. (message "Switching to %s will %s record a timestamp"
  18810. (car org-done-keywords)
  18811. (if org-log-done "automatically" "not")))
  18812. :style toggle :selected org-log-done])
  18813. "--"
  18814. ["Agenda Command..." org-agenda t]
  18815. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18816. ("File List for Agenda")
  18817. ("Special views current file"
  18818. ["TODO Tree" org-show-todo-tree t]
  18819. ["Check Deadlines" org-check-deadlines t]
  18820. ["Timeline" org-timeline t]
  18821. ["Tags/Property tree" org-match-sparse-tree t])
  18822. "--"
  18823. ["Export/Publish..." org-export-dispatch t]
  18824. ("LaTeX"
  18825. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18826. :selected org-cdlatex-mode]
  18827. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18828. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18829. ["Modify math symbol" org-cdlatex-math-modify
  18830. (org-inside-LaTeX-fragment-p)]
  18831. ["Insert citation" org-reftex-citation t]
  18832. "--"
  18833. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  18834. "--"
  18835. ("MobileOrg"
  18836. ["Push Files and Views" org-mobile-push t]
  18837. ["Get Captured and Flagged" org-mobile-pull t]
  18838. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18839. "--"
  18840. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18841. "--"
  18842. ("Documentation"
  18843. ["Show Version" org-version t]
  18844. ["Info Documentation" org-info t])
  18845. ("Customize"
  18846. ["Browse Org Group" org-customize t]
  18847. "--"
  18848. ["Expand This Menu" org-create-customize-menu
  18849. (fboundp 'customize-menu-create)])
  18850. ["Send bug report" org-submit-bug-report t]
  18851. "--"
  18852. ("Refresh/Reload"
  18853. ["Refresh setup current buffer" org-mode-restart t]
  18854. ["Reload Org (after update)" org-reload t]
  18855. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  18856. ))
  18857. (defun org-info (&optional node)
  18858. "Read documentation for Org-mode in the info system.
  18859. With optional NODE, go directly to that node."
  18860. (interactive)
  18861. (info (format "(org)%s" (or node ""))))
  18862. ;;;###autoload
  18863. (defun org-submit-bug-report ()
  18864. "Submit a bug report on Org-mode via mail.
  18865. Don't hesitate to report any problems or inaccurate documentation.
  18866. If you don't have setup sending mail from (X)Emacs, please copy the
  18867. output buffer into your mail program, as it gives us important
  18868. information about your Org-mode version and configuration."
  18869. (interactive)
  18870. (require 'reporter)
  18871. (org-load-modules-maybe)
  18872. (org-require-autoloaded-modules)
  18873. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18874. (reporter-submit-bug-report
  18875. "emacs-orgmode@gnu.org"
  18876. (org-version nil 'full)
  18877. (let (list)
  18878. (save-window-excursion
  18879. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18880. (delete-other-windows)
  18881. (erase-buffer)
  18882. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18883. We would like to add your full Org-mode and Outline configuration to the
  18884. bug report. This greatly simplifies the work of the maintainer and
  18885. other experts on the mailing list.
  18886. HOWEVER, some variables you have customized may contain private
  18887. information. The names of customers, colleagues, or friends, might
  18888. appear in the form of file names, tags, todo states, or search strings.
  18889. If you answer yes to the prompt, you might want to check and remove
  18890. such private information before sending the email.")
  18891. (add-text-properties (point-min) (point-max) '(face org-warning))
  18892. (when (yes-or-no-p "Include your Org-mode configuration ")
  18893. (mapatoms
  18894. (lambda (v)
  18895. (and (boundp v)
  18896. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18897. (or (and (symbol-value v)
  18898. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18899. (and
  18900. (get v 'custom-type) (get v 'standard-value)
  18901. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18902. (push v list)))))
  18903. (kill-buffer (get-buffer "*Warn about privacy*"))
  18904. list))
  18905. nil nil
  18906. "Remember to cover the basics, that is, what you expected to happen and
  18907. what in fact did happen. You don't know how to make a good report? See
  18908. http://orgmode.org/manual/Feedback.html#Feedback
  18909. Your bug report will be posted to the Org-mode mailing list.
  18910. ------------------------------------------------------------------------")
  18911. (save-excursion
  18912. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18913. (replace-match "\\1Bug: \\3 [\\2]")))))
  18914. (defun org-install-agenda-files-menu ()
  18915. (let ((bl (buffer-list)))
  18916. (save-excursion
  18917. (while bl
  18918. (set-buffer (pop bl))
  18919. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18920. (when (derived-mode-p 'org-mode)
  18921. (easy-menu-change
  18922. '("Org") "File List for Agenda"
  18923. (append
  18924. (list
  18925. ["Edit File List" (org-edit-agenda-file-list) t]
  18926. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18927. ["Remove Current File from List" org-remove-file t]
  18928. ["Cycle through agenda files" org-cycle-agenda-files t]
  18929. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18930. "--")
  18931. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18932. ;;;; Documentation
  18933. (defun org-require-autoloaded-modules ()
  18934. (interactive)
  18935. (mapc 'require
  18936. '(org-agenda org-archive org-attach org-clock org-colview org-id
  18937. org-remember org-table org-timer)))
  18938. ;;;###autoload
  18939. (defun org-reload (&optional uncompiled)
  18940. "Reload all org lisp files.
  18941. With prefix arg UNCOMPILED, load the uncompiled versions."
  18942. (interactive "P")
  18943. (require 'loadhist)
  18944. (let* ((org-dir (org-find-library-dir "org"))
  18945. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  18946. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  18947. (remove-re (mapconcat 'identity
  18948. (mapcar (lambda (f) (concat "^" f "$"))
  18949. (list (if (featurep 'xemacs)
  18950. "org-colview"
  18951. "org-colview-xemacs")
  18952. "org" "org-loaddefs" "org-version"))
  18953. "\\|"))
  18954. (feats (delete-dups
  18955. (mapcar 'file-name-sans-extension
  18956. (mapcar 'file-name-nondirectory
  18957. (delq nil
  18958. (mapcar 'feature-file
  18959. features))))))
  18960. (lfeat (append
  18961. (sort
  18962. (setq feats
  18963. (delq nil (mapcar
  18964. (lambda (f)
  18965. (if (and (string-match feature-re f)
  18966. (not (string-match remove-re f)))
  18967. f nil))
  18968. feats)))
  18969. 'string-lessp)
  18970. (list "org-version" "org")))
  18971. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  18972. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  18973. load-uncore load-misses)
  18974. (setq load-misses
  18975. (delq 't
  18976. (mapcar (lambda (f)
  18977. (or (org-load-noerror-mustsuffix (concat org-dir f))
  18978. (and (string= org-dir contrib-dir)
  18979. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  18980. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  18981. (add-to-list 'load-uncore f 'append)
  18982. 't)
  18983. f))
  18984. lfeat)))
  18985. (if load-uncore
  18986. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  18987. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  18988. (if load-misses
  18989. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  18990. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  18991. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  18992. ;;;###autoload
  18993. (defun org-customize ()
  18994. "Call the customize function with org as argument."
  18995. (interactive)
  18996. (org-load-modules-maybe)
  18997. (org-require-autoloaded-modules)
  18998. (customize-browse 'org))
  18999. (defun org-create-customize-menu ()
  19000. "Create a full customization menu for Org-mode, insert it into the menu."
  19001. (interactive)
  19002. (org-load-modules-maybe)
  19003. (org-require-autoloaded-modules)
  19004. (if (fboundp 'customize-menu-create)
  19005. (progn
  19006. (easy-menu-change
  19007. '("Org") "Customize"
  19008. `(["Browse Org group" org-customize t]
  19009. "--"
  19010. ,(customize-menu-create 'org)
  19011. ["Set" Custom-set t]
  19012. ["Save" Custom-save t]
  19013. ["Reset to Current" Custom-reset-current t]
  19014. ["Reset to Saved" Custom-reset-saved t]
  19015. ["Reset to Standard Settings" Custom-reset-standard t]))
  19016. (message "\"Org\"-menu now contains full customization menu"))
  19017. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19018. ;;;; Miscellaneous stuff
  19019. ;;; Generally useful functions
  19020. (defun org-get-at-bol (property)
  19021. "Get text property PROPERTY at beginning of line."
  19022. (get-text-property (point-at-bol) property))
  19023. (defun org-find-text-property-in-string (prop s)
  19024. "Return the first non-nil value of property PROP in string S."
  19025. (or (get-text-property 0 prop s)
  19026. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19027. prop s)))
  19028. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19029. "Display the given MESSAGE as a warning."
  19030. (if (fboundp 'display-warning)
  19031. (display-warning 'org message
  19032. (if (featurep 'xemacs) 'warning :warning))
  19033. (let ((buf (get-buffer-create "*Org warnings*")))
  19034. (with-current-buffer buf
  19035. (goto-char (point-max))
  19036. (insert "Warning (Org): " message)
  19037. (unless (bolp)
  19038. (newline)))
  19039. (display-buffer buf)
  19040. (sit-for 0))))
  19041. (defun org-eval (form)
  19042. "Eval FORM and return result."
  19043. (condition-case error
  19044. (eval form)
  19045. (error (format "%%![Error: %s]" error))))
  19046. (defun org-in-clocktable-p ()
  19047. "Check if the cursor is in a clocktable."
  19048. (let ((pos (point)) start)
  19049. (save-excursion
  19050. (end-of-line 1)
  19051. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19052. (setq start (match-beginning 0))
  19053. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19054. (>= (match-end 0) pos)
  19055. start))))
  19056. (defun org-in-commented-line ()
  19057. "Is point in a line starting with `#'?"
  19058. (equal (char-after (point-at-bol)) ?#))
  19059. (defun org-in-indented-comment-line ()
  19060. "Is point in a line starting with `#' after some white space?"
  19061. (save-excursion
  19062. (save-match-data
  19063. (goto-char (point-at-bol))
  19064. (looking-at "[ \t]*#"))))
  19065. (defun org-in-verbatim-emphasis ()
  19066. (save-match-data
  19067. (and (org-in-regexp org-emph-re 2)
  19068. (>= (point) (match-beginning 3))
  19069. (<= (point) (match-end 4))
  19070. (member (match-string 3) '("=" "~")))))
  19071. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19072. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19073. (if (and marker (marker-buffer marker)
  19074. (buffer-live-p (marker-buffer marker)))
  19075. (progn
  19076. (org-pop-to-buffer-same-window (marker-buffer marker))
  19077. (if (or (> marker (point-max)) (< marker (point-min)))
  19078. (widen))
  19079. (goto-char marker)
  19080. (org-show-context 'org-goto))
  19081. (if bookmark
  19082. (bookmark-jump bookmark)
  19083. (error "Cannot find location"))))
  19084. (defun org-quote-csv-field (s)
  19085. "Quote field for inclusion in CSV material."
  19086. (if (string-match "[\",]" s)
  19087. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19088. s))
  19089. (defun org-force-self-insert (N)
  19090. "Needed to enforce self-insert under remapping."
  19091. (interactive "p")
  19092. (self-insert-command N))
  19093. (defun org-string-width (s)
  19094. "Compute width of string, ignoring invisible characters.
  19095. This ignores character with invisibility property `org-link', and also
  19096. characters with property `org-cwidth', because these will become invisible
  19097. upon the next fontification round."
  19098. (let (b l)
  19099. (when (or (eq t buffer-invisibility-spec)
  19100. (assq 'org-link buffer-invisibility-spec))
  19101. (while (setq b (text-property-any 0 (length s)
  19102. 'invisible 'org-link s))
  19103. (setq s (concat (substring s 0 b)
  19104. (substring s (or (next-single-property-change
  19105. b 'invisible s) (length s)))))))
  19106. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19107. (setq s (concat (substring s 0 b)
  19108. (substring s (or (next-single-property-change
  19109. b 'org-cwidth s) (length s))))))
  19110. (setq l (string-width s) b -1)
  19111. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19112. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19113. l))
  19114. (defun org-shorten-string (s maxlength)
  19115. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19116. If the string is shorter or has length MAXLENGTH, just return the
  19117. original string. If it is longer, the functions finds a space in the
  19118. string, breaks this string off at that locations and adds three dots
  19119. as ellipsis. Including the ellipsis, the string will not be longer
  19120. than MAXLENGTH. If finding a good breaking point in the string does
  19121. not work, the string is just chopped off in the middle of a word
  19122. if necessary."
  19123. (if (<= (length s) maxlength)
  19124. s
  19125. (let* ((n (max (- maxlength 4) 1))
  19126. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19127. (if (string-match re s)
  19128. (concat (match-string 1 s) "...")
  19129. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19130. (defun org-get-indentation (&optional line)
  19131. "Get the indentation of the current line, interpreting tabs.
  19132. When LINE is given, assume it represents a line and compute its indentation."
  19133. (if line
  19134. (if (string-match "^ *" (org-remove-tabs line))
  19135. (match-end 0))
  19136. (save-excursion
  19137. (beginning-of-line 1)
  19138. (skip-chars-forward " \t")
  19139. (current-column))))
  19140. (defun org-get-string-indentation (s)
  19141. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19142. (let ((n -1) (i 0) (w tab-width) c)
  19143. (catch 'exit
  19144. (while (< (setq n (1+ n)) (length s))
  19145. (setq c (aref s n))
  19146. (cond ((= c ?\ ) (setq i (1+ i)))
  19147. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19148. (t (throw 'exit t)))))
  19149. i))
  19150. (defun org-remove-tabs (s &optional width)
  19151. "Replace tabulators in S with spaces.
  19152. Assumes that s is a single line, starting in column 0."
  19153. (setq width (or width tab-width))
  19154. (while (string-match "\t" s)
  19155. (setq s (replace-match
  19156. (make-string
  19157. (- (* width (/ (+ (match-beginning 0) width) width))
  19158. (match-beginning 0)) ?\ )
  19159. t t s)))
  19160. s)
  19161. (defun org-fix-indentation (line ind)
  19162. "Fix indentation in LINE.
  19163. IND is a cons cell with target and minimum indentation.
  19164. If the current indentation in LINE is smaller than the minimum,
  19165. leave it alone. If it is larger than ind, set it to the target."
  19166. (let* ((l (org-remove-tabs line))
  19167. (i (org-get-indentation l))
  19168. (i1 (car ind)) (i2 (cdr ind)))
  19169. (if (>= i i2) (setq l (substring line i2)))
  19170. (if (> i1 0)
  19171. (concat (make-string i1 ?\ ) l)
  19172. l)))
  19173. (defun org-remove-indentation (code &optional n)
  19174. "Remove the maximum common indentation from the lines in CODE.
  19175. N may optionally be the number of spaces to remove."
  19176. (with-temp-buffer
  19177. (insert code)
  19178. (org-do-remove-indentation n)
  19179. (buffer-string)))
  19180. (defun org-do-remove-indentation (&optional n)
  19181. "Remove the maximum common indentation from the buffer."
  19182. (untabify (point-min) (point-max))
  19183. (let ((min 10000) re)
  19184. (if n
  19185. (setq min n)
  19186. (goto-char (point-min))
  19187. (while (re-search-forward "^ *[^ \n]" nil t)
  19188. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19189. (unless (or (= min 0) (= min 10000))
  19190. (setq re (format "^ \\{%d\\}" min))
  19191. (goto-char (point-min))
  19192. (while (re-search-forward re nil t)
  19193. (replace-match "")
  19194. (end-of-line 1))
  19195. min)))
  19196. (defun org-fill-template (template alist)
  19197. "Find each %key of ALIST in TEMPLATE and replace it."
  19198. (let ((case-fold-search nil)
  19199. entry key value)
  19200. (setq alist (sort (copy-sequence alist)
  19201. (lambda (a b) (< (length (car a)) (length (car b))))))
  19202. (while (setq entry (pop alist))
  19203. (setq template
  19204. (replace-regexp-in-string
  19205. (concat "%" (regexp-quote (car entry)))
  19206. (or (cdr entry) "") template t t)))
  19207. template))
  19208. (defun org-base-buffer (buffer)
  19209. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19210. (if (not buffer)
  19211. buffer
  19212. (or (buffer-base-buffer buffer)
  19213. buffer)))
  19214. (defun org-trim (s)
  19215. "Remove whitespace at beginning and end of string."
  19216. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19217. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19218. s)
  19219. (defun org-wrap (string &optional width lines)
  19220. "Wrap string to either a number of lines, or a width in characters.
  19221. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19222. that costs. If there is a word longer than WIDTH, the text is actually
  19223. wrapped to the length of that word.
  19224. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19225. many lines, whatever width that takes.
  19226. The return value is a list of lines, without newlines at the end."
  19227. (let* ((words (org-split-string string "[ \t\n]+"))
  19228. (maxword (apply 'max (mapcar 'org-string-width words)))
  19229. w ll)
  19230. (cond (width
  19231. (org-do-wrap words (max maxword width)))
  19232. (lines
  19233. (setq w maxword)
  19234. (setq ll (org-do-wrap words maxword))
  19235. (if (<= (length ll) lines)
  19236. ll
  19237. (setq ll words)
  19238. (while (> (length ll) lines)
  19239. (setq w (1+ w))
  19240. (setq ll (org-do-wrap words w)))
  19241. ll))
  19242. (t (error "Cannot wrap this")))))
  19243. (defun org-do-wrap (words width)
  19244. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19245. (let (lines line)
  19246. (while words
  19247. (setq line (pop words))
  19248. (while (and words (< (+ (length line) (length (car words))) width))
  19249. (setq line (concat line " " (pop words))))
  19250. (setq lines (push line lines)))
  19251. (nreverse lines)))
  19252. (defun org-split-string (string &optional separators)
  19253. "Splits STRING into substrings at SEPARATORS.
  19254. No empty strings are returned if there are matches at the beginning
  19255. and end of string."
  19256. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19257. (start 0)
  19258. notfirst
  19259. (list nil))
  19260. (while (and (string-match rexp string
  19261. (if (and notfirst
  19262. (= start (match-beginning 0))
  19263. (< start (length string)))
  19264. (1+ start) start))
  19265. (< (match-beginning 0) (length string)))
  19266. (setq notfirst t)
  19267. (or (eq (match-beginning 0) 0)
  19268. (and (eq (match-beginning 0) (match-end 0))
  19269. (eq (match-beginning 0) start))
  19270. (setq list
  19271. (cons (substring string start (match-beginning 0))
  19272. list)))
  19273. (setq start (match-end 0)))
  19274. (or (eq start (length string))
  19275. (setq list
  19276. (cons (substring string start)
  19277. list)))
  19278. (nreverse list)))
  19279. (defun org-quote-vert (s)
  19280. "Replace \"|\" with \"\\vert\"."
  19281. (while (string-match "|" s)
  19282. (setq s (replace-match "\\vert" t t s)))
  19283. s)
  19284. (defun org-uuidgen-p (s)
  19285. "Is S an ID created by UUIDGEN?"
  19286. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19287. (defun org-in-src-block-p (&optional inside)
  19288. "Whether point is in a code source block.
  19289. When INSIDE is non-nil, don't consider we are within a src block
  19290. when point is at #+BEGIN_SRC or #+END_SRC."
  19291. (let ((case-fold-search t) ov)
  19292. (or (and (setq ov (overlays-at (point)))
  19293. (memq 'org-block-background
  19294. (overlay-properties (car ov))))
  19295. (and (not inside)
  19296. (save-match-data
  19297. (save-excursion
  19298. (beginning-of-line)
  19299. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19300. (defun org-context ()
  19301. "Return a list of contexts of the current cursor position.
  19302. If several contexts apply, all are returned.
  19303. Each context entry is a list with a symbol naming the context, and
  19304. two positions indicating start and end of the context. Possible
  19305. contexts are:
  19306. :headline anywhere in a headline
  19307. :headline-stars on the leading stars in a headline
  19308. :todo-keyword on a TODO keyword (including DONE) in a headline
  19309. :tags on the TAGS in a headline
  19310. :priority on the priority cookie in a headline
  19311. :item on the first line of a plain list item
  19312. :item-bullet on the bullet/number of a plain list item
  19313. :checkbox on the checkbox in a plain list item
  19314. :table in an org-mode table
  19315. :table-special on a special filed in a table
  19316. :table-table in a table.el table
  19317. :clocktable in a clocktable
  19318. :src-block in a source block
  19319. :link on a hyperlink
  19320. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19321. :target on a <<target>>
  19322. :radio-target on a <<<radio-target>>>
  19323. :latex-fragment on a LaTeX fragment
  19324. :latex-preview on a LaTeX fragment with overlaid preview image
  19325. This function expects the position to be visible because it uses font-lock
  19326. faces as a help to recognize the following contexts: :table-special, :link,
  19327. and :keyword."
  19328. (let* ((f (get-text-property (point) 'face))
  19329. (faces (if (listp f) f (list f)))
  19330. (case-fold-search t)
  19331. (p (point)) clist o)
  19332. ;; First the large context
  19333. (cond
  19334. ((org-at-heading-p t)
  19335. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19336. (when (progn
  19337. (beginning-of-line 1)
  19338. (looking-at org-todo-line-tags-regexp))
  19339. (push (org-point-in-group p 1 :headline-stars) clist)
  19340. (push (org-point-in-group p 2 :todo-keyword) clist)
  19341. (push (org-point-in-group p 4 :tags) clist))
  19342. (goto-char p)
  19343. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19344. (if (looking-at "\\[#[A-Z0-9]\\]")
  19345. (push (org-point-in-group p 0 :priority) clist)))
  19346. ((org-at-item-p)
  19347. (push (org-point-in-group p 2 :item-bullet) clist)
  19348. (push (list :item (point-at-bol)
  19349. (save-excursion (org-end-of-item) (point)))
  19350. clist)
  19351. (and (org-at-item-checkbox-p)
  19352. (push (org-point-in-group p 0 :checkbox) clist)))
  19353. ((org-at-table-p)
  19354. (push (list :table (org-table-begin) (org-table-end)) clist)
  19355. (if (memq 'org-formula faces)
  19356. (push (list :table-special
  19357. (previous-single-property-change p 'face)
  19358. (next-single-property-change p 'face)) clist)))
  19359. ((org-at-table-p 'any)
  19360. (push (list :table-table) clist)))
  19361. (goto-char p)
  19362. (let ((case-fold-search t))
  19363. ;; New the "medium" contexts: clocktables, source blocks
  19364. (cond ((org-in-clocktable-p)
  19365. (push (list :clocktable
  19366. (and (or (looking-at "#\\+BEGIN: clocktable")
  19367. (search-backward "#+BEGIN: clocktable" nil t))
  19368. (match-beginning 0))
  19369. (and (re-search-forward "#\\+END:?" nil t)
  19370. (match-end 0))) clist))
  19371. ((org-in-src-block-p)
  19372. (push (list :src-block
  19373. (and (or (looking-at "#\\+BEGIN_SRC")
  19374. (search-backward "#+BEGIN_SRC" nil t))
  19375. (match-beginning 0))
  19376. (and (search-forward "#+END_SRC" nil t)
  19377. (match-beginning 0))) clist))))
  19378. (goto-char p)
  19379. ;; Now the small context
  19380. (cond
  19381. ((org-at-timestamp-p)
  19382. (push (org-point-in-group p 0 :timestamp) clist))
  19383. ((memq 'org-link faces)
  19384. (push (list :link
  19385. (previous-single-property-change p 'face)
  19386. (next-single-property-change p 'face)) clist))
  19387. ((memq 'org-special-keyword faces)
  19388. (push (list :keyword
  19389. (previous-single-property-change p 'face)
  19390. (next-single-property-change p 'face)) clist))
  19391. ((org-at-target-p)
  19392. (push (org-point-in-group p 0 :target) clist)
  19393. (goto-char (1- (match-beginning 0)))
  19394. (if (looking-at org-radio-target-regexp)
  19395. (push (org-point-in-group p 0 :radio-target) clist))
  19396. (goto-char p))
  19397. ((setq o (car (delq nil
  19398. (mapcar
  19399. (lambda (x)
  19400. (if (memq x org-latex-fragment-image-overlays) x))
  19401. (overlays-at (point))))))
  19402. (push (list :latex-fragment
  19403. (overlay-start o) (overlay-end o)) clist)
  19404. (push (list :latex-preview
  19405. (overlay-start o) (overlay-end o)) clist))
  19406. ((org-inside-LaTeX-fragment-p)
  19407. ;; FIXME: positions wrong.
  19408. (push (list :latex-fragment (point) (point)) clist)))
  19409. (setq clist (nreverse (delq nil clist)))
  19410. clist))
  19411. ;; FIXME: Compare with at-regexp-p Do we need both?
  19412. (defun org-in-regexp (re &optional nlines visually)
  19413. "Check if point is inside a match of regexp.
  19414. Normally only the current line is checked, but you can include NLINES extra
  19415. lines both before and after point into the search.
  19416. If VISUALLY is set, require that the cursor is not after the match but
  19417. really on, so that the block visually is on the match."
  19418. (catch 'exit
  19419. (let ((pos (point))
  19420. (eol (point-at-eol (+ 1 (or nlines 0))))
  19421. (inc (if visually 1 0)))
  19422. (save-excursion
  19423. (beginning-of-line (- 1 (or nlines 0)))
  19424. (while (re-search-forward re eol t)
  19425. (if (and (<= (match-beginning 0) pos)
  19426. (>= (+ inc (match-end 0)) pos))
  19427. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19428. (defun org-at-regexp-p (regexp)
  19429. "Is point inside a match of REGEXP in the current line?"
  19430. (catch 'exit
  19431. (save-excursion
  19432. (let ((pos (point)) (end (point-at-eol)))
  19433. (beginning-of-line 1)
  19434. (while (re-search-forward regexp end t)
  19435. (if (and (<= (match-beginning 0) pos)
  19436. (>= (match-end 0) pos))
  19437. (throw 'exit t)))
  19438. nil))))
  19439. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19440. "Non-nil when point is between matches of START-RE and END-RE.
  19441. Also return a non-nil value when point is on one of the matches.
  19442. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19443. buffer positions. Default values are the positions of headlines
  19444. surrounding the point.
  19445. The functions returns a cons cell whose car (resp. cdr) is the
  19446. position before START-RE (resp. after END-RE)."
  19447. (save-match-data
  19448. (let ((pos (point))
  19449. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19450. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19451. beg end)
  19452. (save-excursion
  19453. ;; Point is on a block when on START-RE or if START-RE can be
  19454. ;; found before it...
  19455. (and (or (org-at-regexp-p start-re)
  19456. (re-search-backward start-re limit-up t))
  19457. (setq beg (match-beginning 0))
  19458. ;; ... and END-RE after it...
  19459. (goto-char (match-end 0))
  19460. (re-search-forward end-re limit-down t)
  19461. (> (setq end (match-end 0)) pos)
  19462. ;; ... without another START-RE in-between.
  19463. (goto-char (match-beginning 0))
  19464. (not (re-search-backward start-re (1+ beg) t))
  19465. ;; Return value.
  19466. (cons beg end))))))
  19467. (defun org-in-block-p (names)
  19468. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19469. NAMES is a list of strings containing names of blocks.
  19470. Return first block name matched, or nil. Beware that in case of
  19471. nested blocks, the returned name may not belong to the closest
  19472. block from point."
  19473. (save-match-data
  19474. (catch 'exit
  19475. (let ((case-fold-search t)
  19476. (lim-up (save-excursion (outline-previous-heading)))
  19477. (lim-down (save-excursion (outline-next-heading))))
  19478. (mapc (lambda (name)
  19479. (let ((n (regexp-quote name)))
  19480. (when (org-between-regexps-p
  19481. (concat "^[ \t]*#\\+begin_" n)
  19482. (concat "^[ \t]*#\\+end_" n)
  19483. lim-up lim-down)
  19484. (throw 'exit n))))
  19485. names))
  19486. nil)))
  19487. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19488. "Call `multi-occur' with buffers for all agenda files."
  19489. (interactive "sOrg-files matching: \np")
  19490. (let* ((files (org-agenda-files))
  19491. (tnames (mapcar 'file-truename files))
  19492. (extra org-agenda-text-search-extra-files)
  19493. f)
  19494. (when (eq (car extra) 'agenda-archives)
  19495. (setq extra (cdr extra))
  19496. (setq files (org-add-archive-files files)))
  19497. (while (setq f (pop extra))
  19498. (unless (member (file-truename f) tnames)
  19499. (add-to-list 'files f 'append)
  19500. (add-to-list 'tnames (file-truename f) 'append)))
  19501. (multi-occur
  19502. (mapcar (lambda (x)
  19503. (with-current-buffer
  19504. (or (get-file-buffer x) (find-file-noselect x))
  19505. (widen)
  19506. (current-buffer)))
  19507. files)
  19508. regexp)))
  19509. (if (boundp 'occur-mode-find-occurrence-hook)
  19510. ;; Emacs 23
  19511. (add-hook 'occur-mode-find-occurrence-hook
  19512. (lambda ()
  19513. (when (derived-mode-p 'org-mode)
  19514. (org-reveal))))
  19515. ;; Emacs 22
  19516. (defadvice occur-mode-goto-occurrence
  19517. (after org-occur-reveal activate)
  19518. (and (derived-mode-p 'org-mode) (org-reveal)))
  19519. (defadvice occur-mode-goto-occurrence-other-window
  19520. (after org-occur-reveal activate)
  19521. (and (derived-mode-p 'org-mode) (org-reveal)))
  19522. (defadvice occur-mode-display-occurrence
  19523. (after org-occur-reveal activate)
  19524. (when (derived-mode-p 'org-mode)
  19525. (let ((pos (occur-mode-find-occurrence)))
  19526. (with-current-buffer (marker-buffer pos)
  19527. (save-excursion
  19528. (goto-char pos)
  19529. (org-reveal)))))))
  19530. (defun org-occur-link-in-agenda-files ()
  19531. "Create a link and search for it in the agendas.
  19532. The link is not stored in `org-stored-links', it is just created
  19533. for the search purpose."
  19534. (interactive)
  19535. (let ((link (condition-case nil
  19536. (org-store-link nil)
  19537. (error "Unable to create a link to here"))))
  19538. (org-occur-in-agenda-files (regexp-quote link))))
  19539. (defun org-reverse-string (string)
  19540. "Return the reverse of STRING."
  19541. (apply 'string (reverse (string-to-list string))))
  19542. (defun org-uniquify (list)
  19543. "Remove duplicate elements from LIST."
  19544. (let (res)
  19545. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19546. res))
  19547. (defun org-delete-all (elts list)
  19548. "Remove all elements in ELTS from LIST."
  19549. (while elts
  19550. (setq list (delete (pop elts) list)))
  19551. list)
  19552. (defun org-count (cl-item cl-seq)
  19553. "Count the number of occurrences of ITEM in SEQ.
  19554. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19555. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19556. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19557. (while (< cl-start cl-end)
  19558. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19559. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19560. (setq cl-start (1+ cl-start)))
  19561. cl-count))
  19562. (defun org-remove-if (predicate seq)
  19563. "Remove everything from SEQ that fulfills PREDICATE."
  19564. (let (res e)
  19565. (while seq
  19566. (setq e (pop seq))
  19567. (if (not (funcall predicate e)) (push e res)))
  19568. (nreverse res)))
  19569. (defun org-remove-if-not (predicate seq)
  19570. "Remove everything from SEQ that does not fulfill PREDICATE."
  19571. (let (res e)
  19572. (while seq
  19573. (setq e (pop seq))
  19574. (if (funcall predicate e) (push e res)))
  19575. (nreverse res)))
  19576. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19577. "Reduce two-argument FUNCTION across SEQ.
  19578. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19579. \":initial-value\" removed."
  19580. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19581. (cadr (memq :initial-value cl-keys)))
  19582. (cl-seq (pop cl-seq))
  19583. (t (funcall cl-func)))))
  19584. (while cl-seq
  19585. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19586. cl-accum))
  19587. (defun org-back-over-empty-lines ()
  19588. "Move backwards over whitespace, to the beginning of the first empty line.
  19589. Returns the number of empty lines passed."
  19590. (let ((pos (point)))
  19591. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19592. (skip-chars-backward " \t\n\r")
  19593. (unless (eobp)
  19594. (forward-line -1)))
  19595. (beginning-of-line 2)
  19596. (goto-char (min (point) pos))
  19597. (count-lines (point) pos)))
  19598. (defun org-skip-whitespace ()
  19599. (skip-chars-forward " \t\n\r"))
  19600. (defun org-point-in-group (point group &optional context)
  19601. "Check if POINT is in match-group GROUP.
  19602. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19603. match. If the match group does not exist or point is not inside it,
  19604. return nil."
  19605. (and (match-beginning group)
  19606. (>= point (match-beginning group))
  19607. (<= point (match-end group))
  19608. (if context
  19609. (list context (match-beginning group) (match-end group))
  19610. t)))
  19611. (defun org-switch-to-buffer-other-window (&rest args)
  19612. "Switch to buffer in a second window on the current frame.
  19613. In particular, do not allow pop-up frames.
  19614. Returns the newly created buffer."
  19615. (org-no-popups
  19616. (apply 'switch-to-buffer-other-window args)))
  19617. (defun org-combine-plists (&rest plists)
  19618. "Create a single property list from all plists in PLISTS.
  19619. The process starts by copying the first list, and then setting properties
  19620. from the other lists. Settings in the last list are the most significant
  19621. ones and overrule settings in the other lists."
  19622. (let ((rtn (copy-sequence (pop plists)))
  19623. p v ls)
  19624. (while plists
  19625. (setq ls (pop plists))
  19626. (while ls
  19627. (setq p (pop ls) v (pop ls))
  19628. (setq rtn (plist-put rtn p v))))
  19629. rtn))
  19630. (defun org-replace-escapes (string table)
  19631. "Replace %-escapes in STRING with values in TABLE.
  19632. TABLE is an association list with keys like \"%a\" and string values.
  19633. The sequences in STRING may contain normal field width and padding information,
  19634. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19635. so values can contain further %-escapes if they are define later in TABLE."
  19636. (let ((tbl (copy-alist table))
  19637. (case-fold-search nil)
  19638. (pchg 0)
  19639. e re rpl)
  19640. (while (setq e (pop tbl))
  19641. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19642. (when (and (cdr e) (string-match re (cdr e)))
  19643. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19644. (safe "SREF"))
  19645. (add-text-properties 0 3 (list 'sref sref) safe)
  19646. (setcdr e (replace-match safe t t (cdr e)))))
  19647. (while (string-match re string)
  19648. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19649. (cdr e)))
  19650. (setq string (replace-match rpl t t string))))
  19651. (while (setq pchg (next-property-change pchg string))
  19652. (let ((sref (get-text-property pchg 'sref string)))
  19653. (when (and sref (string-match "SREF" string pchg))
  19654. (setq string (replace-match sref t t string)))))
  19655. string))
  19656. (defun org-sublist (list start end)
  19657. "Return a section of LIST, from START to END.
  19658. Counting starts at 1."
  19659. (let (rtn (c start))
  19660. (setq list (nthcdr (1- start) list))
  19661. (while (and list (<= c end))
  19662. (push (pop list) rtn)
  19663. (setq c (1+ c)))
  19664. (nreverse rtn)))
  19665. (defun org-find-base-buffer-visiting (file)
  19666. "Like `find-buffer-visiting' but always return the base buffer and
  19667. not an indirect buffer."
  19668. (let ((buf (or (get-file-buffer file)
  19669. (find-buffer-visiting file))))
  19670. (if buf
  19671. (or (buffer-base-buffer buf) buf)
  19672. nil)))
  19673. (defun org-image-file-name-regexp (&optional extensions)
  19674. "Return regexp matching the file names of images.
  19675. If EXTENSIONS is given, only match these."
  19676. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19677. (image-file-name-regexp)
  19678. (let ((image-file-name-extensions
  19679. (or extensions
  19680. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19681. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19682. (concat "\\."
  19683. (regexp-opt (nconc (mapcar 'upcase
  19684. image-file-name-extensions)
  19685. image-file-name-extensions)
  19686. t)
  19687. "\\'"))))
  19688. (defun org-file-image-p (file &optional extensions)
  19689. "Return non-nil if FILE is an image."
  19690. (save-match-data
  19691. (string-match (org-image-file-name-regexp extensions) file)))
  19692. (defun org-get-cursor-date (&optional with-time)
  19693. "Return the date at cursor in as a time.
  19694. This works in the calendar and in the agenda, anywhere else it just
  19695. returns the current time.
  19696. If WITH-TIME is non-nil, returns the time of the event at point (in
  19697. the agenda) or the current time of the day."
  19698. (let (date day defd tp tm hod mod)
  19699. (when with-time
  19700. (setq tp (get-text-property (point) 'time))
  19701. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19702. (setq hod (string-to-number (match-string 1 tp))
  19703. mod (string-to-number (match-string 2 tp))))
  19704. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19705. mod (nth 1 (decode-time (current-time))))))
  19706. (cond
  19707. ((eq major-mode 'calendar-mode)
  19708. (setq date (calendar-cursor-to-date)
  19709. defd (encode-time 0 (or mod 0) (or hod 0)
  19710. (nth 1 date) (nth 0 date) (nth 2 date))))
  19711. ((eq major-mode 'org-agenda-mode)
  19712. (setq day (get-text-property (point) 'day))
  19713. (if day
  19714. (setq date (calendar-gregorian-from-absolute day)
  19715. defd (encode-time 0 (or mod 0) (or hod 0)
  19716. (nth 1 date) (nth 0 date) (nth 2 date))))))
  19717. (or defd (current-time))))
  19718. (defun org-mark-subtree (&optional up)
  19719. "Mark the current subtree.
  19720. This puts point at the start of the current subtree, and mark at
  19721. the end. If a numeric prefix UP is given, move up into the
  19722. hierarchy of headlines by UP levels before marking the subtree."
  19723. (interactive "P")
  19724. (org-with-limited-levels
  19725. (cond ((org-at-heading-p) (beginning-of-line))
  19726. ((org-before-first-heading-p) (error "Not in a subtree"))
  19727. (t (outline-previous-visible-heading 1))))
  19728. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19729. (if (org-called-interactively-p 'any)
  19730. (call-interactively 'org-mark-element)
  19731. (org-mark-element)))
  19732. ;;; Indentation
  19733. (defun org-indent-line ()
  19734. "Indent line depending on context."
  19735. (interactive)
  19736. (let* ((pos (point))
  19737. (itemp (org-at-item-p))
  19738. (case-fold-search t)
  19739. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19740. (inline-task-p (and (featurep 'org-inlinetask)
  19741. (org-inlinetask-in-task-p)))
  19742. (inline-re (and inline-task-p
  19743. (org-inlinetask-outline-regexp)))
  19744. column)
  19745. (if (and orgstruct-is-++ (eq pos (point)))
  19746. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19747. (indent-according-to-mode))
  19748. (beginning-of-line 1)
  19749. (cond
  19750. ;; Headings
  19751. ((looking-at org-outline-regexp) (setq column 0))
  19752. ;; Footnote definition
  19753. ((looking-at org-footnote-definition-re) (setq column 0))
  19754. ;; Literal examples
  19755. ((looking-at "[ \t]*:\\( \\|$\\)")
  19756. (setq column (org-get-indentation))) ; do nothing
  19757. ;; Lists
  19758. ((ignore-errors (goto-char (org-in-item-p)))
  19759. (setq column (if itemp
  19760. (org-get-indentation)
  19761. (org-list-item-body-column (point))))
  19762. (goto-char pos))
  19763. ;; Drawers
  19764. ((and (looking-at "[ \t]*:END:")
  19765. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19766. (save-excursion
  19767. (goto-char (1- (match-beginning 1)))
  19768. (setq column (current-column))))
  19769. ;; Special blocks
  19770. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19771. (save-excursion
  19772. (re-search-backward
  19773. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19774. (setq column (org-get-indentation (match-string 0))))
  19775. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19776. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19777. (save-excursion
  19778. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19779. (setq column
  19780. (cond ((equal (downcase (match-string 1)) "src")
  19781. ;; src blocks: let `org-edit-src-exit' handle them
  19782. (org-get-indentation))
  19783. ((equal (downcase (match-string 1)) "example")
  19784. (max (org-get-indentation)
  19785. (org-get-indentation (match-string 0))))
  19786. (t
  19787. (org-get-indentation (match-string 0))))))
  19788. ;; This line has nothing special, look at the previous relevant
  19789. ;; line to compute indentation
  19790. (t
  19791. (beginning-of-line 0)
  19792. (while (and (not (bobp))
  19793. (not (looking-at org-table-line-regexp))
  19794. (not (looking-at org-drawer-regexp))
  19795. ;; When point started in an inline task, do not move
  19796. ;; above task starting line.
  19797. (not (and inline-task-p (looking-at inline-re)))
  19798. ;; Skip drawers, blocks, empty lines, verbatim,
  19799. ;; comments, tables, footnotes definitions, lists,
  19800. ;; inline tasks.
  19801. (or (and (looking-at "[ \t]*:END:")
  19802. (re-search-backward org-drawer-regexp nil t))
  19803. (and (looking-at "[ \t]*#\\+end_")
  19804. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19805. (looking-at "[ \t]*[\n:#|]")
  19806. (looking-at org-footnote-definition-re)
  19807. (and (ignore-errors (goto-char (org-in-item-p)))
  19808. (goto-char
  19809. (org-list-get-top-point (org-list-struct))))
  19810. (and (not inline-task-p)
  19811. (featurep 'org-inlinetask)
  19812. (org-inlinetask-in-task-p)
  19813. (or (org-inlinetask-goto-beginning) t))))
  19814. (beginning-of-line 0))
  19815. (cond
  19816. ;; There was an heading above.
  19817. ((looking-at "\\*+[ \t]+")
  19818. (if (not org-adapt-indentation)
  19819. (setq column 0)
  19820. (goto-char (match-end 0))
  19821. (setq column (current-column))))
  19822. ;; A drawer had started and is unfinished
  19823. ((looking-at org-drawer-regexp)
  19824. (goto-char (1- (match-beginning 1)))
  19825. (setq column (current-column)))
  19826. ;; Else, nothing noticeable found: get indentation and go on.
  19827. (t (setq column (org-get-indentation))))))
  19828. ;; Now apply indentation and move cursor accordingly
  19829. (goto-char pos)
  19830. (if (<= (current-column) (current-indentation))
  19831. (org-indent-line-to column)
  19832. (save-excursion (org-indent-line-to column)))
  19833. ;; Special polishing for properties, see `org-property-format'
  19834. (setq column (current-column))
  19835. (beginning-of-line 1)
  19836. (if (looking-at
  19837. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19838. (replace-match (concat (match-string 1)
  19839. (format org-property-format
  19840. (match-string 2) (match-string 3)))
  19841. t t))
  19842. (org-move-to-column column))))
  19843. (defun org-indent-drawer ()
  19844. "Indent the drawer at point."
  19845. (interactive)
  19846. (let ((p (point))
  19847. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19848. (match-end 0)))
  19849. (folded
  19850. (save-excursion
  19851. (end-of-line)
  19852. (when (overlays-at (point))
  19853. (member 'invisible (overlay-properties
  19854. (car (overlays-at (point)))))))))
  19855. (when folded (org-cycle))
  19856. (indent-for-tab-command)
  19857. (while (and (move-beginning-of-line 2) (< (point) e))
  19858. (indent-for-tab-command))
  19859. (goto-char p)
  19860. (when folded (org-cycle)))
  19861. (message "Drawer at point indented"))
  19862. (defun org-indent-block ()
  19863. "Indent the block at point."
  19864. (interactive)
  19865. (let ((p (point))
  19866. (case-fold-search t)
  19867. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19868. (match-end 0)))
  19869. (folded
  19870. (save-excursion
  19871. (end-of-line)
  19872. (when (overlays-at (point))
  19873. (member 'invisible (overlay-properties
  19874. (car (overlays-at (point)))))))))
  19875. (when folded (org-cycle))
  19876. (indent-for-tab-command)
  19877. (while (and (move-beginning-of-line 2) (< (point) e))
  19878. (indent-for-tab-command))
  19879. (goto-char p)
  19880. (when folded (org-cycle)))
  19881. (message "Block at point indented"))
  19882. (defun org-indent-region (start end)
  19883. "Indent region."
  19884. (interactive "r")
  19885. (save-excursion
  19886. (let ((line-end (org-current-line end)))
  19887. (goto-char start)
  19888. (while (< (org-current-line) line-end)
  19889. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19890. (t (call-interactively 'org-indent-line)))
  19891. (move-beginning-of-line 2)))))
  19892. ;;; Filling
  19893. ;; We use our own fill-paragraph and auto-fill functions.
  19894. ;; `org-fill-paragraph' relies on adaptive filling and context
  19895. ;; checking. Appropriate `fill-prefix' is computed with
  19896. ;; `org-adaptive-fill-function'.
  19897. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19898. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19899. ;; `org-adaptive-fill-function' value. Internally,
  19900. ;; `org-comment-line-break-function' breaks the line.
  19901. ;; `org-setup-filling' installs filling and auto-filling related
  19902. ;; variables during `org-mode' initialization.
  19903. (defun org-setup-filling ()
  19904. (interactive)
  19905. ;; Prevent auto-fill from inserting unwanted new items.
  19906. (when (boundp 'fill-nobreak-predicate)
  19907. (org-set-local
  19908. 'fill-nobreak-predicate
  19909. (org-uniquify
  19910. (append fill-nobreak-predicate
  19911. '(org-fill-paragraph-separate-nobreak-p
  19912. org-fill-line-break-nobreak-p
  19913. org-fill-paragraph-with-timestamp-nobreak-p)))))
  19914. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19915. (org-set-local 'auto-fill-inhibit-regexp nil)
  19916. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19917. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19918. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19919. (defvar org-element-paragraph-separate) ; org-element.el
  19920. (defun org-fill-paragraph-separate-nobreak-p ()
  19921. "Non-nil when a line break at point would insert a new item."
  19922. (looking-at (substring org-element-paragraph-separate 1)))
  19923. (defun org-fill-line-break-nobreak-p ()
  19924. "Non-nil when a line break at point would create an Org line break."
  19925. (save-excursion
  19926. (skip-chars-backward "[ \t]")
  19927. (skip-chars-backward "\\\\")
  19928. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19929. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  19930. "Non-nil when a line break at point would insert a new item."
  19931. (and (org-at-timestamp-p t)
  19932. (not (looking-at org-ts-regexp-both))))
  19933. (declare-function message-in-body-p "message" ())
  19934. (defvar orgtbl-line-start-regexp) ; From org-table.el
  19935. (defun org-adaptive-fill-function ()
  19936. "Compute a fill prefix for the current line.
  19937. Return fill prefix, as a string, or nil if current line isn't
  19938. meant to be filled."
  19939. (let (prefix)
  19940. (catch 'exit
  19941. (when (derived-mode-p 'message-mode)
  19942. (save-excursion
  19943. (beginning-of-line)
  19944. (cond ((or (not (message-in-body-p))
  19945. (looking-at orgtbl-line-start-regexp))
  19946. (throw 'exit nil))
  19947. ((looking-at message-cite-prefix-regexp)
  19948. (throw 'exit (match-string-no-properties 0)))
  19949. ((looking-at org-outline-regexp)
  19950. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  19951. (org-with-wide-buffer
  19952. (let* ((p (line-beginning-position))
  19953. (element (save-excursion (beginning-of-line)
  19954. (org-element-at-point)))
  19955. (type (org-element-type element))
  19956. (post-affiliated (org-element-property :post-affiliated element)))
  19957. (unless (and post-affiliated (< p post-affiliated))
  19958. (case type
  19959. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19960. (footnote-definition "")
  19961. ((item plain-list)
  19962. (make-string (org-list-item-body-column
  19963. (or post-affiliated
  19964. (org-element-property :begin element)))
  19965. ? ))
  19966. (paragraph
  19967. ;; Fill prefix is usually the same as the current line,
  19968. ;; except if the paragraph is at the beginning of an item.
  19969. (let ((parent (org-element-property :parent element)))
  19970. (cond ((eq (org-element-type parent) 'item)
  19971. (make-string (org-list-item-body-column
  19972. (org-element-property :begin parent))
  19973. ? ))
  19974. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19975. (match-string 0))
  19976. (t ""))))
  19977. (comment-block
  19978. ;; Only fill contents if P is within block boundaries.
  19979. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19980. (forward-line)
  19981. (point)))
  19982. (cend (save-excursion
  19983. (goto-char (org-element-property :end element))
  19984. (skip-chars-backward " \r\t\n")
  19985. (line-beginning-position))))
  19986. (when (and (>= p cbeg) (< p cend))
  19987. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19988. (match-string 0)
  19989. "")))))))))))
  19990. (declare-function message-goto-body "message" ())
  19991. (defvar message-cite-prefix-regexp) ; From message.el
  19992. (defvar org-element-all-objects) ; From org-element.el
  19993. (defun org-fill-paragraph (&optional justify)
  19994. "Fill element at point, when applicable.
  19995. This function only applies to comment blocks, comments, example
  19996. blocks and paragraphs. Also, as a special case, re-align table
  19997. when point is at one.
  19998. If JUSTIFY is non-nil (interactively, with prefix argument),
  19999. justify as well. If `sentence-end-double-space' is non-nil, then
  20000. period followed by one space does not end a sentence, so don't
  20001. break a line there. The variable `fill-column' controls the
  20002. width for filling.
  20003. For convenience, when point is at a plain list, an item or
  20004. a footnote definition, try to fill the first paragraph within."
  20005. (interactive)
  20006. (if (and (derived-mode-p 'message-mode)
  20007. (or (not (message-in-body-p))
  20008. (save-excursion (move-beginning-of-line 1)
  20009. (looking-at message-cite-prefix-regexp))))
  20010. ;; First ensure filling is correct in message-mode.
  20011. (let ((fill-paragraph-function
  20012. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20013. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20014. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20015. (paragraph-separate
  20016. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20017. (fill-paragraph nil))
  20018. (with-syntax-table org-mode-transpose-word-syntax-table
  20019. ;; Move to end of line in order to get the first paragraph
  20020. ;; within a plain list or a footnote definition.
  20021. (let ((element (save-excursion (end-of-line) (org-element-at-point))))
  20022. ;; First check if point is in a blank line at the beginning of
  20023. ;; the buffer. In that case, ignore filling.
  20024. (case (org-element-type element)
  20025. ;; Use major mode filling function is src blocks.
  20026. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20027. ;; Align Org tables, leave table.el tables as-is.
  20028. (table-row (org-table-align) t)
  20029. (table
  20030. (when (eq (org-element-property :type element) 'org)
  20031. (org-table-align))
  20032. t)
  20033. (paragraph
  20034. ;; Paragraphs may contain `line-break' type objects.
  20035. (let ((beg (max (point-min)
  20036. (org-element-property :contents-begin element)))
  20037. (end (min (point-max)
  20038. (org-element-property :contents-end element))))
  20039. ;; Do nothing if point is at an affiliated keyword.
  20040. (if (< (line-end-position) beg) t
  20041. (when (derived-mode-p 'message-mode)
  20042. ;; In `message-mode', do not fill following citation
  20043. ;; in current paragraph nor text before message body.
  20044. (let ((body-start (save-excursion (message-goto-body))))
  20045. (when body-start (setq beg (max body-start beg))))
  20046. (when (save-excursion
  20047. (re-search-forward
  20048. (concat "^" message-cite-prefix-regexp) end t))
  20049. (setq end (match-beginning 0))))
  20050. ;; Fill paragraph, taking line breaks into account.
  20051. ;; For that, slice the paragraph using line breaks as
  20052. ;; separators, and fill the parts in reverse order to
  20053. ;; avoid messing with markers.
  20054. (save-excursion
  20055. (goto-char end)
  20056. (mapc
  20057. (lambda (pos)
  20058. (fill-region-as-paragraph pos (point) justify)
  20059. (goto-char pos))
  20060. ;; Find the list of ending positions for line breaks
  20061. ;; in the current paragraph. Add paragraph
  20062. ;; beginning to include first slice.
  20063. (nreverse
  20064. (cons beg
  20065. (org-element-map
  20066. (org-element--parse-objects
  20067. beg end nil org-element-all-objects)
  20068. 'line-break
  20069. (lambda (lb) (org-element-property :end lb)))))))
  20070. t)))
  20071. ;; Contents of `comment-block' type elements should be
  20072. ;; filled as plain text, but only if point is within block
  20073. ;; markers.
  20074. (comment-block
  20075. (let* ((case-fold-search t)
  20076. (beg (save-excursion
  20077. (goto-char (org-element-property :begin element))
  20078. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20079. (forward-line)
  20080. (point)))
  20081. (end (save-excursion
  20082. (goto-char (org-element-property :end element))
  20083. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20084. (line-beginning-position))))
  20085. (when (and (>= (point) beg) (< (point) end))
  20086. (fill-region-as-paragraph
  20087. (save-excursion
  20088. (end-of-line)
  20089. (re-search-backward "^[ \t]*$" beg 'move)
  20090. (line-beginning-position))
  20091. (save-excursion
  20092. (beginning-of-line)
  20093. (re-search-forward "^[ \t]*$" end 'move)
  20094. (line-beginning-position))
  20095. justify)))
  20096. t)
  20097. ;; Fill comments.
  20098. (comment (fill-comment-paragraph justify))
  20099. ;; Ignore every other element.
  20100. (otherwise t))))))
  20101. (defun org-auto-fill-function ()
  20102. "Auto-fill function."
  20103. ;; Check if auto-filling is meaningful.
  20104. (let ((fc (current-fill-column)))
  20105. (when (and fc (> (current-column) fc))
  20106. (let* ((fill-prefix (org-adaptive-fill-function))
  20107. ;; Enforce empty fill prefix, if required. Otherwise, it
  20108. ;; will be computed again.
  20109. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20110. (when fill-prefix (do-auto-fill))))))
  20111. (defun org-comment-line-break-function (&optional soft)
  20112. "Break line at point and indent, continuing comment if within one.
  20113. The inserted newline is marked hard if variable
  20114. `use-hard-newlines' is true, unless optional argument SOFT is
  20115. non-nil."
  20116. (if soft (insert-and-inherit ?\n) (newline 1))
  20117. (save-excursion (forward-char -1) (delete-horizontal-space))
  20118. (delete-horizontal-space)
  20119. (indent-to-left-margin)
  20120. (insert-before-markers-and-inherit fill-prefix))
  20121. ;;; Comments
  20122. ;; Org comments syntax is quite complex. It requires the entire line
  20123. ;; to be just a comment. Also, even with the right syntax at the
  20124. ;; beginning of line, some some elements (i.e. verse-block or
  20125. ;; example-block) don't accept comments. Usual Emacs comment commands
  20126. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20127. ;; Org still relies on `comment-dwim', but cannot trust
  20128. ;; `comment-only-p'. So, `comment-region-function' and
  20129. ;; `uncomment-region-function' both point
  20130. ;; to`org-comment-or-uncomment-region'. Eventually,
  20131. ;; `org-insert-comment' takes care of insertion of comments at the
  20132. ;; beginning of line.
  20133. ;; `org-setup-comments-handling' install comments related variables
  20134. ;; during `org-mode' initialization.
  20135. (defun org-setup-comments-handling ()
  20136. (interactive)
  20137. (org-set-local 'comment-use-syntax nil)
  20138. (org-set-local 'comment-start "# ")
  20139. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20140. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20141. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20142. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20143. (defun org-insert-comment ()
  20144. "Insert an empty comment above current line.
  20145. If the line is empty, insert comment at its beginning."
  20146. (beginning-of-line)
  20147. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20148. (org-indent-line)
  20149. (insert "# "))
  20150. (defvar comment-empty-lines) ; From newcomment.el.
  20151. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20152. "Comment or uncomment each non-blank line in the region.
  20153. Uncomment each non-blank line between BEG and END if it only
  20154. contains commented lines. Otherwise, comment them."
  20155. (save-restriction
  20156. ;; Restrict region
  20157. (narrow-to-region (save-excursion (goto-char beg)
  20158. (skip-chars-forward " \r\t\n" end)
  20159. (line-beginning-position))
  20160. (save-excursion (goto-char end)
  20161. (skip-chars-backward " \r\t\n" beg)
  20162. (line-end-position)))
  20163. (let ((uncommentp
  20164. ;; UNCOMMENTP is non-nil when every non blank line between
  20165. ;; BEG and END is a comment.
  20166. (save-excursion
  20167. (goto-char (point-min))
  20168. (while (and (not (eobp))
  20169. (let ((element (org-element-at-point)))
  20170. (and (eq (org-element-type element) 'comment)
  20171. (goto-char (min (point-max)
  20172. (org-element-property
  20173. :end element)))))))
  20174. (eobp))))
  20175. (if uncommentp
  20176. ;; Only blank lines and comments in region: uncomment it.
  20177. (save-excursion
  20178. (goto-char (point-min))
  20179. (while (not (eobp))
  20180. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20181. (replace-match "" nil nil nil 1))
  20182. (forward-line)))
  20183. ;; Comment each line in region.
  20184. (let ((min-indent (point-max)))
  20185. ;; First find the minimum indentation across all lines.
  20186. (save-excursion
  20187. (goto-char (point-min))
  20188. (while (and (not (eobp)) (not (zerop min-indent)))
  20189. (unless (looking-at "[ \t]*$")
  20190. (setq min-indent (min min-indent (current-indentation))))
  20191. (forward-line)))
  20192. ;; Then loop over all lines.
  20193. (save-excursion
  20194. (goto-char (point-min))
  20195. (while (not (eobp))
  20196. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20197. (org-move-to-column min-indent t)
  20198. (insert comment-start))
  20199. (forward-line))))))))
  20200. ;;; Planning
  20201. ;; This section contains tools to operate on timestamp objects, as
  20202. ;; returned by, e.g. `org-element-context'.
  20203. (defun org-timestamp-has-time-p (timestamp)
  20204. "Non-nil when TIMESTAMP has a time specified."
  20205. (org-element-property :hour-start timestamp))
  20206. (defun org-timestamp-format (timestamp format &optional end utc)
  20207. "Format a TIMESTAMP element into a string.
  20208. FORMAT is a format specifier to be passed to
  20209. `format-time-string'.
  20210. When optional argument END is non-nil, use end of date-range or
  20211. time-range, if possible.
  20212. When optional argument UTC is non-nil, time will be expressed as
  20213. Universal Time."
  20214. (format-time-string
  20215. format
  20216. (apply 'encode-time
  20217. (cons 0
  20218. (mapcar
  20219. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20220. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20221. '(:minute-start :hour-start :day-start :month-start
  20222. :year-start)))))
  20223. utc))
  20224. (defun org-timestamp-split-range (timestamp &optional end)
  20225. "Extract a timestamp object from a date or time range.
  20226. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20227. the end of the range. Otherwise, extract its start.
  20228. Return a new timestamp object sharing the same parent as
  20229. TIMESTAMP."
  20230. (let ((type (org-element-property :type timestamp)))
  20231. (if (memq type '(active inactive diary)) timestamp
  20232. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20233. ;; Set new type.
  20234. (org-element-put-property
  20235. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20236. ;; Copy start properties over end properties if END is
  20237. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20238. (let ((p-alist '((:minute-start . :minute-end)
  20239. (:hour-start . :hour-end)
  20240. (:day-start . :day-end)
  20241. (:month-start . :month-end)
  20242. (:year-start . :year-end))))
  20243. (dolist (p-cell p-alist)
  20244. (org-element-put-property
  20245. split-ts
  20246. (funcall (if end 'car 'cdr) p-cell)
  20247. (org-element-property
  20248. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20249. ;; Eventually refresh `:raw-value'.
  20250. (org-element-put-property split-ts :raw-value nil)
  20251. (org-element-put-property
  20252. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20253. (defun org-timestamp-translate (timestamp &optional boundary)
  20254. "Apply `org-translate-time' on a TIMESTAMP object.
  20255. When optional argument BOUNDARY is non-nil, it is either the
  20256. symbol `start' or `end'. In this case, only translate the
  20257. starting or ending part of TIMESTAMP if it is a date or time
  20258. range. Otherwise, translate both parts."
  20259. (if (and (not boundary)
  20260. (memq (org-element-property :type timestamp)
  20261. '(active-range inactive-range)))
  20262. (concat
  20263. (org-translate-time
  20264. (org-element-property :raw-value
  20265. (org-timestamp-split-range timestamp)))
  20266. "--"
  20267. (org-translate-time
  20268. (org-element-property :raw-value
  20269. (org-timestamp-split-range timestamp t))))
  20270. (org-translate-time
  20271. (org-element-property
  20272. :raw-value
  20273. (if (not boundary) timestamp
  20274. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20275. ;;; Other stuff.
  20276. (defun org-toggle-fixed-width-section (arg)
  20277. "Toggle the fixed-width export.
  20278. If there is no active region, the QUOTE keyword at the current headline is
  20279. inserted or removed. When present, it causes the text between this headline
  20280. and the next to be exported as fixed-width text, and unmodified.
  20281. If there is an active region, this command adds or removes a colon as the
  20282. first character of this line. If the first character of a line is a colon,
  20283. this line is also exported in fixed-width font."
  20284. (interactive "P")
  20285. (let* ((cc 0)
  20286. (regionp (org-region-active-p))
  20287. (beg (if regionp (region-beginning) (point)))
  20288. (end (if regionp (region-end)))
  20289. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20290. (case-fold-search nil)
  20291. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20292. off)
  20293. (if regionp
  20294. (save-excursion
  20295. (goto-char beg)
  20296. (setq cc (current-column))
  20297. (beginning-of-line 1)
  20298. (setq off (looking-at re))
  20299. (while (> nlines 0)
  20300. (setq nlines (1- nlines))
  20301. (beginning-of-line 1)
  20302. (cond
  20303. (arg
  20304. (org-move-to-column cc t)
  20305. (insert ": \n")
  20306. (forward-line -1))
  20307. ((and off (looking-at re))
  20308. (replace-match "" t t nil 1))
  20309. ((not off) (org-move-to-column cc t) (insert ": ")))
  20310. (forward-line 1)))
  20311. (save-excursion
  20312. (org-back-to-heading)
  20313. (cond
  20314. ((looking-at (format org-heading-keyword-regexp-format
  20315. org-quote-string))
  20316. (goto-char (match-end 1))
  20317. (looking-at (concat " +" org-quote-string))
  20318. (replace-match "" t t)
  20319. (when (eolp) (insert " ")))
  20320. ((looking-at org-outline-regexp)
  20321. (goto-char (match-end 0))
  20322. (insert org-quote-string " ")))))))
  20323. (defun org-reftex-citation ()
  20324. "Use reftex-citation to insert a citation into the buffer.
  20325. This looks for a line like
  20326. #+BIBLIOGRAPHY: foo plain option:-d
  20327. and derives from it that foo.bib is the bibliography file relevant
  20328. for this document. It then installs the necessary environment for RefTeX
  20329. to work in this buffer and calls `reftex-citation' to insert a citation
  20330. into the buffer.
  20331. Export of such citations to both LaTeX and HTML is handled by the contributed
  20332. package org-exp-bibtex by Taru Karttunen."
  20333. (interactive)
  20334. (let ((reftex-docstruct-symbol 'rds)
  20335. (reftex-cite-format "\\cite{%l}")
  20336. rds bib)
  20337. (save-excursion
  20338. (save-restriction
  20339. (widen)
  20340. (let ((case-fold-search t)
  20341. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20342. (if (not (save-excursion
  20343. (or (re-search-forward re nil t)
  20344. (re-search-backward re nil t))))
  20345. (error "No bibliography defined in file")
  20346. (setq bib (concat (match-string 1) ".bib")
  20347. rds (list (list 'bib bib)))))))
  20348. (call-interactively 'reftex-citation)))
  20349. ;;;; Functions extending outline functionality
  20350. (defun org-beginning-of-line (&optional arg)
  20351. "Go to the beginning of the current line. If that is invisible, continue
  20352. to a visible line beginning. This makes the function of C-a more intuitive.
  20353. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20354. first attempt, and only move to after the tags when the cursor is already
  20355. beyond the end of the headline."
  20356. (interactive "P")
  20357. (let ((pos (point))
  20358. (special (if (consp org-special-ctrl-a/e)
  20359. (car org-special-ctrl-a/e)
  20360. org-special-ctrl-a/e))
  20361. refpos)
  20362. (if (org-bound-and-true-p visual-line-mode)
  20363. (beginning-of-visual-line 1)
  20364. (beginning-of-line 1))
  20365. (if (and arg (fboundp 'move-beginning-of-line))
  20366. (call-interactively 'move-beginning-of-line)
  20367. (if (bobp)
  20368. nil
  20369. (backward-char 1)
  20370. (if (org-truely-invisible-p)
  20371. (while (and (not (bobp)) (org-truely-invisible-p))
  20372. (backward-char 1)
  20373. (beginning-of-line 1))
  20374. (forward-char 1))))
  20375. (when special
  20376. (cond
  20377. ((and (looking-at org-complex-heading-regexp)
  20378. (= (char-after (match-end 1)) ?\ ))
  20379. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20380. (point-at-eol)))
  20381. (goto-char
  20382. (if (eq special t)
  20383. (cond ((> pos refpos) refpos)
  20384. ((= pos (point)) refpos)
  20385. (t (point)))
  20386. (cond ((> pos (point)) (point))
  20387. ((not (eq last-command this-command)) (point))
  20388. (t refpos)))))
  20389. ((org-at-item-p)
  20390. ;; Being at an item and not looking at an the item means point
  20391. ;; was previously moved to beginning of a visual line, which
  20392. ;; doesn't contain the item. Therefore, do nothing special,
  20393. ;; just stay here.
  20394. (when (looking-at org-list-full-item-re)
  20395. ;; Set special position at first white space character after
  20396. ;; bullet, and check-box, if any.
  20397. (let ((after-bullet
  20398. (let ((box (match-end 3)))
  20399. (if (not box) (match-end 1)
  20400. (let ((after (char-after box)))
  20401. (if (and after (= after ? )) (1+ box) box))))))
  20402. ;; Special case: Move point to special position when
  20403. ;; currently after it or at beginning of line.
  20404. (if (eq special t)
  20405. (when (or (> pos after-bullet) (= (point) pos))
  20406. (goto-char after-bullet))
  20407. ;; Reversed case: Move point to special position when
  20408. ;; point was already at beginning of line and command is
  20409. ;; repeated.
  20410. (when (and (= (point) pos) (eq last-command this-command))
  20411. (goto-char after-bullet))))))))
  20412. (org-no-warnings
  20413. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20414. (defun org-end-of-line (&optional arg)
  20415. "Go to the end of the line.
  20416. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20417. tags on the first attempt, and only move to after the tags when
  20418. the cursor is already beyond the end of the headline."
  20419. (interactive "P")
  20420. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20421. org-special-ctrl-a/e))
  20422. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20423. 'end-of-visual-line)
  20424. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20425. (t 'end-of-line))))
  20426. (if (or (not special) arg) (call-interactively move-fun)
  20427. (let* ((element (save-excursion (beginning-of-line)
  20428. (org-element-at-point)))
  20429. (type (org-element-type element)))
  20430. (cond
  20431. ((memq type '(headline inlinetask))
  20432. (let ((pos (point)))
  20433. (beginning-of-line 1)
  20434. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20435. (if (eq special t)
  20436. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20437. (goto-char (match-beginning 1))
  20438. (goto-char (match-end 0)))
  20439. (if (or (< pos (match-end 0))
  20440. (not (eq this-command last-command)))
  20441. (goto-char (match-end 0))
  20442. (goto-char (match-beginning 1))))
  20443. (call-interactively move-fun))))
  20444. ((org-element-property :hiddenp element)
  20445. ;; If element is hidden, `move-end-of-line' would put point
  20446. ;; after it. Use `end-of-line' to stay on current line.
  20447. (call-interactively 'end-of-line))
  20448. (t (call-interactively move-fun)))))
  20449. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20450. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20451. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20452. (defun org-backward-sentence (&optional arg)
  20453. "Go to beginning of sentence, or beginning of table field.
  20454. This will call `backward-sentence' or `org-table-beginning-of-field',
  20455. depending on context."
  20456. (interactive "P")
  20457. (cond
  20458. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20459. (t (call-interactively 'backward-sentence))))
  20460. (defun org-forward-sentence (&optional arg)
  20461. "Go to end of sentence, or end of table field.
  20462. This will call `forward-sentence' or `org-table-end-of-field',
  20463. depending on context."
  20464. (interactive "P")
  20465. (cond
  20466. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20467. (t (call-interactively 'forward-sentence))))
  20468. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20469. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20470. (defun org-kill-line (&optional arg)
  20471. "Kill line, to tags or end of line."
  20472. (interactive "P")
  20473. (cond
  20474. ((or (not org-special-ctrl-k)
  20475. (bolp)
  20476. (not (org-at-heading-p)))
  20477. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20478. org-ctrl-k-protect-subtree)
  20479. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20480. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20481. (error "C-k aborted - would kill hidden subtree")))
  20482. (call-interactively
  20483. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20484. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20485. (kill-region (point) (match-beginning 1))
  20486. (org-set-tags nil t))
  20487. (t (kill-region (point) (point-at-eol)))))
  20488. (define-key org-mode-map "\C-k" 'org-kill-line)
  20489. (defun org-yank (&optional arg)
  20490. "Yank. If the kill is a subtree, treat it specially.
  20491. This command will look at the current kill and check if is a single
  20492. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20493. cursor is at the beginning of a line or after the stars of a currently
  20494. empty headline, then the yank is handled specially. How exactly depends
  20495. on the value of the following variables, both set by default.
  20496. org-yank-folded-subtrees
  20497. When set, the subtree(s) will be folded after insertion, but only
  20498. if doing so would now swallow text after the yanked text.
  20499. org-yank-adjusted-subtrees
  20500. When set, the subtree will be promoted or demoted in order to
  20501. fit into the local outline tree structure, which means that the level
  20502. will be adjusted so that it becomes the smaller one of the two
  20503. *visible* surrounding headings.
  20504. Any prefix to this command will cause `yank' to be called directly with
  20505. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20506. will just
  20507. plainly yank the text as it is.
  20508. \[1] The test checks if the first non-white line is a heading
  20509. and if there are no other headings with fewer stars."
  20510. (interactive "P")
  20511. (org-yank-generic 'yank arg))
  20512. (defun org-yank-generic (command arg)
  20513. "Perform some yank-like command.
  20514. This function implements the behavior described in the `org-yank'
  20515. documentation. However, it has been generalized to work for any
  20516. interactive command with similar behavior."
  20517. ;; pretend to be command COMMAND
  20518. (setq this-command command)
  20519. (if arg
  20520. (call-interactively command)
  20521. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20522. (and (org-kill-is-subtree-p)
  20523. (or (bolp)
  20524. (and (looking-at "[ \t]*$")
  20525. (string-match
  20526. "\\`\\*+\\'"
  20527. (buffer-substring (point-at-bol) (point)))))))
  20528. swallowp)
  20529. (cond
  20530. ((and subtreep org-yank-folded-subtrees)
  20531. (let ((beg (point))
  20532. end)
  20533. (if (and subtreep org-yank-adjusted-subtrees)
  20534. (org-paste-subtree nil nil 'for-yank)
  20535. (call-interactively command))
  20536. (setq end (point))
  20537. (goto-char beg)
  20538. (when (and (bolp) subtreep
  20539. (not (setq swallowp
  20540. (org-yank-folding-would-swallow-text beg end))))
  20541. (org-with-limited-levels
  20542. (or (looking-at org-outline-regexp)
  20543. (re-search-forward org-outline-regexp-bol end t))
  20544. (while (and (< (point) end) (looking-at org-outline-regexp))
  20545. (hide-subtree)
  20546. (org-cycle-show-empty-lines 'folded)
  20547. (condition-case nil
  20548. (outline-forward-same-level 1)
  20549. (error (goto-char end))))))
  20550. (when swallowp
  20551. (message
  20552. "Inserted text not folded because that would swallow text"))
  20553. (goto-char end)
  20554. (skip-chars-forward " \t\n\r")
  20555. (beginning-of-line 1)
  20556. (push-mark beg 'nomsg)))
  20557. ((and subtreep org-yank-adjusted-subtrees)
  20558. (let ((beg (point-at-bol)))
  20559. (org-paste-subtree nil nil 'for-yank)
  20560. (push-mark beg 'nomsg)))
  20561. (t
  20562. (call-interactively command))))))
  20563. (defun org-yank-folding-would-swallow-text (beg end)
  20564. "Would hide-subtree at BEG swallow any text after END?"
  20565. (let (level)
  20566. (org-with-limited-levels
  20567. (save-excursion
  20568. (goto-char beg)
  20569. (when (or (looking-at org-outline-regexp)
  20570. (re-search-forward org-outline-regexp-bol end t))
  20571. (setq level (org-outline-level)))
  20572. (goto-char end)
  20573. (skip-chars-forward " \t\r\n\v\f")
  20574. (if (or (eobp)
  20575. (and (bolp) (looking-at org-outline-regexp)
  20576. (<= (org-outline-level) level)))
  20577. nil ; Nothing would be swallowed
  20578. t))))) ; something would swallow
  20579. (define-key org-mode-map "\C-y" 'org-yank)
  20580. (defun org-truely-invisible-p ()
  20581. "Check if point is at a character currently not visible.
  20582. This version does not only check the character property, but also
  20583. `visible-mode'."
  20584. ;; Early versions of noutline don't have `outline-invisible-p'.
  20585. (if (org-bound-and-true-p visible-mode)
  20586. nil
  20587. (outline-invisible-p)))
  20588. (defun org-invisible-p2 ()
  20589. "Check if point is at a character currently not visible."
  20590. (save-excursion
  20591. (if (and (eolp) (not (bobp))) (backward-char 1))
  20592. ;; Early versions of noutline don't have `outline-invisible-p'.
  20593. (outline-invisible-p)))
  20594. (defun org-back-to-heading (&optional invisible-ok)
  20595. "Call `outline-back-to-heading', but provide a better error message."
  20596. (condition-case nil
  20597. (outline-back-to-heading invisible-ok)
  20598. (error (error "Before first headline at position %d in buffer %s"
  20599. (point) (current-buffer)))))
  20600. (defun org-before-first-heading-p ()
  20601. "Before first heading?"
  20602. (save-excursion
  20603. (end-of-line)
  20604. (null (re-search-backward org-outline-regexp-bol nil t))))
  20605. (defun org-at-heading-p (&optional ignored)
  20606. (outline-on-heading-p t))
  20607. ;; Compatibility alias with Org versions < 7.8.03
  20608. (defalias 'org-on-heading-p 'org-at-heading-p)
  20609. (defun org-at-comment-p nil
  20610. "Is cursor in a line starting with a # character?"
  20611. (save-excursion
  20612. (beginning-of-line)
  20613. (looking-at "^#")))
  20614. (defun org-at-drawer-p nil
  20615. "Is cursor at a drawer keyword?"
  20616. (save-excursion
  20617. (move-beginning-of-line 1)
  20618. (looking-at org-drawer-regexp)))
  20619. (defun org-at-block-p nil
  20620. "Is cursor at a block keyword?"
  20621. (save-excursion
  20622. (move-beginning-of-line 1)
  20623. (looking-at org-block-regexp)))
  20624. (defun org-point-at-end-of-empty-headline ()
  20625. "If point is at the end of an empty headline, return t, else nil.
  20626. If the heading only contains a TODO keyword, it is still still considered
  20627. empty."
  20628. (and (looking-at "[ \t]*$")
  20629. (when org-todo-line-regexp
  20630. (save-excursion
  20631. (beginning-of-line 1)
  20632. (let ((case-fold-search nil))
  20633. (looking-at org-todo-line-regexp)
  20634. (string= (match-string 3) ""))))))
  20635. (defun org-at-heading-or-item-p ()
  20636. (or (org-at-heading-p) (org-at-item-p)))
  20637. (defun org-at-target-p ()
  20638. (or (org-in-regexp org-radio-target-regexp)
  20639. (org-in-regexp org-target-regexp)))
  20640. ;; Compatibility alias with Org versions < 7.8.03
  20641. (defalias 'org-on-target-p 'org-at-target-p)
  20642. (defun org-up-heading-all (arg)
  20643. "Move to the heading line of which the present line is a subheading.
  20644. This function considers both visible and invisible heading lines.
  20645. With argument, move up ARG levels."
  20646. (if (fboundp 'outline-up-heading-all)
  20647. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20648. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20649. (defun org-up-heading-safe ()
  20650. "Move to the heading line of which the present line is a subheading.
  20651. This version will not throw an error. It will return the level of the
  20652. headline found, or nil if no higher level is found.
  20653. Also, this function will be a lot faster than `outline-up-heading',
  20654. because it relies on stars being the outline starters. This can really
  20655. make a significant difference in outlines with very many siblings."
  20656. (let (start-level re)
  20657. (org-back-to-heading t)
  20658. (setq start-level (funcall outline-level))
  20659. (if (equal start-level 1)
  20660. nil
  20661. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20662. (if (re-search-backward re nil t)
  20663. (funcall outline-level)))))
  20664. (defun org-first-sibling-p ()
  20665. "Is this heading the first child of its parents?"
  20666. (interactive)
  20667. (let ((re org-outline-regexp-bol)
  20668. level l)
  20669. (unless (org-at-heading-p t)
  20670. (error "Not at a heading"))
  20671. (setq level (funcall outline-level))
  20672. (save-excursion
  20673. (if (not (re-search-backward re nil t))
  20674. t
  20675. (setq l (funcall outline-level))
  20676. (< l level)))))
  20677. (defun org-goto-sibling (&optional previous)
  20678. "Goto the next sibling, even if it is invisible.
  20679. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20680. when a sibling was found. When none is found, return nil and don't
  20681. move point."
  20682. (let ((fun (if previous 're-search-backward 're-search-forward))
  20683. (pos (point))
  20684. (re org-outline-regexp-bol)
  20685. level l)
  20686. (when (condition-case nil (org-back-to-heading t) (error nil))
  20687. (setq level (funcall outline-level))
  20688. (catch 'exit
  20689. (or previous (forward-char 1))
  20690. (while (funcall fun re nil t)
  20691. (setq l (funcall outline-level))
  20692. (when (< l level) (goto-char pos) (throw 'exit nil))
  20693. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20694. (goto-char pos)
  20695. nil))))
  20696. (defun org-show-siblings ()
  20697. "Show all siblings of the current headline."
  20698. (save-excursion
  20699. (while (org-goto-sibling) (org-flag-heading nil)))
  20700. (save-excursion
  20701. (while (org-goto-sibling 'previous)
  20702. (org-flag-heading nil))))
  20703. (defun org-goto-first-child ()
  20704. "Goto the first child, even if it is invisible.
  20705. Return t when a child was found. Otherwise don't move point and
  20706. return nil."
  20707. (let (level (pos (point)) (re org-outline-regexp-bol))
  20708. (when (condition-case nil (org-back-to-heading t) (error nil))
  20709. (setq level (outline-level))
  20710. (forward-char 1)
  20711. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20712. (progn (goto-char (match-beginning 0)) t)
  20713. (goto-char pos) nil))))
  20714. (defun org-show-hidden-entry ()
  20715. "Show an entry where even the heading is hidden."
  20716. (save-excursion
  20717. (org-show-entry)))
  20718. (defun org-flag-heading (flag &optional entry)
  20719. "Flag the current heading. FLAG non-nil means make invisible.
  20720. When ENTRY is non-nil, show the entire entry."
  20721. (save-excursion
  20722. (org-back-to-heading t)
  20723. ;; Check if we should show the entire entry
  20724. (if entry
  20725. (progn
  20726. (org-show-entry)
  20727. (save-excursion
  20728. (and (outline-next-heading)
  20729. (org-flag-heading nil))))
  20730. (outline-flag-region (max (point-min) (1- (point)))
  20731. (save-excursion (outline-end-of-heading) (point))
  20732. flag))))
  20733. (defun org-get-next-sibling ()
  20734. "Move to next heading of the same level, and return point.
  20735. If there is no such heading, return nil.
  20736. This is like outline-next-sibling, but invisible headings are ok."
  20737. (let ((level (funcall outline-level)))
  20738. (outline-next-heading)
  20739. (while (and (not (eobp)) (> (funcall outline-level) level))
  20740. (outline-next-heading))
  20741. (if (or (eobp) (< (funcall outline-level) level))
  20742. nil
  20743. (point))))
  20744. (defun org-get-last-sibling ()
  20745. "Move to previous heading of the same level, and return point.
  20746. If there is no such heading, return nil."
  20747. (let ((opoint (point))
  20748. (level (funcall outline-level)))
  20749. (outline-previous-heading)
  20750. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20751. (while (and (> (funcall outline-level) level)
  20752. (not (bobp)))
  20753. (outline-previous-heading))
  20754. (if (< (funcall outline-level) level)
  20755. nil
  20756. (point)))))
  20757. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20758. "Goto to the end of a subtree."
  20759. ;; This contains an exact copy of the original function, but it uses
  20760. ;; `org-back-to-heading', to make it work also in invisible
  20761. ;; trees. And is uses an invisible-ok argument.
  20762. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20763. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20764. ;; with many children and grandchildren etc, this can be much faster
  20765. ;; than the outline version.
  20766. (org-back-to-heading invisible-ok)
  20767. (let ((first t)
  20768. (level (funcall outline-level)))
  20769. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20770. ;; A true heading (not a plain list item), in Org-mode
  20771. ;; This means we can easily find the end by looking
  20772. ;; only for the right number of stars. Using a regexp to do
  20773. ;; this is so much faster than using a Lisp loop.
  20774. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20775. (forward-char 1)
  20776. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20777. ;; something else, do it the slow way
  20778. (while (and (not (eobp))
  20779. (or first (> (funcall outline-level) level)))
  20780. (setq first nil)
  20781. (outline-next-heading)))
  20782. (unless to-heading
  20783. (if (memq (preceding-char) '(?\n ?\^M))
  20784. (progn
  20785. ;; Go to end of line before heading
  20786. (forward-char -1)
  20787. (if (memq (preceding-char) '(?\n ?\^M))
  20788. ;; leave blank line before heading
  20789. (forward-char -1))))))
  20790. (point))
  20791. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20792. "Use Org version in org-mode, for dramatic speed-up."
  20793. (if (derived-mode-p 'org-mode)
  20794. (progn
  20795. (org-end-of-subtree nil t)
  20796. (unless (eobp) (backward-char 1)))
  20797. ad-do-it))
  20798. (defun org-end-of-meta-data-and-drawers ()
  20799. "Jump to the first text after meta data and drawers in the current entry.
  20800. This will move over empty lines, lines with planning time stamps,
  20801. clocking lines, and drawers."
  20802. (org-back-to-heading t)
  20803. (let ((end (save-excursion (outline-next-heading) (point)))
  20804. (re (concat "\\(" org-drawer-regexp "\\)"
  20805. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20806. (forward-line 1)
  20807. (while (re-search-forward re end t)
  20808. (if (not (match-end 1))
  20809. ;; empty or planning line
  20810. (forward-line 1)
  20811. ;; a drawer, find the end
  20812. (re-search-forward "^[ \t]*:END:" end 'move)
  20813. (forward-line 1)))
  20814. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20815. (point)))
  20816. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20817. "Move forward to the ARG'th subheading at same level as this one.
  20818. Stop at the first and last subheadings of a superior heading.
  20819. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20820. non-nil it will also look at invisible ones."
  20821. (interactive "p")
  20822. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  20823. (if (and arg (< arg 0))
  20824. (goto-char (point-min))
  20825. (outline-next-heading))
  20826. (org-at-heading-p)
  20827. (let ((level (- (match-end 0) (match-beginning 0) 1))
  20828. (f (if (and arg (< arg 0))
  20829. 're-search-backward
  20830. 're-search-forward))
  20831. (count (if arg (abs arg) 1))
  20832. (result (point)))
  20833. (forward-char (if (and arg (< arg 0)) -1 1))
  20834. (while (and (> count 0)
  20835. (funcall f org-outline-regexp-bol nil 'move))
  20836. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  20837. (cond ((< l level) (setq count 0))
  20838. ((and (= l level)
  20839. (or invisible-ok
  20840. (progn
  20841. (goto-char (line-beginning-position))
  20842. (not (outline-invisible-p)))))
  20843. (setq count (1- count))
  20844. (when (eq l level)
  20845. (setq result (point)))))))
  20846. (goto-char result))
  20847. (beginning-of-line 1)))
  20848. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20849. "Move backward to the ARG'th subheading at same level as this one.
  20850. Stop at the first and last subheadings of a superior heading."
  20851. (interactive "p")
  20852. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  20853. (defun org-next-block (arg &optional backward block-regexp)
  20854. "Jump to the next block.
  20855. With a prefix argument ARG, jump forward ARG many source blocks.
  20856. When BACKWARD is non-nil, jump to the previous block.
  20857. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20858. (interactive "p")
  20859. (let ((re (or block-regexp org-block-regexp))
  20860. (re-search-fn (or (and backward 're-search-backward)
  20861. 're-search-forward)))
  20862. (if (looking-at re) (forward-char 1))
  20863. (condition-case nil
  20864. (funcall re-search-fn re nil nil arg)
  20865. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  20866. (goto-char (match-beginning 0)) (org-show-context)))
  20867. (defun org-previous-block (arg &optional block-regexp)
  20868. "Jump to the previous block.
  20869. With a prefix argument ARG, jump backward ARG many source blocks.
  20870. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20871. (interactive "p")
  20872. (org-next-block arg t block-regexp))
  20873. (defun org-forward-element ()
  20874. "Move forward by one element.
  20875. Move to the next element at the same level, when possible."
  20876. (interactive)
  20877. (cond ((eobp) (user-error "Cannot move further down"))
  20878. ((org-with-limited-levels (org-at-heading-p))
  20879. (let ((origin (point)))
  20880. (goto-char (org-end-of-subtree nil t))
  20881. (unless (org-with-limited-levels (org-at-heading-p))
  20882. (goto-char origin)
  20883. (user-error "Cannot move further down"))))
  20884. (t
  20885. (let* ((elem (org-element-at-point))
  20886. (end (org-element-property :end elem))
  20887. (parent (org-element-property :parent elem)))
  20888. (if (and parent (= (org-element-property :contents-end parent) end))
  20889. (goto-char (org-element-property :end parent))
  20890. (goto-char end))))))
  20891. (defun org-backward-element ()
  20892. "Move backward by one element.
  20893. Move to the previous element at the same level, when possible."
  20894. (interactive)
  20895. (cond ((bobp) (user-error "Cannot move further up"))
  20896. ((org-with-limited-levels (org-at-heading-p))
  20897. ;; At a headline, move to the previous one, if any, or stay
  20898. ;; here.
  20899. (let ((origin (point)))
  20900. (org-with-limited-levels (org-backward-heading-same-level 1))
  20901. ;; When current headline has no sibling above, move to its
  20902. ;; parent.
  20903. (when (= (point) origin)
  20904. (or (org-with-limited-levels (org-up-heading-safe))
  20905. (progn (goto-char origin)
  20906. (user-error "Cannot move further up"))))))
  20907. (t
  20908. (let* ((trail (org-element-at-point 'keep-trail))
  20909. (elem (car trail))
  20910. (prev-elem (nth 1 trail))
  20911. (beg (org-element-property :begin elem)))
  20912. (cond
  20913. ;; Move to beginning of current element if point isn't
  20914. ;; there already.
  20915. ((/= (point) beg) (goto-char beg))
  20916. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20917. ((org-before-first-heading-p) (goto-char (point-min)))
  20918. (t (org-back-to-heading)))))))
  20919. (defun org-up-element ()
  20920. "Move to upper element."
  20921. (interactive)
  20922. (if (org-with-limited-levels (org-at-heading-p))
  20923. (unless (org-up-heading-safe) (error "No surrounding element"))
  20924. (let* ((elem (org-element-at-point))
  20925. (parent (org-element-property :parent elem)))
  20926. (if parent (goto-char (org-element-property :begin parent))
  20927. (if (org-with-limited-levels (org-before-first-heading-p))
  20928. (error "No surrounding element")
  20929. (org-with-limited-levels (org-back-to-heading)))))))
  20930. (defvar org-element-greater-elements)
  20931. (defun org-down-element ()
  20932. "Move to inner element."
  20933. (interactive)
  20934. (let ((element (org-element-at-point)))
  20935. (cond
  20936. ((memq (org-element-type element) '(plain-list table))
  20937. (goto-char (org-element-property :contents-begin element))
  20938. (forward-char))
  20939. ((memq (org-element-type element) org-element-greater-elements)
  20940. ;; If contents are hidden, first disclose them.
  20941. (when (org-element-property :hiddenp element) (org-cycle))
  20942. (goto-char (or (org-element-property :contents-begin element)
  20943. (error "No content for this element"))))
  20944. (t (error "No inner element")))))
  20945. (defun org-drag-element-backward ()
  20946. "Move backward element at point."
  20947. (interactive)
  20948. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20949. (let* ((trail (org-element-at-point 'keep-trail))
  20950. (elem (car trail))
  20951. (prev-elem (nth 1 trail)))
  20952. ;; Error out if no previous element or previous element is
  20953. ;; a parent of the current one.
  20954. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20955. (error "Cannot drag element backward")
  20956. (let ((pos (point)))
  20957. (org-element-swap-A-B prev-elem elem)
  20958. (goto-char (+ (org-element-property :begin prev-elem)
  20959. (- pos (org-element-property :begin elem)))))))))
  20960. (defun org-drag-element-forward ()
  20961. "Move forward element at point."
  20962. (interactive)
  20963. (let* ((pos (point))
  20964. (elem (org-element-at-point)))
  20965. (when (= (point-max) (org-element-property :end elem))
  20966. (error "Cannot drag element forward"))
  20967. (goto-char (org-element-property :end elem))
  20968. (let ((next-elem (org-element-at-point)))
  20969. (when (or (org-element-nested-p elem next-elem)
  20970. (and (eq (org-element-type next-elem) 'headline)
  20971. (not (eq (org-element-type elem) 'headline))))
  20972. (goto-char pos)
  20973. (error "Cannot drag element forward"))
  20974. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20975. ;; body's length (without final blanks) and by the length of
  20976. ;; blanks between ELEM and NEXT-ELEM.
  20977. (let ((size-next (- (save-excursion
  20978. (goto-char (org-element-property :end next-elem))
  20979. (skip-chars-backward " \r\t\n")
  20980. (forward-line)
  20981. ;; Small correction if buffer doesn't end
  20982. ;; with a newline character.
  20983. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20984. (org-element-property :begin next-elem)))
  20985. (size-blank (- (org-element-property :end elem)
  20986. (save-excursion
  20987. (goto-char (org-element-property :end elem))
  20988. (skip-chars-backward " \r\t\n")
  20989. (forward-line)
  20990. (point)))))
  20991. (org-element-swap-A-B elem next-elem)
  20992. (goto-char (+ pos size-next size-blank))))))
  20993. (defun org-drag-line-forward (arg)
  20994. "Drag the line at point ARG lines forward."
  20995. (interactive "p")
  20996. (dotimes (n (abs arg))
  20997. (let ((c (current-column)))
  20998. (if (< 0 arg)
  20999. (progn
  21000. (beginning-of-line 2)
  21001. (transpose-lines 1)
  21002. (beginning-of-line 0))
  21003. (transpose-lines 1)
  21004. (beginning-of-line -1))
  21005. (org-move-to-column c))))
  21006. (defun org-drag-line-backward (arg)
  21007. "Drag the line at point ARG lines backward."
  21008. (interactive "p")
  21009. (org-drag-line-forward (- arg)))
  21010. (defun org-mark-element ()
  21011. "Put point at beginning of this element, mark at end.
  21012. Interactively, if this command is repeated or (in Transient Mark
  21013. mode) if the mark is active, it marks the next element after the
  21014. ones already marked."
  21015. (interactive)
  21016. (let (deactivate-mark)
  21017. (if (and (org-called-interactively-p 'any)
  21018. (or (and (eq last-command this-command) (mark t))
  21019. (and transient-mark-mode mark-active)))
  21020. (set-mark
  21021. (save-excursion
  21022. (goto-char (mark))
  21023. (goto-char (org-element-property :end (org-element-at-point)))))
  21024. (let ((element (org-element-at-point)))
  21025. (end-of-line)
  21026. (push-mark (org-element-property :end element) t t)
  21027. (goto-char (org-element-property :begin element))))))
  21028. (defun org-narrow-to-element ()
  21029. "Narrow buffer to current element."
  21030. (interactive)
  21031. (let ((elem (org-element-at-point)))
  21032. (cond
  21033. ((eq (car elem) 'headline)
  21034. (narrow-to-region
  21035. (org-element-property :begin elem)
  21036. (org-element-property :end elem)))
  21037. ((memq (car elem) org-element-greater-elements)
  21038. (narrow-to-region
  21039. (org-element-property :contents-begin elem)
  21040. (org-element-property :contents-end elem)))
  21041. (t
  21042. (narrow-to-region
  21043. (org-element-property :begin elem)
  21044. (org-element-property :end elem))))))
  21045. (defun org-transpose-element ()
  21046. "Transpose current and previous elements, keeping blank lines between.
  21047. Point is moved after both elements."
  21048. (interactive)
  21049. (org-skip-whitespace)
  21050. (let ((end (org-element-property :end (org-element-at-point))))
  21051. (org-drag-element-backward)
  21052. (goto-char end)))
  21053. (defun org-unindent-buffer ()
  21054. "Un-indent the visible part of the buffer.
  21055. Relative indentation (between items, inside blocks, etc.) isn't
  21056. modified."
  21057. (interactive)
  21058. (unless (eq major-mode 'org-mode)
  21059. (error "Cannot un-indent a buffer not in Org mode"))
  21060. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21061. unindent-tree ; For byte-compiler.
  21062. (unindent-tree
  21063. (function
  21064. (lambda (contents)
  21065. (mapc
  21066. (lambda (element)
  21067. (if (memq (org-element-type element) '(headline section))
  21068. (funcall unindent-tree (org-element-contents element))
  21069. (save-excursion
  21070. (save-restriction
  21071. (narrow-to-region
  21072. (org-element-property :begin element)
  21073. (org-element-property :end element))
  21074. (org-do-remove-indentation)))))
  21075. (reverse contents))))))
  21076. (funcall unindent-tree (org-element-contents parse-tree))))
  21077. (defun org-show-subtree ()
  21078. "Show everything after this heading at deeper levels."
  21079. (interactive)
  21080. (outline-flag-region
  21081. (point)
  21082. (save-excursion
  21083. (org-end-of-subtree t t))
  21084. nil))
  21085. (defun org-show-entry ()
  21086. "Show the body directly following this heading.
  21087. Show the heading too, if it is currently invisible."
  21088. (interactive)
  21089. (save-excursion
  21090. (condition-case nil
  21091. (progn
  21092. (org-back-to-heading t)
  21093. (outline-flag-region
  21094. (max (point-min) (1- (point)))
  21095. (save-excursion
  21096. (if (re-search-forward
  21097. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21098. (match-beginning 1)
  21099. (point-max)))
  21100. nil)
  21101. (org-cycle-hide-drawers 'children))
  21102. (error nil))))
  21103. (defun org-make-options-regexp (kwds &optional extra)
  21104. "Make a regular expression for keyword lines."
  21105. (concat
  21106. "^#\\+\\("
  21107. (mapconcat 'regexp-quote kwds "\\|")
  21108. (if extra (concat "\\|" extra))
  21109. "\\):[ \t]*\\(.*\\)"))
  21110. ;; Make isearch reveal the necessary context
  21111. (defun org-isearch-end ()
  21112. "Reveal context after isearch exits."
  21113. (when isearch-success ; only if search was successful
  21114. (if (featurep 'xemacs)
  21115. ;; Under XEmacs, the hook is run in the correct place,
  21116. ;; we directly show the context.
  21117. (org-show-context 'isearch)
  21118. ;; In Emacs the hook runs *before* restoring the overlays.
  21119. ;; So we have to use a one-time post-command-hook to do this.
  21120. ;; (Emacs 22 has a special variable, see function `org-mode')
  21121. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21122. isearch-mode-end-hook-quit)
  21123. ;; Only when the isearch was not quitted.
  21124. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21125. 'append 'local)))))
  21126. (defun org-isearch-post-command ()
  21127. "Remove self from hook, and show context."
  21128. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21129. (org-show-context 'isearch))
  21130. ;;;; Integration with and fixes for other packages
  21131. ;;; Imenu support
  21132. (defvar org-imenu-markers nil
  21133. "All markers currently used by Imenu.")
  21134. (make-variable-buffer-local 'org-imenu-markers)
  21135. (defun org-imenu-new-marker (&optional pos)
  21136. "Return a new marker for use by Imenu, and remember the marker."
  21137. (let ((m (make-marker)))
  21138. (move-marker m (or pos (point)))
  21139. (push m org-imenu-markers)
  21140. m))
  21141. (defun org-imenu-get-tree ()
  21142. "Produce the index for Imenu."
  21143. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21144. (setq org-imenu-markers nil)
  21145. (let* ((n org-imenu-depth)
  21146. (re (concat "^" (org-get-limited-outline-regexp)))
  21147. (subs (make-vector (1+ n) nil))
  21148. (last-level 0)
  21149. m level head)
  21150. (save-excursion
  21151. (save-restriction
  21152. (widen)
  21153. (goto-char (point-max))
  21154. (while (re-search-backward re nil t)
  21155. (setq level (org-reduced-level (funcall outline-level)))
  21156. (when (and (<= level n)
  21157. (looking-at org-complex-heading-regexp))
  21158. (setq head (org-link-display-format
  21159. (org-match-string-no-properties 4))
  21160. m (org-imenu-new-marker))
  21161. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21162. (if (>= level last-level)
  21163. (push (cons head m) (aref subs level))
  21164. (push (cons head (aref subs (1+ level))) (aref subs level))
  21165. (loop for i from (1+ level) to n do (aset subs i nil)))
  21166. (setq last-level level)))))
  21167. (aref subs 1)))
  21168. (eval-after-load "imenu"
  21169. '(progn
  21170. (add-hook 'imenu-after-jump-hook
  21171. (lambda ()
  21172. (if (derived-mode-p 'org-mode)
  21173. (org-show-context 'org-goto))))))
  21174. (defun org-link-display-format (link)
  21175. "Replace a link with either the description, or the link target
  21176. if no description is present"
  21177. (save-match-data
  21178. (if (string-match org-bracket-link-analytic-regexp link)
  21179. (replace-match (if (match-end 5)
  21180. (match-string 5 link)
  21181. (concat (match-string 1 link)
  21182. (match-string 3 link)))
  21183. nil t link)
  21184. link)))
  21185. (defun org-toggle-link-display ()
  21186. "Toggle the literal or descriptive display of links."
  21187. (interactive)
  21188. (if org-descriptive-links
  21189. (progn (org-remove-from-invisibility-spec '(org-link))
  21190. (org-restart-font-lock)
  21191. (setq org-descriptive-links nil))
  21192. (progn (add-to-invisibility-spec '(org-link))
  21193. (org-restart-font-lock)
  21194. (setq org-descriptive-links t))))
  21195. ;; Speedbar support
  21196. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21197. "Overlay marking the agenda restriction line in speedbar.")
  21198. (overlay-put org-speedbar-restriction-lock-overlay
  21199. 'face 'org-agenda-restriction-lock)
  21200. (overlay-put org-speedbar-restriction-lock-overlay
  21201. 'help-echo "Agendas are currently limited to this item.")
  21202. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21203. (defun org-speedbar-set-agenda-restriction ()
  21204. "Restrict future agenda commands to the location at point in speedbar.
  21205. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21206. (interactive)
  21207. (require 'org-agenda)
  21208. (let (p m tp np dir txt)
  21209. (cond
  21210. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21211. 'org-imenu t))
  21212. (setq m (get-text-property p 'org-imenu-marker))
  21213. (with-current-buffer (marker-buffer m)
  21214. (goto-char m)
  21215. (org-agenda-set-restriction-lock 'subtree)))
  21216. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21217. 'speedbar-function 'speedbar-find-file))
  21218. (setq tp (previous-single-property-change
  21219. (1+ p) 'speedbar-function)
  21220. np (next-single-property-change
  21221. tp 'speedbar-function)
  21222. dir (speedbar-line-directory)
  21223. txt (buffer-substring-no-properties (or tp (point-min))
  21224. (or np (point-max))))
  21225. (with-current-buffer (find-file-noselect
  21226. (let ((default-directory dir))
  21227. (expand-file-name txt)))
  21228. (unless (derived-mode-p 'org-mode)
  21229. (error "Cannot restrict to non-Org-mode file"))
  21230. (org-agenda-set-restriction-lock 'file)))
  21231. (t (error "Don't know how to restrict Org-mode's agenda")))
  21232. (move-overlay org-speedbar-restriction-lock-overlay
  21233. (point-at-bol) (point-at-eol))
  21234. (setq current-prefix-arg nil)
  21235. (org-agenda-maybe-redo)))
  21236. (eval-after-load "speedbar"
  21237. '(progn
  21238. (speedbar-add-supported-extension ".org")
  21239. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21240. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21241. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21242. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21243. (add-hook 'speedbar-visiting-tag-hook
  21244. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21245. ;;; Fixes and Hacks for problems with other packages
  21246. ;; Make flyspell not check words in links, to not mess up our keymap
  21247. (defvar org-element-affiliated-keywords) ; From org-element.el
  21248. (defvar org-element-block-name-alist) ; From org-element.el
  21249. (defun org-mode-flyspell-verify ()
  21250. "Don't let flyspell put overlays at active buttons, or on
  21251. {todo,all-time,additional-option-like}-keywords."
  21252. (let ((pos (max (1- (point)) (point-min)))
  21253. (word (thing-at-point 'word)))
  21254. (and (not (get-text-property pos 'keymap))
  21255. (not (get-text-property pos 'org-no-flyspell))
  21256. (not (member word org-todo-keywords-1))
  21257. (not (member word org-all-time-keywords))
  21258. (not (member word org-options-keywords))
  21259. (not (member word (mapcar 'car org-startup-options)))
  21260. (not (member-ignore-case word org-element-affiliated-keywords))
  21261. (not (member-ignore-case word (org-get-export-keywords)))
  21262. (not (member-ignore-case
  21263. word (mapcar 'car org-element-block-name-alist)))
  21264. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21265. (defun org-remove-flyspell-overlays-in (beg end)
  21266. "Remove flyspell overlays in region."
  21267. (and (org-bound-and-true-p flyspell-mode)
  21268. (fboundp 'flyspell-delete-region-overlays)
  21269. (flyspell-delete-region-overlays beg end))
  21270. (add-text-properties beg end '(org-no-flyspell t)))
  21271. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21272. (eval-after-load "bookmark"
  21273. '(if (boundp 'bookmark-after-jump-hook)
  21274. ;; We can use the hook
  21275. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21276. ;; Hook not available, use advice
  21277. (defadvice bookmark-jump (after org-make-visible activate)
  21278. "Make the position visible."
  21279. (org-bookmark-jump-unhide))))
  21280. ;; Make sure saveplace shows the location if it was hidden
  21281. (eval-after-load "saveplace"
  21282. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21283. "Make the position visible."
  21284. (org-bookmark-jump-unhide)))
  21285. ;; Make sure ecb shows the location if it was hidden
  21286. (eval-after-load "ecb"
  21287. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21288. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21289. (if (derived-mode-p 'org-mode)
  21290. (org-show-context))))
  21291. (defun org-bookmark-jump-unhide ()
  21292. "Unhide the current position, to show the bookmark location."
  21293. (and (derived-mode-p 'org-mode)
  21294. (or (outline-invisible-p)
  21295. (save-excursion (goto-char (max (point-min) (1- (point))))
  21296. (outline-invisible-p)))
  21297. (org-show-context 'bookmark-jump)))
  21298. ;; Make session.el ignore our circular variable
  21299. (eval-after-load "session"
  21300. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21301. ;;;; Experimental code
  21302. (defun org-closed-in-range ()
  21303. "Sparse tree of items closed in a certain time range.
  21304. Still experimental, may disappear in the future."
  21305. (interactive)
  21306. ;; Get the time interval from the user.
  21307. (let* ((time1 (org-float-time
  21308. (org-read-date nil 'to-time nil "Starting date: ")))
  21309. (time2 (org-float-time
  21310. (org-read-date nil 'to-time nil "End date:")))
  21311. ;; callback function
  21312. (callback (lambda ()
  21313. (let ((time
  21314. (org-float-time
  21315. (apply 'encode-time
  21316. (org-parse-time-string
  21317. (match-string 1))))))
  21318. ;; check if time in interval
  21319. (and (>= time time1) (<= time time2))))))
  21320. ;; make tree, check each match with the callback
  21321. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21322. ;;;; Finish up
  21323. (provide 'org)
  21324. (run-hooks 'org-load-hook)
  21325. ;;; org.el ends here