org.el 895 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. (make-variable-buffer-local 'org-table-formula-constants-local)
  65. ;;;; Require other packages
  66. (eval-when-compile
  67. (require 'cl)
  68. (require 'gnus-sum))
  69. (require 'calendar)
  70. (require 'find-func)
  71. (require 'format-spec)
  72. (load "org-loaddefs.el" t t t)
  73. (require 'org-macs)
  74. (require 'org-compat)
  75. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  76. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  77. ;;
  78. ;; In Org buffers, the value of `outline-regexp' is that of
  79. ;; `org-outline-regexp'. The only function still directly relying on
  80. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  81. ;; job when `orgstruct-mode' is active.
  82. (defvar org-outline-regexp "\\*+ "
  83. "Regexp to match Org headlines.")
  84. (defvar org-outline-regexp-bol "^\\*+ "
  85. "Regexp to match Org headlines.
  86. This is similar to `org-outline-regexp' but additionally makes
  87. sure that we are at the beginning of the line.")
  88. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  89. "Matches a headline, putting stars and text into groups.
  90. Stars are put in group 1 and the trimmed body in group 2.")
  91. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  92. (unless (boundp 'calendar-view-holidays-initially-flag)
  93. (org-defvaralias 'calendar-view-holidays-initially-flag
  94. 'view-calendar-holidays-initially))
  95. (unless (boundp 'calendar-view-diary-initially-flag)
  96. (org-defvaralias 'calendar-view-diary-initially-flag
  97. 'view-diary-entries-initially))
  98. (unless (boundp 'diary-fancy-buffer)
  99. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  100. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  101. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  102. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  103. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  104. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  105. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  106. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  107. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  108. (declare-function orgtbl-mode "org-table" (&optional arg))
  109. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  110. (declare-function org-beamer-mode "ox-beamer" ())
  111. (declare-function org-table-edit-field "org-table" (arg))
  112. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  113. (declare-function org-table-set-constants "org-table" ())
  114. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  115. (declare-function org-id-get-create "org-id" (&optional force))
  116. (declare-function org-id-find-id-file "org-id" (id))
  117. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  118. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  119. (declare-function org-agenda-redo "org-agenda" (&optional all))
  120. (declare-function org-table-align "org-table" ())
  121. (declare-function org-table-paste-rectangle "org-table" ())
  122. (declare-function org-table-maybe-eval-formula "org-table" ())
  123. (declare-function org-table-maybe-recalculate-line "org-table" ())
  124. (declare-function org-element--parse-objects "org-element"
  125. (beg end acc restriction))
  126. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  127. (declare-function org-element-contents "org-element" (element))
  128. (declare-function org-element-context "org-element" (&optional element))
  129. (declare-function org-element-interpret-data "org-element"
  130. (data &optional parent))
  131. (declare-function org-element-map "org-element"
  132. (data types fun &optional info first-match no-recursion))
  133. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  134. (declare-function org-element-parse-buffer "org-element"
  135. (&optional granularity visible-only))
  136. (declare-function org-element-property "org-element" (property element))
  137. (declare-function org-element-put-property "org-element"
  138. (element property value))
  139. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  140. (declare-function org-element--parse-objects "org-element"
  141. (beg end acc restriction))
  142. (declare-function org-element-parse-buffer "org-element"
  143. (&optional granularity visible-only))
  144. (declare-function org-element-restriction "org-element" (element))
  145. (declare-function org-element-type "org-element" (element))
  146. ;; load languages based on value of `org-babel-load-languages'
  147. (defvar org-babel-load-languages)
  148. ;;;###autoload
  149. (defun org-babel-do-load-languages (sym value)
  150. "Load the languages defined in `org-babel-load-languages'."
  151. (set-default sym value)
  152. (mapc (lambda (pair)
  153. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  154. (if active
  155. (progn
  156. (require (intern (concat "ob-" lang))))
  157. (progn
  158. (funcall 'fmakunbound
  159. (intern (concat "org-babel-execute:" lang)))
  160. (funcall 'fmakunbound
  161. (intern (concat "org-babel-expand-body:" lang)))))))
  162. org-babel-load-languages))
  163. ;;;###autoload
  164. (defun org-babel-load-file (file &optional compile)
  165. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  166. This function exports the source code using `org-babel-tangle'
  167. and then loads the resulting file using `load-file'. With prefix
  168. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  169. file to byte-code before it is loaded."
  170. (interactive "fFile to load: \nP")
  171. (require 'ob-core)
  172. (let* ((age (lambda (file)
  173. (float-time
  174. (time-subtract (current-time)
  175. (nth 5 (or (file-attributes (file-truename file))
  176. (file-attributes file)))))))
  177. (base-name (file-name-sans-extension file))
  178. (exported-file (concat base-name ".el")))
  179. ;; tangle if the org-mode file is newer than the elisp file
  180. (unless (and (file-exists-p exported-file)
  181. (> (funcall age file) (funcall age exported-file)))
  182. (org-babel-tangle-file file exported-file "emacs-lisp"))
  183. (message "%s %s"
  184. (if compile
  185. (progn (byte-compile-file exported-file 'load)
  186. "Compiled and loaded")
  187. (progn (load-file exported-file) "Loaded"))
  188. exported-file)))
  189. (defcustom org-babel-load-languages '((emacs-lisp . t))
  190. "Languages which can be evaluated in Org-mode buffers.
  191. This list can be used to load support for any of the languages
  192. below, note that each language will depend on a different set of
  193. system executables and/or Emacs modes. When a language is
  194. \"loaded\", then code blocks in that language can be evaluated
  195. with `org-babel-execute-src-block' bound by default to C-c
  196. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  197. be set to remove code block evaluation from the C-c C-c
  198. keybinding. By default only Emacs Lisp (which has no
  199. requirements) is loaded."
  200. :group 'org-babel
  201. :set 'org-babel-do-load-languages
  202. :version "24.1"
  203. :type '(alist :tag "Babel Languages"
  204. :key-type
  205. (choice
  206. (const :tag "Awk" awk)
  207. (const :tag "C" C)
  208. (const :tag "R" R)
  209. (const :tag "Asymptote" asymptote)
  210. (const :tag "Calc" calc)
  211. (const :tag "Clojure" clojure)
  212. (const :tag "CSS" css)
  213. (const :tag "Ditaa" ditaa)
  214. (const :tag "Dot" dot)
  215. (const :tag "Emacs Lisp" emacs-lisp)
  216. (const :tag "Fortran" fortran)
  217. (const :tag "Gnuplot" gnuplot)
  218. (const :tag "Haskell" haskell)
  219. (const :tag "IO" io)
  220. (const :tag "Java" java)
  221. (const :tag "Javascript" js)
  222. (const :tag "LaTeX" latex)
  223. (const :tag "Ledger" ledger)
  224. (const :tag "Lilypond" lilypond)
  225. (const :tag "Lisp" lisp)
  226. (const :tag "Makefile" makefile)
  227. (const :tag "Maxima" maxima)
  228. (const :tag "Matlab" matlab)
  229. (const :tag "Mscgen" mscgen)
  230. (const :tag "Ocaml" ocaml)
  231. (const :tag "Octave" octave)
  232. (const :tag "Org" org)
  233. (const :tag "Perl" perl)
  234. (const :tag "Pico Lisp" picolisp)
  235. (const :tag "PlantUML" plantuml)
  236. (const :tag "Python" python)
  237. (const :tag "Ruby" ruby)
  238. (const :tag "Sass" sass)
  239. (const :tag "Scala" scala)
  240. (const :tag "Scheme" scheme)
  241. (const :tag "Screen" screen)
  242. (const :tag "Shell Script" sh)
  243. (const :tag "Shen" shen)
  244. (const :tag "Sql" sql)
  245. (const :tag "Sqlite" sqlite))
  246. :value-type (boolean :tag "Activate" :value t)))
  247. ;;;; Customization variables
  248. (defcustom org-clone-delete-id nil
  249. "Remove ID property of clones of a subtree.
  250. When non-nil, clones of a subtree don't inherit the ID property.
  251. Otherwise they inherit the ID property with a new unique
  252. identifier."
  253. :type 'boolean
  254. :version "24.1"
  255. :group 'org-id)
  256. ;;; Version
  257. (org-check-version)
  258. ;;;###autoload
  259. (defun org-version (&optional here full message)
  260. "Show the org-mode version in the echo area.
  261. With prefix argument HERE, insert it at point.
  262. When FULL is non-nil, use a verbose version string.
  263. When MESSAGE is non-nil, display a message with the version."
  264. (interactive "P")
  265. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  266. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  267. (load-suffixes (list ".el"))
  268. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  269. (org-trash (or
  270. (and (fboundp 'org-release) (fboundp 'org-git-version))
  271. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  272. (load-suffixes save-load-suffixes)
  273. (org-version (org-release))
  274. (git-version (org-git-version))
  275. (version (format "Org-mode version %s (%s @ %s)"
  276. org-version
  277. git-version
  278. (if org-install-dir
  279. (if (string= org-dir org-install-dir)
  280. org-install-dir
  281. (concat "mixed installation! " org-install-dir " and " org-dir))
  282. "org-loaddefs.el can not be found!")))
  283. (_version (if full version org-version)))
  284. (if (org-called-interactively-p 'interactive)
  285. (if here
  286. (insert version)
  287. (message version))
  288. (if message (message _version))
  289. _version)))
  290. (defconst org-version (org-version))
  291. ;;; Compatibility constants
  292. ;;; The custom variables
  293. (defgroup org nil
  294. "Outline-based notes management and organizer."
  295. :tag "Org"
  296. :group 'outlines
  297. :group 'calendar)
  298. (defcustom org-mode-hook nil
  299. "Mode hook for Org-mode, run after the mode was turned on."
  300. :group 'org
  301. :type 'hook)
  302. (defcustom org-load-hook nil
  303. "Hook that is run after org.el has been loaded."
  304. :group 'org
  305. :type 'hook)
  306. (defcustom org-log-buffer-setup-hook nil
  307. "Hook that is run after an Org log buffer is created."
  308. :group 'org
  309. :version "24.1"
  310. :type 'hook)
  311. (defvar org-modules) ; defined below
  312. (defvar org-modules-loaded nil
  313. "Have the modules been loaded already?")
  314. (defun org-load-modules-maybe (&optional force)
  315. "Load all extensions listed in `org-modules'."
  316. (when (or force (not org-modules-loaded))
  317. (mapc (lambda (ext)
  318. (condition-case nil (require ext)
  319. (error (message "Problems while trying to load feature `%s'" ext))))
  320. org-modules)
  321. (setq org-modules-loaded t)))
  322. (defun org-set-modules (var value)
  323. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  324. (set var value)
  325. (when (featurep 'org)
  326. (org-load-modules-maybe 'force)))
  327. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  328. "Modules that should always be loaded together with org.el.
  329. If a description starts with <C>, the file is not part of Emacs
  330. and loading it will require that you have downloaded and properly
  331. installed the Org mode distribution.
  332. You can also use this system to load external packages (i.e. neither Org
  333. core modules, nor modules from the CONTRIB directory). Just add symbols
  334. to the end of the list. If the package is called org-xyz.el, then you need
  335. to add the symbol `xyz', and the package must have a call to:
  336. \(provide 'org-xyz)
  337. For export specific modules, see also `org-export-backends'."
  338. :group 'org
  339. :set 'org-set-modules
  340. :version "24.4"
  341. :package-version '(Org . "8.0")
  342. :type
  343. '(set :greedy t
  344. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  345. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  346. (const :tag " crypt: Encryption of subtrees" org-crypt)
  347. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  348. (const :tag " docview: Links to doc-view buffers" org-docview)
  349. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  350. (const :tag " habit: Track your consistency with habits" org-habit)
  351. (const :tag " id: Global IDs for identifying entries" org-id)
  352. (const :tag " info: Links to Info nodes" org-info)
  353. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  354. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  355. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  356. (const :tag " mouse: Additional mouse support" org-mouse)
  357. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  358. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  359. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  360. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  361. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  362. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  363. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  364. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  365. (const :tag "C collector: Collect properties into tables" org-collector)
  366. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  367. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  368. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  369. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  370. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  371. (const :tag "C eval: Include command output as text" org-eval)
  372. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  373. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  374. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  375. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  376. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  377. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  378. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  379. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  380. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  381. (const :tag "C mac-message: Links to messages in Apple Mail" org-mac-message)
  382. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  383. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  384. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  385. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  386. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  387. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  388. (const :tag "C registry: A registry for Org-mode links" org-registry)
  389. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  390. (const :tag "C secretary: Team management with org-mode" org-secretary)
  391. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  392. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  393. (const :tag "C track: Keep up with Org-mode development" org-track)
  394. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  395. (const :tag "C vm: Links to VM folders/messages" org-vm)
  396. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  397. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  398. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  399. (defvar org-export--registered-backends) ; From ox.el.
  400. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  401. (declare-function org-export-backend-name "ox" (backend))
  402. (defcustom org-export-backends '(ascii html icalendar latex)
  403. "List of export back-ends that should be always available.
  404. If a description starts with <C>, the file is not part of Emacs
  405. and loading it will require that you have downloaded and properly
  406. installed the Org mode distribution.
  407. Unlike to `org-modules', libraries in this list will not be
  408. loaded along with Org, but only once the export framework is
  409. needed.
  410. This variable needs to be set before org.el is loaded. If you
  411. need to make a change while Emacs is running, use the customize
  412. interface or run the following code, where VAL stands for the new
  413. value of the variable, after updating it:
  414. \(progn
  415. \(setq org-export--registered-backends
  416. \(org-remove-if-not
  417. \(lambda (backend)
  418. \(let ((name (org-export-backend-name backend)))
  419. \(or (memq name val)
  420. \(catch 'parentp
  421. \(dolist (b val)
  422. \(and (org-export-derived-backend-p b name)
  423. \(throw 'parentp t)))))))
  424. org-export--registered-backends))
  425. \(let ((new-list (mapcar 'org-export-backend-name
  426. org-export--registered-backends)))
  427. \(dolist (backend val)
  428. \(cond
  429. \((not (load (format \"ox-%s\" backend) t t))
  430. \(message \"Problems while trying to load export back-end `%s'\"
  431. backend))
  432. \((not (memq backend new-list)) (push backend new-list))))
  433. \(set-default 'org-export-backends new-list)))
  434. Adding a back-end to this list will also pull the back-end it
  435. depends on, if any."
  436. :group 'org
  437. :group 'org-export
  438. :version "24.4"
  439. :package-version '(Org . "8.0")
  440. :initialize 'custom-initialize-set
  441. :set (lambda (var val)
  442. (if (not (featurep 'ox)) (set-default var val)
  443. ;; Any back-end not required anymore (not present in VAL and not
  444. ;; a parent of any back-end in the new value) is removed from the
  445. ;; list of registered back-ends.
  446. (setq org-export--registered-backends
  447. (org-remove-if-not
  448. (lambda (backend)
  449. (let ((name (org-export-backend-name backend)))
  450. (or (memq name val)
  451. (catch 'parentp
  452. (dolist (b val)
  453. (and (org-export-derived-backend-p b name)
  454. (throw 'parentp t)))))))
  455. org-export--registered-backends))
  456. ;; Now build NEW-LIST of both new back-ends and required
  457. ;; parents.
  458. (let ((new-list (mapcar 'org-export-backend-name
  459. org-export--registered-backends)))
  460. (dolist (backend val)
  461. (cond
  462. ((not (load (format "ox-%s" backend) t t))
  463. (message "Problems while trying to load export back-end `%s'"
  464. backend))
  465. ((not (memq backend new-list)) (push backend new-list))))
  466. ;; Set VAR to that list with fixed dependencies.
  467. (set-default var new-list))))
  468. :type '(set :greedy t
  469. (const :tag " ascii Export buffer to ASCII format" ascii)
  470. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  471. (const :tag " html Export buffer to HTML format" html)
  472. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  473. (const :tag " latex Export buffer to LaTeX format" latex)
  474. (const :tag " man Export buffer to MAN format" man)
  475. (const :tag " md Export buffer to Markdown format" md)
  476. (const :tag " odt Export buffer to ODT format" odt)
  477. (const :tag " org Export buffer to Org format" org)
  478. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  479. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  480. (const :tag "C deck Export buffer to deck.js presentations" deck)
  481. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  482. (const :tag "C groff Export buffer to Groff format" groff)
  483. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  484. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  485. (const :tag "C s5 Export buffer to s5 presentations" s5)
  486. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  487. (eval-after-load 'ox
  488. '(mapc
  489. (lambda (backend)
  490. (condition-case nil (require (intern (format "ox-%s" backend)))
  491. (error (message "Problems while trying to load export back-end `%s'"
  492. backend))))
  493. org-export-backends))
  494. (defcustom org-support-shift-select nil
  495. "Non-nil means make shift-cursor commands select text when possible.
  496. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  497. start selecting a region, or enlarge regions started in this way.
  498. In Org-mode, in special contexts, these same keys are used for
  499. other purposes, important enough to compete with shift selection.
  500. Org tries to balance these needs by supporting `shift-select-mode'
  501. outside these special contexts, under control of this variable.
  502. The default of this variable is nil, to avoid confusing behavior. Shifted
  503. cursor keys will then execute Org commands in the following contexts:
  504. - on a headline, changing TODO state (left/right) and priority (up/down)
  505. - on a time stamp, changing the time
  506. - in a plain list item, changing the bullet type
  507. - in a property definition line, switching between allowed values
  508. - in the BEGIN line of a clock table (changing the time block).
  509. Outside these contexts, the commands will throw an error.
  510. When this variable is t and the cursor is not in a special
  511. context, Org-mode will support shift-selection for making and
  512. enlarging regions. To make this more effective, the bullet
  513. cycling will no longer happen anywhere in an item line, but only
  514. if the cursor is exactly on the bullet.
  515. If you set this variable to the symbol `always', then the keys
  516. will not be special in headlines, property lines, and item lines,
  517. to make shift selection work there as well. If this is what you
  518. want, you can use the following alternative commands: `C-c C-t'
  519. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  520. can be used to switch TODO sets, `C-c -' to cycle item bullet
  521. types, and properties can be edited by hand or in column view.
  522. However, when the cursor is on a timestamp, shift-cursor commands
  523. will still edit the time stamp - this is just too good to give up.
  524. XEmacs user should have this variable set to nil, because
  525. `shift-select-mode' is in Emacs 23 or later only."
  526. :group 'org
  527. :type '(choice
  528. (const :tag "Never" nil)
  529. (const :tag "When outside special context" t)
  530. (const :tag "Everywhere except timestamps" always)))
  531. (defcustom org-loop-over-headlines-in-active-region nil
  532. "Shall some commands act upon headlines in the active region?
  533. When set to `t', some commands will be performed in all headlines
  534. within the active region.
  535. When set to `start-level', some commands will be performed in all
  536. headlines within the active region, provided that these headlines
  537. are of the same level than the first one.
  538. When set to a string, those commands will be performed on the
  539. matching headlines within the active region. Such string must be
  540. a tags/property/todo match as it is used in the agenda tags view.
  541. The list of commands is: `org-schedule', `org-deadline',
  542. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  543. `org-archive-to-archive-sibling'. The archiving commands skip
  544. already archived entries."
  545. :type '(choice (const :tag "Don't loop" nil)
  546. (const :tag "All headlines in active region" t)
  547. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  548. (string :tag "Tags/Property/Todo matcher"))
  549. :version "24.1"
  550. :group 'org-todo
  551. :group 'org-archive)
  552. (defgroup org-startup nil
  553. "Options concerning startup of Org-mode."
  554. :tag "Org Startup"
  555. :group 'org)
  556. (defcustom org-startup-folded t
  557. "Non-nil means entering Org-mode will switch to OVERVIEW.
  558. This can also be configured on a per-file basis by adding one of
  559. the following lines anywhere in the buffer:
  560. #+STARTUP: fold (or `overview', this is equivalent)
  561. #+STARTUP: nofold (or `showall', this is equivalent)
  562. #+STARTUP: content
  563. #+STARTUP: showeverything
  564. By default, this option is ignored when Org opens agenda files
  565. for the first time. If you want the agenda to honor the startup
  566. option, set `org-agenda-inhibit-startup' to nil."
  567. :group 'org-startup
  568. :type '(choice
  569. (const :tag "nofold: show all" nil)
  570. (const :tag "fold: overview" t)
  571. (const :tag "content: all headlines" content)
  572. (const :tag "show everything, even drawers" showeverything)))
  573. (defcustom org-startup-truncated t
  574. "Non-nil means entering Org-mode will set `truncate-lines'.
  575. This is useful since some lines containing links can be very long and
  576. uninteresting. Also tables look terrible when wrapped."
  577. :group 'org-startup
  578. :type 'boolean)
  579. (defcustom org-startup-indented nil
  580. "Non-nil means turn on `org-indent-mode' on startup.
  581. This can also be configured on a per-file basis by adding one of
  582. the following lines anywhere in the buffer:
  583. #+STARTUP: indent
  584. #+STARTUP: noindent"
  585. :group 'org-structure
  586. :type '(choice
  587. (const :tag "Not" nil)
  588. (const :tag "Globally (slow on startup in large files)" t)))
  589. (defcustom org-use-sub-superscripts t
  590. "Non-nil means interpret \"_\" and \"^\" for display.
  591. When this option is turned on, you can use TeX-like syntax for sub- and
  592. superscripts. Several characters after \"_\" or \"^\" will be
  593. considered as a single item - so grouping with {} is normally not
  594. needed. For example, the following things will be parsed as single
  595. sub- or superscripts.
  596. 10^24 or 10^tau several digits will be considered 1 item.
  597. 10^-12 or 10^-tau a leading sign with digits or a word
  598. x^2-y^3 will be read as x^2 - y^3, because items are
  599. terminated by almost any nonword/nondigit char.
  600. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  601. Still, ambiguity is possible - so when in doubt use {} to enclose
  602. the sub/superscript. If you set this variable to the symbol
  603. `{}', the braces are *required* in order to trigger
  604. interpretations as sub/superscript. This can be helpful in
  605. documents that need \"_\" frequently in plain text."
  606. :group 'org-startup
  607. :version "24.1"
  608. :type '(choice
  609. (const :tag "Always interpret" t)
  610. (const :tag "Only with braces" {})
  611. (const :tag "Never interpret" nil)))
  612. (defcustom org-startup-with-beamer-mode nil
  613. "Non-nil means turn on `org-beamer-mode' on startup.
  614. This can also be configured on a per-file basis by adding one of
  615. the following lines anywhere in the buffer:
  616. #+STARTUP: beamer"
  617. :group 'org-startup
  618. :version "24.1"
  619. :type 'boolean)
  620. (defcustom org-startup-align-all-tables nil
  621. "Non-nil means align all tables when visiting a file.
  622. This is useful when the column width in tables is forced with <N> cookies
  623. in table fields. Such tables will look correct only after the first re-align.
  624. This can also be configured on a per-file basis by adding one of
  625. the following lines anywhere in the buffer:
  626. #+STARTUP: align
  627. #+STARTUP: noalign"
  628. :group 'org-startup
  629. :type 'boolean)
  630. (defcustom org-startup-with-inline-images nil
  631. "Non-nil means show inline images when loading a new Org file.
  632. This can also be configured on a per-file basis by adding one of
  633. the following lines anywhere in the buffer:
  634. #+STARTUP: inlineimages
  635. #+STARTUP: noinlineimages"
  636. :group 'org-startup
  637. :version "24.1"
  638. :type 'boolean)
  639. (defcustom org-startup-with-latex-preview nil
  640. "Non-nil means preview LaTeX fragments when loading a new Org file.
  641. This can also be configured on a per-file basis by adding one of
  642. the followinglines anywhere in the buffer:
  643. #+STARTUP: latexpreview
  644. #+STARTUP: nolatexpreview"
  645. :group 'org-startup
  646. :version "24.4"
  647. :package-version '(Org . "8.0")
  648. :type 'boolean)
  649. (defcustom org-insert-mode-line-in-empty-file nil
  650. "Non-nil means insert the first line setting Org-mode in empty files.
  651. When the function `org-mode' is called interactively in an empty file, this
  652. normally means that the file name does not automatically trigger Org-mode.
  653. To ensure that the file will always be in Org-mode in the future, a
  654. line enforcing Org-mode will be inserted into the buffer, if this option
  655. has been set."
  656. :group 'org-startup
  657. :type 'boolean)
  658. (defcustom org-replace-disputed-keys nil
  659. "Non-nil means use alternative key bindings for some keys.
  660. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  661. These keys are also used by other packages like shift-selection-mode'
  662. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  663. If you want to use Org-mode together with one of these other modes,
  664. or more generally if you would like to move some Org-mode commands to
  665. other keys, set this variable and configure the keys with the variable
  666. `org-disputed-keys'.
  667. This option is only relevant at load-time of Org-mode, and must be set
  668. *before* org.el is loaded. Changing it requires a restart of Emacs to
  669. become effective."
  670. :group 'org-startup
  671. :type 'boolean)
  672. (defcustom org-use-extra-keys nil
  673. "Non-nil means use extra key sequence definitions for certain commands.
  674. This happens automatically if you run XEmacs or if `window-system'
  675. is nil. This variable lets you do the same manually. You must
  676. set it before loading org.
  677. Example: on Carbon Emacs 22 running graphically, with an external
  678. keyboard on a Powerbook, the default way of setting M-left might
  679. not work for either Alt or ESC. Setting this variable will make
  680. it work for ESC."
  681. :group 'org-startup
  682. :type 'boolean)
  683. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  684. (defcustom org-disputed-keys
  685. '(([(shift up)] . [(meta p)])
  686. ([(shift down)] . [(meta n)])
  687. ([(shift left)] . [(meta -)])
  688. ([(shift right)] . [(meta +)])
  689. ([(control shift right)] . [(meta shift +)])
  690. ([(control shift left)] . [(meta shift -)]))
  691. "Keys for which Org-mode and other modes compete.
  692. This is an alist, cars are the default keys, second element specifies
  693. the alternative to use when `org-replace-disputed-keys' is t.
  694. Keys can be specified in any syntax supported by `define-key'.
  695. The value of this option takes effect only at Org-mode's startup,
  696. therefore you'll have to restart Emacs to apply it after changing."
  697. :group 'org-startup
  698. :type 'alist)
  699. (defun org-key (key)
  700. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  701. Or return the original if not disputed.
  702. Also apply the translations defined in `org-xemacs-key-equivalents'."
  703. (when org-replace-disputed-keys
  704. (let* ((nkey (key-description key))
  705. (x (org-find-if (lambda (x)
  706. (equal (key-description (car x)) nkey))
  707. org-disputed-keys)))
  708. (setq key (if x (cdr x) key))))
  709. (when (featurep 'xemacs)
  710. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  711. key)
  712. (defun org-find-if (predicate seq)
  713. (catch 'exit
  714. (while seq
  715. (if (funcall predicate (car seq))
  716. (throw 'exit (car seq))
  717. (pop seq)))))
  718. (defun org-defkey (keymap key def)
  719. "Define a key, possibly translated, as returned by `org-key'."
  720. (define-key keymap (org-key key) def))
  721. (defcustom org-ellipsis nil
  722. "The ellipsis to use in the Org-mode outline.
  723. When nil, just use the standard three dots. When a string, use that instead,
  724. When a face, use the standard 3 dots, but with the specified face.
  725. The change affects only Org-mode (which will then use its own display table).
  726. Changing this requires executing `M-x org-mode' in a buffer to become
  727. effective."
  728. :group 'org-startup
  729. :type '(choice (const :tag "Default" nil)
  730. (face :tag "Face" :value org-warning)
  731. (string :tag "String" :value "...#")))
  732. (defvar org-display-table nil
  733. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  734. (defgroup org-keywords nil
  735. "Keywords in Org-mode."
  736. :tag "Org Keywords"
  737. :group 'org)
  738. (defcustom org-deadline-string "DEADLINE:"
  739. "String to mark deadline entries.
  740. A deadline is this string, followed by a time stamp. Should be a word,
  741. terminated by a colon. You can insert a schedule keyword and
  742. a timestamp with \\[org-deadline].
  743. Changes become only effective after restarting Emacs."
  744. :group 'org-keywords
  745. :type 'string)
  746. (defcustom org-scheduled-string "SCHEDULED:"
  747. "String to mark scheduled TODO entries.
  748. A schedule is this string, followed by a time stamp. Should be a word,
  749. terminated by a colon. You can insert a schedule keyword and
  750. a timestamp with \\[org-schedule].
  751. Changes become only effective after restarting Emacs."
  752. :group 'org-keywords
  753. :type 'string)
  754. (defcustom org-closed-string "CLOSED:"
  755. "String used as the prefix for timestamps logging closing a TODO entry."
  756. :group 'org-keywords
  757. :type 'string)
  758. (defcustom org-clock-string "CLOCK:"
  759. "String used as prefix for timestamps clocking work hours on an item."
  760. :group 'org-keywords
  761. :type 'string)
  762. (defcustom org-closed-keep-when-no-todo nil
  763. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  764. :group 'org-todo
  765. :group 'org-keywords
  766. :version "24.4"
  767. :package-version '(Org . "8.0")
  768. :type 'boolean)
  769. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  770. org-scheduled-string "\\|"
  771. org-deadline-string "\\|"
  772. org-closed-string "\\|"
  773. org-clock-string "\\)")
  774. "Matches a line with planning or clock info.")
  775. (defcustom org-comment-string "COMMENT"
  776. "Entries starting with this keyword will never be exported.
  777. An entry can be toggled between COMMENT and normal with
  778. \\[org-toggle-comment].
  779. Changes become only effective after restarting Emacs."
  780. :group 'org-keywords
  781. :type 'string)
  782. (defcustom org-quote-string "QUOTE"
  783. "Entries starting with this keyword will be exported in fixed-width font.
  784. Quoting applies only to the text in the entry following the headline, and does
  785. not extend beyond the next headline, even if that is lower level.
  786. An entry can be toggled between QUOTE and normal with
  787. \\[org-toggle-fixed-width-section]."
  788. :group 'org-keywords
  789. :type 'string)
  790. (defconst org-repeat-re
  791. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  792. "Regular expression for specifying repeated events.
  793. After a match, group 1 contains the repeat expression.")
  794. (defgroup org-structure nil
  795. "Options concerning the general structure of Org-mode files."
  796. :tag "Org Structure"
  797. :group 'org)
  798. (defgroup org-reveal-location nil
  799. "Options about how to make context of a location visible."
  800. :tag "Org Reveal Location"
  801. :group 'org-structure)
  802. (defconst org-context-choice
  803. '(choice
  804. (const :tag "Always" t)
  805. (const :tag "Never" nil)
  806. (repeat :greedy t :tag "Individual contexts"
  807. (cons
  808. (choice :tag "Context"
  809. (const agenda)
  810. (const org-goto)
  811. (const occur-tree)
  812. (const tags-tree)
  813. (const link-search)
  814. (const mark-goto)
  815. (const bookmark-jump)
  816. (const isearch)
  817. (const default))
  818. (boolean))))
  819. "Contexts for the reveal options.")
  820. (defcustom org-show-hierarchy-above '((default . t))
  821. "Non-nil means show full hierarchy when revealing a location.
  822. Org-mode often shows locations in an org-mode file which might have
  823. been invisible before. When this is set, the hierarchy of headings
  824. above the exposed location is shown.
  825. Turning this off for example for sparse trees makes them very compact.
  826. Instead of t, this can also be an alist specifying this option for different
  827. contexts. Valid contexts are
  828. agenda when exposing an entry from the agenda
  829. org-goto when using the command `org-goto' on key C-c C-j
  830. occur-tree when using the command `org-occur' on key C-c /
  831. tags-tree when constructing a sparse tree based on tags matches
  832. link-search when exposing search matches associated with a link
  833. mark-goto when exposing the jump goal of a mark
  834. bookmark-jump when exposing a bookmark location
  835. isearch when exiting from an incremental search
  836. default default for all contexts not set explicitly"
  837. :group 'org-reveal-location
  838. :type org-context-choice)
  839. (defcustom org-show-following-heading '((default . nil))
  840. "Non-nil means show following heading when revealing a location.
  841. Org-mode often shows locations in an org-mode file which might have
  842. been invisible before. When this is set, the heading following the
  843. match is shown.
  844. Turning this off for example for sparse trees makes them very compact,
  845. but makes it harder to edit the location of the match. In such a case,
  846. use the command \\[org-reveal] to show more context.
  847. Instead of t, this can also be an alist specifying this option for different
  848. contexts. See `org-show-hierarchy-above' for valid contexts."
  849. :group 'org-reveal-location
  850. :type org-context-choice)
  851. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  852. "Non-nil means show all sibling heading when revealing a location.
  853. Org-mode often shows locations in an org-mode file which might have
  854. been invisible before. When this is set, the sibling of the current entry
  855. heading are all made visible. If `org-show-hierarchy-above' is t,
  856. the same happens on each level of the hierarchy above the current entry.
  857. By default this is on for the isearch context, off for all other contexts.
  858. Turning this off for example for sparse trees makes them very compact,
  859. but makes it harder to edit the location of the match. In such a case,
  860. use the command \\[org-reveal] to show more context.
  861. Instead of t, this can also be an alist specifying this option for different
  862. contexts. See `org-show-hierarchy-above' for valid contexts."
  863. :group 'org-reveal-location
  864. :type org-context-choice
  865. :version "24.4"
  866. :package-version '(Org . "8.0"))
  867. (defcustom org-show-entry-below '((default . nil))
  868. "Non-nil means show the entry below a headline when revealing a location.
  869. Org-mode often shows locations in an org-mode file which might have
  870. been invisible before. When this is set, the text below the headline that is
  871. exposed is also shown.
  872. By default this is off for all contexts.
  873. Instead of t, this can also be an alist specifying this option for different
  874. contexts. See `org-show-hierarchy-above' for valid contexts."
  875. :group 'org-reveal-location
  876. :type org-context-choice)
  877. (defcustom org-indirect-buffer-display 'other-window
  878. "How should indirect tree buffers be displayed?
  879. This applies to indirect buffers created with the commands
  880. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  881. Valid values are:
  882. current-window Display in the current window
  883. other-window Just display in another window.
  884. dedicated-frame Create one new frame, and re-use it each time.
  885. new-frame Make a new frame each time. Note that in this case
  886. previously-made indirect buffers are kept, and you need to
  887. kill these buffers yourself."
  888. :group 'org-structure
  889. :group 'org-agenda-windows
  890. :type '(choice
  891. (const :tag "In current window" current-window)
  892. (const :tag "In current frame, other window" other-window)
  893. (const :tag "Each time a new frame" new-frame)
  894. (const :tag "One dedicated frame" dedicated-frame)))
  895. (defcustom org-use-speed-commands nil
  896. "Non-nil means activate single letter commands at beginning of a headline.
  897. This may also be a function to test for appropriate locations where speed
  898. commands should be active."
  899. :group 'org-structure
  900. :type '(choice
  901. (const :tag "Never" nil)
  902. (const :tag "At beginning of headline stars" t)
  903. (function)))
  904. (defcustom org-speed-commands-user nil
  905. "Alist of additional speed commands.
  906. This list will be checked before `org-speed-commands-default'
  907. when the variable `org-use-speed-commands' is non-nil
  908. and when the cursor is at the beginning of a headline.
  909. The car if each entry is a string with a single letter, which must
  910. be assigned to `self-insert-command' in the global map.
  911. The cdr is either a command to be called interactively, a function
  912. to be called, or a form to be evaluated.
  913. An entry that is just a list with a single string will be interpreted
  914. as a descriptive headline that will be added when listing the speed
  915. commands in the Help buffer using the `?' speed command."
  916. :group 'org-structure
  917. :type '(repeat :value ("k" . ignore)
  918. (choice :value ("k" . ignore)
  919. (list :tag "Descriptive Headline" (string :tag "Headline"))
  920. (cons :tag "Letter and Command"
  921. (string :tag "Command letter")
  922. (choice
  923. (function)
  924. (sexp))))))
  925. (defgroup org-cycle nil
  926. "Options concerning visibility cycling in Org-mode."
  927. :tag "Org Cycle"
  928. :group 'org-structure)
  929. (defcustom org-cycle-skip-children-state-if-no-children t
  930. "Non-nil means skip CHILDREN state in entries that don't have any."
  931. :group 'org-cycle
  932. :type 'boolean)
  933. (defcustom org-cycle-max-level nil
  934. "Maximum level which should still be subject to visibility cycling.
  935. Levels higher than this will, for cycling, be treated as text, not a headline.
  936. When `org-odd-levels-only' is set, a value of N in this variable actually
  937. means 2N-1 stars as the limiting headline.
  938. When nil, cycle all levels.
  939. Note that the limiting level of cycling is also influenced by
  940. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  941. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  942. than its value."
  943. :group 'org-cycle
  944. :type '(choice
  945. (const :tag "No limit" nil)
  946. (integer :tag "Maximum level")))
  947. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  948. "Names of drawers. Drawers are not opened by cycling on the headline above.
  949. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  950. this:
  951. :DRAWERNAME:
  952. .....
  953. :END:
  954. The drawer \"PROPERTIES\" is special for capturing properties through
  955. the property API.
  956. Drawers can be defined on the per-file basis with a line like:
  957. #+DRAWERS: HIDDEN STATE PROPERTIES"
  958. :group 'org-structure
  959. :group 'org-cycle
  960. :type '(repeat (string :tag "Drawer Name")))
  961. (defcustom org-hide-block-startup nil
  962. "Non-nil means entering Org-mode will fold all blocks.
  963. This can also be set in on a per-file basis with
  964. #+STARTUP: hideblocks
  965. #+STARTUP: showblocks"
  966. :group 'org-startup
  967. :group 'org-cycle
  968. :type 'boolean)
  969. (defcustom org-cycle-global-at-bob nil
  970. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  971. This makes it possible to do global cycling without having to use S-TAB or
  972. \\[universal-argument] TAB. For this special case to work, the first line
  973. of the buffer must not be a headline -- it may be empty or some other text.
  974. When used in this way, `org-cycle-hook' is disabled temporarily to make
  975. sure the cursor stays at the beginning of the buffer. When this option is
  976. nil, don't do anything special at the beginning of the buffer."
  977. :group 'org-cycle
  978. :type 'boolean)
  979. (defcustom org-cycle-level-after-item/entry-creation t
  980. "Non-nil means cycle entry level or item indentation in new empty entries.
  981. When the cursor is at the end of an empty headline, i.e., with only stars
  982. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  983. and then all possible ancestor states, before returning to the original state.
  984. This makes data entry extremely fast: M-RET to create a new headline,
  985. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  986. When the cursor is at the end of an empty plain list item, one TAB will
  987. make it a subitem, two or more tabs will back up to make this an item
  988. higher up in the item hierarchy."
  989. :group 'org-cycle
  990. :type 'boolean)
  991. (defcustom org-cycle-emulate-tab t
  992. "Where should `org-cycle' emulate TAB.
  993. nil Never
  994. white Only in completely white lines
  995. whitestart Only at the beginning of lines, before the first non-white char
  996. t Everywhere except in headlines
  997. exc-hl-bol Everywhere except at the start of a headline
  998. If TAB is used in a place where it does not emulate TAB, the current subtree
  999. visibility is cycled."
  1000. :group 'org-cycle
  1001. :type '(choice (const :tag "Never" nil)
  1002. (const :tag "Only in completely white lines" white)
  1003. (const :tag "Before first char in a line" whitestart)
  1004. (const :tag "Everywhere except in headlines" t)
  1005. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1006. (defcustom org-cycle-separator-lines 2
  1007. "Number of empty lines needed to keep an empty line between collapsed trees.
  1008. If you leave an empty line between the end of a subtree and the following
  1009. headline, this empty line is hidden when the subtree is folded.
  1010. Org-mode will leave (exactly) one empty line visible if the number of
  1011. empty lines is equal or larger to the number given in this variable.
  1012. So the default 2 means at least 2 empty lines after the end of a subtree
  1013. are needed to produce free space between a collapsed subtree and the
  1014. following headline.
  1015. If the number is negative, and the number of empty lines is at least -N,
  1016. all empty lines are shown.
  1017. Special case: when 0, never leave empty lines in collapsed view."
  1018. :group 'org-cycle
  1019. :type 'integer)
  1020. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1021. (defcustom org-pre-cycle-hook nil
  1022. "Hook that is run before visibility cycling is happening.
  1023. The function(s) in this hook must accept a single argument which indicates
  1024. the new state that will be set right after running this hook. The
  1025. argument is a symbol. Before a global state change, it can have the values
  1026. `overview', `content', or `all'. Before a local state change, it can have
  1027. the values `folded', `children', or `subtree'."
  1028. :group 'org-cycle
  1029. :type 'hook)
  1030. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1031. org-cycle-hide-drawers
  1032. org-cycle-hide-inline-tasks
  1033. org-cycle-show-empty-lines
  1034. org-optimize-window-after-visibility-change)
  1035. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1036. The function(s) in this hook must accept a single argument which indicates
  1037. the new state that was set by the most recent `org-cycle' command. The
  1038. argument is a symbol. After a global state change, it can have the values
  1039. `overview', `contents', or `all'. After a local state change, it can have
  1040. the values `folded', `children', or `subtree'."
  1041. :group 'org-cycle
  1042. :type 'hook)
  1043. (defgroup org-edit-structure nil
  1044. "Options concerning structure editing in Org-mode."
  1045. :tag "Org Edit Structure"
  1046. :group 'org-structure)
  1047. (defcustom org-odd-levels-only nil
  1048. "Non-nil means skip even levels and only use odd levels for the outline.
  1049. This has the effect that two stars are being added/taken away in
  1050. promotion/demotion commands. It also influences how levels are
  1051. handled by the exporters.
  1052. Changing it requires restart of `font-lock-mode' to become effective
  1053. for fontification also in regions already fontified.
  1054. You may also set this on a per-file basis by adding one of the following
  1055. lines to the buffer:
  1056. #+STARTUP: odd
  1057. #+STARTUP: oddeven"
  1058. :group 'org-edit-structure
  1059. :group 'org-appearance
  1060. :type 'boolean)
  1061. (defcustom org-adapt-indentation t
  1062. "Non-nil means adapt indentation to outline node level.
  1063. When this variable is set, Org assumes that you write outlines by
  1064. indenting text in each node to align with the headline (after the stars).
  1065. The following issues are influenced by this variable:
  1066. - When this is set and the *entire* text in an entry is indented, the
  1067. indentation is increased by one space in a demotion command, and
  1068. decreased by one in a promotion command. If any line in the entry
  1069. body starts with text at column 0, indentation is not changed at all.
  1070. - Property drawers and planning information is inserted indented when
  1071. this variable s set. When nil, they will not be indented.
  1072. - TAB indents a line relative to context. The lines below a headline
  1073. will be indented when this variable is set.
  1074. Note that this is all about true indentation, by adding and removing
  1075. space characters. See also `org-indent.el' which does level-dependent
  1076. indentation in a virtual way, i.e. at display time in Emacs."
  1077. :group 'org-edit-structure
  1078. :type 'boolean)
  1079. (defcustom org-special-ctrl-a/e nil
  1080. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1081. When t, `C-a' will bring back the cursor to the beginning of the
  1082. headline text, i.e. after the stars and after a possible TODO
  1083. keyword. In an item, this will be the position after bullet and
  1084. check-box, if any. When the cursor is already at that position,
  1085. another `C-a' will bring it to the beginning of the line.
  1086. `C-e' will jump to the end of the headline, ignoring the presence
  1087. of tags in the headline. A second `C-e' will then jump to the
  1088. true end of the line, after any tags. This also means that, when
  1089. this variable is non-nil, `C-e' also will never jump beyond the
  1090. end of the heading of a folded section, i.e. not after the
  1091. ellipses.
  1092. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1093. normally, going to the true line boundary first. Only a directly
  1094. following, identical keypress will bring the cursor to the
  1095. special positions.
  1096. This may also be a cons cell where the behavior for `C-a' and
  1097. `C-e' is set separately."
  1098. :group 'org-edit-structure
  1099. :type '(choice
  1100. (const :tag "off" nil)
  1101. (const :tag "on: after stars/bullet and before tags first" t)
  1102. (const :tag "reversed: true line boundary first" reversed)
  1103. (cons :tag "Set C-a and C-e separately"
  1104. (choice :tag "Special C-a"
  1105. (const :tag "off" nil)
  1106. (const :tag "on: after stars/bullet first" t)
  1107. (const :tag "reversed: before stars/bullet first" reversed))
  1108. (choice :tag "Special C-e"
  1109. (const :tag "off" nil)
  1110. (const :tag "on: before tags first" t)
  1111. (const :tag "reversed: after tags first" reversed)))))
  1112. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1113. (defcustom org-special-ctrl-k nil
  1114. "Non-nil means `C-k' will behave specially in headlines.
  1115. When nil, `C-k' will call the default `kill-line' command.
  1116. When t, the following will happen while the cursor is in the headline:
  1117. - When the cursor is at the beginning of a headline, kill the entire
  1118. line and possible the folded subtree below the line.
  1119. - When in the middle of the headline text, kill the headline up to the tags.
  1120. - When after the headline text, kill the tags."
  1121. :group 'org-edit-structure
  1122. :type 'boolean)
  1123. (defcustom org-ctrl-k-protect-subtree nil
  1124. "Non-nil means, do not delete a hidden subtree with C-k.
  1125. When set to the symbol `error', simply throw an error when C-k is
  1126. used to kill (part-of) a headline that has hidden text behind it.
  1127. Any other non-nil value will result in a query to the user, if it is
  1128. OK to kill that hidden subtree. When nil, kill without remorse."
  1129. :group 'org-edit-structure
  1130. :version "24.1"
  1131. :type '(choice
  1132. (const :tag "Do not protect hidden subtrees" nil)
  1133. (const :tag "Protect hidden subtrees with a security query" t)
  1134. (const :tag "Never kill a hidden subtree with C-k" error)))
  1135. (defcustom org-catch-invisible-edits nil
  1136. "Check if in invisible region before inserting or deleting a character.
  1137. Valid values are:
  1138. nil Do not check, so just do invisible edits.
  1139. error Throw an error and do nothing.
  1140. show Make point visible, and do the requested edit.
  1141. show-and-error Make point visible, then throw an error and abort the edit.
  1142. smart Make point visible, and do insertion/deletion if it is
  1143. adjacent to visible text and the change feels predictable.
  1144. Never delete a previously invisible character or add in the
  1145. middle or right after an invisible region. Basically, this
  1146. allows insertion and backward-delete right before ellipses.
  1147. FIXME: maybe in this case we should not even show?"
  1148. :group 'org-edit-structure
  1149. :version "24.1"
  1150. :type '(choice
  1151. (const :tag "Do not check" nil)
  1152. (const :tag "Throw error when trying to edit" error)
  1153. (const :tag "Unhide, but do not do the edit" show-and-error)
  1154. (const :tag "Show invisible part and do the edit" show)
  1155. (const :tag "Be smart and do the right thing" smart)))
  1156. (defcustom org-yank-folded-subtrees t
  1157. "Non-nil means when yanking subtrees, fold them.
  1158. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1159. it starts with a heading and all other headings in it are either children
  1160. or siblings, then fold all the subtrees. However, do this only if no
  1161. text after the yank would be swallowed into a folded tree by this action."
  1162. :group 'org-edit-structure
  1163. :type 'boolean)
  1164. (defcustom org-yank-adjusted-subtrees nil
  1165. "Non-nil means when yanking subtrees, adjust the level.
  1166. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1167. this function for details."
  1168. :group 'org-edit-structure
  1169. :type 'boolean)
  1170. (defcustom org-M-RET-may-split-line '((default . t))
  1171. "Non-nil means M-RET will split the line at the cursor position.
  1172. When nil, it will go to the end of the line before making a
  1173. new line.
  1174. You may also set this option in a different way for different
  1175. contexts. Valid contexts are:
  1176. headline when creating a new headline
  1177. item when creating a new item
  1178. table in a table field
  1179. default the value to be used for all contexts not explicitly
  1180. customized"
  1181. :group 'org-structure
  1182. :group 'org-table
  1183. :type '(choice
  1184. (const :tag "Always" t)
  1185. (const :tag "Never" nil)
  1186. (repeat :greedy t :tag "Individual contexts"
  1187. (cons
  1188. (choice :tag "Context"
  1189. (const headline)
  1190. (const item)
  1191. (const table)
  1192. (const default))
  1193. (boolean)))))
  1194. (defcustom org-insert-heading-respect-content nil
  1195. "Non-nil means insert new headings after the current subtree.
  1196. When nil, the new heading is created directly after the current line.
  1197. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1198. this variable on for the duration of the command."
  1199. :group 'org-structure
  1200. :type 'boolean)
  1201. (defcustom org-blank-before-new-entry '((heading . auto)
  1202. (plain-list-item . auto))
  1203. "Should `org-insert-heading' leave a blank line before new heading/item?
  1204. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1205. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1206. which case Org will look at the surrounding headings/items and try to
  1207. make an intelligent decision whether to insert a blank line or not.
  1208. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1209. the setting here is ignored and no empty line is inserted to avoid breaking
  1210. the list structure."
  1211. :group 'org-edit-structure
  1212. :type '(list
  1213. (cons (const heading)
  1214. (choice (const :tag "Never" nil)
  1215. (const :tag "Always" t)
  1216. (const :tag "Auto" auto)))
  1217. (cons (const plain-list-item)
  1218. (choice (const :tag "Never" nil)
  1219. (const :tag "Always" t)
  1220. (const :tag "Auto" auto)))))
  1221. (defcustom org-insert-heading-hook nil
  1222. "Hook being run after inserting a new heading."
  1223. :group 'org-edit-structure
  1224. :type 'hook)
  1225. (defcustom org-enable-fixed-width-editor t
  1226. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1227. This currently only means they are never auto-wrapped.
  1228. When nil, such lines will be treated like ordinary lines.
  1229. See also the QUOTE keyword."
  1230. :group 'org-edit-structure
  1231. :type 'boolean)
  1232. (defcustom org-goto-auto-isearch t
  1233. "Non-nil means typing characters in `org-goto' starts incremental search.
  1234. When nil, you can use these keybindings to navigate the buffer:
  1235. q Quit the org-goto interface
  1236. n Go to the next visible heading
  1237. p Go to the previous visible heading
  1238. f Go one heading forward on same level
  1239. b Go one heading backward on same level
  1240. u Go one heading up"
  1241. :group 'org-edit-structure
  1242. :type 'boolean)
  1243. (defgroup org-sparse-trees nil
  1244. "Options concerning sparse trees in Org-mode."
  1245. :tag "Org Sparse Trees"
  1246. :group 'org-structure)
  1247. (defcustom org-highlight-sparse-tree-matches t
  1248. "Non-nil means highlight all matches that define a sparse tree.
  1249. The highlights will automatically disappear the next time the buffer is
  1250. changed by an edit command."
  1251. :group 'org-sparse-trees
  1252. :type 'boolean)
  1253. (defcustom org-remove-highlights-with-change t
  1254. "Non-nil means any change to the buffer will remove temporary highlights.
  1255. Such highlights are created by `org-occur' and `org-clock-display'.
  1256. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1257. The highlights created by `org-preview-latex-fragment' always need
  1258. `C-c C-c' to be removed."
  1259. :group 'org-sparse-trees
  1260. :group 'org-time
  1261. :type 'boolean)
  1262. (defcustom org-occur-hook '(org-first-headline-recenter)
  1263. "Hook that is run after `org-occur' has constructed a sparse tree.
  1264. This can be used to recenter the window to show as much of the structure
  1265. as possible."
  1266. :group 'org-sparse-trees
  1267. :type 'hook)
  1268. (defgroup org-imenu-and-speedbar nil
  1269. "Options concerning imenu and speedbar in Org-mode."
  1270. :tag "Org Imenu and Speedbar"
  1271. :group 'org-structure)
  1272. (defcustom org-imenu-depth 2
  1273. "The maximum level for Imenu access to Org-mode headlines.
  1274. This also applied for speedbar access."
  1275. :group 'org-imenu-and-speedbar
  1276. :type 'integer)
  1277. (defgroup org-table nil
  1278. "Options concerning tables in Org-mode."
  1279. :tag "Org Table"
  1280. :group 'org)
  1281. (defcustom org-enable-table-editor 'optimized
  1282. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1283. When nil, such lines will be treated like ordinary lines.
  1284. When equal to the symbol `optimized', the table editor will be optimized to
  1285. do the following:
  1286. - Automatic overwrite mode in front of whitespace in table fields.
  1287. This makes the structure of the table stay in tact as long as the edited
  1288. field does not exceed the column width.
  1289. - Minimize the number of realigns. Normally, the table is aligned each time
  1290. TAB or RET are pressed to move to another field. With optimization this
  1291. happens only if changes to a field might have changed the column width.
  1292. Optimization requires replacing the functions `self-insert-command',
  1293. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1294. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1295. very good at guessing when a re-align will be necessary, but you can always
  1296. force one with \\[org-ctrl-c-ctrl-c].
  1297. If you would like to use the optimized version in Org-mode, but the
  1298. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1299. This variable can be used to turn on and off the table editor during a session,
  1300. but in order to toggle optimization, a restart is required.
  1301. See also the variable `org-table-auto-blank-field'."
  1302. :group 'org-table
  1303. :type '(choice
  1304. (const :tag "off" nil)
  1305. (const :tag "on" t)
  1306. (const :tag "on, optimized" optimized)))
  1307. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1308. (version<= emacs-version "24.1"))
  1309. "Non-nil means cluster self-insert commands for undo when possible.
  1310. If this is set, then, like in the Emacs command loop, 20 consecutive
  1311. characters will be undone together.
  1312. This is configurable, because there is some impact on typing performance."
  1313. :group 'org-table
  1314. :type 'boolean)
  1315. (defcustom org-table-tab-recognizes-table.el t
  1316. "Non-nil means TAB will automatically notice a table.el table.
  1317. When it sees such a table, it moves point into it and - if necessary -
  1318. calls `table-recognize-table'."
  1319. :group 'org-table-editing
  1320. :type 'boolean)
  1321. (defgroup org-link nil
  1322. "Options concerning links in Org-mode."
  1323. :tag "Org Link"
  1324. :group 'org)
  1325. (defvar org-link-abbrev-alist-local nil
  1326. "Buffer-local version of `org-link-abbrev-alist', which see.
  1327. The value of this is taken from the #+LINK lines.")
  1328. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1329. (defcustom org-link-abbrev-alist nil
  1330. "Alist of link abbreviations.
  1331. The car of each element is a string, to be replaced at the start of a link.
  1332. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1333. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1334. [[linkkey:tag][description]]
  1335. The 'linkkey' must be a word word, starting with a letter, followed
  1336. by letters, numbers, '-' or '_'.
  1337. If REPLACE is a string, the tag will simply be appended to create the link.
  1338. If the string contains \"%s\", the tag will be inserted there. If the string
  1339. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1340. at that point (see the function `url-hexify-string'). If the string contains
  1341. the specifier \"%(my-function)\", then the custom function `my-function' will
  1342. be invoked: this function takes the tag as its only argument and must return
  1343. a string.
  1344. REPLACE may also be a function that will be called with the tag as the
  1345. only argument to create the link, which should be returned as a string.
  1346. See the manual for examples."
  1347. :group 'org-link
  1348. :type '(repeat
  1349. (cons
  1350. (string :tag "Protocol")
  1351. (choice
  1352. (string :tag "Format")
  1353. (function)))))
  1354. (defcustom org-descriptive-links t
  1355. "Non-nil means Org will display descriptive links.
  1356. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1357. \"Org Website\", hiding the link itself and just displaying its
  1358. description. When set to `nil', Org will display the full links
  1359. literally.
  1360. You can interactively set the value of this variable by calling
  1361. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1362. :group 'org-link
  1363. :type 'boolean)
  1364. (defcustom org-link-file-path-type 'adaptive
  1365. "How the path name in file links should be stored.
  1366. Valid values are:
  1367. relative Relative to the current directory, i.e. the directory of the file
  1368. into which the link is being inserted.
  1369. absolute Absolute path, if possible with ~ for home directory.
  1370. noabbrev Absolute path, no abbreviation of home directory.
  1371. adaptive Use relative path for files in the current directory and sub-
  1372. directories of it. For other files, use an absolute path."
  1373. :group 'org-link
  1374. :type '(choice
  1375. (const relative)
  1376. (const absolute)
  1377. (const noabbrev)
  1378. (const adaptive)))
  1379. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1380. "Types of links that should be activated in Org-mode files.
  1381. This is a list of symbols, each leading to the activation of a certain link
  1382. type. In principle, it does not hurt to turn on most link types - there may
  1383. be a small gain when turning off unused link types. The types are:
  1384. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1385. angle Links in angular brackets that may contain whitespace like
  1386. <bbdb:Carsten Dominik>.
  1387. plain Plain links in normal text, no whitespace, like http://google.com.
  1388. radio Text that is matched by a radio target, see manual for details.
  1389. tag Tag settings in a headline (link to tag search).
  1390. date Time stamps (link to calendar).
  1391. footnote Footnote labels.
  1392. Changing this variable requires a restart of Emacs to become effective."
  1393. :group 'org-link
  1394. :type '(set :greedy t
  1395. (const :tag "Double bracket links" bracket)
  1396. (const :tag "Angular bracket links" angle)
  1397. (const :tag "Plain text links" plain)
  1398. (const :tag "Radio target matches" radio)
  1399. (const :tag "Tags" tag)
  1400. (const :tag "Timestamps" date)
  1401. (const :tag "Footnotes" footnote)))
  1402. (defcustom org-make-link-description-function nil
  1403. "Function to use for generating link descriptions from links.
  1404. When nil, the link location will be used. This function must take
  1405. two parameters: the first one is the link, the second one is the
  1406. description generated by `org-insert-link'. The function should
  1407. return the description to use."
  1408. :group 'org-link
  1409. :type '(choice (const nil) (function)))
  1410. (defgroup org-link-store nil
  1411. "Options concerning storing links in Org-mode."
  1412. :tag "Org Store Link"
  1413. :group 'org-link)
  1414. (defcustom org-url-hexify-p t
  1415. "When non-nil, hexify URL when creating a link."
  1416. :type 'boolean
  1417. :version "24.3"
  1418. :group 'org-link-store)
  1419. (defcustom org-email-link-description-format "Email %c: %.30s"
  1420. "Format of the description part of a link to an email or usenet message.
  1421. The following %-escapes will be replaced by corresponding information:
  1422. %F full \"From\" field
  1423. %f name, taken from \"From\" field, address if no name
  1424. %T full \"To\" field
  1425. %t first name in \"To\" field, address if no name
  1426. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1427. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1428. %s subject
  1429. %d date
  1430. %m message-id.
  1431. You may use normal field width specification between the % and the letter.
  1432. This is for example useful to limit the length of the subject.
  1433. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1434. :group 'org-link-store
  1435. :type 'string)
  1436. (defcustom org-from-is-user-regexp
  1437. (let (r1 r2)
  1438. (when (and user-mail-address (not (string= user-mail-address "")))
  1439. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1440. (when (and user-full-name (not (string= user-full-name "")))
  1441. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1442. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1443. "Regexp matched against the \"From:\" header of an email or usenet message.
  1444. It should match if the message is from the user him/herself."
  1445. :group 'org-link-store
  1446. :type 'regexp)
  1447. (defcustom org-context-in-file-links t
  1448. "Non-nil means file links from `org-store-link' contain context.
  1449. A search string will be added to the file name with :: as separator and
  1450. used to find the context when the link is activated by the command
  1451. `org-open-at-point'. When this option is t, the entire active region
  1452. will be placed in the search string of the file link. If set to a
  1453. positive integer, only the first n lines of context will be stored.
  1454. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1455. negates this setting for the duration of the command."
  1456. :group 'org-link-store
  1457. :type '(choice boolean integer))
  1458. (defcustom org-keep-stored-link-after-insertion nil
  1459. "Non-nil means keep link in list for entire session.
  1460. The command `org-store-link' adds a link pointing to the current
  1461. location to an internal list. These links accumulate during a session.
  1462. The command `org-insert-link' can be used to insert links into any
  1463. Org-mode file (offering completion for all stored links). When this
  1464. option is nil, every link which has been inserted once using \\[org-insert-link]
  1465. will be removed from the list, to make completing the unused links
  1466. more efficient."
  1467. :group 'org-link-store
  1468. :type 'boolean)
  1469. (defgroup org-link-follow nil
  1470. "Options concerning following links in Org-mode."
  1471. :tag "Org Follow Link"
  1472. :group 'org-link)
  1473. (defcustom org-link-translation-function nil
  1474. "Function to translate links with different syntax to Org syntax.
  1475. This can be used to translate links created for example by the Planner
  1476. or emacs-wiki packages to Org syntax.
  1477. The function must accept two parameters, a TYPE containing the link
  1478. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1479. which is everything after the link protocol. It should return a cons
  1480. with possibly modified values of type and path.
  1481. Org contains a function for this, so if you set this variable to
  1482. `org-translate-link-from-planner', you should be able follow many
  1483. links created by planner."
  1484. :group 'org-link-follow
  1485. :type '(choice (const nil) (function)))
  1486. (defcustom org-follow-link-hook nil
  1487. "Hook that is run after a link has been followed."
  1488. :group 'org-link-follow
  1489. :type 'hook)
  1490. (defcustom org-tab-follows-link nil
  1491. "Non-nil means on links TAB will follow the link.
  1492. Needs to be set before org.el is loaded.
  1493. This really should not be used, it does not make sense, and the
  1494. implementation is bad."
  1495. :group 'org-link-follow
  1496. :type 'boolean)
  1497. (defcustom org-return-follows-link nil
  1498. "Non-nil means on links RET will follow the link.
  1499. In tables, the special behavior of RET has precedence."
  1500. :group 'org-link-follow
  1501. :type 'boolean)
  1502. (defcustom org-mouse-1-follows-link
  1503. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1504. "Non-nil means mouse-1 on a link will follow the link.
  1505. A longer mouse click will still set point. Does not work on XEmacs.
  1506. Needs to be set before org.el is loaded."
  1507. :group 'org-link-follow
  1508. :type 'boolean)
  1509. (defcustom org-mark-ring-length 4
  1510. "Number of different positions to be recorded in the ring.
  1511. Changing this requires a restart of Emacs to work correctly."
  1512. :group 'org-link-follow
  1513. :type 'integer)
  1514. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1515. "Non-nil means internal links in Org files must exactly match a headline.
  1516. When nil, the link search tries to match a phrase with all words
  1517. in the search text."
  1518. :group 'org-link-follow
  1519. :version "24.1"
  1520. :type '(choice
  1521. (const :tag "Use fuzzy text search" nil)
  1522. (const :tag "Match only exact headline" t)
  1523. (const :tag "Match exact headline or query to create it"
  1524. query-to-create)))
  1525. (defcustom org-link-frame-setup
  1526. '((vm . vm-visit-folder-other-frame)
  1527. (vm-imap . vm-visit-imap-folder-other-frame)
  1528. (gnus . org-gnus-no-new-news)
  1529. (file . find-file-other-window)
  1530. (wl . wl-other-frame))
  1531. "Setup the frame configuration for following links.
  1532. When following a link with Emacs, it may often be useful to display
  1533. this link in another window or frame. This variable can be used to
  1534. set this up for the different types of links.
  1535. For VM, use any of
  1536. `vm-visit-folder'
  1537. `vm-visit-folder-other-window'
  1538. `vm-visit-folder-other-frame'
  1539. For Gnus, use any of
  1540. `gnus'
  1541. `gnus-other-frame'
  1542. `org-gnus-no-new-news'
  1543. For FILE, use any of
  1544. `find-file'
  1545. `find-file-other-window'
  1546. `find-file-other-frame'
  1547. For Wanderlust use any of
  1548. `wl'
  1549. `wl-other-frame'
  1550. For the calendar, use the variable `calendar-setup'.
  1551. For BBDB, it is currently only possible to display the matches in
  1552. another window."
  1553. :group 'org-link-follow
  1554. :type '(list
  1555. (cons (const vm)
  1556. (choice
  1557. (const vm-visit-folder)
  1558. (const vm-visit-folder-other-window)
  1559. (const vm-visit-folder-other-frame)))
  1560. (cons (const vm-imap)
  1561. (choice
  1562. (const vm-visit-imap-folder)
  1563. (const vm-visit-imap-folder-other-window)
  1564. (const vm-visit-imap-folder-other-frame)))
  1565. (cons (const gnus)
  1566. (choice
  1567. (const gnus)
  1568. (const gnus-other-frame)
  1569. (const org-gnus-no-new-news)))
  1570. (cons (const file)
  1571. (choice
  1572. (const find-file)
  1573. (const find-file-other-window)
  1574. (const find-file-other-frame)))
  1575. (cons (const wl)
  1576. (choice
  1577. (const wl)
  1578. (const wl-other-frame)))))
  1579. (defcustom org-display-internal-link-with-indirect-buffer nil
  1580. "Non-nil means use indirect buffer to display infile links.
  1581. Activating internal links (from one location in a file to another location
  1582. in the same file) normally just jumps to the location. When the link is
  1583. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1584. is displayed in
  1585. another window. When this option is set, the other window actually displays
  1586. an indirect buffer clone of the current buffer, to avoid any visibility
  1587. changes to the current buffer."
  1588. :group 'org-link-follow
  1589. :type 'boolean)
  1590. (defcustom org-open-non-existing-files nil
  1591. "Non-nil means `org-open-file' will open non-existing files.
  1592. When nil, an error will be generated.
  1593. This variable applies only to external applications because they
  1594. might choke on non-existing files. If the link is to a file that
  1595. will be opened in Emacs, the variable is ignored."
  1596. :group 'org-link-follow
  1597. :type 'boolean)
  1598. (defcustom org-open-directory-means-index-dot-org nil
  1599. "Non-nil means a link to a directory really means to index.org.
  1600. When nil, following a directory link will run dired or open a finder/explorer
  1601. window on that directory."
  1602. :group 'org-link-follow
  1603. :type 'boolean)
  1604. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1605. "Function and arguments to call for following mailto links.
  1606. This is a list with the first element being a Lisp function, and the
  1607. remaining elements being arguments to the function. In string arguments,
  1608. %a will be replaced by the address, and %s will be replaced by the subject
  1609. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1610. :group 'org-link-follow
  1611. :type '(choice
  1612. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1613. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1614. (const :tag "message-mail" (message-mail "%a" "%s"))
  1615. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1616. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1617. "Non-nil means ask for confirmation before executing shell links.
  1618. Shell links can be dangerous: just think about a link
  1619. [[shell:rm -rf ~/*][Google Search]]
  1620. This link would show up in your Org-mode document as \"Google Search\",
  1621. but really it would remove your entire home directory.
  1622. Therefore we advise against setting this variable to nil.
  1623. Just change it to `y-or-n-p' if you want to confirm with a
  1624. single keystroke rather than having to type \"yes\"."
  1625. :group 'org-link-follow
  1626. :type '(choice
  1627. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1628. (const :tag "with y-or-n (faster)" y-or-n-p)
  1629. (const :tag "no confirmation (dangerous)" nil)))
  1630. (put 'org-confirm-shell-link-function
  1631. 'safe-local-variable
  1632. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1633. (defcustom org-confirm-shell-link-not-regexp ""
  1634. "A regexp to skip confirmation for shell links."
  1635. :group 'org-link-follow
  1636. :version "24.1"
  1637. :type 'regexp)
  1638. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1639. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1640. Elisp links can be dangerous: just think about a link
  1641. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1642. This link would show up in your Org-mode document as \"Google Search\",
  1643. but really it would remove your entire home directory.
  1644. Therefore we advise against setting this variable to nil.
  1645. Just change it to `y-or-n-p' if you want to confirm with a
  1646. single keystroke rather than having to type \"yes\"."
  1647. :group 'org-link-follow
  1648. :type '(choice
  1649. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1650. (const :tag "with y-or-n (faster)" y-or-n-p)
  1651. (const :tag "no confirmation (dangerous)" nil)))
  1652. (put 'org-confirm-shell-link-function
  1653. 'safe-local-variable
  1654. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1655. (defcustom org-confirm-elisp-link-not-regexp ""
  1656. "A regexp to skip confirmation for Elisp links."
  1657. :group 'org-link-follow
  1658. :version "24.1"
  1659. :type 'regexp)
  1660. (defconst org-file-apps-defaults-gnu
  1661. '((remote . emacs)
  1662. (system . mailcap)
  1663. (t . mailcap))
  1664. "Default file applications on a UNIX or GNU/Linux system.
  1665. See `org-file-apps'.")
  1666. (defconst org-file-apps-defaults-macosx
  1667. '((remote . emacs)
  1668. (t . "open %s")
  1669. (system . "open %s")
  1670. ("ps.gz" . "gv %s")
  1671. ("eps.gz" . "gv %s")
  1672. ("dvi" . "xdvi %s")
  1673. ("fig" . "xfig %s"))
  1674. "Default file applications on a MacOS X system.
  1675. The system \"open\" is known as a default, but we use X11 applications
  1676. for some files for which the OS does not have a good default.
  1677. See `org-file-apps'.")
  1678. (defconst org-file-apps-defaults-windowsnt
  1679. (list
  1680. '(remote . emacs)
  1681. (cons t
  1682. (list (if (featurep 'xemacs)
  1683. 'mswindows-shell-execute
  1684. 'w32-shell-execute)
  1685. "open" 'file))
  1686. (cons 'system
  1687. (list (if (featurep 'xemacs)
  1688. 'mswindows-shell-execute
  1689. 'w32-shell-execute)
  1690. "open" 'file)))
  1691. "Default file applications on a Windows NT system.
  1692. The system \"open\" is used for most files.
  1693. See `org-file-apps'.")
  1694. (defcustom org-file-apps
  1695. '((auto-mode . emacs)
  1696. ("\\.mm\\'" . default)
  1697. ("\\.x?html?\\'" . default)
  1698. ("\\.pdf\\'" . default))
  1699. "External applications for opening `file:path' items in a document.
  1700. Org-mode uses system defaults for different file types, but
  1701. you can use this variable to set the application for a given file
  1702. extension. The entries in this list are cons cells where the car identifies
  1703. files and the cdr the corresponding command. Possible values for the
  1704. file identifier are
  1705. \"string\" A string as a file identifier can be interpreted in different
  1706. ways, depending on its contents:
  1707. - Alphanumeric characters only:
  1708. Match links with this file extension.
  1709. Example: (\"pdf\" . \"evince %s\")
  1710. to open PDFs with evince.
  1711. - Regular expression: Match links where the
  1712. filename matches the regexp. If you want to
  1713. use groups here, use shy groups.
  1714. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1715. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1716. to open *.html and *.xhtml with firefox.
  1717. - Regular expression which contains (non-shy) groups:
  1718. Match links where the whole link, including \"::\", and
  1719. anything after that, matches the regexp.
  1720. In a custom command string, %1, %2, etc. are replaced with
  1721. the parts of the link that were matched by the groups.
  1722. For backwards compatibility, if a command string is given
  1723. that does not use any of the group matches, this case is
  1724. handled identically to the second one (i.e. match against
  1725. file name only).
  1726. In a custom lisp form, you can access the group matches with
  1727. (match-string n link).
  1728. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1729. to open [[file:document.pdf::5]] with evince at page 5.
  1730. `directory' Matches a directory
  1731. `remote' Matches a remote file, accessible through tramp or efs.
  1732. Remote files most likely should be visited through Emacs
  1733. because external applications cannot handle such paths.
  1734. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1735. so all files Emacs knows how to handle. Using this with
  1736. command `emacs' will open most files in Emacs. Beware that this
  1737. will also open html files inside Emacs, unless you add
  1738. (\"html\" . default) to the list as well.
  1739. t Default for files not matched by any of the other options.
  1740. `system' The system command to open files, like `open' on Windows
  1741. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1742. that will be selected if you call `C-c C-o' with a double
  1743. \\[universal-argument] \\[universal-argument] prefix.
  1744. Possible values for the command are:
  1745. `emacs' The file will be visited by the current Emacs process.
  1746. `default' Use the default application for this file type, which is the
  1747. association for t in the list, most likely in the system-specific
  1748. part.
  1749. This can be used to overrule an unwanted setting in the
  1750. system-specific variable.
  1751. `system' Use the system command for opening files, like \"open\".
  1752. This command is specified by the entry whose car is `system'.
  1753. Most likely, the system-specific version of this variable
  1754. does define this command, but you can overrule/replace it
  1755. here.
  1756. string A command to be executed by a shell; %s will be replaced
  1757. by the path to the file.
  1758. sexp A Lisp form which will be evaluated. The file path will
  1759. be available in the Lisp variable `file'.
  1760. For more examples, see the system specific constants
  1761. `org-file-apps-defaults-macosx'
  1762. `org-file-apps-defaults-windowsnt'
  1763. `org-file-apps-defaults-gnu'."
  1764. :group 'org-link-follow
  1765. :type '(repeat
  1766. (cons (choice :value ""
  1767. (string :tag "Extension")
  1768. (const :tag "System command to open files" system)
  1769. (const :tag "Default for unrecognized files" t)
  1770. (const :tag "Remote file" remote)
  1771. (const :tag "Links to a directory" directory)
  1772. (const :tag "Any files that have Emacs modes"
  1773. auto-mode))
  1774. (choice :value ""
  1775. (const :tag "Visit with Emacs" emacs)
  1776. (const :tag "Use default" default)
  1777. (const :tag "Use the system command" system)
  1778. (string :tag "Command")
  1779. (sexp :tag "Lisp form")))))
  1780. (defcustom org-doi-server-url "http://dx.doi.org/"
  1781. "The URL of the DOI server."
  1782. :type 'string
  1783. :version "24.3"
  1784. :group 'org-link-follow)
  1785. (defgroup org-refile nil
  1786. "Options concerning refiling entries in Org-mode."
  1787. :tag "Org Refile"
  1788. :group 'org)
  1789. (defcustom org-directory "~/org"
  1790. "Directory with org files.
  1791. This is just a default location to look for Org files. There is no need
  1792. at all to put your files into this directory. It is only used in the
  1793. following situations:
  1794. 1. When a capture template specifies a target file that is not an
  1795. absolute path. The path will then be interpreted relative to
  1796. `org-directory'
  1797. 2. When a capture note is filed away in an interactive way (when exiting the
  1798. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1799. with `org-directory' as the default path."
  1800. :group 'org-refile
  1801. :group 'org-remember
  1802. :group 'org-capture
  1803. :type 'directory)
  1804. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1805. "Default target for storing notes.
  1806. Used as a fall back file for org-remember.el and org-capture.el, for
  1807. templates that do not specify a target file."
  1808. :group 'org-refile
  1809. :group 'org-remember
  1810. :group 'org-capture
  1811. :type '(choice
  1812. (const :tag "Default from remember-data-file" nil)
  1813. file))
  1814. (defcustom org-goto-interface 'outline
  1815. "The default interface to be used for `org-goto'.
  1816. Allowed values are:
  1817. outline The interface shows an outline of the relevant file
  1818. and the correct heading is found by moving through
  1819. the outline or by searching with incremental search.
  1820. outline-path-completion Headlines in the current buffer are offered via
  1821. completion. This is the interface also used by
  1822. the refile command."
  1823. :group 'org-refile
  1824. :type '(choice
  1825. (const :tag "Outline" outline)
  1826. (const :tag "Outline-path-completion" outline-path-completion)))
  1827. (defcustom org-goto-max-level 5
  1828. "Maximum target level when running `org-goto' with refile interface."
  1829. :group 'org-refile
  1830. :type 'integer)
  1831. (defcustom org-reverse-note-order nil
  1832. "Non-nil means store new notes at the beginning of a file or entry.
  1833. When nil, new notes will be filed to the end of a file or entry.
  1834. This can also be a list with cons cells of regular expressions that
  1835. are matched against file names, and values."
  1836. :group 'org-remember
  1837. :group 'org-capture
  1838. :group 'org-refile
  1839. :type '(choice
  1840. (const :tag "Reverse always" t)
  1841. (const :tag "Reverse never" nil)
  1842. (repeat :tag "By file name regexp"
  1843. (cons regexp boolean))))
  1844. (defcustom org-log-refile nil
  1845. "Information to record when a task is refiled.
  1846. Possible values are:
  1847. nil Don't add anything
  1848. time Add a time stamp to the task
  1849. note Prompt for a note and add it with template `org-log-note-headings'
  1850. This option can also be set with on a per-file-basis with
  1851. #+STARTUP: nologrefile
  1852. #+STARTUP: logrefile
  1853. #+STARTUP: lognoterefile
  1854. You can have local logging settings for a subtree by setting the LOGGING
  1855. property to one or more of these keywords.
  1856. When bulk-refiling from the agenda, the value `note' is forbidden and
  1857. will temporarily be changed to `time'."
  1858. :group 'org-refile
  1859. :group 'org-progress
  1860. :version "24.1"
  1861. :type '(choice
  1862. (const :tag "No logging" nil)
  1863. (const :tag "Record timestamp" time)
  1864. (const :tag "Record timestamp with note." note)))
  1865. (defcustom org-refile-targets nil
  1866. "Targets for refiling entries with \\[org-refile].
  1867. This is a list of cons cells. Each cell contains:
  1868. - a specification of the files to be considered, either a list of files,
  1869. or a symbol whose function or variable value will be used to retrieve
  1870. a file name or a list of file names. If you use `org-agenda-files' for
  1871. that, all agenda files will be scanned for targets. Nil means consider
  1872. headings in the current buffer.
  1873. - A specification of how to find candidate refile targets. This may be
  1874. any of:
  1875. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1876. This tag has to be present in all target headlines, inheritance will
  1877. not be considered.
  1878. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1879. todo keyword.
  1880. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1881. headlines that are refiling targets.
  1882. - a cons cell (:level . N). Any headline of level N is considered a target.
  1883. Note that, when `org-odd-levels-only' is set, level corresponds to
  1884. order in hierarchy, not to the number of stars.
  1885. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1886. Note that, when `org-odd-levels-only' is set, level corresponds to
  1887. order in hierarchy, not to the number of stars.
  1888. Each element of this list generates a set of possible targets.
  1889. The union of these sets is presented (with completion) to
  1890. the user by `org-refile'.
  1891. You can set the variable `org-refile-target-verify-function' to a function
  1892. to verify each headline found by the simple criteria above.
  1893. When this variable is nil, all top-level headlines in the current buffer
  1894. are used, equivalent to the value `((nil . (:level . 1))'."
  1895. :group 'org-refile
  1896. :type '(repeat
  1897. (cons
  1898. (choice :value org-agenda-files
  1899. (const :tag "All agenda files" org-agenda-files)
  1900. (const :tag "Current buffer" nil)
  1901. (function) (variable) (file))
  1902. (choice :tag "Identify target headline by"
  1903. (cons :tag "Specific tag" (const :value :tag) (string))
  1904. (cons :tag "TODO keyword" (const :value :todo) (string))
  1905. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1906. (cons :tag "Level number" (const :value :level) (integer))
  1907. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1908. (defcustom org-refile-target-verify-function nil
  1909. "Function to verify if the headline at point should be a refile target.
  1910. The function will be called without arguments, with point at the
  1911. beginning of the headline. It should return t and leave point
  1912. where it is if the headline is a valid target for refiling.
  1913. If the target should not be selected, the function must return nil.
  1914. In addition to this, it may move point to a place from where the search
  1915. should be continued. For example, the function may decide that the entire
  1916. subtree of the current entry should be excluded and move point to the end
  1917. of the subtree."
  1918. :group 'org-refile
  1919. :type '(choice
  1920. (const nil)
  1921. (function)))
  1922. (defcustom org-refile-use-cache nil
  1923. "Non-nil means cache refile targets to speed up the process.
  1924. The cache for a particular file will be updated automatically when
  1925. the buffer has been killed, or when any of the marker used for flagging
  1926. refile targets no longer points at a live buffer.
  1927. If you have added new entries to a buffer that might themselves be targets,
  1928. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1929. find that easier, `C-u C-u C-u C-c C-w'."
  1930. :group 'org-refile
  1931. :version "24.1"
  1932. :type 'boolean)
  1933. (defcustom org-refile-use-outline-path nil
  1934. "Non-nil means provide refile targets as paths.
  1935. So a level 3 headline will be available as level1/level2/level3.
  1936. When the value is `file', also include the file name (without directory)
  1937. into the path. In this case, you can also stop the completion after
  1938. the file name, to get entries inserted as top level in the file.
  1939. When `full-file-path', include the full file path."
  1940. :group 'org-refile
  1941. :type '(choice
  1942. (const :tag "Not" nil)
  1943. (const :tag "Yes" t)
  1944. (const :tag "Start with file name" file)
  1945. (const :tag "Start with full file path" full-file-path)))
  1946. (defcustom org-outline-path-complete-in-steps t
  1947. "Non-nil means complete the outline path in hierarchical steps.
  1948. When Org-mode uses the refile interface to select an outline path
  1949. \(see variable `org-refile-use-outline-path'), the completion of
  1950. the path can be done is a single go, or if can be done in steps down
  1951. the headline hierarchy. Going in steps is probably the best if you
  1952. do not use a special completion package like `ido' or `icicles'.
  1953. However, when using these packages, going in one step can be very
  1954. fast, while still showing the whole path to the entry."
  1955. :group 'org-refile
  1956. :type 'boolean)
  1957. (defcustom org-refile-allow-creating-parent-nodes nil
  1958. "Non-nil means allow to create new nodes as refile targets.
  1959. New nodes are then created by adding \"/new node name\" to the completion
  1960. of an existing node. When the value of this variable is `confirm',
  1961. new node creation must be confirmed by the user (recommended)
  1962. When nil, the completion must match an existing entry.
  1963. Note that, if the new heading is not seen by the criteria
  1964. listed in `org-refile-targets', multiple instances of the same
  1965. heading would be created by trying again to file under the new
  1966. heading."
  1967. :group 'org-refile
  1968. :type '(choice
  1969. (const :tag "Never" nil)
  1970. (const :tag "Always" t)
  1971. (const :tag "Prompt for confirmation" confirm)))
  1972. (defcustom org-refile-active-region-within-subtree nil
  1973. "Non-nil means also refile active region within a subtree.
  1974. By default `org-refile' doesn't allow refiling regions if they
  1975. don't contain a set of subtrees, but it might be convenient to
  1976. do so sometimes: in that case, the first line of the region is
  1977. converted to a headline before refiling."
  1978. :group 'org-refile
  1979. :version "24.1"
  1980. :type 'boolean)
  1981. (defgroup org-todo nil
  1982. "Options concerning TODO items in Org-mode."
  1983. :tag "Org TODO"
  1984. :group 'org)
  1985. (defgroup org-progress nil
  1986. "Options concerning Progress logging in Org-mode."
  1987. :tag "Org Progress"
  1988. :group 'org-time)
  1989. (defvar org-todo-interpretation-widgets
  1990. '((:tag "Sequence (cycling hits every state)" sequence)
  1991. (:tag "Type (cycling directly to DONE)" type))
  1992. "The available interpretation symbols for customizing `org-todo-keywords'.
  1993. Interested libraries should add to this list.")
  1994. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1995. "List of TODO entry keyword sequences and their interpretation.
  1996. \\<org-mode-map>This is a list of sequences.
  1997. Each sequence starts with a symbol, either `sequence' or `type',
  1998. indicating if the keywords should be interpreted as a sequence of
  1999. action steps, or as different types of TODO items. The first
  2000. keywords are states requiring action - these states will select a headline
  2001. for inclusion into the global TODO list Org-mode produces. If one of
  2002. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2003. signify that no further action is necessary. If \"|\" is not found,
  2004. the last keyword is treated as the only DONE state of the sequence.
  2005. The command \\[org-todo] cycles an entry through these states, and one
  2006. additional state where no keyword is present. For details about this
  2007. cycling, see the manual.
  2008. TODO keywords and interpretation can also be set on a per-file basis with
  2009. the special #+SEQ_TODO and #+TYP_TODO lines.
  2010. Each keyword can optionally specify a character for fast state selection
  2011. \(in combination with the variable `org-use-fast-todo-selection')
  2012. and specifiers for state change logging, using the same syntax that
  2013. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2014. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2015. indicates to record a time stamp each time this state is selected.
  2016. Each keyword may also specify if a timestamp or a note should be
  2017. recorded when entering or leaving the state, by adding additional
  2018. characters in the parenthesis after the keyword. This looks like this:
  2019. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2020. record only the time of the state change. With X and Y being either
  2021. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2022. Y when leaving the state if and only if the *target* state does not
  2023. define X. You may omit any of the fast-selection key or X or /Y,
  2024. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2025. For backward compatibility, this variable may also be just a list
  2026. of keywords. In this case the interpretation (sequence or type) will be
  2027. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2028. :group 'org-todo
  2029. :group 'org-keywords
  2030. :type '(choice
  2031. (repeat :tag "Old syntax, just keywords"
  2032. (string :tag "Keyword"))
  2033. (repeat :tag "New syntax"
  2034. (cons
  2035. (choice
  2036. :tag "Interpretation"
  2037. ;;Quick and dirty way to see
  2038. ;;`org-todo-interpretations'. This takes the
  2039. ;;place of item arguments
  2040. :convert-widget
  2041. (lambda (widget)
  2042. (widget-put widget
  2043. :args (mapcar
  2044. #'(lambda (x)
  2045. (widget-convert
  2046. (cons 'const x)))
  2047. org-todo-interpretation-widgets))
  2048. widget))
  2049. (repeat
  2050. (string :tag "Keyword"))))))
  2051. (defvar org-todo-keywords-1 nil
  2052. "All TODO and DONE keywords active in a buffer.")
  2053. (make-variable-buffer-local 'org-todo-keywords-1)
  2054. (defvar org-todo-keywords-for-agenda nil)
  2055. (defvar org-done-keywords-for-agenda nil)
  2056. (defvar org-drawers-for-agenda nil)
  2057. (defvar org-todo-keyword-alist-for-agenda nil)
  2058. (defvar org-tag-alist-for-agenda nil
  2059. "Alist of all tags from all agenda files.")
  2060. (defvar org-tag-groups-alist-for-agenda nil
  2061. "Alist of all groups tags from all current agenda files.")
  2062. (defvar org-tag-groups-alist nil)
  2063. (make-variable-buffer-local 'org-tag-groups-alist)
  2064. (defvar org-agenda-contributing-files nil)
  2065. (defvar org-not-done-keywords nil)
  2066. (make-variable-buffer-local 'org-not-done-keywords)
  2067. (defvar org-done-keywords nil)
  2068. (make-variable-buffer-local 'org-done-keywords)
  2069. (defvar org-todo-heads nil)
  2070. (make-variable-buffer-local 'org-todo-heads)
  2071. (defvar org-todo-sets nil)
  2072. (make-variable-buffer-local 'org-todo-sets)
  2073. (defvar org-todo-log-states nil)
  2074. (make-variable-buffer-local 'org-todo-log-states)
  2075. (defvar org-todo-kwd-alist nil)
  2076. (make-variable-buffer-local 'org-todo-kwd-alist)
  2077. (defvar org-todo-key-alist nil)
  2078. (make-variable-buffer-local 'org-todo-key-alist)
  2079. (defvar org-todo-key-trigger nil)
  2080. (make-variable-buffer-local 'org-todo-key-trigger)
  2081. (defcustom org-todo-interpretation 'sequence
  2082. "Controls how TODO keywords are interpreted.
  2083. This variable is in principle obsolete and is only used for
  2084. backward compatibility, if the interpretation of todo keywords is
  2085. not given already in `org-todo-keywords'. See that variable for
  2086. more information."
  2087. :group 'org-todo
  2088. :group 'org-keywords
  2089. :type '(choice (const sequence)
  2090. (const type)))
  2091. (defcustom org-use-fast-todo-selection t
  2092. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2093. This variable describes if and under what circumstances the cycling
  2094. mechanism for TODO keywords will be replaced by a single-key, direct
  2095. selection scheme.
  2096. When nil, fast selection is never used.
  2097. When the symbol `prefix', it will be used when `org-todo' is called
  2098. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2099. `C-u t' in an agenda buffer.
  2100. When t, fast selection is used by default. In this case, the prefix
  2101. argument forces cycling instead.
  2102. In all cases, the special interface is only used if access keys have
  2103. actually been assigned by the user, i.e. if keywords in the configuration
  2104. are followed by a letter in parenthesis, like TODO(t)."
  2105. :group 'org-todo
  2106. :type '(choice
  2107. (const :tag "Never" nil)
  2108. (const :tag "By default" t)
  2109. (const :tag "Only with C-u C-c C-t" prefix)))
  2110. (defcustom org-provide-todo-statistics t
  2111. "Non-nil means update todo statistics after insert and toggle.
  2112. ALL-HEADLINES means update todo statistics by including headlines
  2113. with no TODO keyword as well, counting them as not done.
  2114. A list of TODO keywords means the same, but skip keywords that are
  2115. not in this list.
  2116. When this is set, todo statistics is updated in the parent of the
  2117. current entry each time a todo state is changed."
  2118. :group 'org-todo
  2119. :type '(choice
  2120. (const :tag "Yes, only for TODO entries" t)
  2121. (const :tag "Yes, including all entries" 'all-headlines)
  2122. (repeat :tag "Yes, for TODOs in this list"
  2123. (string :tag "TODO keyword"))
  2124. (other :tag "No TODO statistics" nil)))
  2125. (defcustom org-hierarchical-todo-statistics t
  2126. "Non-nil means TODO statistics covers just direct children.
  2127. When nil, all entries in the subtree are considered.
  2128. This has only an effect if `org-provide-todo-statistics' is set.
  2129. To set this to nil for only a single subtree, use a COOKIE_DATA
  2130. property and include the word \"recursive\" into the value."
  2131. :group 'org-todo
  2132. :type 'boolean)
  2133. (defcustom org-after-todo-state-change-hook nil
  2134. "Hook which is run after the state of a TODO item was changed.
  2135. The new state (a string with a TODO keyword, or nil) is available in the
  2136. Lisp variable `org-state'."
  2137. :group 'org-todo
  2138. :type 'hook)
  2139. (defvar org-blocker-hook nil
  2140. "Hook for functions that are allowed to block a state change.
  2141. Functions in this hook should not modify the buffer.
  2142. Each function gets as its single argument a property list,
  2143. see `org-trigger-hook' for more information about this list.
  2144. If any of the functions in this hook returns nil, the state change
  2145. is blocked.")
  2146. (defvar org-trigger-hook nil
  2147. "Hook for functions that are triggered by a state change.
  2148. Each function gets as its single argument a property list with at
  2149. least the following elements:
  2150. (:type type-of-change :position pos-at-entry-start
  2151. :from old-state :to new-state)
  2152. Depending on the type, more properties may be present.
  2153. This mechanism is currently implemented for:
  2154. TODO state changes
  2155. ------------------
  2156. :type todo-state-change
  2157. :from previous state (keyword as a string), or nil, or a symbol
  2158. 'todo' or 'done', to indicate the general type of state.
  2159. :to new state, like in :from")
  2160. (defcustom org-enforce-todo-dependencies nil
  2161. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2162. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2163. be blocked if any prior sibling is not yet done.
  2164. Finally, if the parent is blocked because of ordered siblings of its own,
  2165. the child will also be blocked."
  2166. :set (lambda (var val)
  2167. (set var val)
  2168. (if val
  2169. (add-hook 'org-blocker-hook
  2170. 'org-block-todo-from-children-or-siblings-or-parent)
  2171. (remove-hook 'org-blocker-hook
  2172. 'org-block-todo-from-children-or-siblings-or-parent)))
  2173. :group 'org-todo
  2174. :type 'boolean)
  2175. (defcustom org-enforce-todo-checkbox-dependencies nil
  2176. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2177. When this is nil, checkboxes have no influence on switching TODO states.
  2178. When non-nil, you first need to check off all check boxes before the TODO
  2179. entry can be switched to DONE.
  2180. This variable needs to be set before org.el is loaded, and you need to
  2181. restart Emacs after a change to make the change effective. The only way
  2182. to change is while Emacs is running is through the customize interface."
  2183. :set (lambda (var val)
  2184. (set var val)
  2185. (if val
  2186. (add-hook 'org-blocker-hook
  2187. 'org-block-todo-from-checkboxes)
  2188. (remove-hook 'org-blocker-hook
  2189. 'org-block-todo-from-checkboxes)))
  2190. :group 'org-todo
  2191. :type 'boolean)
  2192. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2193. "Non-nil means inserting a TODO heading is treated as state change.
  2194. So when the command \\[org-insert-todo-heading] is used, state change
  2195. logging will apply if appropriate. When nil, the new TODO item will
  2196. be inserted directly, and no logging will take place."
  2197. :group 'org-todo
  2198. :type 'boolean)
  2199. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2200. "Non-nil means switching TODO states with S-cursor counts as state change.
  2201. This is the default behavior. However, setting this to nil allows a
  2202. convenient way to select a TODO state and bypass any logging associated
  2203. with that."
  2204. :group 'org-todo
  2205. :type 'boolean)
  2206. (defcustom org-todo-state-tags-triggers nil
  2207. "Tag changes that should be triggered by TODO state changes.
  2208. This is a list. Each entry is
  2209. (state-change (tag . flag) .......)
  2210. State-change can be a string with a state, and empty string to indicate the
  2211. state that has no TODO keyword, or it can be one of the symbols `todo'
  2212. or `done', meaning any not-done or done state, respectively."
  2213. :group 'org-todo
  2214. :group 'org-tags
  2215. :type '(repeat
  2216. (cons (choice :tag "When changing to"
  2217. (const :tag "Not-done state" todo)
  2218. (const :tag "Done state" done)
  2219. (string :tag "State"))
  2220. (repeat
  2221. (cons :tag "Tag action"
  2222. (string :tag "Tag")
  2223. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2224. (defcustom org-log-done nil
  2225. "Information to record when a task moves to the DONE state.
  2226. Possible values are:
  2227. nil Don't add anything, just change the keyword
  2228. time Add a time stamp to the task
  2229. note Prompt for a note and add it with template `org-log-note-headings'
  2230. This option can also be set with on a per-file-basis with
  2231. #+STARTUP: nologdone
  2232. #+STARTUP: logdone
  2233. #+STARTUP: lognotedone
  2234. You can have local logging settings for a subtree by setting the LOGGING
  2235. property to one or more of these keywords."
  2236. :group 'org-todo
  2237. :group 'org-progress
  2238. :type '(choice
  2239. (const :tag "No logging" nil)
  2240. (const :tag "Record CLOSED timestamp" time)
  2241. (const :tag "Record CLOSED timestamp with note." note)))
  2242. ;; Normalize old uses of org-log-done.
  2243. (cond
  2244. ((eq org-log-done t) (setq org-log-done 'time))
  2245. ((and (listp org-log-done) (memq 'done org-log-done))
  2246. (setq org-log-done 'note)))
  2247. (defcustom org-log-reschedule nil
  2248. "Information to record when the scheduling date of a tasks is modified.
  2249. Possible values are:
  2250. nil Don't add anything, just change the date
  2251. time Add a time stamp to the task
  2252. note Prompt for a note and add it with template `org-log-note-headings'
  2253. This option can also be set with on a per-file-basis with
  2254. #+STARTUP: nologreschedule
  2255. #+STARTUP: logreschedule
  2256. #+STARTUP: lognotereschedule"
  2257. :group 'org-todo
  2258. :group 'org-progress
  2259. :type '(choice
  2260. (const :tag "No logging" nil)
  2261. (const :tag "Record timestamp" time)
  2262. (const :tag "Record timestamp with note." note)))
  2263. (defcustom org-log-redeadline nil
  2264. "Information to record when the deadline date of a tasks is modified.
  2265. Possible values are:
  2266. nil Don't add anything, just change the date
  2267. time Add a time stamp to the task
  2268. note Prompt for a note and add it with template `org-log-note-headings'
  2269. This option can also be set with on a per-file-basis with
  2270. #+STARTUP: nologredeadline
  2271. #+STARTUP: logredeadline
  2272. #+STARTUP: lognoteredeadline
  2273. You can have local logging settings for a subtree by setting the LOGGING
  2274. property to one or more of these keywords."
  2275. :group 'org-todo
  2276. :group 'org-progress
  2277. :type '(choice
  2278. (const :tag "No logging" nil)
  2279. (const :tag "Record timestamp" time)
  2280. (const :tag "Record timestamp with note." note)))
  2281. (defcustom org-log-note-clock-out nil
  2282. "Non-nil means record a note when clocking out of an item.
  2283. This can also be configured on a per-file basis by adding one of
  2284. the following lines anywhere in the buffer:
  2285. #+STARTUP: lognoteclock-out
  2286. #+STARTUP: nolognoteclock-out"
  2287. :group 'org-todo
  2288. :group 'org-progress
  2289. :type 'boolean)
  2290. (defcustom org-log-done-with-time t
  2291. "Non-nil means the CLOSED time stamp will contain date and time.
  2292. When nil, only the date will be recorded."
  2293. :group 'org-progress
  2294. :type 'boolean)
  2295. (defcustom org-log-note-headings
  2296. '((done . "CLOSING NOTE %t")
  2297. (state . "State %-12s from %-12S %t")
  2298. (note . "Note taken on %t")
  2299. (reschedule . "Rescheduled from %S on %t")
  2300. (delschedule . "Not scheduled, was %S on %t")
  2301. (redeadline . "New deadline from %S on %t")
  2302. (deldeadline . "Removed deadline, was %S on %t")
  2303. (refile . "Refiled on %t")
  2304. (clock-out . ""))
  2305. "Headings for notes added to entries.
  2306. The value is an alist, with the car being a symbol indicating the note
  2307. context, and the cdr is the heading to be used. The heading may also be the
  2308. empty string.
  2309. %t in the heading will be replaced by a time stamp.
  2310. %T will be an active time stamp instead the default inactive one
  2311. %d will be replaced by a short-format time stamp.
  2312. %D will be replaced by an active short-format time stamp.
  2313. %s will be replaced by the new TODO state, in double quotes.
  2314. %S will be replaced by the old TODO state, in double quotes.
  2315. %u will be replaced by the user name.
  2316. %U will be replaced by the full user name.
  2317. In fact, it is not a good idea to change the `state' entry, because
  2318. agenda log mode depends on the format of these entries."
  2319. :group 'org-todo
  2320. :group 'org-progress
  2321. :type '(list :greedy t
  2322. (cons (const :tag "Heading when closing an item" done) string)
  2323. (cons (const :tag
  2324. "Heading when changing todo state (todo sequence only)"
  2325. state) string)
  2326. (cons (const :tag "Heading when just taking a note" note) string)
  2327. (cons (const :tag "Heading when clocking out" clock-out) string)
  2328. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2329. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2330. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2331. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2332. (cons (const :tag "Heading when refiling" refile) string)))
  2333. (unless (assq 'note org-log-note-headings)
  2334. (push '(note . "%t") org-log-note-headings))
  2335. (defcustom org-log-into-drawer nil
  2336. "Non-nil means insert state change notes and time stamps into a drawer.
  2337. When nil, state changes notes will be inserted after the headline and
  2338. any scheduling and clock lines, but not inside a drawer.
  2339. The value of this variable should be the name of the drawer to use.
  2340. LOGBOOK is proposed as the default drawer for this purpose, you can
  2341. also set this to a string to define the drawer of your choice.
  2342. A value of t is also allowed, representing \"LOGBOOK\".
  2343. A value of t or nil can also be set with on a per-file-basis with
  2344. #+STARTUP: logdrawer
  2345. #+STARTUP: nologdrawer
  2346. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2347. will be ignored.
  2348. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2349. a subtree."
  2350. :group 'org-todo
  2351. :group 'org-progress
  2352. :type '(choice
  2353. (const :tag "Not into a drawer" nil)
  2354. (const :tag "LOGBOOK" t)
  2355. (string :tag "Other")))
  2356. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2357. (defun org-log-into-drawer ()
  2358. "Return the value of `org-log-into-drawer', but let properties overrule.
  2359. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2360. used instead of the default value."
  2361. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2362. (cond
  2363. ((not p) org-log-into-drawer)
  2364. ((equal p "nil") nil)
  2365. ((equal p "t") "LOGBOOK")
  2366. (t p))))
  2367. (defcustom org-log-state-notes-insert-after-drawers nil
  2368. "Non-nil means insert state change notes after any drawers in entry.
  2369. Only the drawers that *immediately* follow the headline and the
  2370. deadline/scheduled line are skipped.
  2371. When nil, insert notes right after the heading and perhaps the line
  2372. with deadline/scheduling if present.
  2373. This variable will have no effect if `org-log-into-drawer' is
  2374. set."
  2375. :group 'org-todo
  2376. :group 'org-progress
  2377. :type 'boolean)
  2378. (defcustom org-log-states-order-reversed t
  2379. "Non-nil means the latest state note will be directly after heading.
  2380. When nil, the state change notes will be ordered according to time.
  2381. This option can also be set with on a per-file-basis with
  2382. #+STARTUP: logstatesreversed
  2383. #+STARTUP: nologstatesreversed"
  2384. :group 'org-todo
  2385. :group 'org-progress
  2386. :type 'boolean)
  2387. (defcustom org-todo-repeat-to-state nil
  2388. "The TODO state to which a repeater should return the repeating task.
  2389. By default this is the first task in a TODO sequence, or the previous state
  2390. in a TODO_TYP set. But you can specify another task here.
  2391. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2392. :group 'org-todo
  2393. :version "24.1"
  2394. :type '(choice (const :tag "Head of sequence" nil)
  2395. (string :tag "Specific state")))
  2396. (defcustom org-log-repeat 'time
  2397. "Non-nil means record moving through the DONE state when triggering repeat.
  2398. An auto-repeating task is immediately switched back to TODO when
  2399. marked DONE. If you are not logging state changes (by adding \"@\"
  2400. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2401. record a closing note, there will be no record of the task moving
  2402. through DONE. This variable forces taking a note anyway.
  2403. nil Don't force a record
  2404. time Record a time stamp
  2405. note Prompt for a note and add it with template `org-log-note-headings'
  2406. This option can also be set with on a per-file-basis with
  2407. #+STARTUP: nologrepeat
  2408. #+STARTUP: logrepeat
  2409. #+STARTUP: lognoterepeat
  2410. You can have local logging settings for a subtree by setting the LOGGING
  2411. property to one or more of these keywords."
  2412. :group 'org-todo
  2413. :group 'org-progress
  2414. :type '(choice
  2415. (const :tag "Don't force a record" nil)
  2416. (const :tag "Force recording the DONE state" time)
  2417. (const :tag "Force recording a note with the DONE state" note)))
  2418. (defgroup org-priorities nil
  2419. "Priorities in Org-mode."
  2420. :tag "Org Priorities"
  2421. :group 'org-todo)
  2422. (defcustom org-enable-priority-commands t
  2423. "Non-nil means priority commands are active.
  2424. When nil, these commands will be disabled, so that you never accidentally
  2425. set a priority."
  2426. :group 'org-priorities
  2427. :type 'boolean)
  2428. (defcustom org-highest-priority ?A
  2429. "The highest priority of TODO items. A character like ?A, ?B etc.
  2430. Must have a smaller ASCII number than `org-lowest-priority'."
  2431. :group 'org-priorities
  2432. :type 'character)
  2433. (defcustom org-lowest-priority ?C
  2434. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2435. Must have a larger ASCII number than `org-highest-priority'."
  2436. :group 'org-priorities
  2437. :type 'character)
  2438. (defcustom org-default-priority ?B
  2439. "The default priority of TODO items.
  2440. This is the priority an item gets if no explicit priority is given.
  2441. When starting to cycle on an empty priority the first step in the cycle
  2442. depends on `org-priority-start-cycle-with-default'. The resulting first
  2443. step priority must not exceed the range from `org-highest-priority' to
  2444. `org-lowest-priority' which means that `org-default-priority' has to be
  2445. in this range exclusive or inclusive the range boundaries. Else the
  2446. first step refuses to set the default and the second will fall back
  2447. to (depending on the command used) the highest or lowest priority."
  2448. :group 'org-priorities
  2449. :type 'character)
  2450. (defcustom org-priority-start-cycle-with-default t
  2451. "Non-nil means start with default priority when starting to cycle.
  2452. When this is nil, the first step in the cycle will be (depending on the
  2453. command used) one higher or lower than the default priority.
  2454. See also `org-default-priority'."
  2455. :group 'org-priorities
  2456. :type 'boolean)
  2457. (defcustom org-get-priority-function nil
  2458. "Function to extract the priority from a string.
  2459. The string is normally the headline. If this is nil Org computes the
  2460. priority from the priority cookie like [#A] in the headline. It returns
  2461. an integer, increasing by 1000 for each priority level.
  2462. The user can set a different function here, which should take a string
  2463. as an argument and return the numeric priority."
  2464. :group 'org-priorities
  2465. :version "24.1"
  2466. :type '(choice
  2467. (const nil)
  2468. (function)))
  2469. (defgroup org-time nil
  2470. "Options concerning time stamps and deadlines in Org-mode."
  2471. :tag "Org Time"
  2472. :group 'org)
  2473. (defcustom org-insert-labeled-timestamps-at-point nil
  2474. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2475. When nil, these labeled time stamps are forces into the second line of an
  2476. entry, just after the headline. When scheduling from the global TODO list,
  2477. the time stamp will always be forced into the second line."
  2478. :group 'org-time
  2479. :type 'boolean)
  2480. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2481. "Formats for `format-time-string' which are used for time stamps.
  2482. It is not recommended to change this constant.")
  2483. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2484. "Number of minutes to round time stamps to.
  2485. These are two values, the first applies when first creating a time stamp.
  2486. The second applies when changing it with the commands `S-up' and `S-down'.
  2487. When changing the time stamp, this means that it will change in steps
  2488. of N minutes, as given by the second value.
  2489. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2490. numbers should be factors of 60, so for example 5, 10, 15.
  2491. When this is larger than 1, you can still force an exact time stamp by using
  2492. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2493. and by using a prefix arg to `S-up/down' to specify the exact number
  2494. of minutes to shift."
  2495. :group 'org-time
  2496. :get #'(lambda (var) ; Make sure both elements are there
  2497. (if (integerp (default-value var))
  2498. (list (default-value var) 5)
  2499. (default-value var)))
  2500. :type '(list
  2501. (integer :tag "when inserting times")
  2502. (integer :tag "when modifying times")))
  2503. ;; Normalize old customizations of this variable.
  2504. (when (integerp org-time-stamp-rounding-minutes)
  2505. (setq org-time-stamp-rounding-minutes
  2506. (list org-time-stamp-rounding-minutes
  2507. org-time-stamp-rounding-minutes)))
  2508. (defcustom org-display-custom-times nil
  2509. "Non-nil means overlay custom formats over all time stamps.
  2510. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2511. To turn this on on a per-file basis, insert anywhere in the file:
  2512. #+STARTUP: customtime"
  2513. :group 'org-time
  2514. :set 'set-default
  2515. :type 'sexp)
  2516. (make-variable-buffer-local 'org-display-custom-times)
  2517. (defcustom org-time-stamp-custom-formats
  2518. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2519. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2520. These are overlaid over the default ISO format if the variable
  2521. `org-display-custom-times' is set. Time like %H:%M should be at the
  2522. end of the second format. The custom formats are also honored by export
  2523. commands, if custom time display is turned on at the time of export."
  2524. :group 'org-time
  2525. :type 'sexp)
  2526. (defun org-time-stamp-format (&optional long inactive)
  2527. "Get the right format for a time string."
  2528. (let ((f (if long (cdr org-time-stamp-formats)
  2529. (car org-time-stamp-formats))))
  2530. (if inactive
  2531. (concat "[" (substring f 1 -1) "]")
  2532. f)))
  2533. (defcustom org-time-clocksum-format
  2534. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2535. "The format string used when creating CLOCKSUM lines.
  2536. This is also used when Org mode generates a time duration.
  2537. The value can be a single format string containing two
  2538. %-sequences, which will be filled with the number of hours and
  2539. minutes in that order.
  2540. Alternatively, the value can be a plist associating any of the
  2541. keys :years, :months, :weeks, :days, :hours or :minutes with
  2542. format strings. The time duration is formatted using only the
  2543. time components that are needed and concatenating the results.
  2544. If a time unit in absent, it falls back to the next smallest
  2545. unit.
  2546. The keys :require-years, :require-months, :require-days,
  2547. :require-weeks, :require-hours, :require-minutes are also
  2548. meaningful. A non-nil value for these keys indicates that the
  2549. corresponding time component should always be included, even if
  2550. its value is 0.
  2551. For example,
  2552. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2553. :require-minutes t)
  2554. means durations longer than a day will be expressed in days,
  2555. hours and minutes, and durations less than a day will always be
  2556. expressed in hours and minutes (even for durations less than an
  2557. hour).
  2558. The value
  2559. \(:days \"%dd\" :minutes \"%dm\")
  2560. means durations longer than a day will be expressed in days and
  2561. minutes, and durations less than a day will be expressed entirely
  2562. in minutes (even for durations longer than an hour)."
  2563. :group 'org-time
  2564. :group 'org-clock
  2565. :version "24.4"
  2566. :package-version '(Org . "8.0")
  2567. :type '(choice (string :tag "Format string")
  2568. (set :tag "Plist"
  2569. (group :inline t (const :tag "Years" :years)
  2570. (string :tag "Format string"))
  2571. (group :inline t
  2572. (const :tag "Always show years" :require-years)
  2573. (const t))
  2574. (group :inline t (const :tag "Months" :months)
  2575. (string :tag "Format string"))
  2576. (group :inline t
  2577. (const :tag "Always show months" :require-months)
  2578. (const t))
  2579. (group :inline t (const :tag "Weeks" :weeks)
  2580. (string :tag "Format string"))
  2581. (group :inline t
  2582. (const :tag "Always show weeks" :require-weeks)
  2583. (const t))
  2584. (group :inline t (const :tag "Days" :days)
  2585. (string :tag "Format string"))
  2586. (group :inline t
  2587. (const :tag "Always show days" :require-days)
  2588. (const t))
  2589. (group :inline t (const :tag "Hours" :hours)
  2590. (string :tag "Format string"))
  2591. (group :inline t
  2592. (const :tag "Always show hours" :require-hours)
  2593. (const t))
  2594. (group :inline t (const :tag "Minutes" :minutes)
  2595. (string :tag "Format string"))
  2596. (group :inline t
  2597. (const :tag "Always show minutes" :require-minutes)
  2598. (const t)))))
  2599. (defcustom org-time-clocksum-use-fractional nil
  2600. "When non-nil, \\[org-clock-display] uses fractional times.
  2601. See `org-time-clocksum-format' for more on time clock formats."
  2602. :group 'org-time
  2603. :group 'org-clock
  2604. :version "24.3"
  2605. :type 'boolean)
  2606. (defcustom org-time-clocksum-use-effort-durations nil
  2607. "When non-nil, \\[org-clock-display] uses effort durations.
  2608. E.g. by default, one day is considered to be a 8 hours effort,
  2609. so a task that has been clocked for 16 hours will be displayed
  2610. as during 2 days in the clock display or in the clocktable.
  2611. See `org-effort-durations' on how to set effort durations
  2612. and `org-time-clocksum-format' for more on time clock formats."
  2613. :group 'org-time
  2614. :group 'org-clock
  2615. :version "24.4"
  2616. :package-version '(Org . "8.0")
  2617. :type 'boolean)
  2618. (defcustom org-time-clocksum-fractional-format "%.2f"
  2619. "The format string used when creating CLOCKSUM lines,
  2620. or when Org mode generates a time duration, if
  2621. `org-time-clocksum-use-fractional' is enabled.
  2622. The value can be a single format string containing one
  2623. %-sequence, which will be filled with the number of hours as
  2624. a float.
  2625. Alternatively, the value can be a plist associating any of the
  2626. keys :years, :months, :weeks, :days, :hours or :minutes with
  2627. a format string. The time duration is formatted using the
  2628. largest time unit which gives a non-zero integer part. If all
  2629. specified formats have zero integer part, the smallest time unit
  2630. is used."
  2631. :group 'org-time
  2632. :type '(choice (string :tag "Format string")
  2633. (set (group :inline t (const :tag "Years" :years)
  2634. (string :tag "Format string"))
  2635. (group :inline t (const :tag "Months" :months)
  2636. (string :tag "Format string"))
  2637. (group :inline t (const :tag "Weeks" :weeks)
  2638. (string :tag "Format string"))
  2639. (group :inline t (const :tag "Days" :days)
  2640. (string :tag "Format string"))
  2641. (group :inline t (const :tag "Hours" :hours)
  2642. (string :tag "Format string"))
  2643. (group :inline t (const :tag "Minutes" :minutes)
  2644. (string :tag "Format string")))))
  2645. (defcustom org-deadline-warning-days 14
  2646. "Number of days before expiration during which a deadline becomes active.
  2647. This variable governs the display in sparse trees and in the agenda.
  2648. When 0 or negative, it means use this number (the absolute value of it)
  2649. even if a deadline has a different individual lead time specified.
  2650. Custom commands can set this variable in the options section."
  2651. :group 'org-time
  2652. :group 'org-agenda-daily/weekly
  2653. :type 'integer)
  2654. (defcustom org-scheduled-delay-days 0
  2655. "Number of days before a scheduled item becomes active.
  2656. This variable governs the display in sparse trees and in the agenda.
  2657. The default value (i.e. 0) means: don't delay scheduled item.
  2658. When negative, it means use this number (the absolute value of it)
  2659. even if a scheduled item has a different individual delay time
  2660. specified.
  2661. Custom commands can set this variable in the options section."
  2662. :group 'org-time
  2663. :group 'org-agenda-daily/weekly
  2664. :version "24.4"
  2665. :package-version '(Org . "8.0")
  2666. :type 'integer)
  2667. (defcustom org-read-date-prefer-future t
  2668. "Non-nil means assume future for incomplete date input from user.
  2669. This affects the following situations:
  2670. 1. The user gives a month but not a year.
  2671. For example, if it is April and you enter \"feb 2\", this will be read
  2672. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2673. 2. The user gives a day, but no month.
  2674. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2675. read this as the third of *next* month. However, if you enter \"17\",
  2676. it will be considered as *this* month.
  2677. If you set this variable to the symbol `time', then also the following
  2678. will work:
  2679. 3. If the user gives a time.
  2680. If the time is before now, it will be interpreted as tomorrow.
  2681. Currently none of this works for ISO week specifications.
  2682. When this option is nil, the current day, month and year will always be
  2683. used as defaults.
  2684. See also `org-agenda-jump-prefer-future'."
  2685. :group 'org-time
  2686. :type '(choice
  2687. (const :tag "Never" nil)
  2688. (const :tag "Check month and day" t)
  2689. (const :tag "Check month, day, and time" time)))
  2690. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2691. "Should the agenda jump command prefer the future for incomplete dates?
  2692. The default is to do the same as configured in `org-read-date-prefer-future'.
  2693. But you can also set a deviating value here.
  2694. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2695. :group 'org-agenda
  2696. :group 'org-time
  2697. :version "24.1"
  2698. :type '(choice
  2699. (const :tag "Use org-read-date-prefer-future"
  2700. org-read-date-prefer-future)
  2701. (const :tag "Never" nil)
  2702. (const :tag "Always" t)))
  2703. (defcustom org-read-date-force-compatible-dates t
  2704. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2705. Depending on the system Emacs is running on, certain dates cannot
  2706. be represented with the type used internally to represent time.
  2707. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2708. correctly. Some systems allow for earlier dates, some for later,
  2709. some for both. One way to find out it to insert any date into an
  2710. Org buffer, putting the cursor on the year and hitting S-up and
  2711. S-down to test the range.
  2712. When this variable is set to t, the date/time prompt will not let
  2713. you specify dates outside the 1970-2037 range, so it is certain that
  2714. these dates will work in whatever version of Emacs you are
  2715. running, and also that you can move a file from one Emacs implementation
  2716. to another. WHenever Org is forcing the year for you, it will display
  2717. a message and beep.
  2718. When this variable is nil, Org will check if the date is
  2719. representable in the specific Emacs implementation you are using.
  2720. If not, it will force a year, usually the current year, and beep
  2721. to remind you. Currently this setting is not recommended because
  2722. the likelihood that you will open your Org files in an Emacs that
  2723. has limited date range is not negligible.
  2724. A workaround for this problem is to use diary sexp dates for time
  2725. stamps outside of this range."
  2726. :group 'org-time
  2727. :version "24.1"
  2728. :type 'boolean)
  2729. (defcustom org-read-date-display-live t
  2730. "Non-nil means display current interpretation of date prompt live.
  2731. This display will be in an overlay, in the minibuffer."
  2732. :group 'org-time
  2733. :type 'boolean)
  2734. (defcustom org-read-date-popup-calendar t
  2735. "Non-nil means pop up a calendar when prompting for a date.
  2736. In the calendar, the date can be selected with mouse-1. However, the
  2737. minibuffer will also be active, and you can simply enter the date as well.
  2738. When nil, only the minibuffer will be available."
  2739. :group 'org-time
  2740. :type 'boolean)
  2741. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2742. 'org-read-date-popup-calendar)
  2743. (make-obsolete-variable
  2744. 'org-read-date-minibuffer-setup-hook
  2745. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2746. (defcustom org-read-date-minibuffer-setup-hook nil
  2747. "Hook to be used to set up keys for the date/time interface.
  2748. Add key definitions to `minibuffer-local-map', which will be a
  2749. temporary copy.
  2750. WARNING: This option is obsolete, you should use
  2751. `org-read-date-minibuffer-local-map' to set up keys."
  2752. :group 'org-time
  2753. :type 'hook)
  2754. (defcustom org-extend-today-until 0
  2755. "The hour when your day really ends. Must be an integer.
  2756. This has influence for the following applications:
  2757. - When switching the agenda to \"today\". It it is still earlier than
  2758. the time given here, the day recognized as TODAY is actually yesterday.
  2759. - When a date is read from the user and it is still before the time given
  2760. here, the current date and time will be assumed to be yesterday, 23:59.
  2761. Also, timestamps inserted in capture templates follow this rule.
  2762. IMPORTANT: This is a feature whose implementation is and likely will
  2763. remain incomplete. Really, it is only here because past midnight seems to
  2764. be the favorite working time of John Wiegley :-)"
  2765. :group 'org-time
  2766. :type 'integer)
  2767. (defcustom org-use-effective-time nil
  2768. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2769. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2770. \"effective time\" of any timestamps between midnight and 8am will be
  2771. 23:59 of the previous day."
  2772. :group 'org-time
  2773. :version "24.1"
  2774. :type 'boolean)
  2775. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2776. "When non-nil, use the last clock out time for `org-todo'.
  2777. Note that this option has precedence over the combined use of
  2778. `org-use-effective-time' and `org-extend-today-until'."
  2779. :group 'org-time
  2780. :version "24.4"
  2781. :package-version '(Org . "8.0")
  2782. :type 'boolean)
  2783. (defcustom org-edit-timestamp-down-means-later nil
  2784. "Non-nil means S-down will increase the time in a time stamp.
  2785. When nil, S-up will increase."
  2786. :group 'org-time
  2787. :type 'boolean)
  2788. (defcustom org-calendar-follow-timestamp-change t
  2789. "Non-nil means make the calendar window follow timestamp changes.
  2790. When a timestamp is modified and the calendar window is visible, it will be
  2791. moved to the new date."
  2792. :group 'org-time
  2793. :type 'boolean)
  2794. (defgroup org-tags nil
  2795. "Options concerning tags in Org-mode."
  2796. :tag "Org Tags"
  2797. :group 'org)
  2798. (defcustom org-tag-alist nil
  2799. "List of tags allowed in Org-mode files.
  2800. When this list is nil, Org-mode will base TAG input on what is already in the
  2801. buffer.
  2802. The value of this variable is an alist, the car of each entry must be a
  2803. keyword as a string, the cdr may be a character that is used to select
  2804. that tag through the fast-tag-selection interface.
  2805. See the manual for details."
  2806. :group 'org-tags
  2807. :type '(repeat
  2808. (choice
  2809. (cons (string :tag "Tag name")
  2810. (character :tag "Access char"))
  2811. (list :tag "Start radio group"
  2812. (const :startgroup)
  2813. (option (string :tag "Group description")))
  2814. (list :tag "Group tags delimiter"
  2815. (const :grouptags))
  2816. (list :tag "End radio group"
  2817. (const :endgroup)
  2818. (option (string :tag "Group description")))
  2819. (const :tag "New line" (:newline)))))
  2820. (defcustom org-tag-persistent-alist nil
  2821. "List of tags that will always appear in all Org-mode files.
  2822. This is in addition to any in buffer settings or customizations
  2823. of `org-tag-alist'.
  2824. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2825. The value of this variable is an alist, the car of each entry must be a
  2826. keyword as a string, the cdr may be a character that is used to select
  2827. that tag through the fast-tag-selection interface.
  2828. See the manual for details.
  2829. To disable these tags on a per-file basis, insert anywhere in the file:
  2830. #+STARTUP: noptag"
  2831. :group 'org-tags
  2832. :type '(repeat
  2833. (choice
  2834. (cons (string :tag "Tag name")
  2835. (character :tag "Access char"))
  2836. (const :tag "Start radio group" (:startgroup))
  2837. (const :tag "Group tags delimiter" (:grouptags))
  2838. (const :tag "End radio group" (:endgroup))
  2839. (const :tag "New line" (:newline)))))
  2840. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2841. "If non-nil, always offer completion for all tags of all agenda files.
  2842. Instead of customizing this variable directly, you might want to
  2843. set it locally for capture buffers, because there no list of
  2844. tags in that file can be created dynamically (there are none).
  2845. (add-hook 'org-capture-mode-hook
  2846. (lambda ()
  2847. (set (make-local-variable
  2848. 'org-complete-tags-always-offer-all-agenda-tags)
  2849. t)))"
  2850. :group 'org-tags
  2851. :version "24.1"
  2852. :type 'boolean)
  2853. (defvar org-file-tags nil
  2854. "List of tags that can be inherited by all entries in the file.
  2855. The tags will be inherited if the variable `org-use-tag-inheritance'
  2856. says they should be.
  2857. This variable is populated from #+FILETAGS lines.")
  2858. (defcustom org-use-fast-tag-selection 'auto
  2859. "Non-nil means use fast tag selection scheme.
  2860. This is a special interface to select and deselect tags with single keys.
  2861. When nil, fast selection is never used.
  2862. When the symbol `auto', fast selection is used if and only if selection
  2863. characters for tags have been configured, either through the variable
  2864. `org-tag-alist' or through a #+TAGS line in the buffer.
  2865. When t, fast selection is always used and selection keys are assigned
  2866. automatically if necessary."
  2867. :group 'org-tags
  2868. :type '(choice
  2869. (const :tag "Always" t)
  2870. (const :tag "Never" nil)
  2871. (const :tag "When selection characters are configured" auto)))
  2872. (defcustom org-fast-tag-selection-single-key nil
  2873. "Non-nil means fast tag selection exits after first change.
  2874. When nil, you have to press RET to exit it.
  2875. During fast tag selection, you can toggle this flag with `C-c'.
  2876. This variable can also have the value `expert'. In this case, the window
  2877. displaying the tags menu is not even shown, until you press C-c again."
  2878. :group 'org-tags
  2879. :type '(choice
  2880. (const :tag "No" nil)
  2881. (const :tag "Yes" t)
  2882. (const :tag "Expert" expert)))
  2883. (defvar org-fast-tag-selection-include-todo nil
  2884. "Non-nil means fast tags selection interface will also offer TODO states.
  2885. This is an undocumented feature, you should not rely on it.")
  2886. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2887. "The column to which tags should be indented in a headline.
  2888. If this number is positive, it specifies the column. If it is negative,
  2889. it means that the tags should be flushright to that column. For example,
  2890. -80 works well for a normal 80 character screen.
  2891. When 0, place tags directly after headline text, with only one space in
  2892. between."
  2893. :group 'org-tags
  2894. :type 'integer)
  2895. (defcustom org-auto-align-tags t
  2896. "Non-nil keeps tags aligned when modifying headlines.
  2897. Some operations (i.e. demoting) change the length of a headline and
  2898. therefore shift the tags around. With this option turned on, after
  2899. each such operation the tags are again aligned to `org-tags-column'."
  2900. :group 'org-tags
  2901. :type 'boolean)
  2902. (defcustom org-use-tag-inheritance t
  2903. "Non-nil means tags in levels apply also for sublevels.
  2904. When nil, only the tags directly given in a specific line apply there.
  2905. This may also be a list of tags that should be inherited, or a regexp that
  2906. matches tags that should be inherited. Additional control is possible
  2907. with the variable `org-tags-exclude-from-inheritance' which gives an
  2908. explicit list of tags to be excluded from inheritance, even if the value of
  2909. `org-use-tag-inheritance' would select it for inheritance.
  2910. If this option is t, a match early-on in a tree can lead to a large
  2911. number of matches in the subtree when constructing the agenda or creating
  2912. a sparse tree. If you only want to see the first match in a tree during
  2913. a search, check out the variable `org-tags-match-list-sublevels'."
  2914. :group 'org-tags
  2915. :type '(choice
  2916. (const :tag "Not" nil)
  2917. (const :tag "Always" t)
  2918. (repeat :tag "Specific tags" (string :tag "Tag"))
  2919. (regexp :tag "Tags matched by regexp")))
  2920. (defcustom org-tags-exclude-from-inheritance nil
  2921. "List of tags that should never be inherited.
  2922. This is a way to exclude a few tags from inheritance. For way to do
  2923. the opposite, to actively allow inheritance for selected tags,
  2924. see the variable `org-use-tag-inheritance'."
  2925. :group 'org-tags
  2926. :type '(repeat (string :tag "Tag")))
  2927. (defun org-tag-inherit-p (tag)
  2928. "Check if TAG is one that should be inherited."
  2929. (cond
  2930. ((member tag org-tags-exclude-from-inheritance) nil)
  2931. ((eq org-use-tag-inheritance t) t)
  2932. ((not org-use-tag-inheritance) nil)
  2933. ((stringp org-use-tag-inheritance)
  2934. (string-match org-use-tag-inheritance tag))
  2935. ((listp org-use-tag-inheritance)
  2936. (member tag org-use-tag-inheritance))
  2937. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2938. (defcustom org-tags-match-list-sublevels t
  2939. "Non-nil means list also sublevels of headlines matching a search.
  2940. This variable applies to tags/property searches, and also to stuck
  2941. projects because this search is based on a tags match as well.
  2942. When set to the symbol `indented', sublevels are indented with
  2943. leading dots.
  2944. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2945. the sublevels of a headline matching a tag search often also match
  2946. the same search. Listing all of them can create very long lists.
  2947. Setting this variable to nil causes subtrees of a match to be skipped.
  2948. This variable is semi-obsolete and probably should always be true. It
  2949. is better to limit inheritance to certain tags using the variables
  2950. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2951. :group 'org-tags
  2952. :type '(choice
  2953. (const :tag "No, don't list them" nil)
  2954. (const :tag "Yes, do list them" t)
  2955. (const :tag "List them, indented with leading dots" indented)))
  2956. (defcustom org-tags-sort-function nil
  2957. "When set, tags are sorted using this function as a comparator."
  2958. :group 'org-tags
  2959. :type '(choice
  2960. (const :tag "No sorting" nil)
  2961. (const :tag "Alphabetical" string<)
  2962. (const :tag "Reverse alphabetical" string>)
  2963. (function :tag "Custom function" nil)))
  2964. (defvar org-tags-history nil
  2965. "History of minibuffer reads for tags.")
  2966. (defvar org-last-tags-completion-table nil
  2967. "The last used completion table for tags.")
  2968. (defvar org-after-tags-change-hook nil
  2969. "Hook that is run after the tags in a line have changed.")
  2970. (defgroup org-properties nil
  2971. "Options concerning properties in Org-mode."
  2972. :tag "Org Properties"
  2973. :group 'org)
  2974. (defcustom org-property-format "%-10s %s"
  2975. "How property key/value pairs should be formatted by `indent-line'.
  2976. When `indent-line' hits a property definition, it will format the line
  2977. according to this format, mainly to make sure that the values are
  2978. lined-up with respect to each other."
  2979. :group 'org-properties
  2980. :type 'string)
  2981. (defcustom org-properties-postprocess-alist nil
  2982. "Alist of properties and functions to adjust inserted values.
  2983. Elements of this alist must be of the form
  2984. ([string] [function])
  2985. where [string] must be a property name and [function] must be a
  2986. lambda expression: this lambda expression must take one argument,
  2987. the value to adjust, and return the new value as a string.
  2988. For example, this element will allow the property \"Remaining\"
  2989. to be updated wrt the relation between the \"Effort\" property
  2990. and the clock summary:
  2991. ((\"Remaining\" (lambda(value)
  2992. (let ((clocksum (org-clock-sum-current-item))
  2993. (effort (org-duration-string-to-minutes
  2994. (org-entry-get (point) \"Effort\"))))
  2995. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2996. :group 'org-properties
  2997. :version "24.1"
  2998. :type '(alist :key-type (string :tag "Property")
  2999. :value-type (function :tag "Function")))
  3000. (defcustom org-use-property-inheritance nil
  3001. "Non-nil means properties apply also for sublevels.
  3002. This setting is chiefly used during property searches. Turning it on can
  3003. cause significant overhead when doing a search, which is why it is not
  3004. on by default.
  3005. When nil, only the properties directly given in the current entry count.
  3006. When t, every property is inherited. The value may also be a list of
  3007. properties that should have inheritance, or a regular expression matching
  3008. properties that should be inherited.
  3009. However, note that some special properties use inheritance under special
  3010. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3011. and the properties ending in \"_ALL\" when they are used as descriptor
  3012. for valid values of a property.
  3013. Note for programmers:
  3014. When querying an entry with `org-entry-get', you can control if inheritance
  3015. should be used. By default, `org-entry-get' looks only at the local
  3016. properties. You can request inheritance by setting the inherit argument
  3017. to t (to force inheritance) or to `selective' (to respect the setting
  3018. in this variable)."
  3019. :group 'org-properties
  3020. :type '(choice
  3021. (const :tag "Not" nil)
  3022. (const :tag "Always" t)
  3023. (repeat :tag "Specific properties" (string :tag "Property"))
  3024. (regexp :tag "Properties matched by regexp")))
  3025. (defun org-property-inherit-p (property)
  3026. "Check if PROPERTY is one that should be inherited."
  3027. (cond
  3028. ((eq org-use-property-inheritance t) t)
  3029. ((not org-use-property-inheritance) nil)
  3030. ((stringp org-use-property-inheritance)
  3031. (string-match org-use-property-inheritance property))
  3032. ((listp org-use-property-inheritance)
  3033. (member property org-use-property-inheritance))
  3034. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3035. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3036. "The default column format, if no other format has been defined.
  3037. This variable can be set on the per-file basis by inserting a line
  3038. #+COLUMNS: %25ITEM ....."
  3039. :group 'org-properties
  3040. :type 'string)
  3041. (defcustom org-columns-ellipses ".."
  3042. "The ellipses to be used when a field in column view is truncated.
  3043. When this is the empty string, as many characters as possible are shown,
  3044. but then there will be no visual indication that the field has been truncated.
  3045. When this is a string of length N, the last N characters of a truncated
  3046. field are replaced by this string. If the column is narrower than the
  3047. ellipses string, only part of the ellipses string will be shown."
  3048. :group 'org-properties
  3049. :type 'string)
  3050. (defcustom org-columns-modify-value-for-display-function nil
  3051. "Function that modifies values for display in column view.
  3052. For example, it can be used to cut out a certain part from a time stamp.
  3053. The function must take 2 arguments:
  3054. column-title The title of the column (*not* the property name)
  3055. value The value that should be modified.
  3056. The function should return the value that should be displayed,
  3057. or nil if the normal value should be used."
  3058. :group 'org-properties
  3059. :type '(choice (const nil) (function)))
  3060. (defcustom org-effort-property "Effort"
  3061. "The property that is being used to keep track of effort estimates.
  3062. Effort estimates given in this property need to have the format H:MM."
  3063. :group 'org-properties
  3064. :group 'org-progress
  3065. :type '(string :tag "Property"))
  3066. (defconst org-global-properties-fixed
  3067. '(("VISIBILITY_ALL" . "folded children content all")
  3068. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3069. "List of property/value pairs that can be inherited by any entry.
  3070. These are fixed values, for the preset properties. The user variable
  3071. that can be used to add to this list is `org-global-properties'.
  3072. The entries in this list are cons cells where the car is a property
  3073. name and cdr is a string with the value. If the value represents
  3074. multiple items like an \"_ALL\" property, separate the items by
  3075. spaces.")
  3076. (defcustom org-global-properties nil
  3077. "List of property/value pairs that can be inherited by any entry.
  3078. This list will be combined with the constant `org-global-properties-fixed'.
  3079. The entries in this list are cons cells where the car is a property
  3080. name and cdr is a string with the value.
  3081. You can set buffer-local values for the same purpose in the variable
  3082. `org-file-properties' this by adding lines like
  3083. #+PROPERTY: NAME VALUE"
  3084. :group 'org-properties
  3085. :type '(repeat
  3086. (cons (string :tag "Property")
  3087. (string :tag "Value"))))
  3088. (defvar org-file-properties nil
  3089. "List of property/value pairs that can be inherited by any entry.
  3090. Valid for the current buffer.
  3091. This variable is populated from #+PROPERTY lines.")
  3092. (make-variable-buffer-local 'org-file-properties)
  3093. (defgroup org-agenda nil
  3094. "Options concerning agenda views in Org-mode."
  3095. :tag "Org Agenda"
  3096. :group 'org)
  3097. (defvar org-category nil
  3098. "Variable used by org files to set a category for agenda display.
  3099. Such files should use a file variable to set it, for example
  3100. # -*- mode: org; org-category: \"ELisp\"
  3101. or contain a special line
  3102. #+CATEGORY: ELisp
  3103. If the file does not specify a category, then file's base name
  3104. is used instead.")
  3105. (make-variable-buffer-local 'org-category)
  3106. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3107. (defcustom org-agenda-files nil
  3108. "The files to be used for agenda display.
  3109. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3110. \\[org-remove-file]. You can also use customize to edit the list.
  3111. If an entry is a directory, all files in that directory that are matched by
  3112. `org-agenda-file-regexp' will be part of the file list.
  3113. If the value of the variable is not a list but a single file name, then
  3114. the list of agenda files is actually stored and maintained in that file, one
  3115. agenda file per line. In this file paths can be given relative to
  3116. `org-directory'. Tilde expansion and environment variable substitution
  3117. are also made."
  3118. :group 'org-agenda
  3119. :type '(choice
  3120. (repeat :tag "List of files and directories" file)
  3121. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3122. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3123. "Regular expression to match files for `org-agenda-files'.
  3124. If any element in the list in that variable contains a directory instead
  3125. of a normal file, all files in that directory that are matched by this
  3126. regular expression will be included."
  3127. :group 'org-agenda
  3128. :type 'regexp)
  3129. (defcustom org-agenda-text-search-extra-files nil
  3130. "List of extra files to be searched by text search commands.
  3131. These files will be searched in addition to the agenda files by the
  3132. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3133. Note that these files will only be searched for text search commands,
  3134. not for the other agenda views like todo lists, tag searches or the weekly
  3135. agenda. This variable is intended to list notes and possibly archive files
  3136. that should also be searched by these two commands.
  3137. In fact, if the first element in the list is the symbol `agenda-archives',
  3138. then all archive files of all agenda files will be added to the search
  3139. scope."
  3140. :group 'org-agenda
  3141. :type '(set :greedy t
  3142. (const :tag "Agenda Archives" agenda-archives)
  3143. (repeat :inline t (file))))
  3144. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3145. 'org-agenda-text-search-extra-files)
  3146. (defcustom org-agenda-skip-unavailable-files nil
  3147. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3148. A nil value means to remove them, after a query, from the list."
  3149. :group 'org-agenda
  3150. :type 'boolean)
  3151. (defcustom org-calendar-to-agenda-key [?c]
  3152. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3153. The command `org-calendar-goto-agenda' will be bound to this key. The
  3154. default is the character `c' because then `c' can be used to switch back and
  3155. forth between agenda and calendar."
  3156. :group 'org-agenda
  3157. :type 'sexp)
  3158. (defcustom org-calendar-insert-diary-entry-key [?i]
  3159. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3160. This option is irrelevant until `org-agenda-diary-file' has been configured
  3161. to point to an Org-mode file. When that is the case, the command
  3162. `org-agenda-diary-entry' will be bound to the key given here, by default
  3163. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3164. if you want to continue doing this, you need to change this to a different
  3165. key."
  3166. :group 'org-agenda
  3167. :type 'sexp)
  3168. (defcustom org-agenda-diary-file 'diary-file
  3169. "File to which to add new entries with the `i' key in agenda and calendar.
  3170. When this is the symbol `diary-file', the functionality in the Emacs
  3171. calendar will be used to add entries to the `diary-file'. But when this
  3172. points to a file, `org-agenda-diary-entry' will be used instead."
  3173. :group 'org-agenda
  3174. :type '(choice
  3175. (const :tag "The standard Emacs diary file" diary-file)
  3176. (file :tag "Special Org file diary entries")))
  3177. (eval-after-load "calendar"
  3178. '(progn
  3179. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3180. 'org-calendar-goto-agenda)
  3181. (add-hook 'calendar-mode-hook
  3182. (lambda ()
  3183. (unless (eq org-agenda-diary-file 'diary-file)
  3184. (define-key calendar-mode-map
  3185. org-calendar-insert-diary-entry-key
  3186. 'org-agenda-diary-entry))))))
  3187. (defgroup org-latex nil
  3188. "Options for embedding LaTeX code into Org-mode."
  3189. :tag "Org LaTeX"
  3190. :group 'org)
  3191. (defcustom org-format-latex-options
  3192. '(:foreground default :background default :scale 1.0
  3193. :html-foreground "Black" :html-background "Transparent"
  3194. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3195. "Options for creating images from LaTeX fragments.
  3196. This is a property list with the following properties:
  3197. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3198. `default' means use the foreground of the default face.
  3199. `auto' means use the foreground from the text face.
  3200. :background the background color, or \"Transparent\".
  3201. `default' means use the background of the default face.
  3202. `auto' means use the background from the text face.
  3203. :scale a scaling factor for the size of the images, to get more pixels
  3204. :html-foreground, :html-background, :html-scale
  3205. the same numbers for HTML export.
  3206. :matchers a list indicating which matchers should be used to
  3207. find LaTeX fragments. Valid members of this list are:
  3208. \"begin\" find environments
  3209. \"$1\" find single characters surrounded by $.$
  3210. \"$\" find math expressions surrounded by $...$
  3211. \"$$\" find math expressions surrounded by $$....$$
  3212. \"\\(\" find math expressions surrounded by \\(...\\)
  3213. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3214. :group 'org-latex
  3215. :type 'plist)
  3216. (defcustom org-format-latex-signal-error t
  3217. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3218. When nil, just push out a message."
  3219. :group 'org-latex
  3220. :version "24.1"
  3221. :type 'boolean)
  3222. (defcustom org-latex-to-mathml-jar-file nil
  3223. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3224. Use this to specify additional executable file say a jar file.
  3225. When using MathToWeb as the converter, specify the full-path to
  3226. your mathtoweb.jar file."
  3227. :group 'org-latex
  3228. :version "24.1"
  3229. :type '(choice
  3230. (const :tag "None" nil)
  3231. (file :tag "JAR file" :must-match t)))
  3232. (defcustom org-latex-to-mathml-convert-command nil
  3233. "Command to convert LaTeX fragments to MathML.
  3234. Replace format-specifiers in the command as noted below and use
  3235. `shell-command' to convert LaTeX to MathML.
  3236. %j: Executable file in fully expanded form as specified by
  3237. `org-latex-to-mathml-jar-file'.
  3238. %I: Input LaTeX file in fully expanded form
  3239. %o: Output MathML file
  3240. This command is used by `org-create-math-formula'.
  3241. When using MathToWeb as the converter, set this to
  3242. \"java -jar %j -unicode -force -df %o %I\"."
  3243. :group 'org-latex
  3244. :version "24.1"
  3245. :type '(choice
  3246. (const :tag "None" nil)
  3247. (string :tag "\nShell command")))
  3248. (defcustom org-latex-create-formula-image-program 'dvipng
  3249. "Program to convert LaTeX fragments with.
  3250. dvipng Process the LaTeX fragments to dvi file, then convert
  3251. dvi files to png files using dvipng.
  3252. This will also include processing of non-math environments.
  3253. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3254. to convert pdf files to png files"
  3255. :group 'org-latex
  3256. :version "24.1"
  3257. :type '(choice
  3258. (const :tag "dvipng" dvipng)
  3259. (const :tag "imagemagick" imagemagick)))
  3260. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3261. "Path to store latex preview images.
  3262. A relative path here creates many directories relative to the
  3263. processed org files paths. An absolute path puts all preview
  3264. images at the same place."
  3265. :group 'org-latex
  3266. :version "24.3"
  3267. :type 'string)
  3268. (defun org-format-latex-mathml-available-p ()
  3269. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3270. (save-match-data
  3271. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3272. org-latex-to-mathml-convert-command)
  3273. (let ((executable (car (split-string
  3274. org-latex-to-mathml-convert-command))))
  3275. (when (executable-find executable)
  3276. (if (string-match
  3277. "%j" org-latex-to-mathml-convert-command)
  3278. (file-readable-p org-latex-to-mathml-jar-file)
  3279. t))))))
  3280. (defcustom org-format-latex-header "\\documentclass{article}
  3281. \\usepackage[usenames]{color}
  3282. \\usepackage{amsmath}
  3283. \\usepackage[mathscr]{eucal}
  3284. \\pagestyle{empty} % do not remove
  3285. \[PACKAGES]
  3286. \[DEFAULT-PACKAGES]
  3287. % The settings below are copied from fullpage.sty
  3288. \\setlength{\\textwidth}{\\paperwidth}
  3289. \\addtolength{\\textwidth}{-3cm}
  3290. \\setlength{\\oddsidemargin}{1.5cm}
  3291. \\addtolength{\\oddsidemargin}{-2.54cm}
  3292. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3293. \\setlength{\\textheight}{\\paperheight}
  3294. \\addtolength{\\textheight}{-\\headheight}
  3295. \\addtolength{\\textheight}{-\\headsep}
  3296. \\addtolength{\\textheight}{-\\footskip}
  3297. \\addtolength{\\textheight}{-3cm}
  3298. \\setlength{\\topmargin}{1.5cm}
  3299. \\addtolength{\\topmargin}{-2.54cm}"
  3300. "The document header used for processing LaTeX fragments.
  3301. It is imperative that this header make sure that no page number
  3302. appears on the page. The package defined in the variables
  3303. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3304. will either replace the placeholder \"[PACKAGES]\" in this
  3305. header, or they will be appended."
  3306. :group 'org-latex
  3307. :type 'string)
  3308. (defun org-set-packages-alist (var val)
  3309. "Set the packages alist and make sure it has 3 elements per entry."
  3310. (set var (mapcar (lambda (x)
  3311. (if (and (consp x) (= (length x) 2))
  3312. (list (car x) (nth 1 x) t)
  3313. x))
  3314. val)))
  3315. (defun org-get-packages-alist (var)
  3316. "Get the packages alist and make sure it has 3 elements per entry."
  3317. (mapcar (lambda (x)
  3318. (if (and (consp x) (= (length x) 2))
  3319. (list (car x) (nth 1 x) t)
  3320. x))
  3321. (default-value var)))
  3322. (defcustom org-latex-default-packages-alist
  3323. '(("AUTO" "inputenc" t)
  3324. ("T1" "fontenc" t)
  3325. ("" "fixltx2e" nil)
  3326. ("" "graphicx" t)
  3327. ("" "longtable" nil)
  3328. ("" "float" nil)
  3329. ("" "wrapfig" nil)
  3330. ("normalem" "ulem" t)
  3331. ("" "textcomp" t)
  3332. ("" "marvosym" t)
  3333. ("" "wasysym" t)
  3334. ("" "latexsym" t)
  3335. ("" "amssymb" t)
  3336. ("" "amstext" nil)
  3337. ("" "hyperref" nil)
  3338. "\\tolerance=1000")
  3339. "Alist of default packages to be inserted in the header.
  3340. Change this only if one of the packages here causes an
  3341. incompatibility with another package you are using.
  3342. The packages in this list are needed by one part or another of
  3343. Org mode to function properly:
  3344. - inputenc, fontenc: for basic font and character selection
  3345. - amstext: for subscript and superscript
  3346. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3347. symbols used for interpreting the entities in `org-entities'.
  3348. You can skip some of these packages if you don't use any of the
  3349. symbols in it.
  3350. - ulem: for underline and strike-through
  3351. - graphicx: for including images
  3352. - float, wrapfig: for figure placement
  3353. - longtable: for long tables
  3354. - hyperref: for cross references
  3355. Therefore you should not modify this variable unless you know
  3356. what you are doing. The one reason to change it anyway is that
  3357. you might be loading some other package that conflicts with one
  3358. of the default packages. Each cell is of the format
  3359. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3360. the package also needs to be included when compiling LaTeX
  3361. snippets into images for inclusion into non-LaTeX output."
  3362. :group 'org-latex
  3363. :group 'org-export-latex
  3364. :set 'org-set-packages-alist
  3365. :get 'org-get-packages-alist
  3366. :version "24.1"
  3367. :type '(repeat
  3368. (choice
  3369. (list :tag "options/package pair"
  3370. (string :tag "options")
  3371. (string :tag "package")
  3372. (boolean :tag "Snippet"))
  3373. (string :tag "A line of LaTeX"))))
  3374. (defcustom org-latex-packages-alist nil
  3375. "Alist of packages to be inserted in every LaTeX header.
  3376. These will be inserted after `org-latex-default-packages-alist'.
  3377. Each cell is of the format:
  3378. \(\"options\" \"package\" snippet-flag)
  3379. SNIPPET-FLAG, when t, indicates that this package is also needed
  3380. when turning LaTeX snippets into images for inclusion into
  3381. non-LaTeX output.
  3382. Make sure that you only list packages here which:
  3383. - you want in every file
  3384. - do not conflict with the setup in `org-format-latex-header'.
  3385. - do not conflict with the default packages in
  3386. `org-latex-default-packages-alist'."
  3387. :group 'org-latex
  3388. :group 'org-export-latex
  3389. :set 'org-set-packages-alist
  3390. :get 'org-get-packages-alist
  3391. :type '(repeat
  3392. (choice
  3393. (list :tag "options/package pair"
  3394. (string :tag "options")
  3395. (string :tag "package")
  3396. (boolean :tag "Snippet"))
  3397. (string :tag "A line of LaTeX"))))
  3398. (defgroup org-appearance nil
  3399. "Settings for Org-mode appearance."
  3400. :tag "Org Appearance"
  3401. :group 'org)
  3402. (defcustom org-level-color-stars-only nil
  3403. "Non-nil means fontify only the stars in each headline.
  3404. When nil, the entire headline is fontified.
  3405. Changing it requires restart of `font-lock-mode' to become effective
  3406. also in regions already fontified."
  3407. :group 'org-appearance
  3408. :type 'boolean)
  3409. (defcustom org-hide-leading-stars nil
  3410. "Non-nil means hide the first N-1 stars in a headline.
  3411. This works by using the face `org-hide' for these stars. This
  3412. face is white for a light background, and black for a dark
  3413. background. You may have to customize the face `org-hide' to
  3414. make this work.
  3415. Changing it requires restart of `font-lock-mode' to become effective
  3416. also in regions already fontified.
  3417. You may also set this on a per-file basis by adding one of the following
  3418. lines to the buffer:
  3419. #+STARTUP: hidestars
  3420. #+STARTUP: showstars"
  3421. :group 'org-appearance
  3422. :type 'boolean)
  3423. (defcustom org-hidden-keywords nil
  3424. "List of symbols corresponding to keywords to be hidden the org buffer.
  3425. For example, a value '(title) for this list will make the document's title
  3426. appear in the buffer without the initial #+TITLE: keyword."
  3427. :group 'org-appearance
  3428. :version "24.1"
  3429. :type '(set (const :tag "#+AUTHOR" author)
  3430. (const :tag "#+DATE" date)
  3431. (const :tag "#+EMAIL" email)
  3432. (const :tag "#+TITLE" title)))
  3433. (defcustom org-custom-properties nil
  3434. "List of properties (as strings) with a special meaning.
  3435. The default use of these custom properties is to let the user
  3436. hide them with `org-toggle-custom-properties-visibility'."
  3437. :group 'org-properties
  3438. :group 'org-appearance
  3439. :version "24.3"
  3440. :type '(repeat (string :tag "Property Name")))
  3441. (defcustom org-fontify-done-headline nil
  3442. "Non-nil means change the face of a headline if it is marked DONE.
  3443. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3444. When this is non-nil, the headline after the keyword is set to the
  3445. `org-headline-done' as an additional indication."
  3446. :group 'org-appearance
  3447. :type 'boolean)
  3448. (defcustom org-fontify-emphasized-text t
  3449. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3450. Changing this variable requires a restart of Emacs to take effect."
  3451. :group 'org-appearance
  3452. :type 'boolean)
  3453. (defcustom org-fontify-whole-heading-line nil
  3454. "Non-nil means fontify the whole line for headings.
  3455. This is useful when setting a background color for the
  3456. org-level-* faces."
  3457. :group 'org-appearance
  3458. :type 'boolean)
  3459. (defcustom org-highlight-latex-and-related nil
  3460. "Non-nil means highlight LaTeX related syntax in the buffer.
  3461. When non nil, the value should be a list containing any of the
  3462. following symbols:
  3463. `latex' Highlight LaTeX snippets and environments.
  3464. `script' Highlight subscript and superscript.
  3465. `entities' Highlight entities."
  3466. :group 'org-appearance
  3467. :version "24.4"
  3468. :package-version '(Org . "8.0")
  3469. :type '(choice
  3470. (const :tag "No highlighting" nil)
  3471. (set :greedy t :tag "Highlight"
  3472. (const :tag "LaTeX snippets and environments" latex)
  3473. (const :tag "Subscript and superscript" script)
  3474. (const :tag "Entities" entities))))
  3475. (defcustom org-hide-emphasis-markers nil
  3476. "Non-nil mean font-lock should hide the emphasis marker characters."
  3477. :group 'org-appearance
  3478. :type 'boolean)
  3479. (defcustom org-pretty-entities nil
  3480. "Non-nil means show entities as UTF8 characters.
  3481. When nil, the \\name form remains in the buffer."
  3482. :group 'org-appearance
  3483. :version "24.1"
  3484. :type 'boolean)
  3485. (defcustom org-pretty-entities-include-sub-superscripts t
  3486. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3487. :group 'org-appearance
  3488. :version "24.1"
  3489. :type 'boolean)
  3490. (defvar org-emph-re nil
  3491. "Regular expression for matching emphasis.
  3492. After a match, the match groups contain these elements:
  3493. 0 The match of the full regular expression, including the characters
  3494. before and after the proper match
  3495. 1 The character before the proper match, or empty at beginning of line
  3496. 2 The proper match, including the leading and trailing markers
  3497. 3 The leading marker like * or /, indicating the type of highlighting
  3498. 4 The text between the emphasis markers, not including the markers
  3499. 5 The character after the match, empty at the end of a line")
  3500. (defvar org-verbatim-re nil
  3501. "Regular expression for matching verbatim text.")
  3502. (defvar org-emphasis-regexp-components) ; defined just below
  3503. (defvar org-emphasis-alist) ; defined just below
  3504. (defun org-set-emph-re (var val)
  3505. "Set variable and compute the emphasis regular expression."
  3506. (set var val)
  3507. (when (and (boundp 'org-emphasis-alist)
  3508. (boundp 'org-emphasis-regexp-components)
  3509. org-emphasis-alist org-emphasis-regexp-components)
  3510. (let* ((e org-emphasis-regexp-components)
  3511. (pre (car e))
  3512. (post (nth 1 e))
  3513. (border (nth 2 e))
  3514. (body (nth 3 e))
  3515. (nl (nth 4 e))
  3516. (body1 (concat body "*?"))
  3517. (markers (mapconcat 'car org-emphasis-alist ""))
  3518. (vmarkers (mapconcat
  3519. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3520. org-emphasis-alist "")))
  3521. ;; make sure special characters appear at the right position in the class
  3522. (if (string-match "\\^" markers)
  3523. (setq markers (concat (replace-match "" t t markers) "^")))
  3524. (if (string-match "-" markers)
  3525. (setq markers (concat (replace-match "" t t markers) "-")))
  3526. (if (string-match "\\^" vmarkers)
  3527. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3528. (if (string-match "-" vmarkers)
  3529. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3530. (if (> nl 0)
  3531. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3532. (int-to-string nl) "\\}")))
  3533. ;; Make the regexp
  3534. (setq org-emph-re
  3535. (concat "\\([" pre "]\\|^\\)"
  3536. "\\("
  3537. "\\([" markers "]\\)"
  3538. "\\("
  3539. "[^" border "]\\|"
  3540. "[^" border "]"
  3541. body1
  3542. "[^" border "]"
  3543. "\\)"
  3544. "\\3\\)"
  3545. "\\([" post "]\\|$\\)"))
  3546. (setq org-verbatim-re
  3547. (concat "\\([" pre "]\\|^\\)"
  3548. "\\("
  3549. "\\([" vmarkers "]\\)"
  3550. "\\("
  3551. "[^" border "]\\|"
  3552. "[^" border "]"
  3553. body1
  3554. "[^" border "]"
  3555. "\\)"
  3556. "\\3\\)"
  3557. "\\([" post "]\\|$\\)")))))
  3558. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3559. ;; set this option proved cumbersome. See this message/thread:
  3560. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3561. (defvar org-emphasis-regexp-components
  3562. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3563. "Components used to build the regular expression for emphasis.
  3564. This is a list with five entries. Terminology: In an emphasis string
  3565. like \" *strong word* \", we call the initial space PREMATCH, the final
  3566. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3567. and \"trong wor\" is the body. The different components in this variable
  3568. specify what is allowed/forbidden in each part:
  3569. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3570. post Chars allowed as postmatch. End of line will be allowed too.
  3571. border The chars *forbidden* as border characters.
  3572. body-regexp A regexp like \".\" to match a body character. Don't use
  3573. non-shy groups here, and don't allow newline here.
  3574. newline The maximum number of newlines allowed in an emphasis exp.
  3575. You need to reload Org or to restart Emacs after customizing this.")
  3576. (defcustom org-emphasis-alist
  3577. `(("*" bold)
  3578. ("/" italic)
  3579. ("_" underline)
  3580. ("=" org-code verbatim)
  3581. ("~" org-verbatim verbatim)
  3582. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3583. "Alist of characters and faces to emphasize text.
  3584. Text starting and ending with a special character will be emphasized,
  3585. for example *bold*, _underlined_ and /italic/. This variable sets the
  3586. marker characters and the face to be used by font-lock for highlighting
  3587. in Org-mode Emacs buffers.
  3588. You need to reload Org or to restart Emacs after customizing this."
  3589. :group 'org-appearance
  3590. :set 'org-set-emph-re
  3591. :version "24.4"
  3592. :package-version '(Org . "8.0")
  3593. :type '(repeat
  3594. (list
  3595. (string :tag "Marker character")
  3596. (choice
  3597. (face :tag "Font-lock-face")
  3598. (plist :tag "Face property list"))
  3599. (option (const verbatim)))))
  3600. (defvar org-protecting-blocks
  3601. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3602. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3603. This is needed for font-lock setup.")
  3604. ;;; Miscellaneous options
  3605. (defgroup org-completion nil
  3606. "Completion in Org-mode."
  3607. :tag "Org Completion"
  3608. :group 'org)
  3609. (defcustom org-completion-use-ido nil
  3610. "Non-nil means use ido completion wherever possible.
  3611. Note that `ido-mode' must be active for this variable to be relevant.
  3612. If you decide to turn this variable on, you might well want to turn off
  3613. `org-outline-path-complete-in-steps'.
  3614. See also `org-completion-use-iswitchb'."
  3615. :group 'org-completion
  3616. :type 'boolean)
  3617. (defcustom org-completion-use-iswitchb nil
  3618. "Non-nil means use iswitchb completion wherever possible.
  3619. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3620. If you decide to turn this variable on, you might well want to turn off
  3621. `org-outline-path-complete-in-steps'.
  3622. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3623. :group 'org-completion
  3624. :type 'boolean)
  3625. (defcustom org-completion-fallback-command 'hippie-expand
  3626. "The expansion command called by \\[pcomplete] in normal context.
  3627. Normal means, no org-mode-specific context."
  3628. :group 'org-completion
  3629. :type 'function)
  3630. ;;; Functions and variables from their packages
  3631. ;; Declared here to avoid compiler warnings
  3632. ;; XEmacs only
  3633. (defvar outline-mode-menu-heading)
  3634. (defvar outline-mode-menu-show)
  3635. (defvar outline-mode-menu-hide)
  3636. (defvar zmacs-regions) ; XEmacs regions
  3637. ;; Emacs only
  3638. (defvar mark-active)
  3639. ;; Various packages
  3640. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3641. (declare-function calendar-forward-day "cal-move" (arg))
  3642. (declare-function calendar-goto-date "cal-move" (date))
  3643. (declare-function calendar-goto-today "cal-move" ())
  3644. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3645. (defvar calc-embedded-close-formula)
  3646. (defvar calc-embedded-open-formula)
  3647. (declare-function cdlatex-tab "ext:cdlatex" ())
  3648. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3649. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3650. (defvar font-lock-unfontify-region-function)
  3651. (declare-function iswitchb-read-buffer "iswitchb"
  3652. (prompt &optional default require-match start matches-set))
  3653. (defvar iswitchb-temp-buflist)
  3654. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3655. (defvar org-agenda-tags-todo-honor-ignore-options)
  3656. (declare-function org-agenda-skip "org-agenda" ())
  3657. (declare-function
  3658. org-agenda-format-item "org-agenda"
  3659. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3660. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3661. (declare-function org-agenda-change-all-lines "org-agenda"
  3662. (newhead hdmarker &optional fixface just-this))
  3663. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3664. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3665. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3666. (beg end))
  3667. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3668. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3669. "org-agenda" (&optional end))
  3670. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3671. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3672. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3673. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3674. (declare-function org-indent-mode "org-indent" (&optional arg))
  3675. (declare-function parse-time-string "parse-time" (string))
  3676. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3677. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3678. (defvar remember-data-file)
  3679. (defvar texmathp-why)
  3680. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3681. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3682. (defvar org-latex-regexps)
  3683. ;;; Autoload and prepare some org modules
  3684. ;; Some table stuff that needs to be defined here, because it is used
  3685. ;; by the functions setting up org-mode or checking for table context.
  3686. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3687. "Detect an org-type or table-type table.")
  3688. (defconst org-table-line-regexp "^[ \t]*|"
  3689. "Detect an org-type table line.")
  3690. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3691. "Detect an org-type table line.")
  3692. (defconst org-table-hline-regexp "^[ \t]*|-"
  3693. "Detect an org-type table hline.")
  3694. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3695. "Detect a table-type table hline.")
  3696. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3697. "Detect the first line outside a table when searching from within it.
  3698. This works for both table types.")
  3699. ;; Autoload the functions in org-table.el that are needed by functions here.
  3700. (eval-and-compile
  3701. (org-autoload "org-table"
  3702. '(org-table-begin org-table-blank-field org-table-end)))
  3703. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  3704. "Detect a #+TBLFM line.")
  3705. ;;;###autoload
  3706. (defun turn-on-orgtbl ()
  3707. "Unconditionally turn on `orgtbl-mode'."
  3708. (require 'org-table)
  3709. (orgtbl-mode 1))
  3710. (defun org-at-table-p (&optional table-type)
  3711. "Return t if the cursor is inside an org-type table.
  3712. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3713. (if org-enable-table-editor
  3714. (save-excursion
  3715. (beginning-of-line 1)
  3716. (looking-at (if table-type org-table-any-line-regexp
  3717. org-table-line-regexp)))
  3718. nil))
  3719. (defsubst org-table-p () (org-at-table-p))
  3720. (defun org-at-table.el-p ()
  3721. "Return t if and only if we are at a table.el table."
  3722. (and (org-at-table-p 'any)
  3723. (save-excursion
  3724. (goto-char (org-table-begin 'any))
  3725. (looking-at org-table1-hline-regexp))))
  3726. (defun org-table-recognize-table.el ()
  3727. "If there is a table.el table nearby, recognize it and move into it."
  3728. (if org-table-tab-recognizes-table.el
  3729. (if (org-at-table.el-p)
  3730. (progn
  3731. (beginning-of-line 1)
  3732. (if (looking-at org-table-dataline-regexp)
  3733. nil
  3734. (if (looking-at org-table1-hline-regexp)
  3735. (progn
  3736. (beginning-of-line 2)
  3737. (if (looking-at org-table-any-border-regexp)
  3738. (beginning-of-line -1)))))
  3739. (if (re-search-forward "|" (org-table-end t) t)
  3740. (progn
  3741. (require 'table)
  3742. (if (table--at-cell-p (point))
  3743. t
  3744. (message "recognizing table.el table...")
  3745. (table-recognize-table)
  3746. (message "recognizing table.el table...done")))
  3747. (error "This should not happen"))
  3748. t)
  3749. nil)
  3750. nil))
  3751. (defun org-at-table-hline-p ()
  3752. "Return t if the cursor is inside a hline in a table."
  3753. (if org-enable-table-editor
  3754. (save-excursion
  3755. (beginning-of-line 1)
  3756. (looking-at org-table-hline-regexp))
  3757. nil))
  3758. (defun org-table-map-tables (function &optional quietly)
  3759. "Apply FUNCTION to the start of all tables in the buffer."
  3760. (save-excursion
  3761. (save-restriction
  3762. (widen)
  3763. (goto-char (point-min))
  3764. (while (re-search-forward org-table-any-line-regexp nil t)
  3765. (unless quietly
  3766. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3767. (beginning-of-line 1)
  3768. (when (and (looking-at org-table-line-regexp)
  3769. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3770. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3771. (save-excursion (funcall function))
  3772. (or (looking-at org-table-line-regexp)
  3773. (forward-char 1)))
  3774. (re-search-forward org-table-any-border-regexp nil 1))))
  3775. (unless quietly (message "Mapping tables: done")))
  3776. ;; Declare and autoload functions from org-agenda.el
  3777. (eval-and-compile
  3778. (org-autoload "org-agenda"
  3779. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3780. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3781. (declare-function org-clock-update-mode-line "org-clock" ())
  3782. (declare-function org-resolve-clocks "org-clock"
  3783. (&optional also-non-dangling-p prompt last-valid))
  3784. (defun org-at-TBLFM-p (&optional pos)
  3785. "Return t when point (or POS) is in #+TBLFM line."
  3786. (save-excursion
  3787. (let ((pos pos)))
  3788. (goto-char (or pos (point)))
  3789. (beginning-of-line 1)
  3790. (looking-at org-TBLFM-regexp)))
  3791. (defvar org-clock-start-time)
  3792. (defvar org-clock-marker (make-marker)
  3793. "Marker recording the last clock-in.")
  3794. (defvar org-clock-hd-marker (make-marker)
  3795. "Marker recording the last clock-in, but the headline position.")
  3796. (defvar org-clock-heading ""
  3797. "The heading of the current clock entry.")
  3798. (defun org-clock-is-active ()
  3799. "Return the buffer where the clock is currently running.
  3800. Return nil if no clock is running."
  3801. (marker-buffer org-clock-marker))
  3802. (eval-and-compile
  3803. (org-autoload "org-clock" '(org-clock-remove-overlays
  3804. org-clock-update-time-maybe
  3805. org-clocktable-shift)))
  3806. (defun org-check-running-clock ()
  3807. "Check if the current buffer contains the running clock.
  3808. If yes, offer to stop it and to save the buffer with the changes."
  3809. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3810. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3811. (buffer-name))))
  3812. (org-clock-out)
  3813. (when (y-or-n-p "Save changed buffer?")
  3814. (save-buffer))))
  3815. (defun org-clocktable-try-shift (dir n)
  3816. "Check if this line starts a clock table, if yes, shift the time block."
  3817. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3818. (org-clocktable-shift dir n)))
  3819. ;;;###autoload
  3820. (defun org-clock-persistence-insinuate ()
  3821. "Set up hooks for clock persistence."
  3822. (require 'org-clock)
  3823. (add-hook 'org-mode-hook 'org-clock-load)
  3824. (add-hook 'kill-emacs-hook 'org-clock-save))
  3825. ;; Define the variable already here, to make sure we have it.
  3826. (defvar org-indent-mode nil
  3827. "Non-nil if Org-Indent mode is enabled.
  3828. Use the command `org-indent-mode' to change this variable.")
  3829. ;; Autoload archiving code
  3830. ;; The stuff that is needed for cycling and tags has to be defined here.
  3831. (defgroup org-archive nil
  3832. "Options concerning archiving in Org-mode."
  3833. :tag "Org Archive"
  3834. :group 'org-structure)
  3835. (defcustom org-archive-location "%s_archive::"
  3836. "The location where subtrees should be archived.
  3837. The value of this variable is a string, consisting of two parts,
  3838. separated by a double-colon. The first part is a filename and
  3839. the second part is a headline.
  3840. When the filename is omitted, archiving happens in the same file.
  3841. %s in the filename will be replaced by the current file
  3842. name (without the directory part). Archiving to a different file
  3843. is useful to keep archived entries from contributing to the
  3844. Org-mode Agenda.
  3845. The archived entries will be filed as subtrees of the specified
  3846. headline. When the headline is omitted, the subtrees are simply
  3847. filed away at the end of the file, as top-level entries. Also in
  3848. the heading you can use %s to represent the file name, this can be
  3849. useful when using the same archive for a number of different files.
  3850. Here are a few examples:
  3851. \"%s_archive::\"
  3852. If the current file is Projects.org, archive in file
  3853. Projects.org_archive, as top-level trees. This is the default.
  3854. \"::* Archived Tasks\"
  3855. Archive in the current file, under the top-level headline
  3856. \"* Archived Tasks\".
  3857. \"~/org/archive.org::\"
  3858. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3859. \"~/org/archive.org::* From %s\"
  3860. Archive in file ~/org/archive.org (absolute path), under headlines
  3861. \"From FILENAME\" where file name is the current file name.
  3862. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3863. The \"datetree/\" string is special, signifying to archive
  3864. items to the datetree. Items are placed in either the CLOSED
  3865. date of the item, or the current date if there is no CLOSED date.
  3866. The heading will be a subentry to the current date. There doesn't
  3867. need to be a heading, but there always needs to be a slash after
  3868. datetree. For example, to store archived items directly in the
  3869. datetree, use \"~/org/datetree.org::datetree/\".
  3870. \"basement::** Finished Tasks\"
  3871. Archive in file ./basement (relative path), as level 3 trees
  3872. below the level 2 heading \"** Finished Tasks\".
  3873. You may set this option on a per-file basis by adding to the buffer a
  3874. line like
  3875. #+ARCHIVE: basement::** Finished Tasks
  3876. You may also define it locally for a subtree by setting an ARCHIVE property
  3877. in the entry. If such a property is found in an entry, or anywhere up
  3878. the hierarchy, it will be used."
  3879. :group 'org-archive
  3880. :type 'string)
  3881. (defcustom org-archive-tag "ARCHIVE"
  3882. "The tag that marks a subtree as archived.
  3883. An archived subtree does not open during visibility cycling, and does
  3884. not contribute to the agenda listings.
  3885. After changing this, font-lock must be restarted in the relevant buffers to
  3886. get the proper fontification."
  3887. :group 'org-archive
  3888. :group 'org-keywords
  3889. :type 'string)
  3890. (defcustom org-agenda-skip-archived-trees t
  3891. "Non-nil means the agenda will skip any items located in archived trees.
  3892. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3893. variable is no longer recommended, you should leave it at the value t.
  3894. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3895. :group 'org-archive
  3896. :group 'org-agenda-skip
  3897. :type 'boolean)
  3898. (defcustom org-columns-skip-archived-trees t
  3899. "Non-nil means ignore archived trees when creating column view."
  3900. :group 'org-archive
  3901. :group 'org-properties
  3902. :type 'boolean)
  3903. (defcustom org-cycle-open-archived-trees nil
  3904. "Non-nil means `org-cycle' will open archived trees.
  3905. An archived tree is a tree marked with the tag ARCHIVE.
  3906. When nil, archived trees will stay folded. You can still open them with
  3907. normal outline commands like `show-all', but not with the cycling commands."
  3908. :group 'org-archive
  3909. :group 'org-cycle
  3910. :type 'boolean)
  3911. (defcustom org-sparse-tree-open-archived-trees nil
  3912. "Non-nil means sparse tree construction shows matches in archived trees.
  3913. When nil, matches in these trees are highlighted, but the trees are kept in
  3914. collapsed state."
  3915. :group 'org-archive
  3916. :group 'org-sparse-trees
  3917. :type 'boolean)
  3918. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3919. "The default date type when building a sparse tree.
  3920. When this is nil, a date is a scheduled or a deadline timestamp.
  3921. Otherwise, these types are allowed:
  3922. all: all timestamps
  3923. active: only active timestamps (<...>)
  3924. inactive: only inactive timestamps (<...)
  3925. scheduled: only scheduled timestamps
  3926. deadline: only deadline timestamps"
  3927. :type '(choice (const :tag "Scheduled or deadline" scheduled-or-deadline)
  3928. (const :tag "All timestamps" all)
  3929. (const :tag "Only active timestamps" active)
  3930. (const :tag "Only inactive timestamps" inactive)
  3931. (const :tag "Only scheduled timestamps" scheduled)
  3932. (const :tag "Only deadline timestamps" deadline)
  3933. (const :tag "Only closed timestamps" closed))
  3934. :version "24.3"
  3935. :group 'org-sparse-trees)
  3936. (defun org-cycle-hide-archived-subtrees (state)
  3937. "Re-hide all archived subtrees after a visibility state change."
  3938. (when (and (not org-cycle-open-archived-trees)
  3939. (not (memq state '(overview folded))))
  3940. (save-excursion
  3941. (let* ((globalp (memq state '(contents all)))
  3942. (beg (if globalp (point-min) (point)))
  3943. (end (if globalp (point-max) (org-end-of-subtree t))))
  3944. (org-hide-archived-subtrees beg end)
  3945. (goto-char beg)
  3946. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3947. (message "%s" (substitute-command-keys
  3948. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3949. (defun org-force-cycle-archived ()
  3950. "Cycle subtree even if it is archived."
  3951. (interactive)
  3952. (setq this-command 'org-cycle)
  3953. (let ((org-cycle-open-archived-trees t))
  3954. (call-interactively 'org-cycle)))
  3955. (defun org-hide-archived-subtrees (beg end)
  3956. "Re-hide all archived subtrees after a visibility state change."
  3957. (save-excursion
  3958. (let* ((re (concat ":" org-archive-tag ":")))
  3959. (goto-char beg)
  3960. (while (re-search-forward re end t)
  3961. (when (org-at-heading-p)
  3962. (org-flag-subtree t)
  3963. (org-end-of-subtree t))))))
  3964. (declare-function outline-end-of-heading "outline" ())
  3965. (declare-function outline-flag-region "outline" (from to flag))
  3966. (defun org-flag-subtree (flag)
  3967. (save-excursion
  3968. (org-back-to-heading t)
  3969. (outline-end-of-heading)
  3970. (outline-flag-region (point)
  3971. (progn (org-end-of-subtree t) (point))
  3972. flag)))
  3973. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3974. (eval-and-compile
  3975. (org-autoload "org-archive"
  3976. '(org-add-archive-files)))
  3977. ;; Autoload Column View Code
  3978. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3979. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3980. (declare-function org-columns-compute "org-colview" (property))
  3981. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3982. '(org-columns-number-to-string
  3983. org-columns-get-format-and-top-level
  3984. org-columns-compute
  3985. org-columns-remove-overlays))
  3986. ;; Autoload ID code
  3987. (declare-function org-id-store-link "org-id")
  3988. (declare-function org-id-locations-load "org-id")
  3989. (declare-function org-id-locations-save "org-id")
  3990. (defvar org-id-track-globally)
  3991. (org-autoload "org-id"
  3992. '(org-id-new
  3993. org-id-copy
  3994. org-id-get-with-outline-path-completion
  3995. org-id-get-with-outline-drilling))
  3996. ;;; Variables for pre-computed regular expressions, all buffer local
  3997. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3998. "Matches first line of a hidden block.")
  3999. (make-variable-buffer-local 'org-drawer-regexp)
  4000. (defvar org-todo-regexp nil
  4001. "Matches any of the TODO state keywords.")
  4002. (make-variable-buffer-local 'org-todo-regexp)
  4003. (defvar org-not-done-regexp nil
  4004. "Matches any of the TODO state keywords except the last one.")
  4005. (make-variable-buffer-local 'org-not-done-regexp)
  4006. (defvar org-not-done-heading-regexp nil
  4007. "Matches a TODO headline that is not done.")
  4008. (make-variable-buffer-local 'org-not-done-regexp)
  4009. (defvar org-todo-line-regexp nil
  4010. "Matches a headline and puts TODO state into group 2 if present.")
  4011. (make-variable-buffer-local 'org-todo-line-regexp)
  4012. (defvar org-complex-heading-regexp nil
  4013. "Matches a headline and puts everything into groups:
  4014. group 1: the stars
  4015. group 2: The todo keyword, maybe
  4016. group 3: Priority cookie
  4017. group 4: True headline
  4018. group 5: Tags")
  4019. (make-variable-buffer-local 'org-complex-heading-regexp)
  4020. (defvar org-complex-heading-regexp-format nil
  4021. "Printf format to make regexp to match an exact headline.
  4022. This regexp will match the headline of any node which has the
  4023. exact headline text that is put into the format, but may have any
  4024. TODO state, priority and tags.")
  4025. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4026. (defvar org-todo-line-tags-regexp nil
  4027. "Matches a headline and puts TODO state into group 2 if present.
  4028. Also put tags into group 4 if tags are present.")
  4029. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4030. (defvar org-ds-keyword-length 12
  4031. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4032. (make-variable-buffer-local 'org-ds-keyword-length)
  4033. (defvar org-deadline-regexp nil
  4034. "Matches the DEADLINE keyword.")
  4035. (make-variable-buffer-local 'org-deadline-regexp)
  4036. (defvar org-deadline-time-regexp nil
  4037. "Matches the DEADLINE keyword together with a time stamp.")
  4038. (make-variable-buffer-local 'org-deadline-time-regexp)
  4039. (defvar org-deadline-time-hour-regexp nil
  4040. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4041. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4042. (defvar org-deadline-line-regexp nil
  4043. "Matches the DEADLINE keyword and the rest of the line.")
  4044. (make-variable-buffer-local 'org-deadline-line-regexp)
  4045. (defvar org-scheduled-regexp nil
  4046. "Matches the SCHEDULED keyword.")
  4047. (make-variable-buffer-local 'org-scheduled-regexp)
  4048. (defvar org-scheduled-time-regexp nil
  4049. "Matches the SCHEDULED keyword together with a time stamp.")
  4050. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4051. (defvar org-scheduled-time-hour-regexp nil
  4052. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4053. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4054. (defvar org-closed-time-regexp nil
  4055. "Matches the CLOSED keyword together with a time stamp.")
  4056. (make-variable-buffer-local 'org-closed-time-regexp)
  4057. (defvar org-keyword-time-regexp nil
  4058. "Matches any of the 4 keywords, together with the time stamp.")
  4059. (make-variable-buffer-local 'org-keyword-time-regexp)
  4060. (defvar org-keyword-time-not-clock-regexp nil
  4061. "Matches any of the 3 keywords, together with the time stamp.")
  4062. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4063. (defvar org-maybe-keyword-time-regexp nil
  4064. "Matches a timestamp, possibly preceded by a keyword.")
  4065. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4066. (defvar org-all-time-keywords nil
  4067. "List of time keywords.")
  4068. (make-variable-buffer-local 'org-all-time-keywords)
  4069. (defconst org-plain-time-of-day-regexp
  4070. (concat
  4071. "\\(\\<[012]?[0-9]"
  4072. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4073. "\\(--?"
  4074. "\\(\\<[012]?[0-9]"
  4075. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4076. "\\)?")
  4077. "Regular expression to match a plain time or time range.
  4078. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4079. groups carry important information:
  4080. 0 the full match
  4081. 1 the first time, range or not
  4082. 8 the second time, if it is a range.")
  4083. (defconst org-plain-time-extension-regexp
  4084. (concat
  4085. "\\(\\<[012]?[0-9]"
  4086. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4087. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4088. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4089. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4090. groups carry important information:
  4091. 0 the full match
  4092. 7 hours of duration
  4093. 9 minutes of duration")
  4094. (defconst org-stamp-time-of-day-regexp
  4095. (concat
  4096. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4097. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4098. "\\(--?"
  4099. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4100. "Regular expression to match a timestamp time or time range.
  4101. After a match, the following groups carry important information:
  4102. 0 the full match
  4103. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4104. 2 the first time, range or not
  4105. 4 the second time, if it is a range.")
  4106. (defconst org-startup-options
  4107. '(("fold" org-startup-folded t)
  4108. ("overview" org-startup-folded t)
  4109. ("nofold" org-startup-folded nil)
  4110. ("showall" org-startup-folded nil)
  4111. ("showeverything" org-startup-folded showeverything)
  4112. ("content" org-startup-folded content)
  4113. ("indent" org-startup-indented t)
  4114. ("noindent" org-startup-indented nil)
  4115. ("hidestars" org-hide-leading-stars t)
  4116. ("showstars" org-hide-leading-stars nil)
  4117. ("odd" org-odd-levels-only t)
  4118. ("oddeven" org-odd-levels-only nil)
  4119. ("align" org-startup-align-all-tables t)
  4120. ("noalign" org-startup-align-all-tables nil)
  4121. ("inlineimages" org-startup-with-inline-images t)
  4122. ("noinlineimages" org-startup-with-inline-images nil)
  4123. ("latexpreview" org-startup-with-latex-preview t)
  4124. ("nolatexpreview" org-startup-with-latex-preview nil)
  4125. ("customtime" org-display-custom-times t)
  4126. ("logdone" org-log-done time)
  4127. ("lognotedone" org-log-done note)
  4128. ("nologdone" org-log-done nil)
  4129. ("lognoteclock-out" org-log-note-clock-out t)
  4130. ("nolognoteclock-out" org-log-note-clock-out nil)
  4131. ("logrepeat" org-log-repeat state)
  4132. ("lognoterepeat" org-log-repeat note)
  4133. ("logdrawer" org-log-into-drawer t)
  4134. ("nologdrawer" org-log-into-drawer nil)
  4135. ("logstatesreversed" org-log-states-order-reversed t)
  4136. ("nologstatesreversed" org-log-states-order-reversed nil)
  4137. ("nologrepeat" org-log-repeat nil)
  4138. ("logreschedule" org-log-reschedule time)
  4139. ("lognotereschedule" org-log-reschedule note)
  4140. ("nologreschedule" org-log-reschedule nil)
  4141. ("logredeadline" org-log-redeadline time)
  4142. ("lognoteredeadline" org-log-redeadline note)
  4143. ("nologredeadline" org-log-redeadline nil)
  4144. ("logrefile" org-log-refile time)
  4145. ("lognoterefile" org-log-refile note)
  4146. ("nologrefile" org-log-refile nil)
  4147. ("fninline" org-footnote-define-inline t)
  4148. ("nofninline" org-footnote-define-inline nil)
  4149. ("fnlocal" org-footnote-section nil)
  4150. ("fnauto" org-footnote-auto-label t)
  4151. ("fnprompt" org-footnote-auto-label nil)
  4152. ("fnconfirm" org-footnote-auto-label confirm)
  4153. ("fnplain" org-footnote-auto-label plain)
  4154. ("fnadjust" org-footnote-auto-adjust t)
  4155. ("nofnadjust" org-footnote-auto-adjust nil)
  4156. ("constcgs" constants-unit-system cgs)
  4157. ("constSI" constants-unit-system SI)
  4158. ("noptag" org-tag-persistent-alist nil)
  4159. ("hideblocks" org-hide-block-startup t)
  4160. ("nohideblocks" org-hide-block-startup nil)
  4161. ("beamer" org-startup-with-beamer-mode t)
  4162. ("entitiespretty" org-pretty-entities t)
  4163. ("entitiesplain" org-pretty-entities nil))
  4164. "Variable associated with STARTUP options for org-mode.
  4165. Each element is a list of three items: the startup options (as written
  4166. in the #+STARTUP line), the corresponding variable, and the value to set
  4167. this variable to if the option is found. An optional forth element PUSH
  4168. means to push this value onto the list in the variable.")
  4169. (defun org-update-property-plist (key val props)
  4170. "Update PROPS with KEY and VAL."
  4171. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4172. (key (if appending (substring key 0 (- (length key) 1)) key))
  4173. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4174. (previous (cdr (assoc key props))))
  4175. (if appending
  4176. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4177. (cons (cons key val) remainder))))
  4178. (defconst org-block-regexp
  4179. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4180. "Regular expression for hiding blocks.")
  4181. (defconst org-heading-keyword-regexp-format
  4182. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4183. "Printf format for a regexp matching a headline with some keyword.
  4184. This regexp will match the headline of any node which has the
  4185. exact keyword that is put into the format. The keyword isn't in
  4186. any group by default, but the stars and the body are.")
  4187. (defconst org-heading-keyword-maybe-regexp-format
  4188. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4189. "Printf format for a regexp matching a headline, possibly with some keyword.
  4190. This regexp can match any headline with the specified keyword, or
  4191. without a keyword. The keyword isn't in any group by default,
  4192. but the stars and the body are.")
  4193. (defcustom org-group-tags t
  4194. "When non-nil (the default), use group tags.
  4195. This can be turned on/off through `org-toggle-tags-groups'."
  4196. :group 'org-tags
  4197. :group 'org-startup
  4198. :type 'boolean)
  4199. (defun org-toggle-tags-groups ()
  4200. "Toggle support for group tags.
  4201. Support for group tags is controlled by the option
  4202. `org-group-tags', which is non-nil by default."
  4203. (interactive)
  4204. (setq org-group-tags (not org-group-tags))
  4205. (cond ((and (derived-mode-p 'org-agenda-mode)
  4206. org-group-tags)
  4207. (org-agenda-redo))
  4208. ((derived-mode-p 'org-mode)
  4209. (let ((org-inhibit-startup t)) (org-mode))))
  4210. (message "Groups tags support has been turned %s"
  4211. (if org-group-tags "on" "off")))
  4212. (defun org-set-regexps-and-options-for-tags ()
  4213. "Precompute variables used for tags."
  4214. (when (derived-mode-p 'org-mode)
  4215. (org-set-local 'org-file-tags nil)
  4216. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4217. (splitre "[ \t]+")
  4218. (start 0)
  4219. tags ftags key value)
  4220. (save-excursion
  4221. (save-restriction
  4222. (widen)
  4223. (goto-char (point-min))
  4224. (while (re-search-forward re nil t)
  4225. (setq key (upcase (org-match-string-no-properties 1))
  4226. value (org-match-string-no-properties 2))
  4227. (if (stringp value) (setq value (org-trim value)))
  4228. (cond
  4229. ((equal key "TAGS")
  4230. (setq tags (append tags (if tags '("\\n") nil)
  4231. (org-split-string value splitre))))
  4232. ((equal key "FILETAGS")
  4233. (when (string-match "\\S-" value)
  4234. (setq ftags
  4235. (append
  4236. ftags
  4237. (apply 'append
  4238. (mapcar (lambda (x) (org-split-string x ":"))
  4239. (org-split-string value)))))))))))
  4240. ;; Process the file tags.
  4241. (and ftags (org-set-local 'org-file-tags
  4242. (mapcar 'org-add-prop-inherited ftags)))
  4243. (org-set-local 'org-tag-groups-alist nil)
  4244. ;; Process the tags.
  4245. ;; FIXME
  4246. (when tags
  4247. (let (e tgs g)
  4248. (while (setq e (pop tags))
  4249. (cond
  4250. ((equal e "{")
  4251. (progn (push '(:startgroup) tgs)
  4252. (when (equal (nth 1 tags) ":")
  4253. (push (list (replace-regexp-in-string
  4254. "(.+)$" "" (nth 0 tags)))
  4255. org-tag-groups-alist)
  4256. (setq g 0))))
  4257. ((equal e ":") (push '(:grouptags) tgs))
  4258. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4259. ((equal e "\\n") (push '(:newline) tgs))
  4260. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4261. (push (cons (match-string 1 e)
  4262. (string-to-char (match-string 2 e))) tgs)
  4263. (if (and g (> g 0))
  4264. (setcar org-tag-groups-alist
  4265. (append (car org-tag-groups-alist)
  4266. (list (match-string 1 e)))))
  4267. (if g (setq g (1+ g))))
  4268. (t (push (list e) tgs)
  4269. (if (and g (> g 0))
  4270. (setcar org-tag-groups-alist
  4271. (append (car org-tag-groups-alist) (list e))))
  4272. (if g (setq g (1+ g))))))
  4273. (org-set-local 'org-tag-alist nil)
  4274. (while (setq e (pop tgs))
  4275. (or (and (stringp (car e))
  4276. (assoc (car e) org-tag-alist))
  4277. (push e org-tag-alist)))
  4278. ;; Return a list with tag variables
  4279. (list org-file-tags org-tag-alist org-tag-groups-alist))))))
  4280. (defvar org-ota nil)
  4281. (defun org-set-regexps-and-options ()
  4282. "Precompute regular expressions used in the current buffer."
  4283. (when (derived-mode-p 'org-mode)
  4284. (org-set-local 'org-todo-kwd-alist nil)
  4285. (org-set-local 'org-todo-key-alist nil)
  4286. (org-set-local 'org-todo-key-trigger nil)
  4287. (org-set-local 'org-todo-keywords-1 nil)
  4288. (org-set-local 'org-done-keywords nil)
  4289. (org-set-local 'org-todo-heads nil)
  4290. (org-set-local 'org-todo-sets nil)
  4291. (org-set-local 'org-todo-log-states nil)
  4292. (org-set-local 'org-file-properties nil)
  4293. (let ((re (org-make-options-regexp
  4294. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4295. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4296. "SETUPFILE" "OPTIONS")
  4297. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4298. (splitre "[ \t]+")
  4299. (scripts org-use-sub-superscripts)
  4300. kwds kws0 kwsa key log value cat arch const links hw dws
  4301. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4302. (start 0))
  4303. (save-excursion
  4304. (save-restriction
  4305. (widen)
  4306. (goto-char (point-min))
  4307. (while
  4308. (or (and
  4309. ext-setup-or-nil
  4310. (not org-ota)
  4311. (let (ret)
  4312. (with-temp-buffer
  4313. (insert ext-setup-or-nil)
  4314. (let ((major-mode 'org-mode) org-ota)
  4315. (setq ret (save-match-data
  4316. (org-set-regexps-and-options-for-tags)))))
  4317. ;; Append setupfile tags to existing tags
  4318. (setq org-file-tags
  4319. (delq nil (append org-file-tags (nth 0 ret)))
  4320. org-tag-alist
  4321. (delq nil (append org-tag-alist (nth 1 ret)))
  4322. org-tag-groups-alist
  4323. (delq nil (append org-tag-groups-alist (nth 2 ret)))
  4324. org-ota t)))
  4325. (and ext-setup-or-nil
  4326. (string-match re ext-setup-or-nil start)
  4327. (setq start (match-end 0)))
  4328. (and (setq ext-setup-or-nil nil start 0)
  4329. (re-search-forward re nil t)))
  4330. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4331. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4332. (if (stringp value) (setq value (org-trim value)))
  4333. (cond
  4334. ((equal key "CATEGORY")
  4335. (setq cat value))
  4336. ((member key '("SEQ_TODO" "TODO"))
  4337. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4338. ((equal key "TYP_TODO")
  4339. (push (cons 'type (org-split-string value splitre)) kwds))
  4340. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4341. ;; general TODO-like setup
  4342. (push (cons (intern (downcase (match-string 1 key)))
  4343. (org-split-string value splitre)) kwds))
  4344. ((equal key "COLUMNS")
  4345. (org-set-local 'org-columns-default-format value))
  4346. ((equal key "LINK")
  4347. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4348. (push (cons (match-string 1 value)
  4349. (org-trim (match-string 2 value)))
  4350. links)))
  4351. ((equal key "PRIORITIES")
  4352. (setq prio (org-split-string value " +")))
  4353. ((equal key "PROPERTY")
  4354. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4355. (setq props (org-update-property-plist (match-string 1 value)
  4356. (match-string 2 value)
  4357. props))))
  4358. ((equal key "DRAWERS")
  4359. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4360. ((equal key "CONSTANTS")
  4361. (org-table-set-constants))
  4362. ((equal key "STARTUP")
  4363. (let ((opts (org-split-string value splitre))
  4364. l var val)
  4365. (while (setq l (pop opts))
  4366. (when (setq l (assoc l org-startup-options))
  4367. (setq var (nth 1 l) val (nth 2 l))
  4368. (if (not (nth 3 l))
  4369. (set (make-local-variable var) val)
  4370. (if (not (listp (symbol-value var)))
  4371. (set (make-local-variable var) nil))
  4372. (set (make-local-variable var) (symbol-value var))
  4373. (add-to-list var val))))))
  4374. ((equal key "ARCHIVE")
  4375. (setq arch value)
  4376. (remove-text-properties 0 (length arch)
  4377. '(face t fontified t) arch))
  4378. ((equal key "OPTIONS")
  4379. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4380. (setq scripts (read (match-string 2 value)))))
  4381. ((and (equal key "SETUPFILE")
  4382. ;; Prevent checking in Gnus messages
  4383. (not buffer-read-only))
  4384. (setq setup-contents (org-file-contents
  4385. (expand-file-name
  4386. (org-remove-double-quotes value))
  4387. 'noerror))
  4388. (if (not ext-setup-or-nil)
  4389. (setq ext-setup-or-nil setup-contents start 0)
  4390. (setq ext-setup-or-nil
  4391. (concat (substring ext-setup-or-nil 0 start)
  4392. "\n" setup-contents "\n"
  4393. (substring ext-setup-or-nil start)))))))
  4394. ;; search for property blocks
  4395. (goto-char (point-min))
  4396. (while (re-search-forward org-block-regexp nil t)
  4397. (when (equal "PROPERTY" (upcase (match-string 1)))
  4398. (setq value (replace-regexp-in-string
  4399. "[\n\r]" " " (match-string 4)))
  4400. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4401. (setq props (org-update-property-plist (match-string 1 value)
  4402. (match-string 2 value)
  4403. props)))))))
  4404. (org-set-local 'org-use-sub-superscripts scripts)
  4405. (when cat
  4406. (org-set-local 'org-category (intern cat))
  4407. (push (cons "CATEGORY" cat) props))
  4408. (when prio
  4409. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4410. (setq prio (mapcar 'string-to-char prio))
  4411. (org-set-local 'org-highest-priority (nth 0 prio))
  4412. (org-set-local 'org-lowest-priority (nth 1 prio))
  4413. (org-set-local 'org-default-priority (nth 2 prio)))
  4414. (and props (org-set-local 'org-file-properties (nreverse props)))
  4415. (and drawers (org-set-local 'org-drawers drawers))
  4416. (and arch (org-set-local 'org-archive-location arch))
  4417. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4418. ;; Process the TODO keywords
  4419. (unless kwds
  4420. ;; Use the global values as if they had been given locally.
  4421. (setq kwds (default-value 'org-todo-keywords))
  4422. (if (stringp (car kwds))
  4423. (setq kwds (list (cons org-todo-interpretation
  4424. (default-value 'org-todo-keywords)))))
  4425. (setq kwds (reverse kwds)))
  4426. (setq kwds (nreverse kwds))
  4427. (let (inter kws kw)
  4428. (while (setq kws (pop kwds))
  4429. (let ((kws (or
  4430. (run-hook-with-args-until-success
  4431. 'org-todo-setup-filter-hook kws)
  4432. kws)))
  4433. (setq inter (pop kws) sep (member "|" kws)
  4434. kws0 (delete "|" (copy-sequence kws))
  4435. kwsa nil
  4436. kws1 (mapcar
  4437. (lambda (x)
  4438. ;; 1 2
  4439. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4440. (progn
  4441. (setq kw (match-string 1 x)
  4442. key (and (match-end 2) (match-string 2 x))
  4443. log (org-extract-log-state-settings x))
  4444. (push (cons kw (and key (string-to-char key))) kwsa)
  4445. (and log (push log org-todo-log-states))
  4446. kw)
  4447. (error "Invalid TODO keyword %s" x)))
  4448. kws0)
  4449. kwsa (if kwsa (append '((:startgroup))
  4450. (nreverse kwsa)
  4451. '((:endgroup))))
  4452. hw (car kws1)
  4453. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4454. tail (list inter hw (car dws) (org-last dws))))
  4455. (add-to-list 'org-todo-heads hw 'append)
  4456. (push kws1 org-todo-sets)
  4457. (setq org-done-keywords (append org-done-keywords dws nil))
  4458. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4459. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4460. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4461. (setq org-todo-sets (nreverse org-todo-sets)
  4462. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4463. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4464. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4465. ;; Compute the regular expressions and other local variables.
  4466. ;; Using `org-outline-regexp-bol' would complicate them much,
  4467. ;; because of the fixed white space at the end of that string.
  4468. (if (not org-done-keywords)
  4469. (setq org-done-keywords (and org-todo-keywords-1
  4470. (list (org-last org-todo-keywords-1)))))
  4471. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4472. (length org-scheduled-string)
  4473. (length org-clock-string)
  4474. (length org-closed-string)))
  4475. org-drawer-regexp
  4476. (concat "^[ \t]*:\\("
  4477. (mapconcat 'regexp-quote org-drawers "\\|")
  4478. "\\):[ \t]*$")
  4479. org-not-done-keywords
  4480. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4481. org-todo-regexp
  4482. (concat "\\("
  4483. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4484. "\\)")
  4485. org-not-done-regexp
  4486. (concat "\\("
  4487. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4488. "\\)")
  4489. org-not-done-heading-regexp
  4490. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4491. org-todo-line-regexp
  4492. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4493. org-complex-heading-regexp
  4494. (concat "^\\(\\*+\\)"
  4495. "\\(?: +" org-todo-regexp "\\)?"
  4496. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4497. "\\(?: +\\(.*?\\)\\)??"
  4498. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4499. "[ \t]*$")
  4500. org-complex-heading-regexp-format
  4501. (concat "^\\(\\*+\\)"
  4502. "\\(?: +" org-todo-regexp "\\)?"
  4503. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4504. "\\(?: +"
  4505. ;; Stats cookies can be stuck to body.
  4506. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4507. "\\(%s\\)"
  4508. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4509. "\\)"
  4510. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4511. "[ \t]*$")
  4512. org-todo-line-tags-regexp
  4513. (concat "^\\(\\*+\\)"
  4514. "\\(?: +" org-todo-regexp "\\)?"
  4515. "\\(?: +\\(.*?\\)\\)??"
  4516. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4517. "[ \t]*$")
  4518. org-deadline-regexp (concat "\\<" org-deadline-string)
  4519. org-deadline-time-regexp
  4520. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4521. org-deadline-time-hour-regexp
  4522. (concat "\\<" org-deadline-string
  4523. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4524. org-deadline-line-regexp
  4525. (concat "\\<\\(" org-deadline-string "\\).*")
  4526. org-scheduled-regexp
  4527. (concat "\\<" org-scheduled-string)
  4528. org-scheduled-time-regexp
  4529. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4530. org-scheduled-time-hour-regexp
  4531. (concat "\\<" org-scheduled-string
  4532. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4533. org-closed-time-regexp
  4534. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4535. org-keyword-time-regexp
  4536. (concat "\\<\\(" org-scheduled-string
  4537. "\\|" org-deadline-string
  4538. "\\|" org-closed-string
  4539. "\\|" org-clock-string "\\)"
  4540. " *[[<]\\([^]>]+\\)[]>]")
  4541. org-keyword-time-not-clock-regexp
  4542. (concat "\\<\\(" org-scheduled-string
  4543. "\\|" org-deadline-string
  4544. "\\|" org-closed-string
  4545. "\\)"
  4546. " *[[<]\\([^]>]+\\)[]>]")
  4547. org-maybe-keyword-time-regexp
  4548. (concat "\\(\\<\\(" org-scheduled-string
  4549. "\\|" org-deadline-string
  4550. "\\|" org-closed-string
  4551. "\\|" org-clock-string "\\)\\)?"
  4552. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4553. org-all-time-keywords
  4554. (mapcar (lambda (w) (substring w 0 -1))
  4555. (list org-scheduled-string org-deadline-string
  4556. org-clock-string org-closed-string)))
  4557. (setq org-ota nil)
  4558. (org-compute-latex-and-related-regexp))))
  4559. (defun org-file-contents (file &optional noerror)
  4560. "Return the contents of FILE, as a string."
  4561. (if (or (not file)
  4562. (not (file-readable-p file)))
  4563. (if noerror
  4564. (message "Cannot read file \"%s\"" file)
  4565. (error "Cannot read file \"%s\"" file))
  4566. (with-temp-buffer
  4567. (insert-file-contents file)
  4568. (buffer-string))))
  4569. (defun org-extract-log-state-settings (x)
  4570. "Extract the log state setting from a TODO keyword string.
  4571. This will extract info from a string like \"WAIT(w@/!)\"."
  4572. (let (kw key log1 log2)
  4573. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4574. (setq kw (match-string 1 x)
  4575. key (and (match-end 2) (match-string 2 x))
  4576. log1 (and (match-end 3) (match-string 3 x))
  4577. log2 (and (match-end 4) (match-string 4 x)))
  4578. (and (or log1 log2)
  4579. (list kw
  4580. (and log1 (if (equal log1 "!") 'time 'note))
  4581. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4582. (defun org-remove-keyword-keys (list)
  4583. "Remove a pair of parenthesis at the end of each string in LIST."
  4584. (mapcar (lambda (x)
  4585. (if (string-match "(.*)$" x)
  4586. (substring x 0 (match-beginning 0))
  4587. x))
  4588. list))
  4589. (defun org-assign-fast-keys (alist)
  4590. "Assign fast keys to a keyword-key alist.
  4591. Respect keys that are already there."
  4592. (let (new e (alt ?0))
  4593. (while (setq e (pop alist))
  4594. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4595. (cdr e)) ;; Key already assigned.
  4596. (push e new)
  4597. (let ((clist (string-to-list (downcase (car e))))
  4598. (used (append new alist)))
  4599. (when (= (car clist) ?@)
  4600. (pop clist))
  4601. (while (and clist (rassoc (car clist) used))
  4602. (pop clist))
  4603. (unless clist
  4604. (while (rassoc alt used)
  4605. (incf alt)))
  4606. (push (cons (car e) (or (car clist) alt)) new))))
  4607. (nreverse new)))
  4608. ;;; Some variables used in various places
  4609. (defvar org-window-configuration nil
  4610. "Used in various places to store a window configuration.")
  4611. (defvar org-selected-window nil
  4612. "Used in various places to store a window configuration.")
  4613. (defvar org-finish-function nil
  4614. "Function to be called when `C-c C-c' is used.
  4615. This is for getting out of special buffers like capture.")
  4616. ;; FIXME: Occasionally check by commenting these, to make sure
  4617. ;; no other functions uses these, forgetting to let-bind them.
  4618. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4619. (defvar org-last-state)
  4620. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4621. ;; Defined somewhere in this file, but used before definition.
  4622. (defvar org-entities) ;; defined in org-entities.el
  4623. (defvar org-struct-menu)
  4624. (defvar org-org-menu)
  4625. (defvar org-tbl-menu)
  4626. ;;;; Define the Org-mode
  4627. ;; We use a before-change function to check if a table might need
  4628. ;; an update.
  4629. (defvar org-table-may-need-update t
  4630. "Indicates that a table might need an update.
  4631. This variable is set by `org-before-change-function'.
  4632. `org-table-align' sets it back to nil.")
  4633. (defun org-before-change-function (beg end)
  4634. "Every change indicates that a table might need an update."
  4635. (setq org-table-may-need-update t))
  4636. (defvar org-mode-map)
  4637. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4638. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4639. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4640. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4641. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4642. (defvar org-table-buffer-is-an nil)
  4643. (defvar bidi-paragraph-direction)
  4644. (defvar buffer-face-mode-face)
  4645. (require 'outline)
  4646. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4647. (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"))
  4648. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4649. ;; Other stuff we need.
  4650. (require 'time-date)
  4651. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4652. (require 'easymenu)
  4653. (require 'overlay)
  4654. ;; (require 'org-macs) moved higher up in the file before it is first used
  4655. (require 'org-entities)
  4656. ;; (require 'org-compat) moved higher up in the file before it is first used
  4657. (require 'org-faces)
  4658. (require 'org-list)
  4659. (require 'org-pcomplete)
  4660. (require 'org-src)
  4661. (require 'org-footnote)
  4662. (require 'org-macro)
  4663. ;; babel
  4664. (require 'ob)
  4665. ;;;###autoload
  4666. (define-derived-mode org-mode outline-mode "Org"
  4667. "Outline-based notes management and organizer, alias
  4668. \"Carsten's outline-mode for keeping track of everything.\"
  4669. Org-mode develops organizational tasks around a NOTES file which
  4670. contains information about projects as plain text. Org-mode is
  4671. implemented on top of outline-mode, which is ideal to keep the content
  4672. of large files well structured. It supports ToDo items, deadlines and
  4673. time stamps, which magically appear in the diary listing of the Emacs
  4674. calendar. Tables are easily created with a built-in table editor.
  4675. Plain text URL-like links connect to websites, emails (VM), Usenet
  4676. messages (Gnus), BBDB entries, and any files related to the project.
  4677. For printing and sharing of notes, an Org-mode file (or a part of it)
  4678. can be exported as a structured ASCII or HTML file.
  4679. The following commands are available:
  4680. \\{org-mode-map}"
  4681. ;; Get rid of Outline menus, they are not needed
  4682. ;; Need to do this here because define-derived-mode sets up
  4683. ;; the keymap so late. Still, it is a waste to call this each time
  4684. ;; we switch another buffer into org-mode.
  4685. (if (featurep 'xemacs)
  4686. (when (boundp 'outline-mode-menu-heading)
  4687. ;; Assume this is Greg's port, it uses easymenu
  4688. (easy-menu-remove outline-mode-menu-heading)
  4689. (easy-menu-remove outline-mode-menu-show)
  4690. (easy-menu-remove outline-mode-menu-hide))
  4691. (define-key org-mode-map [menu-bar headings] 'undefined)
  4692. (define-key org-mode-map [menu-bar hide] 'undefined)
  4693. (define-key org-mode-map [menu-bar show] 'undefined))
  4694. (org-load-modules-maybe)
  4695. (easy-menu-add org-org-menu)
  4696. (easy-menu-add org-tbl-menu)
  4697. (org-install-agenda-files-menu)
  4698. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4699. (add-to-invisibility-spec '(org-cwidth))
  4700. (add-to-invisibility-spec '(org-hide-block . t))
  4701. (when (featurep 'xemacs)
  4702. (org-set-local 'line-move-ignore-invisible t))
  4703. (org-set-local 'outline-regexp org-outline-regexp)
  4704. (org-set-local 'outline-level 'org-outline-level)
  4705. (setq bidi-paragraph-direction 'left-to-right)
  4706. (when (and org-ellipsis
  4707. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4708. (fboundp 'make-glyph-code))
  4709. (unless org-display-table
  4710. (setq org-display-table (make-display-table)))
  4711. (set-display-table-slot
  4712. org-display-table 4
  4713. (vconcat (mapcar
  4714. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4715. org-ellipsis)))
  4716. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4717. (setq buffer-display-table org-display-table))
  4718. (org-set-regexps-and-options-for-tags)
  4719. (org-set-regexps-and-options)
  4720. (org-set-font-lock-defaults)
  4721. (when (and org-tag-faces (not org-tags-special-faces-re))
  4722. ;; tag faces set outside customize.... force initialization.
  4723. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4724. ;; Calc embedded
  4725. (org-set-local 'calc-embedded-open-mode "# ")
  4726. ;; Modify a few syntax entries
  4727. (modify-syntax-entry ?@ "w")
  4728. (modify-syntax-entry ?\" "\"")
  4729. (if org-startup-truncated (setq truncate-lines t))
  4730. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4731. (org-set-local 'font-lock-unfontify-region-function
  4732. 'org-unfontify-region)
  4733. ;; Activate before-change-function
  4734. (org-set-local 'org-table-may-need-update t)
  4735. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4736. 'local)
  4737. ;; Check for running clock before killing a buffer
  4738. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4739. ;; Initialize macros templates.
  4740. (org-macro-initialize-templates)
  4741. ;; Initialize radio targets.
  4742. (org-update-radio-target-regexp)
  4743. ;; Indentation.
  4744. (org-set-local 'indent-line-function 'org-indent-line)
  4745. (org-set-local 'indent-region-function 'org-indent-region)
  4746. ;; Filling and auto-filling.
  4747. (org-setup-filling)
  4748. ;; Comments.
  4749. (org-setup-comments-handling)
  4750. ;; Beginning/end of defun
  4751. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4752. (org-set-local 'end-of-defun-function 'org-forward-element)
  4753. ;; Next error for sparse trees
  4754. (org-set-local 'next-error-function 'org-occur-next-match)
  4755. ;; Make sure dependence stuff works reliably, even for users who set it
  4756. ;; too late :-(
  4757. (if org-enforce-todo-dependencies
  4758. (add-hook 'org-blocker-hook
  4759. 'org-block-todo-from-children-or-siblings-or-parent)
  4760. (remove-hook 'org-blocker-hook
  4761. 'org-block-todo-from-children-or-siblings-or-parent))
  4762. (if org-enforce-todo-checkbox-dependencies
  4763. (add-hook 'org-blocker-hook
  4764. 'org-block-todo-from-checkboxes)
  4765. (remove-hook 'org-blocker-hook
  4766. 'org-block-todo-from-checkboxes))
  4767. ;; Align options lines
  4768. (org-set-local
  4769. 'align-mode-rules-list
  4770. '((org-in-buffer-settings
  4771. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4772. (modes . '(org-mode)))))
  4773. ;; Imenu
  4774. (org-set-local 'imenu-create-index-function
  4775. 'org-imenu-get-tree)
  4776. ;; Make isearch reveal context
  4777. (if (or (featurep 'xemacs)
  4778. (not (boundp 'outline-isearch-open-invisible-function)))
  4779. ;; Emacs 21 and XEmacs make use of the hook
  4780. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4781. ;; Emacs 22 deals with this through a special variable
  4782. (org-set-local 'outline-isearch-open-invisible-function
  4783. (lambda (&rest ignore) (org-show-context 'isearch)))
  4784. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4785. ;; Setup the pcomplete hooks
  4786. (set (make-local-variable 'pcomplete-command-completion-function)
  4787. 'org-pcomplete-initial)
  4788. (set (make-local-variable 'pcomplete-command-name-function)
  4789. 'org-command-at-point)
  4790. (set (make-local-variable 'pcomplete-default-completion-function)
  4791. 'ignore)
  4792. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4793. 'org-parse-arguments)
  4794. (set (make-local-variable 'pcomplete-termination-string) "")
  4795. (when (>= emacs-major-version 23)
  4796. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4797. ;; If empty file that did not turn on org-mode automatically, make it to.
  4798. (if (and org-insert-mode-line-in-empty-file
  4799. (org-called-interactively-p 'any)
  4800. (= (point-min) (point-max)))
  4801. (insert "# -*- mode: org -*-\n\n"))
  4802. (unless org-inhibit-startup
  4803. (org-unmodified
  4804. (and org-startup-with-beamer-mode (org-beamer-mode))
  4805. (when org-startup-align-all-tables
  4806. (org-table-map-tables 'org-table-align 'quietly))
  4807. (when org-startup-with-inline-images
  4808. (org-display-inline-images))
  4809. (when org-startup-with-latex-preview
  4810. (org-preview-latex-fragment))
  4811. (unless org-inhibit-startup-visibility-stuff
  4812. (org-set-startup-visibility))))
  4813. ;; Try to set org-hide correctly
  4814. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4815. ;; Update `customize-package-emacs-version-alist'
  4816. (add-to-list 'customize-package-emacs-version-alist
  4817. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4818. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4819. ("8.0" . "24.4")))
  4820. (defvar org-mode-transpose-word-syntax-table
  4821. (let ((st (make-syntax-table)))
  4822. (mapc (lambda(c) (modify-syntax-entry
  4823. (string-to-char (car c)) "w p" st))
  4824. org-emphasis-alist)
  4825. st))
  4826. (when (fboundp 'abbrev-table-put)
  4827. (abbrev-table-put org-mode-abbrev-table
  4828. :parents (list text-mode-abbrev-table)))
  4829. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4830. (defsubst org-fix-ellipsis-at-bol ()
  4831. (save-excursion (goto-char (window-start)) (recenter 0)))
  4832. (defun org-find-invisible-foreground ()
  4833. (let ((candidates (remove
  4834. "unspecified-bg"
  4835. (nconc
  4836. (list (face-background 'default)
  4837. (face-background 'org-default))
  4838. (mapcar
  4839. (lambda (alist)
  4840. (when (boundp alist)
  4841. (cdr (assoc 'background-color (symbol-value alist)))))
  4842. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4843. (list (face-foreground 'org-hide))))))
  4844. (car (remove nil candidates))))
  4845. (defun org-current-time (&optional rounding-minutes past)
  4846. "Current time, possibly rounded to ROUNDING-MINUTES.
  4847. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4848. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4849. the rounding returns a past time."
  4850. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4851. (car org-time-stamp-rounding-minutes)))
  4852. (time (decode-time)) res)
  4853. (if (< r 1)
  4854. (current-time)
  4855. (setq res
  4856. (apply 'encode-time
  4857. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4858. (nthcdr 2 time))))
  4859. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4860. (seconds-to-time (- (org-float-time res) (* r 60)))
  4861. res))))
  4862. (defun org-today ()
  4863. "Return today date, considering `org-extend-today-until'."
  4864. (time-to-days
  4865. (time-subtract (current-time)
  4866. (list 0 (* 3600 org-extend-today-until) 0))))
  4867. ;;;; Font-Lock stuff, including the activators
  4868. (defvar org-mouse-map (make-sparse-keymap))
  4869. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4870. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4871. (when org-mouse-1-follows-link
  4872. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4873. (when org-tab-follows-link
  4874. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4875. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4876. (require 'font-lock)
  4877. (defconst org-non-link-chars "]\t\n\r<>")
  4878. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4879. "shell" "elisp" "doi" "message"))
  4880. (defvar org-link-types-re nil
  4881. "Matches a link that has a url-like prefix like \"http:\"")
  4882. (defvar org-link-re-with-space nil
  4883. "Matches a link with spaces, optional angular brackets around it.")
  4884. (defvar org-link-re-with-space2 nil
  4885. "Matches a link with spaces, optional angular brackets around it.")
  4886. (defvar org-link-re-with-space3 nil
  4887. "Matches a link with spaces, only for internal part in bracket links.")
  4888. (defvar org-angle-link-re nil
  4889. "Matches link with angular brackets, spaces are allowed.")
  4890. (defvar org-plain-link-re nil
  4891. "Matches plain link, without spaces.")
  4892. (defvar org-bracket-link-regexp nil
  4893. "Matches a link in double brackets.")
  4894. (defvar org-bracket-link-analytic-regexp nil
  4895. "Regular expression used to analyze links.
  4896. Here is what the match groups contain after a match:
  4897. 1: http:
  4898. 2: http
  4899. 3: path
  4900. 4: [desc]
  4901. 5: desc")
  4902. (defvar org-bracket-link-analytic-regexp++ nil
  4903. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4904. (defvar org-any-link-re nil
  4905. "Regular expression matching any link.")
  4906. (defconst org-match-sexp-depth 3
  4907. "Number of stacked braces for sub/superscript matching.")
  4908. (defun org-create-multibrace-regexp (left right n)
  4909. "Create a regular expression which will match a balanced sexp.
  4910. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4911. as single character strings.
  4912. The regexp returned will match the entire expression including the
  4913. delimiters. It will also define a single group which contains the
  4914. match except for the outermost delimiters. The maximum depth of
  4915. stacked delimiters is N. Escaping delimiters is not possible."
  4916. (let* ((nothing (concat "[^" left right "]*?"))
  4917. (or "\\|")
  4918. (re nothing)
  4919. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4920. (while (> n 1)
  4921. (setq n (1- n)
  4922. re (concat re or next)
  4923. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4924. (concat left "\\(" re "\\)" right)))
  4925. (defvar org-match-substring-regexp
  4926. (concat
  4927. "\\(\\S-\\)\\([_^]\\)\\("
  4928. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4929. "\\|"
  4930. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4931. "\\|"
  4932. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4933. "The regular expression matching a sub- or superscript.")
  4934. (defvar org-match-substring-with-braces-regexp
  4935. (concat
  4936. "\\(\\S-\\)\\([_^]\\)\\("
  4937. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4938. "\\)")
  4939. "The regular expression matching a sub- or superscript, forcing braces.")
  4940. (defun org-make-link-regexps ()
  4941. "Update the link regular expressions.
  4942. This should be called after the variable `org-link-types' has changed."
  4943. (setq org-link-types-re
  4944. (concat
  4945. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4946. org-link-re-with-space
  4947. (concat
  4948. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4949. "\\([^" org-non-link-chars " ]"
  4950. "[^" org-non-link-chars "]*"
  4951. "[^" org-non-link-chars " ]\\)>?")
  4952. org-link-re-with-space2
  4953. (concat
  4954. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4955. "\\([^" org-non-link-chars " ]"
  4956. "[^\t\n\r]*"
  4957. "[^" org-non-link-chars " ]\\)>?")
  4958. org-link-re-with-space3
  4959. (concat
  4960. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4961. "\\([^" org-non-link-chars " ]"
  4962. "[^\t\n\r]*\\)")
  4963. org-angle-link-re
  4964. (concat
  4965. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4966. "\\([^" org-non-link-chars " ]"
  4967. "[^" org-non-link-chars "]*"
  4968. "\\)>")
  4969. org-plain-link-re
  4970. (concat
  4971. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4972. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4973. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4974. org-bracket-link-regexp
  4975. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4976. org-bracket-link-analytic-regexp
  4977. (concat
  4978. "\\[\\["
  4979. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4980. "\\([^]]+\\)"
  4981. "\\]"
  4982. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4983. "\\]")
  4984. org-bracket-link-analytic-regexp++
  4985. (concat
  4986. "\\[\\["
  4987. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4988. "\\([^]]+\\)"
  4989. "\\]"
  4990. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4991. "\\]")
  4992. org-any-link-re
  4993. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4994. org-angle-link-re "\\)\\|\\("
  4995. org-plain-link-re "\\)")))
  4996. (org-make-link-regexps)
  4997. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4998. "Regular expression for fast time stamp matching.")
  4999. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  5000. "Regular expression for fast time stamp matching.")
  5001. (defconst org-ts-regexp0
  5002. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5003. "Regular expression matching time strings for analysis.
  5004. This one does not require the space after the date, so it can be used
  5005. on a string that terminates immediately after the date.")
  5006. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5007. "Regular expression matching time strings for analysis.")
  5008. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  5009. "Regular expression matching time stamps, with groups.")
  5010. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  5011. "Regular expression matching time stamps (also [..]), with groups.")
  5012. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  5013. "Regular expression matching a time stamp range.")
  5014. (defconst org-tr-regexp-both
  5015. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  5016. "Regular expression matching a time stamp range.")
  5017. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  5018. org-ts-regexp "\\)?")
  5019. "Regular expression matching a time stamp or time stamp range.")
  5020. (defconst org-tsr-regexp-both
  5021. (concat org-ts-regexp-both "\\(--?-?"
  5022. org-ts-regexp-both "\\)?")
  5023. "Regular expression matching a time stamp or time stamp range.
  5024. The time stamps may be either active or inactive.")
  5025. (defvar org-emph-face nil)
  5026. (defun org-do-emphasis-faces (limit)
  5027. "Run through the buffer and add overlays to emphasized strings."
  5028. (let (rtn a)
  5029. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5030. (if (not (= (char-after (match-beginning 3))
  5031. (char-after (match-beginning 4))))
  5032. (progn
  5033. (setq rtn t)
  5034. (setq a (assoc (match-string 3) org-emphasis-alist))
  5035. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5036. 'face
  5037. (nth 1 a))
  5038. (and (nth 4 a)
  5039. (org-remove-flyspell-overlays-in
  5040. (match-beginning 0) (match-end 0)))
  5041. (add-text-properties (match-beginning 2) (match-end 2)
  5042. '(font-lock-multiline t org-emphasis t))
  5043. (when org-hide-emphasis-markers
  5044. (add-text-properties (match-end 4) (match-beginning 5)
  5045. '(invisible org-link))
  5046. (add-text-properties (match-beginning 3) (match-end 3)
  5047. '(invisible org-link)))))
  5048. (backward-char 1))
  5049. rtn))
  5050. (defun org-emphasize (&optional char)
  5051. "Insert or change an emphasis, i.e. a font like bold or italic.
  5052. If there is an active region, change that region to a new emphasis.
  5053. If there is no region, just insert the marker characters and position
  5054. the cursor between them.
  5055. CHAR should be the marker character. If it is a space, it means to
  5056. remove the emphasis of the selected region.
  5057. If CHAR is not given (for example in an interactive call) it will be
  5058. prompted for."
  5059. (interactive)
  5060. (let ((erc org-emphasis-regexp-components)
  5061. (prompt "")
  5062. (string "") beg end move c s)
  5063. (if (org-region-active-p)
  5064. (setq beg (region-beginning) end (region-end)
  5065. string (buffer-substring beg end))
  5066. (setq move t))
  5067. (unless char
  5068. (message "Emphasis marker or tag: [%s]"
  5069. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5070. (setq char (read-char-exclusive)))
  5071. (if (equal char ?\ )
  5072. (setq s "" move nil)
  5073. (unless (assoc (char-to-string char) org-emphasis-alist)
  5074. (user-error "No such emphasis marker: \"%c\"" char))
  5075. (setq s (char-to-string char)))
  5076. (while (and (> (length string) 1)
  5077. (equal (substring string 0 1) (substring string -1))
  5078. (assoc (substring string 0 1) org-emphasis-alist))
  5079. (setq string (substring string 1 -1)))
  5080. (setq string (concat s string s))
  5081. (if beg (delete-region beg end))
  5082. (unless (or (bolp)
  5083. (string-match (concat "[" (nth 0 erc) "\n]")
  5084. (char-to-string (char-before (point)))))
  5085. (insert " "))
  5086. (unless (or (eobp)
  5087. (string-match (concat "[" (nth 1 erc) "\n]")
  5088. (char-to-string (char-after (point)))))
  5089. (insert " ") (backward-char 1))
  5090. (insert string)
  5091. (and move (backward-char 1))))
  5092. (defconst org-nonsticky-props
  5093. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5094. (defsubst org-rear-nonsticky-at (pos)
  5095. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5096. (defun org-activate-plain-links (limit)
  5097. "Run through the buffer and add overlays to links."
  5098. (let (f hl)
  5099. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5100. (not (org-in-src-block-p)))
  5101. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5102. (setq f (get-text-property (match-beginning 0) 'face))
  5103. (setq hl (org-match-string-no-properties 0))
  5104. (if (or (eq f 'org-tag)
  5105. (and (listp f) (memq 'org-tag f)))
  5106. nil
  5107. (add-text-properties (match-beginning 0) (match-end 0)
  5108. (list 'mouse-face 'highlight
  5109. 'face 'org-link
  5110. 'htmlize-link `(:uri ,hl)
  5111. 'keymap org-mouse-map))
  5112. (org-rear-nonsticky-at (match-end 0)))
  5113. t)))
  5114. (defun org-activate-code (limit)
  5115. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5116. (progn
  5117. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5118. (remove-text-properties (match-beginning 0) (match-end 0)
  5119. '(display t invisible t intangible t))
  5120. t)))
  5121. (defcustom org-src-fontify-natively nil
  5122. "When non-nil, fontify code in code blocks."
  5123. :type 'boolean
  5124. :version "24.1"
  5125. :group 'org-appearance
  5126. :group 'org-babel)
  5127. (defcustom org-allow-promoting-top-level-subtree nil
  5128. "When non-nil, allow promoting a top level subtree.
  5129. The leading star of the top level headline will be replaced
  5130. by a #."
  5131. :type 'boolean
  5132. :version "24.1"
  5133. :group 'org-appearance)
  5134. (defun org-fontify-meta-lines-and-blocks (limit)
  5135. (condition-case nil
  5136. (org-fontify-meta-lines-and-blocks-1 limit)
  5137. (error (message "org-mode fontification error"))))
  5138. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5139. "Fontify #+ lines and blocks."
  5140. (let ((case-fold-search t))
  5141. (if (re-search-forward
  5142. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5143. limit t)
  5144. (let ((beg (match-beginning 0))
  5145. (block-start (match-end 0))
  5146. (block-end nil)
  5147. (lang (match-string 7))
  5148. (beg1 (line-beginning-position 2))
  5149. (dc1 (downcase (match-string 2)))
  5150. (dc3 (downcase (match-string 3)))
  5151. end end1 quoting block-type ovl)
  5152. (cond
  5153. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5154. ;; a single line of backend-specific content
  5155. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5156. (remove-text-properties (match-beginning 0) (match-end 0)
  5157. '(display t invisible t intangible t))
  5158. (add-text-properties (match-beginning 1) (match-end 3)
  5159. '(font-lock-fontified t face org-meta-line))
  5160. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5161. '(font-lock-fontified t face org-block))
  5162. ; for backend-specific code
  5163. t)
  5164. ((and (match-end 4) (equal dc3 "+begin"))
  5165. ;; Truly a block
  5166. (setq block-type (downcase (match-string 5))
  5167. quoting (member block-type org-protecting-blocks))
  5168. (when (re-search-forward
  5169. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5170. nil t) ;; on purpose, we look further than LIMIT
  5171. (setq end (min (point-max) (match-end 0))
  5172. end1 (min (point-max) (1- (match-beginning 0))))
  5173. (setq block-end (match-beginning 0))
  5174. (when quoting
  5175. (remove-text-properties beg end
  5176. '(display t invisible t intangible t)))
  5177. (add-text-properties
  5178. beg end
  5179. '(font-lock-fontified t font-lock-multiline t))
  5180. (add-text-properties beg beg1 '(face org-meta-line))
  5181. (add-text-properties end1 (min (point-max) (1+ end))
  5182. '(face org-meta-line)) ; for end_src
  5183. (cond
  5184. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5185. (org-src-font-lock-fontify-block lang block-start block-end)
  5186. ;; remove old background overlays
  5187. (mapc (lambda (ov)
  5188. (if (eq (overlay-get ov 'face) 'org-block-background)
  5189. (delete-overlay ov)))
  5190. (overlays-at (/ (+ beg1 block-end) 2)))
  5191. ;; add a background overlay
  5192. (setq ovl (make-overlay beg1 block-end))
  5193. (overlay-put ovl 'face 'org-block-background)
  5194. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5195. (quoting
  5196. (add-text-properties beg1 (min (point-max) (1+ end1))
  5197. '(face org-block))) ; end of source block
  5198. ((not org-fontify-quote-and-verse-blocks))
  5199. ((string= block-type "quote")
  5200. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5201. ((string= block-type "verse")
  5202. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5203. (add-text-properties beg beg1 '(face org-block-begin-line))
  5204. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5205. '(face org-block-end-line))
  5206. t))
  5207. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5208. (add-text-properties
  5209. beg (match-end 3)
  5210. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5211. '(font-lock-fontified t invisible t)
  5212. '(font-lock-fontified t face org-document-info-keyword)))
  5213. (add-text-properties
  5214. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5215. (if (string-equal dc1 "+title:")
  5216. '(font-lock-fontified t face org-document-title)
  5217. '(font-lock-fontified t face org-document-info))))
  5218. ((or (equal dc1 "+results")
  5219. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5220. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5221. "+call:" "+header:" "+headers:" "+name:"))
  5222. (and (match-end 4) (equal dc3 "+attr")))
  5223. (add-text-properties
  5224. beg (match-end 0)
  5225. '(font-lock-fontified t face org-meta-line))
  5226. t)
  5227. ((member dc3 '(" " ""))
  5228. (add-text-properties
  5229. beg (match-end 0)
  5230. '(font-lock-fontified t face font-lock-comment-face)))
  5231. ((not (member (char-after beg) '(?\ ?\t)))
  5232. ;; just any other in-buffer setting, but not indented
  5233. (add-text-properties
  5234. beg (match-end 0)
  5235. '(font-lock-fontified t face org-meta-line))
  5236. t)
  5237. (t nil))))))
  5238. (defun org-activate-angle-links (limit)
  5239. "Run through the buffer and add overlays to links."
  5240. (if (and (re-search-forward org-angle-link-re limit t)
  5241. (not (org-in-src-block-p)))
  5242. (progn
  5243. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5244. (add-text-properties (match-beginning 0) (match-end 0)
  5245. (list 'mouse-face 'highlight
  5246. 'keymap org-mouse-map))
  5247. (org-rear-nonsticky-at (match-end 0))
  5248. t)))
  5249. (defun org-activate-footnote-links (limit)
  5250. "Run through the buffer and add overlays to footnotes."
  5251. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5252. (when fn
  5253. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5254. (org-remove-flyspell-overlays-in beg end)
  5255. (add-text-properties beg end
  5256. (list 'mouse-face 'highlight
  5257. 'keymap org-mouse-map
  5258. 'help-echo
  5259. (if (= (point-at-bol) beg)
  5260. "Footnote definition"
  5261. "Footnote reference")
  5262. 'font-lock-fontified t
  5263. 'font-lock-multiline t
  5264. 'face 'org-footnote))))))
  5265. (defun org-activate-bracket-links (limit)
  5266. "Run through the buffer and add overlays to bracketed links."
  5267. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5268. (not (org-in-src-block-p)))
  5269. (let* ((hl (org-match-string-no-properties 1))
  5270. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5271. (ip (org-maybe-intangible
  5272. (list 'invisible 'org-link
  5273. 'keymap org-mouse-map 'mouse-face 'highlight
  5274. 'font-lock-multiline t 'help-echo help
  5275. 'htmlize-link `(:uri ,hl))))
  5276. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5277. 'font-lock-multiline t 'help-echo help
  5278. 'htmlize-link `(:uri ,hl))))
  5279. ;; We need to remove the invisible property here. Table narrowing
  5280. ;; may have made some of this invisible.
  5281. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5282. (remove-text-properties (match-beginning 0) (match-end 0)
  5283. '(invisible nil))
  5284. (if (match-end 3)
  5285. (progn
  5286. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5287. (org-rear-nonsticky-at (match-beginning 3))
  5288. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5289. (org-rear-nonsticky-at (match-end 3))
  5290. (add-text-properties (match-end 3) (match-end 0) ip)
  5291. (org-rear-nonsticky-at (match-end 0)))
  5292. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5293. (org-rear-nonsticky-at (match-beginning 1))
  5294. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5295. (org-rear-nonsticky-at (match-end 1))
  5296. (add-text-properties (match-end 1) (match-end 0) ip)
  5297. (org-rear-nonsticky-at (match-end 0)))
  5298. t)))
  5299. (defun org-activate-dates (limit)
  5300. "Run through the buffer and add overlays to dates."
  5301. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5302. (not (equal (char-before (match-beginning 0)) 91)))
  5303. (progn
  5304. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5305. (add-text-properties (match-beginning 0) (match-end 0)
  5306. (list 'mouse-face 'highlight
  5307. 'keymap org-mouse-map))
  5308. (org-rear-nonsticky-at (match-end 0))
  5309. (when org-display-custom-times
  5310. (if (match-end 3)
  5311. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5312. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5313. t)))
  5314. (defvar org-target-link-regexp nil
  5315. "Regular expression matching radio targets in plain text.")
  5316. (make-variable-buffer-local 'org-target-link-regexp)
  5317. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5318. "Regular expression matching a link target.")
  5319. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5320. "Regular expression matching a radio target.")
  5321. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5322. "Regular expression matching any target.")
  5323. (defun org-activate-target-links (limit)
  5324. "Run through the buffer and add overlays to target matches."
  5325. (when org-target-link-regexp
  5326. (let ((case-fold-search t))
  5327. (if (re-search-forward org-target-link-regexp limit t)
  5328. (progn
  5329. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5330. (add-text-properties (match-beginning 0) (match-end 0)
  5331. (list 'mouse-face 'highlight
  5332. 'keymap org-mouse-map
  5333. 'help-echo "Radio target link"
  5334. 'org-linked-text t))
  5335. (org-rear-nonsticky-at (match-end 0))
  5336. t)))))
  5337. (defun org-update-radio-target-regexp ()
  5338. "Find all radio targets in this file and update the regular expression."
  5339. (interactive)
  5340. (when (memq 'radio org-activate-links)
  5341. (setq org-target-link-regexp
  5342. (org-make-target-link-regexp (org-all-targets 'radio)))
  5343. (org-restart-font-lock)))
  5344. (defun org-hide-wide-columns (limit)
  5345. (let (s e)
  5346. (setq s (text-property-any (point) (or limit (point-max))
  5347. 'org-cwidth t))
  5348. (when s
  5349. (setq e (next-single-property-change s 'org-cwidth))
  5350. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5351. (goto-char e)
  5352. t)))
  5353. (defvar org-latex-and-related-regexp nil
  5354. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5355. (defvar org-match-substring-regexp)
  5356. (defvar org-match-substring-with-braces-regexp)
  5357. (defun org-compute-latex-and-related-regexp ()
  5358. "Compute regular expression for LaTeX, entities and sub/superscript.
  5359. Result depends on variable `org-highlight-latex-and-related'."
  5360. (org-set-local
  5361. 'org-latex-and-related-regexp
  5362. (let* ((re-sub
  5363. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5364. ((eq org-use-sub-superscripts '{})
  5365. (list org-match-substring-with-braces-regexp))
  5366. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5367. (re-latex
  5368. (when (memq 'latex org-highlight-latex-and-related)
  5369. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5370. (delq nil
  5371. (mapcar (lambda (x)
  5372. (and (member (car x) matchers) (nth 1 x)))
  5373. org-latex-regexps)))))
  5374. (re-entities
  5375. (when (memq 'entities org-highlight-latex-and-related)
  5376. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5377. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5378. (defun org-do-latex-and-related (limit)
  5379. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5380. LIMIT bounds the search for syntax to highlight. Stop at first
  5381. highlighted object, if any. Return t if some highlighting was
  5382. done, nil otherwise."
  5383. (when (org-string-nw-p org-latex-and-related-regexp)
  5384. (catch 'found
  5385. (while (re-search-forward org-latex-and-related-regexp limit t)
  5386. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5387. 'face))
  5388. '(org-code org-verbatim underline))
  5389. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5390. '(?_ ?^))
  5391. 1
  5392. 0)))
  5393. (font-lock-prepend-text-property
  5394. (+ offset (match-beginning 0)) (match-end 0)
  5395. 'face 'org-latex-and-related)
  5396. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5397. '(font-lock-multiline t)))
  5398. (throw 'found t)))
  5399. nil)))
  5400. (defun org-restart-font-lock ()
  5401. "Restart `font-lock-mode', to force refontification."
  5402. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5403. (font-lock-mode -1)
  5404. (font-lock-mode 1)))
  5405. (defun org-all-targets (&optional radio)
  5406. "Return a list of all targets in this file.
  5407. When optional argument RADIO is non-nil, only find radio
  5408. targets."
  5409. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5410. (save-excursion
  5411. (goto-char (point-min))
  5412. (while (re-search-forward re nil t)
  5413. ;; Make sure point is really within the object.
  5414. (backward-char)
  5415. (let ((obj (org-element-context)))
  5416. (when (memq (org-element-type obj) '(radio-target target))
  5417. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5418. rtn)))
  5419. (defun org-make-target-link-regexp (targets)
  5420. "Make regular expression matching all strings in TARGETS.
  5421. The regular expression finds the targets also if there is a line break
  5422. between words."
  5423. (and targets
  5424. (concat
  5425. "\\<\\("
  5426. (mapconcat
  5427. (lambda (x)
  5428. (setq x (regexp-quote x))
  5429. (while (string-match " +" x)
  5430. (setq x (replace-match "\\s-+" t t x)))
  5431. x)
  5432. targets
  5433. "\\|")
  5434. "\\)\\>")))
  5435. (defun org-activate-tags (limit)
  5436. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5437. (progn
  5438. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5439. (add-text-properties (match-beginning 1) (match-end 1)
  5440. (list 'mouse-face 'highlight
  5441. 'keymap org-mouse-map))
  5442. (org-rear-nonsticky-at (match-end 1))
  5443. t)))
  5444. (defun org-outline-level ()
  5445. "Compute the outline level of the heading at point.
  5446. If this is called at a normal headline, the level is the number of stars.
  5447. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5448. (save-excursion
  5449. (if (not (condition-case nil
  5450. (org-back-to-heading t)
  5451. (error nil)))
  5452. 0
  5453. (looking-at org-outline-regexp)
  5454. (1- (- (match-end 0) (match-beginning 0))))))
  5455. (defvar org-font-lock-keywords nil)
  5456. (defsubst org-re-property (property &optional literal)
  5457. "Return a regexp matching a PROPERTY line.
  5458. Match group 3 will be set to the value if it exists."
  5459. (concat "^\\(?4:[ \t]*\\)\\(?1::\\(?2:"
  5460. (if literal property (regexp-quote property))
  5461. "\\):\\)[ \t]+\\(?3:[^ \t\r\n].*?\\)\\(?5:[ \t]*\\)$"))
  5462. (defconst org-property-re
  5463. (org-re-property ".*?" 'literal)
  5464. "Regular expression matching a property line.
  5465. There are four matching groups:
  5466. 1: :PROPKEY: including the leading and trailing colon,
  5467. 2: PROPKEY without the leading and trailing colon,
  5468. 3: PROPVAL without leading or trailing spaces,
  5469. 4: the indentation of the current line,
  5470. 5: trailing whitespace.")
  5471. (defvar org-font-lock-hook nil
  5472. "Functions to be called for special font lock stuff.")
  5473. (defvar org-font-lock-set-keywords-hook nil
  5474. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5475. This is called after `org-font-lock-extra-keywords' is defined, but before
  5476. it is installed to be used by font lock. This can be useful if something
  5477. needs to be inserted at a specific position in the font-lock sequence.")
  5478. (defun org-font-lock-hook (limit)
  5479. "Run `org-font-lock-hook' within LIMIT."
  5480. (run-hook-with-args 'org-font-lock-hook limit))
  5481. (defun org-set-font-lock-defaults ()
  5482. "Set font lock defaults for the current buffer."
  5483. (let* ((em org-fontify-emphasized-text)
  5484. (lk org-activate-links)
  5485. (org-font-lock-extra-keywords
  5486. (list
  5487. ;; Call the hook
  5488. '(org-font-lock-hook)
  5489. ;; Headlines
  5490. `(,(if org-fontify-whole-heading-line
  5491. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5492. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5493. (1 (org-get-level-face 1))
  5494. (2 (org-get-level-face 2))
  5495. (3 (org-get-level-face 3)))
  5496. ;; Table lines
  5497. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5498. (1 'org-table t))
  5499. ;; Table internals
  5500. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5501. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5502. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5503. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5504. ;; Drawers
  5505. (list org-drawer-regexp '(0 'org-special-keyword t))
  5506. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5507. ;; Properties
  5508. (list org-property-re
  5509. '(1 'org-special-keyword t)
  5510. '(3 'org-property-value t))
  5511. ;; Links
  5512. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5513. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5514. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5515. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5516. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5517. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5518. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5519. ;; Targets.
  5520. (list org-any-target-regexp '(0 'org-target t))
  5521. ;; Diary sexps.
  5522. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5523. ;; Macro
  5524. '("{{{.+}}}" (0 'org-macro t))
  5525. '(org-hide-wide-columns (0 nil append))
  5526. ;; TODO keyword
  5527. (list (format org-heading-keyword-regexp-format
  5528. org-todo-regexp)
  5529. '(2 (org-get-todo-face 2) t))
  5530. ;; DONE
  5531. (if org-fontify-done-headline
  5532. (list (format org-heading-keyword-regexp-format
  5533. (concat
  5534. "\\(?:"
  5535. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5536. "\\)"))
  5537. '(2 'org-headline-done t))
  5538. nil)
  5539. ;; Priorities
  5540. '(org-font-lock-add-priority-faces)
  5541. ;; Tags
  5542. '(org-font-lock-add-tag-faces)
  5543. ;; Tags groups
  5544. (if (and org-group-tags org-tag-groups-alist)
  5545. (list (concat org-outline-regexp-bol ".+\\(:"
  5546. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5547. ":\\).*$")
  5548. '(1 'org-tag-group prepend)))
  5549. ;; Special keywords
  5550. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5551. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5552. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5553. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5554. ;; Emphasis
  5555. (if em
  5556. (if (featurep 'xemacs)
  5557. '(org-do-emphasis-faces (0 nil append))
  5558. '(org-do-emphasis-faces)))
  5559. ;; Checkboxes
  5560. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5561. 1 'org-checkbox prepend)
  5562. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5563. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5564. (0 (org-get-checkbox-statistics-face) t)))
  5565. ;; Description list items
  5566. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5567. 1 'org-list-dt prepend)
  5568. ;; ARCHIVEd headings
  5569. (list (concat
  5570. org-outline-regexp-bol
  5571. "\\(.*:" org-archive-tag ":.*\\)")
  5572. '(1 'org-archived prepend))
  5573. ;; Specials
  5574. '(org-do-latex-and-related)
  5575. '(org-fontify-entities)
  5576. '(org-raise-scripts)
  5577. ;; Code
  5578. '(org-activate-code (1 'org-code t))
  5579. ;; COMMENT
  5580. (list (format org-heading-keyword-regexp-format
  5581. (concat "\\("
  5582. org-comment-string "\\|" org-quote-string
  5583. "\\)"))
  5584. '(2 'org-special-keyword t))
  5585. ;; Blocks and meta lines
  5586. '(org-fontify-meta-lines-and-blocks))))
  5587. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5588. (run-hooks 'org-font-lock-set-keywords-hook)
  5589. ;; Now set the full font-lock-keywords
  5590. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5591. (org-set-local 'font-lock-defaults
  5592. '(org-font-lock-keywords t nil nil backward-paragraph))
  5593. (kill-local-variable 'font-lock-keywords) nil))
  5594. (defun org-toggle-pretty-entities ()
  5595. "Toggle the composition display of entities as UTF8 characters."
  5596. (interactive)
  5597. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5598. (org-restart-font-lock)
  5599. (if org-pretty-entities
  5600. (message "Entities are now displayed as UTF8 characters")
  5601. (save-restriction
  5602. (widen)
  5603. (org-decompose-region (point-min) (point-max))
  5604. (message "Entities are now displayed as plain text"))))
  5605. (defvar org-custom-properties-overlays nil
  5606. "List of overlays used for custom properties.")
  5607. (make-variable-buffer-local 'org-custom-properties-overlays)
  5608. (defun org-toggle-custom-properties-visibility ()
  5609. "Display or hide properties in `org-custom-properties'."
  5610. (interactive)
  5611. (if org-custom-properties-overlays
  5612. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5613. (setq org-custom-properties-overlays nil))
  5614. (unless (not org-custom-properties)
  5615. (save-excursion
  5616. (save-restriction
  5617. (widen)
  5618. (goto-char (point-min))
  5619. (while (re-search-forward org-property-re nil t)
  5620. (mapc (lambda(p)
  5621. (when (equal p (substring (match-string 1) 1 -1))
  5622. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5623. (overlay-put o 'invisible t)
  5624. (overlay-put o 'org-custom-property t)
  5625. (push o org-custom-properties-overlays))))
  5626. org-custom-properties)))))))
  5627. (defun org-fontify-entities (limit)
  5628. "Find an entity to fontify."
  5629. (let (ee)
  5630. (when org-pretty-entities
  5631. (catch 'match
  5632. (while (re-search-forward
  5633. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5634. limit t)
  5635. (if (and (not (org-in-indented-comment-line))
  5636. (setq ee (org-entity-get (match-string 1)))
  5637. (= (length (nth 6 ee)) 1))
  5638. (let*
  5639. ((end (if (equal (match-string 2) "{}")
  5640. (match-end 2)
  5641. (match-end 1))))
  5642. (add-text-properties
  5643. (match-beginning 0) end
  5644. (list 'font-lock-fontified t))
  5645. (compose-region (match-beginning 0) end
  5646. (nth 6 ee) nil)
  5647. (backward-char 1)
  5648. (throw 'match t))))
  5649. nil))))
  5650. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5651. "Fontify string S like in Org-mode."
  5652. (with-temp-buffer
  5653. (insert s)
  5654. (let ((org-odd-levels-only odd-levels))
  5655. (org-mode)
  5656. (font-lock-fontify-buffer)
  5657. (buffer-string))))
  5658. (defvar org-m nil)
  5659. (defvar org-l nil)
  5660. (defvar org-f nil)
  5661. (defun org-get-level-face (n)
  5662. "Get the right face for match N in font-lock matching of headlines."
  5663. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5664. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5665. (if org-cycle-level-faces
  5666. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5667. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5668. (cond
  5669. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5670. ((eq n 2) org-f)
  5671. (t (if org-level-color-stars-only nil org-f))))
  5672. (defun org-get-todo-face (kwd)
  5673. "Get the right face for a TODO keyword KWD.
  5674. If KWD is a number, get the corresponding match group."
  5675. (if (numberp kwd) (setq kwd (match-string kwd)))
  5676. (or (org-face-from-face-or-color
  5677. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5678. (and (member kwd org-done-keywords) 'org-done)
  5679. 'org-todo))
  5680. (defun org-face-from-face-or-color (context inherit face-or-color)
  5681. "Create a face list that inherits INHERIT, but sets the foreground color.
  5682. When FACE-OR-COLOR is not a string, just return it."
  5683. (if (stringp face-or-color)
  5684. (list :inherit inherit
  5685. (cdr (assoc context org-faces-easy-properties))
  5686. face-or-color)
  5687. face-or-color))
  5688. (defun org-font-lock-add-tag-faces (limit)
  5689. "Add the special tag faces."
  5690. (when (and org-tag-faces org-tags-special-faces-re)
  5691. (while (re-search-forward org-tags-special-faces-re limit t)
  5692. (add-text-properties (match-beginning 1) (match-end 1)
  5693. (list 'face (org-get-tag-face 1)
  5694. 'font-lock-fontified t))
  5695. (backward-char 1))))
  5696. (defun org-font-lock-add-priority-faces (limit)
  5697. "Add the special priority faces."
  5698. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5699. (when (save-match-data (org-at-heading-p))
  5700. (add-text-properties
  5701. (match-beginning 0) (match-end 0)
  5702. (list 'face (or (org-face-from-face-or-color
  5703. 'priority 'org-priority
  5704. (cdr (assoc (char-after (match-beginning 1))
  5705. org-priority-faces)))
  5706. 'org-priority)
  5707. 'font-lock-fontified t)))))
  5708. (defun org-get-tag-face (kwd)
  5709. "Get the right face for a TODO keyword KWD.
  5710. If KWD is a number, get the corresponding match group."
  5711. (if (numberp kwd) (setq kwd (match-string kwd)))
  5712. (or (org-face-from-face-or-color
  5713. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5714. 'org-tag))
  5715. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5716. "Remove fontification and activation overlays from links."
  5717. (font-lock-default-unfontify-region beg end)
  5718. (let* ((buffer-undo-list t)
  5719. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5720. (inhibit-modification-hooks t)
  5721. deactivate-mark buffer-file-name buffer-file-truename)
  5722. (org-decompose-region beg end)
  5723. (remove-text-properties beg end
  5724. '(mouse-face t keymap t org-linked-text t
  5725. invisible t intangible t
  5726. org-no-flyspell t org-emphasis t))
  5727. (org-remove-font-lock-display-properties beg end)))
  5728. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5729. ((raise 0.3) (height 0.7))
  5730. ((raise -0.5))
  5731. ((raise 0.5)))
  5732. "Display properties for showing superscripts and subscripts.")
  5733. (defun org-remove-font-lock-display-properties (beg end)
  5734. "Remove specific display properties that have been added by font lock.
  5735. The will remove the raise properties that are used to show superscripts
  5736. and subscripts."
  5737. (let (next prop)
  5738. (while (< beg end)
  5739. (setq next (next-single-property-change beg 'display nil end)
  5740. prop (get-text-property beg 'display))
  5741. (if (member prop org-script-display)
  5742. (put-text-property beg next 'display nil))
  5743. (setq beg next))))
  5744. (defun org-raise-scripts (limit)
  5745. "Add raise properties to sub/superscripts."
  5746. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5747. (if (re-search-forward
  5748. (if (eq org-use-sub-superscripts t)
  5749. org-match-substring-regexp
  5750. org-match-substring-with-braces-regexp)
  5751. limit t)
  5752. (let* ((pos (point)) table-p comment-p
  5753. (mpos (match-beginning 3))
  5754. (emph-p (get-text-property mpos 'org-emphasis))
  5755. (link-p (get-text-property mpos 'mouse-face))
  5756. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5757. (goto-char (point-at-bol))
  5758. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5759. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5760. (goto-char pos)
  5761. ;; Handle a_b^c
  5762. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5763. (if (or comment-p emph-p link-p keyw-p)
  5764. t
  5765. (put-text-property (match-beginning 3) (match-end 0)
  5766. 'display
  5767. (if (equal (char-after (match-beginning 2)) ?^)
  5768. (nth (if table-p 3 1) org-script-display)
  5769. (nth (if table-p 2 0) org-script-display)))
  5770. (add-text-properties (match-beginning 2) (match-end 2)
  5771. (list 'invisible t
  5772. 'org-dwidth t 'org-dwidth-n 1))
  5773. (if (and (eq (char-after (match-beginning 3)) ?{)
  5774. (eq (char-before (match-end 3)) ?}))
  5775. (progn
  5776. (add-text-properties
  5777. (match-beginning 3) (1+ (match-beginning 3))
  5778. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5779. (add-text-properties
  5780. (1- (match-end 3)) (match-end 3)
  5781. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5782. t)))))
  5783. ;;;; Visibility cycling, including org-goto and indirect buffer
  5784. ;;; Cycling
  5785. (defvar org-cycle-global-status nil)
  5786. (make-variable-buffer-local 'org-cycle-global-status)
  5787. (put 'org-cycle-global-status 'org-state t)
  5788. (defvar org-cycle-subtree-status nil)
  5789. (make-variable-buffer-local 'org-cycle-subtree-status)
  5790. (put 'org-cycle-subtree-status 'org-state t)
  5791. (defvar org-inlinetask-min-level)
  5792. ;;;###autoload
  5793. (defun org-cycle (&optional arg)
  5794. "TAB-action and visibility cycling for Org-mode.
  5795. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5796. is outline visibility cycling, but it also invokes other actions
  5797. in special contexts.
  5798. - When this function is called with a prefix argument, rotate the entire
  5799. buffer through 3 states (global cycling)
  5800. 1. OVERVIEW: Show only top-level headlines.
  5801. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5802. 3. SHOW ALL: Show everything.
  5803. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5804. determined by the variable `org-startup-folded', and by any VISIBILITY
  5805. properties in the buffer.
  5806. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5807. including any drawers.
  5808. - When inside a table, re-align the table and move to the next field.
  5809. - When point is at the beginning of a headline, rotate the subtree started
  5810. by this line through 3 different states (local cycling)
  5811. 1. FOLDED: Only the main headline is shown.
  5812. 2. CHILDREN: The main headline and the direct children are shown.
  5813. From this state, you can move to one of the children
  5814. and zoom in further.
  5815. 3. SUBTREE: Show the entire subtree, including body text.
  5816. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5817. - When point is at the beginning of an empty headline and the variable
  5818. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5819. of the headline by demoting and promoting it to likely levels. This
  5820. speeds up creation document structure by pressing TAB once or several
  5821. times right after creating a new headline.
  5822. - When there is a numeric prefix, go up to a heading with level ARG, do
  5823. a `show-subtree' and return to the previous cursor position. If ARG
  5824. is negative, go up that many levels.
  5825. - When point is not at the beginning of a headline, execute the global
  5826. binding for TAB, which is re-indenting the line. See the option
  5827. `org-cycle-emulate-tab' for details.
  5828. - Special case: if point is at the beginning of the buffer and there is
  5829. no headline in line 1, this function will act as if called with prefix arg
  5830. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5831. But only if also the variable `org-cycle-global-at-bob' is t."
  5832. (interactive "P")
  5833. (org-load-modules-maybe)
  5834. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5835. (and org-cycle-level-after-item/entry-creation
  5836. (or (org-cycle-level)
  5837. (org-cycle-item-indentation))))
  5838. (let* (message-log-max ; Don't populate the *Messages* buffer
  5839. (limit-level
  5840. (or org-cycle-max-level
  5841. (and (boundp 'org-inlinetask-min-level)
  5842. org-inlinetask-min-level
  5843. (1- org-inlinetask-min-level))))
  5844. (nstars (and limit-level
  5845. (if org-odd-levels-only
  5846. (and limit-level (1- (* limit-level 2)))
  5847. limit-level)))
  5848. (org-outline-regexp
  5849. (if (not (derived-mode-p 'org-mode))
  5850. outline-regexp
  5851. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5852. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5853. (not (looking-at org-outline-regexp))))
  5854. (org-cycle-hook
  5855. (if bob-special
  5856. (delq 'org-optimize-window-after-visibility-change
  5857. (copy-sequence org-cycle-hook))
  5858. org-cycle-hook))
  5859. (pos (point)))
  5860. (if (or bob-special (equal arg '(4)))
  5861. ;; special case: use global cycling
  5862. (setq arg t))
  5863. (cond
  5864. ((equal arg '(16))
  5865. (setq last-command 'dummy)
  5866. (org-set-startup-visibility)
  5867. (message "Startup visibility, plus VISIBILITY properties"))
  5868. ((equal arg '(64))
  5869. (show-all)
  5870. (message "Entire buffer visible, including drawers"))
  5871. ;; Table: enter it or move to the next field.
  5872. ((org-at-table-p 'any)
  5873. (if (org-at-table.el-p)
  5874. (message "Use C-c ' to edit table.el tables")
  5875. (if arg (org-table-edit-field t)
  5876. (org-table-justify-field-maybe)
  5877. (call-interactively 'org-table-next-field))))
  5878. ((run-hook-with-args-until-success
  5879. 'org-tab-after-check-for-table-hook))
  5880. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5881. ((eq arg t) (org-cycle-internal-global))
  5882. ;; Drawers: delegate to `org-flag-drawer'.
  5883. ((and org-drawers org-drawer-regexp
  5884. (save-excursion
  5885. (beginning-of-line 1)
  5886. (looking-at org-drawer-regexp)))
  5887. (org-flag-drawer ; toggle block visibility
  5888. (not (get-char-property (match-end 0) 'invisible))))
  5889. ;; Show-subtree, ARG levels up from here.
  5890. ((integerp arg)
  5891. (save-excursion
  5892. (org-back-to-heading)
  5893. (outline-up-heading (if (< arg 0) (- arg)
  5894. (- (funcall outline-level) arg)))
  5895. (org-show-subtree)))
  5896. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5897. ((and (featurep 'org-inlinetask)
  5898. (org-inlinetask-at-task-p)
  5899. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5900. (org-inlinetask-toggle-visibility))
  5901. ((org-try-cdlatex-tab))
  5902. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5903. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5904. (save-excursion (beginning-of-line 1)
  5905. (looking-at org-outline-regexp)))
  5906. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5907. (org-cycle-internal-local))
  5908. ;; From there: TAB emulation and template completion.
  5909. (buffer-read-only (org-back-to-heading))
  5910. ((run-hook-with-args-until-success
  5911. 'org-tab-after-check-for-cycling-hook))
  5912. ((org-try-structure-completion))
  5913. ((run-hook-with-args-until-success
  5914. 'org-tab-before-tab-emulation-hook))
  5915. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5916. (or (not (bolp))
  5917. (not (looking-at org-outline-regexp))))
  5918. (call-interactively (global-key-binding "\t")))
  5919. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5920. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5921. (or (and (eq org-cycle-emulate-tab 'white)
  5922. (= (match-end 0) (point-at-eol)))
  5923. (and (eq org-cycle-emulate-tab 'whitestart)
  5924. (>= (match-end 0) pos))))
  5925. t
  5926. (eq org-cycle-emulate-tab t))
  5927. (call-interactively (global-key-binding "\t")))
  5928. (t (save-excursion
  5929. (org-back-to-heading)
  5930. (org-cycle)))))))
  5931. (defun org-cycle-internal-global ()
  5932. "Do the global cycling action."
  5933. ;; Hack to avoid display of messages for .org attachments in Gnus
  5934. (let (message-log-max ; Don't populate the *Messages* buffer
  5935. (ga (string-match "\\*fontification" (buffer-name))))
  5936. (cond
  5937. ((and (eq last-command this-command)
  5938. (eq org-cycle-global-status 'overview))
  5939. ;; We just created the overview - now do table of contents
  5940. ;; This can be slow in very large buffers, so indicate action
  5941. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5942. (unless ga (message "CONTENTS..."))
  5943. (org-content)
  5944. (unless ga (message "CONTENTS...done"))
  5945. (setq org-cycle-global-status 'contents)
  5946. (run-hook-with-args 'org-cycle-hook 'contents))
  5947. ((and (eq last-command this-command)
  5948. (eq org-cycle-global-status 'contents))
  5949. ;; We just showed the table of contents - now show everything
  5950. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5951. (show-all)
  5952. (unless ga (message "SHOW ALL"))
  5953. (setq org-cycle-global-status 'all)
  5954. (run-hook-with-args 'org-cycle-hook 'all))
  5955. (t
  5956. ;; Default action: go to overview
  5957. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5958. (org-overview)
  5959. (unless ga (message "OVERVIEW"))
  5960. (setq org-cycle-global-status 'overview)
  5961. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5962. (defun org-cycle-internal-local ()
  5963. "Do the local cycling action."
  5964. (let (message-log-max ; Don't populate the *Messages* buffer
  5965. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5966. ;; First, determine end of headline (EOH), end of subtree or item
  5967. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5968. (save-excursion
  5969. (if (org-at-item-p)
  5970. (progn
  5971. (beginning-of-line)
  5972. (setq struct (org-list-struct))
  5973. (setq eoh (point-at-eol))
  5974. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5975. (setq has-children (org-list-has-child-p (point) struct)))
  5976. (org-back-to-heading)
  5977. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5978. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5979. (setq has-children
  5980. (or (save-excursion
  5981. (let ((level (funcall outline-level)))
  5982. (outline-next-heading)
  5983. (and (org-at-heading-p t)
  5984. (> (funcall outline-level) level))))
  5985. (save-excursion
  5986. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5987. ;; Determine end invisible part of buffer (EOL)
  5988. (beginning-of-line 2)
  5989. ;; XEmacs doesn't have `next-single-char-property-change'
  5990. (if (featurep 'xemacs)
  5991. (while (and (not (eobp)) ;; this is like `next-line'
  5992. (get-char-property (1- (point)) 'invisible))
  5993. (beginning-of-line 2))
  5994. (while (and (not (eobp)) ;; this is like `next-line'
  5995. (get-char-property (1- (point)) 'invisible))
  5996. (goto-char (next-single-char-property-change (point) 'invisible))
  5997. (and (eolp) (beginning-of-line 2))))
  5998. (setq eol (point)))
  5999. ;; Find out what to do next and set `this-command'
  6000. (cond
  6001. ((= eos eoh)
  6002. ;; Nothing is hidden behind this heading
  6003. (unless (org-before-first-heading-p)
  6004. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6005. (message "EMPTY ENTRY")
  6006. (setq org-cycle-subtree-status nil)
  6007. (save-excursion
  6008. (goto-char eos)
  6009. (outline-next-heading)
  6010. (if (outline-invisible-p) (org-flag-heading nil))))
  6011. ((and (or (>= eol eos)
  6012. (not (string-match "\\S-" (buffer-substring eol eos))))
  6013. (or has-children
  6014. (not (setq children-skipped
  6015. org-cycle-skip-children-state-if-no-children))))
  6016. ;; Entire subtree is hidden in one line: children view
  6017. (unless (org-before-first-heading-p)
  6018. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6019. (if (org-at-item-p)
  6020. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6021. (org-show-entry)
  6022. (org-with-limited-levels (show-children))
  6023. ;; FIXME: This slows down the func way too much.
  6024. ;; How keep drawers hidden in subtree anyway?
  6025. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6026. ;; (org-cycle-hide-drawers 'subtree))
  6027. ;; Fold every list in subtree to top-level items.
  6028. (when (eq org-cycle-include-plain-lists 'integrate)
  6029. (save-excursion
  6030. (org-back-to-heading)
  6031. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6032. (beginning-of-line 1)
  6033. (let* ((struct (org-list-struct))
  6034. (prevs (org-list-prevs-alist struct))
  6035. (end (org-list-get-bottom-point struct)))
  6036. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6037. (org-list-get-all-items (point) struct prevs))
  6038. (goto-char (if (< end eos) end eos)))))))
  6039. (message "CHILDREN")
  6040. (save-excursion
  6041. (goto-char eos)
  6042. (outline-next-heading)
  6043. (if (outline-invisible-p) (org-flag-heading nil)))
  6044. (setq org-cycle-subtree-status 'children)
  6045. (unless (org-before-first-heading-p)
  6046. (run-hook-with-args 'org-cycle-hook 'children)))
  6047. ((or children-skipped
  6048. (and (eq last-command this-command)
  6049. (eq org-cycle-subtree-status 'children)))
  6050. ;; We just showed the children, or no children are there,
  6051. ;; now show everything.
  6052. (unless (org-before-first-heading-p)
  6053. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6054. (outline-flag-region eoh eos nil)
  6055. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6056. (setq org-cycle-subtree-status 'subtree)
  6057. (unless (org-before-first-heading-p)
  6058. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6059. (t
  6060. ;; Default action: hide the subtree.
  6061. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6062. (outline-flag-region eoh eos t)
  6063. (message "FOLDED")
  6064. (setq org-cycle-subtree-status 'folded)
  6065. (unless (org-before-first-heading-p)
  6066. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6067. ;;;###autoload
  6068. (defun org-global-cycle (&optional arg)
  6069. "Cycle the global visibility. For details see `org-cycle'.
  6070. With \\[universal-argument] prefix arg, switch to startup visibility.
  6071. With a numeric prefix, show all headlines up to that level."
  6072. (interactive "P")
  6073. (let ((org-cycle-include-plain-lists
  6074. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6075. (cond
  6076. ((integerp arg)
  6077. (show-all)
  6078. (hide-sublevels arg)
  6079. (setq org-cycle-global-status 'contents))
  6080. ((equal arg '(4))
  6081. (org-set-startup-visibility)
  6082. (message "Startup visibility, plus VISIBILITY properties."))
  6083. (t
  6084. (org-cycle '(4))))))
  6085. (defun org-set-startup-visibility ()
  6086. "Set the visibility required by startup options and properties."
  6087. (cond
  6088. ((eq org-startup-folded t)
  6089. (org-cycle '(4)))
  6090. ((eq org-startup-folded 'content)
  6091. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  6092. (org-cycle '(4)) (org-cycle '(4)))))
  6093. (unless (eq org-startup-folded 'showeverything)
  6094. (if org-hide-block-startup (org-hide-block-all))
  6095. (org-set-visibility-according-to-property 'no-cleanup)
  6096. (org-cycle-hide-archived-subtrees 'all)
  6097. (org-cycle-hide-drawers 'all)
  6098. (org-cycle-show-empty-lines t)))
  6099. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6100. "Switch subtree visibilities according to :VISIBILITY: property."
  6101. (interactive)
  6102. (let (org-show-entry-below state)
  6103. (save-excursion
  6104. (goto-char (point-min))
  6105. (while (re-search-forward
  6106. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6107. nil t)
  6108. (setq state (match-string 1))
  6109. (save-excursion
  6110. (org-back-to-heading t)
  6111. (hide-subtree)
  6112. (org-reveal)
  6113. (cond
  6114. ((equal state '("fold" "folded"))
  6115. (hide-subtree))
  6116. ((equal state "children")
  6117. (org-show-hidden-entry)
  6118. (show-children))
  6119. ((equal state "content")
  6120. (save-excursion
  6121. (save-restriction
  6122. (org-narrow-to-subtree)
  6123. (org-content))))
  6124. ((member state '("all" "showall"))
  6125. (show-subtree)))))
  6126. (unless no-cleanup
  6127. (org-cycle-hide-archived-subtrees 'all)
  6128. (org-cycle-hide-drawers 'all)
  6129. (org-cycle-show-empty-lines 'all)))))
  6130. ;; This function uses outline-regexp instead of the more fundamental
  6131. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6132. ;; buffers, where outline-regexp is needed.
  6133. (defun org-overview ()
  6134. "Switch to overview mode, showing only top-level headlines.
  6135. Really, this shows all headlines with level equal or greater than the level
  6136. of the first headline in the buffer. This is important, because if the
  6137. first headline is not level one, then (hide-sublevels 1) gives confusing
  6138. results."
  6139. (interactive)
  6140. (let ((l (org-current-line))
  6141. (level (save-excursion
  6142. (goto-char (point-min))
  6143. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6144. (progn
  6145. (goto-char (match-beginning 0))
  6146. (funcall outline-level))))))
  6147. (and level (hide-sublevels level))
  6148. (recenter '(4))
  6149. (org-goto-line l)))
  6150. (defun org-content (&optional arg)
  6151. "Show all headlines in the buffer, like a table of contents.
  6152. With numerical argument N, show content up to level N."
  6153. (interactive "P")
  6154. (save-excursion
  6155. ;; Visit all headings and show their offspring
  6156. (and (integerp arg) (org-overview))
  6157. (goto-char (point-max))
  6158. (catch 'exit
  6159. (while (and (progn (condition-case nil
  6160. (outline-previous-visible-heading 1)
  6161. (error (goto-char (point-min))))
  6162. t)
  6163. (looking-at org-outline-regexp))
  6164. (if (integerp arg)
  6165. (show-children (1- arg))
  6166. (show-branches))
  6167. (if (bobp) (throw 'exit nil))))))
  6168. (defun org-optimize-window-after-visibility-change (state)
  6169. "Adjust the window after a change in outline visibility.
  6170. This function is the default value of the hook `org-cycle-hook'."
  6171. (when (get-buffer-window (current-buffer))
  6172. (cond
  6173. ((eq state 'content) nil)
  6174. ((eq state 'all) nil)
  6175. ((eq state 'folded) nil)
  6176. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6177. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6178. (defun org-remove-empty-overlays-at (pos)
  6179. "Remove outline overlays that do not contain non-white stuff."
  6180. (mapc
  6181. (lambda (o)
  6182. (and (eq 'outline (overlay-get o 'invisible))
  6183. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6184. (overlay-end o))))
  6185. (delete-overlay o)))
  6186. (overlays-at pos)))
  6187. (defun org-clean-visibility-after-subtree-move ()
  6188. "Fix visibility issues after moving a subtree."
  6189. ;; First, find a reasonable region to look at:
  6190. ;; Start two siblings above, end three below
  6191. (let* ((beg (save-excursion
  6192. (and (org-get-last-sibling)
  6193. (org-get-last-sibling))
  6194. (point)))
  6195. (end (save-excursion
  6196. (and (org-get-next-sibling)
  6197. (org-get-next-sibling)
  6198. (org-get-next-sibling))
  6199. (if (org-at-heading-p)
  6200. (point-at-eol)
  6201. (point))))
  6202. (level (looking-at "\\*+"))
  6203. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6204. (save-excursion
  6205. (save-restriction
  6206. (narrow-to-region beg end)
  6207. (when re
  6208. ;; Properly fold already folded siblings
  6209. (goto-char (point-min))
  6210. (while (re-search-forward re nil t)
  6211. (if (and (not (outline-invisible-p))
  6212. (save-excursion
  6213. (goto-char (point-at-eol)) (outline-invisible-p)))
  6214. (hide-entry))))
  6215. (org-cycle-show-empty-lines 'overview)
  6216. (org-cycle-hide-drawers 'overview)))))
  6217. (defun org-cycle-show-empty-lines (state)
  6218. "Show empty lines above all visible headlines.
  6219. The region to be covered depends on STATE when called through
  6220. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6221. entire buffer covered. Note that an empty line is only shown if there
  6222. are at least `org-cycle-separator-lines' empty lines before the headline."
  6223. (when (not (= org-cycle-separator-lines 0))
  6224. (save-excursion
  6225. (let* ((n (abs org-cycle-separator-lines))
  6226. (re (cond
  6227. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6228. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6229. (t (let ((ns (number-to-string (- n 2))))
  6230. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6231. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6232. beg end b e)
  6233. (cond
  6234. ((memq state '(overview contents t))
  6235. (setq beg (point-min) end (point-max)))
  6236. ((memq state '(children folded))
  6237. (setq beg (point) end (progn (org-end-of-subtree t t)
  6238. (beginning-of-line 2)
  6239. (point)))))
  6240. (when beg
  6241. (goto-char beg)
  6242. (while (re-search-forward re end t)
  6243. (unless (get-char-property (match-end 1) 'invisible)
  6244. (setq e (match-end 1))
  6245. (if (< org-cycle-separator-lines 0)
  6246. (setq b (save-excursion
  6247. (goto-char (match-beginning 0))
  6248. (org-back-over-empty-lines)
  6249. (if (save-excursion
  6250. (goto-char (max (point-min) (1- (point))))
  6251. (org-at-heading-p))
  6252. (1- (point))
  6253. (point))))
  6254. (setq b (match-beginning 1)))
  6255. (outline-flag-region b e nil)))))))
  6256. ;; Never hide empty lines at the end of the file.
  6257. (save-excursion
  6258. (goto-char (point-max))
  6259. (outline-previous-heading)
  6260. (outline-end-of-heading)
  6261. (if (and (looking-at "[ \t\n]+")
  6262. (= (match-end 0) (point-max)))
  6263. (outline-flag-region (point) (match-end 0) nil))))
  6264. (defun org-show-empty-lines-in-parent ()
  6265. "Move to the parent and re-show empty lines before visible headlines."
  6266. (save-excursion
  6267. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6268. (org-cycle-show-empty-lines context))))
  6269. (defun org-files-list ()
  6270. "Return `org-agenda-files' list, plus all open org-mode files.
  6271. This is useful for operations that need to scan all of a user's
  6272. open and agenda-wise Org files."
  6273. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6274. (dolist (buf (buffer-list))
  6275. (with-current-buffer buf
  6276. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6277. (let ((file (expand-file-name (buffer-file-name))))
  6278. (unless (member file files)
  6279. (push file files))))))
  6280. files))
  6281. (defsubst org-entry-beginning-position ()
  6282. "Return the beginning position of the current entry."
  6283. (save-excursion (outline-back-to-heading t) (point)))
  6284. (defsubst org-entry-end-position ()
  6285. "Return the end position of the current entry."
  6286. (save-excursion (outline-next-heading) (point)))
  6287. (defun org-cycle-hide-drawers (state)
  6288. "Re-hide all drawers after a visibility state change."
  6289. (when (and (derived-mode-p 'org-mode)
  6290. (not (memq state '(overview folded contents))))
  6291. (save-excursion
  6292. (let* ((globalp (memq state '(contents all)))
  6293. (beg (if globalp (point-min) (point)))
  6294. (end (if globalp (point-max)
  6295. (if (eq state 'children)
  6296. (save-excursion (outline-next-heading) (point))
  6297. (org-end-of-subtree t)))))
  6298. (goto-char beg)
  6299. (while (re-search-forward org-drawer-regexp end t)
  6300. (org-flag-drawer t))))))
  6301. (defun org-cycle-hide-inline-tasks (state)
  6302. "Re-hide inline task when switching to 'contents visibility state."
  6303. (when (and (eq state 'contents)
  6304. (boundp 'org-inlinetask-min-level)
  6305. org-inlinetask-min-level)
  6306. (hide-sublevels (1- org-inlinetask-min-level))))
  6307. (defun org-flag-drawer (flag)
  6308. "When FLAG is non-nil, hide the drawer we are within.
  6309. Otherwise make it visible."
  6310. (save-excursion
  6311. (beginning-of-line 1)
  6312. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6313. (let ((b (match-end 0)))
  6314. (if (re-search-forward
  6315. "^[ \t]*:END:"
  6316. (save-excursion (outline-next-heading) (point)) t)
  6317. (outline-flag-region b (point-at-eol) flag)
  6318. (user-error ":END: line missing at position %s" b))))))
  6319. (defun org-subtree-end-visible-p ()
  6320. "Is the end of the current subtree visible?"
  6321. (pos-visible-in-window-p
  6322. (save-excursion (org-end-of-subtree t) (point))))
  6323. (defun org-first-headline-recenter (&optional N)
  6324. "Move cursor to the first headline and recenter the headline.
  6325. Optional argument N means put the headline into the Nth line of the window."
  6326. (goto-char (point-min))
  6327. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6328. (beginning-of-line)
  6329. (recenter (prefix-numeric-value N))))
  6330. ;;; Saving and restoring visibility
  6331. (defun org-outline-overlay-data (&optional use-markers)
  6332. "Return a list of the locations of all outline overlays.
  6333. These are overlays with the `invisible' property value `outline'.
  6334. The return value is a list of cons cells, with start and stop
  6335. positions for each overlay.
  6336. If USE-MARKERS is set, return the positions as markers."
  6337. (let (beg end)
  6338. (save-excursion
  6339. (save-restriction
  6340. (widen)
  6341. (delq nil
  6342. (mapcar (lambda (o)
  6343. (when (eq (overlay-get o 'invisible) 'outline)
  6344. (setq beg (overlay-start o)
  6345. end (overlay-end o))
  6346. (and beg end (> end beg)
  6347. (if use-markers
  6348. (cons (move-marker (make-marker) beg)
  6349. (move-marker (make-marker) end))
  6350. (cons beg end)))))
  6351. (overlays-in (point-min) (point-max))))))))
  6352. (defun org-set-outline-overlay-data (data)
  6353. "Create visibility overlays for all positions in DATA.
  6354. DATA should have been made by `org-outline-overlay-data'."
  6355. (let (o)
  6356. (save-excursion
  6357. (save-restriction
  6358. (widen)
  6359. (show-all)
  6360. (mapc (lambda (c)
  6361. (outline-flag-region (car c) (cdr c) t))
  6362. data)))))
  6363. ;;; Folding of blocks
  6364. (defvar org-hide-block-overlays nil
  6365. "Overlays hiding blocks.")
  6366. (make-variable-buffer-local 'org-hide-block-overlays)
  6367. (defun org-block-map (function &optional start end)
  6368. "Call FUNCTION at the head of all source blocks in the current buffer.
  6369. Optional arguments START and END can be used to limit the range."
  6370. (let ((start (or start (point-min)))
  6371. (end (or end (point-max))))
  6372. (save-excursion
  6373. (goto-char start)
  6374. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6375. (save-excursion
  6376. (save-match-data
  6377. (goto-char (match-beginning 0))
  6378. (funcall function)))))))
  6379. (defun org-hide-block-toggle-all ()
  6380. "Toggle the visibility of all blocks in the current buffer."
  6381. (org-block-map #'org-hide-block-toggle))
  6382. (defun org-hide-block-all ()
  6383. "Fold all blocks in the current buffer."
  6384. (interactive)
  6385. (org-show-block-all)
  6386. (org-block-map #'org-hide-block-toggle-maybe))
  6387. (defun org-show-block-all ()
  6388. "Unfold all blocks in the current buffer."
  6389. (interactive)
  6390. (mapc 'delete-overlay org-hide-block-overlays)
  6391. (setq org-hide-block-overlays nil))
  6392. (defun org-hide-block-toggle-maybe ()
  6393. "Toggle visibility of block at point."
  6394. (interactive)
  6395. (let ((case-fold-search t))
  6396. (if (save-excursion
  6397. (beginning-of-line 1)
  6398. (looking-at org-block-regexp))
  6399. (progn (org-hide-block-toggle)
  6400. t) ;; to signal that we took action
  6401. nil))) ;; to signal that we did not
  6402. (defun org-hide-block-toggle (&optional force)
  6403. "Toggle the visibility of the current block."
  6404. (interactive)
  6405. (save-excursion
  6406. (beginning-of-line)
  6407. (if (re-search-forward org-block-regexp nil t)
  6408. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6409. (end (match-end 0)) ;; end of entire body
  6410. ov)
  6411. (if (memq t (mapcar (lambda (overlay)
  6412. (eq (overlay-get overlay 'invisible)
  6413. 'org-hide-block))
  6414. (overlays-at start)))
  6415. (if (or (not force) (eq force 'off))
  6416. (mapc (lambda (ov)
  6417. (when (member ov org-hide-block-overlays)
  6418. (setq org-hide-block-overlays
  6419. (delq ov org-hide-block-overlays)))
  6420. (when (eq (overlay-get ov 'invisible)
  6421. 'org-hide-block)
  6422. (delete-overlay ov)))
  6423. (overlays-at start)))
  6424. (setq ov (make-overlay start end))
  6425. (overlay-put ov 'invisible 'org-hide-block)
  6426. ;; make the block accessible to isearch
  6427. (overlay-put
  6428. ov 'isearch-open-invisible
  6429. (lambda (ov)
  6430. (when (member ov org-hide-block-overlays)
  6431. (setq org-hide-block-overlays
  6432. (delq ov org-hide-block-overlays)))
  6433. (when (eq (overlay-get ov 'invisible)
  6434. 'org-hide-block)
  6435. (delete-overlay ov))))
  6436. (push ov org-hide-block-overlays)))
  6437. (user-error "Not looking at a source block"))))
  6438. ;; org-tab-after-check-for-cycling-hook
  6439. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6440. ;; Remove overlays when changing major mode
  6441. (add-hook 'org-mode-hook
  6442. (lambda () (org-add-hook 'change-major-mode-hook
  6443. 'org-show-block-all 'append 'local)))
  6444. ;;; Org-goto
  6445. (defvar org-goto-window-configuration nil)
  6446. (defvar org-goto-marker nil)
  6447. (defvar org-goto-map)
  6448. (defun org-goto-map ()
  6449. "Set the keymap `org-goto'."
  6450. (setq org-goto-map
  6451. (let ((map (make-sparse-keymap)))
  6452. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6453. mouse-drag-region universal-argument org-occur))
  6454. cmd)
  6455. (while (setq cmd (pop cmds))
  6456. (substitute-key-definition cmd cmd map global-map)))
  6457. (suppress-keymap map)
  6458. (org-defkey map "\C-m" 'org-goto-ret)
  6459. (org-defkey map [(return)] 'org-goto-ret)
  6460. (org-defkey map [(left)] 'org-goto-left)
  6461. (org-defkey map [(right)] 'org-goto-right)
  6462. (org-defkey map [(control ?g)] 'org-goto-quit)
  6463. (org-defkey map "\C-i" 'org-cycle)
  6464. (org-defkey map [(tab)] 'org-cycle)
  6465. (org-defkey map [(down)] 'outline-next-visible-heading)
  6466. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6467. (if org-goto-auto-isearch
  6468. (if (fboundp 'define-key-after)
  6469. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6470. nil)
  6471. (org-defkey map "q" 'org-goto-quit)
  6472. (org-defkey map "n" 'outline-next-visible-heading)
  6473. (org-defkey map "p" 'outline-previous-visible-heading)
  6474. (org-defkey map "f" 'outline-forward-same-level)
  6475. (org-defkey map "b" 'outline-backward-same-level)
  6476. (org-defkey map "u" 'outline-up-heading))
  6477. (org-defkey map "/" 'org-occur)
  6478. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6479. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6480. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6481. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6482. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6483. map)))
  6484. (defconst org-goto-help
  6485. "Browse buffer copy, to find location or copy text.%s
  6486. RET=jump to location C-g=quit and return to previous location
  6487. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6488. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6489. (defun org-goto (&optional alternative-interface)
  6490. "Look up a different location in the current file, keeping current visibility.
  6491. When you want look-up or go to a different location in a
  6492. document, the fastest way is often to fold the entire buffer and
  6493. then dive into the tree. This method has the disadvantage, that
  6494. the previous location will be folded, which may not be what you
  6495. want.
  6496. This command works around this by showing a copy of the current
  6497. buffer in an indirect buffer, in overview mode. You can dive
  6498. into the tree in that copy, use org-occur and incremental search
  6499. to find a location. When pressing RET or `Q', the command
  6500. returns to the original buffer in which the visibility is still
  6501. unchanged. After RET it will also jump to the location selected
  6502. in the indirect buffer and expose the headline hierarchy above.
  6503. With a prefix argument, use the alternative interface: e.g. if
  6504. `org-goto-interface' is 'outline use 'outline-path-completion."
  6505. (interactive "P")
  6506. (org-goto-map)
  6507. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6508. (org-refile-use-outline-path t)
  6509. (org-refile-target-verify-function nil)
  6510. (interface
  6511. (if (not alternative-interface)
  6512. org-goto-interface
  6513. (if (eq org-goto-interface 'outline)
  6514. 'outline-path-completion
  6515. 'outline)))
  6516. (org-goto-start-pos (point))
  6517. (selected-point
  6518. (if (eq interface 'outline)
  6519. (car (org-get-location (current-buffer) org-goto-help))
  6520. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6521. (org-refile-check-position pa)
  6522. (nth 3 pa)))))
  6523. (if selected-point
  6524. (progn
  6525. (org-mark-ring-push org-goto-start-pos)
  6526. (goto-char selected-point)
  6527. (if (or (outline-invisible-p) (org-invisible-p2))
  6528. (org-show-context 'org-goto)))
  6529. (message "Quit"))))
  6530. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6531. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6532. (defvar org-goto-local-auto-isearch-map) ; defined below
  6533. (defun org-get-location (buf help)
  6534. "Let the user select a location in the Org-mode buffer BUF.
  6535. This function uses a recursive edit. It returns the selected position
  6536. or nil."
  6537. (org-no-popups
  6538. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6539. (isearch-hide-immediately nil)
  6540. (isearch-search-fun-function
  6541. (lambda () 'org-goto-local-search-headings))
  6542. (org-goto-selected-point org-goto-exit-command))
  6543. (save-excursion
  6544. (save-window-excursion
  6545. (delete-other-windows)
  6546. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6547. (org-pop-to-buffer-same-window
  6548. (condition-case nil
  6549. (make-indirect-buffer (current-buffer) "*org-goto*")
  6550. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6551. (with-output-to-temp-buffer "*Org Help*"
  6552. (princ (format help (if org-goto-auto-isearch
  6553. " Just type for auto-isearch."
  6554. " n/p/f/b/u to navigate, q to quit."))))
  6555. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6556. (setq buffer-read-only nil)
  6557. (let ((org-startup-truncated t)
  6558. (org-startup-folded nil)
  6559. (org-startup-align-all-tables nil))
  6560. (org-mode)
  6561. (org-overview))
  6562. (setq buffer-read-only t)
  6563. (if (and (boundp 'org-goto-start-pos)
  6564. (integer-or-marker-p org-goto-start-pos))
  6565. (let ((org-show-hierarchy-above t)
  6566. (org-show-siblings t)
  6567. (org-show-following-heading t))
  6568. (goto-char org-goto-start-pos)
  6569. (and (outline-invisible-p) (org-show-context)))
  6570. (goto-char (point-min)))
  6571. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6572. (message "Select location and press RET")
  6573. (use-local-map org-goto-map)
  6574. (recursive-edit)))
  6575. (kill-buffer "*org-goto*")
  6576. (cons org-goto-selected-point org-goto-exit-command))))
  6577. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6578. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6579. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6580. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6581. (defun org-goto-local-search-headings (string bound noerror)
  6582. "Search and make sure that any matches are in headlines."
  6583. (catch 'return
  6584. (while (if isearch-forward
  6585. (search-forward string bound noerror)
  6586. (search-backward string bound noerror))
  6587. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6588. (and (member :headline context)
  6589. (not (member :tags context))))
  6590. (throw 'return (point))))))
  6591. (defun org-goto-local-auto-isearch ()
  6592. "Start isearch."
  6593. (interactive)
  6594. (goto-char (point-min))
  6595. (let ((keys (this-command-keys)))
  6596. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6597. (isearch-mode t)
  6598. (isearch-process-search-char (string-to-char keys)))))
  6599. (defun org-goto-ret (&optional arg)
  6600. "Finish `org-goto' by going to the new location."
  6601. (interactive "P")
  6602. (setq org-goto-selected-point (point)
  6603. org-goto-exit-command 'return)
  6604. (throw 'exit nil))
  6605. (defun org-goto-left ()
  6606. "Finish `org-goto' by going to the new location."
  6607. (interactive)
  6608. (if (org-at-heading-p)
  6609. (progn
  6610. (beginning-of-line 1)
  6611. (setq org-goto-selected-point (point)
  6612. org-goto-exit-command 'left)
  6613. (throw 'exit nil))
  6614. (user-error "Not on a heading")))
  6615. (defun org-goto-right ()
  6616. "Finish `org-goto' by going to the new location."
  6617. (interactive)
  6618. (if (org-at-heading-p)
  6619. (progn
  6620. (setq org-goto-selected-point (point)
  6621. org-goto-exit-command 'right)
  6622. (throw 'exit nil))
  6623. (user-error "Not on a heading")))
  6624. (defun org-goto-quit ()
  6625. "Finish `org-goto' without cursor motion."
  6626. (interactive)
  6627. (setq org-goto-selected-point nil)
  6628. (setq org-goto-exit-command 'quit)
  6629. (throw 'exit nil))
  6630. ;;; Indirect buffer display of subtrees
  6631. (defvar org-indirect-dedicated-frame nil
  6632. "This is the frame being used for indirect tree display.")
  6633. (defvar org-last-indirect-buffer nil)
  6634. (defun org-tree-to-indirect-buffer (&optional arg)
  6635. "Create indirect buffer and narrow it to current subtree.
  6636. With a numerical prefix ARG, go up to this level and then take that tree.
  6637. If ARG is negative, go up that many levels.
  6638. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6639. indirect buffer previously made with this command, to avoid proliferation of
  6640. indirect buffers. However, when you call the command with a \
  6641. \\[universal-argument] prefix, or
  6642. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6643. is kept so that you can work with several indirect buffers at the same time.
  6644. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6645. \\[universal-argument] prefix also
  6646. requests that a new frame be made for the new buffer, so that the dedicated
  6647. frame is not changed."
  6648. (interactive "P")
  6649. (let ((cbuf (current-buffer))
  6650. (cwin (selected-window))
  6651. (pos (point))
  6652. beg end level heading ibuf)
  6653. (save-excursion
  6654. (org-back-to-heading t)
  6655. (when (numberp arg)
  6656. (setq level (org-outline-level))
  6657. (if (< arg 0) (setq arg (+ level arg)))
  6658. (while (> (setq level (org-outline-level)) arg)
  6659. (org-up-heading-safe)))
  6660. (setq beg (point)
  6661. heading (org-get-heading))
  6662. (org-end-of-subtree t t)
  6663. (if (org-at-heading-p) (backward-char 1))
  6664. (setq end (point)))
  6665. (if (and (buffer-live-p org-last-indirect-buffer)
  6666. (not (eq org-indirect-buffer-display 'new-frame))
  6667. (not arg))
  6668. (kill-buffer org-last-indirect-buffer))
  6669. (setq ibuf (org-get-indirect-buffer cbuf)
  6670. org-last-indirect-buffer ibuf)
  6671. (cond
  6672. ((or (eq org-indirect-buffer-display 'new-frame)
  6673. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6674. (select-frame (make-frame))
  6675. (delete-other-windows)
  6676. (org-pop-to-buffer-same-window ibuf)
  6677. (org-set-frame-title heading))
  6678. ((eq org-indirect-buffer-display 'dedicated-frame)
  6679. (raise-frame
  6680. (select-frame (or (and org-indirect-dedicated-frame
  6681. (frame-live-p org-indirect-dedicated-frame)
  6682. org-indirect-dedicated-frame)
  6683. (setq org-indirect-dedicated-frame (make-frame)))))
  6684. (delete-other-windows)
  6685. (org-pop-to-buffer-same-window ibuf)
  6686. (org-set-frame-title (concat "Indirect: " heading)))
  6687. ((eq org-indirect-buffer-display 'current-window)
  6688. (org-pop-to-buffer-same-window ibuf))
  6689. ((eq org-indirect-buffer-display 'other-window)
  6690. (pop-to-buffer ibuf))
  6691. (t (error "Invalid value")))
  6692. (if (featurep 'xemacs)
  6693. (save-excursion (org-mode) (turn-on-font-lock)))
  6694. (narrow-to-region beg end)
  6695. (show-all)
  6696. (goto-char pos)
  6697. (run-hook-with-args 'org-cycle-hook 'all)
  6698. (and (window-live-p cwin) (select-window cwin))))
  6699. (defun org-get-indirect-buffer (&optional buffer)
  6700. (setq buffer (or buffer (current-buffer)))
  6701. (let ((n 1) (base (buffer-name buffer)) bname)
  6702. (while (buffer-live-p
  6703. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6704. (setq n (1+ n)))
  6705. (condition-case nil
  6706. (make-indirect-buffer buffer bname 'clone)
  6707. (error (make-indirect-buffer buffer bname)))))
  6708. (defun org-set-frame-title (title)
  6709. "Set the title of the current frame to the string TITLE."
  6710. ;; FIXME: how to name a single frame in XEmacs???
  6711. (unless (featurep 'xemacs)
  6712. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6713. ;;;; Structure editing
  6714. ;;; Inserting headlines
  6715. (defun org-previous-line-empty-p (&optional next)
  6716. "Is the previous line a blank line?
  6717. When NEXT is non-nil, check the next line instead."
  6718. (save-excursion
  6719. (and (not (bobp))
  6720. (or (beginning-of-line (if next 2 0)) t)
  6721. (save-match-data
  6722. (looking-at "[ \t]*$")))))
  6723. (defun org-insert-heading (&optional arg invisible-ok)
  6724. "Insert a new heading or item with same depth at point.
  6725. If point is in a plain list and ARG is nil, create a new list item.
  6726. With one universal prefix argument, insert a heading even in lists.
  6727. With two universal prefix arguments, insert the heading at the end
  6728. of the parent subtree.
  6729. If point is at the beginning of a headline, insert a sibling before
  6730. the current headline. If point is not at the beginning, split the line
  6731. and create a new headline with the text in the current line after point
  6732. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6733. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6734. This is important for non-interactive uses of the command."
  6735. (interactive "P")
  6736. (if (org-called-interactively-p 'any) (org-reveal))
  6737. (let ((itemp (org-in-item-p)))
  6738. (cond
  6739. ((or (= (buffer-size) 0)
  6740. (and (not (save-excursion
  6741. (and (ignore-errors (org-back-to-heading invisible-ok))
  6742. (org-at-heading-p))))
  6743. (or arg (not itemp))))
  6744. (insert
  6745. (if (org-previous-line-empty-p) "" "\n")
  6746. (if (org-in-src-block-p) ",* " "* "))
  6747. (run-hooks 'org-insert-heading-hook))
  6748. ((or arg
  6749. (and (not itemp) org-insert-heading-respect-content)
  6750. (not (org-insert-item
  6751. (save-excursion
  6752. (and itemp
  6753. (goto-char itemp)
  6754. (looking-at org-list-full-item-re)
  6755. (match-string 3))))))
  6756. (let (begn endn)
  6757. (when (org-buffer-narrowed-p)
  6758. (setq begn (point-min) endn (point-max))
  6759. (widen))
  6760. (let* ((empty-line-p nil)
  6761. (eops (equal arg '(16))) ; insert at end of parent subtree
  6762. (org-insert-heading-respect-content
  6763. (or (not (null arg)) org-insert-heading-respect-content))
  6764. (level nil)
  6765. (on-heading (org-at-heading-p))
  6766. ;; Get a level to fall back on
  6767. (fix-level
  6768. (save-excursion
  6769. (org-back-to-heading t)
  6770. (looking-at org-outline-regexp)
  6771. (make-string (1- (length (match-string 0))) ?*)))
  6772. (on-empty-line
  6773. (save-excursion (beginning-of-line 1) (looking-at "^\\s-*$")))
  6774. (head (save-excursion
  6775. (condition-case nil
  6776. (progn
  6777. (org-back-to-heading invisible-ok)
  6778. (when (and (not on-heading)
  6779. (featurep 'org-inlinetask)
  6780. (integerp org-inlinetask-min-level)
  6781. (>= (length (match-string 0))
  6782. org-inlinetask-min-level))
  6783. ;; Find a heading level before the inline task
  6784. (while (and (setq level (org-up-heading-safe))
  6785. (>= level org-inlinetask-min-level)))
  6786. (if (org-at-heading-p)
  6787. (org-back-to-heading invisible-ok)
  6788. (error "This should not happen")))
  6789. (unless (and (save-excursion
  6790. (save-match-data
  6791. (org-backward-heading-same-level 1 invisible-ok))
  6792. (= (point) (match-beginning 0)))
  6793. (not (org-previous-line-empty-p t)))
  6794. (setq empty-line-p (org-previous-line-empty-p)))
  6795. (match-string 0))
  6796. (error (or fix-level "* ")))))
  6797. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6798. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6799. pos hide-previous previous-pos)
  6800. (if ;; At the beginning of a heading, open a new line for insertion
  6801. (and (bolp) (org-at-heading-p)
  6802. (not eops)
  6803. (or (bobp)
  6804. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6805. (open-line (if blank 2 1))
  6806. (save-excursion
  6807. (setq previous-pos (point-at-bol))
  6808. (end-of-line)
  6809. (setq hide-previous (outline-invisible-p)))
  6810. (and org-insert-heading-respect-content
  6811. (save-excursion
  6812. (while (outline-invisible-p)
  6813. (org-show-subtree)
  6814. (org-up-heading-safe))))
  6815. (let ((split
  6816. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6817. (save-excursion
  6818. (let ((p (point)))
  6819. (goto-char (point-at-bol))
  6820. (and (looking-at org-complex-heading-regexp)
  6821. (match-beginning 4)
  6822. (> p (match-beginning 4)))))))
  6823. tags pos)
  6824. (cond
  6825. ;; Insert a new line, possibly at end of parent subtree
  6826. ((and (not arg) (not on-heading) (not on-empty-line)
  6827. (not (save-excursion
  6828. (beginning-of-line 1)
  6829. (or (looking-at org-list-full-item-re)
  6830. ;; Don't convert :end: lines to headline
  6831. (looking-at "^\\s-*:end:")
  6832. (looking-at "^\\s-*#\\+end_?")))))
  6833. (beginning-of-line 1))
  6834. (org-insert-heading-respect-content
  6835. (if (not eops)
  6836. (progn
  6837. (org-end-of-subtree nil t)
  6838. (and (looking-at "^\\*") (backward-char 1))
  6839. (while (and (not (bobp))
  6840. ;; Don't delete spaces in empty headlines
  6841. (not (looking-back org-outline-regexp))
  6842. (member (char-before) '(?\ ?\t ?\n)))
  6843. (backward-delete-char 1)))
  6844. (let ((p (point)))
  6845. (org-up-heading-safe)
  6846. (if (= p (point))
  6847. (goto-char (point-max))
  6848. (org-end-of-subtree nil t))))
  6849. (when (featurep 'org-inlinetask)
  6850. (while (and (not (eobp))
  6851. (looking-at "\\(\\*+\\)[ \t]+")
  6852. (>= (length (match-string 1))
  6853. org-inlinetask-min-level))
  6854. (org-end-of-subtree nil t)))
  6855. (or (bolp) (newline))
  6856. (or (org-previous-line-empty-p)
  6857. (and blank (newline)))
  6858. (if (or empty-line-p eops) (open-line 1)))
  6859. ;; Insert a headling containing text after point
  6860. ((org-at-heading-p)
  6861. (when hide-previous
  6862. (show-children)
  6863. (org-show-entry))
  6864. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6865. (setq tags (and (match-end 2) (match-string 2)))
  6866. (and (match-end 1)
  6867. (delete-region (match-beginning 1) (match-end 1)))
  6868. (setq pos (point-at-bol))
  6869. (or split (end-of-line 1))
  6870. (delete-horizontal-space)
  6871. (if (string-match "\\`\\*+\\'"
  6872. (buffer-substring (point-at-bol) (point)))
  6873. (insert " "))
  6874. (newline (if blank 2 1))
  6875. (when tags
  6876. (save-excursion
  6877. (goto-char pos)
  6878. (end-of-line 1)
  6879. (insert " " tags)
  6880. (org-set-tags nil 'align))))
  6881. (t
  6882. (or split (end-of-line 1))
  6883. (newline (cond ((and blank (not on-empty-line)) 2)
  6884. (blank 1)
  6885. (on-empty-line 0) (t 1)))))))
  6886. (insert head) (just-one-space)
  6887. (setq pos (point))
  6888. (end-of-line 1)
  6889. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6890. (when (and org-insert-heading-respect-content hide-previous)
  6891. (save-excursion
  6892. (goto-char previous-pos)
  6893. (hide-subtree)))
  6894. (when (and begn endn)
  6895. (narrow-to-region (min (point) begn) (max (point) endn)))
  6896. (run-hooks 'org-insert-heading-hook)))))))
  6897. (defun org-get-heading (&optional no-tags no-todo)
  6898. "Return the heading of the current entry, without the stars.
  6899. When NO-TAGS is non-nil, don't include tags.
  6900. When NO-TODO is non-nil, don't include TODO keywords."
  6901. (save-excursion
  6902. (org-back-to-heading t)
  6903. (cond
  6904. ((and no-tags no-todo)
  6905. (looking-at org-complex-heading-regexp)
  6906. (match-string 4))
  6907. (no-tags
  6908. (looking-at (concat org-outline-regexp
  6909. "\\(.*?\\)"
  6910. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6911. (match-string 1))
  6912. (no-todo
  6913. (looking-at org-todo-line-regexp)
  6914. (match-string 3))
  6915. (t (looking-at org-heading-regexp)
  6916. (match-string 2)))))
  6917. (defvar orgstruct-mode) ; defined below
  6918. (defun org-heading-components ()
  6919. "Return the components of the current heading.
  6920. This is a list with the following elements:
  6921. - the level as an integer
  6922. - the reduced level, different if `org-odd-levels-only' is set.
  6923. - the TODO keyword, or nil
  6924. - the priority character, like ?A, or nil if no priority is given
  6925. - the headline text itself, or the tags string if no headline text
  6926. - the tags string, or nil."
  6927. (save-excursion
  6928. (org-back-to-heading t)
  6929. (if (let (case-fold-search)
  6930. (looking-at
  6931. (if orgstruct-mode
  6932. org-heading-regexp
  6933. org-complex-heading-regexp)))
  6934. (if orgstruct-mode
  6935. (list (length (match-string 1))
  6936. (org-reduced-level (length (match-string 1)))
  6937. nil
  6938. nil
  6939. (match-string 2)
  6940. nil)
  6941. (list (length (match-string 1))
  6942. (org-reduced-level (length (match-string 1)))
  6943. (org-match-string-no-properties 2)
  6944. (and (match-end 3) (aref (match-string 3) 2))
  6945. (org-match-string-no-properties 4)
  6946. (org-match-string-no-properties 5))))))
  6947. (defun org-get-entry ()
  6948. "Get the entry text, after heading, entire subtree."
  6949. (save-excursion
  6950. (org-back-to-heading t)
  6951. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6952. (defun org-insert-heading-after-current ()
  6953. "Insert a new heading with same level as current, after current subtree."
  6954. (interactive)
  6955. (org-back-to-heading)
  6956. (org-insert-heading)
  6957. (org-move-subtree-down)
  6958. (end-of-line 1))
  6959. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6960. "Insert heading with `org-insert-heading-respect-content' set to t."
  6961. (interactive "P")
  6962. (let ((org-insert-heading-respect-content t))
  6963. (org-insert-heading arg invisible-ok)))
  6964. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6965. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6966. (interactive "P")
  6967. (let ((org-insert-heading-respect-content t))
  6968. (org-insert-todo-heading force-state t)))
  6969. (defun org-insert-todo-heading (arg &optional force-heading)
  6970. "Insert a new heading with the same level and TODO state as current heading.
  6971. If the heading has no TODO state, or if the state is DONE, use the first
  6972. state (TODO by default). Also one prefix arg, force first state. With two
  6973. prefix args, force inserting at the end of the parent subtree."
  6974. (interactive "P")
  6975. (when (or force-heading (not (org-insert-item 'checkbox)))
  6976. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6977. force-heading))
  6978. (save-excursion
  6979. (org-back-to-heading)
  6980. (outline-previous-heading)
  6981. (looking-at org-todo-line-regexp))
  6982. (let*
  6983. ((new-mark-x
  6984. (if (or arg
  6985. (not (match-beginning 2))
  6986. (member (match-string 2) org-done-keywords))
  6987. (car org-todo-keywords-1)
  6988. (match-string 2)))
  6989. (new-mark
  6990. (or
  6991. (run-hook-with-args-until-success
  6992. 'org-todo-get-default-hook new-mark-x nil)
  6993. new-mark-x)))
  6994. (beginning-of-line 1)
  6995. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6996. (if org-treat-insert-todo-heading-as-state-change
  6997. (org-todo new-mark)
  6998. (insert new-mark " "))))
  6999. (when org-provide-todo-statistics
  7000. (org-update-parent-todo-statistics))))
  7001. (defun org-insert-subheading (arg)
  7002. "Insert a new subheading and demote it.
  7003. Works for outline headings and for plain lists alike."
  7004. (interactive "P")
  7005. (org-insert-heading arg)
  7006. (cond
  7007. ((org-at-heading-p) (org-do-demote))
  7008. ((org-at-item-p) (org-indent-item))))
  7009. (defun org-insert-todo-subheading (arg)
  7010. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7011. Works for outline headings and for plain lists alike."
  7012. (interactive "P")
  7013. (org-insert-todo-heading arg)
  7014. (cond
  7015. ((org-at-heading-p) (org-do-demote))
  7016. ((org-at-item-p) (org-indent-item))))
  7017. ;;; Promotion and Demotion
  7018. (defvar org-after-demote-entry-hook nil
  7019. "Hook run after an entry has been demoted.
  7020. The cursor will be at the beginning of the entry.
  7021. When a subtree is being demoted, the hook will be called for each node.")
  7022. (defvar org-after-promote-entry-hook nil
  7023. "Hook run after an entry has been promoted.
  7024. The cursor will be at the beginning of the entry.
  7025. When a subtree is being promoted, the hook will be called for each node.")
  7026. (defun org-promote-subtree ()
  7027. "Promote the entire subtree.
  7028. See also `org-promote'."
  7029. (interactive)
  7030. (save-excursion
  7031. (org-with-limited-levels (org-map-tree 'org-promote)))
  7032. (org-fix-position-after-promote))
  7033. (defun org-demote-subtree ()
  7034. "Demote the entire subtree. See `org-demote'.
  7035. See also `org-promote'."
  7036. (interactive)
  7037. (save-excursion
  7038. (org-with-limited-levels (org-map-tree 'org-demote)))
  7039. (org-fix-position-after-promote))
  7040. (defun org-do-promote ()
  7041. "Promote the current heading higher up the tree.
  7042. If the region is active in `transient-mark-mode', promote all headings
  7043. in the region."
  7044. (interactive)
  7045. (save-excursion
  7046. (if (org-region-active-p)
  7047. (org-map-region 'org-promote (region-beginning) (region-end))
  7048. (org-promote)))
  7049. (org-fix-position-after-promote))
  7050. (defun org-do-demote ()
  7051. "Demote the current heading lower down the tree.
  7052. If the region is active in `transient-mark-mode', demote all headings
  7053. in the region."
  7054. (interactive)
  7055. (save-excursion
  7056. (if (org-region-active-p)
  7057. (org-map-region 'org-demote (region-beginning) (region-end))
  7058. (org-demote)))
  7059. (org-fix-position-after-promote))
  7060. (defun org-fix-position-after-promote ()
  7061. "Make sure that after pro/demotion cursor position is right."
  7062. (let ((pos (point)))
  7063. (when (save-excursion
  7064. (beginning-of-line 1)
  7065. (looking-at org-todo-line-regexp)
  7066. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7067. (cond ((eobp) (insert " "))
  7068. ((eolp) (insert " "))
  7069. ((equal (char-after) ?\ ) (forward-char 1))))))
  7070. (defun org-current-level ()
  7071. "Return the level of the current entry, or nil if before the first headline.
  7072. The level is the number of stars at the beginning of the headline."
  7073. (save-excursion
  7074. (org-with-limited-levels
  7075. (if (ignore-errors (org-back-to-heading t))
  7076. (funcall outline-level)))))
  7077. (defun org-get-previous-line-level ()
  7078. "Return the outline depth of the last headline before the current line.
  7079. Returns 0 for the first headline in the buffer, and nil if before the
  7080. first headline."
  7081. (let ((current-level (org-current-level))
  7082. (prev-level (when (> (line-number-at-pos) 1)
  7083. (save-excursion
  7084. (beginning-of-line 0)
  7085. (org-current-level)))))
  7086. (cond ((null current-level) nil) ; Before first headline
  7087. ((null prev-level) 0) ; At first headline
  7088. (prev-level))))
  7089. (defun org-reduced-level (l)
  7090. "Compute the effective level of a heading.
  7091. This takes into account the setting of `org-odd-levels-only'."
  7092. (cond
  7093. ((zerop l) 0)
  7094. (org-odd-levels-only (1+ (floor (/ l 2))))
  7095. (t l)))
  7096. (defun org-level-increment ()
  7097. "Return the number of stars that will be added or removed at a
  7098. time to headlines when structure editing, based on the value of
  7099. `org-odd-levels-only'."
  7100. (if org-odd-levels-only 2 1))
  7101. (defun org-get-valid-level (level &optional change)
  7102. "Rectify a level change under the influence of `org-odd-levels-only'
  7103. LEVEL is a current level, CHANGE is by how much the level should be
  7104. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7105. even level numbers will become the next higher odd number."
  7106. (if org-odd-levels-only
  7107. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7108. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7109. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7110. (max 1 (+ level (or change 0)))))
  7111. (if (boundp 'define-obsolete-function-alias)
  7112. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7113. (define-obsolete-function-alias 'org-get-legal-level
  7114. 'org-get-valid-level)
  7115. (define-obsolete-function-alias 'org-get-legal-level
  7116. 'org-get-valid-level "23.1")))
  7117. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  7118. ;; ̀org-with-limited-levels'
  7119. (defun org-promote ()
  7120. "Promote the current heading higher up the tree.
  7121. If the region is active in `transient-mark-mode', promote all headings
  7122. in the region."
  7123. (org-back-to-heading t)
  7124. (let* ((level (save-match-data (funcall outline-level)))
  7125. (after-change-functions (remove 'flyspell-after-change-function
  7126. after-change-functions))
  7127. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7128. (diff (abs (- level (length up-head) -1))))
  7129. (cond ((and (= level 1) org-called-with-limited-levels
  7130. org-allow-promoting-top-level-subtree)
  7131. (replace-match "# " nil t))
  7132. ((= level 1)
  7133. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7134. (t (replace-match up-head nil t)))
  7135. ;; Fixup tag positioning
  7136. (unless (= level 1)
  7137. (and org-auto-align-tags (org-set-tags nil t))
  7138. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7139. (run-hooks 'org-after-promote-entry-hook)))
  7140. (defun org-demote ()
  7141. "Demote the current heading lower down the tree.
  7142. If the region is active in `transient-mark-mode', demote all headings
  7143. in the region."
  7144. (org-back-to-heading t)
  7145. (let* ((level (save-match-data (funcall outline-level)))
  7146. (after-change-functions (remove 'flyspell-after-change-function
  7147. after-change-functions))
  7148. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7149. (diff (abs (- level (length down-head) -1))))
  7150. (replace-match down-head nil t)
  7151. ;; Fixup tag positioning
  7152. (and org-auto-align-tags (org-set-tags nil t))
  7153. (if org-adapt-indentation (org-fixup-indentation diff))
  7154. (run-hooks 'org-after-demote-entry-hook)))
  7155. (defun org-cycle-level ()
  7156. "Cycle the level of an empty headline through possible states.
  7157. This goes first to child, then to parent, level, then up the hierarchy.
  7158. After top level, it switches back to sibling level."
  7159. (interactive)
  7160. (let ((org-adapt-indentation nil))
  7161. (when (org-point-at-end-of-empty-headline)
  7162. (setq this-command 'org-cycle-level) ; Only needed for caching
  7163. (let ((cur-level (org-current-level))
  7164. (prev-level (org-get-previous-line-level)))
  7165. (cond
  7166. ;; If first headline in file, promote to top-level.
  7167. ((= prev-level 0)
  7168. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7169. do (org-do-promote)))
  7170. ;; If same level as prev, demote one.
  7171. ((= prev-level cur-level)
  7172. (org-do-demote))
  7173. ;; If parent is top-level, promote to top level if not already.
  7174. ((= prev-level 1)
  7175. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7176. do (org-do-promote)))
  7177. ;; If top-level, return to prev-level.
  7178. ((= cur-level 1)
  7179. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7180. do (org-do-demote)))
  7181. ;; If less than prev-level, promote one.
  7182. ((< cur-level prev-level)
  7183. (org-do-promote))
  7184. ;; If deeper than prev-level, promote until higher than
  7185. ;; prev-level.
  7186. ((> cur-level prev-level)
  7187. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7188. do (org-do-promote))))
  7189. t))))
  7190. (defun org-map-tree (fun)
  7191. "Call FUN for every heading underneath the current one."
  7192. (org-back-to-heading)
  7193. (let ((level (funcall outline-level)))
  7194. (save-excursion
  7195. (funcall fun)
  7196. (while (and (progn
  7197. (outline-next-heading)
  7198. (> (funcall outline-level) level))
  7199. (not (eobp)))
  7200. (funcall fun)))))
  7201. (defun org-map-region (fun beg end)
  7202. "Call FUN for every heading between BEG and END."
  7203. (let ((org-ignore-region t))
  7204. (save-excursion
  7205. (setq end (copy-marker end))
  7206. (goto-char beg)
  7207. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7208. (< (point) end))
  7209. (funcall fun))
  7210. (while (and (progn
  7211. (outline-next-heading)
  7212. (< (point) end))
  7213. (not (eobp)))
  7214. (funcall fun)))))
  7215. (defvar org-property-end-re) ; silence byte-compiler
  7216. (defun org-fixup-indentation (diff)
  7217. "Change the indentation in the current entry by DIFF.
  7218. However, if any line in the current entry has no indentation, or if it
  7219. would end up with no indentation after the change, nothing at all is done."
  7220. (save-excursion
  7221. (let ((end (save-excursion (outline-next-heading)
  7222. (point-marker)))
  7223. (prohibit (if (> diff 0)
  7224. "^\\S-"
  7225. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7226. col)
  7227. (unless (save-excursion (end-of-line 1)
  7228. (re-search-forward prohibit end t))
  7229. (while (and (< (point) end)
  7230. (re-search-forward "^[ \t]+" end t))
  7231. (goto-char (match-end 0))
  7232. (setq col (current-column))
  7233. (if (< diff 0) (replace-match ""))
  7234. (org-indent-to-column (+ diff col))))
  7235. (move-marker end nil))))
  7236. (defun org-convert-to-odd-levels ()
  7237. "Convert an org-mode file with all levels allowed to one with odd levels.
  7238. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7239. level 5 etc."
  7240. (interactive)
  7241. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7242. (let ((outline-level 'org-outline-level)
  7243. (org-odd-levels-only nil) n)
  7244. (save-excursion
  7245. (goto-char (point-min))
  7246. (while (re-search-forward "^\\*\\*+ " nil t)
  7247. (setq n (- (length (match-string 0)) 2))
  7248. (while (>= (setq n (1- n)) 0)
  7249. (org-demote))
  7250. (end-of-line 1))))))
  7251. (defun org-convert-to-oddeven-levels ()
  7252. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7253. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7254. file contains a section with an even level, conversion would
  7255. destroy the structure of the file. An error is signaled in this
  7256. case."
  7257. (interactive)
  7258. (goto-char (point-min))
  7259. ;; First check if there are no even levels
  7260. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7261. (org-show-context t)
  7262. (error "Not all levels are odd in this file. Conversion not possible"))
  7263. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7264. (let ((outline-regexp org-outline-regexp)
  7265. (outline-level 'org-outline-level)
  7266. (org-odd-levels-only nil) n)
  7267. (save-excursion
  7268. (goto-char (point-min))
  7269. (while (re-search-forward "^\\*\\*+ " nil t)
  7270. (setq n (/ (1- (length (match-string 0))) 2))
  7271. (while (>= (setq n (1- n)) 0)
  7272. (org-promote))
  7273. (end-of-line 1))))))
  7274. (defun org-tr-level (n)
  7275. "Make N odd if required."
  7276. (if org-odd-levels-only (1+ (/ n 2)) n))
  7277. ;;; Vertical tree motion, cutting and pasting of subtrees
  7278. (defun org-move-subtree-up (&optional arg)
  7279. "Move the current subtree up past ARG headlines of the same level."
  7280. (interactive "p")
  7281. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7282. (defun org-move-subtree-down (&optional arg)
  7283. "Move the current subtree down past ARG headlines of the same level."
  7284. (interactive "p")
  7285. (setq arg (prefix-numeric-value arg))
  7286. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7287. 'org-get-last-sibling))
  7288. (ins-point (make-marker))
  7289. (cnt (abs arg))
  7290. (col (current-column))
  7291. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7292. ;; Select the tree
  7293. (org-back-to-heading)
  7294. (setq beg0 (point))
  7295. (save-excursion
  7296. (setq ne-beg (org-back-over-empty-lines))
  7297. (setq beg (point)))
  7298. (save-match-data
  7299. (save-excursion (outline-end-of-heading)
  7300. (setq folded (outline-invisible-p)))
  7301. (outline-end-of-subtree))
  7302. (outline-next-heading)
  7303. (setq ne-end (org-back-over-empty-lines))
  7304. (setq end (point))
  7305. (goto-char beg0)
  7306. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7307. ;; include less whitespace
  7308. (save-excursion
  7309. (goto-char beg)
  7310. (forward-line (- ne-beg ne-end))
  7311. (setq beg (point))))
  7312. ;; Find insertion point, with error handling
  7313. (while (> cnt 0)
  7314. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7315. (progn (goto-char beg0)
  7316. (user-error "Cannot move past superior level or buffer limit")))
  7317. (setq cnt (1- cnt)))
  7318. (if (> arg 0)
  7319. ;; Moving forward - still need to move over subtree
  7320. (progn (org-end-of-subtree t t)
  7321. (save-excursion
  7322. (org-back-over-empty-lines)
  7323. (or (bolp) (newline)))))
  7324. (setq ne-ins (org-back-over-empty-lines))
  7325. (move-marker ins-point (point))
  7326. (setq txt (buffer-substring beg end))
  7327. (org-save-markers-in-region beg end)
  7328. (delete-region beg end)
  7329. (org-remove-empty-overlays-at beg)
  7330. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7331. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7332. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7333. (let ((bbb (point)))
  7334. (insert-before-markers txt)
  7335. (org-reinstall-markers-in-region bbb)
  7336. (move-marker ins-point bbb))
  7337. (or (bolp) (insert "\n"))
  7338. (setq ins-end (point))
  7339. (goto-char ins-point)
  7340. (org-skip-whitespace)
  7341. (when (and (< arg 0)
  7342. (org-first-sibling-p)
  7343. (> ne-ins ne-beg))
  7344. ;; Move whitespace back to beginning
  7345. (save-excursion
  7346. (goto-char ins-end)
  7347. (let ((kill-whole-line t))
  7348. (kill-line (- ne-ins ne-beg)) (point)))
  7349. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7350. (move-marker ins-point nil)
  7351. (if folded
  7352. (hide-subtree)
  7353. (org-show-entry)
  7354. (show-children)
  7355. (org-cycle-hide-drawers 'children))
  7356. (org-clean-visibility-after-subtree-move)
  7357. ;; move back to the initial column we were at
  7358. (move-to-column col)))
  7359. (defvar org-subtree-clip ""
  7360. "Clipboard for cut and paste of subtrees.
  7361. This is actually only a copy of the kill, because we use the normal kill
  7362. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7363. (defvar org-subtree-clip-folded nil
  7364. "Was the last copied subtree folded?
  7365. This is used to fold the tree back after pasting.")
  7366. (defun org-cut-subtree (&optional n)
  7367. "Cut the current subtree into the clipboard.
  7368. With prefix arg N, cut this many sequential subtrees.
  7369. This is a short-hand for marking the subtree and then cutting it."
  7370. (interactive "p")
  7371. (org-copy-subtree n 'cut))
  7372. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7373. "Copy the current subtree it in the clipboard.
  7374. With prefix arg N, copy this many sequential subtrees.
  7375. This is a short-hand for marking the subtree and then copying it.
  7376. If CUT is non-nil, actually cut the subtree.
  7377. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7378. of some markers in the region, even if CUT is non-nil. This is
  7379. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7380. (interactive "p")
  7381. (let (beg end folded (beg0 (point)))
  7382. (if (org-called-interactively-p 'any)
  7383. (org-back-to-heading nil) ; take what looks like a subtree
  7384. (org-back-to-heading t)) ; take what is really there
  7385. (setq beg (point))
  7386. (skip-chars-forward " \t\r\n")
  7387. (save-match-data
  7388. (if nosubtrees
  7389. (outline-next-heading)
  7390. (save-excursion (outline-end-of-heading)
  7391. (setq folded (outline-invisible-p)))
  7392. (condition-case nil
  7393. (org-forward-heading-same-level (1- n) t)
  7394. (error nil))
  7395. (org-end-of-subtree t t)))
  7396. (setq end (point))
  7397. (goto-char beg0)
  7398. (when (> end beg)
  7399. (setq org-subtree-clip-folded folded)
  7400. (when (or cut force-store-markers)
  7401. (org-save-markers-in-region beg end))
  7402. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7403. (setq org-subtree-clip (current-kill 0))
  7404. (message "%s: Subtree(s) with %d characters"
  7405. (if cut "Cut" "Copied")
  7406. (length org-subtree-clip)))))
  7407. (defun org-paste-subtree (&optional level tree for-yank)
  7408. "Paste the clipboard as a subtree, with modification of headline level.
  7409. The entire subtree is promoted or demoted in order to match a new headline
  7410. level.
  7411. If the cursor is at the beginning of a headline, the same level as
  7412. that headline is used to paste the tree.
  7413. If not, the new level is derived from the *visible* headings
  7414. before and after the insertion point, and taken to be the inferior headline
  7415. level of the two. So if the previous visible heading is level 3 and the
  7416. next is level 4 (or vice versa), level 4 will be used for insertion.
  7417. This makes sure that the subtree remains an independent subtree and does
  7418. not swallow low level entries.
  7419. You can also force a different level, either by using a numeric prefix
  7420. argument, or by inserting the heading marker by hand. For example, if the
  7421. cursor is after \"*****\", then the tree will be shifted to level 5.
  7422. If optional TREE is given, use this text instead of the kill ring.
  7423. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7424. move back over whitespace before inserting, and move point to the end of
  7425. the inserted text when done."
  7426. (interactive "P")
  7427. (setq tree (or tree (and kill-ring (current-kill 0))))
  7428. (unless (org-kill-is-subtree-p tree)
  7429. (user-error "%s"
  7430. (substitute-command-keys
  7431. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7432. (org-with-limited-levels
  7433. (let* ((visp (not (outline-invisible-p)))
  7434. (txt tree)
  7435. (^re_ "\\(\\*+\\)[ \t]*")
  7436. (old-level (if (string-match org-outline-regexp-bol txt)
  7437. (- (match-end 0) (match-beginning 0) 1)
  7438. -1))
  7439. (force-level (cond (level (prefix-numeric-value level))
  7440. ((and (looking-at "[ \t]*$")
  7441. (string-match
  7442. "^\\*+$" (buffer-substring
  7443. (point-at-bol) (point))))
  7444. (- (match-end 1) (match-beginning 1)))
  7445. ((and (bolp)
  7446. (looking-at org-outline-regexp))
  7447. (- (match-end 0) (point) 1))))
  7448. (previous-level (save-excursion
  7449. (condition-case nil
  7450. (progn
  7451. (outline-previous-visible-heading 1)
  7452. (if (looking-at ^re_)
  7453. (- (match-end 0) (match-beginning 0) 1)
  7454. 1))
  7455. (error 1))))
  7456. (next-level (save-excursion
  7457. (condition-case nil
  7458. (progn
  7459. (or (looking-at org-outline-regexp)
  7460. (outline-next-visible-heading 1))
  7461. (if (looking-at ^re_)
  7462. (- (match-end 0) (match-beginning 0) 1)
  7463. 1))
  7464. (error 1))))
  7465. (new-level (or force-level (max previous-level next-level)))
  7466. (shift (if (or (= old-level -1)
  7467. (= new-level -1)
  7468. (= old-level new-level))
  7469. 0
  7470. (- new-level old-level)))
  7471. (delta (if (> shift 0) -1 1))
  7472. (func (if (> shift 0) 'org-demote 'org-promote))
  7473. (org-odd-levels-only nil)
  7474. beg end newend)
  7475. ;; Remove the forced level indicator
  7476. (if force-level
  7477. (delete-region (point-at-bol) (point)))
  7478. ;; Paste
  7479. (beginning-of-line (if (bolp) 1 2))
  7480. (setq beg (point))
  7481. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7482. (insert-before-markers txt)
  7483. (unless (string-match "\n\\'" txt) (insert "\n"))
  7484. (setq newend (point))
  7485. (org-reinstall-markers-in-region beg)
  7486. (setq end (point))
  7487. (goto-char beg)
  7488. (skip-chars-forward " \t\n\r")
  7489. (setq beg (point))
  7490. (if (and (outline-invisible-p) visp)
  7491. (save-excursion (outline-show-heading)))
  7492. ;; Shift if necessary
  7493. (unless (= shift 0)
  7494. (save-restriction
  7495. (narrow-to-region beg end)
  7496. (while (not (= shift 0))
  7497. (org-map-region func (point-min) (point-max))
  7498. (setq shift (+ delta shift)))
  7499. (goto-char (point-min))
  7500. (setq newend (point-max))))
  7501. (when (or (org-called-interactively-p 'interactive) for-yank)
  7502. (message "Clipboard pasted as level %d subtree" new-level))
  7503. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7504. kill-ring
  7505. (eq org-subtree-clip (current-kill 0))
  7506. org-subtree-clip-folded)
  7507. ;; The tree was folded before it was killed/copied
  7508. (hide-subtree))
  7509. (and for-yank (goto-char newend)))))
  7510. (defun org-kill-is-subtree-p (&optional txt)
  7511. "Check if the current kill is an outline subtree, or a set of trees.
  7512. Returns nil if kill does not start with a headline, or if the first
  7513. headline level is not the largest headline level in the tree.
  7514. So this will actually accept several entries of equal levels as well,
  7515. which is OK for `org-paste-subtree'.
  7516. If optional TXT is given, check this string instead of the current kill."
  7517. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7518. (re (org-get-limited-outline-regexp))
  7519. (^re (concat "^" re))
  7520. (start-level (and kill
  7521. (string-match
  7522. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7523. kill)
  7524. (- (match-end 2) (match-beginning 2) 1)))
  7525. (start (1+ (or (match-beginning 2) -1))))
  7526. (if (not start-level)
  7527. (progn
  7528. nil) ;; does not even start with a heading
  7529. (catch 'exit
  7530. (while (setq start (string-match ^re kill (1+ start)))
  7531. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7532. (throw 'exit nil)))
  7533. t))))
  7534. (defvar org-markers-to-move nil
  7535. "Markers that should be moved with a cut-and-paste operation.
  7536. Those markers are stored together with their positions relative to
  7537. the start of the region.")
  7538. (defun org-save-markers-in-region (beg end)
  7539. "Check markers in region.
  7540. If these markers are between BEG and END, record their position relative
  7541. to BEG, so that after moving the block of text, we can put the markers back
  7542. into place.
  7543. This function gets called just before an entry or tree gets cut from the
  7544. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7545. called immediately, to move the markers with the entries."
  7546. (setq org-markers-to-move nil)
  7547. (when (featurep 'org-clock)
  7548. (org-clock-save-markers-for-cut-and-paste beg end))
  7549. (when (featurep 'org-agenda)
  7550. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7551. (defun org-check-and-save-marker (marker beg end)
  7552. "Check if MARKER is between BEG and END.
  7553. If yes, remember the marker and the distance to BEG."
  7554. (when (and (marker-buffer marker)
  7555. (equal (marker-buffer marker) (current-buffer)))
  7556. (if (and (>= marker beg) (< marker end))
  7557. (push (cons marker (- marker beg)) org-markers-to-move))))
  7558. (defun org-reinstall-markers-in-region (beg)
  7559. "Move all remembered markers to their position relative to BEG."
  7560. (mapc (lambda (x)
  7561. (move-marker (car x) (+ beg (cdr x))))
  7562. org-markers-to-move)
  7563. (setq org-markers-to-move nil))
  7564. (defun org-narrow-to-subtree ()
  7565. "Narrow buffer to the current subtree."
  7566. (interactive)
  7567. (save-excursion
  7568. (save-match-data
  7569. (org-with-limited-levels
  7570. (narrow-to-region
  7571. (progn (org-back-to-heading t) (point))
  7572. (progn (org-end-of-subtree t t)
  7573. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7574. (point)))))))
  7575. (defun org-narrow-to-block ()
  7576. "Narrow buffer to the current block."
  7577. (interactive)
  7578. (let* ((case-fold-search t)
  7579. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7580. "^[ \t]*#\\+end_.*")))
  7581. (if blockp
  7582. (narrow-to-region (car blockp) (cdr blockp))
  7583. (user-error "Not in a block"))))
  7584. (eval-when-compile
  7585. (defvar org-property-drawer-re))
  7586. (defvar org-property-start-re) ;; defined below
  7587. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7588. "Clone the task (subtree) at point N times.
  7589. The clones will be inserted as siblings.
  7590. In interactive use, the user will be prompted for the number of
  7591. clones to be produced. If the entry has a timestamp, the user
  7592. will also be prompted for a time shift, which may be a repeater
  7593. as used in time stamps, for example `+3d'. To disable this,
  7594. you can call the function with a universal prefix argument.
  7595. When a valid repeater is given and the entry contains any time
  7596. stamps, the clones will become a sequence in time, with time
  7597. stamps in the subtree shifted for each clone produced. If SHIFT
  7598. is nil or the empty string, time stamps will be left alone. The
  7599. ID property of the original subtree is removed.
  7600. If the original subtree did contain time stamps with a repeater,
  7601. the following will happen:
  7602. - the repeater will be removed in each clone
  7603. - an additional clone will be produced, with the current, unshifted
  7604. date(s) in the entry.
  7605. - the original entry will be placed *after* all the clones, with
  7606. repeater intact.
  7607. - the start days in the repeater in the original entry will be shifted
  7608. to past the last clone.
  7609. In this way you can spell out a number of instances of a repeating task,
  7610. and still retain the repeater to cover future instances of the task."
  7611. (interactive "nNumber of clones to produce: ")
  7612. (let ((shift
  7613. (or shift
  7614. (if (and (not (equal current-prefix-arg '(4)))
  7615. (save-excursion
  7616. (re-search-forward org-ts-regexp-both
  7617. (save-excursion
  7618. (org-end-of-subtree t)
  7619. (point)) t)))
  7620. (read-from-minibuffer
  7621. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7622. ""))) ;; No time shift
  7623. (n-no-remove -1)
  7624. (drawer-re org-drawer-regexp)
  7625. beg end template task idprop
  7626. shift-n shift-what doshift nmin nmax)
  7627. (if (not (and (integerp n) (> n 0)))
  7628. (error "Invalid number of replications %s" n))
  7629. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7630. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7631. shift)))
  7632. (error "Invalid shift specification %s" shift))
  7633. (when doshift
  7634. (setq shift-n (string-to-number (match-string 1 shift))
  7635. shift-what (cdr (assoc (match-string 2 shift)
  7636. '(("d" . day) ("w" . week)
  7637. ("m" . month) ("y" . year))))))
  7638. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7639. (setq nmin 1 nmax n)
  7640. (org-back-to-heading t)
  7641. (setq beg (point))
  7642. (setq idprop (org-entry-get nil "ID"))
  7643. (org-end-of-subtree t t)
  7644. (or (bolp) (insert "\n"))
  7645. (setq end (point))
  7646. (setq template (buffer-substring beg end))
  7647. (when (and doshift
  7648. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7649. (delete-region beg end)
  7650. (setq end beg)
  7651. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7652. (goto-char end)
  7653. (loop for n from nmin to nmax do
  7654. ;; prepare clone
  7655. (with-temp-buffer
  7656. (insert template)
  7657. (org-mode)
  7658. (goto-char (point-min))
  7659. (org-show-subtree)
  7660. (and idprop (if org-clone-delete-id
  7661. (org-entry-delete nil "ID")
  7662. (org-id-get-create t)))
  7663. (unless (= n 0)
  7664. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7665. (kill-whole-line))
  7666. (goto-char (point-min))
  7667. (while (re-search-forward drawer-re nil t)
  7668. (mapc (lambda (d)
  7669. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7670. (goto-char (point-min))
  7671. (when doshift
  7672. (while (re-search-forward org-ts-regexp-both nil t)
  7673. (org-timestamp-change (* n shift-n) shift-what))
  7674. (unless (= n n-no-remove)
  7675. (goto-char (point-min))
  7676. (while (re-search-forward org-ts-regexp nil t)
  7677. (save-excursion
  7678. (goto-char (match-beginning 0))
  7679. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7680. (delete-region (match-beginning 1) (match-end 1)))))))
  7681. (setq task (buffer-string)))
  7682. (insert task))
  7683. (goto-char beg)))
  7684. ;;; Outline Sorting
  7685. (defun org-sort (with-case)
  7686. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7687. Optional argument WITH-CASE means sort case-sensitively."
  7688. (interactive "P")
  7689. (cond
  7690. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7691. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7692. (t
  7693. (org-call-with-arg 'org-sort-entries with-case))))
  7694. (defun org-sort-remove-invisible (s)
  7695. "Remove invisible links from string S."
  7696. (remove-text-properties 0 (length s) org-rm-props s)
  7697. (while (string-match org-bracket-link-regexp s)
  7698. (setq s (replace-match (if (match-end 2)
  7699. (match-string 3 s)
  7700. (match-string 1 s)) t t s)))
  7701. (let ((st (format " %s " s)))
  7702. (while (string-match org-emph-re st)
  7703. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7704. (setq s (substring st 1 -1)))
  7705. s)
  7706. (defvar org-priority-regexp) ; defined later in the file
  7707. (defvar org-after-sorting-entries-or-items-hook nil
  7708. "Hook that is run after a bunch of entries or items have been sorted.
  7709. When children are sorted, the cursor is in the parent line when this
  7710. hook gets called. When a region or a plain list is sorted, the cursor
  7711. will be in the first entry of the sorted region/list.")
  7712. (defun org-sort-entries
  7713. (&optional with-case sorting-type getkey-func compare-func property)
  7714. "Sort entries on a certain level of an outline tree.
  7715. If there is an active region, the entries in the region are sorted.
  7716. Else, if the cursor is before the first entry, sort the top-level items.
  7717. Else, the children of the entry at point are sorted.
  7718. Sorting can be alphabetically, numerically, by date/time as given by
  7719. a time stamp, by a property, by priority order, or by a custom function.
  7720. The command prompts for the sorting type unless it has been given to the
  7721. function through the SORTING-TYPE argument, which needs to be a character,
  7722. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7723. precise meaning of each character:
  7724. n Numerically, by converting the beginning of the entry/item to a number.
  7725. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7726. o By order of TODO keywords.
  7727. t By date/time, either the first active time stamp in the entry, or, if
  7728. none exist, by the first inactive one.
  7729. s By the scheduled date/time.
  7730. d By deadline date/time.
  7731. c By creation time, which is assumed to be the first inactive time stamp
  7732. at the beginning of a line.
  7733. p By priority according to the cookie.
  7734. r By the value of a property.
  7735. Capital letters will reverse the sort order.
  7736. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7737. called with point at the beginning of the record. It must return either
  7738. a string or a number that should serve as the sorting key for that record.
  7739. Comparing entries ignores case by default. However, with an optional argument
  7740. WITH-CASE, the sorting considers case as well.
  7741. Sorting is done against the visible part of the headlines, it ignores hidden
  7742. links."
  7743. (interactive "P")
  7744. (let ((case-func (if with-case 'identity 'downcase))
  7745. (cmstr
  7746. ;; The clock marker is lost when using `sort-subr', let's
  7747. ;; store the clocking string.
  7748. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7749. (save-excursion
  7750. (goto-char org-clock-marker)
  7751. (looking-back "^.*") (match-string-no-properties 0))))
  7752. start beg end stars re re2
  7753. txt what tmp)
  7754. ;; Find beginning and end of region to sort
  7755. (cond
  7756. ((org-region-active-p)
  7757. ;; we will sort the region
  7758. (setq end (region-end)
  7759. what "region")
  7760. (goto-char (region-beginning))
  7761. (if (not (org-at-heading-p)) (outline-next-heading))
  7762. (setq start (point)))
  7763. ((or (org-at-heading-p)
  7764. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7765. ;; we will sort the children of the current headline
  7766. (org-back-to-heading)
  7767. (setq start (point)
  7768. end (progn (org-end-of-subtree t t)
  7769. (or (bolp) (insert "\n"))
  7770. (org-back-over-empty-lines)
  7771. (point))
  7772. what "children")
  7773. (goto-char start)
  7774. (show-subtree)
  7775. (outline-next-heading))
  7776. (t
  7777. ;; we will sort the top-level entries in this file
  7778. (goto-char (point-min))
  7779. (or (org-at-heading-p) (outline-next-heading))
  7780. (setq start (point))
  7781. (goto-char (point-max))
  7782. (beginning-of-line 1)
  7783. (when (looking-at ".*?\\S-")
  7784. ;; File ends in a non-white line
  7785. (end-of-line 1)
  7786. (insert "\n"))
  7787. (setq end (point-max))
  7788. (setq what "top-level")
  7789. (goto-char start)
  7790. (show-all)))
  7791. (setq beg (point))
  7792. (if (>= beg end) (user-error "Nothing to sort"))
  7793. (looking-at "\\(\\*+\\)")
  7794. (setq stars (match-string 1)
  7795. re (concat "^" (regexp-quote stars) " +")
  7796. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7797. txt (buffer-substring beg end))
  7798. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7799. (if (and (not (equal stars "*")) (string-match re2 txt))
  7800. (user-error "Region to sort contains a level above the first entry"))
  7801. (unless sorting-type
  7802. (message
  7803. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7804. [t]ime [s]cheduled [d]eadline [c]reated
  7805. A/N/P/R/O/F/T/S/D/C means reversed:"
  7806. what)
  7807. (setq sorting-type (read-char-exclusive))
  7808. (unless getkey-func
  7809. (and (= (downcase sorting-type) ?f)
  7810. (setq getkey-func
  7811. (org-icompleting-read "Sort using function: "
  7812. obarray 'fboundp t nil nil))
  7813. (setq getkey-func (intern getkey-func))))
  7814. (and (= (downcase sorting-type) ?r)
  7815. (not property)
  7816. (setq property
  7817. (org-icompleting-read "Property: "
  7818. (mapcar 'list (org-buffer-property-keys t))
  7819. nil t))))
  7820. (message "Sorting entries...")
  7821. (save-restriction
  7822. (narrow-to-region start end)
  7823. (let ((dcst (downcase sorting-type))
  7824. (case-fold-search nil)
  7825. (now (current-time)))
  7826. (sort-subr
  7827. (/= dcst sorting-type)
  7828. ;; This function moves to the beginning character of the "record" to
  7829. ;; be sorted.
  7830. (lambda nil
  7831. (if (re-search-forward re nil t)
  7832. (goto-char (match-beginning 0))
  7833. (goto-char (point-max))))
  7834. ;; This function moves to the last character of the "record" being
  7835. ;; sorted.
  7836. (lambda nil
  7837. (save-match-data
  7838. (condition-case nil
  7839. (outline-forward-same-level 1)
  7840. (error
  7841. (goto-char (point-max))))))
  7842. ;; This function returns the value that gets sorted against.
  7843. (lambda nil
  7844. (cond
  7845. ((= dcst ?n)
  7846. (if (looking-at org-complex-heading-regexp)
  7847. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7848. nil))
  7849. ((= dcst ?a)
  7850. (if (looking-at org-complex-heading-regexp)
  7851. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7852. nil))
  7853. ((= dcst ?t)
  7854. (let ((end (save-excursion (outline-next-heading) (point))))
  7855. (if (or (re-search-forward org-ts-regexp end t)
  7856. (re-search-forward org-ts-regexp-both end t))
  7857. (org-time-string-to-seconds (match-string 0))
  7858. (org-float-time now))))
  7859. ((= dcst ?c)
  7860. (let ((end (save-excursion (outline-next-heading) (point))))
  7861. (if (re-search-forward
  7862. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7863. end t)
  7864. (org-time-string-to-seconds (match-string 0))
  7865. (org-float-time now))))
  7866. ((= dcst ?s)
  7867. (let ((end (save-excursion (outline-next-heading) (point))))
  7868. (if (re-search-forward org-scheduled-time-regexp end t)
  7869. (org-time-string-to-seconds (match-string 1))
  7870. (org-float-time now))))
  7871. ((= dcst ?d)
  7872. (let ((end (save-excursion (outline-next-heading) (point))))
  7873. (if (re-search-forward org-deadline-time-regexp end t)
  7874. (org-time-string-to-seconds (match-string 1))
  7875. (org-float-time now))))
  7876. ((= dcst ?p)
  7877. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7878. (string-to-char (match-string 2))
  7879. org-default-priority))
  7880. ((= dcst ?r)
  7881. (or (org-entry-get nil property) ""))
  7882. ((= dcst ?o)
  7883. (if (looking-at org-complex-heading-regexp)
  7884. (- 9999 (length (member (match-string 2)
  7885. org-todo-keywords-1)))))
  7886. ((= dcst ?f)
  7887. (if getkey-func
  7888. (progn
  7889. (setq tmp (funcall getkey-func))
  7890. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7891. tmp)
  7892. (error "Invalid key function `%s'" getkey-func)))
  7893. (t (error "Invalid sorting type `%c'" sorting-type))))
  7894. nil
  7895. (cond
  7896. ((= dcst ?a) 'string<)
  7897. ((= dcst ?f) compare-func)
  7898. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7899. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7900. ;; Reset the clock marker if needed
  7901. (when cmstr
  7902. (save-excursion
  7903. (goto-char start)
  7904. (search-forward cmstr nil t)
  7905. (move-marker org-clock-marker (point))))
  7906. (message "Sorting entries...done")))
  7907. (defun org-do-sort (table what &optional with-case sorting-type)
  7908. "Sort TABLE of WHAT according to SORTING-TYPE.
  7909. The user will be prompted for the SORTING-TYPE if the call to this
  7910. function does not specify it. WHAT is only for the prompt, to indicate
  7911. what is being sorted. The sorting key will be extracted from
  7912. the car of the elements of the table.
  7913. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7914. (unless sorting-type
  7915. (message
  7916. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7917. what)
  7918. (setq sorting-type (read-char-exclusive)))
  7919. (let ((dcst (downcase sorting-type))
  7920. extractfun comparefun)
  7921. ;; Define the appropriate functions
  7922. (cond
  7923. ((= dcst ?n)
  7924. (setq extractfun 'string-to-number
  7925. comparefun (if (= dcst sorting-type) '< '>)))
  7926. ((= dcst ?a)
  7927. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7928. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7929. comparefun (if (= dcst sorting-type)
  7930. 'string<
  7931. (lambda (a b) (and (not (string< a b))
  7932. (not (string= a b)))))))
  7933. ((= dcst ?t)
  7934. (setq extractfun
  7935. (lambda (x)
  7936. (if (or (string-match org-ts-regexp x)
  7937. (string-match org-ts-regexp-both x))
  7938. (org-float-time
  7939. (org-time-string-to-time (match-string 0 x)))
  7940. 0))
  7941. comparefun (if (= dcst sorting-type) '< '>)))
  7942. (t (error "Invalid sorting type `%c'" sorting-type)))
  7943. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7944. table)
  7945. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7946. ;;; The orgstruct minor mode
  7947. ;; Define a minor mode which can be used in other modes in order to
  7948. ;; integrate the org-mode structure editing commands.
  7949. ;; This is really a hack, because the org-mode structure commands use
  7950. ;; keys which normally belong to the major mode. Here is how it
  7951. ;; works: The minor mode defines all the keys necessary to operate the
  7952. ;; structure commands, but wraps the commands into a function which
  7953. ;; tests if the cursor is currently at a headline or a plain list
  7954. ;; item. If that is the case, the structure command is used,
  7955. ;; temporarily setting many Org-mode variables like regular
  7956. ;; expressions for filling etc. However, when any of those keys is
  7957. ;; used at a different location, function uses `key-binding' to look
  7958. ;; up if the key has an associated command in another currently active
  7959. ;; keymap (minor modes, major mode, global), and executes that
  7960. ;; command. There might be problems if any of the keys is otherwise
  7961. ;; used as a prefix key.
  7962. (defcustom orgstruct-heading-prefix-regexp nil
  7963. "Regexp that matches the custom prefix of Org headlines in
  7964. orgstruct(++)-mode."
  7965. :group 'org
  7966. :version "24.4"
  7967. :package-version '(Org . "8.0")
  7968. :type 'string)
  7969. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7970. (defcustom orgstruct-setup-hook nil
  7971. "Hook run after orgstruct-mode-map is filled."
  7972. :group 'org
  7973. :version "24.4"
  7974. :package-version '(Org . "8.0")
  7975. :type 'hook)
  7976. (defvar orgstruct-initialized nil)
  7977. (defvar org-local-vars nil
  7978. "List of local variables, for use by `orgstruct-mode'.")
  7979. ;;;###autoload
  7980. (define-minor-mode orgstruct-mode
  7981. "Toggle the minor mode `orgstruct-mode'.
  7982. This mode is for using Org-mode structure commands in other
  7983. modes. The following keys behave as if Org-mode were active, if
  7984. the cursor is on a headline, or on a plain list item (both as
  7985. defined by Org-mode)."
  7986. nil " OrgStruct" (make-sparse-keymap)
  7987. (funcall (if orgstruct-mode
  7988. 'add-to-invisibility-spec
  7989. 'remove-from-invisibility-spec)
  7990. '(outline . t))
  7991. (when orgstruct-mode
  7992. (org-load-modules-maybe)
  7993. (unless orgstruct-initialized
  7994. (orgstruct-setup)
  7995. (setq orgstruct-initialized t))))
  7996. ;;;###autoload
  7997. (defun turn-on-orgstruct ()
  7998. "Unconditionally turn on `orgstruct-mode'."
  7999. (orgstruct-mode 1))
  8000. (defvar org-fb-vars nil)
  8001. (make-variable-buffer-local 'org-fb-vars)
  8002. (defun orgstruct++-mode (&optional arg)
  8003. "Toggle `orgstruct-mode', the enhanced version of it.
  8004. In addition to setting orgstruct-mode, this also exports all
  8005. indentation and autofilling variables from org-mode into the
  8006. buffer. It will also recognize item context in multiline items."
  8007. (interactive "P")
  8008. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8009. (if (< arg 1)
  8010. (progn (orgstruct-mode -1)
  8011. (mapc (lambda(v)
  8012. (org-set-local (car v)
  8013. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8014. org-fb-vars))
  8015. (orgstruct-mode 1)
  8016. (setq org-fb-vars nil)
  8017. (unless org-local-vars
  8018. (setq org-local-vars (org-get-local-variables)))
  8019. (let (var val)
  8020. (mapc
  8021. (lambda (x)
  8022. (when (string-match
  8023. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8024. (symbol-name (car x)))
  8025. (setq var (car x) val (nth 1 x))
  8026. (push (list var `(quote ,(eval var))) org-fb-vars)
  8027. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8028. org-local-vars)
  8029. (org-set-local 'orgstruct-is-++ t))))
  8030. (defvar orgstruct-is-++ nil
  8031. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8032. (make-variable-buffer-local 'orgstruct-is-++)
  8033. ;;;###autoload
  8034. (defun turn-on-orgstruct++ ()
  8035. "Unconditionally turn on `orgstruct++-mode'."
  8036. (orgstruct++-mode 1))
  8037. (defun orgstruct-error ()
  8038. "Error when there is no default binding for a structure key."
  8039. (interactive)
  8040. (funcall (if (fboundp 'user-error)
  8041. 'user-error
  8042. 'error)
  8043. "This key has no function outside structure elements"))
  8044. (defun orgstruct-setup ()
  8045. "Setup orgstruct keymap."
  8046. (dolist (cell '((org-demote . t)
  8047. (org-metaleft . t)
  8048. (org-metaright . t)
  8049. (org-promote . t)
  8050. (org-shiftmetaleft . t)
  8051. (org-shiftmetaright . t)
  8052. org-backward-element
  8053. org-backward-heading-same-level
  8054. org-ctrl-c-ret
  8055. org-ctrl-c-minus
  8056. org-ctrl-c-star
  8057. org-cycle
  8058. org-forward-heading-same-level
  8059. org-insert-heading
  8060. org-insert-heading-respect-content
  8061. org-kill-note-or-show-branches
  8062. org-mark-subtree
  8063. org-meta-return
  8064. org-metadown
  8065. org-metaup
  8066. org-narrow-to-subtree
  8067. org-promote-subtree
  8068. org-reveal
  8069. org-shiftdown
  8070. org-shiftleft
  8071. org-shiftmetadown
  8072. org-shiftmetaup
  8073. org-shiftright
  8074. org-shifttab
  8075. org-shifttab
  8076. org-shiftup
  8077. org-show-subtree
  8078. org-sort
  8079. org-up-element
  8080. outline-demote
  8081. outline-next-visible-heading
  8082. outline-previous-visible-heading
  8083. outline-promote
  8084. outline-up-heading
  8085. show-children))
  8086. (let ((f (or (car-safe cell) cell))
  8087. (disable-when-heading-prefix (cdr-safe cell)))
  8088. (when (fboundp f)
  8089. (let ((new-bindings))
  8090. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8091. (where-is-internal f outline-mode-map)))
  8092. (push binding new-bindings)
  8093. ;; TODO use local-function-key-map
  8094. (dolist (rep '(("<tab>" . "TAB")
  8095. ("<return>" . "RET")
  8096. ("<escape>" . "ESC")
  8097. ("<delete>" . "DEL")))
  8098. (setq binding (read-kbd-macro
  8099. (let ((case-fold-search))
  8100. (replace-regexp-in-string
  8101. (regexp-quote (cdr rep))
  8102. (car rep)
  8103. (key-description binding)))))
  8104. (pushnew binding new-bindings :test 'equal)))
  8105. (dolist (binding new-bindings)
  8106. (let ((key (lookup-key orgstruct-mode-map binding)))
  8107. (when (or (not key) (numberp key))
  8108. (condition-case nil
  8109. (org-defkey orgstruct-mode-map
  8110. binding
  8111. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8112. (error nil)))))))))
  8113. (run-hooks 'orgstruct-setup-hook))
  8114. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8115. "Create a function for binding in the structure minor mode.
  8116. FUN is the command to call inside a table. KEY is the key that
  8117. should be checked in for a command to execute outside of tables.
  8118. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  8119. if `orgstruct-heading-prefix-regexp' is non-nil."
  8120. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8121. (let ((nname name)
  8122. (i 0))
  8123. (while (fboundp (intern nname))
  8124. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8125. (setq name (intern nname)))
  8126. (eval
  8127. (let ((bindings '((org-heading-regexp
  8128. (concat "^"
  8129. orgstruct-heading-prefix-regexp
  8130. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8131. (org-outline-regexp
  8132. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8133. (org-outline-regexp-bol
  8134. (concat "^" org-outline-regexp))
  8135. (outline-regexp org-outline-regexp)
  8136. (outline-heading-end-regexp "\n")
  8137. (outline-level 'org-outline-level)
  8138. (outline-heading-alist))))
  8139. `(defun ,name (arg)
  8140. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8141. "Outside of structure, run the binding of `"
  8142. (key-description key) "'."
  8143. (when disable-when-heading-prefix
  8144. (concat
  8145. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  8146. "back to the default binding due to limitations of Org's implementation of\n"
  8147. "`" (symbol-name fun) "'.")))
  8148. (interactive "p")
  8149. (let* ((disable
  8150. ,(when disable-when-heading-prefix
  8151. '(and orgstruct-heading-prefix-regexp
  8152. (not (string= orgstruct-heading-prefix-regexp "")))))
  8153. (fallback
  8154. (or disable
  8155. (not
  8156. (let* ,bindings
  8157. (org-context-p 'headline 'item
  8158. ,(when (memq fun
  8159. '(org-insert-heading
  8160. org-insert-heading-respect-content
  8161. org-meta-return))
  8162. '(when orgstruct-is-++
  8163. 'item-body))))))))
  8164. (if fallback
  8165. (let* ((orgstruct-mode)
  8166. (binding
  8167. (loop with key = ,key
  8168. for rep in
  8169. '(nil
  8170. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8171. ("<\\([^>]*\\)return>" . "\\1RET")
  8172. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8173. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8174. do
  8175. (when rep
  8176. (setq key (read-kbd-macro
  8177. (let ((case-fold-search))
  8178. (replace-regexp-in-string
  8179. (car rep)
  8180. (cdr rep)
  8181. (key-description key))))))
  8182. thereis (key-binding key))))
  8183. (if (keymapp binding)
  8184. (set-temporary-overlay-map binding)
  8185. (let ((func (or binding
  8186. (unless disable
  8187. 'orgstruct-error))))
  8188. (when func
  8189. (call-interactively func)))))
  8190. (org-run-like-in-org-mode
  8191. (lambda ()
  8192. (interactive)
  8193. (let* ,bindings
  8194. (call-interactively ',fun)))))))))
  8195. name))
  8196. (defun org-contextualize-keys (alist contexts)
  8197. "Return valid elements in ALIST depending on CONTEXTS.
  8198. `org-agenda-custom-commands' or `org-capture-templates' are the
  8199. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8200. or `org-capture-templates-contexts' are the associated contexts
  8201. definitions."
  8202. (let ((contexts
  8203. ;; normalize contexts
  8204. (mapcar
  8205. (lambda(c) (cond ((listp (cadr c))
  8206. (list (car c) (car c) (cadr c)))
  8207. ((string= "" (cadr c))
  8208. (list (car c) (car c) (caddr c)))
  8209. (t c))) contexts))
  8210. (a alist) c r s)
  8211. ;; loop over all commands or templates
  8212. (while (setq c (pop a))
  8213. (let (vrules repl)
  8214. (cond
  8215. ((not (assoc (car c) contexts))
  8216. (push c r))
  8217. ((and (assoc (car c) contexts)
  8218. (setq vrules (org-contextualize-validate-key
  8219. (car c) contexts)))
  8220. (mapc (lambda (vr)
  8221. (when (not (equal (car vr) (cadr vr)))
  8222. (setq repl vr))) vrules)
  8223. (if (not repl) (push c r)
  8224. (push (cadr repl) s)
  8225. (push
  8226. (cons (car c)
  8227. (cdr (or (assoc (cadr repl) alist)
  8228. (error "Undefined key `%s' as contextual replacement for `%s'"
  8229. (cadr repl) (car c)))))
  8230. r))))))
  8231. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8232. (delq
  8233. nil
  8234. (delete-dups
  8235. (mapcar (lambda (x)
  8236. (let ((tpl (car x)))
  8237. (when (not (delq
  8238. nil
  8239. (mapcar (lambda(y)
  8240. (equal y tpl)) s))) x)))
  8241. (reverse r))))))
  8242. (defun org-contextualize-validate-key (key contexts)
  8243. "Check CONTEXTS for agenda or capture KEY."
  8244. (let (r rr res)
  8245. (while (setq r (pop contexts))
  8246. (mapc
  8247. (lambda (rr)
  8248. (when
  8249. (and (equal key (car r))
  8250. (if (functionp rr) (funcall rr)
  8251. (or (and (eq (car rr) 'in-file)
  8252. (buffer-file-name)
  8253. (string-match (cdr rr) (buffer-file-name)))
  8254. (and (eq (car rr) 'in-mode)
  8255. (string-match (cdr rr) (symbol-name major-mode)))
  8256. (and (eq (car rr) 'in-buffer)
  8257. (string-match (cdr rr) (buffer-name)))
  8258. (when (and (eq (car rr) 'not-in-file)
  8259. (buffer-file-name))
  8260. (not (string-match (cdr rr) (buffer-file-name))))
  8261. (when (eq (car rr) 'not-in-mode)
  8262. (not (string-match (cdr rr) (symbol-name major-mode))))
  8263. (when (eq (car rr) 'not-in-buffer)
  8264. (not (string-match (cdr rr) (buffer-name)))))))
  8265. (push r res)))
  8266. (car (last r))))
  8267. (delete-dups (delq nil res))))
  8268. (defun org-context-p (&rest contexts)
  8269. "Check if local context is any of CONTEXTS.
  8270. Possible values in the list of contexts are `table', `headline', and `item'."
  8271. (let ((pos (point)))
  8272. (goto-char (point-at-bol))
  8273. (prog1 (or (and (memq 'table contexts)
  8274. (looking-at "[ \t]*|"))
  8275. (and (memq 'headline contexts)
  8276. (looking-at org-outline-regexp))
  8277. (and (memq 'item contexts)
  8278. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8279. (and (memq 'item-body contexts)
  8280. (org-in-item-p)))
  8281. (goto-char pos))))
  8282. (defun org-get-local-variables ()
  8283. "Return a list of all local variables in an Org mode buffer."
  8284. (let (varlist)
  8285. (with-current-buffer (get-buffer-create "*Org tmp*")
  8286. (erase-buffer)
  8287. (org-mode)
  8288. (setq varlist (buffer-local-variables)))
  8289. (kill-buffer "*Org tmp*")
  8290. (delq nil
  8291. (mapcar
  8292. (lambda (x)
  8293. (setq x
  8294. (if (symbolp x)
  8295. (list x)
  8296. (list (car x) (cdr x))))
  8297. (if (and (not (get (car x) 'org-state))
  8298. (string-match
  8299. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8300. (symbol-name (car x))))
  8301. x nil))
  8302. varlist))))
  8303. (defun org-clone-local-variables (from-buffer &optional regexp)
  8304. "Clone local variables from FROM-BUFFER.
  8305. Optional argument REGEXP selects variables to clone."
  8306. (mapc
  8307. (lambda (pair)
  8308. (and (symbolp (car pair))
  8309. (or (null regexp)
  8310. (string-match regexp (symbol-name (car pair))))
  8311. (set (make-local-variable (car pair))
  8312. (cdr pair))))
  8313. (buffer-local-variables from-buffer)))
  8314. ;;;###autoload
  8315. (defun org-run-like-in-org-mode (cmd)
  8316. "Run a command, pretending that the current buffer is in Org-mode.
  8317. This will temporarily bind local variables that are typically bound in
  8318. Org-mode to the values they have in Org-mode, and then interactively
  8319. call CMD."
  8320. (org-load-modules-maybe)
  8321. (unless org-local-vars
  8322. (setq org-local-vars (org-get-local-variables)))
  8323. (let (binds)
  8324. (dolist (var org-local-vars)
  8325. (when (or (not (boundp (car var)))
  8326. (eq (symbol-value (car var))
  8327. (default-value (car var))))
  8328. (push (list (car var) `(quote ,(cadr var))) binds)))
  8329. (eval `(let ,binds
  8330. (call-interactively (quote ,cmd))))))
  8331. ;;;; Archiving
  8332. (defun org-get-category (&optional pos force-refresh)
  8333. "Get the category applying to position POS."
  8334. (save-match-data
  8335. (if force-refresh (org-refresh-category-properties))
  8336. (let ((pos (or pos (point))))
  8337. (or (get-text-property pos 'org-category)
  8338. (progn (org-refresh-category-properties)
  8339. (get-text-property pos 'org-category))))))
  8340. (defun org-refresh-category-properties ()
  8341. "Refresh category text properties in the buffer."
  8342. (let ((case-fold-search t)
  8343. (inhibit-read-only t)
  8344. (def-cat (cond
  8345. ((null org-category)
  8346. (if buffer-file-name
  8347. (file-name-sans-extension
  8348. (file-name-nondirectory buffer-file-name))
  8349. "???"))
  8350. ((symbolp org-category) (symbol-name org-category))
  8351. (t org-category)))
  8352. beg end cat pos optionp)
  8353. (org-with-silent-modifications
  8354. (save-excursion
  8355. (save-restriction
  8356. (widen)
  8357. (goto-char (point-min))
  8358. (put-text-property (point) (point-max) 'org-category def-cat)
  8359. (while (re-search-forward
  8360. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8361. (setq pos (match-end 0)
  8362. optionp (equal (char-after (match-beginning 0)) ?#)
  8363. cat (org-trim (match-string 2)))
  8364. (if optionp
  8365. (setq beg (point-at-bol) end (point-max))
  8366. (org-back-to-heading t)
  8367. (setq beg (point) end (org-end-of-subtree t t)))
  8368. (put-text-property beg end 'org-category cat)
  8369. (put-text-property beg end 'org-category-position beg)
  8370. (goto-char pos)))))))
  8371. (defun org-refresh-properties (dprop tprop)
  8372. "Refresh buffer text properties.
  8373. DPROP is the drawer property and TPROP is the corresponding text
  8374. property to set."
  8375. (let ((case-fold-search t)
  8376. (inhibit-read-only t) p)
  8377. (org-with-silent-modifications
  8378. (save-excursion
  8379. (save-restriction
  8380. (widen)
  8381. (goto-char (point-min))
  8382. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8383. (setq p (org-match-string-no-properties 1))
  8384. (save-excursion
  8385. (org-back-to-heading t)
  8386. (put-text-property
  8387. (point-at-bol) (org-end-of-subtree t t) tprop p))))))))
  8388. ;;;; Link Stuff
  8389. ;;; Link abbreviations
  8390. (defun org-link-expand-abbrev (link)
  8391. "Apply replacements as defined in `org-link-abbrev-alist'."
  8392. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8393. (let* ((key (match-string 1 link))
  8394. (as (or (assoc key org-link-abbrev-alist-local)
  8395. (assoc key org-link-abbrev-alist)))
  8396. (tag (and (match-end 2) (match-string 3 link)))
  8397. rpl)
  8398. (if (not as)
  8399. link
  8400. (setq rpl (cdr as))
  8401. (cond
  8402. ((symbolp rpl) (funcall rpl tag))
  8403. ((string-match "%(\\([^)]+\\))" rpl)
  8404. (replace-match
  8405. (save-match-data
  8406. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8407. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8408. ((string-match "%h" rpl)
  8409. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8410. (t (concat rpl tag)))))
  8411. link))
  8412. ;;; Storing and inserting links
  8413. (defvar org-insert-link-history nil
  8414. "Minibuffer history for links inserted with `org-insert-link'.")
  8415. (defvar org-stored-links nil
  8416. "Contains the links stored with `org-store-link'.")
  8417. (defvar org-store-link-plist nil
  8418. "Plist with info about the most recently link created with `org-store-link'.")
  8419. (defvar org-link-protocols nil
  8420. "Link protocols added to Org-mode using `org-add-link-type'.")
  8421. (defvar org-store-link-functions nil
  8422. "List of functions that are called to create and store a link.
  8423. Each function will be called in turn until one returns a non-nil
  8424. value. Each function should check if it is responsible for creating
  8425. this link (for example by looking at the major mode).
  8426. If not, it must exit and return nil.
  8427. If yes, it should return a non-nil value after a calling
  8428. `org-store-link-props' with a list of properties and values.
  8429. Special properties are:
  8430. :type The link prefix, like \"http\". This must be given.
  8431. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8432. This is obligatory as well.
  8433. :description Optional default description for the second pair
  8434. of brackets in an Org-mode link. The user can still change
  8435. this when inserting this link into an Org-mode buffer.
  8436. In addition to these, any additional properties can be specified
  8437. and then used in capture templates.")
  8438. (defun org-add-link-type (type &optional follow export)
  8439. "Add TYPE to the list of `org-link-types'.
  8440. Re-compute all regular expressions depending on `org-link-types'
  8441. FOLLOW and EXPORT are two functions.
  8442. FOLLOW should take the link path as the single argument and do whatever
  8443. is necessary to follow the link, for example find a file or display
  8444. a mail message.
  8445. EXPORT should format the link path for export to one of the export formats.
  8446. It should be a function accepting three arguments:
  8447. path the path of the link, the text after the prefix (like \"http:\")
  8448. desc the description of the link, if any, or a description added by
  8449. org-export-normalize-links if there is none
  8450. format the export format, a symbol like `html' or `latex' or `ascii'..
  8451. The function may use the FORMAT information to return different values
  8452. depending on the format. The return value will be put literally into
  8453. the exported file. If the return value is nil, this means Org should
  8454. do what it normally does with links which do not have EXPORT defined.
  8455. Org-mode has a built-in default for exporting links. If you are happy with
  8456. this default, there is no need to define an export function for the link
  8457. type. For a simple example of an export function, see `org-bbdb.el'."
  8458. (add-to-list 'org-link-types type t)
  8459. (org-make-link-regexps)
  8460. (if (assoc type org-link-protocols)
  8461. (setcdr (assoc type org-link-protocols) (list follow export))
  8462. (push (list type follow export) org-link-protocols)))
  8463. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8464. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8465. ;;;###autoload
  8466. (defun org-store-link (arg)
  8467. "\\<org-mode-map>Store an org-link to the current location.
  8468. This link is added to `org-stored-links' and can later be inserted
  8469. into an org-buffer with \\[org-insert-link].
  8470. For some link types, a prefix arg is interpreted.
  8471. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8472. For file links, arg negates `org-context-in-file-links'.
  8473. A double prefix arg force skipping storing functions that are not
  8474. part of Org's core.
  8475. A triple prefix arg force storing a link for each line in the
  8476. active region."
  8477. (interactive "P")
  8478. (org-load-modules-maybe)
  8479. (if (and (equal arg '(64)) (org-region-active-p))
  8480. (save-excursion
  8481. (let ((end (region-end)))
  8482. (goto-char (region-beginning))
  8483. (set-mark (point))
  8484. (while (< (point-at-eol) end)
  8485. (move-end-of-line 1) (activate-mark)
  8486. (let (current-prefix-arg)
  8487. (call-interactively 'org-store-link))
  8488. (move-beginning-of-line 2)
  8489. (set-mark (point)))))
  8490. (org-with-limited-levels
  8491. (setq org-store-link-plist nil)
  8492. (let (link cpltxt desc description search
  8493. txt custom-id agenda-link sfuns sfunsn)
  8494. (cond
  8495. ;; Store a link using an external link type
  8496. ((and (not (equal arg '(16)))
  8497. (setq sfuns
  8498. (delq
  8499. nil (mapcar (lambda (f)
  8500. (let (fs) (if (funcall f) (push f fs))))
  8501. org-store-link-functions))
  8502. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8503. (or (and (cdr sfuns)
  8504. (funcall (intern
  8505. (completing-read
  8506. "Which function for creating the link? "
  8507. sfunsn t (car sfunsn)))))
  8508. (funcall (caar sfuns)))
  8509. (setq link (plist-get org-store-link-plist :link)
  8510. desc (or (plist-get org-store-link-plist
  8511. :description) link))))
  8512. ;; Store a link from a source code buffer
  8513. ((org-src-edit-buffer-p)
  8514. (let (label gc)
  8515. (while (or (not label)
  8516. (save-excursion
  8517. (save-restriction
  8518. (widen)
  8519. (goto-char (point-min))
  8520. (re-search-forward
  8521. (regexp-quote (format org-coderef-label-format label))
  8522. nil t))))
  8523. (when label (message "Label exists already") (sit-for 2))
  8524. (setq label (read-string "Code line label: " label)))
  8525. (end-of-line 1)
  8526. (setq link (format org-coderef-label-format label))
  8527. (setq gc (- 79 (length link)))
  8528. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8529. (insert link)
  8530. (setq link (concat "(" label ")") desc nil)))
  8531. ;; We are in the agenda, link to referenced location
  8532. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8533. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8534. (get-text-property (point) 'org-marker))))
  8535. (when m
  8536. (org-with-point-at m
  8537. (setq agenda-link
  8538. (if (org-called-interactively-p 'any)
  8539. (call-interactively 'org-store-link)
  8540. (org-store-link nil)))))))
  8541. ((eq major-mode 'calendar-mode)
  8542. (let ((cd (calendar-cursor-to-date)))
  8543. (setq link
  8544. (format-time-string
  8545. (car org-time-stamp-formats)
  8546. (apply 'encode-time
  8547. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8548. nil nil nil))))
  8549. (org-store-link-props :type "calendar" :date cd)))
  8550. ((eq major-mode 'help-mode)
  8551. (setq link (concat "help:" (save-excursion
  8552. (goto-char (point-min))
  8553. (looking-at "^[^ ]+")
  8554. (match-string 0))))
  8555. (org-store-link-props :type "help"))
  8556. ((eq major-mode 'w3-mode)
  8557. (setq cpltxt (if (and (buffer-name)
  8558. (not (string-match "Untitled" (buffer-name))))
  8559. (buffer-name)
  8560. (url-view-url t))
  8561. link (url-view-url t))
  8562. (org-store-link-props :type "w3" :url (url-view-url t)))
  8563. ((eq major-mode 'image-mode)
  8564. (setq cpltxt (concat "file:"
  8565. (abbreviate-file-name buffer-file-name))
  8566. link cpltxt)
  8567. (org-store-link-props :type "image" :file buffer-file-name))
  8568. ;; In dired, store a link to the file of the current line
  8569. ((eq major-mode 'dired-mode)
  8570. (let ((file (dired-get-filename nil t)))
  8571. (setq file (if file
  8572. (abbreviate-file-name
  8573. (expand-file-name (dired-get-filename nil t)))
  8574. ;; otherwise, no file so use current directory.
  8575. default-directory))
  8576. (setq cpltxt (concat "file:" file)
  8577. link cpltxt)))
  8578. ((setq search (run-hook-with-args-until-success
  8579. 'org-create-file-search-functions))
  8580. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8581. "::" search))
  8582. (setq cpltxt (or description link)))
  8583. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8584. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8585. (cond
  8586. ;; Store a link using the target at point
  8587. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8588. (setq cpltxt
  8589. (concat "file:"
  8590. (abbreviate-file-name
  8591. (buffer-file-name (buffer-base-buffer)))
  8592. "::" (match-string 1))
  8593. link cpltxt))
  8594. ((and (featurep 'org-id)
  8595. (or (eq org-id-link-to-org-use-id t)
  8596. (and (org-called-interactively-p 'any)
  8597. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8598. (and (eq org-id-link-to-org-use-id
  8599. 'create-if-interactive-and-no-custom-id)
  8600. (not custom-id))))
  8601. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8602. ;; Store a link using the ID at point
  8603. (setq link (condition-case nil
  8604. (prog1 (org-id-store-link)
  8605. (setq desc (plist-get org-store-link-plist
  8606. :description)))
  8607. (error
  8608. ;; Probably before first headline, link only to file
  8609. (concat "file:"
  8610. (abbreviate-file-name
  8611. (buffer-file-name (buffer-base-buffer))))))))
  8612. (t
  8613. ;; Just link to current headline
  8614. (setq cpltxt (concat "file:"
  8615. (abbreviate-file-name
  8616. (buffer-file-name (buffer-base-buffer)))))
  8617. ;; Add a context search string
  8618. (when (org-xor org-context-in-file-links arg)
  8619. (let* ((ee (org-element-at-point))
  8620. (et (org-element-type ee))
  8621. (ev (plist-get (cadr ee) :value))
  8622. (ek (plist-get (cadr ee) :key))
  8623. (eok (and (stringp ek) (string-match "name" ek))))
  8624. (setq txt (cond
  8625. ((org-at-heading-p) nil)
  8626. ((and (eq et 'keyword) eok) ev)
  8627. ((org-region-active-p)
  8628. (buffer-substring (region-beginning) (region-end)))))
  8629. (when (or (null txt) (string-match "\\S-" txt))
  8630. (setq cpltxt
  8631. (concat cpltxt "::"
  8632. (condition-case nil
  8633. (org-make-org-heading-search-string txt)
  8634. (error "")))
  8635. desc (or (and (eq et 'keyword) eok ev)
  8636. (nth 4 (ignore-errors (org-heading-components)))
  8637. "NONE")))))
  8638. (if (string-match "::\\'" cpltxt)
  8639. (setq cpltxt (substring cpltxt 0 -2)))
  8640. (setq link cpltxt))))
  8641. ((buffer-file-name (buffer-base-buffer))
  8642. ;; Just link to this file here.
  8643. (setq cpltxt (concat "file:"
  8644. (abbreviate-file-name
  8645. (buffer-file-name (buffer-base-buffer)))))
  8646. ;; Add a context string.
  8647. (when (org-xor org-context-in-file-links arg)
  8648. (setq txt (if (org-region-active-p)
  8649. (buffer-substring (region-beginning) (region-end))
  8650. (buffer-substring (point-at-bol) (point-at-eol))))
  8651. ;; Only use search option if there is some text.
  8652. (when (string-match "\\S-" txt)
  8653. (setq cpltxt
  8654. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8655. desc "NONE")))
  8656. (setq link cpltxt))
  8657. ((org-called-interactively-p 'interactive)
  8658. (user-error "No method for storing a link from this buffer"))
  8659. (t (setq link nil)))
  8660. ;; We're done setting link and desc, clean up
  8661. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8662. (setq link (or link cpltxt)
  8663. desc (or desc cpltxt))
  8664. (cond ((equal desc "NONE") (setq desc nil))
  8665. ((string-match org-bracket-link-analytic-regexp desc)
  8666. (let ((d0 (match-string 3 desc))
  8667. (p0 (match-string 5 desc)))
  8668. (setq desc
  8669. (replace-regexp-in-string
  8670. org-bracket-link-regexp
  8671. (concat (or p0 d0)
  8672. (if (equal (length (match-string 0 desc))
  8673. (length desc)) "*" "")) desc)))))
  8674. ;; Return the link
  8675. (if (not (and (or (org-called-interactively-p 'any)
  8676. executing-kbd-macro) link))
  8677. (or agenda-link (and link (org-make-link-string link desc)))
  8678. (push (list link desc) org-stored-links)
  8679. (message "Stored: %s" (or desc link))
  8680. (when custom-id
  8681. (setq link (concat "file:" (abbreviate-file-name
  8682. (buffer-file-name)) "::#" custom-id))
  8683. (push (list link desc) org-stored-links)))))))
  8684. (defun org-store-link-props (&rest plist)
  8685. "Store link properties, extract names and addresses."
  8686. (let (x adr)
  8687. (when (setq x (plist-get plist :from))
  8688. (setq adr (mail-extract-address-components x))
  8689. (setq plist (plist-put plist :fromname (car adr)))
  8690. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8691. (when (setq x (plist-get plist :to))
  8692. (setq adr (mail-extract-address-components x))
  8693. (setq plist (plist-put plist :toname (car adr)))
  8694. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8695. (let ((from (plist-get plist :from))
  8696. (to (plist-get plist :to)))
  8697. (when (and from to org-from-is-user-regexp)
  8698. (setq plist
  8699. (plist-put plist :fromto
  8700. (if (string-match org-from-is-user-regexp from)
  8701. (concat "to %t")
  8702. (concat "from %f"))))))
  8703. (setq org-store-link-plist plist))
  8704. (defun org-add-link-props (&rest plist)
  8705. "Add these properties to the link property list."
  8706. (let (key value)
  8707. (while plist
  8708. (setq key (pop plist) value (pop plist))
  8709. (setq org-store-link-plist
  8710. (plist-put org-store-link-plist key value)))))
  8711. (defun org-email-link-description (&optional fmt)
  8712. "Return the description part of an email link.
  8713. This takes information from `org-store-link-plist' and formats it
  8714. according to FMT (default from `org-email-link-description-format')."
  8715. (setq fmt (or fmt org-email-link-description-format))
  8716. (let* ((p org-store-link-plist)
  8717. (to (plist-get p :toaddress))
  8718. (from (plist-get p :fromaddress))
  8719. (table
  8720. (list
  8721. (cons "%c" (plist-get p :fromto))
  8722. (cons "%F" (plist-get p :from))
  8723. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8724. (cons "%T" (plist-get p :to))
  8725. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8726. (cons "%s" (plist-get p :subject))
  8727. (cons "%d" (plist-get p :date))
  8728. (cons "%m" (plist-get p :message-id)))))
  8729. (when (string-match "%c" fmt)
  8730. ;; Check if the user wrote this message
  8731. (if (and org-from-is-user-regexp from to
  8732. (save-match-data (string-match org-from-is-user-regexp from)))
  8733. (setq fmt (replace-match "to %t" t t fmt))
  8734. (setq fmt (replace-match "from %f" t t fmt))))
  8735. (org-replace-escapes fmt table)))
  8736. (defun org-make-org-heading-search-string (&optional string)
  8737. "Make search string for the current headline or STRING."
  8738. (let ((s (or string
  8739. (and (derived-mode-p 'org-mode)
  8740. (save-excursion
  8741. (org-back-to-heading t)
  8742. (org-element-property :raw-value (org-element-at-point))))))
  8743. (lines org-context-in-file-links))
  8744. (or string (setq s (concat "*" s))) ; Add * for headlines
  8745. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8746. (when (and string (integerp lines) (> lines 0))
  8747. (let ((slines (org-split-string s "\n")))
  8748. (when (< lines (length slines))
  8749. (setq s (mapconcat
  8750. 'identity
  8751. (reverse (nthcdr (- (length slines) lines)
  8752. (reverse slines))) "\n")))))
  8753. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8754. (defun org-make-link-string (link &optional description)
  8755. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8756. (unless (string-match "\\S-" link)
  8757. (error "Empty link"))
  8758. (when (and description
  8759. (stringp description)
  8760. (not (string-match "\\S-" description)))
  8761. (setq description nil))
  8762. (when (stringp description)
  8763. ;; Remove brackets from the description, they are fatal.
  8764. (while (string-match "\\[" description)
  8765. (setq description (replace-match "{" t t description)))
  8766. (while (string-match "\\]" description)
  8767. (setq description (replace-match "}" t t description))))
  8768. (when (equal link description)
  8769. ;; No description needed, it is identical
  8770. (setq description nil))
  8771. (when (and (not description)
  8772. (not (string-match (org-image-file-name-regexp) link))
  8773. (not (equal link (org-link-escape link))))
  8774. (setq description (org-extract-attributes link)))
  8775. (setq link
  8776. (cond ((string-match (org-image-file-name-regexp) link) link)
  8777. ((string-match org-link-types-re link)
  8778. (concat (match-string 1 link)
  8779. (org-link-escape (substring link (match-end 1)))))
  8780. (t (org-link-escape link))))
  8781. (concat "[[" link "]"
  8782. (if description (concat "[" description "]") "")
  8783. "]"))
  8784. (defconst org-link-escape-chars
  8785. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8786. "List of characters that should be escaped in link.
  8787. This is the list that is used for internal purposes.")
  8788. (defconst org-link-escape-chars-browser
  8789. '(?\ ?\")
  8790. "List of escapes for characters that are problematic in links.
  8791. This is the list that is used before handing over to the browser.")
  8792. (defun org-link-escape (text &optional table merge)
  8793. "Return percent escaped representation of TEXT.
  8794. TEXT is a string with the text to escape.
  8795. Optional argument TABLE is a list with characters that should be
  8796. escaped. When nil, `org-link-escape-chars' is used.
  8797. If optional argument MERGE is set, merge TABLE into
  8798. `org-link-escape-chars'."
  8799. (cond
  8800. ((and table merge)
  8801. (mapc (lambda (defchr)
  8802. (unless (member defchr table)
  8803. (setq table (cons defchr table)))) org-link-escape-chars))
  8804. ((null table)
  8805. (setq table org-link-escape-chars)))
  8806. (mapconcat
  8807. (lambda (char)
  8808. (if (or (member char table)
  8809. (and (or (< char 32) (= char 37) (> char 126))
  8810. org-url-hexify-p))
  8811. (mapconcat (lambda (sequence-element)
  8812. (format "%%%.2X" sequence-element))
  8813. (or (encode-coding-char char 'utf-8)
  8814. (error "Unable to percent escape character: %s"
  8815. (char-to-string char))) "")
  8816. (char-to-string char))) text ""))
  8817. (defun org-link-unescape (str)
  8818. "Unhex hexified Unicode strings as returned from the JavaScript function
  8819. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8820. (unless (and (null str) (string= "" str))
  8821. (let ((pos 0) (case-fold-search t) unhexed)
  8822. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8823. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8824. (setq str (replace-match unhexed t t str))
  8825. (setq pos (+ pos (length unhexed))))))
  8826. str)
  8827. (defun org-link-unescape-compound (hex)
  8828. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8829. Note: this function also decodes single byte encodings like
  8830. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8831. (save-match-data
  8832. (let* ((bytes (cdr (split-string hex "%")))
  8833. (ret "")
  8834. (eat 0)
  8835. (sum 0))
  8836. (while bytes
  8837. (let* ((val (string-to-number (pop bytes) 16))
  8838. (shift-xor
  8839. (if (= 0 eat)
  8840. (cond
  8841. ((>= val 252) (cons 6 252))
  8842. ((>= val 248) (cons 5 248))
  8843. ((>= val 240) (cons 4 240))
  8844. ((>= val 224) (cons 3 224))
  8845. ((>= val 192) (cons 2 192))
  8846. (t (cons 0 0)))
  8847. (cons 6 128))))
  8848. (if (>= val 192) (setq eat (car shift-xor)))
  8849. (setq val (logxor val (cdr shift-xor)))
  8850. (setq sum (+ (lsh sum (car shift-xor)) val))
  8851. (if (> eat 0) (setq eat (- eat 1)))
  8852. (cond
  8853. ((= 0 eat) ;multi byte
  8854. (setq ret (concat ret (org-char-to-string sum)))
  8855. (setq sum 0))
  8856. ((not bytes) ; single byte(s)
  8857. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8858. )) ;; end (while bytes
  8859. ret )))
  8860. (defun org-link-unescape-single-byte-sequence (hex)
  8861. "Unhexify hex-encoded single byte character sequences."
  8862. (mapconcat (lambda (byte)
  8863. (char-to-string (string-to-number byte 16)))
  8864. (cdr (split-string hex "%")) ""))
  8865. (defun org-xor (a b)
  8866. "Exclusive or."
  8867. (if a (not b) b))
  8868. (defun org-fixup-message-id-for-http (s)
  8869. "Replace special characters in a message id, so it can be used in an http query."
  8870. (when (string-match "%" s)
  8871. (setq s (mapconcat (lambda (c)
  8872. (if (eq c ?%)
  8873. "%25"
  8874. (char-to-string c)))
  8875. s "")))
  8876. (while (string-match "<" s)
  8877. (setq s (replace-match "%3C" t t s)))
  8878. (while (string-match ">" s)
  8879. (setq s (replace-match "%3E" t t s)))
  8880. (while (string-match "@" s)
  8881. (setq s (replace-match "%40" t t s)))
  8882. s)
  8883. (defun org-link-prettify (link)
  8884. "Return a human-readable representation of LINK.
  8885. The car of LINK must be a raw link the cdr of LINK must be either
  8886. a link description or nil."
  8887. (let ((desc (or (cadr link) "<no description>")))
  8888. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8889. "<" (car link) ">")))
  8890. ;;;###autoload
  8891. (defun org-insert-link-global ()
  8892. "Insert a link like Org-mode does.
  8893. This command can be called in any mode to insert a link in Org-mode syntax."
  8894. (interactive)
  8895. (org-load-modules-maybe)
  8896. (org-run-like-in-org-mode 'org-insert-link))
  8897. (defun org-insert-all-links (&optional keep)
  8898. "Insert all links in `org-stored-links'."
  8899. (interactive "P")
  8900. (let ((links (copy-sequence org-stored-links)) l)
  8901. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8902. (insert "- ")
  8903. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8904. (insert "\n"))))
  8905. (defun org-link-fontify-links-to-this-file ()
  8906. "Fontify links to the current file in `org-stored-links'."
  8907. (let ((f (buffer-file-name)) a b)
  8908. (setq a (mapcar (lambda(l)
  8909. (let ((ll (car l)))
  8910. (when (and (string-match "^file:\\(.+\\)::" ll)
  8911. (equal f (expand-file-name (match-string 1 ll))))
  8912. ll)))
  8913. org-stored-links))
  8914. (when (featurep 'org-id)
  8915. (setq b (mapcar (lambda(l)
  8916. (let ((ll (car l)))
  8917. (when (and (string-match "^id:\\(.+\\)$" ll)
  8918. (equal f (expand-file-name
  8919. (or (org-id-find-id-file
  8920. (match-string 1 ll)) ""))))
  8921. ll)))
  8922. org-stored-links)))
  8923. (mapcar (lambda(l)
  8924. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8925. (delq nil (append a b)))))
  8926. (defvar org-link-links-in-this-file nil)
  8927. (defun org-insert-link (&optional complete-file link-location default-description)
  8928. "Insert a link. At the prompt, enter the link.
  8929. Completion can be used to insert any of the link protocol prefixes like
  8930. http or ftp in use.
  8931. The history can be used to select a link previously stored with
  8932. `org-store-link'. When the empty string is entered (i.e. if you just
  8933. press RET at the prompt), the link defaults to the most recently
  8934. stored link. As SPC triggers completion in the minibuffer, you need to
  8935. use M-SPC or C-q SPC to force the insertion of a space character.
  8936. You will also be prompted for a description, and if one is given, it will
  8937. be displayed in the buffer instead of the link.
  8938. If there is already a link at point, this command will allow you to edit link
  8939. and description parts.
  8940. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8941. be selected using completion. The path to the file will be relative to the
  8942. current directory if the file is in the current directory or a subdirectory.
  8943. Otherwise, the link will be the absolute path as completed in the minibuffer
  8944. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8945. option `org-link-file-path-type'.
  8946. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8947. the current directory or below.
  8948. With three \\[universal-argument] prefixes, negate the meaning of
  8949. `org-keep-stored-link-after-insertion'.
  8950. If `org-make-link-description-function' is non-nil, this function will be
  8951. called with the link target, and the result will be the default
  8952. link description.
  8953. If the LINK-LOCATION parameter is non-nil, this value will be
  8954. used as the link location instead of reading one interactively.
  8955. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8956. be used as the default description."
  8957. (interactive "P")
  8958. (let* ((wcf (current-window-configuration))
  8959. (origbuf (current-buffer))
  8960. (region (if (org-region-active-p)
  8961. (buffer-substring (region-beginning) (region-end))))
  8962. (remove (and region (list (region-beginning) (region-end))))
  8963. (desc region)
  8964. tmphist ; byte-compile incorrectly complains about this
  8965. (link link-location)
  8966. (abbrevs org-link-abbrev-alist-local)
  8967. entry file all-prefixes auto-desc)
  8968. (cond
  8969. (link-location) ; specified by arg, just use it.
  8970. ((org-in-regexp org-bracket-link-regexp 1)
  8971. ;; We do have a link at point, and we are going to edit it.
  8972. (setq remove (list (match-beginning 0) (match-end 0)))
  8973. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8974. (setq link (read-string "Link: "
  8975. (org-link-unescape
  8976. (org-match-string-no-properties 1)))))
  8977. ((or (org-in-regexp org-angle-link-re)
  8978. (org-in-regexp org-plain-link-re))
  8979. ;; Convert to bracket link
  8980. (setq remove (list (match-beginning 0) (match-end 0))
  8981. link (read-string "Link: "
  8982. (org-remove-angle-brackets (match-string 0)))))
  8983. ((member complete-file '((4) (16)))
  8984. ;; Completing read for file names.
  8985. (setq link (org-file-complete-link complete-file)))
  8986. (t
  8987. ;; Read link, with completion for stored links.
  8988. (org-link-fontify-links-to-this-file)
  8989. (org-switch-to-buffer-other-window "*Org Links*")
  8990. (with-current-buffer "*Org Links*"
  8991. (erase-buffer)
  8992. (insert "Insert a link.
  8993. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8994. (when org-stored-links
  8995. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8996. (insert (mapconcat 'org-link-prettify
  8997. (reverse org-stored-links) "\n")))
  8998. (goto-char (point-min)))
  8999. (let ((cw (selected-window)))
  9000. (select-window (get-buffer-window "*Org Links*" 'visible))
  9001. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9002. (unless (pos-visible-in-window-p (point-max))
  9003. (org-fit-window-to-buffer))
  9004. (and (window-live-p cw) (select-window cw)))
  9005. ;; Fake a link history, containing the stored links.
  9006. (setq tmphist (append (mapcar 'car org-stored-links)
  9007. org-insert-link-history))
  9008. (setq all-prefixes (append (mapcar 'car abbrevs)
  9009. (mapcar 'car org-link-abbrev-alist)
  9010. org-link-types))
  9011. (unwind-protect
  9012. (progn
  9013. (setq link
  9014. (org-completing-read
  9015. "Link: "
  9016. (append
  9017. (mapcar (lambda (x) (concat x ":"))
  9018. all-prefixes)
  9019. (mapcar 'car org-stored-links))
  9020. nil nil nil
  9021. 'tmphist
  9022. (caar org-stored-links)))
  9023. (if (not (string-match "\\S-" link))
  9024. (user-error "No link selected"))
  9025. (mapc (lambda(l)
  9026. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9027. org-stored-links)
  9028. (if (or (member link all-prefixes)
  9029. (and (equal ":" (substring link -1))
  9030. (member (substring link 0 -1) all-prefixes)
  9031. (setq link (substring link 0 -1))))
  9032. (setq link (with-current-buffer origbuf
  9033. (org-link-try-special-completion link)))))
  9034. (set-window-configuration wcf)
  9035. (kill-buffer "*Org Links*"))
  9036. (setq entry (assoc link org-stored-links))
  9037. (or entry (push link org-insert-link-history))
  9038. (setq desc (or desc (nth 1 entry)))))
  9039. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9040. (not org-keep-stored-link-after-insertion))
  9041. (setq org-stored-links (delq (assoc link org-stored-links)
  9042. org-stored-links)))
  9043. (if (string-match org-plain-link-re link)
  9044. ;; URL-like link, normalize the use of angular brackets.
  9045. (setq link (org-remove-angle-brackets link)))
  9046. ;; Check if we are linking to the current file with a search
  9047. ;; option If yes, simplify the link by using only the search
  9048. ;; option.
  9049. (when (and buffer-file-name
  9050. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9051. (let* ((path (match-string 1 link))
  9052. (case-fold-search nil)
  9053. (search (match-string 2 link)))
  9054. (save-match-data
  9055. (if (equal (file-truename buffer-file-name) (file-truename path))
  9056. ;; We are linking to this same file, with a search option
  9057. (setq link search)))))
  9058. ;; Check if we can/should use a relative path. If yes, simplify the link
  9059. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9060. (let* ((type (match-string 1 link))
  9061. (path (match-string 2 link))
  9062. (origpath path)
  9063. (case-fold-search nil))
  9064. (cond
  9065. ((or (eq org-link-file-path-type 'absolute)
  9066. (equal complete-file '(16)))
  9067. (setq path (abbreviate-file-name (expand-file-name path))))
  9068. ((eq org-link-file-path-type 'noabbrev)
  9069. (setq path (expand-file-name path)))
  9070. ((eq org-link-file-path-type 'relative)
  9071. (setq path (file-relative-name path)))
  9072. (t
  9073. (save-match-data
  9074. (if (string-match (concat "^" (regexp-quote
  9075. (expand-file-name
  9076. (file-name-as-directory
  9077. default-directory))))
  9078. (expand-file-name path))
  9079. ;; We are linking a file with relative path name.
  9080. (setq path (substring (expand-file-name path)
  9081. (match-end 0)))
  9082. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9083. (setq link (concat type path))
  9084. (if (equal desc origpath)
  9085. (setq desc path))))
  9086. (if org-make-link-description-function
  9087. (setq desc
  9088. (or (condition-case nil
  9089. (funcall org-make-link-description-function link desc)
  9090. (error (progn (message "Can't get link description from `%s'"
  9091. (symbol-name org-make-link-description-function))
  9092. (sit-for 2) nil)))
  9093. (read-string "Description: " default-description)))
  9094. (if default-description (setq desc default-description)
  9095. (setq desc (or (and auto-desc desc)
  9096. (read-string "Description: " desc)))))
  9097. (unless (string-match "\\S-" desc) (setq desc nil))
  9098. (if remove (apply 'delete-region remove))
  9099. (insert (org-make-link-string link desc))))
  9100. (defun org-link-try-special-completion (type)
  9101. "If there is completion support for link type TYPE, offer it."
  9102. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9103. (if (functionp fun)
  9104. (funcall fun)
  9105. (read-string "Link (no completion support): " (concat type ":")))))
  9106. (defun org-file-complete-link (&optional arg)
  9107. "Create a file link using completion."
  9108. (let (file link)
  9109. (setq file (org-iread-file-name "File: "))
  9110. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9111. (pwd1 (file-name-as-directory (abbreviate-file-name
  9112. (expand-file-name ".")))))
  9113. (cond
  9114. ((equal arg '(16))
  9115. (setq link (concat
  9116. "file:"
  9117. (abbreviate-file-name (expand-file-name file)))))
  9118. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9119. (setq link (concat "file:" (match-string 1 file))))
  9120. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9121. (expand-file-name file))
  9122. (setq link (concat
  9123. "file:" (match-string 1 (expand-file-name file)))))
  9124. (t (setq link (concat "file:" file)))))
  9125. link))
  9126. (defun org-iread-file-name (&rest args)
  9127. "Read-file-name using `ido-mode' speedup if available.
  9128. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9129. See `read-file-name' for a description of parameters."
  9130. (org-without-partial-completion
  9131. (if (and org-completion-use-ido
  9132. (fboundp 'ido-read-file-name)
  9133. (boundp 'ido-mode) ido-mode
  9134. (listp (second args)))
  9135. (let ((ido-enter-matching-directory nil))
  9136. (apply 'ido-read-file-name args))
  9137. (apply 'read-file-name args))))
  9138. (defun org-completing-read (&rest args)
  9139. "Completing-read with SPACE being a normal character."
  9140. (let ((enable-recursive-minibuffers t)
  9141. (minibuffer-local-completion-map
  9142. (copy-keymap minibuffer-local-completion-map)))
  9143. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9144. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9145. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9146. (apply 'org-icompleting-read args)))
  9147. (defun org-completing-read-no-i (&rest args)
  9148. (let (org-completion-use-ido org-completion-use-iswitchb)
  9149. (apply 'org-completing-read args)))
  9150. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9151. "Use iswitch as a completing-read replacement to choose from choices.
  9152. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9153. from."
  9154. (let* ((iswitchb-use-virtual-buffers nil)
  9155. (iswitchb-make-buflist-hook
  9156. (lambda ()
  9157. (setq iswitchb-temp-buflist choices))))
  9158. (iswitchb-read-buffer prompt)))
  9159. (defun org-icompleting-read (&rest args)
  9160. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9161. (org-without-partial-completion
  9162. (if (and org-completion-use-ido
  9163. (fboundp 'ido-completing-read)
  9164. (boundp 'ido-mode) ido-mode
  9165. (listp (second args)))
  9166. (let ((ido-enter-matching-directory nil))
  9167. (apply 'ido-completing-read (concat (car args))
  9168. (if (consp (car (nth 1 args)))
  9169. (mapcar 'car (nth 1 args))
  9170. (nth 1 args))
  9171. (cddr args)))
  9172. (if (and org-completion-use-iswitchb
  9173. (boundp 'iswitchb-mode) iswitchb-mode
  9174. (listp (second args)))
  9175. (apply 'org-iswitchb-completing-read (concat (car args))
  9176. (if (consp (car (nth 1 args)))
  9177. (mapcar 'car (nth 1 args))
  9178. (nth 1 args))
  9179. (cddr args))
  9180. (apply 'completing-read args)))))
  9181. (defun org-extract-attributes (s)
  9182. "Extract the attributes cookie from a string and set as text property."
  9183. (let (a attr (start 0) key value)
  9184. (save-match-data
  9185. (when (string-match "{{\\([^}]+\\)}}$" s)
  9186. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9187. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9188. (setq key (match-string 1 a) value (match-string 2 a)
  9189. start (match-end 0)
  9190. attr (plist-put attr (intern key) value))))
  9191. (org-add-props s nil 'org-attr attr))
  9192. s))
  9193. ;;; Opening/following a link
  9194. (defvar org-link-search-failed nil)
  9195. (defvar org-open-link-functions nil
  9196. "Hook for functions finding a plain text link.
  9197. These functions must take a single argument, the link content.
  9198. They will be called for links that look like [[link text][description]]
  9199. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9200. like a filename (e.g. \"./blue.png\").
  9201. These functions will be called *before* Org attempts to resolve the
  9202. link by doing text searches in the current buffer - so if you want a
  9203. link \"[[target]]\" to still find \"<<target>>\", your function should
  9204. handle this as a special case.
  9205. When the function does handle the link, it must return a non-nil value.
  9206. If it decides that it is not responsible for this link, it must return
  9207. nil to indicate that that Org-mode can continue with other options
  9208. like exact and fuzzy text search.")
  9209. (defun org-next-link (&optional search-backward)
  9210. "Move forward to the next link.
  9211. If the link is in hidden text, expose it."
  9212. (interactive "P")
  9213. (when (and org-link-search-failed (eq this-command last-command))
  9214. (goto-char (point-min))
  9215. (message "Link search wrapped back to beginning of buffer"))
  9216. (setq org-link-search-failed nil)
  9217. (let* ((pos (point))
  9218. (ct (org-context))
  9219. (a (assoc :link ct))
  9220. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9221. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9222. ((looking-at org-any-link-re)
  9223. ;; Don't stay stuck at link without an org-link face
  9224. (forward-char (if search-backward -1 1))))
  9225. (if (funcall srch-fun org-any-link-re nil t)
  9226. (progn
  9227. (goto-char (match-beginning 0))
  9228. (if (outline-invisible-p) (org-show-context)))
  9229. (goto-char pos)
  9230. (setq org-link-search-failed t)
  9231. (message "No further link found"))))
  9232. (defun org-previous-link ()
  9233. "Move backward to the previous link.
  9234. If the link is in hidden text, expose it."
  9235. (interactive)
  9236. (funcall 'org-next-link t))
  9237. (defun org-translate-link (s)
  9238. "Translate a link string if a translation function has been defined."
  9239. (if (and org-link-translation-function
  9240. (fboundp org-link-translation-function)
  9241. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9242. (progn
  9243. (setq s (funcall org-link-translation-function
  9244. (match-string 1 s) (match-string 2 s)))
  9245. (concat (car s) ":" (cdr s)))
  9246. s))
  9247. (defun org-translate-link-from-planner (type path)
  9248. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9249. This is still an experimental function, your mileage may vary."
  9250. (cond
  9251. ((member type '("http" "https" "news" "ftp"))
  9252. ;; standard Internet links are the same.
  9253. nil)
  9254. ((and (equal type "irc") (string-match "^//" path))
  9255. ;; Planner has two / at the beginning of an irc link, we have 1.
  9256. ;; We should have zero, actually....
  9257. (setq path (substring path 1)))
  9258. ((and (equal type "lisp") (string-match "^/" path))
  9259. ;; Planner has a slash, we do not.
  9260. (setq type "elisp" path (substring path 1)))
  9261. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9262. ;; A typical message link. Planner has the id after the final slash,
  9263. ;; we separate it with a hash mark
  9264. (setq path (concat (match-string 1 path) "#"
  9265. (org-remove-angle-brackets (match-string 2 path))))))
  9266. (cons type path))
  9267. (defun org-find-file-at-mouse (ev)
  9268. "Open file link or URL at mouse."
  9269. (interactive "e")
  9270. (mouse-set-point ev)
  9271. (org-open-at-point 'in-emacs))
  9272. (defun org-open-at-mouse (ev)
  9273. "Open file link or URL at mouse.
  9274. See the docstring of `org-open-file' for details."
  9275. (interactive "e")
  9276. (mouse-set-point ev)
  9277. (if (eq major-mode 'org-agenda-mode)
  9278. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9279. (org-open-at-point))
  9280. (defvar org-window-config-before-follow-link nil
  9281. "The window configuration before following a link.
  9282. This is saved in case the need arises to restore it.")
  9283. (defvar org-open-link-marker (make-marker)
  9284. "Marker pointing to the location where `org-open-at-point; was called.")
  9285. ;;;###autoload
  9286. (defun org-open-at-point-global ()
  9287. "Follow a link like Org-mode does.
  9288. This command can be called in any mode to follow a link that has
  9289. Org-mode syntax."
  9290. (interactive)
  9291. (org-run-like-in-org-mode 'org-open-at-point))
  9292. ;;;###autoload
  9293. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9294. "Open a link in the string S, as if it was in Org-mode."
  9295. (interactive "sLink: \nP")
  9296. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9297. (with-temp-buffer
  9298. (let ((org-inhibit-startup (not reference-buffer)))
  9299. (org-mode)
  9300. (insert s)
  9301. (goto-char (point-min))
  9302. (when reference-buffer
  9303. (setq org-link-abbrev-alist-local
  9304. (with-current-buffer reference-buffer
  9305. org-link-abbrev-alist-local)))
  9306. (org-open-at-point arg reference-buffer)))))
  9307. (defvar org-open-at-point-functions nil
  9308. "Hook that is run when following a link at point.
  9309. Functions in this hook must return t if they identify and follow
  9310. a link at point. If they don't find anything interesting at point,
  9311. they must return nil.")
  9312. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9313. (defun org-open-at-point (&optional arg reference-buffer)
  9314. "Open link at or after point.
  9315. If there is no link at point, this function will search forward up to
  9316. the end of the current line.
  9317. Normally, files will be opened by an appropriate application. If the
  9318. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9319. With a double prefix argument, try to open outside of Emacs, in the
  9320. application the system uses for this file type."
  9321. (interactive "P")
  9322. ;; if in a code block, then open the block's results
  9323. (unless (call-interactively #'org-babel-open-src-block-result)
  9324. (org-load-modules-maybe)
  9325. (move-marker org-open-link-marker (point))
  9326. (setq org-window-config-before-follow-link (current-window-configuration))
  9327. (org-remove-occur-highlights nil nil t)
  9328. (cond
  9329. ((and (org-at-heading-p)
  9330. (not (org-at-timestamp-p t))
  9331. (not (org-in-regexp
  9332. (concat org-plain-link-re "\\|"
  9333. org-bracket-link-regexp "\\|"
  9334. org-angle-link-re "\\|"
  9335. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9336. (not (get-text-property (point) 'org-linked-text)))
  9337. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9338. (lk0 (car lkall))
  9339. (lk (if (stringp lk0) (list lk0) lk0))
  9340. (lkend (cdr lkall)))
  9341. (mapcar (lambda(l)
  9342. (search-forward l nil lkend)
  9343. (goto-char (match-beginning 0))
  9344. (org-open-at-point))
  9345. lk))
  9346. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9347. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9348. ((and (org-at-timestamp-p t)
  9349. (not (org-in-regexp org-bracket-link-regexp)))
  9350. (org-follow-timestamp-link))
  9351. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9352. (not (org-in-regexp org-any-link-re)))
  9353. (org-footnote-action))
  9354. (t
  9355. (let (type path link line search (pos (point)))
  9356. (catch 'match
  9357. (save-excursion
  9358. (skip-chars-forward "^]\n\r")
  9359. (when (org-in-regexp org-bracket-link-regexp 1)
  9360. (setq link (org-extract-attributes
  9361. (org-link-unescape (org-match-string-no-properties 1))))
  9362. (while (string-match " *\n *" link)
  9363. (setq link (replace-match " " t t link)))
  9364. (setq link (org-link-expand-abbrev link))
  9365. (cond
  9366. ((or (file-name-absolute-p link)
  9367. (string-match "^\\.\\.?/" link))
  9368. (setq type "file" path link))
  9369. ((string-match org-link-re-with-space3 link)
  9370. (setq type (match-string 1 link) path (match-string 2 link)))
  9371. ((string-match "^help:+\\(.+\\)" link)
  9372. (setq type "help" path (match-string 1 link)))
  9373. (t (setq type "thisfile" path link)))
  9374. (throw 'match t)))
  9375. (when (get-text-property (point) 'org-linked-text)
  9376. (setq type "thisfile"
  9377. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9378. (1+ (point)) (point))
  9379. path (buffer-substring
  9380. (or (previous-single-property-change pos 'org-linked-text)
  9381. (point-min))
  9382. (or (next-single-property-change pos 'org-linked-text)
  9383. (point-max)))
  9384. ;; Ensure we will search for a <<<radio>>> link, not
  9385. ;; a simple reference like <<ref>>
  9386. path (concat "<" path))
  9387. (throw 'match t))
  9388. (save-excursion
  9389. (when (or (org-in-regexp org-angle-link-re)
  9390. (let ((match (org-in-regexp org-plain-link-re)))
  9391. ;; Check a plain link is not within a bracket link
  9392. (and match
  9393. (save-excursion
  9394. (progn
  9395. (goto-char (car match))
  9396. (not (org-in-regexp org-bracket-link-regexp))))))
  9397. (let ((line_ending (save-excursion (end-of-line) (point))))
  9398. ;; We are in a line before a plain or bracket link
  9399. (or (re-search-forward org-plain-link-re line_ending t)
  9400. (re-search-forward org-bracket-link-regexp line_ending t))))
  9401. (setq type (match-string 1)
  9402. path (org-link-unescape (match-string 2)))
  9403. (throw 'match t)))
  9404. (save-excursion
  9405. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9406. (setq type "tags"
  9407. path (match-string 1))
  9408. (while (string-match ":" path)
  9409. (setq path (replace-match "+" t t path)))
  9410. (throw 'match t)))
  9411. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9412. (setq type "tree-match"
  9413. path (match-string 1))
  9414. (throw 'match t)))
  9415. (unless path
  9416. (user-error "No link found"))
  9417. ;; switch back to reference buffer
  9418. ;; needed when if called in a temporary buffer through
  9419. ;; org-open-link-from-string
  9420. (with-current-buffer (or reference-buffer (current-buffer))
  9421. ;; Remove any trailing spaces in path
  9422. (if (string-match " +\\'" path)
  9423. (setq path (replace-match "" t t path)))
  9424. (if (and org-link-translation-function
  9425. (fboundp org-link-translation-function))
  9426. ;; Check if we need to translate the link
  9427. (let ((tmp (funcall org-link-translation-function type path)))
  9428. (setq type (car tmp) path (cdr tmp))))
  9429. (cond
  9430. ((assoc type org-link-protocols)
  9431. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9432. ((equal type "help")
  9433. (let ((f-or-v (intern path)))
  9434. (cond ((fboundp f-or-v)
  9435. (describe-function f-or-v))
  9436. ((boundp f-or-v)
  9437. (describe-variable f-or-v))
  9438. (t (error "Not a known function or variable")))))
  9439. ((equal type "mailto")
  9440. (let ((cmd (car org-link-mailto-program))
  9441. (args (cdr org-link-mailto-program)) args1
  9442. (address path) (subject "") a)
  9443. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9444. (setq address (match-string 1 path)
  9445. subject (org-link-escape (match-string 2 path))))
  9446. (while args
  9447. (cond
  9448. ((not (stringp (car args))) (push (pop args) args1))
  9449. (t (setq a (pop args))
  9450. (if (string-match "%a" a)
  9451. (setq a (replace-match address t t a)))
  9452. (if (string-match "%s" a)
  9453. (setq a (replace-match subject t t a)))
  9454. (push a args1))))
  9455. (apply cmd (nreverse args1))))
  9456. ((member type '("http" "https" "ftp" "news"))
  9457. (browse-url
  9458. (concat type ":"
  9459. (if (org-string-match-p
  9460. (concat "[[:nonascii:]"
  9461. org-link-escape-chars-browser "]")
  9462. path)
  9463. (org-link-escape path org-link-escape-chars-browser)
  9464. path))))
  9465. ((string= type "doi")
  9466. (browse-url
  9467. (concat org-doi-server-url
  9468. (if (org-string-match-p
  9469. (concat "[[:nonascii:]"
  9470. org-link-escape-chars-browser "]")
  9471. path)
  9472. (org-link-escape path org-link-escape-chars-browser)
  9473. path))))
  9474. ((member type '("message"))
  9475. (browse-url (concat type ":" path)))
  9476. ((string= type "tags")
  9477. (org-tags-view arg path))
  9478. ((string= type "tree-match")
  9479. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9480. ((string= type "file")
  9481. (if (string-match "::\\([0-9]+\\)\\'" path)
  9482. (setq line (string-to-number (match-string 1 path))
  9483. path (substring path 0 (match-beginning 0)))
  9484. (if (string-match "::\\(.+\\)\\'" path)
  9485. (setq search (match-string 1 path)
  9486. path (substring path 0 (match-beginning 0)))))
  9487. (if (string-match "[*?{]" (file-name-nondirectory path))
  9488. (dired path)
  9489. (org-open-file path arg line search)))
  9490. ((string= type "shell")
  9491. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9492. (cmd path))
  9493. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9494. (string-match org-confirm-shell-link-not-regexp cmd))
  9495. (not org-confirm-shell-link-function)
  9496. (funcall org-confirm-shell-link-function
  9497. (format "Execute \"%s\" in shell? "
  9498. (org-add-props cmd nil
  9499. 'face 'org-warning))))
  9500. (progn
  9501. (message "Executing %s" cmd)
  9502. (shell-command cmd buf)
  9503. (if (featurep 'midnight)
  9504. (setq clean-buffer-list-kill-buffer-names
  9505. (cons buf clean-buffer-list-kill-buffer-names))))
  9506. (error "Abort"))))
  9507. ((string= type "elisp")
  9508. (let ((cmd path))
  9509. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9510. (string-match org-confirm-elisp-link-not-regexp cmd))
  9511. (not org-confirm-elisp-link-function)
  9512. (funcall org-confirm-elisp-link-function
  9513. (format "Execute \"%s\" as elisp? "
  9514. (org-add-props cmd nil
  9515. 'face 'org-warning))))
  9516. (message "%s => %s" cmd
  9517. (if (equal (string-to-char cmd) ?\()
  9518. (eval (read cmd))
  9519. (call-interactively (read cmd))))
  9520. (error "Abort"))))
  9521. ((and (string= type "thisfile")
  9522. (run-hook-with-args-until-success
  9523. 'org-open-link-functions path)))
  9524. ((string= type "thisfile")
  9525. (if arg
  9526. (switch-to-buffer-other-window
  9527. (org-get-buffer-for-internal-link (current-buffer)))
  9528. (org-mark-ring-push))
  9529. (let ((cmd `(org-link-search
  9530. ,path
  9531. ,(cond ((equal arg '(4)) ''occur)
  9532. ((equal arg '(16)) ''org-occur))
  9533. ,pos)))
  9534. (condition-case nil (let ((org-link-search-inhibit-query t))
  9535. (eval cmd))
  9536. (error (progn (widen) (eval cmd))))))
  9537. (t (browse-url-at-point)))))))
  9538. (move-marker org-open-link-marker nil)
  9539. (run-hook-with-args 'org-follow-link-hook)))
  9540. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9541. "Offer links in the current entry and return the selected link.
  9542. If there is only one link, return it.
  9543. If NTH is an integer, return the NTH link found.
  9544. If ZERO is a string, check also this string for a link, and if
  9545. there is one, return it."
  9546. (with-current-buffer buffer
  9547. (save-excursion
  9548. (save-restriction
  9549. (widen)
  9550. (goto-char marker)
  9551. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9552. "\\(" org-angle-link-re "\\)\\|"
  9553. "\\(" org-plain-link-re "\\)"))
  9554. (cnt ?0)
  9555. (in-emacs (if (integerp nth) nil nth))
  9556. have-zero end links link c)
  9557. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9558. (push (match-string 0 zero) links)
  9559. (setq cnt (1- cnt) have-zero t))
  9560. (save-excursion
  9561. (org-back-to-heading t)
  9562. (setq end (save-excursion (outline-next-heading) (point)))
  9563. (while (re-search-forward re end t)
  9564. (push (match-string 0) links))
  9565. (setq links (org-uniquify (reverse links))))
  9566. (cond
  9567. ((null links)
  9568. (message "No links"))
  9569. ((equal (length links) 1)
  9570. (setq link (car links)))
  9571. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9572. (setq link (nth (if have-zero nth (1- nth)) links)))
  9573. (t ; we have to select a link
  9574. (save-excursion
  9575. (save-window-excursion
  9576. (delete-other-windows)
  9577. (with-output-to-temp-buffer "*Select Link*"
  9578. (mapc (lambda (l)
  9579. (if (not (string-match org-bracket-link-regexp l))
  9580. (princ (format "[%c] %s\n" (incf cnt)
  9581. (org-remove-angle-brackets l)))
  9582. (if (match-end 3)
  9583. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9584. (match-string 3 l) (match-string 1 l)))
  9585. (princ (format "[%c] %s\n" (incf cnt)
  9586. (match-string 1 l))))))
  9587. links))
  9588. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9589. (message "Select link to open, RET to open all:")
  9590. (setq c (read-char-exclusive))
  9591. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9592. (when (equal c ?q) (error "Abort"))
  9593. (if (equal c ?\C-m)
  9594. (setq link links)
  9595. (setq nth (- c ?0))
  9596. (if have-zero (setq nth (1+ nth)))
  9597. (unless (and (integerp nth) (>= (length links) nth))
  9598. (user-error "Invalid link selection"))
  9599. (setq link (nth (1- nth) links)))))
  9600. (cons link end))))))
  9601. ;; Add special file links that specify the way of opening
  9602. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9603. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9604. (defun org-open-file-with-system (path)
  9605. "Open file at PATH using the system way of opening it."
  9606. (org-open-file path 'system))
  9607. (defun org-open-file-with-emacs (path)
  9608. "Open file at PATH in Emacs."
  9609. (org-open-file path 'emacs))
  9610. ;;; File search
  9611. (defvar org-create-file-search-functions nil
  9612. "List of functions to construct the right search string for a file link.
  9613. These functions are called in turn with point at the location to
  9614. which the link should point.
  9615. A function in the hook should first test if it would like to
  9616. handle this file type, for example by checking the `major-mode'
  9617. or the file extension. If it decides not to handle this file, it
  9618. should just return nil to give other functions a chance. If it
  9619. does handle the file, it must return the search string to be used
  9620. when following the link. The search string will be part of the
  9621. file link, given after a double colon, and `org-open-at-point'
  9622. will automatically search for it. If special measures must be
  9623. taken to make the search successful, another function should be
  9624. added to the companion hook `org-execute-file-search-functions',
  9625. which see.
  9626. A function in this hook may also use `setq' to set the variable
  9627. `description' to provide a suggestion for the descriptive text to
  9628. be used for this link when it gets inserted into an Org-mode
  9629. buffer with \\[org-insert-link].")
  9630. (defvar org-execute-file-search-functions nil
  9631. "List of functions to execute a file search triggered by a link.
  9632. Functions added to this hook must accept a single argument, the
  9633. search string that was part of the file link, the part after the
  9634. double colon. The function must first check if it would like to
  9635. handle this search, for example by checking the `major-mode' or
  9636. the file extension. If it decides not to handle this search, it
  9637. should just return nil to give other functions a chance. If it
  9638. does handle the search, it must return a non-nil value to keep
  9639. other functions from trying.
  9640. Each function can access the current prefix argument through the
  9641. variable `current-prefix-arg'. Note that a single prefix is used
  9642. to force opening a link in Emacs, so it may be good to only use a
  9643. numeric or double prefix to guide the search function.
  9644. In case this is needed, a function in this hook can also restore
  9645. the window configuration before `org-open-at-point' was called using:
  9646. (set-window-configuration org-window-config-before-follow-link)")
  9647. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9648. (defun org-link-search (s &optional type avoid-pos stealth)
  9649. "Search for a link search option.
  9650. If S is surrounded by forward slashes, it is interpreted as a
  9651. regular expression. In org-mode files, this will create an `org-occur'
  9652. sparse tree. In ordinary files, `occur' will be used to list matches.
  9653. If the current buffer is in `dired-mode', grep will be used to search
  9654. in all files. If AVOID-POS is given, ignore matches near that position.
  9655. When optional argument STEALTH is non-nil, do not modify
  9656. visibility around point, thus ignoring
  9657. `org-show-hierarchy-above', `org-show-following-heading' and
  9658. `org-show-siblings' variables."
  9659. (let ((case-fold-search t)
  9660. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9661. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9662. (append '(("") (" ") ("\t") ("\n"))
  9663. org-emphasis-alist)
  9664. "\\|") "\\)"))
  9665. (pos (point))
  9666. (pre nil) (post nil)
  9667. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9668. (cond
  9669. ;; First check if there are any special search functions
  9670. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9671. ;; Now try the builtin stuff
  9672. ((and (equal (string-to-char s0) ?#)
  9673. (> (length s0) 1)
  9674. (save-excursion
  9675. (goto-char (point-min))
  9676. (and
  9677. (re-search-forward
  9678. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9679. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9680. (setq type 'dedicated
  9681. pos (match-beginning 0))))
  9682. ;; There is an exact target for this
  9683. (goto-char pos)
  9684. (org-back-to-heading t)))
  9685. ((save-excursion
  9686. (goto-char (point-min))
  9687. (and
  9688. (re-search-forward
  9689. (concat "<<" (regexp-quote s0) ">>") nil t)
  9690. (setq type 'dedicated
  9691. pos (match-beginning 0))))
  9692. ;; There is an exact target for this
  9693. (goto-char pos))
  9694. ((save-excursion
  9695. (goto-char (point-min))
  9696. (and
  9697. (re-search-forward
  9698. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9699. (setq type 'dedicated pos (match-beginning 0))))
  9700. ;; Found an element with a matching #+name affiliated keyword.
  9701. (goto-char pos))
  9702. ((and (string-match "^(\\(.*\\))$" s0)
  9703. (save-excursion
  9704. (goto-char (point-min))
  9705. (and
  9706. (re-search-forward
  9707. (concat "[^[]" (regexp-quote
  9708. (format org-coderef-label-format
  9709. (match-string 1 s0))))
  9710. nil t)
  9711. (setq type 'dedicated
  9712. pos (1+ (match-beginning 0))))))
  9713. ;; There is a coderef target for this
  9714. (goto-char pos))
  9715. ((string-match "^/\\(.*\\)/$" s)
  9716. ;; A regular expression
  9717. (cond
  9718. ((derived-mode-p 'org-mode)
  9719. (org-occur (match-string 1 s)))
  9720. (t (org-do-occur (match-string 1 s)))))
  9721. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9722. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9723. (goto-char (point-min))
  9724. (cond
  9725. ((let (case-fold-search)
  9726. (re-search-forward (format org-complex-heading-regexp-format
  9727. (regexp-quote s))
  9728. nil t))
  9729. ;; OK, found a match
  9730. (setq type 'dedicated)
  9731. (goto-char (match-beginning 0)))
  9732. ((and (not org-link-search-inhibit-query)
  9733. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9734. (y-or-n-p "No match - create this as a new heading? "))
  9735. (goto-char (point-max))
  9736. (or (bolp) (newline))
  9737. (insert "* " s "\n")
  9738. (beginning-of-line 0))
  9739. (t
  9740. (goto-char pos)
  9741. (error "No match"))))
  9742. (t
  9743. ;; A normal search string
  9744. (when (equal (string-to-char s) ?*)
  9745. ;; Anchor on headlines, post may include tags.
  9746. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9747. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9748. s (substring s 1)))
  9749. (remove-text-properties
  9750. 0 (length s)
  9751. '(face nil mouse-face nil keymap nil fontified nil) s)
  9752. ;; Make a series of regular expressions to find a match
  9753. (setq words (org-split-string s "[ \n\r\t]+")
  9754. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9755. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9756. "\\)" markers)
  9757. re2a_ (concat "\\(" (mapconcat 'downcase words
  9758. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9759. re2a (concat "[ \t\r\n]" re2a_)
  9760. re4_ (concat "\\(" (mapconcat 'downcase words
  9761. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9762. re4 (concat "[^a-zA-Z_]" re4_)
  9763. re1 (concat pre re2 post)
  9764. re3 (concat pre (if pre re4_ re4) post)
  9765. re5 (concat pre ".*" re4)
  9766. re2 (concat pre re2)
  9767. re2a (concat pre (if pre re2a_ re2a))
  9768. re4 (concat pre (if pre re4_ re4))
  9769. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9770. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9771. re5 "\\)"))
  9772. (cond
  9773. ((eq type 'org-occur) (org-occur reall))
  9774. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9775. (t (goto-char (point-min))
  9776. (setq type 'fuzzy)
  9777. (if (or (and (org-search-not-self 1 re0 nil t)
  9778. (setq type 'dedicated))
  9779. (org-search-not-self 1 re1 nil t)
  9780. (org-search-not-self 1 re2 nil t)
  9781. (org-search-not-self 1 re2a nil t)
  9782. (org-search-not-self 1 re3 nil t)
  9783. (org-search-not-self 1 re4 nil t)
  9784. (org-search-not-self 1 re5 nil t))
  9785. (goto-char (match-beginning 1))
  9786. (goto-char pos)
  9787. (error "No match"))))))
  9788. (and (derived-mode-p 'org-mode)
  9789. (not stealth)
  9790. (org-show-context 'link-search))
  9791. type))
  9792. (defun org-search-not-self (group &rest args)
  9793. "Execute `re-search-forward', but only accept matches that do not
  9794. enclose the position of `org-open-link-marker'."
  9795. (let ((m org-open-link-marker))
  9796. (catch 'exit
  9797. (while (apply 're-search-forward args)
  9798. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9799. (goto-char (match-end group))
  9800. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9801. (> (match-beginning 0) (marker-position m))
  9802. (< (match-end 0) (marker-position m)))
  9803. (save-match-data
  9804. (or (not (org-in-regexp
  9805. org-bracket-link-analytic-regexp 1))
  9806. (not (match-end 4)) ; no description
  9807. (and (<= (match-beginning 4) (point))
  9808. (>= (match-end 4) (point))))))
  9809. (throw 'exit (point))))))))
  9810. (defun org-get-buffer-for-internal-link (buffer)
  9811. "Return a buffer to be used for displaying the link target of internal links."
  9812. (cond
  9813. ((not org-display-internal-link-with-indirect-buffer)
  9814. buffer)
  9815. ((string-match "(Clone)$" (buffer-name buffer))
  9816. (message "Buffer is already a clone, not making another one")
  9817. ;; we also do not modify visibility in this case
  9818. buffer)
  9819. (t ; make a new indirect buffer for displaying the link
  9820. (let* ((bn (buffer-name buffer))
  9821. (ibn (concat bn "(Clone)"))
  9822. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9823. (with-current-buffer ib (org-overview))
  9824. ib))))
  9825. (defun org-do-occur (regexp &optional cleanup)
  9826. "Call the Emacs command `occur'.
  9827. If CLEANUP is non-nil, remove the printout of the regular expression
  9828. in the *Occur* buffer. This is useful if the regex is long and not useful
  9829. to read."
  9830. (occur regexp)
  9831. (when cleanup
  9832. (let ((cwin (selected-window)) win beg end)
  9833. (when (setq win (get-buffer-window "*Occur*"))
  9834. (select-window win))
  9835. (goto-char (point-min))
  9836. (when (re-search-forward "match[a-z]+" nil t)
  9837. (setq beg (match-end 0))
  9838. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9839. (setq end (1- (match-beginning 0)))))
  9840. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9841. (goto-char (point-min))
  9842. (select-window cwin))))
  9843. ;;; The mark ring for links jumps
  9844. (defvar org-mark-ring nil
  9845. "Mark ring for positions before jumps in Org-mode.")
  9846. (defvar org-mark-ring-last-goto nil
  9847. "Last position in the mark ring used to go back.")
  9848. ;; Fill and close the ring
  9849. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9850. (loop for i from 1 to org-mark-ring-length do
  9851. (push (make-marker) org-mark-ring))
  9852. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9853. org-mark-ring)
  9854. (defun org-mark-ring-push (&optional pos buffer)
  9855. "Put the current position or POS into the mark ring and rotate it."
  9856. (interactive)
  9857. (setq pos (or pos (point)))
  9858. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9859. (move-marker (car org-mark-ring)
  9860. (or pos (point))
  9861. (or buffer (current-buffer)))
  9862. (message "%s"
  9863. (substitute-command-keys
  9864. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9865. (defun org-mark-ring-goto (&optional n)
  9866. "Jump to the previous position in the mark ring.
  9867. With prefix arg N, jump back that many stored positions. When
  9868. called several times in succession, walk through the entire ring.
  9869. Org-mode commands jumping to a different position in the current file,
  9870. or to another Org-mode file, automatically push the old position
  9871. onto the ring."
  9872. (interactive "p")
  9873. (let (p m)
  9874. (if (eq last-command this-command)
  9875. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9876. (setq p org-mark-ring))
  9877. (setq org-mark-ring-last-goto p)
  9878. (setq m (car p))
  9879. (org-pop-to-buffer-same-window (marker-buffer m))
  9880. (goto-char m)
  9881. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9882. (defun org-remove-angle-brackets (s)
  9883. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9884. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9885. s)
  9886. (defun org-add-angle-brackets (s)
  9887. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9888. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9889. s)
  9890. (defun org-remove-double-quotes (s)
  9891. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9892. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9893. s)
  9894. ;;; Following specific links
  9895. (defun org-follow-timestamp-link ()
  9896. "Open an agenda view for the time-stamp date/range at point."
  9897. (cond
  9898. ((org-at-date-range-p t)
  9899. (let ((org-agenda-start-on-weekday)
  9900. (t1 (match-string 1))
  9901. (t2 (match-string 2)) tt1 tt2)
  9902. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9903. tt2 (time-to-days (org-time-string-to-time t2)))
  9904. (let ((org-agenda-buffer-tmp-name
  9905. (format "*Org Agenda(a:%s)"
  9906. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9907. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9908. ((org-at-timestamp-p t)
  9909. (let ((org-agenda-buffer-tmp-name
  9910. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9911. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9912. (substring (match-string 1) 0 10)))
  9913. 1)))
  9914. (t (error "This should not happen"))))
  9915. ;;; Following file links
  9916. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9917. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9918. (declare-function mailcap-mime-info
  9919. "mailcap" (string &optional request no-decode))
  9920. (defvar org-wait nil)
  9921. (defun org-open-file (path &optional in-emacs line search)
  9922. "Open the file at PATH.
  9923. First, this expands any special file name abbreviations. Then the
  9924. configuration variable `org-file-apps' is checked if it contains an
  9925. entry for this file type, and if yes, the corresponding command is launched.
  9926. If no application is found, Emacs simply visits the file.
  9927. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9928. With a double \\[universal-argument] \\[universal-argument] \
  9929. prefix arg, Org tries to avoid opening in Emacs
  9930. and to use an external application to visit the file.
  9931. Optional LINE specifies a line to go to, optional SEARCH a string
  9932. to search for. If LINE or SEARCH is given, the file will be
  9933. opened in Emacs, unless an entry from org-file-apps that makes
  9934. use of groups in a regexp matches.
  9935. If you want to change the way frames are used when following a
  9936. link, please customize `org-link-frame-setup'.
  9937. If the file does not exist, an error is thrown."
  9938. (let* ((file (if (equal path "")
  9939. buffer-file-name
  9940. (substitute-in-file-name (expand-file-name path))))
  9941. (file-apps (append org-file-apps (org-default-apps)))
  9942. (apps (org-remove-if
  9943. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9944. (apps-dlink (org-remove-if-not
  9945. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9946. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9947. (dirp (if remp nil (file-directory-p file)))
  9948. (file (if (and dirp org-open-directory-means-index-dot-org)
  9949. (concat (file-name-as-directory file) "index.org")
  9950. file))
  9951. (a-m-a-p (assq 'auto-mode apps))
  9952. (dfile (downcase file))
  9953. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9954. (link (cond ((and (eq line nil)
  9955. (eq search nil))
  9956. file)
  9957. (line
  9958. (concat file "::" (number-to-string line)))
  9959. (search
  9960. (concat file "::" search))))
  9961. (dlink (downcase link))
  9962. (old-buffer (current-buffer))
  9963. (old-pos (point))
  9964. (old-mode major-mode)
  9965. ext cmd link-match-data)
  9966. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9967. (setq ext (match-string 1 dfile))
  9968. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9969. (setq ext (match-string 1 dfile))))
  9970. (cond
  9971. ((member in-emacs '((16) system))
  9972. (setq cmd (cdr (assoc 'system apps))))
  9973. (in-emacs (setq cmd 'emacs))
  9974. (t
  9975. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9976. (and dirp (cdr (assoc 'directory apps)))
  9977. ; first, try matching against apps-dlink
  9978. ; if we get a match here, store the match data for later
  9979. (let ((match (assoc-default dlink apps-dlink
  9980. 'string-match)))
  9981. (if match
  9982. (progn (setq link-match-data (match-data))
  9983. match)
  9984. (progn (setq in-emacs (or in-emacs line search))
  9985. nil))) ; if we have no match in apps-dlink,
  9986. ; always open the file in emacs if line or search
  9987. ; is given (for backwards compatibility)
  9988. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9989. 'string-match)
  9990. (cdr (assoc ext apps))
  9991. (cdr (assoc t apps))))))
  9992. (when (eq cmd 'system)
  9993. (setq cmd (cdr (assoc 'system apps))))
  9994. (when (eq cmd 'default)
  9995. (setq cmd (cdr (assoc t apps))))
  9996. (when (eq cmd 'mailcap)
  9997. (require 'mailcap)
  9998. (mailcap-parse-mailcaps)
  9999. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10000. (command (mailcap-mime-info mime-type)))
  10001. (if (stringp command)
  10002. (setq cmd command)
  10003. (setq cmd 'emacs))))
  10004. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10005. (not (file-exists-p file))
  10006. (not org-open-non-existing-files))
  10007. (user-error "No such file: %s" file))
  10008. (cond
  10009. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10010. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10011. (while (string-match "['\"]%s['\"]" cmd)
  10012. (setq cmd (replace-match "%s" t t cmd)))
  10013. (while (string-match "%s" cmd)
  10014. (setq cmd (replace-match
  10015. (save-match-data
  10016. (shell-quote-argument
  10017. (convert-standard-filename file)))
  10018. t t cmd)))
  10019. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10020. (save-match-data
  10021. (let ((match-index 1)
  10022. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10023. (set-match-data link-match-data)
  10024. (while (<= match-index number-of-groups)
  10025. (let ((regex (concat "%" (number-to-string match-index)))
  10026. (replace-with (match-string match-index dlink)))
  10027. (while (string-match regex cmd)
  10028. (setq cmd (replace-match replace-with t t cmd))))
  10029. (setq match-index (+ match-index 1)))))
  10030. (save-window-excursion
  10031. (message "Running %s...done" cmd)
  10032. (start-process-shell-command cmd nil cmd)
  10033. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10034. ((or (stringp cmd)
  10035. (eq cmd 'emacs))
  10036. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10037. (widen)
  10038. (if line (org-goto-line line)
  10039. (if search (org-link-search search))))
  10040. ((consp cmd)
  10041. (let ((file (convert-standard-filename file)))
  10042. (save-match-data
  10043. (set-match-data link-match-data)
  10044. (eval cmd))))
  10045. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10046. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10047. (or (not (equal old-buffer (current-buffer)))
  10048. (not (equal old-pos (point))))
  10049. (org-mark-ring-push old-pos old-buffer))))
  10050. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10051. "This function returns non-nil if `entry' uses a regular
  10052. expression which should be matched against the whole link by
  10053. org-open-file.
  10054. It assumes that is the case when the entry uses a regular
  10055. expression which has at least one grouping construct and the
  10056. action is either a lisp form or a command string containing
  10057. '%1', i.e. using at least one subexpression match as a
  10058. parameter."
  10059. (let ((selector (car entry))
  10060. (action (cdr entry)))
  10061. (if (stringp selector)
  10062. (and (> (regexp-opt-depth selector) 0)
  10063. (or (and (stringp action)
  10064. (string-match "%[0-9]" action))
  10065. (consp action)))
  10066. nil)))
  10067. (defun org-default-apps ()
  10068. "Return the default applications for this operating system."
  10069. (cond
  10070. ((eq system-type 'darwin)
  10071. org-file-apps-defaults-macosx)
  10072. ((eq system-type 'windows-nt)
  10073. org-file-apps-defaults-windowsnt)
  10074. (t org-file-apps-defaults-gnu)))
  10075. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10076. "Convert extensions to regular expressions in the cars of LIST.
  10077. Also, weed out any non-string entries, because the return value is used
  10078. only for regexp matching.
  10079. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10080. point to the symbol `emacs', indicating that the file should
  10081. be opened in Emacs."
  10082. (append
  10083. (delq nil
  10084. (mapcar (lambda (x)
  10085. (if (not (stringp (car x)))
  10086. nil
  10087. (if (string-match "\\W" (car x))
  10088. x
  10089. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10090. list))
  10091. (if add-auto-mode
  10092. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10093. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10094. (defun org-file-remote-p (file)
  10095. "Test whether FILE specifies a location on a remote system.
  10096. Return non-nil if the location is indeed remote.
  10097. For example, the filename \"/user@host:/foo\" specifies a location
  10098. on the system \"/user@host:\"."
  10099. (cond ((fboundp 'file-remote-p)
  10100. (file-remote-p file))
  10101. ((fboundp 'tramp-handle-file-remote-p)
  10102. (tramp-handle-file-remote-p file))
  10103. ((and (boundp 'ange-ftp-name-format)
  10104. (string-match (car ange-ftp-name-format) file))
  10105. t)))
  10106. ;;;; Refiling
  10107. (defun org-get-org-file ()
  10108. "Read a filename, with default directory `org-directory'."
  10109. (let ((default (or org-default-notes-file remember-data-file)))
  10110. (read-file-name (format "File name [%s]: " default)
  10111. (file-name-as-directory org-directory)
  10112. default)))
  10113. (defun org-notes-order-reversed-p ()
  10114. "Check if the current file should receive notes in reversed order."
  10115. (cond
  10116. ((not org-reverse-note-order) nil)
  10117. ((eq t org-reverse-note-order) t)
  10118. ((not (listp org-reverse-note-order)) nil)
  10119. (t (catch 'exit
  10120. (let ((all org-reverse-note-order)
  10121. entry)
  10122. (while (setq entry (pop all))
  10123. (if (string-match (car entry) buffer-file-name)
  10124. (throw 'exit (cdr entry))))
  10125. nil)))))
  10126. (defvar org-refile-target-table nil
  10127. "The list of refile targets, created by `org-refile'.")
  10128. (defvar org-agenda-new-buffers nil
  10129. "Buffers created to visit agenda files.")
  10130. (defvar org-refile-cache nil
  10131. "Cache for refile targets.")
  10132. (defvar org-refile-markers nil
  10133. "All the markers used for caching refile locations.")
  10134. (defun org-refile-marker (pos)
  10135. "Get a new refile marker, but only if caching is in use."
  10136. (if (not org-refile-use-cache)
  10137. pos
  10138. (let ((m (make-marker)))
  10139. (move-marker m pos)
  10140. (push m org-refile-markers)
  10141. m)))
  10142. (defun org-refile-cache-clear ()
  10143. "Clear the refile cache and disable all the markers."
  10144. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10145. (setq org-refile-markers nil)
  10146. (setq org-refile-cache nil)
  10147. (message "Refile cache has been cleared"))
  10148. (defun org-refile-cache-check-set (set)
  10149. "Check if all the markers in the cache still have live buffers."
  10150. (let (marker)
  10151. (catch 'exit
  10152. (while (and set (setq marker (nth 3 (pop set))))
  10153. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10154. (when (and marker (null (marker-buffer marker)))
  10155. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10156. (sit-for 3)
  10157. (throw 'exit nil)))
  10158. t)))
  10159. (defun org-refile-cache-put (set &rest identifiers)
  10160. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10161. (let* ((key (sha1 (prin1-to-string identifiers)))
  10162. (entry (assoc key org-refile-cache)))
  10163. (if entry
  10164. (setcdr entry set)
  10165. (push (cons key set) org-refile-cache))))
  10166. (defun org-refile-cache-get (&rest identifiers)
  10167. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10168. (cond
  10169. ((not org-refile-cache) nil)
  10170. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10171. (t
  10172. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10173. org-refile-cache))))
  10174. (and set (org-refile-cache-check-set set) set)))))
  10175. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10176. "Produce a table with refile targets."
  10177. (let ((case-fold-search nil)
  10178. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10179. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10180. targets tgs txt re files f desc descre fast-path-p level pos0)
  10181. (message "Getting targets...")
  10182. (with-current-buffer (or default-buffer (current-buffer))
  10183. (while (setq entry (pop entries))
  10184. (setq files (car entry) desc (cdr entry))
  10185. (setq fast-path-p nil)
  10186. (cond
  10187. ((null files) (setq files (list (current-buffer))))
  10188. ((eq files 'org-agenda-files)
  10189. (setq files (org-agenda-files 'unrestricted)))
  10190. ((and (symbolp files) (fboundp files))
  10191. (setq files (funcall files)))
  10192. ((and (symbolp files) (boundp files))
  10193. (setq files (symbol-value files))))
  10194. (if (stringp files) (setq files (list files)))
  10195. (cond
  10196. ((eq (car desc) :tag)
  10197. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10198. ((eq (car desc) :todo)
  10199. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10200. ((eq (car desc) :regexp)
  10201. (setq descre (cdr desc)))
  10202. ((eq (car desc) :level)
  10203. (setq descre (concat "^\\*\\{" (number-to-string
  10204. (if org-odd-levels-only
  10205. (1- (* 2 (cdr desc)))
  10206. (cdr desc)))
  10207. "\\}[ \t]")))
  10208. ((eq (car desc) :maxlevel)
  10209. (setq fast-path-p t)
  10210. (setq descre (concat "^\\*\\{1," (number-to-string
  10211. (if org-odd-levels-only
  10212. (1- (* 2 (cdr desc)))
  10213. (cdr desc)))
  10214. "\\}[ \t]")))
  10215. (t (error "Bad refiling target description %s" desc)))
  10216. (while (setq f (pop files))
  10217. (with-current-buffer
  10218. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10219. (or
  10220. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10221. (progn
  10222. (if (bufferp f) (setq f (buffer-file-name
  10223. (buffer-base-buffer f))))
  10224. (setq f (and f (expand-file-name f)))
  10225. (if (eq org-refile-use-outline-path 'file)
  10226. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10227. (save-excursion
  10228. (save-restriction
  10229. (widen)
  10230. (goto-char (point-min))
  10231. (while (re-search-forward descre nil t)
  10232. (goto-char (setq pos0 (point-at-bol)))
  10233. (catch 'next
  10234. (when org-refile-target-verify-function
  10235. (save-match-data
  10236. (or (funcall org-refile-target-verify-function)
  10237. (throw 'next t))))
  10238. (when (and (looking-at org-complex-heading-regexp)
  10239. (not (member (match-string 4) excluded-entries))
  10240. (match-string 4))
  10241. (setq level (org-reduced-level
  10242. (- (match-end 1) (match-beginning 1)))
  10243. txt (org-link-display-format (match-string 4))
  10244. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10245. re (format org-complex-heading-regexp-format
  10246. (regexp-quote (match-string 4))))
  10247. (when org-refile-use-outline-path
  10248. (setq txt (mapconcat
  10249. 'org-protect-slash
  10250. (append
  10251. (if (eq org-refile-use-outline-path
  10252. 'file)
  10253. (list (file-name-nondirectory
  10254. (buffer-file-name
  10255. (buffer-base-buffer))))
  10256. (if (eq org-refile-use-outline-path
  10257. 'full-file-path)
  10258. (list (buffer-file-name
  10259. (buffer-base-buffer)))))
  10260. (org-get-outline-path fast-path-p
  10261. level txt)
  10262. (list txt))
  10263. "/")))
  10264. (push (list txt f re (org-refile-marker (point)))
  10265. tgs)))
  10266. (when (= (point) pos0)
  10267. ;; verification function has not moved point
  10268. (goto-char (point-at-eol))))))))
  10269. (when org-refile-use-cache
  10270. (org-refile-cache-put tgs (buffer-file-name) descre))
  10271. (setq targets (append tgs targets))))))
  10272. (message "Getting targets...done")
  10273. (nreverse targets)))
  10274. (defun org-protect-slash (s)
  10275. (while (string-match "/" s)
  10276. (setq s (replace-match "\\" t t s)))
  10277. s)
  10278. (defvar org-olpa (make-vector 20 nil))
  10279. (defun org-get-outline-path (&optional fastp level heading)
  10280. "Return the outline path to the current entry, as a list.
  10281. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10282. routine which makes outline path derivations for an entire file,
  10283. avoiding backtracing. Refile target collection makes use of that."
  10284. (if fastp
  10285. (progn
  10286. (if (> level 19)
  10287. (error "Outline path failure, more than 19 levels"))
  10288. (loop for i from level upto 19 do
  10289. (aset org-olpa i nil))
  10290. (prog1
  10291. (delq nil (append org-olpa nil))
  10292. (aset org-olpa level heading)))
  10293. (let (rtn case-fold-search)
  10294. (save-excursion
  10295. (save-restriction
  10296. (widen)
  10297. (while (org-up-heading-safe)
  10298. (when (looking-at org-complex-heading-regexp)
  10299. (push (org-trim
  10300. (replace-regexp-in-string
  10301. ;; Remove statistical/checkboxes cookies
  10302. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10303. (org-match-string-no-properties 4)))
  10304. rtn)))
  10305. rtn)))))
  10306. (defun org-format-outline-path (path &optional width prefix separator)
  10307. "Format the outline path PATH for display.
  10308. WIDTH is the maximum number of characters that is available.
  10309. PREFIX is a prefix to be included in the returned string,
  10310. such as the file name.
  10311. SEPARATOR is inserted between the different parts of the path,
  10312. the default is \"/\"."
  10313. (setq width (or width 79))
  10314. (if prefix (setq width (- width (length prefix))))
  10315. (if (not path)
  10316. (or prefix "")
  10317. (let* ((nsteps (length path))
  10318. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10319. (maxwidth (if (<= total-width width)
  10320. 10000 ;; everything fits
  10321. ;; we need to shorten the level headings
  10322. (/ (- width nsteps) nsteps)))
  10323. (org-odd-levels-only nil)
  10324. (n 0)
  10325. (total (1+ (length prefix))))
  10326. (setq maxwidth (max maxwidth 10))
  10327. (concat prefix
  10328. (if prefix (or separator "/"))
  10329. (mapconcat
  10330. (lambda (h)
  10331. (setq n (1+ n))
  10332. (if (and (= n nsteps) (< maxwidth 10000))
  10333. (setq maxwidth (- total-width total)))
  10334. (if (< (length h) maxwidth)
  10335. (progn (setq total (+ total (length h) 1)) h)
  10336. (setq h (substring h 0 (- maxwidth 2))
  10337. total (+ total maxwidth 1))
  10338. (if (string-match "[ \t]+\\'" h)
  10339. (setq h (substring h 0 (match-beginning 0))))
  10340. (setq h (concat h "..")))
  10341. (org-add-props h nil 'face
  10342. (nth (% (1- n) org-n-level-faces)
  10343. org-level-faces))
  10344. h)
  10345. path (or separator "/"))))))
  10346. (defun org-display-outline-path (&optional file current separator just-return-string)
  10347. "Display the current outline path in the echo area.
  10348. If FILE is non-nil, prepend the output with the file name.
  10349. If CURRENT is non-nil, append the current heading to the output.
  10350. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10351. the different parts of the path and defaults to \"/\".
  10352. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10353. (interactive "P")
  10354. (let* (case-fold-search
  10355. message-log-max ; Don't populate the *Messages* buffer
  10356. (bfn (buffer-file-name (buffer-base-buffer)))
  10357. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10358. res)
  10359. (if current (setq path (append path
  10360. (save-excursion
  10361. (org-back-to-heading t)
  10362. (if (looking-at org-complex-heading-regexp)
  10363. (list (match-string 4)))))))
  10364. (setq res
  10365. (org-format-outline-path
  10366. path
  10367. (1- (frame-width))
  10368. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10369. separator))
  10370. (if just-return-string
  10371. (org-no-properties res)
  10372. (message "%s" res))))
  10373. (defvar org-refile-history nil
  10374. "History for refiling operations.")
  10375. (defvar org-after-refile-insert-hook nil
  10376. "Hook run after `org-refile' has inserted its stuff at the new location.
  10377. Note that this is still *before* the stuff will be removed from
  10378. the *old* location.")
  10379. (defvar org-capture-last-stored-marker)
  10380. (defvar org-refile-keep nil
  10381. "Non-nil means `org-refile' will copy instead of refile.")
  10382. (defun org-copy ()
  10383. "Like `org-refile', but copy."
  10384. (interactive)
  10385. (let ((org-refile-keep t))
  10386. (funcall 'org-refile nil nil nil "Copy")))
  10387. (defun org-refile (&optional goto default-buffer rfloc msg)
  10388. "Move the entry or entries at point to another heading.
  10389. The list of target headings is compiled using the information in
  10390. `org-refile-targets', which see.
  10391. At the target location, the entry is filed as a subitem of the target
  10392. heading. Depending on `org-reverse-note-order', the new subitem will
  10393. either be the first or the last subitem.
  10394. If there is an active region, all entries in that region will be moved.
  10395. However, the region must fulfill the requirement that the first heading
  10396. is the first one sets the top-level of the moved text - at most siblings
  10397. below it are allowed.
  10398. With prefix arg GOTO, the command will only visit the target location
  10399. and not actually move anything.
  10400. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10401. go to the location where the last refiling operation has put the subtree.
  10402. With a prefix argument of `2', refile to the running clock.
  10403. RFLOC can be a refile location obtained in a different way.
  10404. MSG is a string to replace \"Refile\" in the default prompt with
  10405. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10406. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10407. If you are using target caching (see `org-refile-use-cache'),
  10408. you have to clear the target cache in order to find new targets.
  10409. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10410. prefix argument (`C-u C-u C-u C-c C-w')."
  10411. (interactive "P")
  10412. (if (member goto '(0 (64)))
  10413. (org-refile-cache-clear)
  10414. (let* ((actionmsg (or msg "Refile"))
  10415. (cbuf (current-buffer))
  10416. (regionp (org-region-active-p))
  10417. (region-start (and regionp (region-beginning)))
  10418. (region-end (and regionp (region-end)))
  10419. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10420. pos it nbuf file re level reversed)
  10421. (setq last-command nil)
  10422. (when regionp
  10423. (goto-char region-start)
  10424. (or (bolp) (goto-char (point-at-bol)))
  10425. (setq region-start (point))
  10426. (unless (or (org-kill-is-subtree-p
  10427. (buffer-substring region-start region-end))
  10428. (prog1 org-refile-active-region-within-subtree
  10429. (let ((s (point-at-eol)))
  10430. (org-toggle-heading)
  10431. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10432. (user-error "The region is not a (sequence of) subtree(s)")))
  10433. (if (equal goto '(16))
  10434. (org-refile-goto-last-stored)
  10435. (when (or
  10436. (and (equal goto 2)
  10437. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10438. (prog1
  10439. (setq it (list (or org-clock-heading "running clock")
  10440. (buffer-file-name
  10441. (marker-buffer org-clock-hd-marker))
  10442. ""
  10443. (marker-position org-clock-hd-marker)))
  10444. (setq goto nil)))
  10445. (setq it (or rfloc
  10446. (let (heading-text)
  10447. (save-excursion
  10448. (unless goto
  10449. (org-back-to-heading t)
  10450. (setq heading-text
  10451. (nth 4 (org-heading-components))))
  10452. (org-refile-get-location
  10453. (cond (goto "Goto")
  10454. (regionp (concat actionmsg " region to"))
  10455. (t (concat actionmsg " subtree \""
  10456. heading-text "\" to")))
  10457. default-buffer
  10458. (and (not (equal '(4) goto))
  10459. org-refile-allow-creating-parent-nodes)
  10460. goto))))))
  10461. (setq file (nth 1 it)
  10462. re (nth 2 it)
  10463. pos (nth 3 it))
  10464. (if (and (not goto)
  10465. pos
  10466. (equal (buffer-file-name) file)
  10467. (if regionp
  10468. (and (>= pos region-start)
  10469. (<= pos region-end))
  10470. (and (>= pos (point))
  10471. (< pos (save-excursion
  10472. (org-end-of-subtree t t))))))
  10473. (error "Cannot refile to position inside the tree or region"))
  10474. (setq nbuf (or (find-buffer-visiting file)
  10475. (find-file-noselect file)))
  10476. (if goto
  10477. (progn
  10478. (org-pop-to-buffer-same-window nbuf)
  10479. (goto-char pos)
  10480. (org-show-context 'org-goto))
  10481. (if regionp
  10482. (progn
  10483. (org-kill-new (buffer-substring region-start region-end))
  10484. (org-save-markers-in-region region-start region-end))
  10485. (org-copy-subtree 1 nil t))
  10486. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10487. (find-file-noselect file)))
  10488. (setq reversed (org-notes-order-reversed-p))
  10489. (save-excursion
  10490. (save-restriction
  10491. (widen)
  10492. (if pos
  10493. (progn
  10494. (goto-char pos)
  10495. (looking-at org-outline-regexp)
  10496. (setq level (org-get-valid-level (funcall outline-level) 1))
  10497. (goto-char
  10498. (if reversed
  10499. (or (outline-next-heading) (point-max))
  10500. (or (save-excursion (org-get-next-sibling))
  10501. (org-end-of-subtree t t)
  10502. (point-max)))))
  10503. (setq level 1)
  10504. (if (not reversed)
  10505. (goto-char (point-max))
  10506. (goto-char (point-min))
  10507. (or (outline-next-heading) (goto-char (point-max)))))
  10508. (if (not (bolp)) (newline))
  10509. (org-paste-subtree level)
  10510. (when org-log-refile
  10511. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10512. (unless (eq org-log-refile 'note)
  10513. (save-excursion (org-add-log-note))))
  10514. (and org-auto-align-tags
  10515. (let ((org-loop-over-headlines-in-active-region nil))
  10516. (org-set-tags nil t)))
  10517. (with-demoted-errors
  10518. (bookmark-set "org-refile-last-stored"))
  10519. ;; If we are refiling for capture, make sure that the
  10520. ;; last-capture pointers point here
  10521. (when (org-bound-and-true-p org-refile-for-capture)
  10522. (with-demoted-errors
  10523. (bookmark-set "org-capture-last-stored-marker"))
  10524. (move-marker org-capture-last-stored-marker (point)))
  10525. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10526. (run-hooks 'org-after-refile-insert-hook))))
  10527. (unless org-refile-keep
  10528. (if regionp
  10529. (delete-region (point) (+ (point) (- region-end region-start)))
  10530. (delete-region
  10531. (and (org-back-to-heading t) (point))
  10532. (min (buffer-size) (org-end-of-subtree t t) (point)))))
  10533. (when (featurep 'org-inlinetask)
  10534. (org-inlinetask-remove-END-maybe))
  10535. (setq org-markers-to-move nil)
  10536. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10537. (defun org-refile-goto-last-stored ()
  10538. "Go to the location where the last refile was stored."
  10539. (interactive)
  10540. (bookmark-jump "org-refile-last-stored")
  10541. (message "This is the location of the last refile"))
  10542. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10543. no-exclude)
  10544. "Prompt the user for a refile location, using PROMPT.
  10545. PROMPT should not be suffixed with a colon and a space, because
  10546. this function appends the default value from
  10547. `org-refile-history' automatically, if that is not empty.
  10548. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10549. this is used for the GOTO interface."
  10550. (let ((org-refile-targets org-refile-targets)
  10551. (org-refile-use-outline-path org-refile-use-outline-path)
  10552. excluded-entries)
  10553. (when (and (derived-mode-p 'org-mode)
  10554. (not org-refile-use-cache)
  10555. (not no-exclude))
  10556. (org-map-tree
  10557. (lambda()
  10558. (setq excluded-entries
  10559. (append excluded-entries (list (org-get-heading t t)))))))
  10560. (setq org-refile-target-table
  10561. (org-refile-get-targets default-buffer excluded-entries)))
  10562. (unless org-refile-target-table
  10563. (user-error "No refile targets"))
  10564. (let* ((cbuf (current-buffer))
  10565. (partial-completion-mode nil)
  10566. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10567. (cfunc (if (and org-refile-use-outline-path
  10568. org-outline-path-complete-in-steps)
  10569. 'org-olpath-completing-read
  10570. 'org-icompleting-read))
  10571. (extra (if org-refile-use-outline-path "/" ""))
  10572. (cbnex (concat (buffer-name) extra))
  10573. (filename (and cfn (expand-file-name cfn)))
  10574. (tbl (mapcar
  10575. (lambda (x)
  10576. (if (and (not (member org-refile-use-outline-path
  10577. '(file full-file-path)))
  10578. (not (equal filename (nth 1 x))))
  10579. (cons (concat (car x) extra " ("
  10580. (file-name-nondirectory (nth 1 x)) ")")
  10581. (cdr x))
  10582. (cons (concat (car x) extra) (cdr x))))
  10583. org-refile-target-table))
  10584. (completion-ignore-case t)
  10585. cdef
  10586. (prompt (concat prompt
  10587. (or (and (car org-refile-history)
  10588. (concat " (default " (car org-refile-history) ")"))
  10589. (and (assoc cbnex tbl) (setq cdef cbnex)
  10590. (concat " (default " cbnex ")"))) ": "))
  10591. pa answ parent-target child parent old-hist)
  10592. (setq old-hist org-refile-history)
  10593. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10594. nil 'org-refile-history (or cdef (car org-refile-history))))
  10595. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10596. (org-refile-check-position pa)
  10597. (if pa
  10598. (progn
  10599. (when (or (not org-refile-history)
  10600. (not (eq old-hist org-refile-history))
  10601. (not (equal (car pa) (car org-refile-history))))
  10602. (setq org-refile-history
  10603. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10604. org-refile-history
  10605. (cdr org-refile-history))))
  10606. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10607. (pop org-refile-history)))
  10608. pa)
  10609. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10610. (progn
  10611. (setq parent (match-string 1 answ)
  10612. child (match-string 2 answ))
  10613. (setq parent-target (or (assoc parent tbl)
  10614. (assoc (concat parent "/") tbl)))
  10615. (when (and parent-target
  10616. (or (eq new-nodes t)
  10617. (and (eq new-nodes 'confirm)
  10618. (y-or-n-p (format "Create new node \"%s\"? "
  10619. child)))))
  10620. (org-refile-new-child parent-target child)))
  10621. (user-error "Invalid target location")))))
  10622. (declare-function org-string-nw-p "org-macs" (s))
  10623. (defun org-refile-check-position (refile-pointer)
  10624. "Check if the refile pointer matches the headline to which it points."
  10625. (let* ((file (nth 1 refile-pointer))
  10626. (re (nth 2 refile-pointer))
  10627. (pos (nth 3 refile-pointer))
  10628. buffer)
  10629. (if (and (not (markerp pos)) (not file))
  10630. (user-error "Please save the buffer to a file before refiling")
  10631. (when (org-string-nw-p re)
  10632. (setq buffer (if (markerp pos)
  10633. (marker-buffer pos)
  10634. (or (find-buffer-visiting file)
  10635. (find-file-noselect file))))
  10636. (with-current-buffer buffer
  10637. (save-excursion
  10638. (save-restriction
  10639. (widen)
  10640. (goto-char pos)
  10641. (beginning-of-line 1)
  10642. (unless (org-looking-at-p re)
  10643. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10644. (defun org-refile-new-child (parent-target child)
  10645. "Use refile target PARENT-TARGET to add new CHILD below it."
  10646. (unless parent-target
  10647. (error "Cannot find parent for new node"))
  10648. (let ((file (nth 1 parent-target))
  10649. (pos (nth 3 parent-target))
  10650. level)
  10651. (with-current-buffer (or (find-buffer-visiting file)
  10652. (find-file-noselect file))
  10653. (save-excursion
  10654. (save-restriction
  10655. (widen)
  10656. (if pos
  10657. (goto-char pos)
  10658. (goto-char (point-max))
  10659. (if (not (bolp)) (newline)))
  10660. (when (looking-at org-outline-regexp)
  10661. (setq level (funcall outline-level))
  10662. (org-end-of-subtree t t))
  10663. (org-back-over-empty-lines)
  10664. (insert "\n" (make-string
  10665. (if pos (org-get-valid-level level 1) 1) ?*)
  10666. " " child "\n")
  10667. (beginning-of-line 0)
  10668. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10669. (defun org-olpath-completing-read (prompt collection &rest args)
  10670. "Read an outline path like a file name."
  10671. (let ((thetable collection)
  10672. (org-completion-use-ido nil) ; does not work with ido.
  10673. (org-completion-use-iswitchb nil)) ; or iswitchb
  10674. (apply
  10675. 'org-icompleting-read prompt
  10676. (lambda (string predicate &optional flag)
  10677. (let (rtn r f (l (length string)))
  10678. (cond
  10679. ((eq flag nil)
  10680. ;; try completion
  10681. (try-completion string thetable))
  10682. ((eq flag t)
  10683. ;; all-completions
  10684. (setq rtn (all-completions string thetable predicate))
  10685. (mapcar
  10686. (lambda (x)
  10687. (setq r (substring x l))
  10688. (if (string-match " ([^)]*)$" x)
  10689. (setq f (match-string 0 x))
  10690. (setq f ""))
  10691. (if (string-match "/" r)
  10692. (concat string (substring r 0 (match-end 0)) f)
  10693. x))
  10694. rtn))
  10695. ((eq flag 'lambda)
  10696. ;; exact match?
  10697. (assoc string thetable)))))
  10698. args)))
  10699. ;;;; Dynamic blocks
  10700. (defun org-find-dblock (name)
  10701. "Find the first dynamic block with name NAME in the buffer.
  10702. If not found, stay at current position and return nil."
  10703. (let ((case-fold-search t) pos)
  10704. (save-excursion
  10705. (goto-char (point-min))
  10706. (setq pos (and (re-search-forward
  10707. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10708. (match-beginning 0))))
  10709. (if pos (goto-char pos))
  10710. pos))
  10711. (defconst org-dblock-start-re
  10712. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10713. "Matches the start line of a dynamic block, with parameters.")
  10714. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10715. "Matches the end of a dynamic block.")
  10716. (defun org-create-dblock (plist)
  10717. "Create a dynamic block section, with parameters taken from PLIST.
  10718. PLIST must contain a :name entry which is used as name of the block."
  10719. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10720. (end-of-line 1)
  10721. (newline))
  10722. (let ((col (current-column))
  10723. (name (plist-get plist :name)))
  10724. (insert "#+BEGIN: " name)
  10725. (while plist
  10726. (if (eq (car plist) :name)
  10727. (setq plist (cddr plist))
  10728. (insert " " (prin1-to-string (pop plist)))))
  10729. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10730. (beginning-of-line -2)))
  10731. (defun org-prepare-dblock ()
  10732. "Prepare dynamic block for refresh.
  10733. This empties the block, puts the cursor at the insert position and returns
  10734. the property list including an extra property :name with the block name."
  10735. (unless (looking-at org-dblock-start-re)
  10736. (user-error "Not at a dynamic block"))
  10737. (let* ((begdel (1+ (match-end 0)))
  10738. (name (org-no-properties (match-string 1)))
  10739. (params (append (list :name name)
  10740. (read (concat "(" (match-string 3) ")")))))
  10741. (save-excursion
  10742. (beginning-of-line 1)
  10743. (skip-chars-forward " \t")
  10744. (setq params (plist-put params :indentation-column (current-column))))
  10745. (unless (re-search-forward org-dblock-end-re nil t)
  10746. (error "Dynamic block not terminated"))
  10747. (setq params
  10748. (append params
  10749. (list :content (buffer-substring
  10750. begdel (match-beginning 0)))))
  10751. (delete-region begdel (match-beginning 0))
  10752. (goto-char begdel)
  10753. (open-line 1)
  10754. params))
  10755. (defun org-map-dblocks (&optional command)
  10756. "Apply COMMAND to all dynamic blocks in the current buffer.
  10757. If COMMAND is not given, use `org-update-dblock'."
  10758. (let ((cmd (or command 'org-update-dblock)))
  10759. (save-excursion
  10760. (goto-char (point-min))
  10761. (while (re-search-forward org-dblock-start-re nil t)
  10762. (goto-char (match-beginning 0))
  10763. (save-excursion
  10764. (condition-case nil
  10765. (funcall cmd)
  10766. (error (message "Error during update of dynamic block"))))
  10767. (unless (re-search-forward org-dblock-end-re nil t)
  10768. (error "Dynamic block not terminated"))))))
  10769. (defun org-dblock-update (&optional arg)
  10770. "User command for updating dynamic blocks.
  10771. Update the dynamic block at point. With prefix ARG, update all dynamic
  10772. blocks in the buffer."
  10773. (interactive "P")
  10774. (if arg
  10775. (org-update-all-dblocks)
  10776. (or (looking-at org-dblock-start-re)
  10777. (org-beginning-of-dblock))
  10778. (org-update-dblock)))
  10779. (defun org-update-dblock ()
  10780. "Update the dynamic block at point.
  10781. This means to empty the block, parse for parameters and then call
  10782. the correct writing function."
  10783. (interactive)
  10784. (save-window-excursion
  10785. (let* ((pos (point))
  10786. (line (org-current-line))
  10787. (params (org-prepare-dblock))
  10788. (name (plist-get params :name))
  10789. (indent (plist-get params :indentation-column))
  10790. (cmd (intern (concat "org-dblock-write:" name))))
  10791. (message "Updating dynamic block `%s' at line %d..." name line)
  10792. (funcall cmd params)
  10793. (message "Updating dynamic block `%s' at line %d...done" name line)
  10794. (goto-char pos)
  10795. (when (and indent (> indent 0))
  10796. (setq indent (make-string indent ?\ ))
  10797. (save-excursion
  10798. (org-beginning-of-dblock)
  10799. (forward-line 1)
  10800. (while (not (looking-at org-dblock-end-re))
  10801. (insert indent)
  10802. (beginning-of-line 2))
  10803. (when (looking-at org-dblock-end-re)
  10804. (and (looking-at "[ \t]+")
  10805. (replace-match ""))
  10806. (insert indent)))))))
  10807. (defun org-beginning-of-dblock ()
  10808. "Find the beginning of the dynamic block at point.
  10809. Error if there is no such block at point."
  10810. (let ((pos (point))
  10811. beg)
  10812. (end-of-line 1)
  10813. (if (and (re-search-backward org-dblock-start-re nil t)
  10814. (setq beg (match-beginning 0))
  10815. (re-search-forward org-dblock-end-re nil t)
  10816. (> (match-end 0) pos))
  10817. (goto-char beg)
  10818. (goto-char pos)
  10819. (error "Not in a dynamic block"))))
  10820. (defun org-update-all-dblocks ()
  10821. "Update all dynamic blocks in the buffer.
  10822. This function can be used in a hook."
  10823. (interactive)
  10824. (when (derived-mode-p 'org-mode)
  10825. (org-map-dblocks 'org-update-dblock)))
  10826. ;;;; Completion
  10827. (defun org-get-export-keywords ()
  10828. "Return a list of all currently understood export keywords.
  10829. Export keywords include options, block names, attributes and
  10830. keywords relative to each registered export back-end."
  10831. (delq nil
  10832. (let (keywords)
  10833. (mapc
  10834. (lambda (back-end)
  10835. (let ((props (cdr back-end)))
  10836. ;; Back-end name (for keywords, like #+LATEX:)
  10837. (push (upcase (symbol-name (car back-end))) keywords)
  10838. ;; Back-end options.
  10839. (mapc (lambda (option) (push (cadr option) keywords))
  10840. (plist-get (cdr back-end) :options-alist))))
  10841. (org-bound-and-true-p org-export-registered-backends))
  10842. keywords)))
  10843. (defconst org-options-keywords
  10844. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10845. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10846. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10847. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10848. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10849. (defcustom org-structure-template-alist
  10850. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC" "<src lang=\"?\">\n\n</src>")
  10851. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE" "<example>\n?\n</example>")
  10852. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE" "<quote>\n?\n</quote>")
  10853. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE" "<verse>\n?\n</verse>")
  10854. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM" "<verbatim>\n?\n</verbatim>")
  10855. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER" "<center>\n?\n</center>")
  10856. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10857. "<literal style=\"latex\">\n?\n</literal>")
  10858. ("L" "#+LaTeX: " "<literal style=\"latex\">?</literal>")
  10859. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10860. "<literal style=\"html\">\n?\n</literal>")
  10861. ("H" "#+HTML: " "<literal style=\"html\">?</literal>")
  10862. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII" "")
  10863. ("A" "#+ASCII: " "")
  10864. ("i" "#+INDEX: ?" "#+INDEX: ?")
  10865. ("I" "#+INCLUDE: %file ?"
  10866. "<include file=%file markup=\"?\">"))
  10867. "Structure completion elements.
  10868. This is a list of abbreviation keys and values. The value gets inserted
  10869. if you type `<' followed by the key and then press the completion key,
  10870. usually `M-TAB'. %file will be replaced by a file name after prompting
  10871. for the file using completion. The cursor will be placed at the position
  10872. of the `?` in the template.
  10873. There are two templates for each key, the first uses the original Org syntax,
  10874. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10875. the default when the /org-mtags.el/ module has been loaded. See also the
  10876. variable `org-mtags-prefer-muse-templates'."
  10877. :group 'org-completion
  10878. :type '(repeat
  10879. (list
  10880. (string :tag "Key")
  10881. (string :tag "Template")
  10882. (string :tag "Muse Template"))))
  10883. (defun org-try-structure-completion ()
  10884. "Try to complete a structure template before point.
  10885. This looks for strings like \"<e\" on an otherwise empty line and
  10886. expands them."
  10887. (let ((l (buffer-substring (point-at-bol) (point)))
  10888. a)
  10889. (when (and (looking-at "[ \t]*$")
  10890. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10891. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10892. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10893. (match-beginning 1)) a)
  10894. t)))
  10895. (defun org-complete-expand-structure-template (start cell)
  10896. "Expand a structure template."
  10897. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10898. (rpl (nth (if musep 2 1) cell))
  10899. (ind ""))
  10900. (delete-region start (point))
  10901. (when (string-match "\\`#\\+" rpl)
  10902. (cond
  10903. ((bolp))
  10904. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10905. (setq ind (buffer-substring (point-at-bol) (point))))
  10906. (t (newline))))
  10907. (setq start (point))
  10908. (if (string-match "%file" rpl)
  10909. (setq rpl (replace-match
  10910. (concat
  10911. "\""
  10912. (save-match-data
  10913. (abbreviate-file-name (read-file-name "Include file: ")))
  10914. "\"")
  10915. t t rpl)))
  10916. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10917. (concat "\n" ind)))
  10918. (insert rpl)
  10919. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10920. ;;;; TODO, DEADLINE, Comments
  10921. (defun org-toggle-comment ()
  10922. "Change the COMMENT state of an entry."
  10923. (interactive)
  10924. (save-excursion
  10925. (org-back-to-heading)
  10926. (let (case-fold-search)
  10927. (cond
  10928. ((looking-at (format org-heading-keyword-regexp-format
  10929. org-comment-string))
  10930. (goto-char (match-end 1))
  10931. (looking-at (concat " +" org-comment-string))
  10932. (replace-match "" t t)
  10933. (when (eolp) (insert " ")))
  10934. ((looking-at org-outline-regexp)
  10935. (goto-char (match-end 0))
  10936. (insert org-comment-string " "))))))
  10937. (defvar org-last-todo-state-is-todo nil
  10938. "This is non-nil when the last TODO state change led to a TODO state.
  10939. If the last change removed the TODO tag or switched to DONE, then
  10940. this is nil.")
  10941. (defvar org-setting-tags nil) ; dynamically skipped
  10942. (defvar org-todo-setup-filter-hook nil
  10943. "Hook for functions that pre-filter todo specs.
  10944. Each function takes a todo spec and returns either nil or the spec
  10945. transformed into canonical form." )
  10946. (defvar org-todo-get-default-hook nil
  10947. "Hook for functions that get a default item for todo.
  10948. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10949. nil or a string to be used for the todo mark." )
  10950. (defvar org-agenda-headline-snapshot-before-repeat)
  10951. (defun org-current-effective-time ()
  10952. "Return current time adjusted for `org-extend-today-until' variable."
  10953. (let* ((ct (org-current-time))
  10954. (dct (decode-time ct))
  10955. (ct1
  10956. (cond
  10957. (org-use-last-clock-out-time-as-effective-time
  10958. (or (org-clock-get-last-clock-out-time) ct))
  10959. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10960. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10961. (t ct))))
  10962. ct1))
  10963. (defun org-todo-yesterday (&optional arg)
  10964. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10965. (interactive "P")
  10966. (if (eq major-mode 'org-agenda-mode)
  10967. (apply 'org-agenda-todo-yesterday arg)
  10968. (let* ((hour (third (decode-time
  10969. (org-current-time))))
  10970. (org-extend-today-until (1+ hour)))
  10971. (org-todo arg))))
  10972. (defvar org-block-entry-blocking ""
  10973. "First entry preventing the TODO state change.")
  10974. (defun org-todo (&optional arg)
  10975. "Change the TODO state of an item.
  10976. The state of an item is given by a keyword at the start of the heading,
  10977. like
  10978. *** TODO Write paper
  10979. *** DONE Call mom
  10980. The different keywords are specified in the variable `org-todo-keywords'.
  10981. By default the available states are \"TODO\" and \"DONE\".
  10982. So for this example: when the item starts with TODO, it is changed to DONE.
  10983. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10984. DONE are present, add TODO at the beginning of the heading.
  10985. With \\[universal-argument] prefix arg, use completion to determine the new \
  10986. state.
  10987. With numeric prefix arg, switch to that state.
  10988. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10989. keywords (nextset).
  10990. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10991. With a numeric prefix arg of 0, inhibit note taking for the change.
  10992. For calling through lisp, arg is also interpreted in the following way:
  10993. 'none -> empty state
  10994. \"\"(empty string) -> switch to empty state
  10995. 'done -> switch to DONE
  10996. 'nextset -> switch to the next set of keywords
  10997. 'previousset -> switch to the previous set of keywords
  10998. \"WAITING\" -> switch to the specified keyword, but only if it
  10999. really is a member of `org-todo-keywords'."
  11000. (interactive "P")
  11001. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11002. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11003. 'region-start-level 'region))
  11004. org-loop-over-headlines-in-active-region)
  11005. (org-map-entries
  11006. `(org-todo ,arg)
  11007. org-loop-over-headlines-in-active-region
  11008. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11009. (if (equal arg '(16)) (setq arg 'nextset))
  11010. (let ((org-blocker-hook org-blocker-hook)
  11011. commentp
  11012. case-fold-search)
  11013. (when (equal arg '(64))
  11014. (setq arg nil org-blocker-hook nil))
  11015. (when (and org-blocker-hook
  11016. (or org-inhibit-blocking
  11017. (org-entry-get nil "NOBLOCKING")))
  11018. (setq org-blocker-hook nil))
  11019. (save-excursion
  11020. (catch 'exit
  11021. (org-back-to-heading t)
  11022. (when (looking-at (concat "^\\*+ " org-comment-string))
  11023. (org-toggle-comment)
  11024. (setq commentp t))
  11025. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11026. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11027. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11028. (let* ((match-data (match-data))
  11029. (startpos (point-at-bol))
  11030. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11031. (org-log-done org-log-done)
  11032. (org-log-repeat org-log-repeat)
  11033. (org-todo-log-states org-todo-log-states)
  11034. (org-inhibit-logging
  11035. (if (equal arg 0)
  11036. (progn (setq arg nil) 'note) org-inhibit-logging))
  11037. (this (match-string 1))
  11038. (hl-pos (match-beginning 0))
  11039. (head (org-get-todo-sequence-head this))
  11040. (ass (assoc head org-todo-kwd-alist))
  11041. (interpret (nth 1 ass))
  11042. (done-word (nth 3 ass))
  11043. (final-done-word (nth 4 ass))
  11044. (org-last-state (or this ""))
  11045. (completion-ignore-case t)
  11046. (member (member this org-todo-keywords-1))
  11047. (tail (cdr member))
  11048. (org-state (cond
  11049. ((and org-todo-key-trigger
  11050. (or (and (equal arg '(4))
  11051. (eq org-use-fast-todo-selection 'prefix))
  11052. (and (not arg) org-use-fast-todo-selection
  11053. (not (eq org-use-fast-todo-selection
  11054. 'prefix)))))
  11055. ;; Use fast selection
  11056. (org-fast-todo-selection))
  11057. ((and (equal arg '(4))
  11058. (or (not org-use-fast-todo-selection)
  11059. (not org-todo-key-trigger)))
  11060. ;; Read a state with completion
  11061. (org-icompleting-read
  11062. "State: " (mapcar 'list org-todo-keywords-1)
  11063. nil t))
  11064. ((eq arg 'right)
  11065. (if this
  11066. (if tail (car tail) nil)
  11067. (car org-todo-keywords-1)))
  11068. ((eq arg 'left)
  11069. (if (equal member org-todo-keywords-1)
  11070. nil
  11071. (if this
  11072. (nth (- (length org-todo-keywords-1)
  11073. (length tail) 2)
  11074. org-todo-keywords-1)
  11075. (org-last org-todo-keywords-1))))
  11076. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11077. (setq arg nil))) ; hack to fall back to cycling
  11078. (arg
  11079. ;; user or caller requests a specific state
  11080. (cond
  11081. ((equal arg "") nil)
  11082. ((eq arg 'none) nil)
  11083. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11084. ((eq arg 'nextset)
  11085. (or (car (cdr (member head org-todo-heads)))
  11086. (car org-todo-heads)))
  11087. ((eq arg 'previousset)
  11088. (let ((org-todo-heads (reverse org-todo-heads)))
  11089. (or (car (cdr (member head org-todo-heads)))
  11090. (car org-todo-heads))))
  11091. ((car (member arg org-todo-keywords-1)))
  11092. ((stringp arg)
  11093. (user-error "State `%s' not valid in this file" arg))
  11094. ((nth (1- (prefix-numeric-value arg))
  11095. org-todo-keywords-1))))
  11096. ((null member) (or head (car org-todo-keywords-1)))
  11097. ((equal this final-done-word) nil) ;; -> make empty
  11098. ((null tail) nil) ;; -> first entry
  11099. ((memq interpret '(type priority))
  11100. (if (eq this-command last-command)
  11101. (car tail)
  11102. (if (> (length tail) 0)
  11103. (or done-word (car org-done-keywords))
  11104. nil)))
  11105. (t
  11106. (car tail))))
  11107. (org-state (or
  11108. (run-hook-with-args-until-success
  11109. 'org-todo-get-default-hook org-state org-last-state)
  11110. org-state))
  11111. (next (if org-state (concat " " org-state " ") " "))
  11112. (change-plist (list :type 'todo-state-change :from this :to org-state
  11113. :position startpos))
  11114. dolog now-done-p)
  11115. (when org-blocker-hook
  11116. (setq org-last-todo-state-is-todo
  11117. (not (member this org-done-keywords)))
  11118. (unless (save-excursion
  11119. (save-match-data
  11120. (org-with-wide-buffer
  11121. (run-hook-with-args-until-failure
  11122. 'org-blocker-hook change-plist))))
  11123. (if (org-called-interactively-p 'interactive)
  11124. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11125. this org-state org-block-entry-blocking)
  11126. ;; fail silently
  11127. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11128. this org-state org-block-entry-blocking)
  11129. (throw 'exit nil))))
  11130. (store-match-data match-data)
  11131. (replace-match next t t)
  11132. (unless (pos-visible-in-window-p hl-pos)
  11133. (message "TODO state changed to %s" (org-trim next)))
  11134. (unless head
  11135. (setq head (org-get-todo-sequence-head org-state)
  11136. ass (assoc head org-todo-kwd-alist)
  11137. interpret (nth 1 ass)
  11138. done-word (nth 3 ass)
  11139. final-done-word (nth 4 ass)))
  11140. (when (memq arg '(nextset previousset))
  11141. (message "Keyword-Set %d/%d: %s"
  11142. (- (length org-todo-sets) -1
  11143. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11144. (length org-todo-sets)
  11145. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11146. (setq org-last-todo-state-is-todo
  11147. (not (member org-state org-done-keywords)))
  11148. (setq now-done-p (and (member org-state org-done-keywords)
  11149. (not (member this org-done-keywords))))
  11150. (and logging (org-local-logging logging))
  11151. (when (and (or org-todo-log-states org-log-done)
  11152. (not (eq org-inhibit-logging t))
  11153. (not (memq arg '(nextset previousset))))
  11154. ;; we need to look at recording a time and note
  11155. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11156. (nth 2 (assoc this org-todo-log-states))))
  11157. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11158. (setq dolog 'time))
  11159. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11160. (and org-state
  11161. (member org-state org-not-done-keywords)
  11162. (not (member this org-not-done-keywords))))
  11163. ;; This is now a todo state and was not one before
  11164. ;; If there was a CLOSED time stamp, get rid of it.
  11165. (org-add-planning-info nil nil 'closed))
  11166. (when (and now-done-p org-log-done)
  11167. ;; It is now done, and it was not done before
  11168. (org-add-planning-info 'closed (org-current-effective-time))
  11169. (if (and (not dolog) (eq 'note org-log-done))
  11170. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11171. (when (and org-state dolog)
  11172. ;; This is a non-nil state, and we need to log it
  11173. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11174. ;; Fixup tag positioning
  11175. (org-todo-trigger-tag-changes org-state)
  11176. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11177. (when org-provide-todo-statistics
  11178. (org-update-parent-todo-statistics))
  11179. (run-hooks 'org-after-todo-state-change-hook)
  11180. (if (and arg (not (member org-state org-done-keywords)))
  11181. (setq head (org-get-todo-sequence-head org-state)))
  11182. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11183. ;; Do we need to trigger a repeat?
  11184. (when now-done-p
  11185. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11186. ;; This is for the agenda, take a snapshot of the headline.
  11187. (save-match-data
  11188. (setq org-agenda-headline-snapshot-before-repeat
  11189. (org-get-heading))))
  11190. (org-auto-repeat-maybe org-state))
  11191. ;; Fixup cursor location if close to the keyword
  11192. (if (and (outline-on-heading-p)
  11193. (not (bolp))
  11194. (save-excursion (beginning-of-line 1)
  11195. (looking-at org-todo-line-regexp))
  11196. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11197. (progn
  11198. (goto-char (or (match-end 2) (match-end 1)))
  11199. (and (looking-at " ") (just-one-space))))
  11200. (when org-trigger-hook
  11201. (save-excursion
  11202. (run-hook-with-args 'org-trigger-hook change-plist)))
  11203. (when commentp (org-toggle-comment))))))))
  11204. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11205. "Block turning an entry into a TODO, using the hierarchy.
  11206. This checks whether the current task should be blocked from state
  11207. changes. Such blocking occurs when:
  11208. 1. The task has children which are not all in a completed state.
  11209. 2. A task has a parent with the property :ORDERED:, and there
  11210. are siblings prior to the current task with incomplete
  11211. status.
  11212. 3. The parent of the task is blocked because it has siblings that should
  11213. be done first, or is child of a block grandparent TODO entry."
  11214. (if (not org-enforce-todo-dependencies)
  11215. t ; if locally turned off don't block
  11216. (catch 'dont-block
  11217. ;; If this is not a todo state change, or if this entry is already DONE,
  11218. ;; do not block
  11219. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11220. (member (plist-get change-plist :from)
  11221. (cons 'done org-done-keywords))
  11222. (member (plist-get change-plist :to)
  11223. (cons 'todo org-not-done-keywords))
  11224. (not (plist-get change-plist :to)))
  11225. (throw 'dont-block t))
  11226. ;; If this task has children, and any are undone, it's blocked
  11227. (save-excursion
  11228. (org-back-to-heading t)
  11229. (let ((this-level (funcall outline-level)))
  11230. (outline-next-heading)
  11231. (let ((child-level (funcall outline-level)))
  11232. (while (and (not (eobp))
  11233. (> child-level this-level))
  11234. ;; this todo has children, check whether they are all
  11235. ;; completed
  11236. (if (and (not (org-entry-is-done-p))
  11237. (org-entry-is-todo-p))
  11238. (progn (setq org-block-entry-blocking (org-get-heading))
  11239. (throw 'dont-block nil)))
  11240. (outline-next-heading)
  11241. (setq child-level (funcall outline-level))))))
  11242. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11243. ;; any previous siblings are undone, it's blocked
  11244. (save-excursion
  11245. (org-back-to-heading t)
  11246. (let* ((pos (point))
  11247. (parent-pos (and (org-up-heading-safe) (point))))
  11248. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11249. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11250. (forward-line 1)
  11251. (re-search-forward org-not-done-heading-regexp pos t))
  11252. (setq org-block-entry-blocking (match-string 0))
  11253. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11254. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11255. (while t
  11256. (goto-char parent-pos)
  11257. (if (not (looking-at org-not-done-heading-regexp))
  11258. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11259. (setq pos (point))
  11260. (setq parent-pos (and (org-up-heading-safe) (point)))
  11261. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11262. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11263. (forward-line 1)
  11264. (re-search-forward org-not-done-heading-regexp pos t)
  11265. (setq org-block-entry-blocking (org-get-heading)))
  11266. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11267. (defcustom org-track-ordered-property-with-tag nil
  11268. "Should the ORDERED property also be shown as a tag?
  11269. The ORDERED property decides if an entry should require subtasks to be
  11270. completed in sequence. Since a property is not very visible, setting
  11271. this option means that toggling the ORDERED property with the command
  11272. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11273. not relevant for the behavior, but it makes things more visible.
  11274. Note that toggling the tag with tags commands will not change the property
  11275. and therefore not influence behavior!
  11276. This can be t, meaning the tag ORDERED should be used, It can also be a
  11277. string to select a different tag for this task."
  11278. :group 'org-todo
  11279. :type '(choice
  11280. (const :tag "No tracking" nil)
  11281. (const :tag "Track with ORDERED tag" t)
  11282. (string :tag "Use other tag")))
  11283. (defun org-toggle-ordered-property ()
  11284. "Toggle the ORDERED property of the current entry.
  11285. For better visibility, you can track the value of this property with a tag.
  11286. See variable `org-track-ordered-property-with-tag'."
  11287. (interactive)
  11288. (let* ((t1 org-track-ordered-property-with-tag)
  11289. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11290. (save-excursion
  11291. (org-back-to-heading)
  11292. (if (org-entry-get nil "ORDERED")
  11293. (progn
  11294. (org-delete-property "ORDERED" "PROPERTIES")
  11295. (and tag (org-toggle-tag tag 'off))
  11296. (message "Subtasks can be completed in arbitrary order"))
  11297. (org-entry-put nil "ORDERED" "t")
  11298. (and tag (org-toggle-tag tag 'on))
  11299. (message "Subtasks must be completed in sequence")))))
  11300. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11301. (defun org-block-todo-from-checkboxes (change-plist)
  11302. "Block turning an entry into a TODO, using checkboxes.
  11303. This checks whether the current task should be blocked from state
  11304. changes because there are unchecked boxes in this entry."
  11305. (if (not org-enforce-todo-checkbox-dependencies)
  11306. t ; if locally turned off don't block
  11307. (catch 'dont-block
  11308. ;; If this is not a todo state change, or if this entry is already DONE,
  11309. ;; do not block
  11310. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11311. (member (plist-get change-plist :from)
  11312. (cons 'done org-done-keywords))
  11313. (member (plist-get change-plist :to)
  11314. (cons 'todo org-not-done-keywords))
  11315. (not (plist-get change-plist :to)))
  11316. (throw 'dont-block t))
  11317. ;; If this task has checkboxes that are not checked, it's blocked
  11318. (save-excursion
  11319. (org-back-to-heading t)
  11320. (let ((beg (point)) end)
  11321. (outline-next-heading)
  11322. (setq end (point))
  11323. (goto-char beg)
  11324. (if (org-list-search-forward
  11325. (concat (org-item-beginning-re)
  11326. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11327. "\\[[- ]\\]")
  11328. end t)
  11329. (progn
  11330. (if (boundp 'org-blocked-by-checkboxes)
  11331. (setq org-blocked-by-checkboxes t))
  11332. (throw 'dont-block nil)))))
  11333. t))) ; do not block
  11334. (defun org-entry-blocked-p ()
  11335. "Is the current entry blocked?"
  11336. (org-with-silent-modifications
  11337. (if (org-entry-get nil "NOBLOCKING")
  11338. nil ;; Never block this entry
  11339. (not (run-hook-with-args-until-failure
  11340. 'org-blocker-hook
  11341. (list :type 'todo-state-change
  11342. :position (point)
  11343. :from 'todo
  11344. :to 'done))))))
  11345. (defun org-update-statistics-cookies (all)
  11346. "Update the statistics cookie, either from TODO or from checkboxes.
  11347. This should be called with the cursor in a line with a statistics cookie."
  11348. (interactive "P")
  11349. (if all
  11350. (progn
  11351. (org-update-checkbox-count 'all)
  11352. (org-map-entries 'org-update-parent-todo-statistics))
  11353. (if (not (org-at-heading-p))
  11354. (org-update-checkbox-count)
  11355. (let ((pos (point-marker))
  11356. end l1 l2)
  11357. (ignore-errors (org-back-to-heading t))
  11358. (if (not (org-at-heading-p))
  11359. (org-update-checkbox-count)
  11360. (setq l1 (org-outline-level))
  11361. (setq end (save-excursion
  11362. (outline-next-heading)
  11363. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11364. (point)))
  11365. (if (and (save-excursion
  11366. (re-search-forward
  11367. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11368. (not (save-excursion (re-search-forward
  11369. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11370. (org-update-checkbox-count)
  11371. (if (and l2 (> l2 l1))
  11372. (progn
  11373. (goto-char end)
  11374. (org-update-parent-todo-statistics))
  11375. (goto-char pos)
  11376. (beginning-of-line 1)
  11377. (while (re-search-forward
  11378. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11379. (point-at-eol) t)
  11380. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11381. (goto-char pos)
  11382. (move-marker pos nil)))))
  11383. (defvar org-entry-property-inherited-from) ;; defined below
  11384. (defun org-update-parent-todo-statistics ()
  11385. "Update any statistics cookie in the parent of the current headline.
  11386. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11387. the entire subtree and this will travel up the hierarchy and update
  11388. statistics everywhere."
  11389. (let* ((prop (save-excursion (org-up-heading-safe)
  11390. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11391. (recursive (or (not org-hierarchical-todo-statistics)
  11392. (and prop (string-match "\\<recursive\\>" prop))))
  11393. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11394. 0))
  11395. (first t)
  11396. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11397. level ltoggle l1 new ndel
  11398. (cnt-all 0) (cnt-done 0) is-percent kwd
  11399. checkbox-beg ov ovs ove cookie-present)
  11400. (catch 'exit
  11401. (save-excursion
  11402. (beginning-of-line 1)
  11403. (setq ltoggle (funcall outline-level))
  11404. ;; Three situations are to consider:
  11405. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11406. ;; to the top-level ancestor on the headline;
  11407. ;; 2. If parent has "recursive" property, repeat up to the
  11408. ;; headline setting that property, taking inheritance into
  11409. ;; account;
  11410. ;; 3. Else, move up to direct parent and proceed only once.
  11411. (while (and (setq level (org-up-heading-safe))
  11412. (or recursive first)
  11413. (>= (point) lim))
  11414. (setq first nil cookie-present nil)
  11415. (unless (and level
  11416. (not (string-match
  11417. "\\<checkbox\\>"
  11418. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11419. "")))))
  11420. (throw 'exit nil))
  11421. (while (re-search-forward box-re (point-at-eol) t)
  11422. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11423. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11424. (save-match-data
  11425. (unless (outline-next-heading) (throw 'exit nil))
  11426. (while (and (looking-at org-complex-heading-regexp)
  11427. (> (setq l1 (length (match-string 1))) level))
  11428. (setq kwd (and (or recursive (= l1 ltoggle))
  11429. (match-string 2)))
  11430. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11431. (and (listp org-provide-todo-statistics)
  11432. (or (member kwd org-provide-todo-statistics)
  11433. (member kwd org-done-keywords))))
  11434. (setq cnt-all (1+ cnt-all))
  11435. (if (eq org-provide-todo-statistics t)
  11436. (and kwd (setq cnt-all (1+ cnt-all)))))
  11437. (and (member kwd org-done-keywords)
  11438. (setq cnt-done (1+ cnt-done)))
  11439. (outline-next-heading)))
  11440. (setq new
  11441. (if is-percent
  11442. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11443. (format "[%d/%d]" cnt-done cnt-all))
  11444. ndel (- (match-end 0) checkbox-beg))
  11445. ;; handle overlays when updating cookie from column view
  11446. (when (setq ov (car (overlays-at checkbox-beg)))
  11447. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11448. (delete-overlay ov))
  11449. (goto-char checkbox-beg)
  11450. (insert new)
  11451. (delete-region (point) (+ (point) ndel))
  11452. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11453. (when ov (move-overlay ov ovs ove)))
  11454. (when cookie-present
  11455. (run-hook-with-args 'org-after-todo-statistics-hook
  11456. cnt-done (- cnt-all cnt-done))))))
  11457. (run-hooks 'org-todo-statistics-hook)))
  11458. (defvar org-after-todo-statistics-hook nil
  11459. "Hook that is called after a TODO statistics cookie has been updated.
  11460. Each function is called with two arguments: the number of not-done entries
  11461. and the number of done entries.
  11462. For example, the following function, when added to this hook, will switch
  11463. an entry to DONE when all children are done, and back to TODO when new
  11464. entries are set to a TODO status. Note that this hook is only called
  11465. when there is a statistics cookie in the headline!
  11466. (defun org-summary-todo (n-done n-not-done)
  11467. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11468. (let (org-log-done org-log-states) ; turn off logging
  11469. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11470. ")
  11471. (defvar org-todo-statistics-hook nil
  11472. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11473. This hook runs even if there is no statistics cookie present, in which case
  11474. `org-after-todo-statistics-hook' would not run.")
  11475. (defun org-todo-trigger-tag-changes (state)
  11476. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11477. (let ((l org-todo-state-tags-triggers)
  11478. changes)
  11479. (when (or (not state) (equal state ""))
  11480. (setq changes (append changes (cdr (assoc "" l)))))
  11481. (when (and (stringp state) (> (length state) 0))
  11482. (setq changes (append changes (cdr (assoc state l)))))
  11483. (when (member state org-not-done-keywords)
  11484. (setq changes (append changes (cdr (assoc 'todo l)))))
  11485. (when (member state org-done-keywords)
  11486. (setq changes (append changes (cdr (assoc 'done l)))))
  11487. (dolist (c changes)
  11488. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11489. (defun org-local-logging (value)
  11490. "Get logging settings from a property VALUE."
  11491. (let* (words w a)
  11492. ;; directly set the variables, they are already local.
  11493. (setq org-log-done nil
  11494. org-log-repeat nil
  11495. org-todo-log-states nil)
  11496. (setq words (org-split-string value))
  11497. (while (setq w (pop words))
  11498. (cond
  11499. ((setq a (assoc w org-startup-options))
  11500. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11501. (set (nth 1 a) (nth 2 a))))
  11502. ((setq a (org-extract-log-state-settings w))
  11503. (and (member (car a) org-todo-keywords-1)
  11504. (push a org-todo-log-states)))))))
  11505. (defun org-get-todo-sequence-head (kwd)
  11506. "Return the head of the TODO sequence to which KWD belongs.
  11507. If KWD is not set, check if there is a text property remembering the
  11508. right sequence."
  11509. (let (p)
  11510. (cond
  11511. ((not kwd)
  11512. (or (get-text-property (point-at-bol) 'org-todo-head)
  11513. (progn
  11514. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11515. nil (point-at-eol)))
  11516. (get-text-property p 'org-todo-head))))
  11517. ((not (member kwd org-todo-keywords-1))
  11518. (car org-todo-keywords-1))
  11519. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11520. (defun org-fast-todo-selection ()
  11521. "Fast TODO keyword selection with single keys.
  11522. Returns the new TODO keyword, or nil if no state change should occur."
  11523. (let* ((fulltable org-todo-key-alist)
  11524. (done-keywords org-done-keywords) ;; needed for the faces.
  11525. (maxlen (apply 'max (mapcar
  11526. (lambda (x)
  11527. (if (stringp (car x)) (string-width (car x)) 0))
  11528. fulltable)))
  11529. (expert nil)
  11530. (fwidth (+ maxlen 3 1 3))
  11531. (ncol (/ (- (window-width) 4) fwidth))
  11532. tg cnt e c tbl
  11533. groups ingroup)
  11534. (save-excursion
  11535. (save-window-excursion
  11536. (if expert
  11537. (set-buffer (get-buffer-create " *Org todo*"))
  11538. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11539. (erase-buffer)
  11540. (org-set-local 'org-done-keywords done-keywords)
  11541. (setq tbl fulltable cnt 0)
  11542. (while (setq e (pop tbl))
  11543. (cond
  11544. ((equal e '(:startgroup))
  11545. (push '() groups) (setq ingroup t)
  11546. (when (not (= cnt 0))
  11547. (setq cnt 0)
  11548. (insert "\n"))
  11549. (insert "{ "))
  11550. ((equal e '(:endgroup))
  11551. (setq ingroup nil cnt 0)
  11552. (insert "}\n"))
  11553. ((equal e '(:newline))
  11554. (when (not (= cnt 0))
  11555. (setq cnt 0)
  11556. (insert "\n")
  11557. (setq e (car tbl))
  11558. (while (equal (car tbl) '(:newline))
  11559. (insert "\n")
  11560. (setq tbl (cdr tbl)))))
  11561. (t
  11562. (setq tg (car e) c (cdr e))
  11563. (if ingroup (push tg (car groups)))
  11564. (setq tg (org-add-props tg nil 'face
  11565. (org-get-todo-face tg)))
  11566. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11567. (insert "[" c "] " tg (make-string
  11568. (- fwidth 4 (length tg)) ?\ ))
  11569. (when (= (setq cnt (1+ cnt)) ncol)
  11570. (insert "\n")
  11571. (if ingroup (insert " "))
  11572. (setq cnt 0)))))
  11573. (insert "\n")
  11574. (goto-char (point-min))
  11575. (if (not expert) (org-fit-window-to-buffer))
  11576. (message "[a-z..]:Set [SPC]:clear")
  11577. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11578. (cond
  11579. ((or (= c ?\C-g)
  11580. (and (= c ?q) (not (rassoc c fulltable))))
  11581. (setq quit-flag t))
  11582. ((= c ?\ ) nil)
  11583. ((setq e (rassoc c fulltable) tg (car e))
  11584. tg)
  11585. (t (setq quit-flag t)))))))
  11586. (defun org-entry-is-todo-p ()
  11587. (member (org-get-todo-state) org-not-done-keywords))
  11588. (defun org-entry-is-done-p ()
  11589. (member (org-get-todo-state) org-done-keywords))
  11590. (defun org-get-todo-state ()
  11591. "Return the TODO keyword of the current subtree."
  11592. (save-excursion
  11593. (org-back-to-heading t)
  11594. (and (looking-at org-todo-line-regexp)
  11595. (match-end 2)
  11596. (match-string 2))))
  11597. (defun org-at-date-range-p (&optional inactive-ok)
  11598. "Is the cursor inside a date range?"
  11599. (interactive)
  11600. (save-excursion
  11601. (catch 'exit
  11602. (let ((pos (point)))
  11603. (skip-chars-backward "^[<\r\n")
  11604. (skip-chars-backward "<[")
  11605. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11606. (>= (match-end 0) pos)
  11607. (throw 'exit t))
  11608. (skip-chars-backward "^<[\r\n")
  11609. (skip-chars-backward "<[")
  11610. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11611. (>= (match-end 0) pos)
  11612. (throw 'exit t)))
  11613. nil)))
  11614. (defun org-get-repeat (&optional tagline)
  11615. "Check if there is a deadline/schedule with repeater in this entry."
  11616. (save-match-data
  11617. (save-excursion
  11618. (org-back-to-heading t)
  11619. (and (re-search-forward (if tagline
  11620. (concat tagline "\\s-*" org-repeat-re)
  11621. org-repeat-re)
  11622. (org-entry-end-position) t)
  11623. (match-string-no-properties 1)))))
  11624. (defvar org-last-changed-timestamp)
  11625. (defvar org-last-inserted-timestamp)
  11626. (defvar org-log-post-message)
  11627. (defvar org-log-note-purpose)
  11628. (defvar org-log-note-how)
  11629. (defvar org-log-note-extra)
  11630. (defun org-auto-repeat-maybe (done-word)
  11631. "Check if the current headline contains a repeated deadline/schedule.
  11632. If yes, set TODO state back to what it was and change the base date
  11633. of repeating deadline/scheduled time stamps to new date.
  11634. This function is run automatically after each state change to a DONE state."
  11635. ;; last-state is dynamically scoped into this function
  11636. (let* ((repeat (org-get-repeat))
  11637. (aa (assoc org-last-state org-todo-kwd-alist))
  11638. (interpret (nth 1 aa))
  11639. (head (nth 2 aa))
  11640. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11641. (msg "Entry repeats: ")
  11642. (org-log-done nil)
  11643. (org-todo-log-states nil)
  11644. re type n what ts time to-state)
  11645. (when repeat
  11646. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11647. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11648. org-todo-repeat-to-state))
  11649. (unless (and to-state (member to-state org-todo-keywords-1))
  11650. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11651. (org-todo to-state)
  11652. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11653. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11654. (org-time-stamp-format t t))))
  11655. (when org-log-repeat
  11656. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11657. (memq 'org-add-log-note post-command-hook))
  11658. ;; OK, we are already setup for some record
  11659. (if (eq org-log-repeat 'note)
  11660. ;; make sure we take a note, not only a time stamp
  11661. (setq org-log-note-how 'note))
  11662. ;; Set up for taking a record
  11663. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11664. org-last-state
  11665. 'findpos org-log-repeat)))
  11666. (org-back-to-heading t)
  11667. (org-add-planning-info nil nil 'closed)
  11668. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11669. org-deadline-time-regexp "\\)\\|\\("
  11670. org-ts-regexp "\\)"))
  11671. (while (re-search-forward
  11672. re (save-excursion (outline-next-heading) (point)) t)
  11673. (setq type (if (match-end 1) org-scheduled-string
  11674. (if (match-end 3) org-deadline-string "Plain:"))
  11675. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11676. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11677. (setq n (string-to-number (match-string 2 ts))
  11678. what (match-string 3 ts))
  11679. (if (equal what "w") (setq n (* n 7) what "d"))
  11680. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11681. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11682. ;; Preparation, see if we need to modify the start date for the change
  11683. (when (match-end 1)
  11684. (setq time (save-match-data (org-time-string-to-time ts)))
  11685. (cond
  11686. ((equal (match-string 1 ts) ".")
  11687. ;; Shift starting date to today
  11688. (org-timestamp-change
  11689. (- (org-today) (time-to-days time))
  11690. 'day))
  11691. ((equal (match-string 1 ts) "+")
  11692. (let ((nshiftmax 10) (nshift 0))
  11693. (while (or (= nshift 0)
  11694. (<= (time-to-days time)
  11695. (time-to-days (current-time))))
  11696. (when (= (incf nshift) nshiftmax)
  11697. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11698. (error "Abort")))
  11699. (org-timestamp-change n (cdr (assoc what whata)))
  11700. (org-at-timestamp-p t)
  11701. (setq ts (match-string 1))
  11702. (setq time (save-match-data (org-time-string-to-time ts)))))
  11703. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11704. ;; rematch, so that we have everything in place for the real shift
  11705. (org-at-timestamp-p t)
  11706. (setq ts (match-string 1))
  11707. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11708. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11709. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11710. (setq org-log-post-message msg)
  11711. (message "%s" msg))))
  11712. (defun org-show-todo-tree (arg)
  11713. "Make a compact tree which shows all headlines marked with TODO.
  11714. The tree will show the lines where the regexp matches, and all higher
  11715. headlines above the match.
  11716. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11717. With a numeric prefix N, construct a sparse tree for the Nth element
  11718. of `org-todo-keywords-1'."
  11719. (interactive "P")
  11720. (let ((case-fold-search nil)
  11721. (kwd-re
  11722. (cond ((null arg) org-not-done-regexp)
  11723. ((equal arg '(4))
  11724. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11725. (mapcar 'list org-todo-keywords-1))))
  11726. (concat "\\("
  11727. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11728. "\\)\\>")))
  11729. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11730. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11731. org-todo-keywords-1)))
  11732. (t (user-error "Invalid prefix argument: %s" arg)))))
  11733. (message "%d TODO entries found"
  11734. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11735. (defun org-deadline (arg &optional time)
  11736. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11737. With one universal prefix argument, remove any deadline from the item.
  11738. With two universal prefix arguments, prompt for a warning delay.
  11739. With argument TIME, set the deadline at the corresponding date. TIME
  11740. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11741. (interactive "P")
  11742. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11743. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11744. 'region-start-level 'region))
  11745. org-loop-over-headlines-in-active-region)
  11746. (org-map-entries
  11747. `(org-deadline ',arg ,time)
  11748. org-loop-over-headlines-in-active-region
  11749. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11750. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11751. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11752. (repeater (and old-date
  11753. (string-match
  11754. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11755. old-date)
  11756. (match-string 1 old-date))))
  11757. (cond
  11758. ((equal arg '(4))
  11759. (when (and old-date org-log-redeadline)
  11760. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11761. org-log-redeadline))
  11762. (org-remove-timestamp-with-keyword org-deadline-string)
  11763. (message "Item no longer has a deadline."))
  11764. ((equal arg '(16))
  11765. (save-excursion
  11766. (if (re-search-forward
  11767. org-deadline-time-regexp
  11768. (save-excursion (outline-next-heading) (point)) t)
  11769. (let* ((rpl0 (match-string 1))
  11770. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11771. (replace-match
  11772. (concat org-deadline-string
  11773. " <" rpl
  11774. (format " -%dd"
  11775. (abs
  11776. (- (time-to-days
  11777. (save-match-data
  11778. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11779. (time-to-days old-date-time))))
  11780. ">") t t))
  11781. (user-error "No deadline information to update"))))
  11782. (t
  11783. (org-add-planning-info 'deadline time 'closed)
  11784. (when (and old-date org-log-redeadline
  11785. (not (equal old-date
  11786. (substring org-last-inserted-timestamp 1 -1))))
  11787. (org-add-log-setup 'redeadline nil old-date 'findpos
  11788. org-log-redeadline))
  11789. (when repeater
  11790. (save-excursion
  11791. (org-back-to-heading t)
  11792. (when (re-search-forward (concat org-deadline-string " "
  11793. org-last-inserted-timestamp)
  11794. (save-excursion
  11795. (outline-next-heading) (point)) t)
  11796. (goto-char (1- (match-end 0)))
  11797. (insert " " repeater)
  11798. (setq org-last-inserted-timestamp
  11799. (concat (substring org-last-inserted-timestamp 0 -1)
  11800. " " repeater
  11801. (substring org-last-inserted-timestamp -1))))))
  11802. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11803. (defun org-schedule (arg &optional time)
  11804. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11805. With one universal prefix argument, remove any scheduling date from the item.
  11806. With two universal prefix arguments, prompt for a delay cookie.
  11807. With argument TIME, scheduled at the corresponding date. TIME can
  11808. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11809. (interactive "P")
  11810. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11811. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11812. 'region-start-level 'region))
  11813. org-loop-over-headlines-in-active-region)
  11814. (org-map-entries
  11815. `(org-schedule ',arg ,time)
  11816. org-loop-over-headlines-in-active-region
  11817. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11818. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11819. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11820. (repeater (and old-date
  11821. (string-match
  11822. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11823. old-date)
  11824. (match-string 1 old-date))))
  11825. (cond
  11826. ((equal arg '(4))
  11827. (progn
  11828. (when (and old-date org-log-reschedule)
  11829. (org-add-log-setup 'delschedule nil old-date 'findpos
  11830. org-log-reschedule))
  11831. (org-remove-timestamp-with-keyword org-scheduled-string)
  11832. (message "Item is no longer scheduled.")))
  11833. ((equal arg '(16))
  11834. (save-excursion
  11835. (if (re-search-forward
  11836. org-scheduled-time-regexp
  11837. (save-excursion (outline-next-heading) (point)) t)
  11838. (let* ((rpl0 (match-string 1))
  11839. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11840. (replace-match
  11841. (concat org-scheduled-string
  11842. " <" rpl
  11843. (format " -%dd"
  11844. (abs
  11845. (- (time-to-days
  11846. (save-match-data
  11847. (org-read-date nil t nil "Delay until" old-date-time)))
  11848. (time-to-days old-date-time))))
  11849. ">") t t))
  11850. (user-error "No scheduled information to update"))))
  11851. (t
  11852. (org-add-planning-info 'scheduled time 'closed)
  11853. (when (and old-date org-log-reschedule
  11854. (not (equal old-date
  11855. (substring org-last-inserted-timestamp 1 -1))))
  11856. (org-add-log-setup 'reschedule nil old-date 'findpos
  11857. org-log-reschedule))
  11858. (when repeater
  11859. (save-excursion
  11860. (org-back-to-heading t)
  11861. (when (re-search-forward (concat org-scheduled-string " "
  11862. org-last-inserted-timestamp)
  11863. (save-excursion
  11864. (outline-next-heading) (point)) t)
  11865. (goto-char (1- (match-end 0)))
  11866. (insert " " repeater)
  11867. (setq org-last-inserted-timestamp
  11868. (concat (substring org-last-inserted-timestamp 0 -1)
  11869. " " repeater
  11870. (substring org-last-inserted-timestamp -1))))))
  11871. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11872. (defun org-get-scheduled-time (pom &optional inherit)
  11873. "Get the scheduled time as a time tuple, of a format suitable
  11874. for calling org-schedule with, or if there is no scheduling,
  11875. returns nil."
  11876. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11877. (when time
  11878. (apply 'encode-time (org-parse-time-string time)))))
  11879. (defun org-get-deadline-time (pom &optional inherit)
  11880. "Get the deadline as a time tuple, of a format suitable for
  11881. calling org-deadline with, or if there is no scheduling, returns
  11882. nil."
  11883. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11884. (when time
  11885. (apply 'encode-time (org-parse-time-string time)))))
  11886. (defun org-remove-timestamp-with-keyword (keyword)
  11887. "Remove all time stamps with KEYWORD in the current entry."
  11888. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11889. beg)
  11890. (save-excursion
  11891. (org-back-to-heading t)
  11892. (setq beg (point))
  11893. (outline-next-heading)
  11894. (while (re-search-backward re beg t)
  11895. (replace-match "")
  11896. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11897. (equal (char-before) ?\ ))
  11898. (backward-delete-char 1)
  11899. (if (string-match "^[ \t]*$" (buffer-substring
  11900. (point-at-bol) (point-at-eol)))
  11901. (delete-region (point-at-bol)
  11902. (min (point-max) (1+ (point-at-eol))))))))))
  11903. (defun org-add-planning-info (what &optional time &rest remove)
  11904. "Insert new timestamp with keyword in the line directly after the headline.
  11905. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11906. If non is given, the user is prompted for a date.
  11907. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11908. be removed."
  11909. (interactive)
  11910. (let (org-time-was-given org-end-time-was-given ts
  11911. end default-time default-input)
  11912. (catch 'exit
  11913. (when (and (memq what '(scheduled deadline))
  11914. (or (not time)
  11915. (and (stringp time)
  11916. (string-match "^[-+]+[0-9]" time))))
  11917. ;; Try to get a default date/time from existing timestamp
  11918. (save-excursion
  11919. (org-back-to-heading t)
  11920. (setq end (save-excursion (outline-next-heading) (point)))
  11921. (when (re-search-forward (if (eq what 'scheduled)
  11922. org-scheduled-time-regexp
  11923. org-deadline-time-regexp)
  11924. end t)
  11925. (setq ts (match-string 1)
  11926. default-time
  11927. (apply 'encode-time (org-parse-time-string ts))
  11928. default-input (and ts (org-get-compact-tod ts))))))
  11929. (when what
  11930. (setq time
  11931. (if (stringp time)
  11932. ;; This is a string (relative or absolute), set proper date
  11933. (apply 'encode-time
  11934. (org-read-date-analyze
  11935. time default-time (decode-time default-time)))
  11936. ;; If necessary, get the time from the user
  11937. (or time (org-read-date nil 'to-time nil nil
  11938. default-time default-input)))))
  11939. (when (and org-insert-labeled-timestamps-at-point
  11940. (member what '(scheduled deadline)))
  11941. (insert
  11942. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11943. (org-insert-time-stamp time org-time-was-given
  11944. nil nil nil (list org-end-time-was-given))
  11945. (setq what nil))
  11946. (save-excursion
  11947. (save-restriction
  11948. (let (col list elt ts buffer-invisibility-spec)
  11949. (org-back-to-heading t)
  11950. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11951. (goto-char (match-end 1))
  11952. (setq col (current-column))
  11953. (goto-char (match-end 0))
  11954. (if (eobp) (insert "\n") (forward-char 1))
  11955. (when (and (not what)
  11956. (not (looking-at
  11957. (concat "[ \t]*"
  11958. org-keyword-time-not-clock-regexp))))
  11959. ;; Nothing to add, nothing to remove...... :-)
  11960. (throw 'exit nil))
  11961. (if (and (not (looking-at org-outline-regexp))
  11962. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11963. "[^\r\n]*"))
  11964. (not (equal (match-string 1) org-clock-string)))
  11965. (narrow-to-region (match-beginning 0) (match-end 0))
  11966. (insert-before-markers "\n")
  11967. (backward-char 1)
  11968. (narrow-to-region (point) (point))
  11969. (and org-adapt-indentation (org-indent-to-column col)))
  11970. ;; Check if we have to remove something.
  11971. (setq list (cons what remove))
  11972. (while list
  11973. (setq elt (pop list))
  11974. (when (or (and (eq elt 'scheduled)
  11975. (re-search-forward org-scheduled-time-regexp nil t))
  11976. (and (eq elt 'deadline)
  11977. (re-search-forward org-deadline-time-regexp nil t))
  11978. (and (eq elt 'closed)
  11979. (re-search-forward org-closed-time-regexp nil t)))
  11980. (replace-match "")
  11981. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11982. (and (looking-at "[ \t]+") (replace-match ""))
  11983. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11984. (when what
  11985. (insert
  11986. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11987. (cond ((eq what 'scheduled) org-scheduled-string)
  11988. ((eq what 'deadline) org-deadline-string)
  11989. ((eq what 'closed) org-closed-string))
  11990. " ")
  11991. (setq ts (org-insert-time-stamp
  11992. time
  11993. (or org-time-was-given
  11994. (and (eq what 'closed) org-log-done-with-time))
  11995. (eq what 'closed)
  11996. nil nil (list org-end-time-was-given)))
  11997. (insert
  11998. (if (not (or (bolp) (eq (char-before) ?\ )
  11999. (memq (char-after) '(32 10))
  12000. (eobp))) " " ""))
  12001. (end-of-line 1))
  12002. (goto-char (point-min))
  12003. (widen)
  12004. (if (and (looking-at "[ \t]*\n")
  12005. (equal (char-before) ?\n))
  12006. (delete-region (1- (point)) (point-at-eol)))
  12007. ts))))))
  12008. (defvar org-log-note-marker (make-marker))
  12009. (defvar org-log-note-purpose nil)
  12010. (defvar org-log-note-state nil)
  12011. (defvar org-log-note-previous-state nil)
  12012. (defvar org-log-note-how nil)
  12013. (defvar org-log-note-extra nil)
  12014. (defvar org-log-note-window-configuration nil)
  12015. (defvar org-log-note-return-to (make-marker))
  12016. (defvar org-log-note-effective-time nil
  12017. "Remembered current time so that dynamically scoped
  12018. `org-extend-today-until' affects tha timestamps in state change
  12019. log")
  12020. (defvar org-log-post-message nil
  12021. "Message to be displayed after a log note has been stored.
  12022. The auto-repeater uses this.")
  12023. (defun org-add-note ()
  12024. "Add a note to the current entry.
  12025. This is done in the same way as adding a state change note."
  12026. (interactive)
  12027. (org-add-log-setup 'note nil nil 'findpos nil))
  12028. (defvar org-property-end-re)
  12029. (defun org-add-log-setup (&optional purpose state prev-state
  12030. findpos how extra)
  12031. "Set up the post command hook to take a note.
  12032. If this is about to TODO state change, the new state is expected in STATE.
  12033. When FINDPOS is non-nil, find the correct position for the note in
  12034. the current entry. If not, assume that it can be inserted at point.
  12035. HOW is an indicator what kind of note should be created.
  12036. EXTRA is additional text that will be inserted into the notes buffer."
  12037. (let* ((org-log-into-drawer (org-log-into-drawer))
  12038. (drawer (cond ((stringp org-log-into-drawer)
  12039. org-log-into-drawer)
  12040. (org-log-into-drawer "LOGBOOK"))))
  12041. (save-restriction
  12042. (save-excursion
  12043. (when findpos
  12044. (org-back-to-heading t)
  12045. (narrow-to-region (point) (save-excursion
  12046. (outline-next-heading) (point)))
  12047. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12048. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12049. "[^\r\n]*\\)?"))
  12050. (goto-char (match-end 0))
  12051. (cond
  12052. (drawer
  12053. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12054. nil t)
  12055. (progn
  12056. (goto-char (match-end 0))
  12057. (or org-log-states-order-reversed
  12058. (and (re-search-forward org-property-end-re nil t)
  12059. (goto-char (1- (match-beginning 0))))))
  12060. (insert "\n:" drawer ":\n:END:")
  12061. (beginning-of-line 0)
  12062. (org-indent-line)
  12063. (beginning-of-line 2)
  12064. (org-indent-line)
  12065. (end-of-line 0)))
  12066. ((and org-log-state-notes-insert-after-drawers
  12067. (save-excursion
  12068. (forward-line) (looking-at org-drawer-regexp)))
  12069. (forward-line)
  12070. (while (looking-at org-drawer-regexp)
  12071. (goto-char (match-end 0))
  12072. (re-search-forward org-property-end-re (point-max) t)
  12073. (forward-line))
  12074. (forward-line -1)))
  12075. (unless org-log-states-order-reversed
  12076. (and (= (char-after) ?\n) (forward-char 1))
  12077. (org-skip-over-state-notes)
  12078. (skip-chars-backward " \t\n\r")))
  12079. (move-marker org-log-note-marker (point))
  12080. (setq org-log-note-purpose purpose
  12081. org-log-note-state state
  12082. org-log-note-previous-state prev-state
  12083. org-log-note-how how
  12084. org-log-note-extra extra
  12085. org-log-note-effective-time (org-current-effective-time))
  12086. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12087. (defun org-skip-over-state-notes ()
  12088. "Skip past the list of State notes in an entry."
  12089. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12090. (when (ignore-errors (goto-char (org-in-item-p)))
  12091. (let* ((struct (org-list-struct))
  12092. (prevs (org-list-prevs-alist struct)))
  12093. (while (looking-at "[ \t]*- State")
  12094. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12095. (org-list-get-item-end (point) struct)))))))
  12096. (defun org-add-log-note (&optional purpose)
  12097. "Pop up a window for taking a note, and add this note later at point."
  12098. (remove-hook 'post-command-hook 'org-add-log-note)
  12099. (setq org-log-note-window-configuration (current-window-configuration))
  12100. (delete-other-windows)
  12101. (move-marker org-log-note-return-to (point))
  12102. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12103. (goto-char org-log-note-marker)
  12104. (org-switch-to-buffer-other-window "*Org Note*")
  12105. (erase-buffer)
  12106. (if (memq org-log-note-how '(time state))
  12107. (let (current-prefix-arg) (org-store-log-note))
  12108. (let ((org-inhibit-startup t)) (org-mode))
  12109. (insert (format "# Insert note for %s.
  12110. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12111. (cond
  12112. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12113. ((eq org-log-note-purpose 'done) "closed todo item")
  12114. ((eq org-log-note-purpose 'state)
  12115. (format "state change from \"%s\" to \"%s\""
  12116. (or org-log-note-previous-state "")
  12117. (or org-log-note-state "")))
  12118. ((eq org-log-note-purpose 'reschedule)
  12119. "rescheduling")
  12120. ((eq org-log-note-purpose 'delschedule)
  12121. "no longer scheduled")
  12122. ((eq org-log-note-purpose 'redeadline)
  12123. "changing deadline")
  12124. ((eq org-log-note-purpose 'deldeadline)
  12125. "removing deadline")
  12126. ((eq org-log-note-purpose 'refile)
  12127. "refiling")
  12128. ((eq org-log-note-purpose 'note)
  12129. "this entry")
  12130. (t (error "This should not happen")))))
  12131. (if org-log-note-extra (insert org-log-note-extra))
  12132. (org-set-local 'org-finish-function 'org-store-log-note)
  12133. (run-hooks 'org-log-buffer-setup-hook)))
  12134. (defvar org-note-abort nil) ; dynamically scoped
  12135. (defun org-store-log-note ()
  12136. "Finish taking a log note, and insert it to where it belongs."
  12137. (let ((txt (buffer-string)))
  12138. (kill-buffer (current-buffer))
  12139. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12140. lines ind bul)
  12141. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12142. (setq txt (replace-match "" t t txt)))
  12143. (if (string-match "\\s-+\\'" txt)
  12144. (setq txt (replace-match "" t t txt)))
  12145. (setq lines (org-split-string txt "\n"))
  12146. (when (and note (string-match "\\S-" note))
  12147. (setq note
  12148. (org-replace-escapes
  12149. note
  12150. (list (cons "%u" (user-login-name))
  12151. (cons "%U" user-full-name)
  12152. (cons "%t" (format-time-string
  12153. (org-time-stamp-format 'long 'inactive)
  12154. org-log-note-effective-time))
  12155. (cons "%T" (format-time-string
  12156. (org-time-stamp-format 'long nil)
  12157. org-log-note-effective-time))
  12158. (cons "%d" (format-time-string
  12159. (org-time-stamp-format nil 'inactive)
  12160. org-log-note-effective-time))
  12161. (cons "%D" (format-time-string
  12162. (org-time-stamp-format nil nil)
  12163. org-log-note-effective-time))
  12164. (cons "%s" (if org-log-note-state
  12165. (concat "\"" org-log-note-state "\"")
  12166. ""))
  12167. (cons "%S" (if org-log-note-previous-state
  12168. (concat "\"" org-log-note-previous-state "\"")
  12169. "\"\"")))))
  12170. (if lines (setq note (concat note " \\\\")))
  12171. (push note lines))
  12172. (when (or current-prefix-arg org-note-abort)
  12173. (when org-log-into-drawer
  12174. (org-remove-empty-drawer-at
  12175. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12176. org-log-note-marker))
  12177. (setq lines nil))
  12178. (when lines
  12179. (with-current-buffer (marker-buffer org-log-note-marker)
  12180. (save-excursion
  12181. (goto-char org-log-note-marker)
  12182. (move-marker org-log-note-marker nil)
  12183. (end-of-line 1)
  12184. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12185. (setq ind (save-excursion
  12186. (if (ignore-errors (goto-char (org-in-item-p)))
  12187. (let ((struct (org-list-struct)))
  12188. (org-list-get-ind
  12189. (org-list-get-top-point struct) struct))
  12190. (skip-chars-backward " \r\t\n")
  12191. (cond
  12192. ((and (org-at-heading-p)
  12193. org-adapt-indentation)
  12194. (1+ (org-current-level)))
  12195. ((org-at-heading-p) 0)
  12196. (t (org-get-indentation))))))
  12197. (setq bul (org-list-bullet-string "-"))
  12198. (org-indent-line-to ind)
  12199. (insert bul (pop lines))
  12200. (let ((ind-body (+ (length bul) ind)))
  12201. (while lines
  12202. (insert "\n")
  12203. (org-indent-line-to ind-body)
  12204. (insert (pop lines))))
  12205. (message "Note stored")
  12206. (org-back-to-heading t)
  12207. (org-cycle-hide-drawers 'children))
  12208. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12209. ;; from within `org-add-log-note' because `buffer-undo-list'
  12210. ;; is then modified outside of `org-with-remote-undo'.
  12211. (when (eq this-command 'org-agenda-todo)
  12212. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12213. ;; Don't add undo information when called from `org-agenda-todo'
  12214. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12215. (set-window-configuration org-log-note-window-configuration)
  12216. (with-current-buffer (marker-buffer org-log-note-return-to)
  12217. (goto-char org-log-note-return-to))
  12218. (move-marker org-log-note-return-to nil)
  12219. (and org-log-post-message (message "%s" org-log-post-message))))
  12220. (defun org-remove-empty-drawer-at (drawer pos)
  12221. "Remove an empty drawer DRAWER at position POS.
  12222. POS may also be a marker."
  12223. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12224. (save-excursion
  12225. (save-restriction
  12226. (widen)
  12227. (goto-char pos)
  12228. (if (org-in-regexp
  12229. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12230. (replace-match ""))))))
  12231. (defvar org-ts-type nil)
  12232. (defun org-sparse-tree (&optional arg type)
  12233. "Create a sparse tree, prompt for the details.
  12234. This command can create sparse trees. You first need to select the type
  12235. of match used to create the tree:
  12236. t Show all TODO entries.
  12237. T Show entries with a specific TODO keyword.
  12238. m Show entries selected by a tags/property match.
  12239. p Enter a property name and its value (both with completion on existing
  12240. names/values) and show entries with that property.
  12241. r Show entries matching a regular expression (`/' can be used as well).
  12242. b Show deadlines and scheduled items before a date.
  12243. a Show deadlines and scheduled items after a date.
  12244. d Show deadlines due within `org-deadline-warning-days'.
  12245. D Show deadlines and scheduled items between a date range."
  12246. (interactive "P")
  12247. (let (ans kwd value ts-type)
  12248. (setq type (or type org-sparse-tree-default-date-type))
  12249. (setq org-ts-type type)
  12250. (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"
  12251. (cond ((eq type 'all) "all timestamps")
  12252. ((eq type 'scheduled) "only scheduled")
  12253. ((eq type 'deadline) "only deadline")
  12254. ((eq type 'active) "only active timestamps")
  12255. ((eq type 'inactive) "only inactive timestamps")
  12256. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12257. ((eq type 'closed) "with a closed time-stamp")
  12258. (t "scheduled/deadline")))
  12259. (setq ans (read-char-exclusive))
  12260. (cond
  12261. ((equal ans ?c)
  12262. (org-sparse-tree
  12263. arg (cadr (member type '(scheduled-or-deadline
  12264. all scheduled deadline active inactive closed)))))
  12265. ((equal ans ?d)
  12266. (call-interactively 'org-check-deadlines))
  12267. ((equal ans ?b)
  12268. (call-interactively 'org-check-before-date))
  12269. ((equal ans ?a)
  12270. (call-interactively 'org-check-after-date))
  12271. ((equal ans ?D)
  12272. (call-interactively 'org-check-dates-range))
  12273. ((equal ans ?t)
  12274. (call-interactively 'org-show-todo-tree))
  12275. ((equal ans ?T)
  12276. (org-show-todo-tree '(4)))
  12277. ((member ans '(?T ?m))
  12278. (call-interactively 'org-match-sparse-tree))
  12279. ((member ans '(?p ?P))
  12280. (setq kwd (org-icompleting-read "Property: "
  12281. (mapcar 'list (org-buffer-property-keys))))
  12282. (setq value (org-icompleting-read "Value: "
  12283. (mapcar 'list (org-property-values kwd))))
  12284. (unless (string-match "\\`{.*}\\'" value)
  12285. (setq value (concat "\"" value "\"")))
  12286. (org-match-sparse-tree arg (concat kwd "=" value)))
  12287. ((member ans '(?r ?R ?/))
  12288. (call-interactively 'org-occur))
  12289. (t (user-error "No such sparse tree command \"%c\"" ans)))))
  12290. (defvar org-occur-highlights nil
  12291. "List of overlays used for occur matches.")
  12292. (make-variable-buffer-local 'org-occur-highlights)
  12293. (defvar org-occur-parameters nil
  12294. "Parameters of the active org-occur calls.
  12295. This is a list, each call to org-occur pushes as cons cell,
  12296. containing the regular expression and the callback, onto the list.
  12297. The list can contain several entries if `org-occur' has been called
  12298. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12299. will only contain one set of parameters. When the highlights are
  12300. removed (for example with `C-c C-c', or with the next edit (depending
  12301. on `org-remove-highlights-with-change'), this variable is emptied
  12302. as well.")
  12303. (make-variable-buffer-local 'org-occur-parameters)
  12304. (defun org-occur (regexp &optional keep-previous callback)
  12305. "Make a compact tree which shows all matches of REGEXP.
  12306. The tree will show the lines where the regexp matches, and all higher
  12307. headlines above the match. It will also show the heading after the match,
  12308. to make sure editing the matching entry is easy.
  12309. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12310. call to `org-occur' will be kept, to allow stacking of calls to this
  12311. command.
  12312. If CALLBACK is non-nil, it is a function which is called to confirm
  12313. that the match should indeed be shown."
  12314. (interactive "sRegexp: \nP")
  12315. (when (equal regexp "")
  12316. (user-error "Regexp cannot be empty"))
  12317. (unless keep-previous
  12318. (org-remove-occur-highlights nil nil t))
  12319. (push (cons regexp callback) org-occur-parameters)
  12320. (let ((cnt 0))
  12321. (save-excursion
  12322. (goto-char (point-min))
  12323. (if (or (not keep-previous) ; do not want to keep
  12324. (not org-occur-highlights)) ; no previous matches
  12325. ;; hide everything
  12326. (org-overview))
  12327. (while (re-search-forward regexp nil t)
  12328. (when (or (not callback)
  12329. (save-match-data (funcall callback)))
  12330. (setq cnt (1+ cnt))
  12331. (when org-highlight-sparse-tree-matches
  12332. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12333. (org-show-context 'occur-tree))))
  12334. (when org-remove-highlights-with-change
  12335. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12336. nil 'local))
  12337. (unless org-sparse-tree-open-archived-trees
  12338. (org-hide-archived-subtrees (point-min) (point-max)))
  12339. (run-hooks 'org-occur-hook)
  12340. (if (org-called-interactively-p 'interactive)
  12341. (message "%d match(es) for regexp %s" cnt regexp))
  12342. cnt))
  12343. (defun org-occur-next-match (&optional n reset)
  12344. "Function for `next-error-function' to find sparse tree matches.
  12345. N is the number of matches to move, when negative move backwards.
  12346. RESET is entirely ignored - this function always goes back to the
  12347. starting point when no match is found."
  12348. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12349. (search-func (if (< n 0)
  12350. 'previous-single-char-property-change
  12351. 'next-single-char-property-change))
  12352. (n (abs n))
  12353. (pos (point))
  12354. p1)
  12355. (catch 'exit
  12356. (while (setq p1 (funcall search-func (point) 'org-type))
  12357. (when (equal p1 limit)
  12358. (goto-char pos)
  12359. (error "No more matches"))
  12360. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12361. (setq n (1- n))
  12362. (when (= n 0)
  12363. (goto-char p1)
  12364. (throw 'exit (point))))
  12365. (goto-char p1))
  12366. (goto-char p1)
  12367. (error "No more matches"))))
  12368. (defun org-show-context (&optional key)
  12369. "Make sure point and context are visible.
  12370. How much context is shown depends upon the variables
  12371. `org-show-hierarchy-above', `org-show-following-heading',
  12372. `org-show-entry-below' and `org-show-siblings'."
  12373. (let ((heading-p (org-at-heading-p t))
  12374. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12375. (following-p (org-get-alist-option org-show-following-heading key))
  12376. (entry-p (org-get-alist-option org-show-entry-below key))
  12377. (siblings-p (org-get-alist-option org-show-siblings key)))
  12378. ;; Show heading or entry text
  12379. (if (and heading-p (not entry-p))
  12380. (org-flag-heading nil) ; only show the heading
  12381. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12382. (org-show-hidden-entry))) ; show entire entry
  12383. (when following-p
  12384. ;; Show next sibling, or heading below text
  12385. (save-excursion
  12386. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12387. (org-flag-heading nil))))
  12388. (when siblings-p (org-show-siblings))
  12389. (when hierarchy-p
  12390. ;; show all higher headings, possibly with siblings
  12391. (save-excursion
  12392. (while (and (condition-case nil
  12393. (progn (org-up-heading-all 1) t)
  12394. (error nil))
  12395. (not (bobp)))
  12396. (org-flag-heading nil)
  12397. (when siblings-p (org-show-siblings)))))
  12398. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12399. (defvar org-reveal-start-hook nil
  12400. "Hook run before revealing a location.")
  12401. (defun org-reveal (&optional siblings)
  12402. "Show current entry, hierarchy above it, and the following headline.
  12403. This can be used to show a consistent set of context around locations
  12404. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12405. not t for the search context.
  12406. With optional argument SIBLINGS, on each level of the hierarchy all
  12407. siblings are shown. This repairs the tree structure to what it would
  12408. look like when opened with hierarchical calls to `org-cycle'.
  12409. With double optional argument \\[universal-argument] \\[universal-argument], \
  12410. go to the parent and show the
  12411. entire tree."
  12412. (interactive "P")
  12413. (run-hooks 'org-reveal-start-hook)
  12414. (let ((org-show-hierarchy-above t)
  12415. (org-show-following-heading t)
  12416. (org-show-siblings (if siblings t org-show-siblings)))
  12417. (org-show-context nil))
  12418. (when (equal siblings '(16))
  12419. (save-excursion
  12420. (when (org-up-heading-safe)
  12421. (org-show-subtree)
  12422. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12423. (defun org-highlight-new-match (beg end)
  12424. "Highlight from BEG to END and mark the highlight is an occur headline."
  12425. (let ((ov (make-overlay beg end)))
  12426. (overlay-put ov 'face 'secondary-selection)
  12427. (overlay-put ov 'org-type 'org-occur)
  12428. (push ov org-occur-highlights)))
  12429. (defun org-remove-occur-highlights (&optional beg end noremove)
  12430. "Remove the occur highlights from the buffer.
  12431. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12432. from the `before-change-functions' in the current buffer."
  12433. (interactive)
  12434. (unless org-inhibit-highlight-removal
  12435. (mapc 'delete-overlay org-occur-highlights)
  12436. (setq org-occur-highlights nil)
  12437. (setq org-occur-parameters nil)
  12438. (unless noremove
  12439. (remove-hook 'before-change-functions
  12440. 'org-remove-occur-highlights 'local))))
  12441. ;;;; Priorities
  12442. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12443. "Regular expression matching the priority indicator.")
  12444. (defvar org-remove-priority-next-time nil)
  12445. (defun org-priority-up ()
  12446. "Increase the priority of the current item."
  12447. (interactive)
  12448. (org-priority 'up))
  12449. (defun org-priority-down ()
  12450. "Decrease the priority of the current item."
  12451. (interactive)
  12452. (org-priority 'down))
  12453. (defun org-priority (&optional action show)
  12454. "Change the priority of an item.
  12455. ACTION can be `set', `up', `down', or a character."
  12456. (interactive "P")
  12457. (if (equal action '(4))
  12458. (org-show-priority)
  12459. (unless org-enable-priority-commands
  12460. (user-error "Priority commands are disabled"))
  12461. (setq action (or action 'set))
  12462. (let (current new news have remove)
  12463. (save-excursion
  12464. (org-back-to-heading t)
  12465. (if (looking-at org-priority-regexp)
  12466. (setq current (string-to-char (match-string 2))
  12467. have t))
  12468. (cond
  12469. ((eq action 'remove)
  12470. (setq remove t new ?\ ))
  12471. ((or (eq action 'set)
  12472. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12473. (if (not (eq action 'set))
  12474. (setq new action)
  12475. (message "Priority %c-%c, SPC to remove: "
  12476. org-highest-priority org-lowest-priority)
  12477. (save-match-data
  12478. (setq new (read-char-exclusive))))
  12479. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12480. (= (upcase org-lowest-priority) org-lowest-priority))
  12481. (setq new (upcase new)))
  12482. (cond ((equal new ?\ ) (setq remove t))
  12483. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12484. (user-error "Priority must be between `%c' and `%c'"
  12485. org-highest-priority org-lowest-priority))))
  12486. ((eq action 'up)
  12487. (setq new (if have
  12488. (1- current) ; normal cycling
  12489. ;; last priority was empty
  12490. (if (eq last-command this-command)
  12491. org-lowest-priority ; wrap around empty to lowest
  12492. ;; default
  12493. (if org-priority-start-cycle-with-default
  12494. org-default-priority
  12495. (1- org-default-priority))))))
  12496. ((eq action 'down)
  12497. (setq new (if have
  12498. (1+ current) ; normal cycling
  12499. ;; last priority was empty
  12500. (if (eq last-command this-command)
  12501. org-highest-priority ; wrap around empty to highest
  12502. ;; default
  12503. (if org-priority-start-cycle-with-default
  12504. org-default-priority
  12505. (1+ org-default-priority))))))
  12506. (t (user-error "Invalid action")))
  12507. (if (or (< (upcase new) org-highest-priority)
  12508. (> (upcase new) org-lowest-priority))
  12509. (if (and (memq action '(up down))
  12510. (not have) (not (eq last-command this-command)))
  12511. ;; `new' is from default priority
  12512. (error
  12513. "The default can not be set, see `org-default-priority' why")
  12514. ;; normal cycling: `new' is beyond highest/lowest priority
  12515. ;; and is wrapped around to the empty priority
  12516. (setq remove t)))
  12517. (setq news (format "%c" new))
  12518. (if have
  12519. (if remove
  12520. (replace-match "" t t nil 1)
  12521. (replace-match news t t nil 2))
  12522. (if remove
  12523. (user-error "No priority cookie found in line")
  12524. (let ((case-fold-search nil))
  12525. (looking-at org-todo-line-regexp))
  12526. (if (match-end 2)
  12527. (progn
  12528. (goto-char (match-end 2))
  12529. (insert " [#" news "]"))
  12530. (goto-char (match-beginning 3))
  12531. (insert "[#" news "] "))))
  12532. (org-preserve-lc (org-set-tags nil 'align)))
  12533. (if remove
  12534. (message "Priority removed")
  12535. (message "Priority of current item set to %s" news)))))
  12536. (defun org-show-priority ()
  12537. "Show the priority of the current item.
  12538. This priority is composed of the main priority given with the [#A] cookies,
  12539. and by additional input from the age of a schedules or deadline entry."
  12540. (interactive)
  12541. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12542. (org-get-at-bol 'priority)
  12543. (save-excursion
  12544. (save-match-data
  12545. (beginning-of-line)
  12546. (and (looking-at org-heading-regexp)
  12547. (org-get-priority (match-string 0))))))))
  12548. (message "Priority is %d" (if pri pri -1000))))
  12549. (defun org-get-priority (s)
  12550. "Find priority cookie and return priority."
  12551. (save-match-data
  12552. (if (functionp org-get-priority-function)
  12553. (funcall org-get-priority-function)
  12554. (if (not (string-match org-priority-regexp s))
  12555. (* 1000 (- org-lowest-priority org-default-priority))
  12556. (* 1000 (- org-lowest-priority
  12557. (string-to-char (match-string 2 s))))))))
  12558. ;;;; Tags
  12559. (defvar org-agenda-archives-mode)
  12560. (defvar org-map-continue-from nil
  12561. "Position from where mapping should continue.
  12562. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12563. (defvar org-scanner-tags nil
  12564. "The current tag list while the tags scanner is running.")
  12565. (defvar org-trust-scanner-tags nil
  12566. "Should `org-get-tags-at' use the tags for the scanner.
  12567. This is for internal dynamical scoping only.
  12568. When this is non-nil, the function `org-get-tags-at' will return the value
  12569. of `org-scanner-tags' instead of building the list by itself. This
  12570. can lead to large speed-ups when the tags scanner is used in a file with
  12571. many entries, and when the list of tags is retrieved, for example to
  12572. obtain a list of properties. Building the tags list for each entry in such
  12573. a file becomes an N^2 operation - but with this variable set, it scales
  12574. as N.")
  12575. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12576. "Sca headline tags with inheritance and produce output ACTION.
  12577. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12578. or `agenda' to produce an entry list for an agenda view. It can also be
  12579. a Lisp form or a function that should be called at each matched headline, in
  12580. this case the return value is a list of all return values from these calls.
  12581. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12582. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12583. only lines with a not-done TODO keyword are included in the output.
  12584. This should be the same variable that was scoped into
  12585. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12586. START-LEVEL can be a string with asterisks, reducing the scope to
  12587. headlines matching this string."
  12588. (require 'org-agenda)
  12589. (let* ((re (concat "^"
  12590. (if start-level
  12591. ;; Get the correct level to match
  12592. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12593. org-outline-regexp)
  12594. " *\\(\\<\\("
  12595. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12596. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12597. (props (list 'face 'default
  12598. 'done-face 'org-agenda-done
  12599. 'undone-face 'default
  12600. 'mouse-face 'highlight
  12601. 'org-not-done-regexp org-not-done-regexp
  12602. 'org-todo-regexp org-todo-regexp
  12603. 'org-complex-heading-regexp org-complex-heading-regexp
  12604. 'help-echo
  12605. (format "mouse-2 or RET jump to org file %s"
  12606. (abbreviate-file-name
  12607. (or (buffer-file-name (buffer-base-buffer))
  12608. (buffer-name (buffer-base-buffer)))))))
  12609. (org-map-continue-from nil)
  12610. lspos tags tags-list
  12611. (tags-alist (list (cons 0 org-file-tags)))
  12612. (llast 0) rtn rtn1 level category i txt
  12613. todo marker entry priority)
  12614. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12615. (setq action (list 'lambda nil action)))
  12616. (save-excursion
  12617. (goto-char (point-min))
  12618. (when (eq action 'sparse-tree)
  12619. (org-overview)
  12620. (org-remove-occur-highlights))
  12621. (while (let (case-fold-search)
  12622. (re-search-forward re nil t))
  12623. (setq org-map-continue-from nil)
  12624. (catch :skip
  12625. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12626. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12627. (goto-char (setq lspos (match-beginning 0)))
  12628. (setq level (org-reduced-level (org-outline-level))
  12629. category (org-get-category))
  12630. (setq i llast llast level)
  12631. ;; remove tag lists from same and sublevels
  12632. (while (>= i level)
  12633. (when (setq entry (assoc i tags-alist))
  12634. (setq tags-alist (delete entry tags-alist)))
  12635. (setq i (1- i)))
  12636. ;; add the next tags
  12637. (when tags
  12638. (setq tags (org-split-string tags ":")
  12639. tags-alist
  12640. (cons (cons level tags) tags-alist)))
  12641. ;; compile tags for current headline
  12642. (setq tags-list
  12643. (if org-use-tag-inheritance
  12644. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12645. tags)
  12646. org-scanner-tags tags-list)
  12647. (when org-use-tag-inheritance
  12648. (setcdr (car tags-alist)
  12649. (mapcar (lambda (x)
  12650. (setq x (copy-sequence x))
  12651. (org-add-prop-inherited x))
  12652. (cdar tags-alist))))
  12653. (when (and tags org-use-tag-inheritance
  12654. (or (not (eq t org-use-tag-inheritance))
  12655. org-tags-exclude-from-inheritance))
  12656. ;; selective inheritance, remove uninherited ones
  12657. (setcdr (car tags-alist)
  12658. (org-remove-uninherited-tags (cdar tags-alist))))
  12659. (when (and
  12660. ;; eval matcher only when the todo condition is OK
  12661. (and (or (not todo-only) (member todo org-not-done-keywords))
  12662. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12663. (eval matcher)))
  12664. ;; Call the skipper, but return t if it does not skip,
  12665. ;; so that the `and' form continues evaluating
  12666. (progn
  12667. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12668. t)
  12669. ;; Check if timestamps are deselecting this entry
  12670. (or (not todo-only)
  12671. (and (member todo org-not-done-keywords)
  12672. (or (not org-agenda-tags-todo-honor-ignore-options)
  12673. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12674. ;; select this headline
  12675. (cond
  12676. ((eq action 'sparse-tree)
  12677. (and org-highlight-sparse-tree-matches
  12678. (org-get-heading) (match-end 0)
  12679. (org-highlight-new-match
  12680. (match-beginning 1) (match-end 1)))
  12681. (org-show-context 'tags-tree))
  12682. ((eq action 'agenda)
  12683. (setq txt (org-agenda-format-item
  12684. ""
  12685. (concat
  12686. (if (eq org-tags-match-list-sublevels 'indented)
  12687. (make-string (1- level) ?.) "")
  12688. (org-get-heading))
  12689. level category
  12690. tags-list)
  12691. priority (org-get-priority txt))
  12692. (goto-char lspos)
  12693. (setq marker (org-agenda-new-marker))
  12694. (org-add-props txt props
  12695. 'org-marker marker 'org-hd-marker marker 'org-category category
  12696. 'todo-state todo
  12697. 'priority priority 'type "tagsmatch")
  12698. (push txt rtn))
  12699. ((functionp action)
  12700. (setq org-map-continue-from nil)
  12701. (save-excursion
  12702. (setq rtn1 (funcall action))
  12703. (push rtn1 rtn)))
  12704. (t (user-error "Invalid action")))
  12705. ;; if we are to skip sublevels, jump to end of subtree
  12706. (unless org-tags-match-list-sublevels
  12707. (org-end-of-subtree t)
  12708. (backward-char 1))))
  12709. ;; Get the correct position from where to continue
  12710. (if org-map-continue-from
  12711. (goto-char org-map-continue-from)
  12712. (and (= (point) lspos) (end-of-line 1)))))
  12713. (when (and (eq action 'sparse-tree)
  12714. (not org-sparse-tree-open-archived-trees))
  12715. (org-hide-archived-subtrees (point-min) (point-max)))
  12716. (nreverse rtn)))
  12717. (defun org-remove-uninherited-tags (tags)
  12718. "Remove all tags that are not inherited from the list TAGS."
  12719. (cond
  12720. ((eq org-use-tag-inheritance t)
  12721. (if org-tags-exclude-from-inheritance
  12722. (org-delete-all org-tags-exclude-from-inheritance tags)
  12723. tags))
  12724. ((not org-use-tag-inheritance) nil)
  12725. ((stringp org-use-tag-inheritance)
  12726. (delq nil (mapcar
  12727. (lambda (x)
  12728. (if (and (string-match org-use-tag-inheritance x)
  12729. (not (member x org-tags-exclude-from-inheritance)))
  12730. x nil))
  12731. tags)))
  12732. ((listp org-use-tag-inheritance)
  12733. (delq nil (mapcar
  12734. (lambda (x)
  12735. (if (member x org-use-tag-inheritance) x nil))
  12736. tags)))))
  12737. (defun org-match-sparse-tree (&optional todo-only match)
  12738. "Create a sparse tree according to tags string MATCH.
  12739. MATCH can contain positive and negative selection of tags, like
  12740. \"+WORK+URGENT-WITHBOSS\".
  12741. If optional argument TODO-ONLY is non-nil, only select lines that are
  12742. also TODO lines."
  12743. (interactive "P")
  12744. (org-agenda-prepare-buffers (list (current-buffer)))
  12745. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12746. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12747. (defvar org-cached-props nil)
  12748. (defun org-cached-entry-get (pom property)
  12749. (if (or (eq t org-use-property-inheritance)
  12750. (and (stringp org-use-property-inheritance)
  12751. (string-match org-use-property-inheritance property))
  12752. (and (listp org-use-property-inheritance)
  12753. (member property org-use-property-inheritance)))
  12754. ;; Caching is not possible, check it directly
  12755. (org-entry-get pom property 'inherit)
  12756. ;; Get all properties, so that we can do complicated checks easily
  12757. (cdr (assoc property (or org-cached-props
  12758. (setq org-cached-props
  12759. (org-entry-properties pom)))))))
  12760. (defun org-global-tags-completion-table (&optional files)
  12761. "Return the list of all tags in all agenda buffer/files.
  12762. Optional FILES argument is a list of files which can be used
  12763. instead of the agenda files."
  12764. (save-excursion
  12765. (org-uniquify
  12766. (delq nil
  12767. (apply 'append
  12768. (mapcar
  12769. (lambda (file)
  12770. (set-buffer (find-file-noselect file))
  12771. (append (org-get-buffer-tags)
  12772. (mapcar (lambda (x) (if (stringp (car-safe x))
  12773. (list (car-safe x)) nil))
  12774. org-tag-alist)))
  12775. (if (and files (car files))
  12776. files
  12777. (org-agenda-files))))))))
  12778. (defun org-make-tags-matcher (match)
  12779. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12780. The variable `todo-only' is scoped dynamically into this function.
  12781. It will be set to t if the matcher restricts matching to TODO entries,
  12782. otherwise will not be touched.
  12783. Returns a cons of the selection string MATCH and the constructed
  12784. lisp form implementing the matcher. The matcher is to be evaluated
  12785. at an Org entry, with point on the headline, and returns t if the
  12786. entry matches the selection string MATCH. The returned lisp form
  12787. references two variables with information about the entry, which
  12788. must be bound around the form's evaluation: todo, the TODO keyword
  12789. at the entry (or nil of none); and tags-list, the list of all tags
  12790. at the entry including inherited ones. Additionally, the category
  12791. of the entry (if any) must be specified as the text property
  12792. 'org-category on the headline.
  12793. See also `org-scan-tags'.
  12794. "
  12795. (declare (special todo-only))
  12796. (unless (boundp 'todo-only)
  12797. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12798. (unless match
  12799. ;; Get a new match request, with completion against the global
  12800. ;; tags table and the local tags in current buffer
  12801. (let ((org-last-tags-completion-table
  12802. (org-uniquify
  12803. (delq nil (append (org-get-buffer-tags)
  12804. (org-global-tags-completion-table))))))
  12805. (setq match (org-completing-read-no-i
  12806. "Match: " 'org-tags-completion-function nil nil nil
  12807. 'org-tags-history))))
  12808. ;; Parse the string and create a lisp form
  12809. (let ((match0 match)
  12810. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12811. minus tag mm
  12812. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12813. orterms term orlist re-p str-p level-p level-op time-p
  12814. prop-p pn pv po gv rest)
  12815. ;; Expand group tags
  12816. (setq match (org-tags-expand match))
  12817. (if (string-match "/+" match)
  12818. ;; match contains also a todo-matching request
  12819. (progn
  12820. (setq tagsmatch (substring match 0 (match-beginning 0))
  12821. todomatch (substring match (match-end 0)))
  12822. (if (string-match "^!" todomatch)
  12823. (setq todo-only t todomatch (substring todomatch 1)))
  12824. (if (string-match "^\\s-*$" todomatch)
  12825. (setq todomatch nil)))
  12826. ;; only matching tags
  12827. (setq tagsmatch match todomatch nil))
  12828. ;; Make the tags matcher
  12829. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12830. (setq tagsmatcher t)
  12831. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12832. (while (setq term (pop orterms))
  12833. (while (and (equal (substring term -1) "\\") orterms)
  12834. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12835. (while (string-match re term)
  12836. (setq rest (substring term (match-end 0))
  12837. minus (and (match-end 1)
  12838. (equal (match-string 1 term) "-"))
  12839. tag (save-match-data (replace-regexp-in-string
  12840. "\\\\-" "-"
  12841. (match-string 2 term)))
  12842. re-p (equal (string-to-char tag) ?{)
  12843. level-p (match-end 4)
  12844. prop-p (match-end 5)
  12845. mm (cond
  12846. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12847. (level-p
  12848. (setq level-op (org-op-to-function (match-string 3 term)))
  12849. `(,level-op level ,(string-to-number
  12850. (match-string 4 term))))
  12851. (prop-p
  12852. (setq pn (match-string 5 term)
  12853. po (match-string 6 term)
  12854. pv (match-string 7 term)
  12855. re-p (equal (string-to-char pv) ?{)
  12856. str-p (equal (string-to-char pv) ?\")
  12857. time-p (save-match-data
  12858. (string-match "^\"[[<].*[]>]\"$" pv))
  12859. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12860. (if time-p (setq pv (org-matcher-time pv)))
  12861. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12862. (cond
  12863. ((equal pn "CATEGORY")
  12864. (setq gv '(get-text-property (point) 'org-category)))
  12865. ((equal pn "TODO")
  12866. (setq gv 'todo))
  12867. (t
  12868. (setq gv `(org-cached-entry-get nil ,pn))))
  12869. (if re-p
  12870. (if (eq po 'org<>)
  12871. `(not (string-match ,pv (or ,gv "")))
  12872. `(string-match ,pv (or ,gv "")))
  12873. (if str-p
  12874. `(,po (or ,gv "") ,pv)
  12875. `(,po (string-to-number (or ,gv ""))
  12876. ,(string-to-number pv) ))))
  12877. (t `(member ,tag tags-list)))
  12878. mm (if minus (list 'not mm) mm)
  12879. term rest)
  12880. (push mm tagsmatcher))
  12881. (push (if (> (length tagsmatcher) 1)
  12882. (cons 'and tagsmatcher)
  12883. (car tagsmatcher))
  12884. orlist)
  12885. (setq tagsmatcher nil))
  12886. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12887. (setq tagsmatcher
  12888. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12889. ;; Make the todo matcher
  12890. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12891. (setq todomatcher t)
  12892. (setq orterms (org-split-string todomatch "|") orlist nil)
  12893. (while (setq term (pop orterms))
  12894. (while (string-match re term)
  12895. (setq minus (and (match-end 1)
  12896. (equal (match-string 1 term) "-"))
  12897. kwd (match-string 2 term)
  12898. re-p (equal (string-to-char kwd) ?{)
  12899. term (substring term (match-end 0))
  12900. mm (if re-p
  12901. `(string-match ,(substring kwd 1 -1) todo)
  12902. (list 'equal 'todo kwd))
  12903. mm (if minus (list 'not mm) mm))
  12904. (push mm todomatcher))
  12905. (push (if (> (length todomatcher) 1)
  12906. (cons 'and todomatcher)
  12907. (car todomatcher))
  12908. orlist)
  12909. (setq todomatcher nil))
  12910. (setq todomatcher (if (> (length orlist) 1)
  12911. (cons 'or orlist) (car orlist))))
  12912. ;; Return the string and lisp forms of the matcher
  12913. (setq matcher (if todomatcher
  12914. (list 'and tagsmatcher todomatcher)
  12915. tagsmatcher))
  12916. (when todo-only
  12917. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12918. matcher)))
  12919. (cons match0 matcher)))
  12920. (defun org-tags-expand (match &optional single-as-list downcased)
  12921. "Expand group tags in MATCH.
  12922. This replaces every group tag in MATCH with a regexp tag search.
  12923. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12924. will be replaced like this:
  12925. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  12926. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  12927. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  12928. Replacing by a regexp preserves the structure of the match.
  12929. E.g., this expansion
  12930. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  12931. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12932. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12933. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12934. assumed to be a single group tag, and the function will return
  12935. the list of tags in this group.
  12936. When DOWNCASE is non-nil, expand downcased TAGS."
  12937. (if org-group-tags
  12938. (let* ((case-fold-search t)
  12939. (stable org-mode-syntax-table)
  12940. (tal (or org-tag-groups-alist-for-agenda
  12941. org-tag-groups-alist))
  12942. (tal (if downcased
  12943. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12944. (tml (mapcar 'car tal))
  12945. (rtnmatch match) rpl)
  12946. ;; @ and _ are allowed as word-components in tags
  12947. (modify-syntax-entry ?@ "w" stable)
  12948. (modify-syntax-entry ?_ "w" stable)
  12949. (while (and tml
  12950. (with-syntax-table stable
  12951. (string-match
  12952. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  12953. (regexp-opt tml) "\\>\\)") rtnmatch)))
  12954. (let* ((dir (match-string 1 rtnmatch))
  12955. (tag (match-string 2 rtnmatch))
  12956. (tag (if downcased (downcase tag) tag)))
  12957. (setq tml (delete tag tml))
  12958. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  12959. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  12960. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  12961. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  12962. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  12963. (if single-as-list
  12964. (or (reverse rpl) (list rtnmatch))
  12965. rtnmatch))
  12966. (if single-as-list (list (if downcased (downcase match) match))
  12967. match)))
  12968. (defun org-op-to-function (op &optional stringp)
  12969. "Turn an operator into the appropriate function."
  12970. (setq op
  12971. (cond
  12972. ((equal op "<" ) '(< string< org-time<))
  12973. ((equal op ">" ) '(> org-string> org-time>))
  12974. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12975. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12976. ((member op '("=" "==")) '(= string= org-time=))
  12977. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12978. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12979. (defun org<> (a b) (not (= a b)))
  12980. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12981. (defun org-string>= (a b) (not (string< a b)))
  12982. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12983. (defun org-string<> (a b) (not (string= a b)))
  12984. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12985. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12986. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12987. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12988. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12989. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12990. (defun org-2ft (s)
  12991. "Convert S to a floating point time.
  12992. If S is already a number, just return it. If it is a string, parse
  12993. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12994. (cond
  12995. ((numberp s) s)
  12996. ((stringp s)
  12997. (condition-case nil
  12998. (float-time (apply 'encode-time (org-parse-time-string s)))
  12999. (error 0.)))
  13000. (t 0.)))
  13001. (defun org-time-today ()
  13002. "Time in seconds today at 0:00.
  13003. Returns the float number of seconds since the beginning of the
  13004. epoch to the beginning of today (00:00)."
  13005. (float-time (apply 'encode-time
  13006. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13007. (defun org-matcher-time (s)
  13008. "Interpret a time comparison value."
  13009. (save-match-data
  13010. (cond
  13011. ((string= s "<now>") (float-time))
  13012. ((string= s "<today>") (org-time-today))
  13013. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13014. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13015. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13016. (+ (org-time-today)
  13017. (* (string-to-number (match-string 1 s))
  13018. (cdr (assoc (match-string 2 s)
  13019. '(("d" . 86400.0) ("w" . 604800.0)
  13020. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13021. (t (org-2ft s)))))
  13022. (defun org-match-any-p (re list)
  13023. "Does re match any element of list?"
  13024. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13025. (delq nil list))
  13026. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13027. (defvar org-tags-overlay (make-overlay 1 1))
  13028. (org-detach-overlay org-tags-overlay)
  13029. (defun org-get-local-tags-at (&optional pos)
  13030. "Get a list of tags defined in the current headline."
  13031. (org-get-tags-at pos 'local))
  13032. (defun org-get-local-tags ()
  13033. "Get a list of tags defined in the current headline."
  13034. (org-get-tags-at nil 'local))
  13035. (defun org-get-tags-at (&optional pos local)
  13036. "Get a list of all headline tags applicable at POS.
  13037. POS defaults to point. If tags are inherited, the list contains
  13038. the targets in the same sequence as the headlines appear, i.e.
  13039. the tags of the current headline come last.
  13040. When LOCAL is non-nil, only return tags from the current headline,
  13041. ignore inherited ones."
  13042. (interactive)
  13043. (if (and org-trust-scanner-tags
  13044. (or (not pos) (equal pos (point)))
  13045. (not local))
  13046. org-scanner-tags
  13047. (let (tags ltags lastpos parent)
  13048. (save-excursion
  13049. (save-restriction
  13050. (widen)
  13051. (goto-char (or pos (point)))
  13052. (save-match-data
  13053. (catch 'done
  13054. (condition-case nil
  13055. (progn
  13056. (org-back-to-heading t)
  13057. (while (not (equal lastpos (point)))
  13058. (setq lastpos (point))
  13059. (when (looking-at
  13060. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13061. (setq ltags (org-split-string
  13062. (org-match-string-no-properties 1) ":"))
  13063. (when parent
  13064. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13065. (setq tags (append
  13066. (if parent
  13067. (org-remove-uninherited-tags ltags)
  13068. ltags)
  13069. tags)))
  13070. (or org-use-tag-inheritance (throw 'done t))
  13071. (if local (throw 'done t))
  13072. (or (org-up-heading-safe) (error nil))
  13073. (setq parent t)))
  13074. (error nil)))))
  13075. (if local
  13076. tags
  13077. (reverse (delete-dups
  13078. (reverse (append
  13079. (org-remove-uninherited-tags
  13080. org-file-tags) tags)))))))))
  13081. (defun org-add-prop-inherited (s)
  13082. (add-text-properties 0 (length s) '(inherited t) s)
  13083. s)
  13084. (defun org-toggle-tag (tag &optional onoff)
  13085. "Toggle the tag TAG for the current line.
  13086. If ONOFF is `on' or `off', don't toggle but set to this state."
  13087. (let (res current)
  13088. (save-excursion
  13089. (org-back-to-heading t)
  13090. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13091. (point-at-eol) t)
  13092. (progn
  13093. (setq current (match-string 1))
  13094. (replace-match ""))
  13095. (setq current ""))
  13096. (setq current (nreverse (org-split-string current ":")))
  13097. (cond
  13098. ((eq onoff 'on)
  13099. (setq res t)
  13100. (or (member tag current) (push tag current)))
  13101. ((eq onoff 'off)
  13102. (or (not (member tag current)) (setq current (delete tag current))))
  13103. (t (if (member tag current)
  13104. (setq current (delete tag current))
  13105. (setq res t)
  13106. (push tag current))))
  13107. (end-of-line 1)
  13108. (if current
  13109. (progn
  13110. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13111. (org-set-tags nil t))
  13112. (delete-horizontal-space))
  13113. (run-hooks 'org-after-tags-change-hook))
  13114. res))
  13115. (defun org-align-tags-here (to-col)
  13116. ;; Assumes that this is a headline
  13117. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13118. (beginning-of-line 1)
  13119. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13120. (< pos (match-beginning 2)))
  13121. (progn
  13122. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13123. (goto-char (match-beginning 1))
  13124. (insert " ")
  13125. (delete-region (point) (1+ (match-beginning 2)))
  13126. (setq ncol (max (current-column)
  13127. (1+ col)
  13128. (if (> to-col 0)
  13129. to-col
  13130. (- (abs to-col) tags-l))))
  13131. (setq p (point))
  13132. (insert (make-string (- ncol (current-column)) ?\ ))
  13133. (setq ncol (current-column))
  13134. (when indent-tabs-mode (tabify p (point-at-eol)))
  13135. (org-move-to-column (min ncol col) t))
  13136. (goto-char pos))))
  13137. (defun org-set-tags-command (&optional arg just-align)
  13138. "Call the set-tags command for the current entry."
  13139. (interactive "P")
  13140. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13141. (org-set-tags arg just-align)
  13142. (save-excursion
  13143. (unless (and (org-region-active-p)
  13144. org-loop-over-headlines-in-active-region)
  13145. (org-back-to-heading t))
  13146. (org-set-tags arg just-align))))
  13147. (defun org-set-tags-to (data)
  13148. "Set the tags of the current entry to DATA, replacing the current tags.
  13149. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13150. If DATA is nil or the empty string, any tags will be removed."
  13151. (interactive "sTags: ")
  13152. (setq data
  13153. (cond
  13154. ((eq data nil) "")
  13155. ((equal data "") "")
  13156. ((stringp data)
  13157. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13158. ":"))
  13159. ((listp data)
  13160. (concat ":" (mapconcat 'identity data ":") ":"))))
  13161. (when data
  13162. (save-excursion
  13163. (org-back-to-heading t)
  13164. (when (looking-at org-complex-heading-regexp)
  13165. (if (match-end 5)
  13166. (progn
  13167. (goto-char (match-beginning 5))
  13168. (insert data)
  13169. (delete-region (point) (point-at-eol))
  13170. (org-set-tags nil 'align))
  13171. (goto-char (point-at-eol))
  13172. (insert " " data)
  13173. (org-set-tags nil 'align)))
  13174. (beginning-of-line 1)
  13175. (if (looking-at ".*?\\([ \t]+\\)$")
  13176. (delete-region (match-beginning 1) (match-end 1))))))
  13177. (defun org-align-all-tags ()
  13178. "Align the tags i all headings."
  13179. (interactive)
  13180. (save-excursion
  13181. (or (ignore-errors (org-back-to-heading t))
  13182. (outline-next-heading))
  13183. (if (org-at-heading-p)
  13184. (org-set-tags t)
  13185. (message "No headings"))))
  13186. (defvar org-indent-indentation-per-level)
  13187. (defun org-set-tags (&optional arg just-align)
  13188. "Set the tags for the current headline.
  13189. With prefix ARG, realign all tags in headings in the current buffer."
  13190. (interactive "P")
  13191. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13192. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13193. 'region-start-level 'region))
  13194. org-loop-over-headlines-in-active-region)
  13195. (org-map-entries
  13196. ;; We don't use ARG and JUST-ALIGN here these args are not
  13197. ;; useful when looping over headlines
  13198. `(org-set-tags)
  13199. org-loop-over-headlines-in-active-region
  13200. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13201. (let* ((re org-outline-regexp-bol)
  13202. (current (unless arg (org-get-tags-string)))
  13203. (col (current-column))
  13204. (org-setting-tags t)
  13205. table current-tags inherited-tags ; computed below when needed
  13206. tags p0 c0 c1 rpl di tc level)
  13207. (if arg
  13208. (save-excursion
  13209. (goto-char (point-min))
  13210. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13211. (while (re-search-forward re nil t)
  13212. (org-set-tags nil t)
  13213. (end-of-line 1)))
  13214. (message "All tags realigned to column %d" org-tags-column))
  13215. (if just-align
  13216. (setq tags current)
  13217. ;; Get a new set of tags from the user
  13218. (save-excursion
  13219. (setq table (append org-tag-persistent-alist
  13220. (or org-tag-alist (org-get-buffer-tags))
  13221. (and
  13222. org-complete-tags-always-offer-all-agenda-tags
  13223. (org-global-tags-completion-table
  13224. (org-agenda-files))))
  13225. org-last-tags-completion-table table
  13226. current-tags (org-split-string current ":")
  13227. inherited-tags (nreverse
  13228. (nthcdr (length current-tags)
  13229. (nreverse (org-get-tags-at))))
  13230. tags
  13231. (if (or (eq t org-use-fast-tag-selection)
  13232. (and org-use-fast-tag-selection
  13233. (delq nil (mapcar 'cdr table))))
  13234. (org-fast-tag-selection
  13235. current-tags inherited-tags table
  13236. (if org-fast-tag-selection-include-todo
  13237. org-todo-key-alist))
  13238. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13239. (org-trim
  13240. (org-icompleting-read "Tags: "
  13241. 'org-tags-completion-function
  13242. nil nil current 'org-tags-history))))))
  13243. (while (string-match "[-+&]+" tags)
  13244. ;; No boolean logic, just a list
  13245. (setq tags (replace-match ":" t t tags))))
  13246. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13247. (if org-tags-sort-function
  13248. (setq tags (mapconcat 'identity
  13249. (sort (org-split-string
  13250. tags (org-re "[^[:alnum:]_@#%]+"))
  13251. org-tags-sort-function) ":")))
  13252. (if (string-match "\\`[\t ]*\\'" tags)
  13253. (setq tags "")
  13254. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13255. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13256. ;; Insert new tags at the correct column
  13257. (beginning-of-line 1)
  13258. (setq level (or (and (looking-at org-outline-regexp)
  13259. (- (match-end 0) (point) 1))
  13260. 1))
  13261. (cond
  13262. ((and (equal current "") (equal tags "")))
  13263. ((re-search-forward
  13264. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13265. (point-at-eol) t)
  13266. (if (equal tags "")
  13267. (setq rpl "")
  13268. (goto-char (match-beginning 0))
  13269. (setq c0 (current-column)
  13270. ;; compute offset for the case of org-indent-mode active
  13271. di (if org-indent-mode
  13272. (* (1- org-indent-indentation-per-level) (1- level))
  13273. 0)
  13274. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13275. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13276. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13277. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13278. (replace-match rpl t t)
  13279. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13280. tags)
  13281. (t (error "Tags alignment failed")))
  13282. (org-move-to-column col)
  13283. (unless just-align
  13284. (run-hooks 'org-after-tags-change-hook))))))
  13285. (defun org-change-tag-in-region (beg end tag off)
  13286. "Add or remove TAG for each entry in the region.
  13287. This works in the agenda, and also in an org-mode buffer."
  13288. (interactive
  13289. (list (region-beginning) (region-end)
  13290. (let ((org-last-tags-completion-table
  13291. (if (derived-mode-p 'org-mode)
  13292. (org-uniquify
  13293. (delq nil (append (org-get-buffer-tags)
  13294. (org-global-tags-completion-table))))
  13295. (org-global-tags-completion-table))))
  13296. (org-icompleting-read
  13297. "Tag: " 'org-tags-completion-function nil nil nil
  13298. 'org-tags-history))
  13299. (progn
  13300. (message "[s]et or [r]emove? ")
  13301. (equal (read-char-exclusive) ?r))))
  13302. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13303. (let ((agendap (equal major-mode 'org-agenda-mode))
  13304. l1 l2 m buf pos newhead (cnt 0))
  13305. (goto-char end)
  13306. (setq l2 (1- (org-current-line)))
  13307. (goto-char beg)
  13308. (setq l1 (org-current-line))
  13309. (loop for l from l1 to l2 do
  13310. (org-goto-line l)
  13311. (setq m (get-text-property (point) 'org-hd-marker))
  13312. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13313. (and agendap m))
  13314. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13315. pos (if agendap m (point)))
  13316. (with-current-buffer buf
  13317. (save-excursion
  13318. (save-restriction
  13319. (goto-char pos)
  13320. (setq cnt (1+ cnt))
  13321. (org-toggle-tag tag (if off 'off 'on))
  13322. (setq newhead (org-get-heading)))))
  13323. (and agendap (org-agenda-change-all-lines newhead m))))
  13324. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13325. (defun org-tags-completion-function (string predicate &optional flag)
  13326. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13327. (confirm (lambda (x) (stringp (car x)))))
  13328. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13329. (setq s1 (match-string 1 string)
  13330. s2 (match-string 2 string))
  13331. (setq s1 "" s2 string))
  13332. (cond
  13333. ((eq flag nil)
  13334. ;; try completion
  13335. (setq rtn (try-completion s2 ctable confirm))
  13336. (if (stringp rtn)
  13337. (setq rtn
  13338. (concat s1 s2 (substring rtn (length s2))
  13339. (if (and org-add-colon-after-tag-completion
  13340. (assoc rtn ctable))
  13341. ":" ""))))
  13342. rtn)
  13343. ((eq flag t)
  13344. ;; all-completions
  13345. (all-completions s2 ctable confirm))
  13346. ((eq flag 'lambda)
  13347. ;; exact match?
  13348. (assoc s2 ctable)))))
  13349. (defun org-fast-tag-insert (kwd tags face &optional end)
  13350. "Insert KDW, and the TAGS, the latter with face FACE.
  13351. Also insert END."
  13352. (insert (format "%-12s" (concat kwd ":"))
  13353. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13354. (or end "")))
  13355. (defun org-fast-tag-show-exit (flag)
  13356. (save-excursion
  13357. (org-goto-line 3)
  13358. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13359. (replace-match ""))
  13360. (when flag
  13361. (end-of-line 1)
  13362. (org-move-to-column (- (window-width) 19) t)
  13363. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13364. (defun org-set-current-tags-overlay (current prefix)
  13365. "Add an overlay to CURRENT tag with PREFIX."
  13366. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13367. (if (featurep 'xemacs)
  13368. (org-overlay-display org-tags-overlay (concat prefix s)
  13369. 'secondary-selection)
  13370. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13371. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13372. (defvar org-last-tag-selection-key nil)
  13373. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13374. "Fast tag selection with single keys.
  13375. CURRENT is the current list of tags in the headline, INHERITED is the
  13376. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13377. possibly with grouping information. TODO-TABLE is a similar table with
  13378. TODO keywords, should these have keys assigned to them.
  13379. If the keys are nil, a-z are automatically assigned.
  13380. Returns the new tags string, or nil to not change the current settings."
  13381. (let* ((fulltable (append table todo-table))
  13382. (maxlen (apply 'max (mapcar
  13383. (lambda (x)
  13384. (if (stringp (car x)) (string-width (car x)) 0))
  13385. fulltable)))
  13386. (buf (current-buffer))
  13387. (expert (eq org-fast-tag-selection-single-key 'expert))
  13388. (buffer-tags nil)
  13389. (fwidth (+ maxlen 3 1 3))
  13390. (ncol (/ (- (window-width) 4) fwidth))
  13391. (i-face 'org-done)
  13392. (c-face 'org-todo)
  13393. tg cnt e c char c1 c2 ntable tbl rtn
  13394. ov-start ov-end ov-prefix
  13395. (exit-after-next org-fast-tag-selection-single-key)
  13396. (done-keywords org-done-keywords)
  13397. groups ingroup)
  13398. (save-excursion
  13399. (beginning-of-line 1)
  13400. (if (looking-at
  13401. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13402. (setq ov-start (match-beginning 1)
  13403. ov-end (match-end 1)
  13404. ov-prefix "")
  13405. (setq ov-start (1- (point-at-eol))
  13406. ov-end (1+ ov-start))
  13407. (skip-chars-forward "^\n\r")
  13408. (setq ov-prefix
  13409. (concat
  13410. (buffer-substring (1- (point)) (point))
  13411. (if (> (current-column) org-tags-column)
  13412. " "
  13413. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13414. (move-overlay org-tags-overlay ov-start ov-end)
  13415. (save-window-excursion
  13416. (if expert
  13417. (set-buffer (get-buffer-create " *Org tags*"))
  13418. (delete-other-windows)
  13419. (split-window-vertically)
  13420. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13421. (erase-buffer)
  13422. (org-set-local 'org-done-keywords done-keywords)
  13423. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13424. (org-fast-tag-insert "Current" current c-face "\n\n")
  13425. (org-fast-tag-show-exit exit-after-next)
  13426. (org-set-current-tags-overlay current ov-prefix)
  13427. (setq tbl fulltable char ?a cnt 0)
  13428. (while (setq e (pop tbl))
  13429. (cond
  13430. ((equal (car e) :startgroup)
  13431. (push '() groups) (setq ingroup t)
  13432. (when (not (= cnt 0))
  13433. (setq cnt 0)
  13434. (insert "\n"))
  13435. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13436. ((equal (car e) :endgroup)
  13437. (setq ingroup nil cnt 0)
  13438. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13439. ((equal e '(:newline))
  13440. (when (not (= cnt 0))
  13441. (setq cnt 0)
  13442. (insert "\n")
  13443. (setq e (car tbl))
  13444. (while (equal (car tbl) '(:newline))
  13445. (insert "\n")
  13446. (setq tbl (cdr tbl)))))
  13447. ((equal e '(:grouptags)) nil)
  13448. (t
  13449. (setq tg (copy-sequence (car e)) c2 nil)
  13450. (if (cdr e)
  13451. (setq c (cdr e))
  13452. ;; automatically assign a character.
  13453. (setq c1 (string-to-char
  13454. (downcase (substring
  13455. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13456. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13457. (while (or (rassoc char ntable) (rassoc char table))
  13458. (setq char (1+ char)))
  13459. (setq c2 c1))
  13460. (setq c (or c2 char)))
  13461. (if ingroup (push tg (car groups)))
  13462. (setq tg (org-add-props tg nil 'face
  13463. (cond
  13464. ((not (assoc tg table))
  13465. (org-get-todo-face tg))
  13466. ((member tg current) c-face)
  13467. ((member tg inherited) i-face))))
  13468. (if (equal (caar tbl) :grouptags)
  13469. (org-add-props tg nil 'face 'org-tag-group))
  13470. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13471. (insert "[" c "] " tg (make-string
  13472. (- fwidth 4 (length tg)) ?\ ))
  13473. (push (cons tg c) ntable)
  13474. (when (= (setq cnt (1+ cnt)) ncol)
  13475. (insert "\n")
  13476. (if ingroup (insert " "))
  13477. (setq cnt 0)))))
  13478. (setq ntable (nreverse ntable))
  13479. (insert "\n")
  13480. (goto-char (point-min))
  13481. (if (not expert) (org-fit-window-to-buffer))
  13482. (setq rtn
  13483. (catch 'exit
  13484. (while t
  13485. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13486. (if (not groups) "no " "")
  13487. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13488. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13489. (setq org-last-tag-selection-key c)
  13490. (cond
  13491. ((= c ?\r) (throw 'exit t))
  13492. ((= c ?!)
  13493. (setq groups (not groups))
  13494. (goto-char (point-min))
  13495. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13496. ((= c ?\C-c)
  13497. (if (not expert)
  13498. (org-fast-tag-show-exit
  13499. (setq exit-after-next (not exit-after-next)))
  13500. (setq expert nil)
  13501. (delete-other-windows)
  13502. (set-window-buffer (split-window-vertically) " *Org tags*")
  13503. (org-switch-to-buffer-other-window " *Org tags*")
  13504. (org-fit-window-to-buffer)))
  13505. ((or (= c ?\C-g)
  13506. (and (= c ?q) (not (rassoc c ntable))))
  13507. (org-detach-overlay org-tags-overlay)
  13508. (setq quit-flag t))
  13509. ((= c ?\ )
  13510. (setq current nil)
  13511. (if exit-after-next (setq exit-after-next 'now)))
  13512. ((= c ?\t)
  13513. (condition-case nil
  13514. (setq tg (org-icompleting-read
  13515. "Tag: "
  13516. (or buffer-tags
  13517. (with-current-buffer buf
  13518. (org-get-buffer-tags)))))
  13519. (quit (setq tg "")))
  13520. (when (string-match "\\S-" tg)
  13521. (add-to-list 'buffer-tags (list tg))
  13522. (if (member tg current)
  13523. (setq current (delete tg current))
  13524. (push tg current)))
  13525. (if exit-after-next (setq exit-after-next 'now)))
  13526. ((setq e (rassoc c todo-table) tg (car e))
  13527. (with-current-buffer buf
  13528. (save-excursion (org-todo tg)))
  13529. (if exit-after-next (setq exit-after-next 'now)))
  13530. ((setq e (rassoc c ntable) tg (car e))
  13531. (if (member tg current)
  13532. (setq current (delete tg current))
  13533. (loop for g in groups do
  13534. (if (member tg g)
  13535. (mapc (lambda (x)
  13536. (setq current (delete x current)))
  13537. g)))
  13538. (push tg current))
  13539. (if exit-after-next (setq exit-after-next 'now))))
  13540. ;; Create a sorted list
  13541. (setq current
  13542. (sort current
  13543. (lambda (a b)
  13544. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13545. (if (eq exit-after-next 'now) (throw 'exit t))
  13546. (goto-char (point-min))
  13547. (beginning-of-line 2)
  13548. (delete-region (point) (point-at-eol))
  13549. (org-fast-tag-insert "Current" current c-face)
  13550. (org-set-current-tags-overlay current ov-prefix)
  13551. (while (re-search-forward
  13552. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13553. (setq tg (match-string 1))
  13554. (add-text-properties
  13555. (match-beginning 1) (match-end 1)
  13556. (list 'face
  13557. (cond
  13558. ((member tg current) c-face)
  13559. ((member tg inherited) i-face)
  13560. (t (get-text-property (match-beginning 1) 'face))))))
  13561. (goto-char (point-min)))))
  13562. (org-detach-overlay org-tags-overlay)
  13563. (if rtn
  13564. (mapconcat 'identity current ":")
  13565. nil))))
  13566. (defun org-get-tags-string ()
  13567. "Get the TAGS string in the current headline."
  13568. (unless (org-at-heading-p t)
  13569. (user-error "Not on a heading"))
  13570. (save-excursion
  13571. (beginning-of-line 1)
  13572. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13573. (org-match-string-no-properties 1)
  13574. "")))
  13575. (defun org-get-tags ()
  13576. "Get the list of tags specified in the current headline."
  13577. (org-split-string (org-get-tags-string) ":"))
  13578. (defun org-get-buffer-tags ()
  13579. "Get a table of all tags used in the buffer, for completion."
  13580. (let (tags)
  13581. (save-excursion
  13582. (goto-char (point-min))
  13583. (while (re-search-forward
  13584. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13585. (when (equal (char-after (point-at-bol 0)) ?*)
  13586. (mapc (lambda (x) (add-to-list 'tags x))
  13587. (org-split-string (org-match-string-no-properties 1) ":")))))
  13588. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13589. (mapcar 'list tags)))
  13590. ;;;; The mapping API
  13591. (defun org-map-entries (func &optional match scope &rest skip)
  13592. "Call FUNC at each headline selected by MATCH in SCOPE.
  13593. FUNC is a function or a lisp form. The function will be called without
  13594. arguments, with the cursor positioned at the beginning of the headline.
  13595. The return values of all calls to the function will be collected and
  13596. returned as a list.
  13597. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13598. does not need to preserve point. After evaluation, the cursor will be
  13599. moved to the end of the line (presumably of the headline of the
  13600. processed entry) and search continues from there. Under some
  13601. circumstances, this may not produce the wanted results. For example,
  13602. if you have removed (e.g. archived) the current (sub)tree it could
  13603. mean that the next entry will be skipped entirely. In such cases, you
  13604. can specify the position from where search should continue by making
  13605. FUNC set the variable `org-map-continue-from' to the desired buffer
  13606. position.
  13607. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13608. Only headlines that are matched by this query will be considered during
  13609. the iteration. When MATCH is nil or t, all headlines will be
  13610. visited by the iteration.
  13611. SCOPE determines the scope of this command. It can be any of:
  13612. nil The current buffer, respecting the restriction if any
  13613. tree The subtree started with the entry at point
  13614. region The entries within the active region, if any
  13615. region-start-level
  13616. The entries within the active region, but only those at
  13617. the same level than the first one.
  13618. file The current buffer, without restriction
  13619. file-with-archives
  13620. The current buffer, and any archives associated with it
  13621. agenda All agenda files
  13622. agenda-with-archives
  13623. All agenda files with any archive files associated with them
  13624. \(file1 file2 ...)
  13625. If this is a list, all files in the list will be scanned
  13626. The remaining args are treated as settings for the skipping facilities of
  13627. the scanner. The following items can be given here:
  13628. archive skip trees with the archive tag
  13629. comment skip trees with the COMMENT keyword
  13630. function or Emacs Lisp form:
  13631. will be used as value for `org-agenda-skip-function', so
  13632. whenever the function returns a position, FUNC will not be
  13633. called for that entry and search will continue from the
  13634. position returned
  13635. If your function needs to retrieve the tags including inherited tags
  13636. at the *current* entry, you can use the value of the variable
  13637. `org-scanner-tags' which will be much faster than getting the value
  13638. with `org-get-tags-at'. If your function gets properties with
  13639. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13640. to t around the call to `org-entry-properties' to get the same speedup.
  13641. Note that if your function moves around to retrieve tags and properties at
  13642. a *different* entry, you cannot use these techniques."
  13643. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13644. (not (org-region-active-p)))
  13645. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13646. (org-agenda-skip-archived-trees (memq 'archive skip))
  13647. (org-agenda-skip-comment-trees (memq 'comment skip))
  13648. (org-agenda-skip-function
  13649. (car (org-delete-all '(comment archive) skip)))
  13650. (org-tags-match-list-sublevels t)
  13651. (start-level (eq scope 'region-start-level))
  13652. matcher file res
  13653. org-todo-keywords-for-agenda
  13654. org-done-keywords-for-agenda
  13655. org-todo-keyword-alist-for-agenda
  13656. org-drawers-for-agenda
  13657. org-tag-alist-for-agenda
  13658. todo-only)
  13659. (cond
  13660. ((eq match t) (setq matcher t))
  13661. ((eq match nil) (setq matcher t))
  13662. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13663. (save-window-excursion
  13664. (save-restriction
  13665. (cond ((eq scope 'tree)
  13666. (org-back-to-heading t)
  13667. (org-narrow-to-subtree)
  13668. (setq scope nil))
  13669. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13670. (org-region-active-p))
  13671. ;; If needed, set start-level to a string like "2"
  13672. (when start-level
  13673. (save-excursion
  13674. (goto-char (region-beginning))
  13675. (unless (org-at-heading-p) (outline-next-heading))
  13676. (setq start-level (org-current-level))))
  13677. (narrow-to-region (region-beginning)
  13678. (save-excursion
  13679. (goto-char (region-end))
  13680. (unless (and (bolp) (org-at-heading-p))
  13681. (outline-next-heading))
  13682. (point)))
  13683. (setq scope nil)))
  13684. (if (not scope)
  13685. (progn
  13686. (org-agenda-prepare-buffers
  13687. (list (buffer-file-name (current-buffer))))
  13688. (setq res (org-scan-tags func matcher todo-only start-level)))
  13689. ;; Get the right scope
  13690. (cond
  13691. ((and scope (listp scope) (symbolp (car scope)))
  13692. (setq scope (eval scope)))
  13693. ((eq scope 'agenda)
  13694. (setq scope (org-agenda-files t)))
  13695. ((eq scope 'agenda-with-archives)
  13696. (setq scope (org-agenda-files t))
  13697. (setq scope (org-add-archive-files scope)))
  13698. ((eq scope 'file)
  13699. (setq scope (list (buffer-file-name))))
  13700. ((eq scope 'file-with-archives)
  13701. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13702. (org-agenda-prepare-buffers scope)
  13703. (while (setq file (pop scope))
  13704. (with-current-buffer (org-find-base-buffer-visiting file)
  13705. (save-excursion
  13706. (save-restriction
  13707. (widen)
  13708. (goto-char (point-min))
  13709. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13710. res)))
  13711. ;;;; Properties
  13712. ;;; Setting and retrieving properties
  13713. (defconst org-special-properties
  13714. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13715. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13716. "The special properties valid in Org-mode.
  13717. These are properties that are not defined in the property drawer,
  13718. but in some other way.")
  13719. (defconst org-default-properties
  13720. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13721. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13722. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13723. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13724. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13725. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13726. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13727. "Some properties that are used by Org-mode for various purposes.
  13728. Being in this list makes sure that they are offered for completion.")
  13729. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13730. "Regular expression matching the first line of a property drawer.")
  13731. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13732. "Regular expression matching the last line of a property drawer.")
  13733. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13734. "Regular expression matching the first line of a property drawer.")
  13735. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13736. "Regular expression matching the first line of a property drawer.")
  13737. (defconst org-property-drawer-re
  13738. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13739. org-property-end-re "\\)\n?")
  13740. "Matches an entire property drawer.")
  13741. (defconst org-clock-drawer-re
  13742. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13743. org-property-end-re "\\)\n?")
  13744. "Matches an entire clock drawer.")
  13745. (defun org-property-action ()
  13746. "Do an action on properties."
  13747. (interactive)
  13748. (let (c)
  13749. (org-at-property-p)
  13750. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13751. (setq c (read-char-exclusive))
  13752. (cond
  13753. ((equal c ?s)
  13754. (call-interactively 'org-set-property))
  13755. ((equal c ?d)
  13756. (call-interactively 'org-delete-property))
  13757. ((equal c ?D)
  13758. (call-interactively 'org-delete-property-globally))
  13759. ((equal c ?c)
  13760. (call-interactively 'org-compute-property-at-point))
  13761. (t (user-error "No such property action %c" c)))))
  13762. (defun org-inc-effort ()
  13763. "Increment the value of the effort property in the current entry."
  13764. (interactive)
  13765. (org-set-effort nil t))
  13766. (defvar org-clock-effort) ;; Defined in org-clock.el
  13767. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13768. (defun org-set-effort (&optional value increment)
  13769. "Set the effort property of the current entry.
  13770. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13771. 10th allowed value.
  13772. When INCREMENT is non-nil, set the property to the next allowed value."
  13773. (interactive "P")
  13774. (if (equal value 0) (setq value 10))
  13775. (let* ((completion-ignore-case t)
  13776. (prop org-effort-property)
  13777. (cur (org-entry-get nil prop))
  13778. (allowed (org-property-get-allowed-values nil prop 'table))
  13779. (existing (mapcar 'list (org-property-values prop)))
  13780. (heading (nth 4 (org-heading-components)))
  13781. rpl
  13782. (val (cond
  13783. ((stringp value) value)
  13784. ((and allowed (integerp value))
  13785. (or (car (nth (1- value) allowed))
  13786. (car (org-last allowed))))
  13787. ((and allowed increment)
  13788. (or (caadr (member (list cur) allowed))
  13789. (user-error "Allowed effort values are not set")))
  13790. (allowed
  13791. (message "Select 1-9,0, [RET%s]: %s"
  13792. (if cur (concat "=" cur) "")
  13793. (mapconcat 'car allowed " "))
  13794. (setq rpl (read-char-exclusive))
  13795. (if (equal rpl ?\r)
  13796. cur
  13797. (setq rpl (- rpl ?0))
  13798. (if (equal rpl 0) (setq rpl 10))
  13799. (if (and (> rpl 0) (<= rpl (length allowed)))
  13800. (car (nth (1- rpl) allowed))
  13801. (org-completing-read "Effort: " allowed nil))))
  13802. (t
  13803. (let (org-completion-use-ido org-completion-use-iswitchb)
  13804. (org-completing-read
  13805. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13806. (concat "[" cur "]") "")
  13807. ": ")
  13808. existing nil nil "" nil cur))))))
  13809. (unless (equal (org-entry-get nil prop) val)
  13810. (org-entry-put nil prop val))
  13811. (save-excursion
  13812. (org-back-to-heading t)
  13813. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13814. (when (string= heading org-clock-current-task)
  13815. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13816. (org-clock-update-mode-line))
  13817. (message "%s is now %s" prop val)))
  13818. (defun org-at-property-p ()
  13819. "Is cursor inside a property drawer?"
  13820. (save-excursion
  13821. (when (equal 'node-property (car (org-element-at-point)))
  13822. (beginning-of-line 1)
  13823. (looking-at org-property-re))))
  13824. (defun org-get-property-block (&optional beg end force)
  13825. "Return the (beg . end) range of the body of the property drawer.
  13826. BEG and END are the beginning and end of the current subtree, or of
  13827. the part before the first headline. If they are not given, they will
  13828. be found. If the drawer does not exist and FORCE is non-nil, create
  13829. the drawer."
  13830. (catch 'exit
  13831. (save-excursion
  13832. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13833. (progn (org-back-to-heading t) (point))))
  13834. (end (or end (and (not (outline-next-heading)) (point-max))
  13835. (point))))
  13836. (goto-char beg)
  13837. (if (re-search-forward org-property-start-re end t)
  13838. (setq beg (1+ (match-end 0)))
  13839. (if force
  13840. (save-excursion
  13841. (org-insert-property-drawer)
  13842. (setq end (progn (outline-next-heading) (point))))
  13843. (throw 'exit nil))
  13844. (goto-char beg)
  13845. (if (re-search-forward org-property-start-re end t)
  13846. (setq beg (1+ (match-end 0)))))
  13847. (if (re-search-forward org-property-end-re end t)
  13848. (setq end (match-beginning 0))
  13849. (or force (throw 'exit nil))
  13850. (goto-char beg)
  13851. (setq end beg)
  13852. (org-indent-line)
  13853. (insert ":END:\n"))
  13854. (cons beg end)))))
  13855. (defun org-entry-properties (&optional pom which specific)
  13856. "Get all properties of the entry at point-or-marker POM.
  13857. This includes the TODO keyword, the tags, time strings for deadline,
  13858. scheduled, and clocking, and any additional properties defined in the
  13859. entry. The return value is an alist, keys may occur multiple times
  13860. if the property key was used several times.
  13861. POM may also be nil, in which case the current entry is used.
  13862. If WHICH is nil or `all', get all properties. If WHICH is
  13863. `special' or `standard', only get that subclass. If WHICH
  13864. is a string only get exactly this property. SPECIFIC can be a string, the
  13865. specific property we are interested in. Specifying it can speed
  13866. things up because then unnecessary parsing is avoided."
  13867. (setq which (or which 'all))
  13868. (org-with-point-at pom
  13869. (let ((clockstr (substring org-clock-string 0 -1))
  13870. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13871. (case-fold-search nil)
  13872. beg end range props sum-props key key1 value string clocksum clocksumt)
  13873. (save-excursion
  13874. (when (condition-case nil
  13875. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13876. (error nil))
  13877. (setq beg (point))
  13878. (setq sum-props (get-text-property (point) 'org-summaries))
  13879. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13880. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13881. (outline-next-heading)
  13882. (setq end (point))
  13883. (when (memq which '(all special))
  13884. ;; Get the special properties, like TODO and tags
  13885. (goto-char beg)
  13886. (when (and (or (not specific) (string= specific "TODO"))
  13887. (looking-at org-todo-line-regexp) (match-end 2))
  13888. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13889. (when (and (or (not specific) (string= specific "PRIORITY"))
  13890. (looking-at org-priority-regexp))
  13891. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13892. (when (or (not specific) (string= specific "FILE"))
  13893. (push (cons "FILE" buffer-file-name) props))
  13894. (when (and (or (not specific) (string= specific "TAGS"))
  13895. (setq value (org-get-tags-string))
  13896. (string-match "\\S-" value))
  13897. (push (cons "TAGS" value) props))
  13898. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13899. (setq value (org-get-tags-at)))
  13900. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13901. ":"))
  13902. props))
  13903. (when (or (not specific) (string= specific "BLOCKED"))
  13904. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13905. (when (or (not specific)
  13906. (member specific
  13907. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13908. "TIMESTAMP" "TIMESTAMP_IA")))
  13909. (catch 'match
  13910. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13911. (setq key (if (match-end 1)
  13912. (substring (org-match-string-no-properties 1)
  13913. 0 -1))
  13914. string (if (equal key clockstr)
  13915. (org-trim
  13916. (buffer-substring-no-properties
  13917. (match-beginning 3) (goto-char
  13918. (point-at-eol))))
  13919. (substring (org-match-string-no-properties 3)
  13920. 1 -1)))
  13921. ;; Get the correct property name from the key. This is
  13922. ;; necessary if the user has configured time keywords.
  13923. (setq key1 (concat key ":"))
  13924. (cond
  13925. ((not key)
  13926. (setq key
  13927. (if (= (char-after (match-beginning 3)) ?\[)
  13928. "TIMESTAMP_IA" "TIMESTAMP")))
  13929. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13930. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13931. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13932. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13933. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13934. (progn
  13935. (push (cons key string) props)
  13936. ;; no need to search further if match is found
  13937. (throw 'match t))
  13938. (when (or (equal key "CLOCK") (not (assoc key props)))
  13939. (push (cons key string) props)))))))
  13940. (when (memq which '(all standard))
  13941. ;; Get the standard properties, like :PROP: ...
  13942. (setq range (org-get-property-block beg end))
  13943. (when range
  13944. (goto-char (car range))
  13945. (while (re-search-forward org-property-re
  13946. (cdr range) t)
  13947. (setq key (org-match-string-no-properties 2)
  13948. value (org-trim (or (org-match-string-no-properties 3) "")))
  13949. (unless (member key excluded)
  13950. (push (cons key (or value "")) props)))))
  13951. (if clocksum
  13952. (push (cons "CLOCKSUM"
  13953. (org-columns-number-to-string (/ (float clocksum) 60.)
  13954. 'add_times))
  13955. props))
  13956. (if clocksumt
  13957. (push (cons "CLOCKSUM_T"
  13958. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13959. 'add_times))
  13960. props))
  13961. (unless (assoc "CATEGORY" props)
  13962. (push (cons "CATEGORY" (org-get-category)) props))
  13963. (append sum-props (nreverse props)))))))
  13964. (defun org-entry-get (pom property &optional inherit literal-nil)
  13965. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13966. If INHERIT is non-nil and the entry does not have the property,
  13967. then also check higher levels of the hierarchy.
  13968. If INHERIT is the symbol `selective', use inheritance only if the setting
  13969. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13970. If the property is present but empty, the return value is the empty string.
  13971. If the property is not present at all, nil is returned.
  13972. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13973. do not interpret it as the list atom nil. This is used for inheritance
  13974. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13975. (org-with-point-at pom
  13976. (if (and inherit (if (eq inherit 'selective)
  13977. (org-property-inherit-p property)
  13978. t))
  13979. (org-entry-get-with-inheritance property literal-nil)
  13980. (if (member property org-special-properties)
  13981. ;; We need a special property. Use `org-entry-properties' to
  13982. ;; retrieve it, but specify the wanted property
  13983. (cdr (assoc property (org-entry-properties nil 'special property)))
  13984. (let ((range (org-get-property-block)))
  13985. (when (and range (not (eq (car range) (cdr range))))
  13986. (let* ((props (list (or (assoc property org-file-properties)
  13987. (assoc property org-global-properties)
  13988. (assoc property org-global-properties-fixed))))
  13989. (ap (lambda (key)
  13990. (when (re-search-forward
  13991. (org-re-property key) (cdr range) t)
  13992. (setq props
  13993. (org-update-property-plist
  13994. key
  13995. (if (match-end 3)
  13996. (org-match-string-no-properties 3) "")
  13997. props)))))
  13998. val)
  13999. (goto-char (car range))
  14000. (funcall ap property)
  14001. (goto-char (car range))
  14002. (while (funcall ap (concat property "+")))
  14003. (setq val (cdr (assoc property props)))
  14004. (when val (if literal-nil val (org-not-nil val))))))))))
  14005. (defun org-property-or-variable-value (var &optional inherit)
  14006. "Check if there is a property fixing the value of VAR.
  14007. If yes, return this value. If not, return the current value of the variable."
  14008. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14009. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14010. (read prop)
  14011. (symbol-value var))))
  14012. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  14013. "Delete the property PROPERTY from entry at point-or-marker POM.
  14014. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14015. an empty drawer to delete."
  14016. (org-with-point-at pom
  14017. (if (member property org-special-properties)
  14018. nil ; cannot delete these properties.
  14019. (let ((range (org-get-property-block)))
  14020. (if (and range
  14021. (goto-char (car range))
  14022. (re-search-forward
  14023. (org-re-property property)
  14024. (cdr range) t))
  14025. (progn
  14026. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14027. (and delete-empty-drawer
  14028. (org-remove-empty-drawer-at
  14029. delete-empty-drawer (car range)))
  14030. t)
  14031. nil)))))
  14032. ;; Multi-values properties are properties that contain multiple values
  14033. ;; These values are assumed to be single words, separated by whitespace.
  14034. (defun org-entry-add-to-multivalued-property (pom property value)
  14035. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14036. (let* ((old (org-entry-get pom property))
  14037. (values (and old (org-split-string old "[ \t]"))))
  14038. (setq value (org-entry-protect-space value))
  14039. (unless (member value values)
  14040. (setq values (append values (list value)))
  14041. (org-entry-put pom property
  14042. (mapconcat 'identity values " ")))))
  14043. (defun org-entry-remove-from-multivalued-property (pom property value)
  14044. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14045. (let* ((old (org-entry-get pom property))
  14046. (values (and old (org-split-string old "[ \t]"))))
  14047. (setq value (org-entry-protect-space value))
  14048. (when (member value values)
  14049. (setq values (delete value values))
  14050. (org-entry-put pom property
  14051. (mapconcat 'identity values " ")))))
  14052. (defun org-entry-member-in-multivalued-property (pom property value)
  14053. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14054. (let* ((old (org-entry-get pom property))
  14055. (values (and old (org-split-string old "[ \t]"))))
  14056. (setq value (org-entry-protect-space value))
  14057. (member value values)))
  14058. (defun org-entry-get-multivalued-property (pom property)
  14059. "Return a list of values in a multivalued property."
  14060. (let* ((value (org-entry-get pom property))
  14061. (values (and value (org-split-string value "[ \t]"))))
  14062. (mapcar 'org-entry-restore-space values)))
  14063. (defun org-entry-put-multivalued-property (pom property &rest values)
  14064. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14065. VALUES should be a list of strings. Spaces will be protected."
  14066. (org-entry-put pom property
  14067. (mapconcat 'org-entry-protect-space values " "))
  14068. (let* ((value (org-entry-get pom property))
  14069. (values (and value (org-split-string value "[ \t]"))))
  14070. (mapcar 'org-entry-restore-space values)))
  14071. (defun org-entry-protect-space (s)
  14072. "Protect spaces and newline in string S."
  14073. (while (string-match " " s)
  14074. (setq s (replace-match "%20" t t s)))
  14075. (while (string-match "\n" s)
  14076. (setq s (replace-match "%0A" t t s)))
  14077. s)
  14078. (defun org-entry-restore-space (s)
  14079. "Restore spaces and newline in string S."
  14080. (while (string-match "%20" s)
  14081. (setq s (replace-match " " t t s)))
  14082. (while (string-match "%0A" s)
  14083. (setq s (replace-match "\n" t t s)))
  14084. s)
  14085. (defvar org-entry-property-inherited-from (make-marker)
  14086. "Marker pointing to the entry from where a property was inherited.
  14087. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14088. location of the entry where the inheritance search matched. If there was
  14089. no match, the marker will point nowhere.
  14090. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14091. is set.")
  14092. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14093. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14094. The search will stop at the first ancestor which has the property defined.
  14095. If the value found is \"nil\", return nil to show that the property
  14096. should be considered as undefined (this is the meaning of nil here).
  14097. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14098. (move-marker org-entry-property-inherited-from nil)
  14099. (let (tmp)
  14100. (save-excursion
  14101. (save-restriction
  14102. (widen)
  14103. (catch 'ex
  14104. (while t
  14105. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14106. (or (ignore-errors (org-back-to-heading t))
  14107. (goto-char (point-min)))
  14108. (move-marker org-entry-property-inherited-from (point))
  14109. (throw 'ex tmp))
  14110. (or (ignore-errors (org-up-heading-safe))
  14111. (throw 'ex nil))))))
  14112. (setq tmp (or tmp
  14113. (cdr (assoc property org-file-properties))
  14114. (cdr (assoc property org-global-properties))
  14115. (cdr (assoc property org-global-properties-fixed))))
  14116. (if literal-nil tmp (org-not-nil tmp))))
  14117. (defvar org-property-changed-functions nil
  14118. "Hook called when the value of a property has changed.
  14119. Each hook function should accept two arguments, the name of the property
  14120. and the new value.")
  14121. (defun org-entry-put (pom property value)
  14122. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14123. (org-with-point-at pom
  14124. (org-back-to-heading t)
  14125. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14126. range)
  14127. (cond
  14128. ((equal property "TODO")
  14129. (when (and (stringp value) (string-match "\\S-" value)
  14130. (not (member value org-todo-keywords-1)))
  14131. (user-error "\"%s\" is not a valid TODO state" value))
  14132. (if (or (not value)
  14133. (not (string-match "\\S-" value)))
  14134. (setq value 'none))
  14135. (org-todo value)
  14136. (org-set-tags nil 'align))
  14137. ((equal property "PRIORITY")
  14138. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14139. (string-to-char value) ?\ ))
  14140. (org-set-tags nil 'align))
  14141. ((equal property "CLOCKSUM")
  14142. (if (not (re-search-forward
  14143. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14144. (error "Cannot find a clock log")
  14145. (goto-char (- (match-end 1) 2))
  14146. (cond
  14147. ((eq value 'earlier) (org-timestamp-down))
  14148. ((eq value 'later) (org-timestamp-up)))
  14149. (org-clock-sum-current-item)))
  14150. ((equal property "SCHEDULED")
  14151. (if (re-search-forward org-scheduled-time-regexp end t)
  14152. (cond
  14153. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14154. ((eq value 'later) (org-timestamp-change 1 'day))
  14155. (t (call-interactively 'org-schedule)))
  14156. (call-interactively 'org-schedule)))
  14157. ((equal property "DEADLINE")
  14158. (if (re-search-forward org-deadline-time-regexp end t)
  14159. (cond
  14160. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14161. ((eq value 'later) (org-timestamp-change 1 'day))
  14162. (t (call-interactively 'org-deadline)))
  14163. (call-interactively 'org-deadline)))
  14164. ((member property org-special-properties)
  14165. (error "The %s property can not yet be set with `org-entry-put'"
  14166. property))
  14167. (t ; a non-special property
  14168. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14169. (setq range (org-get-property-block beg end 'force))
  14170. (goto-char (car range))
  14171. (if (re-search-forward
  14172. (org-re-property property) (cdr range) t)
  14173. (progn
  14174. (delete-region (match-beginning 0) (match-end 0))
  14175. (goto-char (match-beginning 0)))
  14176. (goto-char (cdr range))
  14177. (insert "\n")
  14178. (backward-char 1)
  14179. (org-indent-line))
  14180. (insert ":" property ":")
  14181. (and value (insert " " value))
  14182. (org-indent-line)))))
  14183. (run-hook-with-args 'org-property-changed-functions property value)))
  14184. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14185. "Get all property keys in the current buffer.
  14186. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14187. like tags and TODO state.
  14188. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14189. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14190. and others.
  14191. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14192. formats in the current buffer."
  14193. (let (rtn range cfmt s p)
  14194. (save-excursion
  14195. (save-restriction
  14196. (widen)
  14197. (goto-char (point-min))
  14198. (while (re-search-forward org-property-start-re nil t)
  14199. (setq range (org-get-property-block))
  14200. (goto-char (car range))
  14201. (while (re-search-forward org-property-re
  14202. (cdr range) t)
  14203. (add-to-list 'rtn (org-match-string-no-properties 2)))
  14204. (outline-next-heading))))
  14205. (when include-specials
  14206. (setq rtn (append org-special-properties rtn)))
  14207. (when include-defaults
  14208. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14209. (add-to-list 'rtn org-effort-property))
  14210. (when include-columns
  14211. (save-excursion
  14212. (save-restriction
  14213. (widen)
  14214. (goto-char (point-min))
  14215. (while (re-search-forward
  14216. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14217. nil t)
  14218. (setq cfmt (match-string 2) s 0)
  14219. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14220. cfmt s)
  14221. (setq s (match-end 0)
  14222. p (match-string 1 cfmt))
  14223. (unless (or (equal p "ITEM")
  14224. (member p org-special-properties))
  14225. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14226. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14227. (defun org-property-values (key)
  14228. "Return a list of all values of property KEY in the current buffer."
  14229. (save-excursion
  14230. (save-restriction
  14231. (widen)
  14232. (goto-char (point-min))
  14233. (let ((re (org-re-property key))
  14234. values)
  14235. (while (re-search-forward re nil t)
  14236. (add-to-list 'values (org-trim (match-string 3))))
  14237. (delete "" values)))))
  14238. (defun org-insert-property-drawer ()
  14239. "Insert a property drawer into the current entry."
  14240. (org-back-to-heading t)
  14241. (looking-at org-outline-regexp)
  14242. (let ((indent (if org-adapt-indentation
  14243. (- (match-end 0) (match-beginning 0))
  14244. 0))
  14245. (beg (point))
  14246. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14247. end hiddenp)
  14248. (outline-next-heading)
  14249. (setq end (point))
  14250. (goto-char beg)
  14251. (while (re-search-forward re end t))
  14252. (setq hiddenp (outline-invisible-p))
  14253. (end-of-line 1)
  14254. (and (equal (char-after) ?\n) (forward-char 1))
  14255. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14256. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14257. ;; just skip this line
  14258. (beginning-of-line 2)
  14259. ;; Drawer start, find the end
  14260. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14261. (beginning-of-line 1)))
  14262. (org-skip-over-state-notes)
  14263. (skip-chars-backward " \t\n\r")
  14264. (if (eq (char-before) ?*) (forward-char 1))
  14265. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14266. (beginning-of-line 0)
  14267. (org-indent-to-column indent)
  14268. (beginning-of-line 2)
  14269. (org-indent-to-column indent)
  14270. (beginning-of-line 0)
  14271. (if hiddenp
  14272. (save-excursion
  14273. (org-back-to-heading t)
  14274. (hide-entry))
  14275. (org-flag-drawer t))))
  14276. (defun org-insert-drawer (&optional arg drawer)
  14277. "Insert a drawer at point.
  14278. Optional argument DRAWER, when non-nil, is a string representing
  14279. drawer's name. Otherwise, the user is prompted for a name.
  14280. If a region is active, insert the drawer around that region
  14281. instead.
  14282. Point is left between drawer's boundaries."
  14283. (interactive "P")
  14284. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14285. "LOGBOOK"))
  14286. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14287. ;; internally by Org. They are meant to be inserted
  14288. ;; automatically.
  14289. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14290. ;; Remove system drawers from list. Note: For some reason,
  14291. ;; `org-completing-read' ignores the predicate while
  14292. ;; `completing-read' handles it fine.
  14293. (drawer (if arg "PROPERTIES"
  14294. (or drawer
  14295. (completing-read
  14296. "Drawer: " org-drawers
  14297. (lambda (d) (not (member d system-drawers))))))))
  14298. (cond
  14299. ;; With C-u, fall back on `org-insert-property-drawer'
  14300. (arg (org-insert-property-drawer))
  14301. ;; With an active region, insert a drawer at point.
  14302. ((not (org-region-active-p))
  14303. (progn
  14304. (unless (bolp) (insert "\n"))
  14305. (insert (format ":%s:\n\n:END:\n" drawer))
  14306. (forward-line -2)))
  14307. ;; Otherwise, insert the drawer at point
  14308. (t
  14309. (let ((rbeg (region-beginning))
  14310. (rend (copy-marker (region-end))))
  14311. (unwind-protect
  14312. (progn
  14313. (goto-char rbeg)
  14314. (beginning-of-line)
  14315. (when (save-excursion
  14316. (re-search-forward org-outline-regexp-bol rend t))
  14317. (user-error "Drawers cannot contain headlines"))
  14318. ;; Position point at the beginning of the first
  14319. ;; non-blank line in region. Insert drawer's opening
  14320. ;; there, then indent it.
  14321. (org-skip-whitespace)
  14322. (beginning-of-line)
  14323. (insert ":" drawer ":\n")
  14324. (forward-line -1)
  14325. (indent-for-tab-command)
  14326. ;; Move point to the beginning of the first blank line
  14327. ;; after the last non-blank line in region. Insert
  14328. ;; drawer's closing, then indent it.
  14329. (goto-char rend)
  14330. (skip-chars-backward " \r\t\n")
  14331. (insert "\n:END:")
  14332. (deactivate-mark t)
  14333. (indent-for-tab-command)
  14334. (unless (eolp) (insert "\n")))
  14335. ;; Clear marker, whatever the outcome of insertion is.
  14336. (set-marker rend nil)))))))
  14337. (defvar org-property-set-functions-alist nil
  14338. "Property set function alist.
  14339. Each entry should have the following format:
  14340. (PROPERTY . READ-FUNCTION)
  14341. The read function will be called with the same argument as
  14342. `org-completing-read'.")
  14343. (defun org-set-property-function (property)
  14344. "Get the function that should be used to set PROPERTY.
  14345. This is computed according to `org-property-set-functions-alist'."
  14346. (or (cdr (assoc property org-property-set-functions-alist))
  14347. 'org-completing-read))
  14348. (defun org-read-property-value (property)
  14349. "Read PROPERTY value from user."
  14350. (let* ((completion-ignore-case t)
  14351. (allowed (org-property-get-allowed-values nil property 'table))
  14352. (cur (org-entry-get nil property))
  14353. (prompt (concat property " value"
  14354. (if (and cur (string-match "\\S-" cur))
  14355. (concat " [" cur "]") "") ": "))
  14356. (set-function (org-set-property-function property))
  14357. (val (if allowed
  14358. (funcall set-function prompt allowed nil
  14359. (not (get-text-property 0 'org-unrestricted
  14360. (caar allowed))))
  14361. (let (org-completion-use-ido org-completion-use-iswitchb)
  14362. (funcall set-function prompt
  14363. (mapcar 'list (org-property-values property))
  14364. nil nil "" nil cur)))))
  14365. (if (equal val "")
  14366. cur
  14367. val)))
  14368. (defvar org-last-set-property nil)
  14369. (defvar org-last-set-property-value nil)
  14370. (defun org-read-property-name ()
  14371. "Read a property name."
  14372. (let* ((completion-ignore-case t)
  14373. (keys (org-buffer-property-keys nil t t))
  14374. (default-prop (or (save-excursion
  14375. (save-match-data
  14376. (beginning-of-line)
  14377. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14378. (null (string= (match-string 1) "END"))
  14379. (match-string 1))))
  14380. org-last-set-property))
  14381. (property (org-icompleting-read
  14382. (concat "Property"
  14383. (if default-prop (concat " [" default-prop "]") "")
  14384. ": ")
  14385. (mapcar 'list keys)
  14386. nil nil nil nil
  14387. default-prop)))
  14388. (if (member property keys)
  14389. property
  14390. (or (cdr (assoc (downcase property)
  14391. (mapcar (lambda (x) (cons (downcase x) x))
  14392. keys)))
  14393. property))))
  14394. (defun org-set-property-and-value (use-last)
  14395. "Allow to set [PROPERTY]: [value] direction from prompt.
  14396. When use-default, don't even ask, just use the last
  14397. \"[PROPERTY]: [value]\" string from the history."
  14398. (interactive "P")
  14399. (let* ((completion-ignore-case t)
  14400. (pv (or (and use-last org-last-set-property-value)
  14401. (org-completing-read
  14402. "Enter a \"[Property]: [value]\" pair: "
  14403. nil nil nil nil nil
  14404. org-last-set-property-value)))
  14405. prop val)
  14406. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14407. (setq prop (match-string 1 pv)
  14408. val (match-string 2 pv))
  14409. (org-set-property prop val))))
  14410. (defun org-set-property (property value)
  14411. "In the current entry, set PROPERTY to VALUE.
  14412. When called interactively, this will prompt for a property name, offering
  14413. completion on existing and default properties. And then it will prompt
  14414. for a value, offering completion either on allowed values (via an inherited
  14415. xxx_ALL property) or on existing values in other instances of this property
  14416. in the current file."
  14417. (interactive (list nil nil))
  14418. (let* ((property (or property (org-read-property-name)))
  14419. (value (or value (org-read-property-value property)))
  14420. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14421. (setq org-last-set-property property)
  14422. (setq org-last-set-property-value (concat property ": " value))
  14423. ;; Possibly postprocess the inserted value:
  14424. (when fn (setq value (funcall fn value)))
  14425. (unless (equal (org-entry-get nil property) value)
  14426. (org-entry-put nil property value))))
  14427. (defun org-delete-property (property &optional delete-empty-drawer)
  14428. "In the current entry, delete PROPERTY.
  14429. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14430. an empty drawer to delete."
  14431. (interactive
  14432. (let* ((completion-ignore-case t)
  14433. (prop (org-icompleting-read "Property: "
  14434. (org-entry-properties nil 'standard))))
  14435. (list prop)))
  14436. (message "Property %s %s" property
  14437. (if (org-entry-delete nil property delete-empty-drawer)
  14438. "deleted"
  14439. "was not present in the entry")))
  14440. (defun org-delete-property-globally (property)
  14441. "Remove PROPERTY globally, from all entries."
  14442. (interactive
  14443. (let* ((completion-ignore-case t)
  14444. (prop (org-icompleting-read
  14445. "Globally remove property: "
  14446. (mapcar 'list (org-buffer-property-keys)))))
  14447. (list prop)))
  14448. (save-excursion
  14449. (save-restriction
  14450. (widen)
  14451. (goto-char (point-min))
  14452. (let ((cnt 0))
  14453. (while (re-search-forward
  14454. (org-re-property property)
  14455. nil t)
  14456. (setq cnt (1+ cnt))
  14457. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14458. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14459. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14460. (defun org-compute-property-at-point ()
  14461. "Compute the property at point.
  14462. This looks for an enclosing column format, extracts the operator and
  14463. then applies it to the property in the column format's scope."
  14464. (interactive)
  14465. (unless (org-at-property-p)
  14466. (user-error "Not at a property"))
  14467. (let ((prop (org-match-string-no-properties 2)))
  14468. (org-columns-get-format-and-top-level)
  14469. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14470. (user-error "No operator defined for property %s" prop))
  14471. (org-columns-compute prop)))
  14472. (defvar org-property-allowed-value-functions nil
  14473. "Hook for functions supplying allowed values for a specific property.
  14474. The functions must take a single argument, the name of the property, and
  14475. return a flat list of allowed values. If \":ETC\" is one of
  14476. the values, this means that these values are intended as defaults for
  14477. completion, but that other values should be allowed too.
  14478. The functions must return nil if they are not responsible for this
  14479. property.")
  14480. (defun org-property-get-allowed-values (pom property &optional table)
  14481. "Get allowed values for the property PROPERTY.
  14482. When TABLE is non-nil, return an alist that can directly be used for
  14483. completion."
  14484. (let (vals)
  14485. (cond
  14486. ((equal property "TODO")
  14487. (setq vals (org-with-point-at pom
  14488. (append org-todo-keywords-1 '("")))))
  14489. ((equal property "PRIORITY")
  14490. (let ((n org-lowest-priority))
  14491. (while (>= n org-highest-priority)
  14492. (push (char-to-string n) vals)
  14493. (setq n (1- n)))))
  14494. ((member property org-special-properties))
  14495. ((setq vals (run-hook-with-args-until-success
  14496. 'org-property-allowed-value-functions property)))
  14497. (t
  14498. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14499. (when (and vals (string-match "\\S-" vals))
  14500. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14501. (setq vals (mapcar (lambda (x)
  14502. (cond ((stringp x) x)
  14503. ((numberp x) (number-to-string x))
  14504. ((symbolp x) (symbol-name x))
  14505. (t "???")))
  14506. vals)))))
  14507. (when (member ":ETC" vals)
  14508. (setq vals (remove ":ETC" vals))
  14509. (org-add-props (car vals) '(org-unrestricted t)))
  14510. (if table (mapcar 'list vals) vals)))
  14511. (defun org-property-previous-allowed-value (&optional previous)
  14512. "Switch to the next allowed value for this property."
  14513. (interactive)
  14514. (org-property-next-allowed-value t))
  14515. (defun org-property-next-allowed-value (&optional previous)
  14516. "Switch to the next allowed value for this property."
  14517. (interactive)
  14518. (unless (org-at-property-p)
  14519. (user-error "Not at a property"))
  14520. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14521. (key (match-string 2))
  14522. (value (match-string 3))
  14523. (allowed (or (org-property-get-allowed-values (point) key)
  14524. (and (member value '("[ ]" "[-]" "[X]"))
  14525. '("[ ]" "[X]"))))
  14526. (heading (save-match-data (nth 4 (org-heading-components))))
  14527. nval)
  14528. (unless allowed
  14529. (user-error "Allowed values for this property have not been defined"))
  14530. (if previous (setq allowed (reverse allowed)))
  14531. (if (member value allowed)
  14532. (setq nval (car (cdr (member value allowed)))))
  14533. (setq nval (or nval (car allowed)))
  14534. (if (equal nval value)
  14535. (user-error "Only one allowed value for this property"))
  14536. (org-at-property-p)
  14537. (replace-match (concat " :" key ": " nval) t t)
  14538. (org-indent-line)
  14539. (beginning-of-line 1)
  14540. (skip-chars-forward " \t")
  14541. (when (equal prop org-effort-property)
  14542. (save-excursion
  14543. (org-back-to-heading t)
  14544. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14545. (when (string= org-clock-current-task heading)
  14546. (setq org-clock-effort nval)
  14547. (org-clock-update-mode-line)))
  14548. (run-hook-with-args 'org-property-changed-functions key nval)))
  14549. (defun org-find-olp (path &optional this-buffer)
  14550. "Return a marker pointing to the entry at outline path OLP.
  14551. If anything goes wrong, throw an error.
  14552. You can wrap this call to catch the error like this:
  14553. (condition-case msg
  14554. (org-mobile-locate-entry (match-string 4))
  14555. (error (nth 1 msg)))
  14556. The return value will then be either a string with the error message,
  14557. or a marker if everything is OK.
  14558. If THIS-BUFFER is set, the outline path does not contain a file,
  14559. only headings."
  14560. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14561. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14562. (level 1)
  14563. (lmin 1)
  14564. (lmax 1)
  14565. limit re end found pos heading cnt flevel)
  14566. (unless buffer (error "File not found :%s" file))
  14567. (with-current-buffer buffer
  14568. (save-excursion
  14569. (save-restriction
  14570. (widen)
  14571. (setq limit (point-max))
  14572. (goto-char (point-min))
  14573. (while (setq heading (pop path))
  14574. (setq re (format org-complex-heading-regexp-format
  14575. (regexp-quote heading)))
  14576. (setq cnt 0 pos (point))
  14577. (while (re-search-forward re end t)
  14578. (setq level (- (match-end 1) (match-beginning 1)))
  14579. (if (and (>= level lmin) (<= level lmax))
  14580. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14581. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14582. lmax heading))
  14583. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14584. lmax heading))
  14585. (goto-char found)
  14586. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14587. (setq end (save-excursion (org-end-of-subtree t t))))
  14588. (when (org-at-heading-p)
  14589. (point-marker)))))))
  14590. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14591. "Find node HEADING in BUFFER.
  14592. Return a marker to the heading if it was found, or nil if not.
  14593. If POS-ONLY is set, return just the position instead of a marker.
  14594. The heading text must match exact, but it may have a TODO keyword,
  14595. a priority cookie and tags in the standard locations."
  14596. (with-current-buffer (or buffer (current-buffer))
  14597. (save-excursion
  14598. (save-restriction
  14599. (widen)
  14600. (goto-char (point-min))
  14601. (let (case-fold-search)
  14602. (if (re-search-forward
  14603. (format org-complex-heading-regexp-format
  14604. (regexp-quote heading)) nil t)
  14605. (if pos-only
  14606. (match-beginning 0)
  14607. (move-marker (make-marker) (match-beginning 0)))))))))
  14608. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14609. "Find Org node headline HEADING in all .org files in directory DIR.
  14610. When the target headline is found, return a marker to this location."
  14611. (let ((files (directory-files (or dir default-directory)
  14612. nil "\\`[^.#].*\\.org\\'"))
  14613. file visiting m buffer)
  14614. (catch 'found
  14615. (while (setq file (pop files))
  14616. (message "trying %s" file)
  14617. (setq visiting (org-find-base-buffer-visiting file))
  14618. (setq buffer (or visiting (find-file-noselect file)))
  14619. (setq m (org-find-exact-headline-in-buffer
  14620. heading buffer))
  14621. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14622. (and m (throw 'found m))))))
  14623. (defun org-find-entry-with-id (ident)
  14624. "Locate the entry that contains the ID property with exact value IDENT.
  14625. IDENT can be a string, a symbol or a number, this function will search for
  14626. the string representation of it.
  14627. Return the position where this entry starts, or nil if there is no such entry."
  14628. (interactive "sID: ")
  14629. (let ((id (cond
  14630. ((stringp ident) ident)
  14631. ((symbol-name ident) (symbol-name ident))
  14632. ((numberp ident) (number-to-string ident))
  14633. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14634. (case-fold-search nil))
  14635. (save-excursion
  14636. (save-restriction
  14637. (widen)
  14638. (goto-char (point-min))
  14639. (when (re-search-forward
  14640. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14641. nil t)
  14642. (org-back-to-heading t)
  14643. (point))))))
  14644. ;;;; Timestamps
  14645. (defvar org-last-changed-timestamp nil)
  14646. (defvar org-last-inserted-timestamp nil
  14647. "The last time stamp inserted with `org-insert-time-stamp'.")
  14648. (defvar org-time-was-given) ; dynamically scoped parameter
  14649. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14650. (defvar org-ts-what) ; dynamically scoped parameter
  14651. (defun org-time-stamp (arg &optional inactive)
  14652. "Prompt for a date/time and insert a time stamp.
  14653. If the user specifies a time like HH:MM or if this command is
  14654. called with at least one prefix argument, the time stamp contains
  14655. the date and the time. Otherwise, only the date is be included.
  14656. All parts of a date not specified by the user is filled in from
  14657. the current date/time. So if you just press return without
  14658. typing anything, the time stamp will represent the current
  14659. date/time.
  14660. If there is already a timestamp at the cursor, it will be
  14661. modified.
  14662. With two universal prefix arguments, insert an active timestamp
  14663. with the current time without prompting the user."
  14664. (interactive "P")
  14665. (let* ((ts nil)
  14666. (default-time
  14667. ;; Default time is either today, or, when entering a range,
  14668. ;; the range start.
  14669. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14670. (save-excursion
  14671. (re-search-backward
  14672. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14673. (- (point) 20) t)))
  14674. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14675. (current-time)))
  14676. (default-input (and ts (org-get-compact-tod ts)))
  14677. (repeater (save-excursion
  14678. (save-match-data
  14679. (beginning-of-line)
  14680. (when (re-search-forward
  14681. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14682. (save-excursion (progn (end-of-line) (point))) t)
  14683. (match-string 0)))))
  14684. org-time-was-given org-end-time-was-given time)
  14685. (cond
  14686. ((and (org-at-timestamp-p t)
  14687. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14688. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14689. (insert "--")
  14690. (setq time (let ((this-command this-command))
  14691. (org-read-date arg 'totime nil nil
  14692. default-time default-input inactive)))
  14693. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14694. ((org-at-timestamp-p t)
  14695. (setq time (let ((this-command this-command))
  14696. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14697. (when (org-at-timestamp-p t) ; just to get the match data
  14698. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14699. (replace-match "")
  14700. (setq org-last-changed-timestamp
  14701. (org-insert-time-stamp
  14702. time (or org-time-was-given arg)
  14703. inactive nil nil (list org-end-time-was-given)))
  14704. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14705. (setq org-last-changed-timestamp
  14706. (concat (substring org-last-inserted-timestamp 0 -1)
  14707. " " repeater ">"))))
  14708. (message "Timestamp updated"))
  14709. ((equal arg '(16))
  14710. (org-insert-time-stamp (current-time) t))
  14711. (t
  14712. (setq time (let ((this-command this-command))
  14713. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14714. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14715. nil nil (list org-end-time-was-given))))))
  14716. ;; FIXME: can we use this for something else, like computing time differences?
  14717. (defun org-get-compact-tod (s)
  14718. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14719. (let* ((t1 (match-string 1 s))
  14720. (h1 (string-to-number (match-string 2 s)))
  14721. (m1 (string-to-number (match-string 3 s)))
  14722. (t2 (and (match-end 4) (match-string 5 s)))
  14723. (h2 (and t2 (string-to-number (match-string 6 s))))
  14724. (m2 (and t2 (string-to-number (match-string 7 s))))
  14725. dh dm)
  14726. (if (not t2)
  14727. t1
  14728. (setq dh (- h2 h1) dm (- m2 m1))
  14729. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14730. (concat t1 "+" (number-to-string dh)
  14731. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14732. (defun org-time-stamp-inactive (&optional arg)
  14733. "Insert an inactive time stamp.
  14734. An inactive time stamp is enclosed in square brackets instead of angle
  14735. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14736. does not link to the calendar and cannot be changed with the S-cursor keys.
  14737. So these are more for recording a certain time/date."
  14738. (interactive "P")
  14739. (org-time-stamp arg 'inactive))
  14740. (defvar org-date-ovl (make-overlay 1 1))
  14741. (overlay-put org-date-ovl 'face 'org-date-selected)
  14742. (org-detach-overlay org-date-ovl)
  14743. (defvar org-ans1) ; dynamically scoped parameter
  14744. (defvar org-ans2) ; dynamically scoped parameter
  14745. (defvar org-plain-time-of-day-regexp) ; defined below
  14746. (defvar org-overriding-default-time nil) ; dynamically scoped
  14747. (defvar org-read-date-overlay nil)
  14748. (defvar org-dcst nil) ; dynamically scoped
  14749. (defvar org-read-date-history nil)
  14750. (defvar org-read-date-final-answer nil)
  14751. (defvar org-read-date-analyze-futurep nil)
  14752. (defvar org-read-date-analyze-forced-year nil)
  14753. (defvar org-read-date-inactive)
  14754. (defvar org-read-date-minibuffer-local-map
  14755. (let ((map (make-sparse-keymap)))
  14756. (set-keymap-parent map minibuffer-local-map)
  14757. (org-defkey map (kbd ".")
  14758. (lambda () (interactive)
  14759. ;; Are we at the beginning of the prompt?
  14760. (if (looking-back "^[^:]+: ")
  14761. (org-eval-in-calendar '(calendar-goto-today))
  14762. (insert "."))))
  14763. (org-defkey map (kbd "C-.")
  14764. (lambda () (interactive)
  14765. (org-eval-in-calendar '(calendar-goto-today))))
  14766. (org-defkey map [(meta shift left)]
  14767. (lambda () (interactive)
  14768. (org-eval-in-calendar '(calendar-backward-month 1))))
  14769. (org-defkey map [(meta shift right)]
  14770. (lambda () (interactive)
  14771. (org-eval-in-calendar '(calendar-forward-month 1))))
  14772. (org-defkey map [(meta shift up)]
  14773. (lambda () (interactive)
  14774. (org-eval-in-calendar '(calendar-backward-year 1))))
  14775. (org-defkey map [(meta shift down)]
  14776. (lambda () (interactive)
  14777. (org-eval-in-calendar '(calendar-forward-year 1))))
  14778. (org-defkey map [?\e (shift left)]
  14779. (lambda () (interactive)
  14780. (org-eval-in-calendar '(calendar-backward-month 1))))
  14781. (org-defkey map [?\e (shift right)]
  14782. (lambda () (interactive)
  14783. (org-eval-in-calendar '(calendar-forward-month 1))))
  14784. (org-defkey map [?\e (shift up)]
  14785. (lambda () (interactive)
  14786. (org-eval-in-calendar '(calendar-backward-year 1))))
  14787. (org-defkey map [?\e (shift down)]
  14788. (lambda () (interactive)
  14789. (org-eval-in-calendar '(calendar-forward-year 1))))
  14790. (org-defkey map [(shift up)]
  14791. (lambda () (interactive)
  14792. (org-eval-in-calendar '(calendar-backward-week 1))))
  14793. (org-defkey map [(shift down)]
  14794. (lambda () (interactive)
  14795. (org-eval-in-calendar '(calendar-forward-week 1))))
  14796. (org-defkey map [(shift left)]
  14797. (lambda () (interactive)
  14798. (org-eval-in-calendar '(calendar-backward-day 1))))
  14799. (org-defkey map [(shift right)]
  14800. (lambda () (interactive)
  14801. (org-eval-in-calendar '(calendar-forward-day 1))))
  14802. (org-defkey map "!"
  14803. (lambda () (interactive)
  14804. (org-eval-in-calendar '(diary-view-entries))
  14805. (message "")))
  14806. (org-defkey map ">"
  14807. (lambda () (interactive)
  14808. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14809. (org-defkey map "<"
  14810. (lambda () (interactive)
  14811. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14812. (org-defkey map "\C-v"
  14813. (lambda () (interactive)
  14814. (org-eval-in-calendar
  14815. '(calendar-scroll-left-three-months 1))))
  14816. (org-defkey map "\M-v"
  14817. (lambda () (interactive)
  14818. (org-eval-in-calendar
  14819. '(calendar-scroll-right-three-months 1))))
  14820. map)
  14821. "Keymap for minibuffer commands when using `org-read-date'.")
  14822. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14823. default-time default-input inactive)
  14824. "Read a date, possibly a time, and make things smooth for the user.
  14825. The prompt will suggest to enter an ISO date, but you can also enter anything
  14826. which will at least partially be understood by `parse-time-string'.
  14827. Unrecognized parts of the date will default to the current day, month, year,
  14828. hour and minute. If this command is called to replace a timestamp at point,
  14829. or to enter the second timestamp of a range, the default time is taken
  14830. from the existing stamp. Furthermore, the command prefers the future,
  14831. so if you are giving a date where the year is not given, and the day-month
  14832. combination is already past in the current year, it will assume you
  14833. mean next year. For details, see the manual. A few examples:
  14834. 3-2-5 --> 2003-02-05
  14835. feb 15 --> currentyear-02-15
  14836. 2/15 --> currentyear-02-15
  14837. sep 12 9 --> 2009-09-12
  14838. 12:45 --> today 12:45
  14839. 22 sept 0:34 --> currentyear-09-22 0:34
  14840. 12 --> currentyear-currentmonth-12
  14841. Fri --> nearest Friday after today
  14842. -Tue --> last Tuesday
  14843. etc.
  14844. Furthermore you can specify a relative date by giving, as the *first* thing
  14845. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14846. change in days weeks, months, years.
  14847. With a single plus or minus, the date is relative to today. With a double
  14848. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14849. +4d --> four days from today
  14850. +4 --> same as above
  14851. +2w --> two weeks from today
  14852. ++5 --> five days from default date
  14853. The function understands only English month and weekday abbreviations.
  14854. While prompting, a calendar is popped up - you can also select the
  14855. date with the mouse (button 1). The calendar shows a period of three
  14856. months. To scroll it to other months, use the keys `>' and `<'.
  14857. If you don't like the calendar, turn it off with
  14858. \(setq org-read-date-popup-calendar nil)
  14859. With optional argument TO-TIME, the date will immediately be converted
  14860. to an internal time.
  14861. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14862. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14863. still enter a time, and this function will inform the calling routine
  14864. about this change. The calling routine may then choose to change the
  14865. format used to insert the time stamp into the buffer to include the time.
  14866. With optional argument FROM-STRING, read from this string instead from
  14867. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14868. the time/date that is used for everything that is not specified by the
  14869. user."
  14870. (require 'parse-time)
  14871. (let* ((org-time-stamp-rounding-minutes
  14872. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14873. (org-dcst org-display-custom-times)
  14874. (ct (org-current-time))
  14875. (org-def (or org-overriding-default-time default-time ct))
  14876. (org-defdecode (decode-time org-def))
  14877. (dummy (progn
  14878. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14879. (setcar (nthcdr 2 org-defdecode) -1)
  14880. (setcar (nthcdr 1 org-defdecode) 59)
  14881. (setq org-def (apply 'encode-time org-defdecode)
  14882. org-defdecode (decode-time org-def)))))
  14883. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14884. (calendar-frame-setup nil)
  14885. (calendar-setup nil)
  14886. (calendar-move-hook nil)
  14887. (calendar-view-diary-initially-flag nil)
  14888. (calendar-view-holidays-initially-flag nil)
  14889. (timestr (format-time-string
  14890. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14891. (prompt (concat (if prompt (concat prompt " ") "")
  14892. (format "Date+time [%s]: " timestr)))
  14893. ans (org-ans0 "") org-ans1 org-ans2 final)
  14894. (cond
  14895. (from-string (setq ans from-string))
  14896. (org-read-date-popup-calendar
  14897. (save-excursion
  14898. (save-window-excursion
  14899. (calendar)
  14900. (org-eval-in-calendar '(setq cursor-type nil) t)
  14901. (unwind-protect
  14902. (progn
  14903. (calendar-forward-day (- (time-to-days org-def)
  14904. (calendar-absolute-from-gregorian
  14905. (calendar-current-date))))
  14906. (org-eval-in-calendar nil t)
  14907. (let* ((old-map (current-local-map))
  14908. (map (copy-keymap calendar-mode-map))
  14909. (minibuffer-local-map
  14910. (copy-keymap org-read-date-minibuffer-local-map)))
  14911. (org-defkey map (kbd "RET") 'org-calendar-select)
  14912. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14913. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14914. (unwind-protect
  14915. (progn
  14916. (use-local-map map)
  14917. (setq org-read-date-inactive inactive)
  14918. (add-hook 'post-command-hook 'org-read-date-display)
  14919. (setq org-ans0 (read-string prompt default-input
  14920. 'org-read-date-history nil))
  14921. ;; org-ans0: from prompt
  14922. ;; org-ans1: from mouse click
  14923. ;; org-ans2: from calendar motion
  14924. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14925. (remove-hook 'post-command-hook 'org-read-date-display)
  14926. (use-local-map old-map)
  14927. (when org-read-date-overlay
  14928. (delete-overlay org-read-date-overlay)
  14929. (setq org-read-date-overlay nil)))))
  14930. (bury-buffer "*Calendar*")))))
  14931. (t ; Naked prompt only
  14932. (unwind-protect
  14933. (setq ans (read-string prompt default-input
  14934. 'org-read-date-history timestr))
  14935. (when org-read-date-overlay
  14936. (delete-overlay org-read-date-overlay)
  14937. (setq org-read-date-overlay nil)))))
  14938. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14939. (when org-read-date-analyze-forced-year
  14940. (message "Year was forced into %s"
  14941. (if org-read-date-force-compatible-dates
  14942. "compatible range (1970-2037)"
  14943. "range representable on this machine"))
  14944. (ding))
  14945. ;; One round trip to get rid of 34th of August and stuff like that....
  14946. (setq final (decode-time (apply 'encode-time final)))
  14947. (setq org-read-date-final-answer ans)
  14948. (if to-time
  14949. (apply 'encode-time final)
  14950. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14951. (format "%04d-%02d-%02d %02d:%02d"
  14952. (nth 5 final) (nth 4 final) (nth 3 final)
  14953. (nth 2 final) (nth 1 final))
  14954. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14955. (defvar org-def)
  14956. (defvar org-defdecode)
  14957. (defvar org-with-time)
  14958. (defun org-read-date-display ()
  14959. "Display the current date prompt interpretation in the minibuffer."
  14960. (when org-read-date-display-live
  14961. (when org-read-date-overlay
  14962. (delete-overlay org-read-date-overlay))
  14963. (when (minibufferp (current-buffer))
  14964. (save-excursion
  14965. (end-of-line 1)
  14966. (while (not (equal (buffer-substring
  14967. (max (point-min) (- (point) 4)) (point))
  14968. " "))
  14969. (insert " ")))
  14970. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14971. " " (or org-ans1 org-ans2)))
  14972. (org-end-time-was-given nil)
  14973. (f (org-read-date-analyze ans org-def org-defdecode))
  14974. (fmts (if org-dcst
  14975. org-time-stamp-custom-formats
  14976. org-time-stamp-formats))
  14977. (fmt (if (or org-with-time
  14978. (and (boundp 'org-time-was-given) org-time-was-given))
  14979. (cdr fmts)
  14980. (car fmts)))
  14981. (txt (format-time-string fmt (apply 'encode-time f)))
  14982. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14983. (txt (concat "=> " txt)))
  14984. (when (and org-end-time-was-given
  14985. (string-match org-plain-time-of-day-regexp txt))
  14986. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14987. org-end-time-was-given
  14988. (substring txt (match-end 0)))))
  14989. (when org-read-date-analyze-futurep
  14990. (setq txt (concat txt " (=>F)")))
  14991. (setq org-read-date-overlay
  14992. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14993. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14994. (defun org-read-date-analyze (ans org-def org-defdecode)
  14995. "Analyze the combined answer of the date prompt."
  14996. ;; FIXME: cleanup and comment
  14997. (let ((nowdecode (decode-time (current-time)))
  14998. delta deltan deltaw deltadef year month day
  14999. hour minute second wday pm h2 m2 tl wday1
  15000. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15001. (setq org-read-date-analyze-futurep nil
  15002. org-read-date-analyze-forced-year nil)
  15003. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15004. (setq ans "+0"))
  15005. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15006. (setq ans (replace-match "" t t ans)
  15007. deltan (car delta)
  15008. deltaw (nth 1 delta)
  15009. deltadef (nth 2 delta)))
  15010. ;; Check if there is an iso week date in there. If yes, store the
  15011. ;; info and postpone interpreting it until the rest of the parsing
  15012. ;; is done.
  15013. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15014. (setq iso-year (if (match-end 1)
  15015. (org-small-year-to-year
  15016. (string-to-number (match-string 1 ans))))
  15017. iso-weekday (if (match-end 3)
  15018. (string-to-number (match-string 3 ans)))
  15019. iso-week (string-to-number (match-string 2 ans)))
  15020. (setq ans (replace-match "" t t ans)))
  15021. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15022. (when (string-match
  15023. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15024. (setq year (if (match-end 2)
  15025. (string-to-number (match-string 2 ans))
  15026. (progn (setq kill-year t)
  15027. (string-to-number (format-time-string "%Y"))))
  15028. month (string-to-number (match-string 3 ans))
  15029. day (string-to-number (match-string 4 ans)))
  15030. (if (< year 100) (setq year (+ 2000 year)))
  15031. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15032. t nil ans)))
  15033. ;; Help matching dotted european dates
  15034. (when (string-match
  15035. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15036. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15037. (setq kill-year t)
  15038. (string-to-number (format-time-string "%Y")))
  15039. day (string-to-number (match-string 1 ans))
  15040. month (string-to-number (match-string 2 ans))
  15041. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15042. t nil ans)))
  15043. ;; Help matching american dates, like 5/30 or 5/30/7
  15044. (when (string-match
  15045. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15046. (setq year (if (match-end 4)
  15047. (string-to-number (match-string 4 ans))
  15048. (progn (setq kill-year t)
  15049. (string-to-number (format-time-string "%Y"))))
  15050. month (string-to-number (match-string 1 ans))
  15051. day (string-to-number (match-string 2 ans)))
  15052. (if (< year 100) (setq year (+ 2000 year)))
  15053. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15054. t nil ans)))
  15055. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15056. ;; If there is a time with am/pm, and *no* time without it, we convert
  15057. ;; so that matching will be successful.
  15058. (loop for i from 1 to 2 do ; twice, for end time as well
  15059. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15060. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15061. (setq hour (string-to-number (match-string 1 ans))
  15062. minute (if (match-end 3)
  15063. (string-to-number (match-string 3 ans))
  15064. 0)
  15065. pm (equal ?p
  15066. (string-to-char (downcase (match-string 4 ans)))))
  15067. (if (and (= hour 12) (not pm))
  15068. (setq hour 0)
  15069. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15070. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15071. t t ans))))
  15072. ;; Check if a time range is given as a duration
  15073. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15074. (setq hour (string-to-number (match-string 1 ans))
  15075. h2 (+ hour (string-to-number (match-string 3 ans)))
  15076. minute (string-to-number (match-string 2 ans))
  15077. m2 (+ minute (if (match-end 5) (string-to-number
  15078. (match-string 5 ans))0)))
  15079. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15080. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15081. t t ans)))
  15082. ;; Check if there is a time range
  15083. (when (boundp 'org-end-time-was-given)
  15084. (setq org-time-was-given nil)
  15085. (when (and (string-match org-plain-time-of-day-regexp ans)
  15086. (match-end 8))
  15087. (setq org-end-time-was-given (match-string 8 ans))
  15088. (setq ans (concat (substring ans 0 (match-beginning 7))
  15089. (substring ans (match-end 7))))))
  15090. (setq tl (parse-time-string ans)
  15091. day (or (nth 3 tl) (nth 3 org-defdecode))
  15092. month (or (nth 4 tl)
  15093. (if (and org-read-date-prefer-future
  15094. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15095. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15096. (nth 4 org-defdecode)))
  15097. year (or (and (not kill-year) (nth 5 tl))
  15098. (if (and org-read-date-prefer-future
  15099. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15100. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15101. (nth 5 org-defdecode)))
  15102. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15103. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15104. second (or (nth 0 tl) 0)
  15105. wday (nth 6 tl))
  15106. (when (and (eq org-read-date-prefer-future 'time)
  15107. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15108. (equal day (nth 3 nowdecode))
  15109. (equal month (nth 4 nowdecode))
  15110. (equal year (nth 5 nowdecode))
  15111. (nth 2 tl)
  15112. (or (< (nth 2 tl) (nth 2 nowdecode))
  15113. (and (= (nth 2 tl) (nth 2 nowdecode))
  15114. (nth 1 tl)
  15115. (< (nth 1 tl) (nth 1 nowdecode)))))
  15116. (setq day (1+ day)
  15117. futurep t))
  15118. ;; Special date definitions below
  15119. (cond
  15120. (iso-week
  15121. ;; There was an iso week
  15122. (require 'cal-iso)
  15123. (setq futurep nil)
  15124. (setq year (or iso-year year)
  15125. day (or iso-weekday wday 1)
  15126. wday nil ; to make sure that the trigger below does not match
  15127. iso-date (calendar-gregorian-from-absolute
  15128. (calendar-absolute-from-iso
  15129. (list iso-week day year))))
  15130. ; FIXME: Should we also push ISO weeks into the future?
  15131. ; (when (and org-read-date-prefer-future
  15132. ; (not iso-year)
  15133. ; (< (calendar-absolute-from-gregorian iso-date)
  15134. ; (time-to-days (current-time))))
  15135. ; (setq year (1+ year)
  15136. ; iso-date (calendar-gregorian-from-absolute
  15137. ; (calendar-absolute-from-iso
  15138. ; (list iso-week day year)))))
  15139. (setq month (car iso-date)
  15140. year (nth 2 iso-date)
  15141. day (nth 1 iso-date)))
  15142. (deltan
  15143. (setq futurep nil)
  15144. (unless deltadef
  15145. (let ((now (decode-time (current-time))))
  15146. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15147. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15148. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15149. ((equal deltaw "m") (setq month (+ month deltan)))
  15150. ((equal deltaw "y") (setq year (+ year deltan)))))
  15151. ((and wday (not (nth 3 tl)))
  15152. ;; Weekday was given, but no day, so pick that day in the week
  15153. ;; on or after the derived date.
  15154. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15155. (unless (equal wday wday1)
  15156. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15157. (if (and (boundp 'org-time-was-given)
  15158. (nth 2 tl))
  15159. (setq org-time-was-given t))
  15160. (if (< year 100) (setq year (+ 2000 year)))
  15161. ;; Check of the date is representable
  15162. (if org-read-date-force-compatible-dates
  15163. (progn
  15164. (if (< year 1970)
  15165. (setq year 1970 org-read-date-analyze-forced-year t))
  15166. (if (> year 2037)
  15167. (setq year 2037 org-read-date-analyze-forced-year t)))
  15168. (condition-case nil
  15169. (ignore (encode-time second minute hour day month year))
  15170. (error
  15171. (setq year (nth 5 org-defdecode))
  15172. (setq org-read-date-analyze-forced-year t))))
  15173. (setq org-read-date-analyze-futurep futurep)
  15174. (list second minute hour day month year)))
  15175. (defvar parse-time-weekdays)
  15176. (defun org-read-date-get-relative (s today default)
  15177. "Check string S for special relative date string.
  15178. TODAY and DEFAULT are internal times, for today and for a default.
  15179. Return shift list (N what def-flag)
  15180. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15181. N is the number of WHATs to shift.
  15182. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15183. the DEFAULT date rather than TODAY."
  15184. (require 'parse-time)
  15185. (when (and
  15186. (string-match
  15187. (concat
  15188. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15189. "\\([0-9]+\\)?"
  15190. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15191. "\\([ \t]\\|$\\)") s)
  15192. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15193. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15194. (string-to-char (substring (match-string 1 s) -1))
  15195. ?+))
  15196. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15197. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15198. (what (if (match-end 3) (match-string 3 s) "d"))
  15199. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15200. (date (if rel default today))
  15201. (wday (nth 6 (decode-time date)))
  15202. delta)
  15203. (if wday1
  15204. (progn
  15205. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15206. (if (= delta 0) (setq delta 7))
  15207. (if (= dir ?-)
  15208. (progn
  15209. (setq delta (- delta 7))
  15210. (if (= delta 0) (setq delta -7))))
  15211. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15212. (list delta "d" rel))
  15213. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15214. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15215. "Turn a user-specified date into the internal representation.
  15216. The internal representation needed by the calendar is (month day year).
  15217. This is a wrapper to handle the brain-dead convention in calendar that
  15218. user function argument order change dependent on argument order."
  15219. (if (boundp 'calendar-date-style)
  15220. (cond
  15221. ((eq calendar-date-style 'american)
  15222. (list arg1 arg2 arg3))
  15223. ((eq calendar-date-style 'european)
  15224. (list arg2 arg1 arg3))
  15225. ((eq calendar-date-style 'iso)
  15226. (list arg2 arg3 arg1)))
  15227. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15228. (if (org-bound-and-true-p european-calendar-style)
  15229. (list arg2 arg1 arg3)
  15230. (list arg1 arg2 arg3)))))
  15231. (defun org-eval-in-calendar (form &optional keepdate)
  15232. "Eval FORM in the calendar window and return to current window.
  15233. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15234. otherwise stick to the current value of `org-ans2'."
  15235. (let ((sf (selected-frame))
  15236. (sw (selected-window)))
  15237. (select-window (get-buffer-window "*Calendar*" t))
  15238. (eval form)
  15239. (when (and (not keepdate) (calendar-cursor-to-date))
  15240. (let* ((date (calendar-cursor-to-date))
  15241. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15242. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15243. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15244. (select-window sw)
  15245. (org-select-frame-set-input-focus sf)))
  15246. (defun org-calendar-select ()
  15247. "Return to `org-read-date' with the date currently selected.
  15248. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15249. (interactive)
  15250. (when (calendar-cursor-to-date)
  15251. (let* ((date (calendar-cursor-to-date))
  15252. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15253. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15254. (if (active-minibuffer-window) (exit-minibuffer))))
  15255. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15256. "Insert a date stamp for the date given by the internal TIME.
  15257. WITH-HM means use the stamp format that includes the time of the day.
  15258. INACTIVE means use square brackets instead of angular ones, so that the
  15259. stamp will not contribute to the agenda.
  15260. PRE and POST are optional strings to be inserted before and after the
  15261. stamp.
  15262. The command returns the inserted time stamp."
  15263. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15264. stamp)
  15265. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15266. (insert-before-markers (or pre ""))
  15267. (when (listp extra)
  15268. (setq extra (car extra))
  15269. (if (and (stringp extra)
  15270. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15271. (setq extra (format "-%02d:%02d"
  15272. (string-to-number (match-string 1 extra))
  15273. (string-to-number (match-string 2 extra))))
  15274. (setq extra nil)))
  15275. (when extra
  15276. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15277. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15278. (insert-before-markers (or post ""))
  15279. (setq org-last-inserted-timestamp stamp)))
  15280. (defun org-toggle-time-stamp-overlays ()
  15281. "Toggle the use of custom time stamp formats."
  15282. (interactive)
  15283. (setq org-display-custom-times (not org-display-custom-times))
  15284. (unless org-display-custom-times
  15285. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15286. (while (setq p (next-single-property-change p 'display))
  15287. (if (and (get-text-property p 'display)
  15288. (eq (get-text-property p 'face) 'org-date))
  15289. (remove-text-properties
  15290. p (setq p (next-single-property-change p 'display))
  15291. '(display t))))
  15292. (set-buffer-modified-p bmp)))
  15293. (if (featurep 'xemacs)
  15294. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15295. (org-restart-font-lock)
  15296. (setq org-table-may-need-update t)
  15297. (if org-display-custom-times
  15298. (message "Time stamps are overlaid with custom format")
  15299. (message "Time stamp overlays removed")))
  15300. (defun org-display-custom-time (beg end)
  15301. "Overlay modified time stamp format over timestamp between BEG and END."
  15302. (let* ((ts (buffer-substring beg end))
  15303. t1 w1 with-hm tf time str w2 (off 0))
  15304. (save-match-data
  15305. (setq t1 (org-parse-time-string ts t))
  15306. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15307. (setq off (- (match-end 0) (match-beginning 0)))))
  15308. (setq end (- end off))
  15309. (setq w1 (- end beg)
  15310. with-hm (and (nth 1 t1) (nth 2 t1))
  15311. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15312. time (org-fix-decoded-time t1)
  15313. str (org-add-props
  15314. (format-time-string
  15315. (substring tf 1 -1) (apply 'encode-time time))
  15316. nil 'mouse-face 'highlight)
  15317. w2 (length str))
  15318. (if (not (= w2 w1))
  15319. (add-text-properties (1+ beg) (+ 2 beg)
  15320. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15321. (if (featurep 'xemacs)
  15322. (progn
  15323. (put-text-property beg end 'invisible t)
  15324. (put-text-property beg end 'end-glyph (make-glyph str)))
  15325. (put-text-property beg end 'display str))))
  15326. (defun org-translate-time (string)
  15327. "Translate all timestamps in STRING to custom format.
  15328. But do this only if the variable `org-display-custom-times' is set."
  15329. (when org-display-custom-times
  15330. (save-match-data
  15331. (let* ((start 0)
  15332. (re org-ts-regexp-both)
  15333. t1 with-hm inactive tf time str beg end)
  15334. (while (setq start (string-match re string start))
  15335. (setq beg (match-beginning 0)
  15336. end (match-end 0)
  15337. t1 (save-match-data
  15338. (org-parse-time-string (substring string beg end) t))
  15339. with-hm (and (nth 1 t1) (nth 2 t1))
  15340. inactive (equal (substring string beg (1+ beg)) "[")
  15341. tf (funcall (if with-hm 'cdr 'car)
  15342. org-time-stamp-custom-formats)
  15343. time (org-fix-decoded-time t1)
  15344. str (format-time-string
  15345. (concat
  15346. (if inactive "[" "<") (substring tf 1 -1)
  15347. (if inactive "]" ">"))
  15348. (apply 'encode-time time))
  15349. string (replace-match str t t string)
  15350. start (+ start (length str)))))))
  15351. string)
  15352. (defun org-fix-decoded-time (time)
  15353. "Set 0 instead of nil for the first 6 elements of time.
  15354. Don't touch the rest."
  15355. (let ((n 0))
  15356. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15357. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15358. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15359. "Difference between TIMESTAMP-STRING and now in days.
  15360. If SECONDS is non-nil, return the difference in seconds."
  15361. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15362. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15363. (funcall fdiff (current-time)))))
  15364. (defun org-deadline-close (timestamp-string &optional ndays)
  15365. "Is the time in TIMESTAMP-STRING close to the current date?"
  15366. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15367. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15368. (not (org-entry-is-done-p))))
  15369. (defun org-get-wdays (ts &optional delay zero-delay)
  15370. "Get the deadline lead time appropriate for timestring TS.
  15371. When DELAY is non-nil, get the delay time for scheduled items
  15372. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15373. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15374. don't try to find the delay cookie in the scheduled timestamp."
  15375. (let ((tv (if delay org-scheduled-delay-days
  15376. org-deadline-warning-days)))
  15377. (cond
  15378. ((or (and delay (< tv 0))
  15379. (and delay zero-delay (<= tv 0))
  15380. (and (not delay) (<= tv 0)))
  15381. ;; Enforce this value no matter what
  15382. (- tv))
  15383. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15384. ;; lead time is specified.
  15385. (floor (* (string-to-number (match-string 1 ts))
  15386. (cdr (assoc (match-string 2 ts)
  15387. '(("d" . 1) ("w" . 7)
  15388. ("m" . 30.4) ("y" . 365.25)
  15389. ("h" . 0.041667)))))))
  15390. ;; go for the default.
  15391. (t tv))))
  15392. (defun org-calendar-select-mouse (ev)
  15393. "Return to `org-read-date' with the date currently selected.
  15394. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15395. (interactive "e")
  15396. (mouse-set-point ev)
  15397. (when (calendar-cursor-to-date)
  15398. (let* ((date (calendar-cursor-to-date))
  15399. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15400. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15401. (if (active-minibuffer-window) (exit-minibuffer))))
  15402. (defun org-check-deadlines (ndays)
  15403. "Check if there are any deadlines due or past due.
  15404. A deadline is considered due if it happens within `org-deadline-warning-days'
  15405. days from today's date. If the deadline appears in an entry marked DONE,
  15406. it is not shown. The prefix arg NDAYS can be used to test that many
  15407. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15408. (interactive "P")
  15409. (let* ((org-warn-days
  15410. (cond
  15411. ((equal ndays '(4)) 100000)
  15412. (ndays (prefix-numeric-value ndays))
  15413. (t (abs org-deadline-warning-days))))
  15414. (case-fold-search nil)
  15415. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15416. (callback
  15417. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15418. (message "%d deadlines past-due or due within %d days"
  15419. (org-occur regexp nil callback)
  15420. org-warn-days)))
  15421. (defsubst org-re-timestamp (type)
  15422. "Return a regexp for timestamp TYPE.
  15423. Allowed values for TYPE are:
  15424. all: all timestamps
  15425. active: only active timestamps (<...>)
  15426. inactive: only inactive timestamps ([...])
  15427. scheduled: only scheduled timestamps
  15428. deadline: only deadline timestamps
  15429. closed: only closed time-stamps
  15430. When TYPE is nil, fall back on returning a regexp that matches
  15431. both scheduled and deadline timestamps."
  15432. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15433. ((eq type 'active) org-ts-regexp)
  15434. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15435. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15436. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15437. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15438. ((eq type 'scheduled-or-deadline)
  15439. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15440. (defun org-check-before-date (date)
  15441. "Check if there are deadlines or scheduled entries before DATE."
  15442. (interactive (list (org-read-date)))
  15443. (let ((case-fold-search nil)
  15444. (regexp (org-re-timestamp org-ts-type))
  15445. (callback
  15446. (lambda () (time-less-p
  15447. (org-time-string-to-time (match-string 1))
  15448. (org-time-string-to-time date)))))
  15449. (message "%d entries before %s"
  15450. (org-occur regexp nil callback) date)))
  15451. (defun org-check-after-date (date)
  15452. "Check if there are deadlines or scheduled entries after DATE."
  15453. (interactive (list (org-read-date)))
  15454. (let ((case-fold-search nil)
  15455. (regexp (org-re-timestamp org-ts-type))
  15456. (callback
  15457. (lambda () (not
  15458. (time-less-p
  15459. (org-time-string-to-time (match-string 1))
  15460. (org-time-string-to-time date))))))
  15461. (message "%d entries after %s"
  15462. (org-occur regexp nil callback) date)))
  15463. (defun org-check-dates-range (start-date end-date)
  15464. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15465. (interactive (list (org-read-date nil nil nil "Range starts")
  15466. (org-read-date nil nil nil "Range end")))
  15467. (let ((case-fold-search nil)
  15468. (regexp (org-re-timestamp org-ts-type))
  15469. (callback
  15470. (lambda ()
  15471. (let ((match (match-string 1)))
  15472. (and
  15473. (not (time-less-p
  15474. (org-time-string-to-time match)
  15475. (org-time-string-to-time start-date)))
  15476. (time-less-p
  15477. (org-time-string-to-time match)
  15478. (org-time-string-to-time end-date)))))))
  15479. (message "%d entries between %s and %s"
  15480. (org-occur regexp nil callback) start-date end-date)))
  15481. (defun org-evaluate-time-range (&optional to-buffer)
  15482. "Evaluate a time range by computing the difference between start and end.
  15483. Normally the result is just printed in the echo area, but with prefix arg
  15484. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15485. If the time range is actually in a table, the result is inserted into the
  15486. next column.
  15487. For time difference computation, a year is assumed to be exactly 365
  15488. days in order to avoid rounding problems."
  15489. (interactive "P")
  15490. (or
  15491. (org-clock-update-time-maybe)
  15492. (save-excursion
  15493. (unless (org-at-date-range-p t)
  15494. (goto-char (point-at-bol))
  15495. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15496. (if (not (org-at-date-range-p t))
  15497. (user-error "Not at a time-stamp range, and none found in current line")))
  15498. (let* ((ts1 (match-string 1))
  15499. (ts2 (match-string 2))
  15500. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15501. (match-end (match-end 0))
  15502. (time1 (org-time-string-to-time ts1))
  15503. (time2 (org-time-string-to-time ts2))
  15504. (t1 (org-float-time time1))
  15505. (t2 (org-float-time time2))
  15506. (diff (abs (- t2 t1)))
  15507. (negative (< (- t2 t1) 0))
  15508. ;; (ys (floor (* 365 24 60 60)))
  15509. (ds (* 24 60 60))
  15510. (hs (* 60 60))
  15511. (fy "%dy %dd %02d:%02d")
  15512. (fy1 "%dy %dd")
  15513. (fd "%dd %02d:%02d")
  15514. (fd1 "%dd")
  15515. (fh "%02d:%02d")
  15516. y d h m align)
  15517. (if havetime
  15518. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15519. y 0
  15520. d (floor (/ diff ds)) diff (mod diff ds)
  15521. h (floor (/ diff hs)) diff (mod diff hs)
  15522. m (floor (/ diff 60)))
  15523. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15524. y 0
  15525. d (floor (+ (/ diff ds) 0.5))
  15526. h 0 m 0))
  15527. (if (not to-buffer)
  15528. (message "%s" (org-make-tdiff-string y d h m))
  15529. (if (org-at-table-p)
  15530. (progn
  15531. (goto-char match-end)
  15532. (setq align t)
  15533. (and (looking-at " *|") (goto-char (match-end 0))))
  15534. (goto-char match-end))
  15535. (if (looking-at
  15536. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15537. (replace-match ""))
  15538. (if negative (insert " -"))
  15539. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15540. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15541. (insert " " (format fh h m))))
  15542. (if align (org-table-align))
  15543. (message "Time difference inserted")))))
  15544. (defun org-make-tdiff-string (y d h m)
  15545. (let ((fmt "")
  15546. (l nil))
  15547. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15548. l (push y l)))
  15549. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15550. l (push d l)))
  15551. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15552. l (push h l)))
  15553. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15554. l (push m l)))
  15555. (apply 'format fmt (nreverse l))))
  15556. (defun org-time-string-to-time (s &optional buffer pos)
  15557. "Convert a timestamp string into internal time."
  15558. (condition-case errdata
  15559. (apply 'encode-time (org-parse-time-string s))
  15560. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15561. s (if (not (and buffer pos))
  15562. ""
  15563. (format " at %d in buffer `%s'" pos buffer))
  15564. (cdr errdata)))))
  15565. (defun org-time-string-to-seconds (s)
  15566. "Convert a timestamp string to a number of seconds."
  15567. (org-float-time (org-time-string-to-time s)))
  15568. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15569. "Convert a time stamp to an absolute day number.
  15570. If there is a specifier for a cyclic time stamp, get the closest
  15571. date to DAYNR.
  15572. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15573. The variable `date' is bound by the calendar when this is called."
  15574. (cond
  15575. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15576. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15577. daynr
  15578. (+ daynr 1000)))
  15579. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15580. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15581. (time-to-days (current-time))) (match-string 0 s)
  15582. prefer show-all))
  15583. (t (time-to-days
  15584. (condition-case errdata
  15585. (apply 'encode-time (org-parse-time-string s))
  15586. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15587. s (if (not (and buffer pos))
  15588. ""
  15589. (format " at %d in buffer `%s'" pos buffer))
  15590. (cdr errdata))))))))
  15591. (defun org-days-to-iso-week (days)
  15592. "Return the iso week number."
  15593. (require 'cal-iso)
  15594. (car (calendar-iso-from-absolute days)))
  15595. (defun org-small-year-to-year (year)
  15596. "Convert 2-digit years into 4-digit years.
  15597. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15598. The year 2000 cannot be abbreviated. Any year larger than 99
  15599. is returned unchanged."
  15600. (if (< year 38)
  15601. (setq year (+ 2000 year))
  15602. (if (< year 100)
  15603. (setq year (+ 1900 year))))
  15604. year)
  15605. (defun org-time-from-absolute (d)
  15606. "Return the time corresponding to date D.
  15607. D may be an absolute day number, or a calendar-type list (month day year)."
  15608. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15609. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15610. (defun org-calendar-holiday ()
  15611. "List of holidays, for Diary display in Org-mode."
  15612. (require 'holidays)
  15613. (let ((hl (funcall
  15614. (if (fboundp 'calendar-check-holidays)
  15615. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15616. (if hl (mapconcat 'identity hl "; "))))
  15617. (defun org-diary-sexp-entry (sexp entry date)
  15618. "Process a SEXP diary ENTRY for DATE."
  15619. (require 'diary-lib)
  15620. (let ((result (if calendar-debug-sexp
  15621. (let ((stack-trace-on-error t))
  15622. (eval (car (read-from-string sexp))))
  15623. (condition-case nil
  15624. (eval (car (read-from-string sexp)))
  15625. (error
  15626. (beep)
  15627. (message "Bad sexp at line %d in %s: %s"
  15628. (org-current-line)
  15629. (buffer-file-name) sexp)
  15630. (sleep-for 2))))))
  15631. (cond ((stringp result) (split-string result "; "))
  15632. ((and (consp result)
  15633. (not (consp (cdr result)))
  15634. (stringp (cdr result))) (cdr result))
  15635. ((and (consp result)
  15636. (stringp (car result))) result)
  15637. (result entry))))
  15638. (defun org-diary-to-ical-string (frombuf)
  15639. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15640. This uses the icalendar.el library."
  15641. (let* ((tmpdir (if (featurep 'xemacs)
  15642. (temp-directory)
  15643. temporary-file-directory))
  15644. (tmpfile (make-temp-name
  15645. (expand-file-name "orgics" tmpdir)))
  15646. buf rtn b e)
  15647. (with-current-buffer frombuf
  15648. (icalendar-export-region (point-min) (point-max) tmpfile)
  15649. (setq buf (find-buffer-visiting tmpfile))
  15650. (set-buffer buf)
  15651. (goto-char (point-min))
  15652. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15653. (setq b (match-beginning 0)))
  15654. (goto-char (point-max))
  15655. (if (re-search-backward "^END:VEVENT" nil t)
  15656. (setq e (match-end 0)))
  15657. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15658. (kill-buffer buf)
  15659. (delete-file tmpfile)
  15660. rtn))
  15661. (defun org-closest-date (start current change prefer show-all)
  15662. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15663. When PREFER is `past', return a date that is either CURRENT or past.
  15664. When PREFER is `future', return a date that is either CURRENT or future.
  15665. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15666. ;; Make the proper lists from the dates
  15667. (catch 'exit
  15668. (let ((a1 '(("h" . hour)
  15669. ("d" . day)
  15670. ("w" . week)
  15671. ("m" . month)
  15672. ("y" . year)))
  15673. (shour (nth 2 (org-parse-time-string start)))
  15674. dn dw sday cday n1 n2 n0
  15675. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15676. (setq start (org-date-to-gregorian start)
  15677. current (org-date-to-gregorian
  15678. (if show-all
  15679. current
  15680. (time-to-days (current-time))))
  15681. sday (calendar-absolute-from-gregorian start)
  15682. cday (calendar-absolute-from-gregorian current))
  15683. (if (<= cday sday) (throw 'exit sday))
  15684. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15685. (setq dn (string-to-number (match-string 1 change))
  15686. dw (cdr (assoc (match-string 2 change) a1)))
  15687. (user-error "Invalid change specifier: %s" change))
  15688. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15689. (cond
  15690. ((eq dw 'hour)
  15691. (let ((missing-hours
  15692. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15693. dn)))
  15694. (setq n1 (if (zerop missing-hours) cday
  15695. (- cday (1+ (floor (/ missing-hours 24)))))
  15696. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15697. ((eq dw 'day)
  15698. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15699. n2 (+ n1 dn)))
  15700. ((eq dw 'year)
  15701. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15702. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15703. (setq date1 (list m d y1)
  15704. n1 (calendar-absolute-from-gregorian date1)
  15705. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15706. n2 (calendar-absolute-from-gregorian date2)))
  15707. ((eq dw 'month)
  15708. ;; approx number of month between the two dates
  15709. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15710. ;; How often does dn fit in there?
  15711. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15712. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15713. m (+ m nm)
  15714. ny (floor (/ m 12))
  15715. y (+ y ny)
  15716. m (- m (* ny 12)))
  15717. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15718. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15719. (setq m2 (+ m dn) y2 y)
  15720. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15721. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15722. (while (<= n2 cday)
  15723. (setq n1 n2 m m2 y y2)
  15724. (setq m2 (+ m dn) y2 y)
  15725. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15726. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15727. ;; Make sure n1 is the earlier date
  15728. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15729. (if show-all
  15730. (cond
  15731. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15732. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15733. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15734. (cond
  15735. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15736. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15737. (t (if (= cday n1) n1 n2)))))))
  15738. (defun org-date-to-gregorian (date)
  15739. "Turn any specification of DATE into a Gregorian date for the calendar."
  15740. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15741. ((and (listp date) (= (length date) 3)) date)
  15742. ((stringp date)
  15743. (setq date (org-parse-time-string date))
  15744. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15745. ((listp date)
  15746. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15747. (defun org-parse-time-string (s &optional nodefault)
  15748. "Parse the standard Org-mode time string.
  15749. This should be a lot faster than the normal `parse-time-string'.
  15750. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15751. hour and minute fields will be nil if not given."
  15752. (cond ((string-match org-ts-regexp0 s)
  15753. (list 0
  15754. (if (or (match-beginning 8) (not nodefault))
  15755. (string-to-number (or (match-string 8 s) "0")))
  15756. (if (or (match-beginning 7) (not nodefault))
  15757. (string-to-number (or (match-string 7 s) "0")))
  15758. (string-to-number (match-string 4 s))
  15759. (string-to-number (match-string 3 s))
  15760. (string-to-number (match-string 2 s))
  15761. nil nil nil))
  15762. ((string-match "^<[^>]+>$" s)
  15763. (decode-time (seconds-to-time (org-matcher-time s))))
  15764. (t (error "Not a standard Org-mode time string: %s" s))))
  15765. (defun org-timestamp-up (&optional arg)
  15766. "Increase the date item at the cursor by one.
  15767. If the cursor is on the year, change the year. If it is on the month,
  15768. the day or the time, change that.
  15769. With prefix ARG, change by that many units."
  15770. (interactive "p")
  15771. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15772. (defun org-timestamp-down (&optional arg)
  15773. "Decrease the date item at the cursor by one.
  15774. If the cursor is on the year, change the year. If it is on the month,
  15775. the day or the time, change that.
  15776. With prefix ARG, change by that many units."
  15777. (interactive "p")
  15778. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15779. (defun org-timestamp-up-day (&optional arg)
  15780. "Increase the date in the time stamp by one day.
  15781. With prefix ARG, change that many days."
  15782. (interactive "p")
  15783. (if (and (not (org-at-timestamp-p t))
  15784. (org-at-heading-p))
  15785. (org-todo 'up)
  15786. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15787. (defun org-timestamp-down-day (&optional arg)
  15788. "Decrease the date in the time stamp by one day.
  15789. With prefix ARG, change that many days."
  15790. (interactive "p")
  15791. (if (and (not (org-at-timestamp-p t))
  15792. (org-at-heading-p))
  15793. (org-todo 'down)
  15794. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15795. (defun org-at-timestamp-p (&optional inactive-ok)
  15796. "Determine if the cursor is in or at a timestamp."
  15797. (interactive)
  15798. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15799. (pos (point))
  15800. (ans (or (looking-at tsr)
  15801. (save-excursion
  15802. (skip-chars-backward "^[<\n\r\t")
  15803. (if (> (point) (point-min)) (backward-char 1))
  15804. (and (looking-at tsr)
  15805. (> (- (match-end 0) pos) -1))))))
  15806. (and ans
  15807. (boundp 'org-ts-what)
  15808. (setq org-ts-what
  15809. (cond
  15810. ((= pos (match-beginning 0)) 'bracket)
  15811. ;; Point is considered to be "on the bracket" whether
  15812. ;; it's really on it or right after it.
  15813. ((= pos (1- (match-end 0))) 'bracket)
  15814. ((= pos (match-end 0)) 'after)
  15815. ((org-pos-in-match-range pos 2) 'year)
  15816. ((org-pos-in-match-range pos 3) 'month)
  15817. ((org-pos-in-match-range pos 7) 'hour)
  15818. ((org-pos-in-match-range pos 8) 'minute)
  15819. ((or (org-pos-in-match-range pos 4)
  15820. (org-pos-in-match-range pos 5)) 'day)
  15821. ((and (> pos (or (match-end 8) (match-end 5)))
  15822. (< pos (match-end 0)))
  15823. (- pos (or (match-end 8) (match-end 5))))
  15824. (t 'day))))
  15825. ans))
  15826. (defun org-toggle-timestamp-type ()
  15827. "Toggle the type (<active> or [inactive]) of a time stamp."
  15828. (interactive)
  15829. (when (org-at-timestamp-p t)
  15830. (let ((beg (match-beginning 0)) (end (match-end 0))
  15831. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15832. (save-excursion
  15833. (goto-char beg)
  15834. (while (re-search-forward "[][<>]" end t)
  15835. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15836. t t)))
  15837. (message "Timestamp is now %sactive"
  15838. (if (equal (char-after beg) ?<) "" "in")))))
  15839. (defun org-at-clock-log-p nil
  15840. "Is the cursor on the clock log line?"
  15841. (save-excursion
  15842. (move-beginning-of-line 1)
  15843. (looking-at "^[ \t]*CLOCK:")))
  15844. (defvar org-clock-history) ; defined in org-clock.el
  15845. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15846. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15847. "Change the date in the time stamp at point.
  15848. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15849. `year', `minute', `second'. If WHAT is not given, the cursor position
  15850. in the timestamp determines what will be changed.
  15851. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15852. (let ((origin (point)) origin-cat
  15853. with-hm inactive
  15854. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15855. org-ts-what
  15856. extra rem
  15857. ts time time0 fixnext clrgx)
  15858. (if (not (org-at-timestamp-p t))
  15859. (user-error "Not at a timestamp"))
  15860. (if (and (not what) (eq org-ts-what 'bracket))
  15861. (org-toggle-timestamp-type)
  15862. ;; Point isn't on brackets. Remember the part of the time-stamp
  15863. ;; the point was in. Indeed, size of time-stamps may change,
  15864. ;; but point must be kept in the same category nonetheless.
  15865. (setq origin-cat org-ts-what)
  15866. (if (and (not what) (not (eq org-ts-what 'day))
  15867. org-display-custom-times
  15868. (get-text-property (point) 'display)
  15869. (not (get-text-property (1- (point)) 'display)))
  15870. (setq org-ts-what 'day))
  15871. (setq org-ts-what (or what org-ts-what)
  15872. inactive (= (char-after (match-beginning 0)) ?\[)
  15873. ts (match-string 0))
  15874. (replace-match "")
  15875. (when (string-match
  15876. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15877. ts)
  15878. (setq extra (match-string 1 ts))
  15879. (if suppress-tmp-delay
  15880. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15881. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15882. (setq with-hm t))
  15883. (setq time0 (org-parse-time-string ts))
  15884. (when (and updown
  15885. (eq org-ts-what 'minute)
  15886. (not current-prefix-arg))
  15887. ;; This looks like s-up and s-down. Change by one rounding step.
  15888. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15889. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15890. (setcar (cdr time0) (+ (nth 1 time0)
  15891. (if (> n 0) (- rem) (- dm rem))))))
  15892. (setq time
  15893. (encode-time (or (car time0) 0)
  15894. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15895. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15896. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15897. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15898. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15899. (nthcdr 6 time0)))
  15900. (when (and (member org-ts-what '(hour minute))
  15901. extra
  15902. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15903. (setq extra (org-modify-ts-extra
  15904. extra
  15905. (if (eq org-ts-what 'hour) 2 5)
  15906. n dm)))
  15907. (when (integerp org-ts-what)
  15908. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15909. (if (eq what 'calendar)
  15910. (let ((cal-date (org-get-date-from-calendar)))
  15911. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15912. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15913. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15914. (setcar time0 (or (car time0) 0))
  15915. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15916. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15917. (setq time (apply 'encode-time time0))))
  15918. ;; Insert the new time-stamp, and ensure point stays in the same
  15919. ;; category as before (i.e. not after the last position in that
  15920. ;; category).
  15921. (let ((pos (point)))
  15922. ;; Stay before inserted string. `save-excursion' is of no use.
  15923. (setq org-last-changed-timestamp
  15924. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15925. (goto-char pos))
  15926. (save-match-data
  15927. (looking-at org-ts-regexp3)
  15928. (goto-char (cond
  15929. ;; `day' category ends before `hour' if any, or at
  15930. ;; the end of the day name.
  15931. ((eq origin-cat 'day)
  15932. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15933. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15934. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15935. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15936. ;; `year' and `month' have both fixed size: point
  15937. ;; couldn't have moved into another part.
  15938. (t origin))))
  15939. ;; Update clock if on a CLOCK line.
  15940. (org-clock-update-time-maybe)
  15941. ;; Maybe adjust the closest clock in `org-clock-history'
  15942. (when org-clock-adjust-closest
  15943. (if (not (and (org-at-clock-log-p)
  15944. (< 1 (length (delq nil (mapcar 'marker-position
  15945. org-clock-history))))))
  15946. (message "No clock to adjust")
  15947. (cond ((save-excursion ; fix previous clock?
  15948. (re-search-backward org-ts-regexp0 nil t)
  15949. (org-looking-back (concat org-clock-string " \\[")))
  15950. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15951. ((save-excursion ; fix next clock?
  15952. (re-search-backward org-ts-regexp0 nil t)
  15953. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15954. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15955. (save-window-excursion
  15956. ;; Find closest clock to point, adjust the previous/next one in history
  15957. (let* ((p (save-excursion (org-back-to-heading t)))
  15958. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15959. (clfixnth
  15960. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15961. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15962. (if (not clfixpos)
  15963. (message "No clock to adjust")
  15964. (save-excursion
  15965. (org-goto-marker-or-bmk clfixpos)
  15966. (org-show-subtree)
  15967. (when (re-search-forward clrgx nil t)
  15968. (goto-char (match-beginning 1))
  15969. (let (org-clock-adjust-closest)
  15970. (org-timestamp-change n org-ts-what updown))
  15971. (message "Clock adjusted in %s for heading: %s"
  15972. (file-name-nondirectory (buffer-file-name))
  15973. (org-get-heading t t)))))))))
  15974. ;; Try to recenter the calendar window, if any.
  15975. (if (and org-calendar-follow-timestamp-change
  15976. (get-buffer-window "*Calendar*" t)
  15977. (memq org-ts-what '(day month year)))
  15978. (org-recenter-calendar (time-to-days time))))))
  15979. (defun org-modify-ts-extra (s pos n dm)
  15980. "Change the different parts of the lead-time and repeat fields in timestamp."
  15981. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15982. ng h m new rem)
  15983. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15984. (cond
  15985. ((or (org-pos-in-match-range pos 2)
  15986. (org-pos-in-match-range pos 3))
  15987. (setq m (string-to-number (match-string 3 s))
  15988. h (string-to-number (match-string 2 s)))
  15989. (if (org-pos-in-match-range pos 2)
  15990. (setq h (+ h n))
  15991. (setq n (* dm (org-no-warnings (signum n))))
  15992. (when (not (= 0 (setq rem (% m dm))))
  15993. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15994. (setq m (+ m n)))
  15995. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15996. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15997. (setq h (min 24 (max 0 h)))
  15998. (setq ng 1 new (format "-%02d:%02d" h m)))
  15999. ((org-pos-in-match-range pos 6)
  16000. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16001. ((org-pos-in-match-range pos 5)
  16002. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16003. ((org-pos-in-match-range pos 9)
  16004. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16005. ((org-pos-in-match-range pos 8)
  16006. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16007. (when ng
  16008. (setq s (concat
  16009. (substring s 0 (match-beginning ng))
  16010. new
  16011. (substring s (match-end ng))))))
  16012. s))
  16013. (defun org-recenter-calendar (date)
  16014. "If the calendar is visible, recenter it to DATE."
  16015. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16016. (when cwin
  16017. (let ((calendar-move-hook nil))
  16018. (with-selected-window cwin
  16019. (calendar-goto-date (if (listp date) date
  16020. (calendar-gregorian-from-absolute date))))))))
  16021. (defun org-goto-calendar (&optional arg)
  16022. "Go to the Emacs calendar at the current date.
  16023. If there is a time stamp in the current line, go to that date.
  16024. A prefix ARG can be used to force the current date."
  16025. (interactive "P")
  16026. (let ((tsr org-ts-regexp) diff
  16027. (calendar-move-hook nil)
  16028. (calendar-view-holidays-initially-flag nil)
  16029. (calendar-view-diary-initially-flag nil))
  16030. (if (or (org-at-timestamp-p)
  16031. (save-excursion
  16032. (beginning-of-line 1)
  16033. (looking-at (concat ".*" tsr))))
  16034. (let ((d1 (time-to-days (current-time)))
  16035. (d2 (time-to-days
  16036. (org-time-string-to-time (match-string 1)))))
  16037. (setq diff (- d2 d1))))
  16038. (calendar)
  16039. (calendar-goto-today)
  16040. (if (and diff (not arg)) (calendar-forward-day diff))))
  16041. (defun org-get-date-from-calendar ()
  16042. "Return a list (month day year) of date at point in calendar."
  16043. (with-current-buffer "*Calendar*"
  16044. (save-match-data
  16045. (calendar-cursor-to-date))))
  16046. (defun org-date-from-calendar ()
  16047. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16048. If there is already a time stamp at the cursor position, update it."
  16049. (interactive)
  16050. (if (org-at-timestamp-p t)
  16051. (org-timestamp-change 0 'calendar)
  16052. (let ((cal-date (org-get-date-from-calendar)))
  16053. (org-insert-time-stamp
  16054. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16055. (defcustom org-effort-durations
  16056. `(("h" . 60)
  16057. ("d" . ,(* 60 8))
  16058. ("w" . ,(* 60 8 5))
  16059. ("m" . ,(* 60 8 5 4))
  16060. ("y" . ,(* 60 8 5 40)))
  16061. "Conversion factor to minutes for an effort modifier.
  16062. Each entry has the form (MODIFIER . MINUTES).
  16063. In an effort string, a number followed by MODIFIER is multiplied
  16064. by the specified number of MINUTES to obtain an effort in
  16065. minutes.
  16066. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16067. effort string \"2hours\" is equivalent to 120 minutes."
  16068. :group 'org-agenda
  16069. :version "24.1"
  16070. :type '(alist :key-type (string :tag "Modifier")
  16071. :value-type (number :tag "Minutes")))
  16072. (defun org-minutes-to-clocksum-string (m)
  16073. "Format number of minutes as a clocksum string.
  16074. The format is determined by `org-time-clocksum-format',
  16075. `org-time-clocksum-use-fractional' and
  16076. `org-time-clocksum-fractional-format' and
  16077. `org-time-clocksum-use-effort-durations'."
  16078. (let ((clocksum "")
  16079. (m (round m)) ; Don't allow fractions of minutes
  16080. h d w mo y fmt n)
  16081. (setq h (if org-time-clocksum-use-effort-durations
  16082. (cdr (assoc "h" org-effort-durations)) 60)
  16083. d (if org-time-clocksum-use-effort-durations
  16084. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16085. w (if org-time-clocksum-use-effort-durations
  16086. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16087. mo (if org-time-clocksum-use-effort-durations
  16088. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16089. y (if org-time-clocksum-use-effort-durations
  16090. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16091. ;; fractional format
  16092. (if org-time-clocksum-use-fractional
  16093. (cond
  16094. ;; single format string
  16095. ((stringp org-time-clocksum-fractional-format)
  16096. (format org-time-clocksum-fractional-format (/ m (float h))))
  16097. ;; choice of fractional formats for different time units
  16098. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16099. (> (/ (truncate m) (* y d h)) 0))
  16100. (format fmt (/ m (* y d (float h)))))
  16101. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16102. (> (/ (truncate m) (* mo d h)) 0))
  16103. (format fmt (/ m (* mo d (float h)))))
  16104. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16105. (> (/ (truncate m) (* w d h)) 0))
  16106. (format fmt (/ m (* w d (float h)))))
  16107. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16108. (> (/ (truncate m) (* d h)) 0))
  16109. (format fmt (/ m (* d (float h)))))
  16110. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16111. (> (/ (truncate m) h) 0))
  16112. (format fmt (/ m (float h))))
  16113. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16114. (format fmt m))
  16115. ;; fall back to smallest time unit with a format
  16116. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16117. (format fmt (/ m (float h))))
  16118. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16119. (format fmt (/ m (* d (float h)))))
  16120. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16121. (format fmt (/ m (* w d (float h)))))
  16122. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16123. (format fmt (/ m (* mo d (float h)))))
  16124. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16125. (format fmt (/ m (* y d (float h))))))
  16126. ;; standard (non-fractional) format, with single format string
  16127. (if (stringp org-time-clocksum-format)
  16128. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16129. ;; separate formats components
  16130. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16131. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16132. (plist-get org-time-clocksum-format :require-years))
  16133. (setq clocksum (concat clocksum (format fmt n))
  16134. m (- m (* n y d h))))
  16135. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16136. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16137. (plist-get org-time-clocksum-format :require-months))
  16138. (setq clocksum (concat clocksum (format fmt n))
  16139. m (- m (* n mo d h))))
  16140. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16141. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16142. (plist-get org-time-clocksum-format :require-weeks))
  16143. (setq clocksum (concat clocksum (format fmt n))
  16144. m (- m (* n w d h))))
  16145. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16146. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16147. (plist-get org-time-clocksum-format :require-days))
  16148. (setq clocksum (concat clocksum (format fmt n))
  16149. m (- m (* n d h))))
  16150. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16151. (or (> (setq n (/ (truncate m) h)) 0)
  16152. (plist-get org-time-clocksum-format :require-hours))
  16153. (setq clocksum (concat clocksum (format fmt n))
  16154. m (- m (* n h))))
  16155. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16156. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16157. (setq clocksum (concat clocksum (format fmt m))))
  16158. ;; return formatted time duration
  16159. clocksum))))
  16160. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16161. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16162. "Org mode version 8.0")
  16163. (defun org-hours-to-clocksum-string (n)
  16164. (org-minutes-to-clocksum-string (* n 60)))
  16165. (defun org-hh:mm-string-to-minutes (s)
  16166. "Convert a string H:MM to a number of minutes.
  16167. If the string is just a number, interpret it as minutes.
  16168. In fact, the first hh:mm or number in the string will be taken,
  16169. there can be extra stuff in the string.
  16170. If no number is found, the return value is 0."
  16171. (cond
  16172. ((integerp s) s)
  16173. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16174. (+ (* (string-to-number (match-string 1 s)) 60)
  16175. (string-to-number (match-string 2 s))))
  16176. ((string-match "\\([0-9]+\\)" s)
  16177. (string-to-number (match-string 1 s)))
  16178. (t 0)))
  16179. (defcustom org-image-actual-width t
  16180. "Should we use the actual width of images when inlining them?
  16181. When set to `t', always use the image width.
  16182. When set to a number, use imagemagick (when available) to set
  16183. the image's width to this value.
  16184. When set to a number in a list, try to get the width from any
  16185. #+ATTR.* keyword if it matches a width specification like
  16186. #+ATTR_HTML: :width 300px
  16187. and fall back on that number if none is found.
  16188. When set to nil, try to get the width from an #+ATTR.* keyword
  16189. and fall back on the original width if none is found.
  16190. This requires Emacs >= 24.1, build with imagemagick support."
  16191. :group 'org-appearance
  16192. :version "24.4"
  16193. :package-version '(Org . "8.0")
  16194. :type '(choice
  16195. (const :tag "Use the image width" t)
  16196. (integer :tag "Use a number of pixels")
  16197. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16198. (const :tag "Use #+ATTR* or don't resize" nil)))
  16199. (defcustom org-agenda-inhibit-startup nil
  16200. "Inhibit startup when preparing agenda buffers.
  16201. When this variable is `t' (the default), the initialization of
  16202. the Org agenda buffers is inhibited: e.g. the visibility state
  16203. is not set, the tables are not re-aligned, etc."
  16204. :type 'boolean
  16205. :version "24.3"
  16206. :group 'org-agenda)
  16207. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16208. "Convert a duration string S to minutes.
  16209. A bare number is interpreted as minutes, modifiers can be set by
  16210. customizing `org-effort-durations' (which see).
  16211. Entries containing a colon are interpreted as H:MM by
  16212. `org-hh:mm-string-to-minutes'."
  16213. (let ((result 0)
  16214. (re (concat "\\([0-9.]+\\) *\\("
  16215. (regexp-opt (mapcar 'car org-effort-durations))
  16216. "\\)")))
  16217. (while (string-match re s)
  16218. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16219. (string-to-number (match-string 1 s))))
  16220. (setq s (replace-match "" nil t s)))
  16221. (setq result (floor result))
  16222. (incf result (org-hh:mm-string-to-minutes s))
  16223. (if output-to-string (number-to-string result) result)))
  16224. ;;;; Files
  16225. (defun org-save-all-org-buffers ()
  16226. "Save all Org-mode buffers without user confirmation."
  16227. (interactive)
  16228. (message "Saving all Org-mode buffers...")
  16229. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16230. (when (featurep 'org-id) (org-id-locations-save))
  16231. (message "Saving all Org-mode buffers... done"))
  16232. (defun org-revert-all-org-buffers ()
  16233. "Revert all Org-mode buffers.
  16234. Prompt for confirmation when there are unsaved changes.
  16235. Be sure you know what you are doing before letting this function
  16236. overwrite your changes.
  16237. This function is useful in a setup where one tracks org files
  16238. with a version control system, to revert on one machine after pulling
  16239. changes from another. I believe the procedure must be like this:
  16240. 1. M-x org-save-all-org-buffers
  16241. 2. Pull changes from the other machine, resolve conflicts
  16242. 3. M-x org-revert-all-org-buffers"
  16243. (interactive)
  16244. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16245. (user-error "Abort"))
  16246. (save-excursion
  16247. (save-window-excursion
  16248. (mapc
  16249. (lambda (b)
  16250. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16251. (with-current-buffer b buffer-file-name))
  16252. (org-pop-to-buffer-same-window b)
  16253. (revert-buffer t 'no-confirm)))
  16254. (buffer-list))
  16255. (when (and (featurep 'org-id) org-id-track-globally)
  16256. (org-id-locations-load)))))
  16257. ;;;; Agenda files
  16258. ;;;###autoload
  16259. (defun org-switchb (&optional arg)
  16260. "Switch between Org buffers.
  16261. With one prefix argument, restrict available buffers to files.
  16262. With two prefix arguments, restrict available buffers to agenda files.
  16263. Defaults to `iswitchb' for buffer name completion.
  16264. Set `org-completion-use-ido' to make it use ido instead."
  16265. (interactive "P")
  16266. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16267. ((equal arg '(16)) (org-buffer-list 'agenda))
  16268. (t (org-buffer-list))))
  16269. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16270. (org-completion-use-ido org-completion-use-ido))
  16271. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16272. (setq org-completion-use-iswitchb t))
  16273. (org-pop-to-buffer-same-window
  16274. (org-icompleting-read "Org buffer: "
  16275. (mapcar 'list (mapcar 'buffer-name blist))
  16276. nil t))))
  16277. ;;; Define some older names previously used for this functionality
  16278. ;;;###autoload
  16279. (defalias 'org-ido-switchb 'org-switchb)
  16280. ;;;###autoload
  16281. (defalias 'org-iswitchb 'org-switchb)
  16282. (defun org-buffer-list (&optional predicate exclude-tmp)
  16283. "Return a list of Org buffers.
  16284. PREDICATE can be `export', `files' or `agenda'.
  16285. export restrict the list to Export buffers.
  16286. files restrict the list to buffers visiting Org files.
  16287. agenda restrict the list to buffers visiting agenda files.
  16288. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16289. (let* ((bfn nil)
  16290. (agenda-files (and (eq predicate 'agenda)
  16291. (mapcar 'file-truename (org-agenda-files t))))
  16292. (filter
  16293. (cond
  16294. ((eq predicate 'files)
  16295. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16296. ((eq predicate 'export)
  16297. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16298. ((eq predicate 'agenda)
  16299. (lambda (b)
  16300. (with-current-buffer b
  16301. (and (derived-mode-p 'org-mode)
  16302. (setq bfn (buffer-file-name b))
  16303. (member (file-truename bfn) agenda-files)))))
  16304. (t (lambda (b) (with-current-buffer b
  16305. (or (derived-mode-p 'org-mode)
  16306. (string-match "\*Org .*Export"
  16307. (buffer-name b)))))))))
  16308. (delq nil
  16309. (mapcar
  16310. (lambda(b)
  16311. (if (and (funcall filter b)
  16312. (or (not exclude-tmp)
  16313. (not (string-match "tmp" (buffer-name b)))))
  16314. b
  16315. nil))
  16316. (buffer-list)))))
  16317. (defun org-agenda-files (&optional unrestricted archives)
  16318. "Get the list of agenda files.
  16319. Optional UNRESTRICTED means return the full list even if a restriction
  16320. is currently in place.
  16321. When ARCHIVES is t, include all archive files that are really being
  16322. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16323. `org-agenda-archives-mode' is t."
  16324. (let ((files
  16325. (cond
  16326. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16327. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16328. ((listp org-agenda-files) org-agenda-files)
  16329. (t (error "Invalid value of `org-agenda-files'")))))
  16330. (setq files (apply 'append
  16331. (mapcar (lambda (f)
  16332. (if (file-directory-p f)
  16333. (directory-files
  16334. f t org-agenda-file-regexp)
  16335. (list f)))
  16336. files)))
  16337. (when org-agenda-skip-unavailable-files
  16338. (setq files (delq nil
  16339. (mapcar (function
  16340. (lambda (file)
  16341. (and (file-readable-p file) file)))
  16342. files))))
  16343. (when (or (eq archives t)
  16344. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16345. (setq files (org-add-archive-files files)))
  16346. files))
  16347. (defun org-agenda-file-p (&optional file)
  16348. "Return non-nil, if FILE is an agenda file.
  16349. If FILE is omitted, use the file associated with the current
  16350. buffer."
  16351. (member (or file (buffer-file-name))
  16352. (org-agenda-files t)))
  16353. (defun org-edit-agenda-file-list ()
  16354. "Edit the list of agenda files.
  16355. Depending on setup, this either uses customize to edit the variable
  16356. `org-agenda-files', or it visits the file that is holding the list. In the
  16357. latter case, the buffer is set up in a way that saving it automatically kills
  16358. the buffer and restores the previous window configuration."
  16359. (interactive)
  16360. (if (stringp org-agenda-files)
  16361. (let ((cw (current-window-configuration)))
  16362. (find-file org-agenda-files)
  16363. (org-set-local 'org-window-configuration cw)
  16364. (org-add-hook 'after-save-hook
  16365. (lambda ()
  16366. (set-window-configuration
  16367. (prog1 org-window-configuration
  16368. (kill-buffer (current-buffer))))
  16369. (org-install-agenda-files-menu)
  16370. (message "New agenda file list installed"))
  16371. nil 'local)
  16372. (message "%s" (substitute-command-keys
  16373. "Edit list and finish with \\[save-buffer]")))
  16374. (customize-variable 'org-agenda-files)))
  16375. (defun org-store-new-agenda-file-list (list)
  16376. "Set new value for the agenda file list and save it correctly."
  16377. (if (stringp org-agenda-files)
  16378. (let ((fe (org-read-agenda-file-list t)) b u)
  16379. (while (setq b (find-buffer-visiting org-agenda-files))
  16380. (kill-buffer b))
  16381. (with-temp-file org-agenda-files
  16382. (insert
  16383. (mapconcat
  16384. (lambda (f) ;; Keep un-expanded entries.
  16385. (if (setq u (assoc f fe))
  16386. (cdr u)
  16387. f))
  16388. list "\n")
  16389. "\n")))
  16390. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16391. (org-insert-mode-line-in-empty-file nil))
  16392. (setq org-agenda-files list)
  16393. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16394. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16395. "Read the list of agenda files from a file.
  16396. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16397. filenames, used by `org-store-new-agenda-file-list' to write back
  16398. un-expanded file names."
  16399. (when (file-directory-p org-agenda-files)
  16400. (error "`org-agenda-files' cannot be a single directory"))
  16401. (when (stringp org-agenda-files)
  16402. (with-temp-buffer
  16403. (insert-file-contents org-agenda-files)
  16404. (mapcar
  16405. (lambda (f)
  16406. (let ((e (expand-file-name (substitute-in-file-name f)
  16407. org-directory)))
  16408. (if pair-with-expansion
  16409. (cons e f)
  16410. e)))
  16411. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16412. ;;;###autoload
  16413. (defun org-cycle-agenda-files ()
  16414. "Cycle through the files in `org-agenda-files'.
  16415. If the current buffer visits an agenda file, find the next one in the list.
  16416. If the current buffer does not, find the first agenda file."
  16417. (interactive)
  16418. (let* ((fs (org-agenda-files t))
  16419. (files (append fs (list (car fs))))
  16420. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16421. file)
  16422. (unless files (user-error "No agenda files"))
  16423. (catch 'exit
  16424. (while (setq file (pop files))
  16425. (if (equal (file-truename file) tcf)
  16426. (when (car files)
  16427. (find-file (car files))
  16428. (throw 'exit t))))
  16429. (find-file (car fs)))
  16430. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16431. (defun org-agenda-file-to-front (&optional to-end)
  16432. "Move/add the current file to the top of the agenda file list.
  16433. If the file is not present in the list, it is added to the front. If it is
  16434. present, it is moved there. With optional argument TO-END, add/move to the
  16435. end of the list."
  16436. (interactive "P")
  16437. (let ((org-agenda-skip-unavailable-files nil)
  16438. (file-alist (mapcar (lambda (x)
  16439. (cons (file-truename x) x))
  16440. (org-agenda-files t)))
  16441. (ctf (file-truename
  16442. (or buffer-file-name
  16443. (user-error "Please save the current buffer to a file"))))
  16444. x had)
  16445. (setq x (assoc ctf file-alist) had x)
  16446. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16447. (if to-end
  16448. (setq file-alist (append (delq x file-alist) (list x)))
  16449. (setq file-alist (cons x (delq x file-alist))))
  16450. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16451. (org-install-agenda-files-menu)
  16452. (message "File %s to %s of agenda file list"
  16453. (if had "moved" "added") (if to-end "end" "front"))))
  16454. (defun org-remove-file (&optional file)
  16455. "Remove current file from the list of files in variable `org-agenda-files'.
  16456. These are the files which are being checked for agenda entries.
  16457. Optional argument FILE means use this file instead of the current."
  16458. (interactive)
  16459. (let* ((org-agenda-skip-unavailable-files nil)
  16460. (file (or file buffer-file-name
  16461. (user-error "Current buffer does not visit a file")))
  16462. (true-file (file-truename file))
  16463. (afile (abbreviate-file-name file))
  16464. (files (delq nil (mapcar
  16465. (lambda (x)
  16466. (if (equal true-file
  16467. (file-truename x))
  16468. nil x))
  16469. (org-agenda-files t)))))
  16470. (if (not (= (length files) (length (org-agenda-files t))))
  16471. (progn
  16472. (org-store-new-agenda-file-list files)
  16473. (org-install-agenda-files-menu)
  16474. (message "Removed file: %s" afile))
  16475. (message "File was not in list: %s (not removed)" afile))))
  16476. (defun org-file-menu-entry (file)
  16477. (vector file (list 'find-file file) t))
  16478. (defun org-check-agenda-file (file)
  16479. "Make sure FILE exists. If not, ask user what to do."
  16480. (when (not (file-exists-p file))
  16481. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16482. (abbreviate-file-name file))
  16483. (let ((r (downcase (read-char-exclusive))))
  16484. (cond
  16485. ((equal r ?r)
  16486. (org-remove-file file)
  16487. (throw 'nextfile t))
  16488. (t (error "Abort"))))))
  16489. (defun org-get-agenda-file-buffer (file)
  16490. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16491. it to the list of buffers which might be released later."
  16492. (let ((buf (org-find-base-buffer-visiting file)))
  16493. (if buf
  16494. buf ; just return it
  16495. ;; Make a new buffer and remember it
  16496. (setq buf (find-file-noselect file))
  16497. (if buf (push buf org-agenda-new-buffers))
  16498. buf)))
  16499. (defun org-release-buffers (blist)
  16500. "Release all buffers in list, asking the user for confirmation when needed.
  16501. When a buffer is unmodified, it is just killed. When modified, it is saved
  16502. \(if the user agrees) and then killed."
  16503. (let (buf file)
  16504. (while (setq buf (pop blist))
  16505. (setq file (buffer-file-name buf))
  16506. (when (and (buffer-modified-p buf)
  16507. file
  16508. (y-or-n-p (format "Save file %s? " file)))
  16509. (with-current-buffer buf (save-buffer)))
  16510. (kill-buffer buf))))
  16511. (defun org-agenda-prepare-buffers (files)
  16512. "Create buffers for all agenda files, protect archived trees and comments."
  16513. (interactive)
  16514. (let ((pa '(:org-archived t))
  16515. (pc '(:org-comment t))
  16516. (pall '(:org-archived t :org-comment t))
  16517. (inhibit-read-only t)
  16518. (org-inhibit-startup org-agenda-inhibit-startup)
  16519. (rea (concat ":" org-archive-tag ":"))
  16520. file re)
  16521. (setq org-tag-alist-for-agenda nil
  16522. org-tag-groups-alist-for-agenda nil)
  16523. (save-excursion
  16524. (save-restriction
  16525. (while (setq file (pop files))
  16526. (catch 'nextfile
  16527. (if (bufferp file)
  16528. (set-buffer file)
  16529. (org-check-agenda-file file)
  16530. (set-buffer (org-get-agenda-file-buffer file)))
  16531. (widen)
  16532. (org-set-regexps-and-options-for-tags)
  16533. (goto-char (point-min))
  16534. (let ((case-fold-search t))
  16535. (when (search-forward "#+setupfile" nil t)
  16536. ;; Don't set all regexps and options systematically as
  16537. ;; this is only run for setting agenda tags from setup
  16538. ;; file
  16539. (org-set-regexps-and-options)))
  16540. (org-refresh-category-properties)
  16541. (org-refresh-properties org-effort-property 'org-effort)
  16542. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16543. (setq org-todo-keywords-for-agenda
  16544. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16545. (setq org-done-keywords-for-agenda
  16546. (append org-done-keywords-for-agenda org-done-keywords))
  16547. (setq org-todo-keyword-alist-for-agenda
  16548. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16549. (setq org-drawers-for-agenda
  16550. (append org-drawers-for-agenda org-drawers))
  16551. (setq org-tag-alist-for-agenda
  16552. (org-uniquify
  16553. (append org-tag-alist-for-agenda
  16554. org-tag-alist
  16555. org-tag-persistent-alist)))
  16556. (if org-group-tags
  16557. (setq org-tag-groups-alist-for-agenda
  16558. (org-uniquify-alist
  16559. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16560. (org-with-silent-modifications
  16561. (save-excursion
  16562. (remove-text-properties (point-min) (point-max) pall)
  16563. (when org-agenda-skip-archived-trees
  16564. (goto-char (point-min))
  16565. (while (re-search-forward rea nil t)
  16566. (if (org-at-heading-p t)
  16567. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16568. (goto-char (point-min))
  16569. (setq re (format org-heading-keyword-regexp-format
  16570. org-comment-string))
  16571. (while (re-search-forward re nil t)
  16572. (add-text-properties
  16573. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16574. (setq org-todo-keywords-for-agenda
  16575. (org-uniquify org-todo-keywords-for-agenda))
  16576. (setq org-todo-keyword-alist-for-agenda
  16577. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16578. ;;;; CDLaTeX minor mode
  16579. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16580. "Keymap for the minor `org-cdlatex-mode'.")
  16581. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16582. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16583. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16584. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16585. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16586. (defvar org-cdlatex-texmathp-advice-is-done nil
  16587. "Flag remembering if we have applied the advice to texmathp already.")
  16588. (define-minor-mode org-cdlatex-mode
  16589. "Toggle the minor `org-cdlatex-mode'.
  16590. This mode supports entering LaTeX environment and math in LaTeX fragments
  16591. in Org-mode.
  16592. \\{org-cdlatex-mode-map}"
  16593. nil " OCDL" nil
  16594. (when org-cdlatex-mode
  16595. (require 'cdlatex)
  16596. (run-hooks 'cdlatex-mode-hook)
  16597. (cdlatex-compute-tables))
  16598. (unless org-cdlatex-texmathp-advice-is-done
  16599. (setq org-cdlatex-texmathp-advice-is-done t)
  16600. (defadvice texmathp (around org-math-always-on activate)
  16601. "Always return t in org-mode buffers.
  16602. This is because we want to insert math symbols without dollars even outside
  16603. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16604. an embedded LaTeX fragment, let texmathp do its job.
  16605. \\[org-cdlatex-mode-map]"
  16606. (interactive)
  16607. (let (p)
  16608. (cond
  16609. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16610. ((eq this-command 'cdlatex-math-symbol)
  16611. (setq ad-return-value t
  16612. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16613. (t
  16614. (let ((p (org-inside-LaTeX-fragment-p)))
  16615. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16616. (setq ad-return-value t
  16617. texmathp-why '("Org-mode embedded math" . 0))
  16618. (if p ad-do-it)))))))))
  16619. (defun turn-on-org-cdlatex ()
  16620. "Unconditionally turn on `org-cdlatex-mode'."
  16621. (org-cdlatex-mode 1))
  16622. (defun org-try-cdlatex-tab ()
  16623. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16624. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16625. - inside a LaTeX fragment, or
  16626. - after the first word in a line, where an abbreviation expansion could
  16627. insert a LaTeX environment."
  16628. (when org-cdlatex-mode
  16629. (cond
  16630. ;; Before any word on the line: No expansion possible.
  16631. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16632. ;; Just after first word on the line: Expand it. Make sure it
  16633. ;; cannot happen on headlines, though.
  16634. ((save-excursion
  16635. (skip-chars-backward "a-zA-Z0-9*")
  16636. (skip-chars-backward " \t")
  16637. (and (bolp) (not (org-at-heading-p))))
  16638. (cdlatex-tab) t)
  16639. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16640. (defun org-cdlatex-underscore-caret (&optional arg)
  16641. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16642. Revert to the normal definition outside of these fragments."
  16643. (interactive "P")
  16644. (if (org-inside-LaTeX-fragment-p)
  16645. (call-interactively 'cdlatex-sub-superscript)
  16646. (let (org-cdlatex-mode)
  16647. (call-interactively (key-binding (vector last-input-event))))))
  16648. (defun org-cdlatex-math-modify (&optional arg)
  16649. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16650. Revert to the normal definition outside of these fragments."
  16651. (interactive "P")
  16652. (if (org-inside-LaTeX-fragment-p)
  16653. (call-interactively 'cdlatex-math-modify)
  16654. (let (org-cdlatex-mode)
  16655. (call-interactively (key-binding (vector last-input-event))))))
  16656. ;;;; LaTeX fragments
  16657. (defvar org-latex-regexps
  16658. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16659. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16660. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16661. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16662. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16663. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16664. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16665. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16666. "Regular expressions for matching embedded LaTeX.")
  16667. (defun org-inside-LaTeX-fragment-p ()
  16668. "Test if point is inside a LaTeX fragment.
  16669. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16670. sequence appearing also before point.
  16671. Even though the matchers for math are configurable, this function assumes
  16672. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16673. delimiters are skipped when they have been removed by customization.
  16674. The return value is nil, or a cons cell with the delimiter and the
  16675. position of this delimiter.
  16676. This function does a reasonably good job, but can locally be fooled by
  16677. for example currency specifications. For example it will assume being in
  16678. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16679. fragments that are properly closed, but during editing, we have to live
  16680. with the uncertainty caused by missing closing delimiters. This function
  16681. looks only before point, not after."
  16682. (catch 'exit
  16683. (let ((pos (point))
  16684. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16685. (lim (progn
  16686. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16687. (point)))
  16688. dd-on str (start 0) m re)
  16689. (goto-char pos)
  16690. (when dodollar
  16691. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16692. re (nth 1 (assoc "$" org-latex-regexps)))
  16693. (while (string-match re str start)
  16694. (cond
  16695. ((= (match-end 0) (length str))
  16696. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16697. ((= (match-end 0) (- (length str) 5))
  16698. (throw 'exit nil))
  16699. (t (setq start (match-end 0))))))
  16700. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16701. (goto-char pos)
  16702. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16703. (and (match-beginning 2) (throw 'exit nil))
  16704. ;; count $$
  16705. (while (re-search-backward "\\$\\$" lim t)
  16706. (setq dd-on (not dd-on)))
  16707. (goto-char pos)
  16708. (if dd-on (cons "$$" m))))))
  16709. (defun org-inside-latex-macro-p ()
  16710. "Is point inside a LaTeX macro or its arguments?"
  16711. (save-match-data
  16712. (org-in-regexp
  16713. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16714. (defvar org-latex-fragment-image-overlays nil
  16715. "List of overlays carrying the images of latex fragments.")
  16716. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16717. (defun org-remove-latex-fragment-image-overlays ()
  16718. "Remove all overlays with LaTeX fragment images in current buffer."
  16719. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16720. (setq org-latex-fragment-image-overlays nil))
  16721. (defun org-preview-latex-fragment (&optional subtree)
  16722. "Preview the LaTeX fragment at point, or all locally or globally.
  16723. If the cursor is in a LaTeX fragment, create the image and overlay
  16724. it over the source code. If there is no fragment at point, display
  16725. all fragments in the current text, from one headline to the next. With
  16726. prefix SUBTREE, display all fragments in the current subtree. With a
  16727. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16728. the cursor is before the first headline,
  16729. display all fragments in the buffer.
  16730. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16731. (interactive "P")
  16732. (unless buffer-file-name
  16733. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16734. (when (display-graphic-p)
  16735. (org-remove-latex-fragment-image-overlays)
  16736. (save-excursion
  16737. (save-restriction
  16738. (let (beg end at msg)
  16739. (cond
  16740. ((or (equal subtree '(16))
  16741. (not (save-excursion
  16742. (re-search-backward org-outline-regexp-bol nil t))))
  16743. (setq beg (point-min) end (point-max)
  16744. msg "Creating images for buffer...%s"))
  16745. ((equal subtree '(4))
  16746. (org-back-to-heading)
  16747. (setq beg (point) end (org-end-of-subtree t)
  16748. msg "Creating images for subtree...%s"))
  16749. (t
  16750. (if (setq at (org-inside-LaTeX-fragment-p))
  16751. (goto-char (max (point-min) (- (cdr at) 2)))
  16752. (org-back-to-heading))
  16753. (setq beg (point) end (progn (outline-next-heading) (point))
  16754. msg (if at "Creating image...%s"
  16755. "Creating images for entry...%s"))))
  16756. (message msg "")
  16757. (narrow-to-region beg end)
  16758. (goto-char beg)
  16759. (org-format-latex
  16760. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16761. (file-name-nondirectory
  16762. buffer-file-name)))
  16763. default-directory 'overlays msg at 'forbuffer
  16764. org-latex-create-formula-image-program)
  16765. (message msg "done. Use `C-c C-c' to remove images."))))))
  16766. (defun org-format-latex (prefix &optional dir overlays msg at
  16767. forbuffer processing-type)
  16768. "Replace LaTeX fragments with links to an image, and produce images.
  16769. Some of the options can be changed using the variable
  16770. `org-format-latex-options'."
  16771. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16772. (let* ((prefixnodir (file-name-nondirectory prefix))
  16773. (absprefix (expand-file-name prefix dir))
  16774. (todir (file-name-directory absprefix))
  16775. (opt org-format-latex-options)
  16776. (optnew org-format-latex-options)
  16777. (matchers (plist-get opt :matchers))
  16778. (re-list org-latex-regexps)
  16779. (cnt 0) txt hash link beg end re e checkdir
  16780. string
  16781. m n block-type block linkfile movefile ov)
  16782. ;; Check the different regular expressions
  16783. (while (setq e (pop re-list))
  16784. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16785. block (if block-type "\n\n" ""))
  16786. (when (member m matchers)
  16787. (goto-char (point-min))
  16788. (while (re-search-forward re nil t)
  16789. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16790. (or (not overlays)
  16791. (not (eq (get-char-property (match-beginning n)
  16792. 'org-overlay-type)
  16793. 'org-latex-overlay))))
  16794. (cond
  16795. ((eq processing-type 'verbatim))
  16796. ((eq processing-type 'mathjax)
  16797. ;; Prepare for MathJax processing.
  16798. (setq string (match-string n))
  16799. (when (member m '("$" "$1"))
  16800. (save-excursion
  16801. (delete-region (match-beginning n) (match-end n))
  16802. (goto-char (match-beginning n))
  16803. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16804. ((or (eq processing-type 'dvipng)
  16805. (eq processing-type 'imagemagick))
  16806. ;; Process to an image.
  16807. (setq txt (match-string n)
  16808. beg (match-beginning n) end (match-end n)
  16809. cnt (1+ cnt))
  16810. (let ((face (face-at-point))
  16811. (fg (plist-get opt :foreground))
  16812. (bg (plist-get opt :background))
  16813. ;; Ensure full list is printed.
  16814. print-length print-level)
  16815. (when forbuffer
  16816. ;; Get the colors from the face at point.
  16817. (goto-char beg)
  16818. (when (eq fg 'auto)
  16819. (setq fg (face-attribute face :foreground nil 'default)))
  16820. (when (eq bg 'auto)
  16821. (setq bg (face-attribute face :background nil 'default)))
  16822. (setq optnew (copy-sequence opt))
  16823. (plist-put optnew :foreground fg)
  16824. (plist-put optnew :background bg))
  16825. (setq hash (sha1 (prin1-to-string
  16826. (list org-format-latex-header
  16827. org-latex-default-packages-alist
  16828. org-latex-packages-alist
  16829. org-format-latex-options
  16830. forbuffer txt fg bg)))
  16831. linkfile (format "%s_%s.png" prefix hash)
  16832. movefile (format "%s_%s.png" absprefix hash)))
  16833. (setq link (concat block "[[file:" linkfile "]]" block))
  16834. (if msg (message msg cnt))
  16835. (goto-char beg)
  16836. (unless checkdir ; Ensure the directory exists.
  16837. (setq checkdir t)
  16838. (or (file-directory-p todir) (make-directory todir t)))
  16839. (unless (file-exists-p movefile)
  16840. (org-create-formula-image
  16841. txt movefile optnew forbuffer processing-type))
  16842. (if overlays
  16843. (progn
  16844. (mapc (lambda (o)
  16845. (if (eq (overlay-get o 'org-overlay-type)
  16846. 'org-latex-overlay)
  16847. (delete-overlay o)))
  16848. (overlays-in beg end))
  16849. (setq ov (make-overlay beg end))
  16850. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16851. (if (featurep 'xemacs)
  16852. (progn
  16853. (overlay-put ov 'invisible t)
  16854. (overlay-put
  16855. ov 'end-glyph
  16856. (make-glyph (vector 'png :file movefile))))
  16857. (overlay-put
  16858. ov 'display
  16859. (list 'image :type 'png :file movefile :ascent 'center)))
  16860. (push ov org-latex-fragment-image-overlays)
  16861. (goto-char end))
  16862. (delete-region beg end)
  16863. (insert (org-add-props link
  16864. (list 'org-latex-src
  16865. (replace-regexp-in-string
  16866. "\"" "" txt)
  16867. 'org-latex-src-embed-type
  16868. (if block-type 'paragraph 'character))))))
  16869. ((eq processing-type 'mathml)
  16870. ;; Process to MathML
  16871. (unless (save-match-data (org-format-latex-mathml-available-p))
  16872. (user-error "LaTeX to MathML converter not configured"))
  16873. (setq txt (match-string n)
  16874. beg (match-beginning n) end (match-end n)
  16875. cnt (1+ cnt))
  16876. (if msg (message msg cnt))
  16877. (goto-char beg)
  16878. (delete-region beg end)
  16879. (insert (org-format-latex-as-mathml
  16880. txt block-type prefix dir)))
  16881. (t
  16882. (error "Unknown conversion type %s for LaTeX fragments"
  16883. processing-type)))))))))
  16884. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16885. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16886. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16887. sucessful, return the portion between \"<math...> </math>\"
  16888. elements otherwise return nil. When MATHML-FILE is specified,
  16889. write the results in to that file. When invoked as an
  16890. interactive command, prompt for LATEX-FRAG, with initial value
  16891. set to the current active region and echo the results for user
  16892. inspection."
  16893. (interactive (list (let ((frag (when (org-region-active-p)
  16894. (buffer-substring-no-properties
  16895. (region-beginning) (region-end)))))
  16896. (read-string "LaTeX Fragment: " frag nil frag))))
  16897. (unless latex-frag (error "Invalid LaTeX fragment"))
  16898. (let* ((tmp-in-file (file-relative-name
  16899. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16900. (ignore (write-region latex-frag nil tmp-in-file))
  16901. (tmp-out-file (file-relative-name
  16902. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16903. (cmd (format-spec
  16904. org-latex-to-mathml-convert-command
  16905. `((?j . ,(shell-quote-argument
  16906. (expand-file-name org-latex-to-mathml-jar-file)))
  16907. (?I . ,(shell-quote-argument tmp-in-file))
  16908. (?o . ,(shell-quote-argument tmp-out-file)))))
  16909. mathml shell-command-output)
  16910. (when (org-called-interactively-p 'any)
  16911. (unless (org-format-latex-mathml-available-p)
  16912. (user-error "LaTeX to MathML converter not configured")))
  16913. (message "Running %s" cmd)
  16914. (setq shell-command-output (shell-command-to-string cmd))
  16915. (setq mathml
  16916. (when (file-readable-p tmp-out-file)
  16917. (with-current-buffer (find-file-noselect tmp-out-file t)
  16918. (goto-char (point-min))
  16919. (when (re-search-forward
  16920. (concat
  16921. (regexp-quote
  16922. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16923. "\\(.\\|\n\\)*"
  16924. (regexp-quote "</math>")) nil t)
  16925. (prog1 (match-string 0) (kill-buffer))))))
  16926. (cond
  16927. (mathml
  16928. (setq mathml
  16929. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16930. (when mathml-file
  16931. (write-region mathml nil mathml-file))
  16932. (when (org-called-interactively-p 'any)
  16933. (message mathml)))
  16934. ((message "LaTeX to MathML conversion failed")
  16935. (message shell-command-output)))
  16936. (delete-file tmp-in-file)
  16937. (when (file-exists-p tmp-out-file)
  16938. (delete-file tmp-out-file))
  16939. mathml))
  16940. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16941. prefix &optional dir)
  16942. "Use `org-create-math-formula' but check local cache first."
  16943. (let* ((absprefix (expand-file-name prefix dir))
  16944. (print-length nil) (print-level nil)
  16945. (formula-id (concat
  16946. "formula-"
  16947. (sha1
  16948. (prin1-to-string
  16949. (list latex-frag
  16950. org-latex-to-mathml-convert-command)))))
  16951. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16952. (formula-cache-dir (file-name-directory formula-cache)))
  16953. (unless (file-directory-p formula-cache-dir)
  16954. (make-directory formula-cache-dir t))
  16955. (unless (file-exists-p formula-cache)
  16956. (org-create-math-formula latex-frag formula-cache))
  16957. (if (file-exists-p formula-cache)
  16958. ;; Successful conversion. Return the link to MathML file.
  16959. (org-add-props
  16960. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16961. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16962. 'org-latex-src-embed-type (if latex-frag-type
  16963. 'paragraph 'character)))
  16964. ;; Failed conversion. Return the LaTeX fragment verbatim
  16965. latex-frag)))
  16966. (defun org-create-formula-image (string tofile options buffer &optional type)
  16967. "Create an image from LaTeX source using dvipng or convert.
  16968. This function calls either `org-create-formula-image-with-dvipng'
  16969. or `org-create-formula-image-with-imagemagick' depending on the
  16970. value of `org-latex-create-formula-image-program' or on the value
  16971. of the optional TYPE variable.
  16972. Note: ultimately these two function should be combined as they
  16973. share a good deal of logic."
  16974. (org-check-external-command
  16975. "latex" "needed to convert LaTeX fragments to images")
  16976. (funcall
  16977. (case (or type org-latex-create-formula-image-program)
  16978. ('dvipng
  16979. (org-check-external-command
  16980. "dvipng" "needed to convert LaTeX fragments to images")
  16981. #'org-create-formula-image-with-dvipng)
  16982. ('imagemagick
  16983. (org-check-external-command
  16984. "convert" "you need to install imagemagick")
  16985. #'org-create-formula-image-with-imagemagick)
  16986. (t (error
  16987. "Invalid value of `org-latex-create-formula-image-program'")))
  16988. string tofile options buffer))
  16989. (declare-function org-export-get-backend "ox" (name))
  16990. (declare-function org-export--get-global-options "ox" (&optional backend))
  16991. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16992. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  16993. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  16994. (defun org-create-formula--latex-header ()
  16995. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16996. (let ((info (org-combine-plists (org-export--get-global-options
  16997. (org-export-get-backend 'latex))
  16998. (org-export--get-inbuffer-options
  16999. (org-export-get-backend 'latex)))))
  17000. (org-latex-guess-babel-language
  17001. (org-latex-guess-inputenc
  17002. (org-splice-latex-header
  17003. org-format-latex-header
  17004. org-latex-default-packages-alist
  17005. org-latex-packages-alist t
  17006. (plist-get info :latex-header)))
  17007. info)))
  17008. ;; This function borrows from Ganesh Swami's latex2png.el
  17009. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17010. "This calls dvipng."
  17011. (require 'ox-latex)
  17012. (let* ((tmpdir (if (featurep 'xemacs)
  17013. (temp-directory)
  17014. temporary-file-directory))
  17015. (texfilebase (make-temp-name
  17016. (expand-file-name "orgtex" tmpdir)))
  17017. (texfile (concat texfilebase ".tex"))
  17018. (dvifile (concat texfilebase ".dvi"))
  17019. (pngfile (concat texfilebase ".png"))
  17020. (fnh (if (featurep 'xemacs)
  17021. (font-height (face-font 'default))
  17022. (face-attribute 'default :height nil)))
  17023. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17024. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17025. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17026. "Black"))
  17027. (bg (or (plist-get options (if buffer :background :html-background))
  17028. "Transparent")))
  17029. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17030. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17031. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17032. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17033. (let ((latex-header (org-create-formula--latex-header)))
  17034. (with-temp-file texfile
  17035. (insert latex-header)
  17036. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17037. (let ((dir default-directory))
  17038. (condition-case nil
  17039. (progn
  17040. (cd tmpdir)
  17041. (call-process "latex" nil nil nil texfile))
  17042. (error nil))
  17043. (cd dir))
  17044. (if (not (file-exists-p dvifile))
  17045. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17046. (condition-case nil
  17047. (if (featurep 'xemacs)
  17048. (call-process "dvipng" nil nil nil
  17049. "-fg" fg "-bg" bg
  17050. "-T" "tight"
  17051. "-o" pngfile
  17052. dvifile)
  17053. (call-process "dvipng" nil nil nil
  17054. "-fg" fg "-bg" bg
  17055. "-D" dpi
  17056. ;;"-x" scale "-y" scale
  17057. "-T" "tight"
  17058. "-o" pngfile
  17059. dvifile))
  17060. (error nil))
  17061. (if (not (file-exists-p pngfile))
  17062. (if org-format-latex-signal-error
  17063. (error "Failed to create png file from %s" texfile)
  17064. (message "Failed to create png file from %s" texfile)
  17065. nil)
  17066. ;; Use the requested file name and clean up
  17067. (copy-file pngfile tofile 'replace)
  17068. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17069. (if (file-exists-p (concat texfilebase e))
  17070. (delete-file (concat texfilebase e))))
  17071. pngfile))))
  17072. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17073. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17074. "This calls convert, which is included into imagemagick."
  17075. (require 'ox-latex)
  17076. (let* ((tmpdir (if (featurep 'xemacs)
  17077. (temp-directory)
  17078. temporary-file-directory))
  17079. (texfilebase (make-temp-name
  17080. (expand-file-name "orgtex" tmpdir)))
  17081. (texfile (concat texfilebase ".tex"))
  17082. (pdffile (concat texfilebase ".pdf"))
  17083. (pngfile (concat texfilebase ".png"))
  17084. (fnh (if (featurep 'xemacs)
  17085. (font-height (face-font 'default))
  17086. (face-attribute 'default :height nil)))
  17087. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17088. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17089. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17090. "black"))
  17091. (bg (or (plist-get options (if buffer :background :html-background))
  17092. "white")))
  17093. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17094. (setq fg (org-latex-color-format fg)))
  17095. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17096. (setq bg (org-latex-color-format
  17097. (if (string= bg "Transparent") "white" bg))))
  17098. (let ((latex-header (org-create-formula--latex-header)))
  17099. (with-temp-file texfile
  17100. (insert latex-header)
  17101. (insert "\n\\begin{document}\n"
  17102. "\\definecolor{fg}{rgb}{" fg "}\n"
  17103. "\\definecolor{bg}{rgb}{" bg "}\n"
  17104. "\n\\pagecolor{bg}\n"
  17105. "\n{\\color{fg}\n"
  17106. string
  17107. "\n}\n"
  17108. "\n\\end{document}\n")))
  17109. (org-latex-compile texfile t)
  17110. (if (not (file-exists-p pdffile))
  17111. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17112. (condition-case nil
  17113. (if (featurep 'xemacs)
  17114. (call-process "convert" nil nil nil
  17115. "-density" "96"
  17116. "-trim"
  17117. "-antialias"
  17118. pdffile
  17119. "-quality" "100"
  17120. ;; "-sharpen" "0x1.0"
  17121. pngfile)
  17122. (call-process "convert" nil nil nil
  17123. "-density" dpi
  17124. "-trim"
  17125. "-antialias"
  17126. pdffile
  17127. "-quality" "100"
  17128. ;; "-sharpen" "0x1.0"
  17129. pngfile))
  17130. (error nil))
  17131. (if (not (file-exists-p pngfile))
  17132. (if org-format-latex-signal-error
  17133. (error "Failed to create png file from %s" texfile)
  17134. (message "Failed to create png file from %s" texfile)
  17135. nil)
  17136. ;; Use the requested file name and clean up
  17137. (copy-file pngfile tofile 'replace)
  17138. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17139. (if (file-exists-p (concat texfilebase e))
  17140. (delete-file (concat texfilebase e))))
  17141. pngfile))))
  17142. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17143. "Fill a LaTeX header template TPL.
  17144. In the template, the following place holders will be recognized:
  17145. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17146. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17147. [PACKAGES] \\usepackage statements for PKG
  17148. [NO-PACKAGES] do not include PKG
  17149. [EXTRA] the string EXTRA
  17150. [NO-EXTRA] do not include EXTRA
  17151. For backward compatibility, if both the positive and the negative place
  17152. holder is missing, the positive one (without the \"NO-\") will be
  17153. assumed to be present at the end of the template.
  17154. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17155. EXTRA is a string.
  17156. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17157. (let (rpl (end ""))
  17158. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17159. (setq rpl (if (or (match-end 1) (not def-pkg))
  17160. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17161. tpl (replace-match rpl t t tpl))
  17162. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17163. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17164. (setq rpl (if (or (match-end 1) (not pkg))
  17165. "" (org-latex-packages-to-string pkg snippets-p t))
  17166. tpl (replace-match rpl t t tpl))
  17167. (if pkg (setq end
  17168. (concat end "\n"
  17169. (org-latex-packages-to-string pkg snippets-p)))))
  17170. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17171. (setq rpl (if (or (match-end 1) (not extra))
  17172. "" (concat extra "\n"))
  17173. tpl (replace-match rpl t t tpl))
  17174. (if (and extra (string-match "\\S-" extra))
  17175. (setq end (concat end "\n" extra))))
  17176. (if (string-match "\\S-" end)
  17177. (concat tpl "\n" end)
  17178. tpl)))
  17179. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17180. "Turn an alist of packages into a string with the \\usepackage macros."
  17181. (setq pkg (mapconcat (lambda(p)
  17182. (cond
  17183. ((stringp p) p)
  17184. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17185. (format "%% Package %s omitted" (cadr p)))
  17186. ((equal "" (car p))
  17187. (format "\\usepackage{%s}" (cadr p)))
  17188. (t
  17189. (format "\\usepackage[%s]{%s}"
  17190. (car p) (cadr p)))))
  17191. pkg
  17192. "\n"))
  17193. (if newline (concat pkg "\n") pkg))
  17194. (defun org-dvipng-color (attr)
  17195. "Return a RGB color specification for dvipng."
  17196. (apply 'format "rgb %s %s %s"
  17197. (mapcar 'org-normalize-color
  17198. (if (featurep 'xemacs)
  17199. (color-rgb-components
  17200. (face-property 'default
  17201. (cond ((eq attr :foreground) 'foreground)
  17202. ((eq attr :background) 'background))))
  17203. (color-values (face-attribute 'default attr nil))))))
  17204. (defun org-dvipng-color-format (color-name)
  17205. "Convert COLOR-NAME to a RGB color value for dvipng."
  17206. (apply 'format "rgb %s %s %s"
  17207. (mapcar 'org-normalize-color
  17208. (color-values color-name))))
  17209. (defun org-latex-color (attr)
  17210. "Return a RGB color for the LaTeX color package."
  17211. (apply 'format "%s,%s,%s"
  17212. (mapcar 'org-normalize-color
  17213. (if (featurep 'xemacs)
  17214. (color-rgb-components
  17215. (face-property 'default
  17216. (cond ((eq attr :foreground) 'foreground)
  17217. ((eq attr :background) 'background))))
  17218. (color-values (face-attribute 'default attr nil))))))
  17219. (defun org-latex-color-format (color-name)
  17220. "Convert COLOR-NAME to a RGB color value."
  17221. (apply 'format "%s,%s,%s"
  17222. (mapcar 'org-normalize-color
  17223. (color-values color-name))))
  17224. (defun org-normalize-color (value)
  17225. "Return string to be used as color value for an RGB component."
  17226. (format "%g" (/ value 65535.0)))
  17227. ;; Image display
  17228. (defvar org-inline-image-overlays nil)
  17229. (make-variable-buffer-local 'org-inline-image-overlays)
  17230. (defun org-toggle-inline-images (&optional include-linked)
  17231. "Toggle the display of inline images.
  17232. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17233. (interactive "P")
  17234. (if org-inline-image-overlays
  17235. (progn
  17236. (org-remove-inline-images)
  17237. (message "Inline image display turned off"))
  17238. (org-display-inline-images include-linked)
  17239. (if (and (org-called-interactively-p)
  17240. org-inline-image-overlays)
  17241. (message "%d images displayed inline"
  17242. (length org-inline-image-overlays))
  17243. (message "No images to display inline"))))
  17244. (defun org-redisplay-inline-images ()
  17245. "Refresh the display of inline images."
  17246. (interactive)
  17247. (if (not org-inline-image-overlays)
  17248. (org-toggle-inline-images)
  17249. (org-toggle-inline-images)
  17250. (org-toggle-inline-images)))
  17251. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17252. "Display inline images.
  17253. Normally only links without a description part are inlined, because this
  17254. is how it will work for export. When INCLUDE-LINKED is set, also links
  17255. with a description part will be inlined. This can be nice for a quick
  17256. look at those images, but it does not reflect what exported files will look
  17257. like.
  17258. When REFRESH is set, refresh existing images between BEG and END.
  17259. This will create new image displays only if necessary.
  17260. BEG and END default to the buffer boundaries."
  17261. (interactive "P")
  17262. (when (display-graphic-p)
  17263. (unless refresh
  17264. (org-remove-inline-images)
  17265. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17266. (save-excursion
  17267. (save-restriction
  17268. (widen)
  17269. (setq beg (or beg (point-min)) end (or end (point-max)))
  17270. (goto-char beg)
  17271. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17272. (substring (org-image-file-name-regexp) 0 -2)
  17273. "\\)\\]" (if include-linked "" "\\]")))
  17274. (case-fold-search t)
  17275. old file ov img type attrwidth width)
  17276. (while (re-search-forward re end t)
  17277. (setq old (get-char-property-and-overlay (match-beginning 1)
  17278. 'org-image-overlay)
  17279. file (expand-file-name
  17280. (concat (or (match-string 3) "") (match-string 4))))
  17281. (when (image-type-available-p 'imagemagick)
  17282. (setq attrwidth (if (or (listp org-image-actual-width)
  17283. (null org-image-actual-width))
  17284. (save-excursion
  17285. (save-match-data
  17286. (when (re-search-backward
  17287. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17288. (save-excursion
  17289. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17290. (string-to-number (match-string 1))))))
  17291. width (cond ((eq org-image-actual-width t) nil)
  17292. ((null org-image-actual-width) attrwidth)
  17293. ((numberp org-image-actual-width)
  17294. org-image-actual-width)
  17295. ((listp org-image-actual-width)
  17296. (or attrwidth (car org-image-actual-width))))
  17297. type (if width 'imagemagick)))
  17298. (when (file-exists-p file)
  17299. (if (and (car-safe old) refresh)
  17300. (image-refresh (overlay-get (cdr old) 'display))
  17301. (setq img (save-match-data (create-image file type nil :width width)))
  17302. (when img
  17303. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17304. (overlay-put ov 'display img)
  17305. (overlay-put ov 'face 'default)
  17306. (overlay-put ov 'org-image-overlay t)
  17307. (overlay-put ov 'modification-hooks
  17308. (list 'org-display-inline-remove-overlay))
  17309. (push ov org-inline-image-overlays))))))))))
  17310. (define-obsolete-function-alias
  17311. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17312. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17313. "Remove inline-display overlay if a corresponding region is modified."
  17314. (let ((inhibit-modification-hooks t))
  17315. (when (and ov after)
  17316. (delete ov org-inline-image-overlays)
  17317. (delete-overlay ov))))
  17318. (defun org-remove-inline-images ()
  17319. "Remove inline display of images."
  17320. (interactive)
  17321. (mapc 'delete-overlay org-inline-image-overlays)
  17322. (setq org-inline-image-overlays nil))
  17323. ;;;; Key bindings
  17324. ;; Outline functions from `outline-mode-prefix-map'
  17325. ;; that can be remapped in Org:
  17326. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17327. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17328. (define-key org-mode-map [remap outline-forward-same-level]
  17329. 'org-forward-heading-same-level)
  17330. (define-key org-mode-map [remap outline-backward-same-level]
  17331. 'org-backward-heading-same-level)
  17332. (define-key org-mode-map [remap show-branches]
  17333. 'org-kill-note-or-show-branches)
  17334. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17335. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17336. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17337. ;; Outline functions from `outline-mode-prefix-map' that can not
  17338. ;; be remapped in Org:
  17339. ;;
  17340. ;; - the column "key binding" shows whether the Outline function is still
  17341. ;; available in Org mode on the same key that it has been bound to in
  17342. ;; Outline mode:
  17343. ;; - "overridden": key used for a different functionality in Org mode
  17344. ;; - else: key still bound to the same Outline function in Org mode
  17345. ;;
  17346. ;; | Outline function | key binding | Org replacement |
  17347. ;; |------------------------------------+-------------+-----------------------|
  17348. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17349. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17350. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17351. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17352. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17353. ;; | `show-entry' | overridden | no replacement |
  17354. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17355. ;; | `show-branches' | `C-c C-k' | still same function |
  17356. ;; | `show-subtree' | overridden | visibility cycling |
  17357. ;; | `show-all' | overridden | no replacement |
  17358. ;; | `hide-subtree' | overridden | visibility cycling |
  17359. ;; | `hide-body' | overridden | no replacement |
  17360. ;; | `hide-entry' | overridden | visibility cycling |
  17361. ;; | `hide-leaves' | overridden | no replacement |
  17362. ;; | `hide-sublevels' | overridden | no replacement |
  17363. ;; | `hide-other' | overridden | no replacement |
  17364. ;; Make `C-c C-x' a prefix key
  17365. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17366. ;; TAB key with modifiers
  17367. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17368. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17369. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17370. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17371. ;; The following line is necessary under Suse GNU/Linux
  17372. (unless (featurep 'xemacs)
  17373. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17374. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17375. (define-key org-mode-map [backtab] 'org-shifttab)
  17376. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17377. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17378. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17379. ;; Cursor keys with modifiers
  17380. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17381. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17382. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17383. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17384. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17385. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17386. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17387. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17388. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17389. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17390. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17391. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17392. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17393. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17394. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17395. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17396. ;; Babel keys
  17397. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17398. (mapc (lambda (pair)
  17399. (define-key org-babel-map (car pair) (cdr pair)))
  17400. org-babel-key-bindings)
  17401. ;;; Extra keys for tty access.
  17402. ;; We only set them when really needed because otherwise the
  17403. ;; menus don't show the simple keys
  17404. (when (or org-use-extra-keys
  17405. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17406. (not window-system))
  17407. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17408. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17409. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17410. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17411. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17412. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17413. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17414. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17415. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17416. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17417. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17418. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17419. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17420. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17421. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17422. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17423. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17424. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17425. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17426. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17427. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17428. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17429. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17430. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17431. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17432. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17433. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17434. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17435. ;; All the other keys
  17436. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17437. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17438. (if (boundp 'narrow-map)
  17439. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17440. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17441. (if (boundp 'narrow-map)
  17442. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17443. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17444. (if (boundp 'narrow-map)
  17445. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17446. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17447. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17448. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17449. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17450. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17451. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17452. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17453. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17454. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17455. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17456. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17457. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17458. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17459. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17460. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17461. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17462. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17463. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17464. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17465. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17466. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17467. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17468. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17469. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17470. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17471. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17472. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17473. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17474. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17475. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17476. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17477. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17478. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17479. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17480. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17481. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17482. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17483. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17484. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17485. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17486. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17487. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17488. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17489. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17490. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17491. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17492. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17493. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17494. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17495. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17496. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17497. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17498. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17499. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17500. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17501. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17502. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17503. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17504. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17505. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17506. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17507. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-element)
  17508. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-element)
  17509. (org-defkey org-mode-map "\C-m" 'org-return)
  17510. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17511. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17512. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17513. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17514. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17515. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17516. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17517. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17518. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17519. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17520. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17521. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17522. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17523. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17524. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17525. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17526. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17527. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17528. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17529. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17530. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17531. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17532. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17533. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17534. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17535. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17536. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17537. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17538. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17539. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17540. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17541. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17542. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17543. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17544. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17545. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17546. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17547. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17548. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17549. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17550. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17551. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17552. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17553. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17554. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17555. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17556. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17557. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17558. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17559. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17560. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17561. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17562. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17563. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17564. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17565. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17566. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17567. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17568. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17569. (when (featurep 'xemacs)
  17570. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17571. (defconst org-speed-commands-default
  17572. '(
  17573. ("Outline Navigation")
  17574. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17575. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17576. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17577. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17578. ("F" . org-next-block)
  17579. ("B" . org-previous-block)
  17580. ("u" . (org-speed-move-safe 'outline-up-heading))
  17581. ("j" . org-goto)
  17582. ("g" . (org-refile t))
  17583. ("Outline Visibility")
  17584. ("c" . org-cycle)
  17585. ("C" . org-shifttab)
  17586. (" " . org-display-outline-path)
  17587. ("s" . org-narrow-to-subtree)
  17588. ("=" . org-columns)
  17589. ("Outline Structure Editing")
  17590. ("U" . org-shiftmetaup)
  17591. ("D" . org-shiftmetadown)
  17592. ("r" . org-metaright)
  17593. ("l" . org-metaleft)
  17594. ("R" . org-shiftmetaright)
  17595. ("L" . org-shiftmetaleft)
  17596. ("i" . (progn (forward-char 1) (call-interactively
  17597. 'org-insert-heading-respect-content)))
  17598. ("^" . org-sort)
  17599. ("w" . org-refile)
  17600. ("a" . org-archive-subtree-default-with-confirmation)
  17601. ("@" . org-mark-subtree)
  17602. ("#" . org-toggle-comment)
  17603. ("Clock Commands")
  17604. ("I" . org-clock-in)
  17605. ("O" . org-clock-out)
  17606. ("Meta Data Editing")
  17607. ("t" . org-todo)
  17608. ("," . (org-priority))
  17609. ("0" . (org-priority ?\ ))
  17610. ("1" . (org-priority ?A))
  17611. ("2" . (org-priority ?B))
  17612. ("3" . (org-priority ?C))
  17613. (":" . org-set-tags-command)
  17614. ("e" . org-set-effort)
  17615. ("E" . org-inc-effort)
  17616. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17617. (org-entry-put (point) "APPT_WARNTIME" m)))
  17618. ("Agenda Views etc")
  17619. ("v" . org-agenda)
  17620. ("/" . org-sparse-tree)
  17621. ("Misc")
  17622. ("o" . org-open-at-point)
  17623. ("?" . org-speed-command-help)
  17624. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17625. (">" . (org-agenda-remove-restriction-lock))
  17626. )
  17627. "The default speed commands.")
  17628. (defun org-print-speed-command (e)
  17629. (if (> (length (car e)) 1)
  17630. (progn
  17631. (princ "\n")
  17632. (princ (car e))
  17633. (princ "\n")
  17634. (princ (make-string (length (car e)) ?-))
  17635. (princ "\n"))
  17636. (princ (car e))
  17637. (princ " ")
  17638. (if (symbolp (cdr e))
  17639. (princ (symbol-name (cdr e)))
  17640. (prin1 (cdr e)))
  17641. (princ "\n")))
  17642. (defun org-speed-command-help ()
  17643. "Show the available speed commands."
  17644. (interactive)
  17645. (if (not org-use-speed-commands)
  17646. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17647. (with-output-to-temp-buffer "*Help*"
  17648. (princ "User-defined Speed commands\n===========================\n")
  17649. (mapc 'org-print-speed-command org-speed-commands-user)
  17650. (princ "\n")
  17651. (princ "Built-in Speed commands\n=======================\n")
  17652. (mapc 'org-print-speed-command org-speed-commands-default))
  17653. (with-current-buffer "*Help*"
  17654. (setq truncate-lines t))))
  17655. (defun org-speed-move-safe (cmd)
  17656. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17657. If not, return to the original position and throw an error."
  17658. (interactive)
  17659. (let ((pos (point)))
  17660. (call-interactively cmd)
  17661. (unless (and (bolp) (org-at-heading-p))
  17662. (goto-char pos)
  17663. (error "Boundary reached while executing %s" cmd))))
  17664. (defvar org-self-insert-command-undo-counter 0)
  17665. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17666. (defvar org-speed-command nil)
  17667. (define-obsolete-function-alias
  17668. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17669. (defun org-speed-command-activate (keys)
  17670. "Hook for activating single-letter speed commands.
  17671. `org-speed-commands-default' specifies a minimal command set.
  17672. Use `org-speed-commands-user' for further customization."
  17673. (when (or (and (bolp) (looking-at org-outline-regexp))
  17674. (and (functionp org-use-speed-commands)
  17675. (funcall org-use-speed-commands)))
  17676. (cdr (assoc keys (append org-speed-commands-user
  17677. org-speed-commands-default)))))
  17678. (define-obsolete-function-alias
  17679. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17680. (defun org-babel-speed-command-activate (keys)
  17681. "Hook for activating single-letter code block commands."
  17682. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17683. (cdr (assoc keys org-babel-key-bindings))))
  17684. (defcustom org-speed-command-hook
  17685. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17686. "Hook for activating speed commands at strategic locations.
  17687. Hook functions are called in sequence until a valid handler is
  17688. found.
  17689. Each hook takes a single argument, a user-pressed command key
  17690. which is also a `self-insert-command' from the global map.
  17691. Within the hook, examine the cursor position and the command key
  17692. and return nil or a valid handler as appropriate. Handler could
  17693. be one of an interactive command, a function, or a form.
  17694. Set `org-use-speed-commands' to non-nil value to enable this
  17695. hook. The default setting is `org-speed-command-activate'."
  17696. :group 'org-structure
  17697. :version "24.1"
  17698. :type 'hook)
  17699. (defun org-self-insert-command (N)
  17700. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17701. If the cursor is in a table looking at whitespace, the whitespace is
  17702. overwritten, and the table is not marked as requiring realignment."
  17703. (interactive "p")
  17704. (org-check-before-invisible-edit 'insert)
  17705. (cond
  17706. ((and org-use-speed-commands
  17707. (setq org-speed-command
  17708. (run-hook-with-args-until-success
  17709. 'org-speed-command-hook (this-command-keys))))
  17710. (cond
  17711. ((commandp org-speed-command)
  17712. (setq this-command org-speed-command)
  17713. (call-interactively org-speed-command))
  17714. ((functionp org-speed-command)
  17715. (funcall org-speed-command))
  17716. ((and org-speed-command (listp org-speed-command))
  17717. (eval org-speed-command))
  17718. (t (let (org-use-speed-commands)
  17719. (call-interactively 'org-self-insert-command)))))
  17720. ((and
  17721. (org-table-p)
  17722. (progn
  17723. ;; check if we blank the field, and if that triggers align
  17724. (and (featurep 'org-table) org-table-auto-blank-field
  17725. (member last-command
  17726. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17727. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17728. ;; got extra space, this field does not determine column width
  17729. (let (org-table-may-need-update) (org-table-blank-field))
  17730. ;; no extra space, this field may determine column width
  17731. (org-table-blank-field)))
  17732. t)
  17733. (eq N 1)
  17734. (looking-at "[^|\n]* |"))
  17735. (let (org-table-may-need-update)
  17736. (goto-char (1- (match-end 0)))
  17737. (backward-delete-char 1)
  17738. (goto-char (match-beginning 0))
  17739. (self-insert-command N)))
  17740. (t
  17741. (setq org-table-may-need-update t)
  17742. (self-insert-command N)
  17743. (org-fix-tags-on-the-fly)
  17744. (if org-self-insert-cluster-for-undo
  17745. (if (not (eq last-command 'org-self-insert-command))
  17746. (setq org-self-insert-command-undo-counter 1)
  17747. (if (>= org-self-insert-command-undo-counter 20)
  17748. (setq org-self-insert-command-undo-counter 1)
  17749. (and (> org-self-insert-command-undo-counter 0)
  17750. buffer-undo-list (listp buffer-undo-list)
  17751. (not (cadr buffer-undo-list)) ; remove nil entry
  17752. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17753. (setq org-self-insert-command-undo-counter
  17754. (1+ org-self-insert-command-undo-counter))))))))
  17755. (defun org-check-before-invisible-edit (kind)
  17756. "Check is editing if kind KIND would be dangerous with invisible text around.
  17757. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17758. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17759. (if (and org-catch-invisible-edits
  17760. (or (not (boundp 'visible-mode)) (not visible-mode))
  17761. (or (get-char-property (point) 'invisible)
  17762. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17763. ;; OK, we need to take a closer look
  17764. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17765. (invisible-before-point (if (bobp) nil (get-char-property
  17766. (1- (point)) 'invisible)))
  17767. (border-and-ok-direction
  17768. (or
  17769. ;; Check if we are acting predictably before invisible text
  17770. (and invisible-at-point (not invisible-before-point)
  17771. (memq kind '(insert delete-backward)))
  17772. ;; Check if we are acting predictably after invisible text
  17773. ;; This works not well, and I have turned it off. It seems
  17774. ;; better to always show and stop after invisible text.
  17775. ;; (and (not invisible-at-point) invisible-before-point
  17776. ;; (memq kind '(insert delete)))
  17777. )))
  17778. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17779. (memq invisible-before-point '(outline org-hide-block t)))
  17780. (if (eq org-catch-invisible-edits 'error)
  17781. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  17782. (if (and org-custom-properties-overlays
  17783. (y-or-n-p "Display invisible properties in this buffer? "))
  17784. (org-toggle-custom-properties-visibility)
  17785. ;; Make the area visible
  17786. (save-excursion
  17787. (if invisible-before-point
  17788. (goto-char (previous-single-char-property-change
  17789. (point) 'invisible)))
  17790. (org-cycle))
  17791. (cond
  17792. ((eq org-catch-invisible-edits 'show)
  17793. ;; That's it, we do the edit after showing
  17794. (message
  17795. "Unfolding invisible region around point before editing")
  17796. (sit-for 1))
  17797. ((and (eq org-catch-invisible-edits 'smart)
  17798. border-and-ok-direction)
  17799. (message "Unfolding invisible region around point before editing"))
  17800. (t
  17801. ;; Don't do the edit, make the user repeat it in full visibility
  17802. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17803. (defun org-fix-tags-on-the-fly ()
  17804. (when (and (equal (char-after (point-at-bol)) ?*)
  17805. (org-at-heading-p))
  17806. (org-align-tags-here org-tags-column)))
  17807. (defun org-delete-backward-char (N)
  17808. "Like `delete-backward-char', insert whitespace at field end in tables.
  17809. When deleting backwards, in tables this function will insert whitespace in
  17810. front of the next \"|\" separator, to keep the table aligned. The table will
  17811. still be marked for re-alignment if the field did fill the entire column,
  17812. because, in this case the deletion might narrow the column."
  17813. (interactive "p")
  17814. (save-match-data
  17815. (org-check-before-invisible-edit 'delete-backward)
  17816. (if (and (org-table-p)
  17817. (eq N 1)
  17818. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17819. (looking-at ".*?|"))
  17820. (let ((pos (point))
  17821. (noalign (looking-at "[^|\n\r]* |"))
  17822. (c org-table-may-need-update))
  17823. (backward-delete-char N)
  17824. (if (not overwrite-mode)
  17825. (progn
  17826. (skip-chars-forward "^|")
  17827. (insert " ")
  17828. (goto-char (1- pos))))
  17829. ;; noalign: if there were two spaces at the end, this field
  17830. ;; does not determine the width of the column.
  17831. (if noalign (setq org-table-may-need-update c)))
  17832. (backward-delete-char N)
  17833. (org-fix-tags-on-the-fly))))
  17834. (defun org-delete-char (N)
  17835. "Like `delete-char', but insert whitespace at field end in tables.
  17836. When deleting characters, in tables this function will insert whitespace in
  17837. front of the next \"|\" separator, to keep the table aligned. The table will
  17838. still be marked for re-alignment if the field did fill the entire column,
  17839. because, in this case the deletion might narrow the column."
  17840. (interactive "p")
  17841. (save-match-data
  17842. (org-check-before-invisible-edit 'delete)
  17843. (if (and (org-table-p)
  17844. (not (bolp))
  17845. (not (= (char-after) ?|))
  17846. (eq N 1))
  17847. (if (looking-at ".*?|")
  17848. (let ((pos (point))
  17849. (noalign (looking-at "[^|\n\r]* |"))
  17850. (c org-table-may-need-update))
  17851. (replace-match
  17852. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17853. (goto-char pos)
  17854. ;; noalign: if there were two spaces at the end, this field
  17855. ;; does not determine the width of the column.
  17856. (if noalign (setq org-table-may-need-update c)))
  17857. (delete-char N))
  17858. (delete-char N)
  17859. (org-fix-tags-on-the-fly))))
  17860. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17861. (put 'org-self-insert-command 'delete-selection t)
  17862. (put 'orgtbl-self-insert-command 'delete-selection t)
  17863. (put 'org-delete-char 'delete-selection 'supersede)
  17864. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17865. (put 'org-yank 'delete-selection 'yank)
  17866. ;; Make `flyspell-mode' delay after some commands
  17867. (put 'org-self-insert-command 'flyspell-delayed t)
  17868. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17869. (put 'org-delete-char 'flyspell-delayed t)
  17870. (put 'org-delete-backward-char 'flyspell-delayed t)
  17871. ;; Make pabbrev-mode expand after org-mode commands
  17872. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17873. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17874. ;; How to do this: Measure non-white length of current string
  17875. ;; If equal to column width, we should realign.
  17876. (defun org-remap (map &rest commands)
  17877. "In MAP, remap the functions given in COMMANDS.
  17878. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17879. (let (new old)
  17880. (while commands
  17881. (setq old (pop commands) new (pop commands))
  17882. (if (fboundp 'command-remapping)
  17883. (org-defkey map (vector 'remap old) new)
  17884. (substitute-key-definition old new map global-map)))))
  17885. (defun org-transpose-words ()
  17886. "Transpose words for Org.
  17887. This uses the `org-mode-transpose-word-syntax-table' syntax
  17888. table, which interprets characters in `org-emphasis-alist' as
  17889. word constituants."
  17890. (interactive)
  17891. (with-syntax-table org-mode-transpose-word-syntax-table
  17892. (call-interactively 'transpose-words)))
  17893. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17894. (when (eq org-enable-table-editor 'optimized)
  17895. ;; If the user wants maximum table support, we need to hijack
  17896. ;; some standard editing functions
  17897. (org-remap org-mode-map
  17898. 'self-insert-command 'org-self-insert-command
  17899. 'delete-char 'org-delete-char
  17900. 'delete-backward-char 'org-delete-backward-char)
  17901. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17902. (defvar org-ctrl-c-ctrl-c-hook nil
  17903. "Hook for functions attaching themselves to `C-c C-c'.
  17904. This can be used to add additional functionality to the C-c C-c
  17905. key which executes context-dependent commands. This hook is run
  17906. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17907. run after the last test.
  17908. Each function will be called with no arguments. The function
  17909. must check if the context is appropriate for it to act. If yes,
  17910. it should do its thing and then return a non-nil value. If the
  17911. context is wrong, just do nothing and return nil.")
  17912. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17913. "Hook for functions attaching themselves to `C-c C-c'.
  17914. This can be used to add additional functionality to the C-c C-c
  17915. key which executes context-dependent commands. This hook is run
  17916. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17917. before the first test.
  17918. Each function will be called with no arguments. The function
  17919. must check if the context is appropriate for it to act. If yes,
  17920. it should do its thing and then return a non-nil value. If the
  17921. context is wrong, just do nothing and return nil.")
  17922. (defvar org-tab-first-hook nil
  17923. "Hook for functions to attach themselves to TAB.
  17924. See `org-ctrl-c-ctrl-c-hook' for more information.
  17925. This hook runs as the first action when TAB is pressed, even before
  17926. `org-cycle' messes around with the `outline-regexp' to cater for
  17927. inline tasks and plain list item folding.
  17928. If any function in this hook returns t, any other actions that
  17929. would have been caused by TAB (such as table field motion or visibility
  17930. cycling) will not occur.")
  17931. (defvar org-tab-after-check-for-table-hook nil
  17932. "Hook for functions to attach themselves to TAB.
  17933. See `org-ctrl-c-ctrl-c-hook' for more information.
  17934. This hook runs after it has been established that the cursor is not in a
  17935. table, but before checking if the cursor is in a headline or if global cycling
  17936. should be done.
  17937. If any function in this hook returns t, not other actions like visibility
  17938. cycling will be done.")
  17939. (defvar org-tab-after-check-for-cycling-hook nil
  17940. "Hook for functions to attach themselves to TAB.
  17941. See `org-ctrl-c-ctrl-c-hook' for more information.
  17942. This hook runs after it has been established that not table field motion and
  17943. not visibility should be done because of current context. This is probably
  17944. the place where a package like yasnippets can hook in.")
  17945. (defvar org-tab-before-tab-emulation-hook nil
  17946. "Hook for functions to attach themselves to TAB.
  17947. See `org-ctrl-c-ctrl-c-hook' for more information.
  17948. This hook runs after every other options for TAB have been exhausted, but
  17949. before indentation and \t insertion takes place.")
  17950. (defvar org-metaleft-hook nil
  17951. "Hook for functions attaching themselves to `M-left'.
  17952. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17953. (defvar org-metaright-hook nil
  17954. "Hook for functions attaching themselves to `M-right'.
  17955. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17956. (defvar org-metaup-hook nil
  17957. "Hook for functions attaching themselves to `M-up'.
  17958. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17959. (defvar org-metadown-hook nil
  17960. "Hook for functions attaching themselves to `M-down'.
  17961. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17962. (defvar org-shiftmetaleft-hook nil
  17963. "Hook for functions attaching themselves to `M-S-left'.
  17964. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17965. (defvar org-shiftmetaright-hook nil
  17966. "Hook for functions attaching themselves to `M-S-right'.
  17967. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17968. (defvar org-shiftmetaup-hook nil
  17969. "Hook for functions attaching themselves to `M-S-up'.
  17970. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17971. (defvar org-shiftmetadown-hook nil
  17972. "Hook for functions attaching themselves to `M-S-down'.
  17973. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17974. (defvar org-metareturn-hook nil
  17975. "Hook for functions attaching themselves to `M-RET'.
  17976. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17977. (defvar org-shiftup-hook nil
  17978. "Hook for functions attaching themselves to `S-up'.
  17979. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17980. (defvar org-shiftup-final-hook nil
  17981. "Hook for functions attaching themselves to `S-up'.
  17982. This one runs after all other options except shift-select have been excluded.
  17983. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17984. (defvar org-shiftdown-hook nil
  17985. "Hook for functions attaching themselves to `S-down'.
  17986. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17987. (defvar org-shiftdown-final-hook nil
  17988. "Hook for functions attaching themselves to `S-down'.
  17989. This one runs after all other options except shift-select have been excluded.
  17990. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17991. (defvar org-shiftleft-hook nil
  17992. "Hook for functions attaching themselves to `S-left'.
  17993. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17994. (defvar org-shiftleft-final-hook nil
  17995. "Hook for functions attaching themselves to `S-left'.
  17996. This one runs after all other options except shift-select have been excluded.
  17997. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17998. (defvar org-shiftright-hook nil
  17999. "Hook for functions attaching themselves to `S-right'.
  18000. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18001. (defvar org-shiftright-final-hook nil
  18002. "Hook for functions attaching themselves to `S-right'.
  18003. This one runs after all other options except shift-select have been excluded.
  18004. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18005. (defun org-modifier-cursor-error ()
  18006. "Throw an error, a modified cursor command was applied in wrong context."
  18007. (user-error "This command is active in special context like tables, headlines or items"))
  18008. (defun org-shiftselect-error ()
  18009. "Throw an error because Shift-Cursor command was applied in wrong context."
  18010. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18011. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18012. (user-error "This command works only in special context like headlines or timestamps")))
  18013. (defun org-call-for-shift-select (cmd)
  18014. (let ((this-command-keys-shift-translated t))
  18015. (call-interactively cmd)))
  18016. (defun org-shifttab (&optional arg)
  18017. "Global visibility cycling or move to previous table field.
  18018. Call `org-table-previous-field' within a table.
  18019. When ARG is nil, cycle globally through visibility states.
  18020. When ARG is a numeric prefix, show contents of this level."
  18021. (interactive "P")
  18022. (cond
  18023. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18024. ((integerp arg)
  18025. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18026. (message "Content view to level: %d" arg)
  18027. (org-content (prefix-numeric-value arg2))
  18028. (org-cycle-show-empty-lines t)
  18029. (setq org-cycle-global-status 'overview)))
  18030. (t (call-interactively 'org-global-cycle))))
  18031. (defun org-shiftmetaleft ()
  18032. "Promote subtree or delete table column.
  18033. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18034. `org-table-delete-column', depending on context. See the
  18035. individual commands for more information."
  18036. (interactive)
  18037. (cond
  18038. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18039. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18040. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18041. ((if (not (org-region-active-p)) (org-at-item-p)
  18042. (save-excursion (goto-char (region-beginning))
  18043. (org-at-item-p)))
  18044. (call-interactively 'org-outdent-item-tree))
  18045. (t (org-modifier-cursor-error))))
  18046. (defun org-shiftmetaright ()
  18047. "Demote subtree or insert table column.
  18048. Calls `org-demote-subtree', `org-indent-item-tree', or
  18049. `org-table-insert-column', depending on context. See the
  18050. individual commands for more information."
  18051. (interactive)
  18052. (cond
  18053. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18054. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18055. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18056. ((if (not (org-region-active-p)) (org-at-item-p)
  18057. (save-excursion (goto-char (region-beginning))
  18058. (org-at-item-p)))
  18059. (call-interactively 'org-indent-item-tree))
  18060. (t (org-modifier-cursor-error))))
  18061. (defun org-shiftmetaup (&optional arg)
  18062. "Move subtree up or kill table row.
  18063. Calls `org-move-subtree-up' or `org-table-kill-row' or
  18064. `org-move-item-up' or `org-timestamp-up', depending on context.
  18065. See the individual commands for more information."
  18066. (interactive "P")
  18067. (cond
  18068. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18069. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18070. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18071. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18072. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18073. (call-interactively 'org-timestamp-up)))
  18074. (t (call-interactively 'org-drag-line-backward))))
  18075. (defun org-shiftmetadown (&optional arg)
  18076. "Move subtree down or insert table row.
  18077. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18078. `org-move-item-down' or `org-timestamp-up', depending on context.
  18079. See the individual commands for more information."
  18080. (interactive "P")
  18081. (cond
  18082. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18083. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18084. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18085. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18086. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18087. (call-interactively 'org-timestamp-down)))
  18088. (t (call-interactively 'org-drag-line-forward))))
  18089. (defsubst org-hidden-tree-error ()
  18090. (user-error
  18091. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18092. (defun org-metaleft (&optional arg)
  18093. "Promote heading or move table column to left.
  18094. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18095. With no specific context, calls the Emacs default `backward-word'.
  18096. See the individual commands for more information."
  18097. (interactive "P")
  18098. (cond
  18099. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18100. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18101. ((org-with-limited-levels
  18102. (or (org-at-heading-p)
  18103. (and (org-region-active-p)
  18104. (save-excursion
  18105. (goto-char (region-beginning))
  18106. (org-at-heading-p)))))
  18107. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18108. (call-interactively 'org-do-promote))
  18109. ;; At an inline task.
  18110. ((org-at-heading-p)
  18111. (call-interactively 'org-inlinetask-promote))
  18112. ((or (org-at-item-p)
  18113. (and (org-region-active-p)
  18114. (save-excursion
  18115. (goto-char (region-beginning))
  18116. (org-at-item-p))))
  18117. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18118. (call-interactively 'org-outdent-item))
  18119. (t (call-interactively 'backward-word))))
  18120. (defun org-metaright (&optional arg)
  18121. "Demote a subtree, a list item or move table column to right.
  18122. In front of a drawer or a block keyword, indent it correctly.
  18123. With no specific context, calls the Emacs default `forward-word'.
  18124. See the individual commands for more information."
  18125. (interactive "P")
  18126. (cond
  18127. ((run-hook-with-args-until-success 'org-metaright-hook))
  18128. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18129. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18130. ((org-at-block-p) (call-interactively 'org-indent-block))
  18131. ((org-with-limited-levels
  18132. (or (org-at-heading-p)
  18133. (and (org-region-active-p)
  18134. (save-excursion
  18135. (goto-char (region-beginning))
  18136. (org-at-heading-p)))))
  18137. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18138. (call-interactively 'org-do-demote))
  18139. ;; At an inline task.
  18140. ((org-at-heading-p)
  18141. (call-interactively 'org-inlinetask-demote))
  18142. ((or (org-at-item-p)
  18143. (and (org-region-active-p)
  18144. (save-excursion
  18145. (goto-char (region-beginning))
  18146. (org-at-item-p))))
  18147. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18148. (call-interactively 'org-indent-item))
  18149. (t (call-interactively 'forward-word))))
  18150. (defun org-check-for-hidden (what)
  18151. "Check if there are hidden headlines/items in the current visual line.
  18152. WHAT can be either `headlines' or `items'. If the current line is
  18153. an outline or item heading and it has a folded subtree below it,
  18154. this function returns t, nil otherwise."
  18155. (let ((re (cond
  18156. ((eq what 'headlines) org-outline-regexp-bol)
  18157. ((eq what 'items) (org-item-beginning-re))
  18158. (t (error "This should not happen"))))
  18159. beg end)
  18160. (save-excursion
  18161. (catch 'exit
  18162. (unless (org-region-active-p)
  18163. (setq beg (point-at-bol))
  18164. (beginning-of-line 2)
  18165. (while (and (not (eobp)) ;; this is like `next-line'
  18166. (get-char-property (1- (point)) 'invisible))
  18167. (beginning-of-line 2))
  18168. (setq end (point))
  18169. (goto-char beg)
  18170. (goto-char (point-at-eol))
  18171. (setq end (max end (point)))
  18172. (while (re-search-forward re end t)
  18173. (if (get-char-property (match-beginning 0) 'invisible)
  18174. (throw 'exit t))))
  18175. nil))))
  18176. (defun org-metaup (&optional arg)
  18177. "Move subtree up or move table row up.
  18178. Calls `org-move-subtree-up' or `org-table-move-row' or
  18179. `org-move-item-up', depending on context. See the individual commands
  18180. for more information."
  18181. (interactive "P")
  18182. (cond
  18183. ((run-hook-with-args-until-success 'org-metaup-hook))
  18184. ((org-region-active-p)
  18185. (let* ((a (min (region-beginning) (region-end)))
  18186. (b (1- (max (region-beginning) (region-end))))
  18187. (c (save-excursion (goto-char a)
  18188. (move-beginning-of-line 0)))
  18189. (d (save-excursion (goto-char a)
  18190. (move-end-of-line 0) (point))))
  18191. (transpose-regions a b c d)
  18192. (goto-char c)))
  18193. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18194. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18195. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18196. (t (org-drag-element-backward))))
  18197. (defun org-metadown (&optional arg)
  18198. "Move subtree down or move table row down.
  18199. Calls `org-move-subtree-down' or `org-table-move-row' or
  18200. `org-move-item-down', depending on context. See the individual
  18201. commands for more information."
  18202. (interactive "P")
  18203. (cond
  18204. ((run-hook-with-args-until-success 'org-metadown-hook))
  18205. ((org-region-active-p)
  18206. (let* ((a (min (region-beginning) (region-end)))
  18207. (b (max (region-beginning) (region-end)))
  18208. (c (save-excursion (goto-char b)
  18209. (move-beginning-of-line 1)))
  18210. (d (save-excursion (goto-char b)
  18211. (move-end-of-line 1) (1+ (point)))))
  18212. (transpose-regions a b c d)
  18213. (goto-char d)))
  18214. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18215. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18216. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18217. (t (org-drag-element-forward))))
  18218. (defun org-shiftup (&optional arg)
  18219. "Increase item in timestamp or increase priority of current headline.
  18220. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18221. depending on context. See the individual commands for more information."
  18222. (interactive "P")
  18223. (cond
  18224. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18225. ((and org-support-shift-select (org-region-active-p))
  18226. (org-call-for-shift-select 'previous-line))
  18227. ((org-at-timestamp-p t)
  18228. (call-interactively (if org-edit-timestamp-down-means-later
  18229. 'org-timestamp-down 'org-timestamp-up)))
  18230. ((and (not (eq org-support-shift-select 'always))
  18231. org-enable-priority-commands
  18232. (org-at-heading-p))
  18233. (call-interactively 'org-priority-up))
  18234. ((and (not org-support-shift-select) (org-at-item-p))
  18235. (call-interactively 'org-previous-item))
  18236. ((org-clocktable-try-shift 'up arg))
  18237. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18238. (org-support-shift-select
  18239. (org-call-for-shift-select 'previous-line))
  18240. (t (org-shiftselect-error))))
  18241. (defun org-shiftdown (&optional arg)
  18242. "Decrease item in timestamp or decrease priority of current headline.
  18243. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18244. depending on context. See the individual commands for more information."
  18245. (interactive "P")
  18246. (cond
  18247. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18248. ((and org-support-shift-select (org-region-active-p))
  18249. (org-call-for-shift-select 'next-line))
  18250. ((org-at-timestamp-p t)
  18251. (call-interactively (if org-edit-timestamp-down-means-later
  18252. 'org-timestamp-up 'org-timestamp-down)))
  18253. ((and (not (eq org-support-shift-select 'always))
  18254. org-enable-priority-commands
  18255. (org-at-heading-p))
  18256. (call-interactively 'org-priority-down))
  18257. ((and (not org-support-shift-select) (org-at-item-p))
  18258. (call-interactively 'org-next-item))
  18259. ((org-clocktable-try-shift 'down arg))
  18260. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18261. (org-support-shift-select
  18262. (org-call-for-shift-select 'next-line))
  18263. (t (org-shiftselect-error))))
  18264. (defun org-shiftright (&optional arg)
  18265. "Cycle the thing at point or in the current line, depending on context.
  18266. Depending on context, this does one of the following:
  18267. - switch a timestamp at point one day into the future
  18268. - on a headline, switch to the next TODO keyword.
  18269. - on an item, switch entire list to the next bullet type
  18270. - on a property line, switch to the next allowed value
  18271. - on a clocktable definition line, move time block into the future"
  18272. (interactive "P")
  18273. (cond
  18274. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18275. ((and org-support-shift-select (org-region-active-p))
  18276. (org-call-for-shift-select 'forward-char))
  18277. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18278. ((and (not (eq org-support-shift-select 'always))
  18279. (org-at-heading-p))
  18280. (let ((org-inhibit-logging
  18281. (not org-treat-S-cursor-todo-selection-as-state-change))
  18282. (org-inhibit-blocking
  18283. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18284. (org-call-with-arg 'org-todo 'right)))
  18285. ((or (and org-support-shift-select
  18286. (not (eq org-support-shift-select 'always))
  18287. (org-at-item-bullet-p))
  18288. (and (not org-support-shift-select) (org-at-item-p)))
  18289. (org-call-with-arg 'org-cycle-list-bullet nil))
  18290. ((and (not (eq org-support-shift-select 'always))
  18291. (org-at-property-p))
  18292. (call-interactively 'org-property-next-allowed-value))
  18293. ((org-clocktable-try-shift 'right arg))
  18294. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18295. (org-support-shift-select
  18296. (org-call-for-shift-select 'forward-char))
  18297. (t (org-shiftselect-error))))
  18298. (defun org-shiftleft (&optional arg)
  18299. "Cycle the thing at point or in the current line, depending on context.
  18300. Depending on context, this does one of the following:
  18301. - switch a timestamp at point one day into the past
  18302. - on a headline, switch to the previous TODO keyword.
  18303. - on an item, switch entire list to the previous bullet type
  18304. - on a property line, switch to the previous allowed value
  18305. - on a clocktable definition line, move time block into the past"
  18306. (interactive "P")
  18307. (cond
  18308. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18309. ((and org-support-shift-select (org-region-active-p))
  18310. (org-call-for-shift-select 'backward-char))
  18311. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18312. ((and (not (eq org-support-shift-select 'always))
  18313. (org-at-heading-p))
  18314. (let ((org-inhibit-logging
  18315. (not org-treat-S-cursor-todo-selection-as-state-change))
  18316. (org-inhibit-blocking
  18317. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18318. (org-call-with-arg 'org-todo 'left)))
  18319. ((or (and org-support-shift-select
  18320. (not (eq org-support-shift-select 'always))
  18321. (org-at-item-bullet-p))
  18322. (and (not org-support-shift-select) (org-at-item-p)))
  18323. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18324. ((and (not (eq org-support-shift-select 'always))
  18325. (org-at-property-p))
  18326. (call-interactively 'org-property-previous-allowed-value))
  18327. ((org-clocktable-try-shift 'left arg))
  18328. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18329. (org-support-shift-select
  18330. (org-call-for-shift-select 'backward-char))
  18331. (t (org-shiftselect-error))))
  18332. (defun org-shiftcontrolright ()
  18333. "Switch to next TODO set."
  18334. (interactive)
  18335. (cond
  18336. ((and org-support-shift-select (org-region-active-p))
  18337. (org-call-for-shift-select 'forward-word))
  18338. ((and (not (eq org-support-shift-select 'always))
  18339. (org-at-heading-p))
  18340. (org-call-with-arg 'org-todo 'nextset))
  18341. (org-support-shift-select
  18342. (org-call-for-shift-select 'forward-word))
  18343. (t (org-shiftselect-error))))
  18344. (defun org-shiftcontrolleft ()
  18345. "Switch to previous TODO set."
  18346. (interactive)
  18347. (cond
  18348. ((and org-support-shift-select (org-region-active-p))
  18349. (org-call-for-shift-select 'backward-word))
  18350. ((and (not (eq org-support-shift-select 'always))
  18351. (org-at-heading-p))
  18352. (org-call-with-arg 'org-todo 'previousset))
  18353. (org-support-shift-select
  18354. (org-call-for-shift-select 'backward-word))
  18355. (t (org-shiftselect-error))))
  18356. (defun org-shiftcontrolup (&optional n)
  18357. "Change timestamps synchronously up in CLOCK log lines.
  18358. Optional argument N tells to change by that many units."
  18359. (interactive "P")
  18360. (cond ((and (not org-support-shift-select)
  18361. (org-at-clock-log-p)
  18362. (org-at-timestamp-p t))
  18363. (org-clock-timestamps-up n))
  18364. (t (org-shiftselect-error))))
  18365. (defun org-shiftcontroldown (&optional n)
  18366. "Change timestamps synchronously down in CLOCK log lines.
  18367. Optional argument N tells to change by that many units."
  18368. (interactive "P")
  18369. (cond ((and (not org-support-shift-select)
  18370. (org-at-clock-log-p)
  18371. (org-at-timestamp-p t))
  18372. (org-clock-timestamps-down n))
  18373. (t (org-shiftselect-error))))
  18374. (defun org-ctrl-c-ret ()
  18375. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18376. (interactive)
  18377. (cond
  18378. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18379. (t (call-interactively 'org-insert-heading))))
  18380. (defun org-find-visible ()
  18381. (let ((s (point)))
  18382. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18383. (get-char-property s 'invisible)))
  18384. s))
  18385. (defun org-find-invisible ()
  18386. (let ((s (point)))
  18387. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18388. (not (get-char-property s 'invisible))))
  18389. s))
  18390. (defun org-copy-visible (beg end)
  18391. "Copy the visible parts of the region."
  18392. (interactive "r")
  18393. (let (snippets s)
  18394. (save-excursion
  18395. (save-restriction
  18396. (narrow-to-region beg end)
  18397. (setq s (goto-char (point-min)))
  18398. (while (not (= (point) (point-max)))
  18399. (goto-char (org-find-invisible))
  18400. (push (buffer-substring s (point)) snippets)
  18401. (setq s (goto-char (org-find-visible))))))
  18402. (kill-new (apply 'concat (nreverse snippets)))))
  18403. (defun org-copy-special ()
  18404. "Copy region in table or copy current subtree.
  18405. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18406. See the individual commands for more information."
  18407. (interactive)
  18408. (call-interactively
  18409. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18410. (defun org-cut-special ()
  18411. "Cut region in table or cut current subtree.
  18412. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18413. See the individual commands for more information."
  18414. (interactive)
  18415. (call-interactively
  18416. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18417. (defun org-paste-special (arg)
  18418. "Paste rectangular region into table, or past subtree relative to level.
  18419. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18420. See the individual commands for more information."
  18421. (interactive "P")
  18422. (if (org-at-table-p)
  18423. (org-table-paste-rectangle)
  18424. (org-paste-subtree arg)))
  18425. (defsubst org-in-fixed-width-region-p ()
  18426. "Is point in a fixed-width region?"
  18427. (save-match-data
  18428. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18429. (defun org-edit-special (&optional arg)
  18430. "Call a special editor for the element at point.
  18431. When at a table, call the formula editor with `org-table-edit-formulas'.
  18432. When in a source code block, call `org-edit-src-code'.
  18433. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18434. When at an #+INCLUDE keyword, visit the included file.
  18435. On a link, call `ffap' to visit the link at point.
  18436. Otherwise, return a user error."
  18437. (interactive "P")
  18438. (let ((element (org-element-at-point)))
  18439. (assert (not buffer-read-only) nil
  18440. "Buffer is read-only: %s" (buffer-name))
  18441. (case (org-element-type element)
  18442. (src-block
  18443. (if (not arg) (org-edit-src-code)
  18444. (let* ((info (org-babel-get-src-block-info))
  18445. (lang (nth 0 info))
  18446. (params (nth 2 info))
  18447. (session (cdr (assq :session params))))
  18448. (if (not session) (org-edit-src-code)
  18449. ;; At a src-block with a session and function called with
  18450. ;; an ARG: switch to the buffer related to the inferior
  18451. ;; process.
  18452. (switch-to-buffer
  18453. (funcall (intern (concat "org-babel-prep-session:" lang))
  18454. session params))))))
  18455. (keyword
  18456. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18457. (find-file
  18458. (org-remove-double-quotes
  18459. (car (org-split-string (org-element-property :value element)))))
  18460. (user-error "No special environment to edit here")))
  18461. (table
  18462. (if (eq (org-element-property :type element) 'table.el)
  18463. (org-edit-src-code)
  18464. (call-interactively 'org-table-edit-formulas)))
  18465. ;; Only Org tables contain `table-row' type elements.
  18466. (table-row (call-interactively 'org-table-edit-formulas))
  18467. ((example-block export-block) (org-edit-src-code))
  18468. (fixed-width (org-edit-fixed-width-region))
  18469. (otherwise
  18470. ;; No notable element at point. Though, we may be at a link,
  18471. ;; which is an object. Thus, scan deeper.
  18472. (if (eq (org-element-type (org-element-context element)) 'link)
  18473. (call-interactively 'ffap)
  18474. (user-error "No special environment to edit here"))))))
  18475. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18476. (defun org-ctrl-c-ctrl-c (&optional arg)
  18477. "Set tags in headline, or update according to changed information at point.
  18478. This command does many different things, depending on context:
  18479. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18480. this is what we do.
  18481. - If the cursor is on a statistics cookie, update it.
  18482. - If the cursor is in a headline, prompt for tags and insert them
  18483. into the current line, aligned to `org-tags-column'. When called
  18484. with prefix arg, realign all tags in the current buffer.
  18485. - If the cursor is in one of the special #+KEYWORD lines, this
  18486. triggers scanning the buffer for these lines and updating the
  18487. information.
  18488. - If the cursor is inside a table, realign the table. This command
  18489. works even if the automatic table editor has been turned off.
  18490. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18491. the entire table.
  18492. - If the cursor is at a footnote reference or definition, jump to
  18493. the corresponding definition or references, respectively.
  18494. - If the cursor is a the beginning of a dynamic block, update it.
  18495. - If the current buffer is a capture buffer, close note and file it.
  18496. - If the cursor is on a <<<target>>>, update radio targets and
  18497. corresponding links in this buffer.
  18498. - If the cursor is on a numbered item in a plain list, renumber the
  18499. ordered list.
  18500. - If the cursor is on a checkbox, toggle it.
  18501. - If the cursor is on a code block, evaluate it. The variable
  18502. `org-confirm-babel-evaluate' can be used to control prompting
  18503. before code block evaluation, by default every code block
  18504. evaluation requires confirmation. Code block evaluation can be
  18505. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18506. (interactive "P")
  18507. (cond
  18508. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18509. org-occur-highlights
  18510. org-latex-fragment-image-overlays)
  18511. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18512. (org-remove-occur-highlights)
  18513. (org-remove-latex-fragment-image-overlays)
  18514. (message "Temporary highlights/overlays removed from current buffer"))
  18515. ((and (local-variable-p 'org-finish-function (current-buffer))
  18516. (fboundp org-finish-function))
  18517. (funcall org-finish-function))
  18518. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18519. (t
  18520. (let* ((context (org-element-context)) (type (org-element-type context)))
  18521. ;; Test if point is within a blank line.
  18522. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18523. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18524. (user-error "C-c C-c can do nothing useful at this location"))
  18525. ;; For convenience: at the first line of a paragraph on the
  18526. ;; same line as an item, apply function on that item instead.
  18527. (when (eq type 'paragraph)
  18528. (let ((parent (org-element-property :parent context)))
  18529. (when (and (eq (org-element-type parent) 'item)
  18530. (= (point-at-bol) (org-element-property :begin parent)))
  18531. (setq context parent type 'item))))
  18532. ;; Act according to type of element or object at point.
  18533. (case type
  18534. (clock (org-clock-update-time-maybe))
  18535. (dynamic-block
  18536. (save-excursion
  18537. (goto-char (org-element-property :post-affiliated context))
  18538. (org-update-dblock)))
  18539. (footnote-definition
  18540. (goto-char (org-element-property :post-affiliated context))
  18541. (call-interactively 'org-footnote-action))
  18542. (footnote-reference (call-interactively 'org-footnote-action))
  18543. ((headline inlinetask)
  18544. (save-excursion (goto-char (org-element-property :begin context))
  18545. (call-interactively 'org-set-tags)))
  18546. (item
  18547. ;; At an item: a double C-u set checkbox to "[-]"
  18548. ;; unconditionally, whereas a single one will toggle its
  18549. ;; presence. Without an universal argument, if the item
  18550. ;; has a checkbox, toggle it. Otherwise repair the list.
  18551. (let* ((box (org-element-property :checkbox context))
  18552. (struct (org-element-property :structure context))
  18553. (old-struct (copy-tree struct))
  18554. (parents (org-list-parents-alist struct))
  18555. (prevs (org-list-prevs-alist struct))
  18556. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18557. (org-list-set-checkbox
  18558. (org-element-property :begin context) struct
  18559. (cond ((equal arg '(16)) "[-]")
  18560. ((and (not box) (equal arg '(4))) "[ ]")
  18561. ((or (not box) (equal arg '(4))) nil)
  18562. ((eq box 'on) "[ ]")
  18563. (t "[X]")))
  18564. ;; Mimic `org-list-write-struct' but with grabbing
  18565. ;; a return value from `org-list-struct-fix-box'.
  18566. (org-list-struct-fix-ind struct parents 2)
  18567. (org-list-struct-fix-item-end struct)
  18568. (org-list-struct-fix-bul struct prevs)
  18569. (org-list-struct-fix-ind struct parents)
  18570. (let ((block-item
  18571. (org-list-struct-fix-box struct parents prevs orderedp)))
  18572. (if (and box (equal struct old-struct))
  18573. (if (equal arg '(16))
  18574. (message "Checkboxes already reset")
  18575. (user-error "Cannot toggle this checkbox: %s"
  18576. (if (eq box 'on)
  18577. "all subitems checked"
  18578. "unchecked subitems")))
  18579. (org-list-struct-apply-struct struct old-struct)
  18580. (org-update-checkbox-count-maybe))
  18581. (when block-item
  18582. (message "Checkboxes were removed due to empty box at line %d"
  18583. (org-current-line block-item))))))
  18584. (keyword
  18585. (let ((org-inhibit-startup-visibility-stuff t)
  18586. (org-startup-align-all-tables nil))
  18587. (when (boundp 'org-table-coordinate-overlays)
  18588. (mapc 'delete-overlay org-table-coordinate-overlays)
  18589. (setq org-table-coordinate-overlays nil))
  18590. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18591. (message "Local setup has been refreshed"))
  18592. (plain-list
  18593. ;; At a plain list, with a double C-u argument, set
  18594. ;; checkboxes of each item to "[-]", whereas a single one
  18595. ;; will toggle their presence according to the state of the
  18596. ;; first item in the list. Without an argument, repair the
  18597. ;; list.
  18598. (let* ((begin (org-element-property :contents-begin context))
  18599. (beginm (move-marker (make-marker) begin))
  18600. (struct (org-element-property :structure context))
  18601. (old-struct (copy-tree struct))
  18602. (first-box (save-excursion
  18603. (goto-char begin)
  18604. (looking-at org-list-full-item-re)
  18605. (match-string-no-properties 3)))
  18606. (new-box (cond ((equal arg '(16)) "[-]")
  18607. ((equal arg '(4)) (unless first-box "[ ]"))
  18608. ((equal first-box "[X]") "[ ]")
  18609. (t "[X]"))))
  18610. (cond
  18611. (arg
  18612. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18613. (org-list-get-all-items
  18614. begin struct (org-list-prevs-alist struct))))
  18615. ((and first-box (eq (point) begin))
  18616. ;; For convenience, when point is at bol on the first
  18617. ;; item of the list and no argument is provided, simply
  18618. ;; toggle checkbox of that item, if any.
  18619. (org-list-set-checkbox begin struct new-box)))
  18620. (org-list-write-struct
  18621. struct (org-list-parents-alist struct) old-struct)
  18622. (org-update-checkbox-count-maybe)
  18623. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18624. ((property-drawer node-property)
  18625. (call-interactively 'org-property-action))
  18626. ((radio-target target)
  18627. (call-interactively 'org-update-radio-target-regexp))
  18628. (statistics-cookie
  18629. (call-interactively 'org-update-statistics-cookies))
  18630. ((table table-cell table-row)
  18631. ;; At a table, recalculate every field and align it. Also
  18632. ;; send the table if necessary. If the table has
  18633. ;; a `table.el' type, just give up. At a table row or
  18634. ;; cell, maybe recalculate line but always align table.
  18635. (if (eq (org-element-property :type context) 'table.el)
  18636. (message "Use C-c ' to edit table.el tables")
  18637. (let ((org-enable-table-editor t))
  18638. (if (or (eq type 'table)
  18639. ;; Check if point is at a TBLFM line.
  18640. (and (eq type 'table-row)
  18641. (= (point) (org-element-property :end context))))
  18642. (save-excursion
  18643. (if (org-at-TBLFM-p)
  18644. (progn (require 'org-table)
  18645. (org-table-calc-current-TBLFM))
  18646. (goto-char (org-element-property :contents-begin context))
  18647. (org-call-with-arg 'org-table-recalculate (or arg t))
  18648. (orgtbl-send-table 'maybe)))
  18649. (org-table-maybe-eval-formula)
  18650. (cond (arg (call-interactively 'org-table-recalculate))
  18651. ((org-table-maybe-recalculate-line))
  18652. (t (org-table-align)))))))
  18653. (timestamp (org-timestamp-change 0 'day))
  18654. (otherwise
  18655. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18656. (user-error
  18657. "C-c C-c can do nothing useful at this location")))))))))
  18658. (defun org-mode-restart ()
  18659. "Restart Org-mode, to scan again for special lines.
  18660. Also updates the keyword regular expressions."
  18661. (interactive)
  18662. (org-mode)
  18663. (message "Org-mode restarted"))
  18664. (defun org-kill-note-or-show-branches ()
  18665. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18666. (interactive)
  18667. (if (not org-finish-function)
  18668. (progn
  18669. (hide-subtree)
  18670. (call-interactively 'show-branches))
  18671. (let ((org-note-abort t))
  18672. (funcall org-finish-function))))
  18673. (defun org-open-line (n)
  18674. "Insert a new row in tables, call `open-line' elsewhere."
  18675. (interactive "*p")
  18676. (if (org-at-table-p)
  18677. (org-table-insert-row)
  18678. (open-line n)))
  18679. (defun org-return (&optional indent)
  18680. "Goto next table row or insert a newline.
  18681. Calls `org-table-next-row' or `newline', depending on context.
  18682. See the individual commands for more information."
  18683. (interactive)
  18684. (let (org-ts-what)
  18685. (cond
  18686. ((or (bobp) (org-in-src-block-p))
  18687. (if indent (newline-and-indent) (newline)))
  18688. ((org-at-table-p)
  18689. (org-table-justify-field-maybe)
  18690. (call-interactively 'org-table-next-row))
  18691. ;; when `newline-and-indent' is called within a list, make sure
  18692. ;; text moved stays inside the item.
  18693. ((and (org-in-item-p) indent)
  18694. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18695. (progn
  18696. (save-match-data (newline))
  18697. (org-indent-line-to (length (match-string 0))))
  18698. (let ((ind (org-get-indentation)))
  18699. (newline)
  18700. (if (org-looking-back org-list-end-re)
  18701. (org-indent-line)
  18702. (org-indent-line-to ind)))))
  18703. ((and org-return-follows-link
  18704. (org-at-timestamp-p t)
  18705. (not (eq org-ts-what 'after)))
  18706. (org-follow-timestamp-link))
  18707. ((and org-return-follows-link
  18708. (let ((tprop (get-text-property (point) 'face)))
  18709. (or (eq tprop 'org-link)
  18710. (and (listp tprop) (memq 'org-link tprop)))))
  18711. (call-interactively 'org-open-at-point))
  18712. ((and (org-at-heading-p)
  18713. (looking-at
  18714. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18715. (org-show-entry)
  18716. (end-of-line 1)
  18717. (newline))
  18718. (t (if indent (newline-and-indent) (newline))))))
  18719. (defun org-return-indent ()
  18720. "Goto next table row or insert a newline and indent.
  18721. Calls `org-table-next-row' or `newline-and-indent', depending on
  18722. context. See the individual commands for more information."
  18723. (interactive)
  18724. (org-return t))
  18725. (defun org-ctrl-c-star ()
  18726. "Compute table, or change heading status of lines.
  18727. Calls `org-table-recalculate' or `org-toggle-heading',
  18728. depending on context."
  18729. (interactive)
  18730. (cond
  18731. ((org-at-table-p)
  18732. (call-interactively 'org-table-recalculate))
  18733. (t
  18734. ;; Convert all lines in region to list items
  18735. (call-interactively 'org-toggle-heading))))
  18736. (defun org-ctrl-c-minus ()
  18737. "Insert separator line in table or modify bullet status of line.
  18738. Also turns a plain line or a region of lines into list items.
  18739. Calls `org-table-insert-hline', `org-toggle-item', or
  18740. `org-cycle-list-bullet', depending on context."
  18741. (interactive)
  18742. (cond
  18743. ((org-at-table-p)
  18744. (call-interactively 'org-table-insert-hline))
  18745. ((org-region-active-p)
  18746. (call-interactively 'org-toggle-item))
  18747. ((org-in-item-p)
  18748. (call-interactively 'org-cycle-list-bullet))
  18749. (t
  18750. (call-interactively 'org-toggle-item))))
  18751. (defun org-toggle-item (arg)
  18752. "Convert headings or normal lines to items, items to normal lines.
  18753. If there is no active region, only the current line is considered.
  18754. If the first non blank line in the region is a headline, convert
  18755. all headlines to items, shifting text accordingly.
  18756. If it is an item, convert all items to normal lines.
  18757. If it is normal text, change region into a list of items.
  18758. With a prefix argument ARG, change the region in a single item."
  18759. (interactive "P")
  18760. (let ((shift-text
  18761. (function
  18762. ;; Shift text in current section to IND, from point to END.
  18763. ;; The function leaves point to END line.
  18764. (lambda (ind end)
  18765. (let ((min-i 1000) (end (copy-marker end)))
  18766. ;; First determine the minimum indentation (MIN-I) of
  18767. ;; the text.
  18768. (save-excursion
  18769. (catch 'exit
  18770. (while (< (point) end)
  18771. (let ((i (org-get-indentation)))
  18772. (cond
  18773. ;; Skip blank lines and inline tasks.
  18774. ((looking-at "^[ \t]*$"))
  18775. ((looking-at org-outline-regexp-bol))
  18776. ;; We can't find less than 0 indentation.
  18777. ((zerop i) (throw 'exit (setq min-i 0)))
  18778. ((< i min-i) (setq min-i i))))
  18779. (forward-line))))
  18780. ;; Then indent each line so that a line indented to
  18781. ;; MIN-I becomes indented to IND. Ignore blank lines
  18782. ;; and inline tasks in the process.
  18783. (let ((delta (- ind min-i)))
  18784. (while (< (point) end)
  18785. (unless (or (looking-at "^[ \t]*$")
  18786. (looking-at org-outline-regexp-bol))
  18787. (org-indent-line-to (+ (org-get-indentation) delta)))
  18788. (forward-line)))))))
  18789. (skip-blanks
  18790. (function
  18791. ;; Return beginning of first non-blank line, starting from
  18792. ;; line at POS.
  18793. (lambda (pos)
  18794. (save-excursion
  18795. (goto-char pos)
  18796. (skip-chars-forward " \r\t\n")
  18797. (point-at-bol)))))
  18798. beg end)
  18799. ;; Determine boundaries of changes.
  18800. (if (org-region-active-p)
  18801. (setq beg (funcall skip-blanks (region-beginning))
  18802. end (copy-marker (region-end)))
  18803. (setq beg (funcall skip-blanks (point-at-bol))
  18804. end (copy-marker (point-at-eol))))
  18805. ;; Depending on the starting line, choose an action on the text
  18806. ;; between BEG and END.
  18807. (org-with-limited-levels
  18808. (save-excursion
  18809. (goto-char beg)
  18810. (cond
  18811. ;; Case 1. Start at an item: de-itemize. Note that it only
  18812. ;; happens when a region is active: `org-ctrl-c-minus'
  18813. ;; would call `org-cycle-list-bullet' otherwise.
  18814. ((org-at-item-p)
  18815. (while (< (point) end)
  18816. (when (org-at-item-p)
  18817. (skip-chars-forward " \t")
  18818. (delete-region (point) (match-end 0)))
  18819. (forward-line)))
  18820. ;; Case 2. Start at an heading: convert to items.
  18821. ((org-at-heading-p)
  18822. (let* ((bul (org-list-bullet-string "-"))
  18823. (bul-len (length bul))
  18824. ;; Indentation of the first heading. It should be
  18825. ;; relative to the indentation of its parent, if any.
  18826. (start-ind (save-excursion
  18827. (cond
  18828. ((not org-adapt-indentation) 0)
  18829. ((not (outline-previous-heading)) 0)
  18830. (t (length (match-string 0))))))
  18831. ;; Level of first heading. Further headings will be
  18832. ;; compared to it to determine hierarchy in the list.
  18833. (ref-level (org-reduced-level (org-outline-level))))
  18834. (while (< (point) end)
  18835. (let* ((level (org-reduced-level (org-outline-level)))
  18836. (delta (max 0 (- level ref-level))))
  18837. ;; If current headline is less indented than the first
  18838. ;; one, set it as reference, in order to preserve
  18839. ;; subtrees.
  18840. (when (< level ref-level) (setq ref-level level))
  18841. (replace-match bul t t)
  18842. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18843. ;; Ensure all text down to END (or SECTION-END) belongs
  18844. ;; to the newly created item.
  18845. (let ((section-end (save-excursion
  18846. (or (outline-next-heading) (point)))))
  18847. (forward-line)
  18848. (funcall shift-text
  18849. (+ start-ind (* (1+ delta) bul-len))
  18850. (min end section-end)))))))
  18851. ;; Case 3. Normal line with ARG: make the first line of region
  18852. ;; an item, and shift indentation of others lines to
  18853. ;; set them as item's body.
  18854. (arg (let* ((bul (org-list-bullet-string "-"))
  18855. (bul-len (length bul))
  18856. (ref-ind (org-get-indentation)))
  18857. (skip-chars-forward " \t")
  18858. (insert bul)
  18859. (forward-line)
  18860. (while (< (point) end)
  18861. ;; Ensure that lines less indented than first one
  18862. ;; still get included in item body.
  18863. (funcall shift-text
  18864. (+ ref-ind bul-len)
  18865. (min end (save-excursion (or (outline-next-heading)
  18866. (point)))))
  18867. (forward-line))))
  18868. ;; Case 4. Normal line without ARG: turn each non-item line
  18869. ;; into an item.
  18870. (t
  18871. (while (< (point) end)
  18872. (unless (or (org-at-heading-p) (org-at-item-p))
  18873. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18874. (replace-match
  18875. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18876. (forward-line))))))))
  18877. (defun org-toggle-heading (&optional nstars)
  18878. "Convert headings to normal text, or items or text to headings.
  18879. If there is no active region, only convert the current line.
  18880. With a \\[universal-argument] prefix, convert the whole list at
  18881. point into heading.
  18882. In a region:
  18883. - If the first non blank line is a headline, remove the stars
  18884. from all headlines in the region.
  18885. - If it is a normal line, turn each and every normal line (i.e.,
  18886. not an heading or an item) in the region into headings. If you
  18887. want to convert only the first line of this region, use one
  18888. universal prefix argument.
  18889. - If it is a plain list item, turn all plain list items into headings.
  18890. When converting a line into a heading, the number of stars is chosen
  18891. such that the lines become children of the current entry. However,
  18892. when a numeric prefix argument is given, its value determines the
  18893. number of stars to add."
  18894. (interactive "P")
  18895. (let ((skip-blanks
  18896. (function
  18897. ;; Return beginning of first non-blank line, starting from
  18898. ;; line at POS.
  18899. (lambda (pos)
  18900. (save-excursion
  18901. (goto-char pos)
  18902. (while (org-at-comment-p) (forward-line))
  18903. (skip-chars-forward " \r\t\n")
  18904. (point-at-bol)))))
  18905. beg end toggled)
  18906. ;; Determine boundaries of changes. If a universal prefix has
  18907. ;; been given, put the list in a region. If region ends at a bol,
  18908. ;; do not consider the last line to be in the region.
  18909. (when (and current-prefix-arg (org-at-item-p))
  18910. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18911. (org-mark-element))
  18912. (if (org-region-active-p)
  18913. (setq beg (funcall skip-blanks (region-beginning))
  18914. end (copy-marker (save-excursion
  18915. (goto-char (region-end))
  18916. (if (bolp) (point) (point-at-eol)))))
  18917. (setq beg (funcall skip-blanks (point-at-bol))
  18918. end (copy-marker (point-at-eol))))
  18919. ;; Ensure inline tasks don't count as headings.
  18920. (org-with-limited-levels
  18921. (save-excursion
  18922. (goto-char beg)
  18923. (cond
  18924. ;; Case 1. Started at an heading: de-star headings.
  18925. ((org-at-heading-p)
  18926. (while (< (point) end)
  18927. (when (org-at-heading-p t)
  18928. (looking-at org-outline-regexp) (replace-match "")
  18929. (setq toggled t))
  18930. (forward-line)))
  18931. ;; Case 2. Started at an item: change items into headlines.
  18932. ;; One star will be added by `org-list-to-subtree'.
  18933. ((org-at-item-p)
  18934. (let* ((stars (make-string
  18935. ;; subtract the star that will be added again by
  18936. ;; `org-list-to-subtree'
  18937. (if (numberp nstars) (1- nstars)
  18938. (or (org-current-level) 0))
  18939. ?*))
  18940. (add-stars
  18941. (cond (nstars "") ; stars from prefix only
  18942. ((equal stars "") "") ; before first heading
  18943. (org-odd-levels-only "*") ; inside heading, odd
  18944. (t "")))) ; inside heading, oddeven
  18945. (while (< (point) end)
  18946. (when (org-at-item-p)
  18947. ;; Pay attention to cases when region ends before list.
  18948. (let* ((struct (org-list-struct))
  18949. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18950. (save-restriction
  18951. (narrow-to-region (point) list-end)
  18952. (insert
  18953. (org-list-to-subtree
  18954. (org-list-parse-list t)
  18955. '(:istart (concat stars add-stars (funcall get-stars depth))
  18956. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18957. (setq toggled t))
  18958. (forward-line))))
  18959. ;; Case 3. Started at normal text: make every line an heading,
  18960. ;; skipping headlines and items.
  18961. (t (let* ((stars
  18962. (make-string
  18963. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18964. (add-stars
  18965. (cond (nstars "") ; stars from prefix only
  18966. ((equal stars "") "*") ; before first heading
  18967. (org-odd-levels-only "**") ; inside heading, odd
  18968. (t "*"))) ; inside heading, oddeven
  18969. (rpl (concat stars add-stars " "))
  18970. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18971. (while (< (point) (if (equal nstars '(4)) lend end))
  18972. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18973. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18974. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18975. (forward-line)))))))
  18976. (unless toggled (message "Cannot toggle heading from here"))))
  18977. (defun org-meta-return (&optional arg)
  18978. "Insert a new heading or wrap a region in a table.
  18979. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18980. See the individual commands for more information."
  18981. (interactive "P")
  18982. (org-check-before-invisible-edit 'insert)
  18983. (cond
  18984. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18985. ((or (org-at-drawer-p) (org-in-drawer-p) (org-at-property-p))
  18986. (newline-and-indent))
  18987. ((org-at-table-p)
  18988. (call-interactively 'org-table-wrap-region))
  18989. (t (call-interactively 'org-insert-heading))))
  18990. ;;; Menu entries
  18991. (defsubst org-in-subtree-not-table-p ()
  18992. "Are we in a subtree and not in a table?"
  18993. (and (not (org-before-first-heading-p))
  18994. (not (org-at-table-p))))
  18995. ;; Define the Org-mode menus
  18996. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18997. '("Tbl"
  18998. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18999. ["Next Field" org-cycle (org-at-table-p)]
  19000. ["Previous Field" org-shifttab (org-at-table-p)]
  19001. ["Next Row" org-return (org-at-table-p)]
  19002. "--"
  19003. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19004. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19005. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19006. "--"
  19007. ("Column"
  19008. ["Move Column Left" org-metaleft (org-at-table-p)]
  19009. ["Move Column Right" org-metaright (org-at-table-p)]
  19010. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19011. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19012. ("Row"
  19013. ["Move Row Up" org-metaup (org-at-table-p)]
  19014. ["Move Row Down" org-metadown (org-at-table-p)]
  19015. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19016. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19017. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19018. "--"
  19019. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19020. ("Rectangle"
  19021. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19022. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19023. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19024. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19025. "--"
  19026. ("Calculate"
  19027. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19028. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19029. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19030. "--"
  19031. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19032. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19033. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19034. "--"
  19035. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19036. "--"
  19037. ["Sum Column/Rectangle" org-table-sum
  19038. (or (org-at-table-p) (org-region-active-p))]
  19039. ["Which Column?" org-table-current-column (org-at-table-p)])
  19040. ["Debug Formulas"
  19041. org-table-toggle-formula-debugger
  19042. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19043. ["Show Col/Row Numbers"
  19044. org-table-toggle-coordinate-overlays
  19045. :style toggle
  19046. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19047. "--"
  19048. ["Create" org-table-create (and (not (org-at-table-p))
  19049. org-enable-table-editor)]
  19050. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19051. ["Import from File" org-table-import (not (org-at-table-p))]
  19052. ["Export to File" org-table-export (org-at-table-p)]
  19053. "--"
  19054. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19055. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19056. '("Org"
  19057. ("Show/Hide"
  19058. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19059. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19060. ["Sparse Tree..." org-sparse-tree t]
  19061. ["Reveal Context" org-reveal t]
  19062. ["Show All" show-all t]
  19063. "--"
  19064. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19065. "--"
  19066. ["New Heading" org-insert-heading t]
  19067. ("Navigate Headings"
  19068. ["Up" outline-up-heading t]
  19069. ["Next" outline-next-visible-heading t]
  19070. ["Previous" outline-previous-visible-heading t]
  19071. ["Next Same Level" outline-forward-same-level t]
  19072. ["Previous Same Level" outline-backward-same-level t]
  19073. "--"
  19074. ["Jump" org-goto t])
  19075. ("Edit Structure"
  19076. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19077. "--"
  19078. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19079. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19080. "--"
  19081. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19082. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19083. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19084. "--"
  19085. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19086. "--"
  19087. ["Copy visible text" org-copy-visible t]
  19088. "--"
  19089. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19090. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19091. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19092. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19093. "--"
  19094. ["Sort Region/Children" org-sort t]
  19095. "--"
  19096. ["Convert to odd levels" org-convert-to-odd-levels t]
  19097. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19098. ("Editing"
  19099. ["Emphasis..." org-emphasize t]
  19100. ["Edit Source Example" org-edit-special t]
  19101. "--"
  19102. ["Footnote new/jump" org-footnote-action t]
  19103. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19104. ("Archive"
  19105. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19106. "--"
  19107. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19108. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19109. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19110. )
  19111. "--"
  19112. ("Hyperlinks"
  19113. ["Store Link (Global)" org-store-link t]
  19114. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19115. ["Insert Link" org-insert-link t]
  19116. ["Follow Link" org-open-at-point t]
  19117. "--"
  19118. ["Next link" org-next-link t]
  19119. ["Previous link" org-previous-link t]
  19120. "--"
  19121. ["Descriptive Links"
  19122. org-toggle-link-display
  19123. :style radio
  19124. :selected org-descriptive-links
  19125. ]
  19126. ["Literal Links"
  19127. org-toggle-link-display
  19128. :style radio
  19129. :selected (not org-descriptive-links)])
  19130. "--"
  19131. ("TODO Lists"
  19132. ["TODO/DONE/-" org-todo t]
  19133. ("Select keyword"
  19134. ["Next keyword" org-shiftright (org-at-heading-p)]
  19135. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19136. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19137. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19138. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19139. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19140. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19141. "--"
  19142. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19143. :selected org-enforce-todo-dependencies :style toggle :active t]
  19144. "Settings for tree at point"
  19145. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19146. :selected (org-entry-get nil "ORDERED")
  19147. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19148. ["Do Children parallel" org-toggle-ordered-property :style radio
  19149. :selected (not (org-entry-get nil "ORDERED"))
  19150. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19151. "--"
  19152. ["Set Priority" org-priority t]
  19153. ["Priority Up" org-shiftup t]
  19154. ["Priority Down" org-shiftdown t]
  19155. "--"
  19156. ["Get news from all feeds" org-feed-update-all t]
  19157. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19158. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19159. ("TAGS and Properties"
  19160. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19161. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19162. "--"
  19163. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19164. ["Column view of properties" org-columns t]
  19165. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19166. ("Dates and Scheduling"
  19167. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19168. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19169. ("Change Date"
  19170. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19171. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19172. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19173. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19174. ["Compute Time Range" org-evaluate-time-range t]
  19175. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19176. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19177. "--"
  19178. ["Custom time format" org-toggle-time-stamp-overlays
  19179. :style radio :selected org-display-custom-times]
  19180. "--"
  19181. ["Goto Calendar" org-goto-calendar t]
  19182. ["Date from Calendar" org-date-from-calendar t]
  19183. "--"
  19184. ["Start/Restart Timer" org-timer-start t]
  19185. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19186. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19187. ["Insert Timer String" org-timer t]
  19188. ["Insert Timer Item" org-timer-item t])
  19189. ("Logging work"
  19190. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19191. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19192. ["Clock out" org-clock-out t]
  19193. ["Clock cancel" org-clock-cancel t]
  19194. "--"
  19195. ["Mark as default task" org-clock-mark-default-task t]
  19196. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19197. ["Goto running clock" org-clock-goto t]
  19198. "--"
  19199. ["Display times" org-clock-display t]
  19200. ["Create clock table" org-clock-report t]
  19201. "--"
  19202. ["Record DONE time"
  19203. (progn (setq org-log-done (not org-log-done))
  19204. (message "Switching to %s will %s record a timestamp"
  19205. (car org-done-keywords)
  19206. (if org-log-done "automatically" "not")))
  19207. :style toggle :selected org-log-done])
  19208. "--"
  19209. ["Agenda Command..." org-agenda t]
  19210. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19211. ("File List for Agenda")
  19212. ("Special views current file"
  19213. ["TODO Tree" org-show-todo-tree t]
  19214. ["Check Deadlines" org-check-deadlines t]
  19215. ["Timeline" org-timeline t]
  19216. ["Tags/Property tree" org-match-sparse-tree t])
  19217. "--"
  19218. ["Export/Publish..." org-export-dispatch t]
  19219. ("LaTeX"
  19220. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19221. :selected org-cdlatex-mode]
  19222. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19223. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19224. ["Modify math symbol" org-cdlatex-math-modify
  19225. (org-inside-LaTeX-fragment-p)]
  19226. ["Insert citation" org-reftex-citation t]
  19227. "--"
  19228. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19229. "--"
  19230. ("MobileOrg"
  19231. ["Push Files and Views" org-mobile-push t]
  19232. ["Get Captured and Flagged" org-mobile-pull t]
  19233. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19234. "--"
  19235. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19236. "--"
  19237. ("Documentation"
  19238. ["Show Version" org-version t]
  19239. ["Info Documentation" org-info t])
  19240. ("Customize"
  19241. ["Browse Org Group" org-customize t]
  19242. "--"
  19243. ["Expand This Menu" org-create-customize-menu
  19244. (fboundp 'customize-menu-create)])
  19245. ["Send bug report" org-submit-bug-report t]
  19246. "--"
  19247. ("Refresh/Reload"
  19248. ["Refresh setup current buffer" org-mode-restart t]
  19249. ["Reload Org (after update)" org-reload t]
  19250. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19251. ))
  19252. (defun org-info (&optional node)
  19253. "Read documentation for Org-mode in the info system.
  19254. With optional NODE, go directly to that node."
  19255. (interactive)
  19256. (info (format "(org)%s" (or node ""))))
  19257. ;;;###autoload
  19258. (defun org-submit-bug-report ()
  19259. "Submit a bug report on Org-mode via mail.
  19260. Don't hesitate to report any problems or inaccurate documentation.
  19261. If you don't have setup sending mail from (X)Emacs, please copy the
  19262. output buffer into your mail program, as it gives us important
  19263. information about your Org-mode version and configuration."
  19264. (interactive)
  19265. (require 'reporter)
  19266. (org-load-modules-maybe)
  19267. (org-require-autoloaded-modules)
  19268. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19269. (reporter-submit-bug-report
  19270. "emacs-orgmode@gnu.org"
  19271. (org-version nil 'full)
  19272. (let (list)
  19273. (save-window-excursion
  19274. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19275. (delete-other-windows)
  19276. (erase-buffer)
  19277. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19278. We would like to add your full Org-mode and Outline configuration to the
  19279. bug report. This greatly simplifies the work of the maintainer and
  19280. other experts on the mailing list.
  19281. HOWEVER, some variables you have customized may contain private
  19282. information. The names of customers, colleagues, or friends, might
  19283. appear in the form of file names, tags, todo states, or search strings.
  19284. If you answer yes to the prompt, you might want to check and remove
  19285. such private information before sending the email.")
  19286. (add-text-properties (point-min) (point-max) '(face org-warning))
  19287. (when (yes-or-no-p "Include your Org-mode configuration ")
  19288. (mapatoms
  19289. (lambda (v)
  19290. (and (boundp v)
  19291. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19292. (or (and (symbol-value v)
  19293. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19294. (and
  19295. (get v 'custom-type) (get v 'standard-value)
  19296. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19297. (push v list)))))
  19298. (kill-buffer (get-buffer "*Warn about privacy*"))
  19299. list))
  19300. nil nil
  19301. "Remember to cover the basics, that is, what you expected to happen and
  19302. what in fact did happen. You don't know how to make a good report? See
  19303. http://orgmode.org/manual/Feedback.html#Feedback
  19304. Your bug report will be posted to the Org-mode mailing list.
  19305. ------------------------------------------------------------------------")
  19306. (save-excursion
  19307. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19308. (replace-match "\\1Bug: \\3 [\\2]")))))
  19309. (defun org-install-agenda-files-menu ()
  19310. (let ((bl (buffer-list)))
  19311. (save-excursion
  19312. (while bl
  19313. (set-buffer (pop bl))
  19314. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19315. (when (derived-mode-p 'org-mode)
  19316. (easy-menu-change
  19317. '("Org") "File List for Agenda"
  19318. (append
  19319. (list
  19320. ["Edit File List" (org-edit-agenda-file-list) t]
  19321. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19322. ["Remove Current File from List" org-remove-file t]
  19323. ["Cycle through agenda files" org-cycle-agenda-files t]
  19324. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19325. "--")
  19326. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19327. ;;;; Documentation
  19328. (defun org-require-autoloaded-modules ()
  19329. (interactive)
  19330. (mapc 'require
  19331. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19332. org-remember org-table org-timer)))
  19333. ;;;###autoload
  19334. (defun org-reload (&optional uncompiled)
  19335. "Reload all org lisp files.
  19336. With prefix arg UNCOMPILED, load the uncompiled versions."
  19337. (interactive "P")
  19338. (require 'loadhist)
  19339. (let* ((org-dir (org-find-library-dir "org"))
  19340. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19341. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19342. (remove-re (mapconcat 'identity
  19343. (mapcar (lambda (f) (concat "^" f "$"))
  19344. (list (if (featurep 'xemacs)
  19345. "org-colview"
  19346. "org-colview-xemacs")
  19347. "org" "org-loaddefs" "org-version"))
  19348. "\\|"))
  19349. (feats (delete-dups
  19350. (mapcar 'file-name-sans-extension
  19351. (mapcar 'file-name-nondirectory
  19352. (delq nil
  19353. (mapcar 'feature-file
  19354. features))))))
  19355. (lfeat (append
  19356. (sort
  19357. (setq feats
  19358. (delq nil (mapcar
  19359. (lambda (f)
  19360. (if (and (string-match feature-re f)
  19361. (not (string-match remove-re f)))
  19362. f nil))
  19363. feats)))
  19364. 'string-lessp)
  19365. (list "org-version" "org")))
  19366. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19367. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19368. load-uncore load-misses)
  19369. (setq load-misses
  19370. (delq 't
  19371. (mapcar (lambda (f)
  19372. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19373. (and (string= org-dir contrib-dir)
  19374. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19375. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19376. (add-to-list 'load-uncore f 'append)
  19377. 't)
  19378. f))
  19379. lfeat)))
  19380. (if load-uncore
  19381. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19382. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19383. (if load-misses
  19384. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19385. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19386. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19387. ;;;###autoload
  19388. (defun org-customize ()
  19389. "Call the customize function with org as argument."
  19390. (interactive)
  19391. (org-load-modules-maybe)
  19392. (org-require-autoloaded-modules)
  19393. (customize-browse 'org))
  19394. (defun org-create-customize-menu ()
  19395. "Create a full customization menu for Org-mode, insert it into the menu."
  19396. (interactive)
  19397. (org-load-modules-maybe)
  19398. (org-require-autoloaded-modules)
  19399. (if (fboundp 'customize-menu-create)
  19400. (progn
  19401. (easy-menu-change
  19402. '("Org") "Customize"
  19403. `(["Browse Org group" org-customize t]
  19404. "--"
  19405. ,(customize-menu-create 'org)
  19406. ["Set" Custom-set t]
  19407. ["Save" Custom-save t]
  19408. ["Reset to Current" Custom-reset-current t]
  19409. ["Reset to Saved" Custom-reset-saved t]
  19410. ["Reset to Standard Settings" Custom-reset-standard t]))
  19411. (message "\"Org\"-menu now contains full customization menu"))
  19412. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19413. ;;;; Miscellaneous stuff
  19414. ;;; Generally useful functions
  19415. (defun org-get-at-bol (property)
  19416. "Get text property PROPERTY at beginning of line."
  19417. (get-text-property (point-at-bol) property))
  19418. (defun org-find-text-property-in-string (prop s)
  19419. "Return the first non-nil value of property PROP in string S."
  19420. (or (get-text-property 0 prop s)
  19421. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19422. prop s)))
  19423. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19424. "Display the given MESSAGE as a warning."
  19425. (if (fboundp 'display-warning)
  19426. (display-warning 'org message
  19427. (if (featurep 'xemacs) 'warning :warning))
  19428. (let ((buf (get-buffer-create "*Org warnings*")))
  19429. (with-current-buffer buf
  19430. (goto-char (point-max))
  19431. (insert "Warning (Org): " message)
  19432. (unless (bolp)
  19433. (newline)))
  19434. (display-buffer buf)
  19435. (sit-for 0))))
  19436. (defun org-eval (form)
  19437. "Eval FORM and return result."
  19438. (condition-case error
  19439. (eval form)
  19440. (error (format "%%![Error: %s]" error))))
  19441. (defun org-in-clocktable-p ()
  19442. "Check if the cursor is in a clocktable."
  19443. (let ((pos (point)) start)
  19444. (save-excursion
  19445. (end-of-line 1)
  19446. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19447. (setq start (match-beginning 0))
  19448. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19449. (>= (match-end 0) pos)
  19450. start))))
  19451. (defun org-in-commented-line ()
  19452. "Is point in a line starting with `#'?"
  19453. (equal (char-after (point-at-bol)) ?#))
  19454. (defun org-in-indented-comment-line ()
  19455. "Is point in a line starting with `#' after some white space?"
  19456. (save-excursion
  19457. (save-match-data
  19458. (goto-char (point-at-bol))
  19459. (looking-at "[ \t]*#"))))
  19460. (defun org-in-verbatim-emphasis ()
  19461. (save-match-data
  19462. (and (org-in-regexp org-emph-re 2)
  19463. (>= (point) (match-beginning 3))
  19464. (<= (point) (match-end 4))
  19465. (member (match-string 3) '("=" "~")))))
  19466. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19467. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19468. (if (and marker (marker-buffer marker)
  19469. (buffer-live-p (marker-buffer marker)))
  19470. (progn
  19471. (org-pop-to-buffer-same-window (marker-buffer marker))
  19472. (if (or (> marker (point-max)) (< marker (point-min)))
  19473. (widen))
  19474. (goto-char marker)
  19475. (org-show-context 'org-goto))
  19476. (if bookmark
  19477. (bookmark-jump bookmark)
  19478. (error "Cannot find location"))))
  19479. (defun org-quote-csv-field (s)
  19480. "Quote field for inclusion in CSV material."
  19481. (if (string-match "[\",]" s)
  19482. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19483. s))
  19484. (defun org-force-self-insert (N)
  19485. "Needed to enforce self-insert under remapping."
  19486. (interactive "p")
  19487. (self-insert-command N))
  19488. (defun org-string-width (s)
  19489. "Compute width of string, ignoring invisible characters.
  19490. This ignores character with invisibility property `org-link', and also
  19491. characters with property `org-cwidth', because these will become invisible
  19492. upon the next fontification round."
  19493. (let (b l)
  19494. (when (or (eq t buffer-invisibility-spec)
  19495. (assq 'org-link buffer-invisibility-spec))
  19496. (while (setq b (text-property-any 0 (length s)
  19497. 'invisible 'org-link s))
  19498. (setq s (concat (substring s 0 b)
  19499. (substring s (or (next-single-property-change
  19500. b 'invisible s) (length s)))))))
  19501. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19502. (setq s (concat (substring s 0 b)
  19503. (substring s (or (next-single-property-change
  19504. b 'org-cwidth s) (length s))))))
  19505. (setq l (string-width s) b -1)
  19506. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19507. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19508. l))
  19509. (defun org-shorten-string (s maxlength)
  19510. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19511. If the string is shorter or has length MAXLENGTH, just return the
  19512. original string. If it is longer, the functions finds a space in the
  19513. string, breaks this string off at that locations and adds three dots
  19514. as ellipsis. Including the ellipsis, the string will not be longer
  19515. than MAXLENGTH. If finding a good breaking point in the string does
  19516. not work, the string is just chopped off in the middle of a word
  19517. if necessary."
  19518. (if (<= (length s) maxlength)
  19519. s
  19520. (let* ((n (max (- maxlength 4) 1))
  19521. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19522. (if (string-match re s)
  19523. (concat (match-string 1 s) "...")
  19524. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19525. (defun org-get-indentation (&optional line)
  19526. "Get the indentation of the current line, interpreting tabs.
  19527. When LINE is given, assume it represents a line and compute its indentation."
  19528. (if line
  19529. (if (string-match "^ *" (org-remove-tabs line))
  19530. (match-end 0))
  19531. (save-excursion
  19532. (beginning-of-line 1)
  19533. (skip-chars-forward " \t")
  19534. (current-column))))
  19535. (defun org-get-string-indentation (s)
  19536. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19537. (let ((n -1) (i 0) (w tab-width) c)
  19538. (catch 'exit
  19539. (while (< (setq n (1+ n)) (length s))
  19540. (setq c (aref s n))
  19541. (cond ((= c ?\ ) (setq i (1+ i)))
  19542. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19543. (t (throw 'exit t)))))
  19544. i))
  19545. (defun org-remove-tabs (s &optional width)
  19546. "Replace tabulators in S with spaces.
  19547. Assumes that s is a single line, starting in column 0."
  19548. (setq width (or width tab-width))
  19549. (while (string-match "\t" s)
  19550. (setq s (replace-match
  19551. (make-string
  19552. (- (* width (/ (+ (match-beginning 0) width) width))
  19553. (match-beginning 0)) ?\ )
  19554. t t s)))
  19555. s)
  19556. (defun org-fix-indentation (line ind)
  19557. "Fix indentation in LINE.
  19558. IND is a cons cell with target and minimum indentation.
  19559. If the current indentation in LINE is smaller than the minimum,
  19560. leave it alone. If it is larger than ind, set it to the target."
  19561. (let* ((l (org-remove-tabs line))
  19562. (i (org-get-indentation l))
  19563. (i1 (car ind)) (i2 (cdr ind)))
  19564. (if (>= i i2) (setq l (substring line i2)))
  19565. (if (> i1 0)
  19566. (concat (make-string i1 ?\ ) l)
  19567. l)))
  19568. (defun org-remove-indentation (code &optional n)
  19569. "Remove the maximum common indentation from the lines in CODE.
  19570. N may optionally be the number of spaces to remove."
  19571. (with-temp-buffer
  19572. (insert code)
  19573. (org-do-remove-indentation n)
  19574. (buffer-string)))
  19575. (defun org-do-remove-indentation (&optional n)
  19576. "Remove the maximum common indentation from the buffer."
  19577. (untabify (point-min) (point-max))
  19578. (let ((min 10000) re)
  19579. (if n
  19580. (setq min n)
  19581. (goto-char (point-min))
  19582. (while (re-search-forward "^ *[^ \n]" nil t)
  19583. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19584. (unless (or (= min 0) (= min 10000))
  19585. (setq re (format "^ \\{%d\\}" min))
  19586. (goto-char (point-min))
  19587. (while (re-search-forward re nil t)
  19588. (replace-match "")
  19589. (end-of-line 1))
  19590. min)))
  19591. (defun org-fill-template (template alist)
  19592. "Find each %key of ALIST in TEMPLATE and replace it."
  19593. (let ((case-fold-search nil)
  19594. entry key value)
  19595. (setq alist (sort (copy-sequence alist)
  19596. (lambda (a b) (< (length (car a)) (length (car b))))))
  19597. (while (setq entry (pop alist))
  19598. (setq template
  19599. (replace-regexp-in-string
  19600. (concat "%" (regexp-quote (car entry)))
  19601. (or (cdr entry) "") template t t)))
  19602. template))
  19603. (defun org-base-buffer (buffer)
  19604. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19605. (if (not buffer)
  19606. buffer
  19607. (or (buffer-base-buffer buffer)
  19608. buffer)))
  19609. (defun org-trim (s)
  19610. "Remove whitespace at beginning and end of string."
  19611. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19612. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19613. s)
  19614. (defun org-wrap (string &optional width lines)
  19615. "Wrap string to either a number of lines, or a width in characters.
  19616. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19617. that costs. If there is a word longer than WIDTH, the text is actually
  19618. wrapped to the length of that word.
  19619. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19620. many lines, whatever width that takes.
  19621. The return value is a list of lines, without newlines at the end."
  19622. (let* ((words (org-split-string string "[ \t\n]+"))
  19623. (maxword (apply 'max (mapcar 'org-string-width words)))
  19624. w ll)
  19625. (cond (width
  19626. (org-do-wrap words (max maxword width)))
  19627. (lines
  19628. (setq w maxword)
  19629. (setq ll (org-do-wrap words maxword))
  19630. (if (<= (length ll) lines)
  19631. ll
  19632. (setq ll words)
  19633. (while (> (length ll) lines)
  19634. (setq w (1+ w))
  19635. (setq ll (org-do-wrap words w)))
  19636. ll))
  19637. (t (error "Cannot wrap this")))))
  19638. (defun org-do-wrap (words width)
  19639. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19640. (let (lines line)
  19641. (while words
  19642. (setq line (pop words))
  19643. (while (and words (< (+ (length line) (length (car words))) width))
  19644. (setq line (concat line " " (pop words))))
  19645. (setq lines (push line lines)))
  19646. (nreverse lines)))
  19647. (defun org-split-string (string &optional separators)
  19648. "Splits STRING into substrings at SEPARATORS.
  19649. No empty strings are returned if there are matches at the beginning
  19650. and end of string."
  19651. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19652. (start 0)
  19653. notfirst
  19654. (list nil))
  19655. (while (and (string-match rexp string
  19656. (if (and notfirst
  19657. (= start (match-beginning 0))
  19658. (< start (length string)))
  19659. (1+ start) start))
  19660. (< (match-beginning 0) (length string)))
  19661. (setq notfirst t)
  19662. (or (eq (match-beginning 0) 0)
  19663. (and (eq (match-beginning 0) (match-end 0))
  19664. (eq (match-beginning 0) start))
  19665. (setq list
  19666. (cons (substring string start (match-beginning 0))
  19667. list)))
  19668. (setq start (match-end 0)))
  19669. (or (eq start (length string))
  19670. (setq list
  19671. (cons (substring string start)
  19672. list)))
  19673. (nreverse list)))
  19674. (defun org-quote-vert (s)
  19675. "Replace \"|\" with \"\\vert\"."
  19676. (while (string-match "|" s)
  19677. (setq s (replace-match "\\vert" t t s)))
  19678. s)
  19679. (defun org-uuidgen-p (s)
  19680. "Is S an ID created by UUIDGEN?"
  19681. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19682. (defun org-in-src-block-p (&optional inside)
  19683. "Whether point is in a code source block.
  19684. When INSIDE is non-nil, don't consider we are within a src block
  19685. when point is at #+BEGIN_SRC or #+END_SRC."
  19686. (let ((case-fold-search t) ov)
  19687. (or (and (setq ov (overlays-at (point)))
  19688. (memq 'org-block-background
  19689. (overlay-properties (car ov))))
  19690. (and (not inside)
  19691. (save-match-data
  19692. (save-excursion
  19693. (beginning-of-line)
  19694. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19695. (defun org-context ()
  19696. "Return a list of contexts of the current cursor position.
  19697. If several contexts apply, all are returned.
  19698. Each context entry is a list with a symbol naming the context, and
  19699. two positions indicating start and end of the context. Possible
  19700. contexts are:
  19701. :headline anywhere in a headline
  19702. :headline-stars on the leading stars in a headline
  19703. :todo-keyword on a TODO keyword (including DONE) in a headline
  19704. :tags on the TAGS in a headline
  19705. :priority on the priority cookie in a headline
  19706. :item on the first line of a plain list item
  19707. :item-bullet on the bullet/number of a plain list item
  19708. :checkbox on the checkbox in a plain list item
  19709. :table in an org-mode table
  19710. :table-special on a special filed in a table
  19711. :table-table in a table.el table
  19712. :clocktable in a clocktable
  19713. :src-block in a source block
  19714. :link on a hyperlink
  19715. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19716. :target on a <<target>>
  19717. :radio-target on a <<<radio-target>>>
  19718. :latex-fragment on a LaTeX fragment
  19719. :latex-preview on a LaTeX fragment with overlaid preview image
  19720. This function expects the position to be visible because it uses font-lock
  19721. faces as a help to recognize the following contexts: :table-special, :link,
  19722. and :keyword."
  19723. (let* ((f (get-text-property (point) 'face))
  19724. (faces (if (listp f) f (list f)))
  19725. (case-fold-search t)
  19726. (p (point)) clist o)
  19727. ;; First the large context
  19728. (cond
  19729. ((org-at-heading-p t)
  19730. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19731. (when (progn
  19732. (beginning-of-line 1)
  19733. (looking-at org-todo-line-tags-regexp))
  19734. (push (org-point-in-group p 1 :headline-stars) clist)
  19735. (push (org-point-in-group p 2 :todo-keyword) clist)
  19736. (push (org-point-in-group p 4 :tags) clist))
  19737. (goto-char p)
  19738. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19739. (if (looking-at "\\[#[A-Z0-9]\\]")
  19740. (push (org-point-in-group p 0 :priority) clist)))
  19741. ((org-at-item-p)
  19742. (push (org-point-in-group p 2 :item-bullet) clist)
  19743. (push (list :item (point-at-bol)
  19744. (save-excursion (org-end-of-item) (point)))
  19745. clist)
  19746. (and (org-at-item-checkbox-p)
  19747. (push (org-point-in-group p 0 :checkbox) clist)))
  19748. ((org-at-table-p)
  19749. (push (list :table (org-table-begin) (org-table-end)) clist)
  19750. (if (memq 'org-formula faces)
  19751. (push (list :table-special
  19752. (previous-single-property-change p 'face)
  19753. (next-single-property-change p 'face)) clist)))
  19754. ((org-at-table-p 'any)
  19755. (push (list :table-table) clist)))
  19756. (goto-char p)
  19757. (let ((case-fold-search t))
  19758. ;; New the "medium" contexts: clocktables, source blocks
  19759. (cond ((org-in-clocktable-p)
  19760. (push (list :clocktable
  19761. (and (or (looking-at "#\\+BEGIN: clocktable")
  19762. (search-backward "#+BEGIN: clocktable" nil t))
  19763. (match-beginning 0))
  19764. (and (re-search-forward "#\\+END:?" nil t)
  19765. (match-end 0))) clist))
  19766. ((org-in-src-block-p)
  19767. (push (list :src-block
  19768. (and (or (looking-at "#\\+BEGIN_SRC")
  19769. (search-backward "#+BEGIN_SRC" nil t))
  19770. (match-beginning 0))
  19771. (and (search-forward "#+END_SRC" nil t)
  19772. (match-beginning 0))) clist))))
  19773. (goto-char p)
  19774. ;; Now the small context
  19775. (cond
  19776. ((org-at-timestamp-p)
  19777. (push (org-point-in-group p 0 :timestamp) clist))
  19778. ((memq 'org-link faces)
  19779. (push (list :link
  19780. (previous-single-property-change p 'face)
  19781. (next-single-property-change p 'face)) clist))
  19782. ((memq 'org-special-keyword faces)
  19783. (push (list :keyword
  19784. (previous-single-property-change p 'face)
  19785. (next-single-property-change p 'face)) clist))
  19786. ((org-at-target-p)
  19787. (push (org-point-in-group p 0 :target) clist)
  19788. (goto-char (1- (match-beginning 0)))
  19789. (if (looking-at org-radio-target-regexp)
  19790. (push (org-point-in-group p 0 :radio-target) clist))
  19791. (goto-char p))
  19792. ((setq o (car (delq nil
  19793. (mapcar
  19794. (lambda (x)
  19795. (if (memq x org-latex-fragment-image-overlays) x))
  19796. (overlays-at (point))))))
  19797. (push (list :latex-fragment
  19798. (overlay-start o) (overlay-end o)) clist)
  19799. (push (list :latex-preview
  19800. (overlay-start o) (overlay-end o)) clist))
  19801. ((org-inside-LaTeX-fragment-p)
  19802. ;; FIXME: positions wrong.
  19803. (push (list :latex-fragment (point) (point)) clist)))
  19804. (setq clist (nreverse (delq nil clist)))
  19805. clist))
  19806. ;; FIXME: Compare with at-regexp-p Do we need both?
  19807. (defun org-in-regexp (re &optional nlines visually)
  19808. "Check if point is inside a match of regexp.
  19809. Normally only the current line is checked, but you can include NLINES extra
  19810. lines both before and after point into the search.
  19811. If VISUALLY is set, require that the cursor is not after the match but
  19812. really on, so that the block visually is on the match."
  19813. (catch 'exit
  19814. (let ((pos (point))
  19815. (eol (point-at-eol (+ 1 (or nlines 0))))
  19816. (inc (if visually 1 0)))
  19817. (save-excursion
  19818. (beginning-of-line (- 1 (or nlines 0)))
  19819. (while (re-search-forward re eol t)
  19820. (if (and (<= (match-beginning 0) pos)
  19821. (>= (+ inc (match-end 0)) pos))
  19822. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19823. (defun org-at-regexp-p (regexp)
  19824. "Is point inside a match of REGEXP in the current line?"
  19825. (catch 'exit
  19826. (save-excursion
  19827. (let ((pos (point)) (end (point-at-eol)))
  19828. (beginning-of-line 1)
  19829. (while (re-search-forward regexp end t)
  19830. (if (and (<= (match-beginning 0) pos)
  19831. (>= (match-end 0) pos))
  19832. (throw 'exit t)))
  19833. nil))))
  19834. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19835. "Non-nil when point is between matches of START-RE and END-RE.
  19836. Also return a non-nil value when point is on one of the matches.
  19837. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19838. buffer positions. Default values are the positions of headlines
  19839. surrounding the point.
  19840. The functions returns a cons cell whose car (resp. cdr) is the
  19841. position before START-RE (resp. after END-RE)."
  19842. (save-match-data
  19843. (let ((pos (point))
  19844. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19845. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19846. beg end)
  19847. (save-excursion
  19848. ;; Point is on a block when on START-RE or if START-RE can be
  19849. ;; found before it...
  19850. (and (or (org-at-regexp-p start-re)
  19851. (re-search-backward start-re limit-up t))
  19852. (setq beg (match-beginning 0))
  19853. ;; ... and END-RE after it...
  19854. (goto-char (match-end 0))
  19855. (re-search-forward end-re limit-down t)
  19856. (> (setq end (match-end 0)) pos)
  19857. ;; ... without another START-RE in-between.
  19858. (goto-char (match-beginning 0))
  19859. (not (re-search-backward start-re (1+ beg) t))
  19860. ;; Return value.
  19861. (cons beg end))))))
  19862. (defun org-in-block-p (names)
  19863. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19864. NAMES is a list of strings containing names of blocks.
  19865. Return first block name matched, or nil. Beware that in case of
  19866. nested blocks, the returned name may not belong to the closest
  19867. block from point."
  19868. (save-match-data
  19869. (catch 'exit
  19870. (let ((case-fold-search t)
  19871. (lim-up (save-excursion (outline-previous-heading)))
  19872. (lim-down (save-excursion (outline-next-heading))))
  19873. (mapc (lambda (name)
  19874. (let ((n (regexp-quote name)))
  19875. (when (org-between-regexps-p
  19876. (concat "^[ \t]*#\\+begin_" n)
  19877. (concat "^[ \t]*#\\+end_" n)
  19878. lim-up lim-down)
  19879. (throw 'exit n))))
  19880. names))
  19881. nil)))
  19882. (defun org-in-drawer-p ()
  19883. "Is point within a drawer?"
  19884. (save-match-data
  19885. (let ((case-fold-search t)
  19886. (lim-up (save-excursion (outline-previous-heading)))
  19887. (lim-down (save-excursion (outline-next-heading))))
  19888. (org-between-regexps-p
  19889. (concat "^[ \t]*:" (regexp-opt org-drawers) ":")
  19890. "^[ \t]*:end:.*$"
  19891. lim-up lim-down))))
  19892. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19893. "Call `multi-occur' with buffers for all agenda files."
  19894. (interactive "sOrg-files matching: \np")
  19895. (let* ((files (org-agenda-files))
  19896. (tnames (mapcar 'file-truename files))
  19897. (extra org-agenda-text-search-extra-files)
  19898. f)
  19899. (when (eq (car extra) 'agenda-archives)
  19900. (setq extra (cdr extra))
  19901. (setq files (org-add-archive-files files)))
  19902. (while (setq f (pop extra))
  19903. (unless (member (file-truename f) tnames)
  19904. (add-to-list 'files f 'append)
  19905. (add-to-list 'tnames (file-truename f) 'append)))
  19906. (multi-occur
  19907. (mapcar (lambda (x)
  19908. (with-current-buffer
  19909. (or (get-file-buffer x) (find-file-noselect x))
  19910. (widen)
  19911. (current-buffer)))
  19912. files)
  19913. regexp)))
  19914. (if (boundp 'occur-mode-find-occurrence-hook)
  19915. ;; Emacs 23
  19916. (add-hook 'occur-mode-find-occurrence-hook
  19917. (lambda ()
  19918. (when (derived-mode-p 'org-mode)
  19919. (org-reveal))))
  19920. ;; Emacs 22
  19921. (defadvice occur-mode-goto-occurrence
  19922. (after org-occur-reveal activate)
  19923. (and (derived-mode-p 'org-mode) (org-reveal)))
  19924. (defadvice occur-mode-goto-occurrence-other-window
  19925. (after org-occur-reveal activate)
  19926. (and (derived-mode-p 'org-mode) (org-reveal)))
  19927. (defadvice occur-mode-display-occurrence
  19928. (after org-occur-reveal activate)
  19929. (when (derived-mode-p 'org-mode)
  19930. (let ((pos (occur-mode-find-occurrence)))
  19931. (with-current-buffer (marker-buffer pos)
  19932. (save-excursion
  19933. (goto-char pos)
  19934. (org-reveal)))))))
  19935. (defun org-occur-link-in-agenda-files ()
  19936. "Create a link and search for it in the agendas.
  19937. The link is not stored in `org-stored-links', it is just created
  19938. for the search purpose."
  19939. (interactive)
  19940. (let ((link (condition-case nil
  19941. (org-store-link nil)
  19942. (error "Unable to create a link to here"))))
  19943. (org-occur-in-agenda-files (regexp-quote link))))
  19944. (defun org-reverse-string (string)
  19945. "Return the reverse of STRING."
  19946. (apply 'string (reverse (string-to-list string))))
  19947. (defsubst org-uniquify (list)
  19948. "Non-destructively remove duplicate elements from LIST."
  19949. (let ((res (copy-sequence list))) (delete-dups res)))
  19950. (defun org-uniquify-alist (alist)
  19951. "Merge elements of ALIST with the same key.
  19952. For example, in this alist:
  19953. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  19954. => '((a 1 3) (b 2))
  19955. merge (a 1) and (a 3) into (a 1 3).
  19956. The function returns the new ALIST."
  19957. (let (rtn)
  19958. (mapc
  19959. (lambda (e)
  19960. (let (n)
  19961. (if (not (assoc (car e) rtn))
  19962. (push e rtn)
  19963. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  19964. (setq rtn (assq-delete-all (car e) rtn))
  19965. (push n rtn))))
  19966. alist)
  19967. rtn))
  19968. (defun org-delete-all (elts list)
  19969. "Remove all elements in ELTS from LIST."
  19970. (while elts
  19971. (setq list (delete (pop elts) list)))
  19972. list)
  19973. (defun org-count (cl-item cl-seq)
  19974. "Count the number of occurrences of ITEM in SEQ.
  19975. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19976. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19977. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19978. (while (< cl-start cl-end)
  19979. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19980. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19981. (setq cl-start (1+ cl-start)))
  19982. cl-count))
  19983. (defun org-remove-if (predicate seq)
  19984. "Remove everything from SEQ that fulfills PREDICATE."
  19985. (let (res e)
  19986. (while seq
  19987. (setq e (pop seq))
  19988. (if (not (funcall predicate e)) (push e res)))
  19989. (nreverse res)))
  19990. (defun org-remove-if-not (predicate seq)
  19991. "Remove everything from SEQ that does not fulfill PREDICATE."
  19992. (let (res e)
  19993. (while seq
  19994. (setq e (pop seq))
  19995. (if (funcall predicate e) (push e res)))
  19996. (nreverse res)))
  19997. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19998. "Reduce two-argument FUNCTION across SEQ.
  19999. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20000. \":initial-value\" removed."
  20001. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20002. (cadr (memq :initial-value cl-keys)))
  20003. (cl-seq (pop cl-seq))
  20004. (t (funcall cl-func)))))
  20005. (while cl-seq
  20006. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20007. cl-accum))
  20008. (defun org-every (pred seq)
  20009. "Return true if PREDICATE is true of every element of SEQ.
  20010. Adapted from `every' in cl.el."
  20011. (catch 'org-every
  20012. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20013. t))
  20014. (defun org-some (pred seq)
  20015. "Return true if PREDICATE is true of any element of SEQ.
  20016. Adapted from `some' in cl.el."
  20017. (catch 'org-some
  20018. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20019. nil))
  20020. (defun org-back-over-empty-lines ()
  20021. "Move backwards over whitespace, to the beginning of the first empty line.
  20022. Returns the number of empty lines passed."
  20023. (let ((pos (point)))
  20024. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20025. (skip-chars-backward " \t\n\r")
  20026. (unless (eobp)
  20027. (forward-line -1)))
  20028. (beginning-of-line 2)
  20029. (goto-char (min (point) pos))
  20030. (count-lines (point) pos)))
  20031. (defun org-skip-whitespace ()
  20032. (skip-chars-forward " \t\n\r"))
  20033. (defun org-point-in-group (point group &optional context)
  20034. "Check if POINT is in match-group GROUP.
  20035. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20036. match. If the match group does not exist or point is not inside it,
  20037. return nil."
  20038. (and (match-beginning group)
  20039. (>= point (match-beginning group))
  20040. (<= point (match-end group))
  20041. (if context
  20042. (list context (match-beginning group) (match-end group))
  20043. t)))
  20044. (defun org-switch-to-buffer-other-window (&rest args)
  20045. "Switch to buffer in a second window on the current frame.
  20046. In particular, do not allow pop-up frames.
  20047. Returns the newly created buffer."
  20048. (org-no-popups
  20049. (apply 'switch-to-buffer-other-window args)))
  20050. (defun org-combine-plists (&rest plists)
  20051. "Create a single property list from all plists in PLISTS.
  20052. The process starts by copying the first list, and then setting properties
  20053. from the other lists. Settings in the last list are the most significant
  20054. ones and overrule settings in the other lists."
  20055. (let ((rtn (copy-sequence (pop plists)))
  20056. p v ls)
  20057. (while plists
  20058. (setq ls (pop plists))
  20059. (while ls
  20060. (setq p (pop ls) v (pop ls))
  20061. (setq rtn (plist-put rtn p v))))
  20062. rtn))
  20063. (defun org-replace-escapes (string table)
  20064. "Replace %-escapes in STRING with values in TABLE.
  20065. TABLE is an association list with keys like \"%a\" and string values.
  20066. The sequences in STRING may contain normal field width and padding information,
  20067. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20068. so values can contain further %-escapes if they are define later in TABLE."
  20069. (let ((tbl (copy-alist table))
  20070. (case-fold-search nil)
  20071. (pchg 0)
  20072. e re rpl)
  20073. (while (setq e (pop tbl))
  20074. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20075. (when (and (cdr e) (string-match re (cdr e)))
  20076. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20077. (safe "SREF"))
  20078. (add-text-properties 0 3 (list 'sref sref) safe)
  20079. (setcdr e (replace-match safe t t (cdr e)))))
  20080. (while (string-match re string)
  20081. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20082. (cdr e)))
  20083. (setq string (replace-match rpl t t string))))
  20084. (while (setq pchg (next-property-change pchg string))
  20085. (let ((sref (get-text-property pchg 'sref string)))
  20086. (when (and sref (string-match "SREF" string pchg))
  20087. (setq string (replace-match sref t t string)))))
  20088. string))
  20089. (defun org-sublist (list start end)
  20090. "Return a section of LIST, from START to END.
  20091. Counting starts at 1."
  20092. (let (rtn (c start))
  20093. (setq list (nthcdr (1- start) list))
  20094. (while (and list (<= c end))
  20095. (push (pop list) rtn)
  20096. (setq c (1+ c)))
  20097. (nreverse rtn)))
  20098. (defun org-find-base-buffer-visiting (file)
  20099. "Like `find-buffer-visiting' but always return the base buffer and
  20100. not an indirect buffer."
  20101. (let ((buf (or (get-file-buffer file)
  20102. (find-buffer-visiting file))))
  20103. (if buf
  20104. (or (buffer-base-buffer buf) buf)
  20105. nil)))
  20106. (defun org-image-file-name-regexp (&optional extensions)
  20107. "Return regexp matching the file names of images.
  20108. If EXTENSIONS is given, only match these."
  20109. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20110. (image-file-name-regexp)
  20111. (let ((image-file-name-extensions
  20112. (or extensions
  20113. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20114. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20115. (concat "\\."
  20116. (regexp-opt (nconc (mapcar 'upcase
  20117. image-file-name-extensions)
  20118. image-file-name-extensions)
  20119. t)
  20120. "\\'"))))
  20121. (defun org-file-image-p (file &optional extensions)
  20122. "Return non-nil if FILE is an image."
  20123. (save-match-data
  20124. (string-match (org-image-file-name-regexp extensions) file)))
  20125. (defun org-get-cursor-date (&optional with-time)
  20126. "Return the date at cursor in as a time.
  20127. This works in the calendar and in the agenda, anywhere else it just
  20128. returns the current time.
  20129. If WITH-TIME is non-nil, returns the time of the event at point (in
  20130. the agenda) or the current time of the day."
  20131. (let (date day defd tp tm hod mod)
  20132. (when with-time
  20133. (setq tp (get-text-property (point) 'time))
  20134. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20135. (setq hod (string-to-number (match-string 1 tp))
  20136. mod (string-to-number (match-string 2 tp))))
  20137. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20138. mod (nth 1 (decode-time (current-time))))))
  20139. (cond
  20140. ((eq major-mode 'calendar-mode)
  20141. (setq date (calendar-cursor-to-date)
  20142. defd (encode-time 0 (or mod 0) (or hod 0)
  20143. (nth 1 date) (nth 0 date) (nth 2 date))))
  20144. ((eq major-mode 'org-agenda-mode)
  20145. (setq day (get-text-property (point) 'day))
  20146. (if day
  20147. (setq date (calendar-gregorian-from-absolute day)
  20148. defd (encode-time 0 (or mod 0) (or hod 0)
  20149. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20150. (or defd (current-time))))
  20151. (defun org-mark-subtree (&optional up)
  20152. "Mark the current subtree.
  20153. This puts point at the start of the current subtree, and mark at
  20154. the end. If a numeric prefix UP is given, move up into the
  20155. hierarchy of headlines by UP levels before marking the subtree."
  20156. (interactive "P")
  20157. (org-with-limited-levels
  20158. (cond ((org-at-heading-p) (beginning-of-line))
  20159. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20160. (t (outline-previous-visible-heading 1))))
  20161. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20162. (if (org-called-interactively-p 'any)
  20163. (call-interactively 'org-mark-element)
  20164. (org-mark-element)))
  20165. ;;; Indentation
  20166. (defun org-indent-line ()
  20167. "Indent line depending on context."
  20168. (interactive)
  20169. (let* ((pos (point))
  20170. (itemp (org-at-item-p))
  20171. (case-fold-search t)
  20172. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20173. (inline-task-p (and (featurep 'org-inlinetask)
  20174. (org-inlinetask-in-task-p)))
  20175. (inline-re (and inline-task-p
  20176. (org-inlinetask-outline-regexp)))
  20177. column)
  20178. (if (and orgstruct-is-++ (eq pos (point)))
  20179. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20180. (indent-according-to-mode))
  20181. (beginning-of-line 1)
  20182. (cond
  20183. ;; Headings
  20184. ((looking-at org-outline-regexp) (setq column 0))
  20185. ;; Footnote definition
  20186. ((looking-at org-footnote-definition-re) (setq column 0))
  20187. ;; Literal examples
  20188. ((looking-at "[ \t]*:\\( \\|$\\)")
  20189. (setq column (org-get-indentation))) ; do nothing
  20190. ;; Lists
  20191. ((ignore-errors (goto-char (org-in-item-p)))
  20192. (setq column (if itemp
  20193. (org-get-indentation)
  20194. (org-list-item-body-column (point))))
  20195. (goto-char pos))
  20196. ;; Drawers
  20197. ((and (looking-at "[ \t]*:END:")
  20198. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20199. (save-excursion
  20200. (goto-char (1- (match-beginning 1)))
  20201. (setq column (current-column))))
  20202. ;; Special blocks
  20203. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20204. (save-excursion
  20205. (re-search-backward
  20206. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20207. (setq column (org-get-indentation (match-string 0))))
  20208. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20209. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20210. (save-excursion
  20211. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20212. (setq column
  20213. (cond ((equal (downcase (match-string 1)) "src")
  20214. ;; src blocks: let `org-edit-src-exit' handle them
  20215. (org-get-indentation))
  20216. ((equal (downcase (match-string 1)) "example")
  20217. (max (org-get-indentation)
  20218. (org-get-indentation (match-string 0))))
  20219. (t
  20220. (org-get-indentation (match-string 0))))))
  20221. ;; This line has nothing special, look at the previous relevant
  20222. ;; line to compute indentation
  20223. (t
  20224. (beginning-of-line 0)
  20225. (while (and (not (bobp))
  20226. (not (looking-at org-table-line-regexp))
  20227. (not (looking-at org-drawer-regexp))
  20228. ;; When point started in an inline task, do not move
  20229. ;; above task starting line.
  20230. (not (and inline-task-p (looking-at inline-re)))
  20231. ;; Skip drawers, blocks, empty lines, verbatim,
  20232. ;; comments, tables, footnotes definitions, lists,
  20233. ;; inline tasks.
  20234. (or (and (looking-at "[ \t]*:END:")
  20235. (re-search-backward org-drawer-regexp nil t))
  20236. (and (looking-at "[ \t]*#\\+end_")
  20237. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20238. (looking-at "[ \t]*[\n:#|]")
  20239. (looking-at org-footnote-definition-re)
  20240. (and (not inline-task-p)
  20241. (featurep 'org-inlinetask)
  20242. (org-inlinetask-in-task-p)
  20243. (or (org-inlinetask-goto-beginning) t))))
  20244. (beginning-of-line 0))
  20245. (cond
  20246. ;; There was a list item above.
  20247. ((save-excursion
  20248. (and (ignore-errors (goto-char (org-in-item-p)))
  20249. (goto-char
  20250. (org-list-get-top-point (org-list-struct)))))
  20251. (looking-at org-list-full-item-re)
  20252. (setq column (length (match-string 0))))
  20253. ;; There was an heading above.
  20254. ((looking-at "\\*+[ \t]+")
  20255. (if (not org-adapt-indentation)
  20256. (setq column 0)
  20257. (goto-char (match-end 0))
  20258. (setq column (current-column))))
  20259. ;; A drawer had started and is unfinished
  20260. ((looking-at org-drawer-regexp)
  20261. (goto-char (1- (match-beginning 1)))
  20262. (setq column (current-column)))
  20263. ;; Else, nothing noticeable found: get indentation and go on.
  20264. (t (setq column (org-get-indentation))))))
  20265. ;; Now apply indentation and move cursor accordingly
  20266. (goto-char pos)
  20267. (if (<= (current-column) (current-indentation))
  20268. (org-indent-line-to column)
  20269. (save-excursion (org-indent-line-to column)))
  20270. ;; Special polishing for properties, see `org-property-format'
  20271. (setq column (current-column))
  20272. (beginning-of-line 1)
  20273. (if (looking-at org-property-re)
  20274. (replace-match (concat (match-string 4)
  20275. (format org-property-format
  20276. (match-string 1) (match-string 3)))
  20277. t t))
  20278. (org-move-to-column column))))
  20279. (defun org-indent-drawer ()
  20280. "Indent the drawer at point."
  20281. (interactive)
  20282. (let ((p (point))
  20283. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20284. (match-end 0)))
  20285. (folded
  20286. (save-excursion
  20287. (end-of-line)
  20288. (when (overlays-at (point))
  20289. (member 'invisible (overlay-properties
  20290. (car (overlays-at (point)))))))))
  20291. (when folded (org-cycle))
  20292. (indent-for-tab-command)
  20293. (while (and (move-beginning-of-line 2) (< (point) e))
  20294. (indent-for-tab-command))
  20295. (goto-char p)
  20296. (when folded (org-cycle)))
  20297. (message "Drawer at point indented"))
  20298. (defun org-indent-block ()
  20299. "Indent the block at point."
  20300. (interactive)
  20301. (let ((p (point))
  20302. (case-fold-search t)
  20303. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20304. (match-end 0)))
  20305. (folded
  20306. (save-excursion
  20307. (end-of-line)
  20308. (when (overlays-at (point))
  20309. (member 'invisible (overlay-properties
  20310. (car (overlays-at (point)))))))))
  20311. (when folded (org-cycle))
  20312. (indent-for-tab-command)
  20313. (while (and (move-beginning-of-line 2) (< (point) e))
  20314. (indent-for-tab-command))
  20315. (goto-char p)
  20316. (when folded (org-cycle)))
  20317. (message "Block at point indented"))
  20318. (defun org-indent-region (start end)
  20319. "Indent region."
  20320. (interactive "r")
  20321. (save-excursion
  20322. (let ((line-end (org-current-line end)))
  20323. (goto-char start)
  20324. (while (< (org-current-line) line-end)
  20325. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  20326. (t (call-interactively 'org-indent-line)))
  20327. (move-beginning-of-line 2)))))
  20328. ;;; Filling
  20329. ;; We use our own fill-paragraph and auto-fill functions.
  20330. ;; `org-fill-paragraph' relies on adaptive filling and context
  20331. ;; checking. Appropriate `fill-prefix' is computed with
  20332. ;; `org-adaptive-fill-function'.
  20333. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20334. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20335. ;; `org-adaptive-fill-function' value. Internally,
  20336. ;; `org-comment-line-break-function' breaks the line.
  20337. ;; `org-setup-filling' installs filling and auto-filling related
  20338. ;; variables during `org-mode' initialization.
  20339. (defvar org-element-paragraph-separate) ; org-element.el
  20340. (defun org-setup-filling ()
  20341. (require 'org-element)
  20342. ;; Prevent auto-fill from inserting unwanted new items.
  20343. (when (boundp 'fill-nobreak-predicate)
  20344. (org-set-local
  20345. 'fill-nobreak-predicate
  20346. (org-uniquify
  20347. (append fill-nobreak-predicate
  20348. '(org-fill-line-break-nobreak-p
  20349. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20350. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  20351. (org-set-local 'paragraph-start paragraph-ending)
  20352. (org-set-local 'paragraph-separate paragraph-ending))
  20353. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20354. (org-set-local 'auto-fill-inhibit-regexp nil)
  20355. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20356. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20357. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20358. (defun org-fill-line-break-nobreak-p ()
  20359. "Non-nil when a new line at point would create an Org line break."
  20360. (save-excursion
  20361. (skip-chars-backward "[ \t]")
  20362. (skip-chars-backward "\\\\")
  20363. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20364. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20365. "Non-nil when a new line at point would split a timestamp."
  20366. (and (org-at-timestamp-p t)
  20367. (not (looking-at org-ts-regexp-both))))
  20368. (declare-function message-in-body-p "message" ())
  20369. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20370. (defun org-adaptive-fill-function ()
  20371. "Compute a fill prefix for the current line.
  20372. Return fill prefix, as a string, or nil if current line isn't
  20373. meant to be filled."
  20374. (let (prefix)
  20375. (catch 'exit
  20376. (when (derived-mode-p 'message-mode)
  20377. (save-excursion
  20378. (beginning-of-line)
  20379. (cond ((or (not (message-in-body-p))
  20380. (looking-at orgtbl-line-start-regexp))
  20381. (throw 'exit nil))
  20382. ((looking-at message-cite-prefix-regexp)
  20383. (throw 'exit (match-string-no-properties 0)))
  20384. ((looking-at org-outline-regexp)
  20385. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20386. (org-with-wide-buffer
  20387. (let* ((p (line-beginning-position))
  20388. (element (save-excursion
  20389. (beginning-of-line)
  20390. (or (ignore-errors (org-element-at-point))
  20391. (user-error "An element cannot be parsed line %d"
  20392. (line-number-at-pos (point))))))
  20393. (type (org-element-type element))
  20394. (post-affiliated (org-element-property :post-affiliated element)))
  20395. (unless (and post-affiliated (< p post-affiliated))
  20396. (case type
  20397. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20398. (footnote-definition "")
  20399. ((item plain-list)
  20400. (make-string (org-list-item-body-column
  20401. (or post-affiliated
  20402. (org-element-property :begin element)))
  20403. ? ))
  20404. (paragraph
  20405. ;; Fill prefix is usually the same as the current line,
  20406. ;; except if the paragraph is at the beginning of an item.
  20407. (let ((parent (org-element-property :parent element)))
  20408. (cond ((eq (org-element-type parent) 'item)
  20409. (make-string (org-list-item-body-column
  20410. (org-element-property :begin parent))
  20411. ? ))
  20412. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20413. (match-string 0))
  20414. (t ""))))
  20415. (comment-block
  20416. ;; Only fill contents if P is within block boundaries.
  20417. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20418. (forward-line)
  20419. (point)))
  20420. (cend (save-excursion
  20421. (goto-char (org-element-property :end element))
  20422. (skip-chars-backward " \r\t\n")
  20423. (line-beginning-position))))
  20424. (when (and (>= p cbeg) (< p cend))
  20425. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20426. (match-string 0)
  20427. "")))))))))))
  20428. (declare-function message-goto-body "message" ())
  20429. (defvar message-cite-prefix-regexp) ; From message.el
  20430. (defun org-fill-paragraph (&optional justify)
  20431. "Fill element at point, when applicable.
  20432. This function only applies to comment blocks, comments, example
  20433. blocks and paragraphs. Also, as a special case, re-align table
  20434. when point is at one.
  20435. If JUSTIFY is non-nil (interactively, with prefix argument),
  20436. justify as well. If `sentence-end-double-space' is non-nil, then
  20437. period followed by one space does not end a sentence, so don't
  20438. break a line there. The variable `fill-column' controls the
  20439. width for filling.
  20440. For convenience, when point is at a plain list, an item or
  20441. a footnote definition, try to fill the first paragraph within."
  20442. (interactive)
  20443. (if (and (derived-mode-p 'message-mode)
  20444. (or (not (message-in-body-p))
  20445. (save-excursion (move-beginning-of-line 1)
  20446. (looking-at message-cite-prefix-regexp))))
  20447. ;; First ensure filling is correct in message-mode.
  20448. (let ((fill-paragraph-function
  20449. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20450. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20451. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20452. (paragraph-separate
  20453. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20454. (fill-paragraph nil))
  20455. (with-syntax-table org-mode-transpose-word-syntax-table
  20456. ;; Move to end of line in order to get the first paragraph
  20457. ;; within a plain list or a footnote definition.
  20458. (let ((element (save-excursion
  20459. (end-of-line)
  20460. (or (ignore-errors (org-element-at-point))
  20461. (user-error "An element cannot be parsed line %d"
  20462. (line-number-at-pos (point)))))))
  20463. ;; First check if point is in a blank line at the beginning of
  20464. ;; the buffer. In that case, ignore filling.
  20465. (case (org-element-type element)
  20466. ;; Use major mode filling function is src blocks.
  20467. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20468. ;; Align Org tables, leave table.el tables as-is.
  20469. (table-row (org-table-align) t)
  20470. (table
  20471. (when (eq (org-element-property :type element) 'org)
  20472. (save-excursion
  20473. (goto-char (org-element-property :post-affiliated element))
  20474. (org-table-align)))
  20475. t)
  20476. (paragraph
  20477. ;; Paragraphs may contain `line-break' type objects.
  20478. (let ((beg (max (point-min)
  20479. (org-element-property :contents-begin element)))
  20480. (end (min (point-max)
  20481. (org-element-property :contents-end element))))
  20482. ;; Do nothing if point is at an affiliated keyword.
  20483. (if (< (line-end-position) beg) t
  20484. (when (derived-mode-p 'message-mode)
  20485. ;; In `message-mode', do not fill following citation
  20486. ;; in current paragraph nor text before message body.
  20487. (let ((body-start (save-excursion (message-goto-body))))
  20488. (when body-start (setq beg (max body-start beg))))
  20489. (when (save-excursion
  20490. (re-search-forward
  20491. (concat "^" message-cite-prefix-regexp) end t))
  20492. (setq end (match-beginning 0))))
  20493. ;; Fill paragraph, taking line breaks into account.
  20494. ;; For that, slice the paragraph using line breaks as
  20495. ;; separators, and fill the parts in reverse order to
  20496. ;; avoid messing with markers.
  20497. (save-excursion
  20498. (goto-char end)
  20499. (mapc
  20500. (lambda (pos)
  20501. (fill-region-as-paragraph pos (point) justify)
  20502. (goto-char pos))
  20503. ;; Find the list of ending positions for line breaks
  20504. ;; in the current paragraph. Add paragraph
  20505. ;; beginning to include first slice.
  20506. (nreverse
  20507. (cons beg
  20508. (org-element-map
  20509. (org-element--parse-objects
  20510. beg end nil (org-element-restriction 'paragraph))
  20511. 'line-break
  20512. (lambda (lb) (org-element-property :end lb)))))))
  20513. t)))
  20514. ;; Contents of `comment-block' type elements should be
  20515. ;; filled as plain text, but only if point is within block
  20516. ;; markers.
  20517. (comment-block
  20518. (let* ((case-fold-search t)
  20519. (beg (save-excursion
  20520. (goto-char (org-element-property :begin element))
  20521. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20522. (forward-line)
  20523. (point)))
  20524. (end (save-excursion
  20525. (goto-char (org-element-property :end element))
  20526. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20527. (line-beginning-position))))
  20528. (when (and (>= (point) beg) (< (point) end))
  20529. (fill-region-as-paragraph
  20530. (save-excursion
  20531. (end-of-line)
  20532. (re-search-backward "^[ \t]*$" beg 'move)
  20533. (line-beginning-position))
  20534. (save-excursion
  20535. (beginning-of-line)
  20536. (re-search-forward "^[ \t]*$" end 'move)
  20537. (line-beginning-position))
  20538. justify)))
  20539. t)
  20540. ;; Fill comments.
  20541. (comment (fill-comment-paragraph justify))
  20542. ;; Ignore every other element.
  20543. (otherwise t))))))
  20544. (defun org-auto-fill-function ()
  20545. "Auto-fill function."
  20546. ;; Check if auto-filling is meaningful.
  20547. (let ((fc (current-fill-column)))
  20548. (when (and fc (> (current-column) fc))
  20549. (let* ((fill-prefix (org-adaptive-fill-function))
  20550. ;; Enforce empty fill prefix, if required. Otherwise, it
  20551. ;; will be computed again.
  20552. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20553. (when fill-prefix (do-auto-fill))))))
  20554. (defun org-comment-line-break-function (&optional soft)
  20555. "Break line at point and indent, continuing comment if within one.
  20556. The inserted newline is marked hard if variable
  20557. `use-hard-newlines' is true, unless optional argument SOFT is
  20558. non-nil."
  20559. (if soft (insert-and-inherit ?\n) (newline 1))
  20560. (save-excursion (forward-char -1) (delete-horizontal-space))
  20561. (delete-horizontal-space)
  20562. (indent-to-left-margin)
  20563. (insert-before-markers-and-inherit fill-prefix))
  20564. ;;; Comments
  20565. ;; Org comments syntax is quite complex. It requires the entire line
  20566. ;; to be just a comment. Also, even with the right syntax at the
  20567. ;; beginning of line, some some elements (i.e. verse-block or
  20568. ;; example-block) don't accept comments. Usual Emacs comment commands
  20569. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20570. ;; Org still relies on `comment-dwim', but cannot trust
  20571. ;; `comment-only-p'. So, `comment-region-function' and
  20572. ;; `uncomment-region-function' both point
  20573. ;; to`org-comment-or-uncomment-region'. Eventually,
  20574. ;; `org-insert-comment' takes care of insertion of comments at the
  20575. ;; beginning of line.
  20576. ;; `org-setup-comments-handling' install comments related variables
  20577. ;; during `org-mode' initialization.
  20578. (defun org-setup-comments-handling ()
  20579. (interactive)
  20580. (org-set-local 'comment-use-syntax nil)
  20581. (org-set-local 'comment-start "# ")
  20582. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20583. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20584. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20585. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20586. (defun org-insert-comment ()
  20587. "Insert an empty comment above current line.
  20588. If the line is empty, insert comment at its beginning."
  20589. (beginning-of-line)
  20590. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20591. (org-indent-line)
  20592. (insert "# "))
  20593. (defvar comment-empty-lines) ; From newcomment.el.
  20594. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20595. "Comment or uncomment each non-blank line in the region.
  20596. Uncomment each non-blank line between BEG and END if it only
  20597. contains commented lines. Otherwise, comment them."
  20598. (save-restriction
  20599. ;; Restrict region
  20600. (narrow-to-region (save-excursion (goto-char beg)
  20601. (skip-chars-forward " \r\t\n" end)
  20602. (line-beginning-position))
  20603. (save-excursion (goto-char end)
  20604. (skip-chars-backward " \r\t\n" beg)
  20605. (line-end-position)))
  20606. (let ((uncommentp
  20607. ;; UNCOMMENTP is non-nil when every non blank line between
  20608. ;; BEG and END is a comment.
  20609. (save-excursion
  20610. (goto-char (point-min))
  20611. (while (and (not (eobp))
  20612. (let ((element (org-element-at-point)))
  20613. (and (eq (org-element-type element) 'comment)
  20614. (goto-char (min (point-max)
  20615. (org-element-property
  20616. :end element)))))))
  20617. (eobp))))
  20618. (if uncommentp
  20619. ;; Only blank lines and comments in region: uncomment it.
  20620. (save-excursion
  20621. (goto-char (point-min))
  20622. (while (not (eobp))
  20623. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20624. (replace-match "" nil nil nil 1))
  20625. (forward-line)))
  20626. ;; Comment each line in region.
  20627. (let ((min-indent (point-max)))
  20628. ;; First find the minimum indentation across all lines.
  20629. (save-excursion
  20630. (goto-char (point-min))
  20631. (while (and (not (eobp)) (not (zerop min-indent)))
  20632. (unless (looking-at "[ \t]*$")
  20633. (setq min-indent (min min-indent (current-indentation))))
  20634. (forward-line)))
  20635. ;; Then loop over all lines.
  20636. (save-excursion
  20637. (goto-char (point-min))
  20638. (while (not (eobp))
  20639. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20640. ;; Don't get fooled by invisible text (e.g. link path)
  20641. ;; when moving to column MIN-INDENT.
  20642. (let ((buffer-invisibility-spec nil))
  20643. (org-move-to-column min-indent t))
  20644. (insert comment-start))
  20645. (forward-line))))))))
  20646. ;;; Planning
  20647. ;; This section contains tools to operate on timestamp objects, as
  20648. ;; returned by, e.g. `org-element-context'.
  20649. (defun org-timestamp-has-time-p (timestamp)
  20650. "Non-nil when TIMESTAMP has a time specified."
  20651. (org-element-property :hour-start timestamp))
  20652. (defun org-timestamp-format (timestamp format &optional end utc)
  20653. "Format a TIMESTAMP element into a string.
  20654. FORMAT is a format specifier to be passed to
  20655. `format-time-string'.
  20656. When optional argument END is non-nil, use end of date-range or
  20657. time-range, if possible.
  20658. When optional argument UTC is non-nil, time will be expressed as
  20659. Universal Time."
  20660. (format-time-string
  20661. format
  20662. (apply 'encode-time
  20663. (cons 0
  20664. (mapcar
  20665. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20666. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20667. '(:minute-start :hour-start :day-start :month-start
  20668. :year-start)))))
  20669. utc))
  20670. (defun org-timestamp-split-range (timestamp &optional end)
  20671. "Extract a timestamp object from a date or time range.
  20672. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20673. the end of the range. Otherwise, extract its start.
  20674. Return a new timestamp object sharing the same parent as
  20675. TIMESTAMP."
  20676. (let ((type (org-element-property :type timestamp)))
  20677. (if (memq type '(active inactive diary)) timestamp
  20678. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20679. ;; Set new type.
  20680. (org-element-put-property
  20681. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20682. ;; Copy start properties over end properties if END is
  20683. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20684. (let ((p-alist '((:minute-start . :minute-end)
  20685. (:hour-start . :hour-end)
  20686. (:day-start . :day-end)
  20687. (:month-start . :month-end)
  20688. (:year-start . :year-end))))
  20689. (dolist (p-cell p-alist)
  20690. (org-element-put-property
  20691. split-ts
  20692. (funcall (if end 'car 'cdr) p-cell)
  20693. (org-element-property
  20694. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20695. ;; Eventually refresh `:raw-value'.
  20696. (org-element-put-property split-ts :raw-value nil)
  20697. (org-element-put-property
  20698. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20699. (defun org-timestamp-translate (timestamp &optional boundary)
  20700. "Apply `org-translate-time' on a TIMESTAMP object.
  20701. When optional argument BOUNDARY is non-nil, it is either the
  20702. symbol `start' or `end'. In this case, only translate the
  20703. starting or ending part of TIMESTAMP if it is a date or time
  20704. range. Otherwise, translate both parts."
  20705. (if (and (not boundary)
  20706. (memq (org-element-property :type timestamp)
  20707. '(active-range inactive-range)))
  20708. (concat
  20709. (org-translate-time
  20710. (org-element-property :raw-value
  20711. (org-timestamp-split-range timestamp)))
  20712. "--"
  20713. (org-translate-time
  20714. (org-element-property :raw-value
  20715. (org-timestamp-split-range timestamp t))))
  20716. (org-translate-time
  20717. (org-element-property
  20718. :raw-value
  20719. (if (not boundary) timestamp
  20720. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20721. ;;; Other stuff.
  20722. (defun org-toggle-fixed-width-section (arg)
  20723. "Toggle the fixed-width export.
  20724. If there is no active region, the QUOTE keyword at the current headline is
  20725. inserted or removed. When present, it causes the text between this headline
  20726. and the next to be exported as fixed-width text, and unmodified.
  20727. If there is an active region, this command adds or removes a colon as the
  20728. first character of this line. If the first character of a line is a colon,
  20729. this line is also exported in fixed-width font."
  20730. (interactive "P")
  20731. (let* ((cc 0)
  20732. (regionp (org-region-active-p))
  20733. (beg (if regionp (region-beginning) (point)))
  20734. (end (if regionp (region-end)))
  20735. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20736. (case-fold-search nil)
  20737. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20738. off)
  20739. (if regionp
  20740. (save-excursion
  20741. (goto-char beg)
  20742. (setq cc (current-column))
  20743. (beginning-of-line 1)
  20744. (setq off (looking-at re))
  20745. (while (> nlines 0)
  20746. (setq nlines (1- nlines))
  20747. (beginning-of-line 1)
  20748. (cond
  20749. (arg
  20750. (org-move-to-column cc t)
  20751. (insert ": \n")
  20752. (forward-line -1))
  20753. ((and off (looking-at re))
  20754. (replace-match "" t t nil 1))
  20755. ((not off) (org-move-to-column cc t) (insert ": ")))
  20756. (forward-line 1)))
  20757. (save-excursion
  20758. (org-back-to-heading)
  20759. (cond
  20760. ((looking-at (format org-heading-keyword-regexp-format
  20761. org-quote-string))
  20762. (goto-char (match-end 1))
  20763. (looking-at (concat " +" org-quote-string))
  20764. (replace-match "" t t)
  20765. (when (eolp) (insert " ")))
  20766. ((looking-at org-outline-regexp)
  20767. (goto-char (match-end 0))
  20768. (insert org-quote-string " ")))))))
  20769. (defun org-reftex-citation ()
  20770. "Use reftex-citation to insert a citation into the buffer.
  20771. This looks for a line like
  20772. #+BIBLIOGRAPHY: foo plain option:-d
  20773. and derives from it that foo.bib is the bibliography file relevant
  20774. for this document. It then installs the necessary environment for RefTeX
  20775. to work in this buffer and calls `reftex-citation' to insert a citation
  20776. into the buffer.
  20777. Export of such citations to both LaTeX and HTML is handled by the contributed
  20778. package ox-bibtex by Taru Karttunen."
  20779. (interactive)
  20780. (let ((reftex-docstruct-symbol 'rds)
  20781. (reftex-cite-format "\\cite{%l}")
  20782. rds bib)
  20783. (save-excursion
  20784. (save-restriction
  20785. (widen)
  20786. (let ((case-fold-search t)
  20787. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20788. (if (not (save-excursion
  20789. (or (re-search-forward re nil t)
  20790. (re-search-backward re nil t))))
  20791. (error "No bibliography defined in file")
  20792. (setq bib (concat (match-string 1) ".bib")
  20793. rds (list (list 'bib bib)))))))
  20794. (call-interactively 'reftex-citation)))
  20795. ;;;; Functions extending outline functionality
  20796. (defun org-beginning-of-line (&optional arg)
  20797. "Go to the beginning of the current line. If that is invisible, continue
  20798. to a visible line beginning. This makes the function of C-a more intuitive.
  20799. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20800. first attempt, and only move to after the tags when the cursor is already
  20801. beyond the end of the headline."
  20802. (interactive "P")
  20803. (let ((pos (point))
  20804. (special (if (consp org-special-ctrl-a/e)
  20805. (car org-special-ctrl-a/e)
  20806. org-special-ctrl-a/e))
  20807. refpos)
  20808. (if (org-bound-and-true-p visual-line-mode)
  20809. (beginning-of-visual-line 1)
  20810. (beginning-of-line 1))
  20811. (if (and arg (fboundp 'move-beginning-of-line))
  20812. (call-interactively 'move-beginning-of-line)
  20813. (if (bobp)
  20814. nil
  20815. (backward-char 1)
  20816. (if (org-truely-invisible-p)
  20817. (while (and (not (bobp)) (org-truely-invisible-p))
  20818. (backward-char 1)
  20819. (beginning-of-line 1))
  20820. (forward-char 1))))
  20821. (when special
  20822. (cond
  20823. ((and (looking-at org-complex-heading-regexp)
  20824. (= (char-after (match-end 1)) ?\ ))
  20825. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20826. (point-at-eol)))
  20827. (goto-char
  20828. (if (eq special t)
  20829. (cond ((> pos refpos) refpos)
  20830. ((= pos (point)) refpos)
  20831. (t (point)))
  20832. (cond ((> pos (point)) (point))
  20833. ((not (eq last-command this-command)) (point))
  20834. (t refpos)))))
  20835. ((org-at-item-p)
  20836. ;; Being at an item and not looking at an the item means point
  20837. ;; was previously moved to beginning of a visual line, which
  20838. ;; doesn't contain the item. Therefore, do nothing special,
  20839. ;; just stay here.
  20840. (when (looking-at org-list-full-item-re)
  20841. ;; Set special position at first white space character after
  20842. ;; bullet, and check-box, if any.
  20843. (let ((after-bullet
  20844. (let ((box (match-end 3)))
  20845. (if (not box) (match-end 1)
  20846. (let ((after (char-after box)))
  20847. (if (and after (= after ? )) (1+ box) box))))))
  20848. ;; Special case: Move point to special position when
  20849. ;; currently after it or at beginning of line.
  20850. (if (eq special t)
  20851. (when (or (> pos after-bullet) (= (point) pos))
  20852. (goto-char after-bullet))
  20853. ;; Reversed case: Move point to special position when
  20854. ;; point was already at beginning of line and command is
  20855. ;; repeated.
  20856. (when (and (= (point) pos) (eq last-command this-command))
  20857. (goto-char after-bullet))))))))
  20858. (org-no-warnings
  20859. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  20860. (setq disable-point-adjustment
  20861. (or (not (invisible-p (point)))
  20862. (not (invisible-p (max (point-min) (1- (point))))))))
  20863. (defun org-end-of-line (&optional arg)
  20864. "Go to the end of the line.
  20865. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20866. tags on the first attempt, and only move to after the tags when
  20867. the cursor is already beyond the end of the headline."
  20868. (interactive "P")
  20869. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20870. org-special-ctrl-a/e))
  20871. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20872. 'end-of-visual-line)
  20873. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20874. (t 'end-of-line))))
  20875. (if (or (not special) arg) (call-interactively move-fun)
  20876. (let* ((element (save-excursion (beginning-of-line)
  20877. (org-element-at-point)))
  20878. (type (org-element-type element)))
  20879. (cond
  20880. ((memq type '(headline inlinetask))
  20881. (let ((pos (point)))
  20882. (beginning-of-line 1)
  20883. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20884. (if (eq special t)
  20885. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20886. (goto-char (match-beginning 1))
  20887. (goto-char (match-end 0)))
  20888. (if (or (< pos (match-end 0))
  20889. (not (eq this-command last-command)))
  20890. (goto-char (match-end 0))
  20891. (goto-char (match-beginning 1))))
  20892. (call-interactively move-fun))))
  20893. ((org-element-property :hiddenp element)
  20894. ;; If element is hidden, `move-end-of-line' would put point
  20895. ;; after it. Use `end-of-line' to stay on current line.
  20896. (call-interactively 'end-of-line))
  20897. (t (call-interactively move-fun)))))
  20898. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  20899. (setq disable-point-adjustment
  20900. (or (not (invisible-p (point)))
  20901. (not (invisible-p (max (point-min) (1- (point))))))))
  20902. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20903. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20904. (defun org-backward-sentence (&optional arg)
  20905. "Go to beginning of sentence, or beginning of table field.
  20906. This will call `backward-sentence' or `org-table-beginning-of-field',
  20907. depending on context."
  20908. (interactive "P")
  20909. (cond
  20910. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20911. (t (call-interactively 'backward-sentence))))
  20912. (defun org-forward-sentence (&optional arg)
  20913. "Go to end of sentence, or end of table field.
  20914. This will call `forward-sentence' or `org-table-end-of-field',
  20915. depending on context."
  20916. (interactive "P")
  20917. (cond
  20918. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20919. (t (call-interactively 'forward-sentence))))
  20920. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20921. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20922. (defun org-kill-line (&optional arg)
  20923. "Kill line, to tags or end of line."
  20924. (interactive "P")
  20925. (cond
  20926. ((or (not org-special-ctrl-k)
  20927. (bolp)
  20928. (not (org-at-heading-p)))
  20929. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20930. org-ctrl-k-protect-subtree)
  20931. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20932. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20933. (user-error "C-k aborted as it would kill a hidden subtree")))
  20934. (call-interactively
  20935. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20936. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20937. (kill-region (point) (match-beginning 1))
  20938. (org-set-tags nil t))
  20939. (t (kill-region (point) (point-at-eol)))))
  20940. (define-key org-mode-map "\C-k" 'org-kill-line)
  20941. (defun org-yank (&optional arg)
  20942. "Yank. If the kill is a subtree, treat it specially.
  20943. This command will look at the current kill and check if is a single
  20944. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20945. cursor is at the beginning of a line or after the stars of a currently
  20946. empty headline, then the yank is handled specially. How exactly depends
  20947. on the value of the following variables, both set by default.
  20948. org-yank-folded-subtrees
  20949. When set, the subtree(s) will be folded after insertion, but only
  20950. if doing so would now swallow text after the yanked text.
  20951. org-yank-adjusted-subtrees
  20952. When set, the subtree will be promoted or demoted in order to
  20953. fit into the local outline tree structure, which means that the level
  20954. will be adjusted so that it becomes the smaller one of the two
  20955. *visible* surrounding headings.
  20956. Any prefix to this command will cause `yank' to be called directly with
  20957. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20958. will just
  20959. plainly yank the text as it is.
  20960. \[1] The test checks if the first non-white line is a heading
  20961. and if there are no other headings with fewer stars."
  20962. (interactive "P")
  20963. (org-yank-generic 'yank arg))
  20964. (defun org-yank-generic (command arg)
  20965. "Perform some yank-like command.
  20966. This function implements the behavior described in the `org-yank'
  20967. documentation. However, it has been generalized to work for any
  20968. interactive command with similar behavior."
  20969. ;; pretend to be command COMMAND
  20970. (setq this-command command)
  20971. (if arg
  20972. (call-interactively command)
  20973. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20974. (and (org-kill-is-subtree-p)
  20975. (or (bolp)
  20976. (and (looking-at "[ \t]*$")
  20977. (string-match
  20978. "\\`\\*+\\'"
  20979. (buffer-substring (point-at-bol) (point)))))))
  20980. swallowp)
  20981. (cond
  20982. ((and subtreep org-yank-folded-subtrees)
  20983. (let ((beg (point))
  20984. end)
  20985. (if (and subtreep org-yank-adjusted-subtrees)
  20986. (org-paste-subtree nil nil 'for-yank)
  20987. (call-interactively command))
  20988. (setq end (point))
  20989. (goto-char beg)
  20990. (when (and (bolp) subtreep
  20991. (not (setq swallowp
  20992. (org-yank-folding-would-swallow-text beg end))))
  20993. (org-with-limited-levels
  20994. (or (looking-at org-outline-regexp)
  20995. (re-search-forward org-outline-regexp-bol end t))
  20996. (while (and (< (point) end) (looking-at org-outline-regexp))
  20997. (hide-subtree)
  20998. (org-cycle-show-empty-lines 'folded)
  20999. (condition-case nil
  21000. (outline-forward-same-level 1)
  21001. (error (goto-char end))))))
  21002. (when swallowp
  21003. (message
  21004. "Inserted text not folded because that would swallow text"))
  21005. (goto-char end)
  21006. (skip-chars-forward " \t\n\r")
  21007. (beginning-of-line 1)
  21008. (push-mark beg 'nomsg)))
  21009. ((and subtreep org-yank-adjusted-subtrees)
  21010. (let ((beg (point-at-bol)))
  21011. (org-paste-subtree nil nil 'for-yank)
  21012. (push-mark beg 'nomsg)))
  21013. (t
  21014. (call-interactively command))))))
  21015. (defun org-yank-folding-would-swallow-text (beg end)
  21016. "Would hide-subtree at BEG swallow any text after END?"
  21017. (let (level)
  21018. (org-with-limited-levels
  21019. (save-excursion
  21020. (goto-char beg)
  21021. (when (or (looking-at org-outline-regexp)
  21022. (re-search-forward org-outline-regexp-bol end t))
  21023. (setq level (org-outline-level)))
  21024. (goto-char end)
  21025. (skip-chars-forward " \t\r\n\v\f")
  21026. (if (or (eobp)
  21027. (and (bolp) (looking-at org-outline-regexp)
  21028. (<= (org-outline-level) level)))
  21029. nil ; Nothing would be swallowed
  21030. t))))) ; something would swallow
  21031. (define-key org-mode-map "\C-y" 'org-yank)
  21032. (defun org-truely-invisible-p ()
  21033. "Check if point is at a character currently not visible.
  21034. This version does not only check the character property, but also
  21035. `visible-mode'."
  21036. ;; Early versions of noutline don't have `outline-invisible-p'.
  21037. (if (org-bound-and-true-p visible-mode)
  21038. nil
  21039. (outline-invisible-p)))
  21040. (defun org-invisible-p2 ()
  21041. "Check if point is at a character currently not visible."
  21042. (save-excursion
  21043. (if (and (eolp) (not (bobp))) (backward-char 1))
  21044. ;; Early versions of noutline don't have `outline-invisible-p'.
  21045. (outline-invisible-p)))
  21046. (defun org-back-to-heading (&optional invisible-ok)
  21047. "Call `outline-back-to-heading', but provide a better error message."
  21048. (condition-case nil
  21049. (outline-back-to-heading invisible-ok)
  21050. (error (error "Before first headline at position %d in buffer %s"
  21051. (point) (current-buffer)))))
  21052. (defun org-before-first-heading-p ()
  21053. "Before first heading?"
  21054. (save-excursion
  21055. (end-of-line)
  21056. (null (re-search-backward org-outline-regexp-bol nil t))))
  21057. (defun org-at-heading-p (&optional ignored)
  21058. (outline-on-heading-p t))
  21059. ;; Compatibility alias with Org versions < 7.8.03
  21060. (defalias 'org-on-heading-p 'org-at-heading-p)
  21061. (defun org-at-comment-p nil
  21062. "Is cursor in a line starting with a # character?"
  21063. (save-excursion
  21064. (beginning-of-line)
  21065. (looking-at "^#")))
  21066. (defun org-at-drawer-p nil
  21067. "Is cursor at a drawer keyword?"
  21068. (save-excursion
  21069. (move-beginning-of-line 1)
  21070. (looking-at org-drawer-regexp)))
  21071. (defun org-at-block-p nil
  21072. "Is cursor at a block keyword?"
  21073. (save-excursion
  21074. (move-beginning-of-line 1)
  21075. (looking-at org-block-regexp)))
  21076. (defun org-point-at-end-of-empty-headline ()
  21077. "If point is at the end of an empty headline, return t, else nil.
  21078. If the heading only contains a TODO keyword, it is still still considered
  21079. empty."
  21080. (and (looking-at "[ \t]*$")
  21081. (when org-todo-line-regexp
  21082. (save-excursion
  21083. (beginning-of-line 1)
  21084. (let ((case-fold-search nil))
  21085. (looking-at org-todo-line-regexp)
  21086. (string= (match-string 3) ""))))))
  21087. (defun org-at-heading-or-item-p ()
  21088. (or (org-at-heading-p) (org-at-item-p)))
  21089. (defun org-at-target-p ()
  21090. (or (org-in-regexp org-radio-target-regexp)
  21091. (org-in-regexp org-target-regexp)))
  21092. ;; Compatibility alias with Org versions < 7.8.03
  21093. (defalias 'org-on-target-p 'org-at-target-p)
  21094. (defun org-up-heading-all (arg)
  21095. "Move to the heading line of which the present line is a subheading.
  21096. This function considers both visible and invisible heading lines.
  21097. With argument, move up ARG levels."
  21098. (if (fboundp 'outline-up-heading-all)
  21099. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21100. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21101. (defun org-up-heading-safe ()
  21102. "Move to the heading line of which the present line is a subheading.
  21103. This version will not throw an error. It will return the level of the
  21104. headline found, or nil if no higher level is found.
  21105. Also, this function will be a lot faster than `outline-up-heading',
  21106. because it relies on stars being the outline starters. This can really
  21107. make a significant difference in outlines with very many siblings."
  21108. (let (start-level re)
  21109. (org-back-to-heading t)
  21110. (setq start-level (funcall outline-level))
  21111. (if (equal start-level 1)
  21112. nil
  21113. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21114. (if (re-search-backward re nil t)
  21115. (funcall outline-level)))))
  21116. (defun org-first-sibling-p ()
  21117. "Is this heading the first child of its parents?"
  21118. (interactive)
  21119. (let ((re org-outline-regexp-bol)
  21120. level l)
  21121. (unless (org-at-heading-p t)
  21122. (user-error "Not at a heading"))
  21123. (setq level (funcall outline-level))
  21124. (save-excursion
  21125. (if (not (re-search-backward re nil t))
  21126. t
  21127. (setq l (funcall outline-level))
  21128. (< l level)))))
  21129. (defun org-goto-sibling (&optional previous)
  21130. "Goto the next sibling, even if it is invisible.
  21131. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21132. when a sibling was found. When none is found, return nil and don't
  21133. move point."
  21134. (let ((fun (if previous 're-search-backward 're-search-forward))
  21135. (pos (point))
  21136. (re org-outline-regexp-bol)
  21137. level l)
  21138. (when (condition-case nil (org-back-to-heading t) (error nil))
  21139. (setq level (funcall outline-level))
  21140. (catch 'exit
  21141. (or previous (forward-char 1))
  21142. (while (funcall fun re nil t)
  21143. (setq l (funcall outline-level))
  21144. (when (< l level) (goto-char pos) (throw 'exit nil))
  21145. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21146. (goto-char pos)
  21147. nil))))
  21148. (defun org-show-siblings ()
  21149. "Show all siblings of the current headline."
  21150. (save-excursion
  21151. (while (org-goto-sibling) (org-flag-heading nil)))
  21152. (save-excursion
  21153. (while (org-goto-sibling 'previous)
  21154. (org-flag-heading nil))))
  21155. (defun org-goto-first-child ()
  21156. "Goto the first child, even if it is invisible.
  21157. Return t when a child was found. Otherwise don't move point and
  21158. return nil."
  21159. (let (level (pos (point)) (re org-outline-regexp-bol))
  21160. (when (condition-case nil (org-back-to-heading t) (error nil))
  21161. (setq level (outline-level))
  21162. (forward-char 1)
  21163. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21164. (progn (goto-char (match-beginning 0)) t)
  21165. (goto-char pos) nil))))
  21166. (defun org-show-hidden-entry ()
  21167. "Show an entry where even the heading is hidden."
  21168. (save-excursion
  21169. (org-show-entry)))
  21170. (defun org-flag-heading (flag &optional entry)
  21171. "Flag the current heading. FLAG non-nil means make invisible.
  21172. When ENTRY is non-nil, show the entire entry."
  21173. (save-excursion
  21174. (org-back-to-heading t)
  21175. ;; Check if we should show the entire entry
  21176. (if entry
  21177. (progn
  21178. (org-show-entry)
  21179. (save-excursion
  21180. (and (outline-next-heading)
  21181. (org-flag-heading nil))))
  21182. (outline-flag-region (max (point-min) (1- (point)))
  21183. (save-excursion (outline-end-of-heading) (point))
  21184. flag))))
  21185. (defun org-get-next-sibling ()
  21186. "Move to next heading of the same level, and return point.
  21187. If there is no such heading, return nil.
  21188. This is like outline-next-sibling, but invisible headings are ok."
  21189. (let ((level (funcall outline-level)))
  21190. (outline-next-heading)
  21191. (while (and (not (eobp)) (> (funcall outline-level) level))
  21192. (outline-next-heading))
  21193. (if (or (eobp) (< (funcall outline-level) level))
  21194. nil
  21195. (point))))
  21196. (defun org-get-last-sibling ()
  21197. "Move to previous heading of the same level, and return point.
  21198. If there is no such heading, return nil."
  21199. (let ((opoint (point))
  21200. (level (funcall outline-level)))
  21201. (outline-previous-heading)
  21202. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21203. (while (and (> (funcall outline-level) level)
  21204. (not (bobp)))
  21205. (outline-previous-heading))
  21206. (if (< (funcall outline-level) level)
  21207. nil
  21208. (point)))))
  21209. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21210. "Goto to the end of a subtree."
  21211. ;; This contains an exact copy of the original function, but it uses
  21212. ;; `org-back-to-heading', to make it work also in invisible
  21213. ;; trees. And is uses an invisible-ok argument.
  21214. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21215. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21216. ;; with many children and grandchildren etc, this can be much faster
  21217. ;; than the outline version.
  21218. (org-back-to-heading invisible-ok)
  21219. (let ((first t)
  21220. (level (funcall outline-level)))
  21221. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21222. ;; A true heading (not a plain list item), in Org-mode
  21223. ;; This means we can easily find the end by looking
  21224. ;; only for the right number of stars. Using a regexp to do
  21225. ;; this is so much faster than using a Lisp loop.
  21226. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21227. (forward-char 1)
  21228. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21229. ;; something else, do it the slow way
  21230. (while (and (not (eobp))
  21231. (or first (> (funcall outline-level) level)))
  21232. (setq first nil)
  21233. (outline-next-heading)))
  21234. (unless to-heading
  21235. (if (memq (preceding-char) '(?\n ?\^M))
  21236. (progn
  21237. ;; Go to end of line before heading
  21238. (forward-char -1)
  21239. (if (memq (preceding-char) '(?\n ?\^M))
  21240. ;; leave blank line before heading
  21241. (forward-char -1))))))
  21242. (point))
  21243. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21244. "Use Org version in org-mode, for dramatic speed-up."
  21245. (if (derived-mode-p 'org-mode)
  21246. (progn
  21247. (org-end-of-subtree nil t)
  21248. (unless (eobp) (backward-char 1)))
  21249. ad-do-it))
  21250. (defun org-end-of-meta-data-and-drawers ()
  21251. "Jump to the first text after meta data and drawers in the current entry.
  21252. This will move over empty lines, lines with planning time stamps,
  21253. clocking lines, and drawers."
  21254. (org-back-to-heading t)
  21255. (let ((end (save-excursion (outline-next-heading) (point)))
  21256. (re (concat "\\(" org-drawer-regexp "\\)"
  21257. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21258. (forward-line 1)
  21259. (while (re-search-forward re end t)
  21260. (if (not (match-end 1))
  21261. ;; empty or planning line
  21262. (forward-line 1)
  21263. ;; a drawer, find the end
  21264. (re-search-forward "^[ \t]*:END:" end 'move)
  21265. (forward-line 1)))
  21266. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21267. (point)))
  21268. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21269. "Move forward to the ARG'th subheading at same level as this one.
  21270. Stop at the first and last subheadings of a superior heading.
  21271. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21272. non-nil it will also look at invisible ones."
  21273. (interactive "p")
  21274. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21275. (if (and arg (< arg 0))
  21276. (goto-char (point-min))
  21277. (outline-next-heading))
  21278. (org-at-heading-p)
  21279. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21280. (f (if (and arg (< arg 0))
  21281. 're-search-backward
  21282. 're-search-forward))
  21283. (count (if arg (abs arg) 1))
  21284. (result (point)))
  21285. (while (and (prog1 (> count 0)
  21286. (forward-char (if (and arg (< arg 0)) -1 1)))
  21287. (funcall f org-outline-regexp-bol nil 'move))
  21288. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21289. (cond ((< l level) (setq count 0))
  21290. ((and (= l level)
  21291. (or invisible-ok
  21292. (progn
  21293. (goto-char (line-beginning-position))
  21294. (not (outline-invisible-p)))))
  21295. (setq count (1- count))
  21296. (when (eq l level)
  21297. (setq result (point)))))))
  21298. (goto-char result))
  21299. (beginning-of-line 1)))
  21300. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21301. "Move backward to the ARG'th subheading at same level as this one.
  21302. Stop at the first and last subheadings of a superior heading."
  21303. (interactive "p")
  21304. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21305. (defun org-next-block (arg &optional backward block-regexp)
  21306. "Jump to the next block.
  21307. With a prefix argument ARG, jump forward ARG many source blocks.
  21308. When BACKWARD is non-nil, jump to the previous block.
  21309. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21310. (interactive "p")
  21311. (let ((re (or block-regexp org-block-regexp))
  21312. (re-search-fn (or (and backward 're-search-backward)
  21313. 're-search-forward)))
  21314. (if (looking-at re) (forward-char 1))
  21315. (condition-case nil
  21316. (funcall re-search-fn re nil nil arg)
  21317. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21318. (goto-char (match-beginning 0)) (org-show-context)))
  21319. (defun org-previous-block (arg &optional block-regexp)
  21320. "Jump to the previous block.
  21321. With a prefix argument ARG, jump backward ARG many source blocks.
  21322. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21323. (interactive "p")
  21324. (org-next-block arg t block-regexp))
  21325. (defun org-forward-element ()
  21326. "Move forward by one element.
  21327. Move to the next element at the same level, when possible."
  21328. (interactive)
  21329. (cond ((eobp) (user-error "Cannot move further down"))
  21330. ((org-with-limited-levels (org-at-heading-p))
  21331. (let ((origin (point)))
  21332. (goto-char (org-end-of-subtree nil t))
  21333. (unless (org-with-limited-levels (org-at-heading-p))
  21334. (goto-char origin)
  21335. (user-error "Cannot move further down"))))
  21336. (t
  21337. (let* ((elem (org-element-at-point))
  21338. (end (org-element-property :end elem))
  21339. (parent (org-element-property :parent elem)))
  21340. (if (and parent (= (org-element-property :contents-end parent) end))
  21341. (goto-char (org-element-property :end parent))
  21342. (goto-char end))))))
  21343. (defun org-backward-element ()
  21344. "Move backward by one element.
  21345. Move to the previous element at the same level, when possible."
  21346. (interactive)
  21347. (cond ((bobp) (user-error "Cannot move further up"))
  21348. ((org-with-limited-levels (org-at-heading-p))
  21349. ;; At a headline, move to the previous one, if any, or stay
  21350. ;; here.
  21351. (let ((origin (point)))
  21352. (org-with-limited-levels (org-backward-heading-same-level 1))
  21353. ;; When current headline has no sibling above, move to its
  21354. ;; parent.
  21355. (when (= (point) origin)
  21356. (or (org-with-limited-levels (org-up-heading-safe))
  21357. (progn (goto-char origin)
  21358. (user-error "Cannot move further up"))))))
  21359. (t
  21360. (let* ((trail (org-element-at-point 'keep-trail))
  21361. (elem (car trail))
  21362. (prev-elem (nth 1 trail))
  21363. (beg (org-element-property :begin elem)))
  21364. (cond
  21365. ;; Move to beginning of current element if point isn't
  21366. ;; there already.
  21367. ((/= (point) beg) (goto-char beg))
  21368. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21369. ((org-before-first-heading-p) (goto-char (point-min)))
  21370. (t (org-back-to-heading)))))))
  21371. (defun org-up-element ()
  21372. "Move to upper element."
  21373. (interactive)
  21374. (if (org-with-limited-levels (org-at-heading-p))
  21375. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  21376. (let* ((elem (org-element-at-point))
  21377. (parent (org-element-property :parent elem)))
  21378. (if parent (goto-char (org-element-property :begin parent))
  21379. (if (org-with-limited-levels (org-before-first-heading-p))
  21380. (user-error "No surrounding element")
  21381. (org-with-limited-levels (org-back-to-heading)))))))
  21382. (defvar org-element-greater-elements)
  21383. (defun org-down-element ()
  21384. "Move to inner element."
  21385. (interactive)
  21386. (let ((element (org-element-at-point)))
  21387. (cond
  21388. ((memq (org-element-type element) '(plain-list table))
  21389. (goto-char (org-element-property :contents-begin element))
  21390. (forward-char))
  21391. ((memq (org-element-type element) org-element-greater-elements)
  21392. ;; If contents are hidden, first disclose them.
  21393. (when (org-element-property :hiddenp element) (org-cycle))
  21394. (goto-char (or (org-element-property :contents-begin element)
  21395. (user-error "No content for this element"))))
  21396. (t (user-error "No inner element")))))
  21397. (defun org-drag-element-backward ()
  21398. "Move backward element at point."
  21399. (interactive)
  21400. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21401. (let* ((trail (org-element-at-point 'keep-trail))
  21402. (elem (car trail))
  21403. (prev-elem (nth 1 trail)))
  21404. ;; Error out if no previous element or previous element is
  21405. ;; a parent of the current one.
  21406. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21407. (user-error "Cannot drag element backward")
  21408. (let ((pos (point)))
  21409. (org-element-swap-A-B prev-elem elem)
  21410. (goto-char (+ (org-element-property :begin prev-elem)
  21411. (- pos (org-element-property :begin elem)))))))))
  21412. (defun org-drag-element-forward ()
  21413. "Move forward element at point."
  21414. (interactive)
  21415. (let* ((pos (point))
  21416. (elem (org-element-at-point)))
  21417. (when (= (point-max) (org-element-property :end elem))
  21418. (user-error "Cannot drag element forward"))
  21419. (goto-char (org-element-property :end elem))
  21420. (let ((next-elem (org-element-at-point)))
  21421. (when (or (org-element-nested-p elem next-elem)
  21422. (and (eq (org-element-type next-elem) 'headline)
  21423. (not (eq (org-element-type elem) 'headline))))
  21424. (goto-char pos)
  21425. (user-error "Cannot drag element forward"))
  21426. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21427. ;; body's length (without final blanks) and by the length of
  21428. ;; blanks between ELEM and NEXT-ELEM.
  21429. (let ((size-next (- (save-excursion
  21430. (goto-char (org-element-property :end next-elem))
  21431. (skip-chars-backward " \r\t\n")
  21432. (forward-line)
  21433. ;; Small correction if buffer doesn't end
  21434. ;; with a newline character.
  21435. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21436. (org-element-property :begin next-elem)))
  21437. (size-blank (- (org-element-property :end elem)
  21438. (save-excursion
  21439. (goto-char (org-element-property :end elem))
  21440. (skip-chars-backward " \r\t\n")
  21441. (forward-line)
  21442. (point)))))
  21443. (org-element-swap-A-B elem next-elem)
  21444. (goto-char (+ pos size-next size-blank))))))
  21445. (defun org-drag-line-forward (arg)
  21446. "Drag the line at point ARG lines forward."
  21447. (interactive "p")
  21448. (dotimes (n (abs arg))
  21449. (let ((c (current-column)))
  21450. (if (< 0 arg)
  21451. (progn
  21452. (beginning-of-line 2)
  21453. (transpose-lines 1)
  21454. (beginning-of-line 0))
  21455. (transpose-lines 1)
  21456. (beginning-of-line -1))
  21457. (org-move-to-column c))))
  21458. (defun org-drag-line-backward (arg)
  21459. "Drag the line at point ARG lines backward."
  21460. (interactive "p")
  21461. (org-drag-line-forward (- arg)))
  21462. (defun org-mark-element ()
  21463. "Put point at beginning of this element, mark at end.
  21464. Interactively, if this command is repeated or (in Transient Mark
  21465. mode) if the mark is active, it marks the next element after the
  21466. ones already marked."
  21467. (interactive)
  21468. (let (deactivate-mark)
  21469. (if (and (org-called-interactively-p 'any)
  21470. (or (and (eq last-command this-command) (mark t))
  21471. (and transient-mark-mode mark-active)))
  21472. (set-mark
  21473. (save-excursion
  21474. (goto-char (mark))
  21475. (goto-char (org-element-property :end (org-element-at-point)))))
  21476. (let ((element (org-element-at-point)))
  21477. (end-of-line)
  21478. (push-mark (org-element-property :end element) t t)
  21479. (goto-char (org-element-property :begin element))))))
  21480. (defun org-narrow-to-element ()
  21481. "Narrow buffer to current element."
  21482. (interactive)
  21483. (let ((elem (org-element-at-point)))
  21484. (cond
  21485. ((eq (car elem) 'headline)
  21486. (narrow-to-region
  21487. (org-element-property :begin elem)
  21488. (org-element-property :end elem)))
  21489. ((memq (car elem) org-element-greater-elements)
  21490. (narrow-to-region
  21491. (org-element-property :contents-begin elem)
  21492. (org-element-property :contents-end elem)))
  21493. (t
  21494. (narrow-to-region
  21495. (org-element-property :begin elem)
  21496. (org-element-property :end elem))))))
  21497. (defun org-transpose-element ()
  21498. "Transpose current and previous elements, keeping blank lines between.
  21499. Point is moved after both elements."
  21500. (interactive)
  21501. (org-skip-whitespace)
  21502. (let ((end (org-element-property :end (org-element-at-point))))
  21503. (org-drag-element-backward)
  21504. (goto-char end)))
  21505. (defun org-unindent-buffer ()
  21506. "Un-indent the visible part of the buffer.
  21507. Relative indentation (between items, inside blocks, etc.) isn't
  21508. modified."
  21509. (interactive)
  21510. (unless (eq major-mode 'org-mode)
  21511. (user-error "Cannot un-indent a buffer not in Org mode"))
  21512. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21513. unindent-tree ; For byte-compiler.
  21514. (unindent-tree
  21515. (function
  21516. (lambda (contents)
  21517. (mapc
  21518. (lambda (element)
  21519. (if (memq (org-element-type element) '(headline section))
  21520. (funcall unindent-tree (org-element-contents element))
  21521. (save-excursion
  21522. (save-restriction
  21523. (narrow-to-region
  21524. (org-element-property :begin element)
  21525. (org-element-property :end element))
  21526. (org-do-remove-indentation)))))
  21527. (reverse contents))))))
  21528. (funcall unindent-tree (org-element-contents parse-tree))))
  21529. (defun org-show-subtree ()
  21530. "Show everything after this heading at deeper levels."
  21531. (interactive)
  21532. (outline-flag-region
  21533. (point)
  21534. (save-excursion
  21535. (org-end-of-subtree t t))
  21536. nil))
  21537. (defun org-show-entry ()
  21538. "Show the body directly following this heading.
  21539. Show the heading too, if it is currently invisible."
  21540. (interactive)
  21541. (save-excursion
  21542. (condition-case nil
  21543. (progn
  21544. (org-back-to-heading t)
  21545. (outline-flag-region
  21546. (max (point-min) (1- (point)))
  21547. (save-excursion
  21548. (if (re-search-forward
  21549. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21550. (match-beginning 1)
  21551. (point-max)))
  21552. nil)
  21553. (org-cycle-hide-drawers 'children))
  21554. (error nil))))
  21555. (defun org-make-options-regexp (kwds &optional extra)
  21556. "Make a regular expression for keyword lines."
  21557. (concat
  21558. "^#\\+\\("
  21559. (mapconcat 'regexp-quote kwds "\\|")
  21560. (if extra (concat "\\|" extra))
  21561. "\\):[ \t]*\\(.*\\)"))
  21562. ;; Make isearch reveal the necessary context
  21563. (defun org-isearch-end ()
  21564. "Reveal context after isearch exits."
  21565. (when isearch-success ; only if search was successful
  21566. (if (featurep 'xemacs)
  21567. ;; Under XEmacs, the hook is run in the correct place,
  21568. ;; we directly show the context.
  21569. (org-show-context 'isearch)
  21570. ;; In Emacs the hook runs *before* restoring the overlays.
  21571. ;; So we have to use a one-time post-command-hook to do this.
  21572. ;; (Emacs 22 has a special variable, see function `org-mode')
  21573. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21574. isearch-mode-end-hook-quit)
  21575. ;; Only when the isearch was not quitted.
  21576. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21577. 'append 'local)))
  21578. (org-fix-ellipsis-at-bol)))
  21579. (defun org-isearch-post-command ()
  21580. "Remove self from hook, and show context."
  21581. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21582. (org-show-context 'isearch))
  21583. ;;;; Integration with and fixes for other packages
  21584. ;;; Imenu support
  21585. (defvar org-imenu-markers nil
  21586. "All markers currently used by Imenu.")
  21587. (make-variable-buffer-local 'org-imenu-markers)
  21588. (defun org-imenu-new-marker (&optional pos)
  21589. "Return a new marker for use by Imenu, and remember the marker."
  21590. (let ((m (make-marker)))
  21591. (move-marker m (or pos (point)))
  21592. (push m org-imenu-markers)
  21593. m))
  21594. (defun org-imenu-get-tree ()
  21595. "Produce the index for Imenu."
  21596. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21597. (setq org-imenu-markers nil)
  21598. (let* ((n org-imenu-depth)
  21599. (re (concat "^" (org-get-limited-outline-regexp)))
  21600. (subs (make-vector (1+ n) nil))
  21601. (last-level 0)
  21602. m level head0 head)
  21603. (save-excursion
  21604. (save-restriction
  21605. (widen)
  21606. (goto-char (point-max))
  21607. (while (re-search-backward re nil t)
  21608. (setq level (org-reduced-level (funcall outline-level)))
  21609. (when (and (<= level n)
  21610. (looking-at org-complex-heading-regexp)
  21611. (setq head0 (org-match-string-no-properties 4)))
  21612. (setq head (org-link-display-format head0)
  21613. m (org-imenu-new-marker))
  21614. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21615. (if (>= level last-level)
  21616. (push (cons head m) (aref subs level))
  21617. (push (cons head (aref subs (1+ level))) (aref subs level))
  21618. (loop for i from (1+ level) to n do (aset subs i nil)))
  21619. (setq last-level level)))))
  21620. (aref subs 1)))
  21621. (eval-after-load "imenu"
  21622. '(progn
  21623. (add-hook 'imenu-after-jump-hook
  21624. (lambda ()
  21625. (if (derived-mode-p 'org-mode)
  21626. (org-show-context 'org-goto))))))
  21627. (defun org-link-display-format (link)
  21628. "Replace a link with its the description.
  21629. If there is no description, use the link target."
  21630. (save-match-data
  21631. (if (string-match org-bracket-link-analytic-regexp link)
  21632. (replace-match (if (match-end 5)
  21633. (match-string 5 link)
  21634. (concat (match-string 1 link)
  21635. (match-string 3 link)))
  21636. nil t link)
  21637. link)))
  21638. (defun org-toggle-link-display ()
  21639. "Toggle the literal or descriptive display of links."
  21640. (interactive)
  21641. (if org-descriptive-links
  21642. (progn (org-remove-from-invisibility-spec '(org-link))
  21643. (org-restart-font-lock)
  21644. (setq org-descriptive-links nil))
  21645. (progn (add-to-invisibility-spec '(org-link))
  21646. (org-restart-font-lock)
  21647. (setq org-descriptive-links t))))
  21648. ;; Speedbar support
  21649. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21650. "Overlay marking the agenda restriction line in speedbar.")
  21651. (overlay-put org-speedbar-restriction-lock-overlay
  21652. 'face 'org-agenda-restriction-lock)
  21653. (overlay-put org-speedbar-restriction-lock-overlay
  21654. 'help-echo "Agendas are currently limited to this item.")
  21655. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21656. (defun org-speedbar-set-agenda-restriction ()
  21657. "Restrict future agenda commands to the location at point in speedbar.
  21658. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21659. (interactive)
  21660. (require 'org-agenda)
  21661. (let (p m tp np dir txt)
  21662. (cond
  21663. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21664. 'org-imenu t))
  21665. (setq m (get-text-property p 'org-imenu-marker))
  21666. (with-current-buffer (marker-buffer m)
  21667. (goto-char m)
  21668. (org-agenda-set-restriction-lock 'subtree)))
  21669. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21670. 'speedbar-function 'speedbar-find-file))
  21671. (setq tp (previous-single-property-change
  21672. (1+ p) 'speedbar-function)
  21673. np (next-single-property-change
  21674. tp 'speedbar-function)
  21675. dir (speedbar-line-directory)
  21676. txt (buffer-substring-no-properties (or tp (point-min))
  21677. (or np (point-max))))
  21678. (with-current-buffer (find-file-noselect
  21679. (let ((default-directory dir))
  21680. (expand-file-name txt)))
  21681. (unless (derived-mode-p 'org-mode)
  21682. (user-error "Cannot restrict to non-Org-mode file"))
  21683. (org-agenda-set-restriction-lock 'file)))
  21684. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  21685. (move-overlay org-speedbar-restriction-lock-overlay
  21686. (point-at-bol) (point-at-eol))
  21687. (setq current-prefix-arg nil)
  21688. (org-agenda-maybe-redo)))
  21689. (defvar speedbar-file-key-map)
  21690. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  21691. (eval-after-load "speedbar"
  21692. '(progn
  21693. (speedbar-add-supported-extension ".org")
  21694. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21695. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21696. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21697. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21698. (add-hook 'speedbar-visiting-tag-hook
  21699. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21700. ;;; Fixes and Hacks for problems with other packages
  21701. ;; Make flyspell not check words in links, to not mess up our keymap
  21702. (defvar org-element-affiliated-keywords) ; From org-element.el
  21703. (defvar org-element-block-name-alist) ; From org-element.el
  21704. (defun org-mode-flyspell-verify ()
  21705. "Don't let flyspell put overlays at active buttons, or on
  21706. {todo,all-time,additional-option-like}-keywords."
  21707. (require 'org-element) ; For `org-element-affiliated-keywords'
  21708. (let ((pos (max (1- (point)) (point-min)))
  21709. (word (thing-at-point 'word)))
  21710. (and (not (get-text-property pos 'keymap))
  21711. (not (get-text-property pos 'org-no-flyspell))
  21712. (not (member word org-todo-keywords-1))
  21713. (not (member word org-all-time-keywords))
  21714. (not (member word org-options-keywords))
  21715. (not (member word (mapcar 'car org-startup-options)))
  21716. (not (member-ignore-case word org-element-affiliated-keywords))
  21717. (not (member-ignore-case word (org-get-export-keywords)))
  21718. (not (member-ignore-case
  21719. word (mapcar 'car org-element-block-name-alist)))
  21720. (not (member-ignore-case word '("BEGIN" "END" "ATTR")))
  21721. (not (org-in-src-block-p)))))
  21722. (defun org-remove-flyspell-overlays-in (beg end)
  21723. "Remove flyspell overlays in region."
  21724. (and (org-bound-and-true-p flyspell-mode)
  21725. (fboundp 'flyspell-delete-region-overlays)
  21726. (flyspell-delete-region-overlays beg end))
  21727. (add-text-properties beg end '(org-no-flyspell t)))
  21728. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21729. (eval-after-load "bookmark"
  21730. '(if (boundp 'bookmark-after-jump-hook)
  21731. ;; We can use the hook
  21732. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21733. ;; Hook not available, use advice
  21734. (defadvice bookmark-jump (after org-make-visible activate)
  21735. "Make the position visible."
  21736. (org-bookmark-jump-unhide))))
  21737. ;; Make sure saveplace shows the location if it was hidden
  21738. (eval-after-load "saveplace"
  21739. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21740. "Make the position visible."
  21741. (org-bookmark-jump-unhide)))
  21742. ;; Make sure ecb shows the location if it was hidden
  21743. (eval-after-load "ecb"
  21744. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21745. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21746. (if (derived-mode-p 'org-mode)
  21747. (org-show-context))))
  21748. (defun org-bookmark-jump-unhide ()
  21749. "Unhide the current position, to show the bookmark location."
  21750. (and (derived-mode-p 'org-mode)
  21751. (or (outline-invisible-p)
  21752. (save-excursion (goto-char (max (point-min) (1- (point))))
  21753. (outline-invisible-p)))
  21754. (org-show-context 'bookmark-jump)))
  21755. ;; Make session.el ignore our circular variable
  21756. (defvar session-globals-exclude)
  21757. (eval-after-load "session"
  21758. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21759. ;;;; Finish up
  21760. (provide 'org)
  21761. (run-hooks 'org-load-hook)
  21762. ;;; org.el ends here