org.el 938 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2014 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. (make-variable-buffer-local 'org-table-formula-constants-local)
  65. ;;;; Require other packages
  66. (eval-when-compile
  67. (require 'cl)
  68. (require 'gnus-sum))
  69. (require 'calendar)
  70. (require 'find-func)
  71. (require 'format-spec)
  72. (or (equal this-command 'eval-buffer)
  73. (condition-case nil
  74. (load (concat (file-name-directory load-file-name)
  75. "org-loaddefs.el")
  76. nil t t t)
  77. (error
  78. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  79. (sit-for 3)
  80. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  81. (sit-for 3))))
  82. (require 'org-macs)
  83. (require 'org-compat)
  84. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  85. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  86. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  87. ;;
  88. ;; In Org buffers, the value of `outline-regexp' is that of
  89. ;; `org-outline-regexp'. The only function still directly relying on
  90. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  91. ;; job when `orgstruct-mode' is active.
  92. (defvar org-outline-regexp "\\*+ "
  93. "Regexp to match Org headlines.")
  94. (defvar org-outline-regexp-bol "^\\*+ "
  95. "Regexp to match Org headlines.
  96. This is similar to `org-outline-regexp' but additionally makes
  97. sure that we are at the beginning of the line.")
  98. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  99. "Matches a headline, putting stars and text into groups.
  100. Stars are put in group 1 and the trimmed body in group 2.")
  101. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  102. (unless (boundp 'calendar-view-holidays-initially-flag)
  103. (org-defvaralias 'calendar-view-holidays-initially-flag
  104. 'view-calendar-holidays-initially))
  105. (unless (boundp 'calendar-view-diary-initially-flag)
  106. (org-defvaralias 'calendar-view-diary-initially-flag
  107. 'view-diary-entries-initially))
  108. (unless (boundp 'diary-fancy-buffer)
  109. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  110. (declare-function org-add-archive-files "org-archive" (files))
  111. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  112. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  113. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  114. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  115. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  116. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  117. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  118. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  119. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  120. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  121. (declare-function org-clock-update-time-maybe "org-clock" ())
  122. (declare-function org-clocktable-shift "org-clock" (dir n))
  123. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  124. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  125. (declare-function orgtbl-mode "org-table" (&optional arg))
  126. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  127. (declare-function org-beamer-mode "ox-beamer" ())
  128. (declare-function org-table-blank-field "org-table" ())
  129. (declare-function org-table-edit-field "org-table" (arg))
  130. (declare-function org-table-insert-row "org-table" (&optional arg))
  131. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  132. (declare-function org-table-set-constants "org-table" ())
  133. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  134. (declare-function org-id-get-create "org-id" (&optional force))
  135. (declare-function org-add-archive-files "org-archive" (files))
  136. (declare-function org-id-find-id-file "org-id" (id))
  137. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  138. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  139. (declare-function org-agenda-redo "org-agenda" (&optional all))
  140. (declare-function org-table-align "org-table" ())
  141. (declare-function org-table-begin "org-table" (&optional table-type))
  142. (declare-function org-table-blank-field "org-table" ())
  143. (declare-function org-table-end "org-table" (&optional table-type))
  144. (declare-function org-table-end-of-field "org-table" (&optional n))
  145. (declare-function org-table-insert-row "org-table" (&optional arg))
  146. (declare-function org-table-paste-rectangle "org-table" ())
  147. (declare-function org-table-maybe-eval-formula "org-table" ())
  148. (declare-function org-table-maybe-recalculate-line "org-table" ())
  149. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  150. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  151. (declare-function org-element-at-point "org-element" ())
  152. (declare-function org-element-cache-reset "org-element" (&optional all))
  153. (declare-function org-element-cache-refresh "org-element" (pos))
  154. (declare-function org-element-contents "org-element" (element))
  155. (declare-function org-element-context "org-element" (&optional element))
  156. (declare-function org-element-interpret-data "org-element"
  157. (data &optional parent))
  158. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  159. (declare-function org-element-parse-buffer "org-element"
  160. (&optional granularity visible-only))
  161. (declare-function org-element-property "org-element" (property element))
  162. (declare-function org-element-put-property "org-element"
  163. (element property value))
  164. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  165. (declare-function org-element-parse-buffer "org-element"
  166. (&optional granularity visible-only))
  167. (declare-function org-element-type "org-element" (element))
  168. (defsubst org-uniquify (list)
  169. "Non-destructively remove duplicate elements from LIST."
  170. (let ((res (copy-sequence list))) (delete-dups res)))
  171. (defsubst org-get-at-bol (property)
  172. "Get text property PROPERTY at the beginning of line."
  173. (get-text-property (point-at-bol) property))
  174. (defsubst org-trim (s)
  175. "Remove whitespace at the beginning and the end of string S."
  176. (replace-regexp-in-string
  177. "\\`[ \t\n\r]+" ""
  178. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  179. ;; load languages based on value of `org-babel-load-languages'
  180. (defvar org-babel-load-languages)
  181. ;;;###autoload
  182. (defun org-babel-do-load-languages (sym value)
  183. "Load the languages defined in `org-babel-load-languages'."
  184. (set-default sym value)
  185. (mapc (lambda (pair)
  186. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  187. (if active
  188. (progn
  189. (require (intern (concat "ob-" lang))))
  190. (progn
  191. (funcall 'fmakunbound
  192. (intern (concat "org-babel-execute:" lang)))
  193. (funcall 'fmakunbound
  194. (intern (concat "org-babel-expand-body:" lang)))))))
  195. org-babel-load-languages))
  196. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  197. ;;;###autoload
  198. (defun org-babel-load-file (file &optional compile)
  199. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  200. This function exports the source code using `org-babel-tangle'
  201. and then loads the resulting file using `load-file'. With prefix
  202. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  203. file to byte-code before it is loaded."
  204. (interactive "fFile to load: \nP")
  205. (let* ((age (lambda (file)
  206. (float-time
  207. (time-subtract (current-time)
  208. (nth 5 (or (file-attributes (file-truename file))
  209. (file-attributes file)))))))
  210. (base-name (file-name-sans-extension file))
  211. (exported-file (concat base-name ".el")))
  212. ;; tangle if the org-mode file is newer than the elisp file
  213. (unless (and (file-exists-p exported-file)
  214. (> (funcall age file) (funcall age exported-file)))
  215. (setq exported-file
  216. (car (org-babel-tangle-file file exported-file "emacs-lisp"))))
  217. (message "%s %s"
  218. (if compile
  219. (progn (byte-compile-file exported-file 'load)
  220. "Compiled and loaded")
  221. (progn (load-file exported-file) "Loaded"))
  222. exported-file)))
  223. (defcustom org-babel-load-languages '((emacs-lisp . t))
  224. "Languages which can be evaluated in Org-mode buffers.
  225. This list can be used to load support for any of the languages
  226. below, note that each language will depend on a different set of
  227. system executables and/or Emacs modes. When a language is
  228. \"loaded\", then code blocks in that language can be evaluated
  229. with `org-babel-execute-src-block' bound by default to C-c
  230. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  231. be set to remove code block evaluation from the C-c C-c
  232. keybinding. By default only Emacs Lisp (which has no
  233. requirements) is loaded."
  234. :group 'org-babel
  235. :set 'org-babel-do-load-languages
  236. :version "24.1"
  237. :type '(alist :tag "Babel Languages"
  238. :key-type
  239. (choice
  240. (const :tag "Awk" awk)
  241. (const :tag "C" C)
  242. (const :tag "R" R)
  243. (const :tag "Asymptote" asymptote)
  244. (const :tag "Calc" calc)
  245. (const :tag "Clojure" clojure)
  246. (const :tag "CSS" css)
  247. (const :tag "Ditaa" ditaa)
  248. (const :tag "Dot" dot)
  249. (const :tag "Emacs Lisp" emacs-lisp)
  250. (const :tag "Forth" forth)
  251. (const :tag "Fortran" fortran)
  252. (const :tag "Gnuplot" gnuplot)
  253. (const :tag "Haskell" haskell)
  254. (const :tag "IO" io)
  255. (const :tag "J" J)
  256. (const :tag "Java" java)
  257. (const :tag "Javascript" js)
  258. (const :tag "LaTeX" latex)
  259. (const :tag "Ledger" ledger)
  260. (const :tag "Lilypond" lilypond)
  261. (const :tag "Lisp" lisp)
  262. (const :tag "Makefile" makefile)
  263. (const :tag "Maxima" maxima)
  264. (const :tag "Matlab" matlab)
  265. (const :tag "Mscgen" mscgen)
  266. (const :tag "Ocaml" ocaml)
  267. (const :tag "Octave" octave)
  268. (const :tag "Org" org)
  269. (const :tag "Perl" perl)
  270. (const :tag "Pico Lisp" picolisp)
  271. (const :tag "PlantUML" plantuml)
  272. (const :tag "Python" python)
  273. (const :tag "Ruby" ruby)
  274. (const :tag "Sass" sass)
  275. (const :tag "Scala" scala)
  276. (const :tag "Scheme" scheme)
  277. (const :tag "Screen" screen)
  278. (const :tag "Shell Script" shell)
  279. (const :tag "Shen" shen)
  280. (const :tag "Sql" sql)
  281. (const :tag "Sqlite" sqlite)
  282. (const :tag "ebnf2ps" ebnf2ps))
  283. :value-type (boolean :tag "Activate" :value t)))
  284. ;;;; Customization variables
  285. (defcustom org-clone-delete-id nil
  286. "Remove ID property of clones of a subtree.
  287. When non-nil, clones of a subtree don't inherit the ID property.
  288. Otherwise they inherit the ID property with a new unique
  289. identifier."
  290. :type 'boolean
  291. :version "24.1"
  292. :group 'org-id)
  293. ;;; Version
  294. (org-check-version)
  295. ;;;###autoload
  296. (defun org-version (&optional here full message)
  297. "Show the org-mode version.
  298. Interactively, or when MESSAGE is non-nil, show it in echo area.
  299. With prefix argument, or when HERE is non-nil, insert it at point.
  300. In non-interactive uses, a reduced version string is output unless
  301. FULL is given."
  302. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  303. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  304. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  305. (load-suffixes (list ".el"))
  306. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  307. (org-trash (or
  308. (and (fboundp 'org-release) (fboundp 'org-git-version))
  309. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  310. (load-suffixes save-load-suffixes)
  311. (org-version (org-release))
  312. (git-version (org-git-version))
  313. (version (format "Org-mode version %s (%s @ %s)"
  314. org-version
  315. git-version
  316. (if org-install-dir
  317. (if (string= org-dir org-install-dir)
  318. org-install-dir
  319. (concat "mixed installation! " org-install-dir " and " org-dir))
  320. "org-loaddefs.el can not be found!")))
  321. (version1 (if full version org-version)))
  322. (when here (insert version1))
  323. (when message (message "%s" version1))
  324. version1))
  325. (defconst org-version (org-version))
  326. ;;; Syntax Constants
  327. ;;;; Block
  328. (defconst org-block-regexp
  329. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  330. "Regular expression for hiding blocks.")
  331. (defconst org-dblock-start-re
  332. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  333. "Matches the start line of a dynamic block, with parameters.")
  334. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  335. "Matches the end of a dynamic block.")
  336. ;;;; Clock and Planning
  337. (defconst org-clock-string "CLOCK:"
  338. "String used as prefix for timestamps clocking work hours on an item.")
  339. (defconst org-closed-string "CLOSED:"
  340. "String used as the prefix for timestamps logging closing a TODO entry.")
  341. (defconst org-deadline-string "DEADLINE:"
  342. "String to mark deadline entries.
  343. A deadline is this string, followed by a time stamp. Should be a word,
  344. terminated by a colon. You can insert a schedule keyword and
  345. a timestamp with \\[org-deadline].")
  346. (defconst org-scheduled-string "SCHEDULED:"
  347. "String to mark scheduled TODO entries.
  348. A schedule is this string, followed by a time stamp. Should be a word,
  349. terminated by a colon. You can insert a schedule keyword and
  350. a timestamp with \\[org-schedule].")
  351. (defconst org-planning-line-re
  352. (concat "^[ \t]*"
  353. (regexp-opt
  354. (list org-closed-string org-deadline-string org-scheduled-string)
  355. t))
  356. "Matches a line with planning info.
  357. Matched keyword is in group 1.")
  358. (defconst org-clock-line-re
  359. (concat "^[ \t]*" org-clock-string)
  360. "Matches a line with clock info.")
  361. ;;;; Drawer
  362. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  363. "Matches first or last line of a hidden block.
  364. Group 1 contains drawer's name or \"END\".")
  365. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  366. "Regular expression matching the first line of a property drawer.")
  367. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  368. "Regular expression matching the last line of a property drawer.")
  369. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  370. "Regular expression matching the first line of a clock drawer.")
  371. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  372. "Regular expression matching the last line of a clock drawer.")
  373. (defconst org-property-drawer-re
  374. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  375. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*"
  376. "[ \t]*:END:[ \t]*$")
  377. "Matches an entire property drawer.")
  378. (defconst org-clock-drawer-re
  379. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  380. org-clock-drawer-end-re "\\)\n?")
  381. "Matches an entire clock drawer.")
  382. ;;;; Headline
  383. (defconst org-heading-keyword-regexp-format
  384. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  385. "Printf format for a regexp matching a headline with some keyword.
  386. This regexp will match the headline of any node which has the
  387. exact keyword that is put into the format. The keyword isn't in
  388. any group by default, but the stars and the body are.")
  389. (defconst org-heading-keyword-maybe-regexp-format
  390. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  391. "Printf format for a regexp matching a headline, possibly with some keyword.
  392. This regexp can match any headline with the specified keyword, or
  393. without a keyword. The keyword isn't in any group by default,
  394. but the stars and the body are.")
  395. (defconst org-archive-tag "ARCHIVE"
  396. "The tag that marks a subtree as archived.
  397. An archived subtree does not open during visibility cycling, and does
  398. not contribute to the agenda listings.")
  399. (defconst org-comment-string "COMMENT"
  400. "Entries starting with this keyword will never be exported.
  401. An entry can be toggled between COMMENT and normal with
  402. \\[org-toggle-comment].")
  403. ;;;; LaTeX Environments and Fragments
  404. (defconst org-latex-regexps
  405. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  406. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  407. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  408. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  409. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  410. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  411. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  412. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  413. "Regular expressions for matching embedded LaTeX.")
  414. ;;;; Node Property
  415. (defconst org-effort-property "Effort"
  416. "The property that is being used to keep track of effort estimates.
  417. Effort estimates given in this property need to have the format H:MM.")
  418. ;;;; Table
  419. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  420. "Detect an org-type or table-type table.")
  421. (defconst org-table-line-regexp "^[ \t]*|"
  422. "Detect an org-type table line.")
  423. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  424. "Detect an org-type table line.")
  425. (defconst org-table-hline-regexp "^[ \t]*|-"
  426. "Detect an org-type table hline.")
  427. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  428. "Detect a table-type table hline.")
  429. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  430. "Detect the first line outside a table when searching from within it.
  431. This works for both table types.")
  432. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  433. "Detect a #+TBLFM line.")
  434. ;;;; Timestamp
  435. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  436. "Regular expression for fast time stamp matching.")
  437. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  438. "Regular expression for fast time stamp matching.")
  439. (defconst org-ts-regexp0
  440. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  441. "Regular expression matching time strings for analysis.
  442. This one does not require the space after the date, so it can be used
  443. on a string that terminates immediately after the date.")
  444. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  445. "Regular expression matching time strings for analysis.")
  446. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  447. "Regular expression matching time stamps, with groups.")
  448. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  449. "Regular expression matching time stamps (also [..]), with groups.")
  450. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  451. "Regular expression matching a time stamp range.")
  452. (defconst org-tr-regexp-both
  453. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  454. "Regular expression matching a time stamp range.")
  455. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  456. org-ts-regexp "\\)?")
  457. "Regular expression matching a time stamp or time stamp range.")
  458. (defconst org-tsr-regexp-both
  459. (concat org-ts-regexp-both "\\(--?-?"
  460. org-ts-regexp-both "\\)?")
  461. "Regular expression matching a time stamp or time stamp range.
  462. The time stamps may be either active or inactive.")
  463. (defconst org-repeat-re
  464. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  465. "Regular expression for specifying repeated events.
  466. After a match, group 1 contains the repeat expression.")
  467. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  468. "Formats for `format-time-string' which are used for time stamps.")
  469. ;;; The custom variables
  470. (defgroup org nil
  471. "Outline-based notes management and organizer."
  472. :tag "Org"
  473. :group 'outlines
  474. :group 'calendar)
  475. (defcustom org-mode-hook nil
  476. "Mode hook for Org-mode, run after the mode was turned on."
  477. :group 'org
  478. :type 'hook)
  479. (defcustom org-load-hook nil
  480. "Hook that is run after org.el has been loaded."
  481. :group 'org
  482. :type 'hook)
  483. (defcustom org-log-buffer-setup-hook nil
  484. "Hook that is run after an Org log buffer is created."
  485. :group 'org
  486. :version "24.1"
  487. :type 'hook)
  488. (defvar org-modules) ; defined below
  489. (defvar org-modules-loaded nil
  490. "Have the modules been loaded already?")
  491. (defun org-load-modules-maybe (&optional force)
  492. "Load all extensions listed in `org-modules'."
  493. (when (or force (not org-modules-loaded))
  494. (mapc (lambda (ext)
  495. (condition-case nil (require ext)
  496. (error (message "Problems while trying to load feature `%s'" ext))))
  497. org-modules)
  498. (setq org-modules-loaded t)))
  499. (defun org-set-modules (var value)
  500. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  501. (set var value)
  502. (when (featurep 'org)
  503. (org-load-modules-maybe 'force)
  504. (org-element-cache-reset 'all)))
  505. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  506. "Modules that should always be loaded together with org.el.
  507. If a description starts with <C>, the file is not part of Emacs
  508. and loading it will require that you have downloaded and properly
  509. installed the Org mode distribution.
  510. You can also use this system to load external packages (i.e. neither Org
  511. core modules, nor modules from the CONTRIB directory). Just add symbols
  512. to the end of the list. If the package is called org-xyz.el, then you need
  513. to add the symbol `xyz', and the package must have a call to:
  514. \(provide 'org-xyz)
  515. For export specific modules, see also `org-export-backends'."
  516. :group 'org
  517. :set 'org-set-modules
  518. :version "24.4"
  519. :package-version '(Org . "8.0")
  520. :type
  521. '(set :greedy t
  522. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  523. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  524. (const :tag " crypt: Encryption of subtrees" org-crypt)
  525. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  526. (const :tag " docview: Links to doc-view buffers" org-docview)
  527. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  528. (const :tag " habit: Track your consistency with habits" org-habit)
  529. (const :tag " id: Global IDs for identifying entries" org-id)
  530. (const :tag " info: Links to Info nodes" org-info)
  531. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  532. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  533. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  534. (const :tag " mouse: Additional mouse support" org-mouse)
  535. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  536. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  537. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  538. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  539. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  540. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  541. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  542. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  543. (const :tag "C collector: Collect properties into tables" org-collector)
  544. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  545. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  546. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  547. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  548. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  549. (const :tag "C eval: Include command output as text" org-eval)
  550. (const :tag "C eww: Store link to url of eww" org-eww)
  551. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  552. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  553. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  554. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  555. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  556. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  557. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  558. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  559. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  560. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  561. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  562. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  563. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  564. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  565. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  566. (const :tag "C registry: A registry for Org-mode links" org-registry)
  567. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  568. (const :tag "C secretary: Team management with org-mode" org-secretary)
  569. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  570. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  571. (const :tag "C track: Keep up with Org-mode development" org-track)
  572. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  573. (const :tag "C vm: Links to VM folders/messages" org-vm)
  574. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  575. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  576. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  577. (defvar org-export--registered-backends) ; From ox.el.
  578. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  579. (declare-function org-export-backend-name "ox" (backend))
  580. (defcustom org-export-backends '(ascii html icalendar latex)
  581. "List of export back-ends that should be always available.
  582. If a description starts with <C>, the file is not part of Emacs
  583. and loading it will require that you have downloaded and properly
  584. installed the Org mode distribution.
  585. Unlike to `org-modules', libraries in this list will not be
  586. loaded along with Org, but only once the export framework is
  587. needed.
  588. This variable needs to be set before org.el is loaded. If you
  589. need to make a change while Emacs is running, use the customize
  590. interface or run the following code, where VAL stands for the new
  591. value of the variable, after updating it:
  592. \(progn
  593. \(setq org-export--registered-backends
  594. \(org-remove-if-not
  595. \(lambda (backend)
  596. \(let ((name (org-export-backend-name backend)))
  597. \(or (memq name val)
  598. \(catch 'parentp
  599. \(dolist (b val)
  600. \(and (org-export-derived-backend-p b name)
  601. \(throw 'parentp t)))))))
  602. org-export--registered-backends))
  603. \(let ((new-list (mapcar 'org-export-backend-name
  604. org-export--registered-backends)))
  605. \(dolist (backend val)
  606. \(cond
  607. \((not (load (format \"ox-%s\" backend) t t))
  608. \(message \"Problems while trying to load export back-end `%s'\"
  609. backend))
  610. \((not (memq backend new-list)) (push backend new-list))))
  611. \(set-default 'org-export-backends new-list)))
  612. Adding a back-end to this list will also pull the back-end it
  613. depends on, if any."
  614. :group 'org
  615. :group 'org-export
  616. :version "24.4"
  617. :package-version '(Org . "8.0")
  618. :initialize 'custom-initialize-set
  619. :set (lambda (var val)
  620. (if (not (featurep 'ox)) (set-default var val)
  621. ;; Any back-end not required anymore (not present in VAL and not
  622. ;; a parent of any back-end in the new value) is removed from the
  623. ;; list of registered back-ends.
  624. (setq org-export--registered-backends
  625. (org-remove-if-not
  626. (lambda (backend)
  627. (let ((name (org-export-backend-name backend)))
  628. (or (memq name val)
  629. (catch 'parentp
  630. (dolist (b val)
  631. (and (org-export-derived-backend-p b name)
  632. (throw 'parentp t)))))))
  633. org-export--registered-backends))
  634. ;; Now build NEW-LIST of both new back-ends and required
  635. ;; parents.
  636. (let ((new-list (mapcar 'org-export-backend-name
  637. org-export--registered-backends)))
  638. (dolist (backend val)
  639. (cond
  640. ((not (load (format "ox-%s" backend) t t))
  641. (message "Problems while trying to load export back-end `%s'"
  642. backend))
  643. ((not (memq backend new-list)) (push backend new-list))))
  644. ;; Set VAR to that list with fixed dependencies.
  645. (set-default var new-list))))
  646. :type '(set :greedy t
  647. (const :tag " ascii Export buffer to ASCII format" ascii)
  648. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  649. (const :tag " html Export buffer to HTML format" html)
  650. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  651. (const :tag " latex Export buffer to LaTeX format" latex)
  652. (const :tag " man Export buffer to MAN format" man)
  653. (const :tag " md Export buffer to Markdown format" md)
  654. (const :tag " odt Export buffer to ODT format" odt)
  655. (const :tag " org Export buffer to Org format" org)
  656. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  657. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  658. (const :tag "C deck Export buffer to deck.js presentations" deck)
  659. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  660. (const :tag "C groff Export buffer to Groff format" groff)
  661. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  662. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  663. (const :tag "C s5 Export buffer to s5 presentations" s5)
  664. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  665. (eval-after-load 'ox
  666. '(mapc
  667. (lambda (backend)
  668. (condition-case nil (require (intern (format "ox-%s" backend)))
  669. (error (message "Problems while trying to load export back-end `%s'"
  670. backend))))
  671. org-export-backends))
  672. (defcustom org-support-shift-select nil
  673. "Non-nil means make shift-cursor commands select text when possible.
  674. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  675. start selecting a region, or enlarge regions started in this way.
  676. In Org-mode, in special contexts, these same keys are used for
  677. other purposes, important enough to compete with shift selection.
  678. Org tries to balance these needs by supporting `shift-select-mode'
  679. outside these special contexts, under control of this variable.
  680. The default of this variable is nil, to avoid confusing behavior. Shifted
  681. cursor keys will then execute Org commands in the following contexts:
  682. - on a headline, changing TODO state (left/right) and priority (up/down)
  683. - on a time stamp, changing the time
  684. - in a plain list item, changing the bullet type
  685. - in a property definition line, switching between allowed values
  686. - in the BEGIN line of a clock table (changing the time block).
  687. Outside these contexts, the commands will throw an error.
  688. When this variable is t and the cursor is not in a special
  689. context, Org-mode will support shift-selection for making and
  690. enlarging regions. To make this more effective, the bullet
  691. cycling will no longer happen anywhere in an item line, but only
  692. if the cursor is exactly on the bullet.
  693. If you set this variable to the symbol `always', then the keys
  694. will not be special in headlines, property lines, and item lines,
  695. to make shift selection work there as well. If this is what you
  696. want, you can use the following alternative commands: `C-c C-t'
  697. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  698. can be used to switch TODO sets, `C-c -' to cycle item bullet
  699. types, and properties can be edited by hand or in column view.
  700. However, when the cursor is on a timestamp, shift-cursor commands
  701. will still edit the time stamp - this is just too good to give up.
  702. XEmacs user should have this variable set to nil, because
  703. `shift-select-mode' is in Emacs 23 or later only."
  704. :group 'org
  705. :type '(choice
  706. (const :tag "Never" nil)
  707. (const :tag "When outside special context" t)
  708. (const :tag "Everywhere except timestamps" always)))
  709. (defcustom org-loop-over-headlines-in-active-region nil
  710. "Shall some commands act upon headlines in the active region?
  711. When set to `t', some commands will be performed in all headlines
  712. within the active region.
  713. When set to `start-level', some commands will be performed in all
  714. headlines within the active region, provided that these headlines
  715. are of the same level than the first one.
  716. When set to a string, those commands will be performed on the
  717. matching headlines within the active region. Such string must be
  718. a tags/property/todo match as it is used in the agenda tags view.
  719. The list of commands is: `org-schedule', `org-deadline',
  720. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  721. `org-archive-to-archive-sibling'. The archiving commands skip
  722. already archived entries."
  723. :type '(choice (const :tag "Don't loop" nil)
  724. (const :tag "All headlines in active region" t)
  725. (const :tag "In active region, headlines at the same level than the first one" start-level)
  726. (string :tag "Tags/Property/Todo matcher"))
  727. :version "24.1"
  728. :group 'org-todo
  729. :group 'org-archive)
  730. (defgroup org-startup nil
  731. "Options concerning startup of Org-mode."
  732. :tag "Org Startup"
  733. :group 'org)
  734. (defcustom org-startup-folded t
  735. "Non-nil means entering Org-mode will switch to OVERVIEW.
  736. This can also be configured on a per-file basis by adding one of
  737. the following lines anywhere in the buffer:
  738. #+STARTUP: fold (or `overview', this is equivalent)
  739. #+STARTUP: nofold (or `showall', this is equivalent)
  740. #+STARTUP: content
  741. #+STARTUP: showeverything
  742. By default, this option is ignored when Org opens agenda files
  743. for the first time. If you want the agenda to honor the startup
  744. option, set `org-agenda-inhibit-startup' to nil."
  745. :group 'org-startup
  746. :type '(choice
  747. (const :tag "nofold: show all" nil)
  748. (const :tag "fold: overview" t)
  749. (const :tag "content: all headlines" content)
  750. (const :tag "show everything, even drawers" showeverything)))
  751. (defcustom org-startup-truncated t
  752. "Non-nil means entering Org-mode will set `truncate-lines'.
  753. This is useful since some lines containing links can be very long and
  754. uninteresting. Also tables look terrible when wrapped."
  755. :group 'org-startup
  756. :type 'boolean)
  757. (defcustom org-startup-indented nil
  758. "Non-nil means turn on `org-indent-mode' on startup.
  759. This can also be configured on a per-file basis by adding one of
  760. the following lines anywhere in the buffer:
  761. #+STARTUP: indent
  762. #+STARTUP: noindent"
  763. :group 'org-structure
  764. :type '(choice
  765. (const :tag "Not" nil)
  766. (const :tag "Globally (slow on startup in large files)" t)))
  767. (defcustom org-use-sub-superscripts t
  768. "Non-nil means interpret \"_\" and \"^\" for display.
  769. If you want to control how Org exports those characters, see
  770. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  771. used to be an alias for `org-export-with-sub-superscripts' in
  772. Org <8.0, it is not anymore.
  773. When this option is turned on, you can use TeX-like syntax for
  774. sub- and superscripts within the buffer. Several characters after
  775. \"_\" or \"^\" will be considered as a single item - so grouping
  776. with {} is normally not needed. For example, the following things
  777. will be parsed as single sub- or superscripts:
  778. 10^24 or 10^tau several digits will be considered 1 item.
  779. 10^-12 or 10^-tau a leading sign with digits or a word
  780. x^2-y^3 will be read as x^2 - y^3, because items are
  781. terminated by almost any nonword/nondigit char.
  782. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  783. Still, ambiguity is possible. So when in doubt, use {} to enclose
  784. the sub/superscript. If you set this variable to the symbol `{}',
  785. the braces are *required* in order to trigger interpretations as
  786. sub/superscript. This can be helpful in documents that need \"_\"
  787. frequently in plain text."
  788. :group 'org-startup
  789. :version "24.4"
  790. :package-version '(Org . "8.0")
  791. :type '(choice
  792. (const :tag "Always interpret" t)
  793. (const :tag "Only with braces" {})
  794. (const :tag "Never interpret" nil)))
  795. (defcustom org-startup-with-beamer-mode nil
  796. "Non-nil means turn on `org-beamer-mode' on startup.
  797. This can also be configured on a per-file basis by adding one of
  798. the following lines anywhere in the buffer:
  799. #+STARTUP: beamer"
  800. :group 'org-startup
  801. :version "24.1"
  802. :type 'boolean)
  803. (defcustom org-startup-align-all-tables nil
  804. "Non-nil means align all tables when visiting a file.
  805. This is useful when the column width in tables is forced with <N> cookies
  806. in table fields. Such tables will look correct only after the first re-align.
  807. This can also be configured on a per-file basis by adding one of
  808. the following lines anywhere in the buffer:
  809. #+STARTUP: align
  810. #+STARTUP: noalign"
  811. :group 'org-startup
  812. :type 'boolean)
  813. (defcustom org-startup-with-inline-images nil
  814. "Non-nil means show inline images when loading a new Org file.
  815. This can also be configured on a per-file basis by adding one of
  816. the following lines anywhere in the buffer:
  817. #+STARTUP: inlineimages
  818. #+STARTUP: noinlineimages"
  819. :group 'org-startup
  820. :version "24.1"
  821. :type 'boolean)
  822. (defcustom org-startup-with-latex-preview nil
  823. "Non-nil means preview LaTeX fragments when loading a new Org file.
  824. This can also be configured on a per-file basis by adding one of
  825. the following lines anywhere in the buffer:
  826. #+STARTUP: latexpreview
  827. #+STARTUP: nolatexpreview"
  828. :group 'org-startup
  829. :version "24.4"
  830. :package-version '(Org . "8.0")
  831. :type 'boolean)
  832. (defcustom org-insert-mode-line-in-empty-file nil
  833. "Non-nil means insert the first line setting Org-mode in empty files.
  834. When the function `org-mode' is called interactively in an empty file, this
  835. normally means that the file name does not automatically trigger Org-mode.
  836. To ensure that the file will always be in Org-mode in the future, a
  837. line enforcing Org-mode will be inserted into the buffer, if this option
  838. has been set."
  839. :group 'org-startup
  840. :type 'boolean)
  841. (defcustom org-replace-disputed-keys nil
  842. "Non-nil means use alternative key bindings for some keys.
  843. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  844. These keys are also used by other packages like shift-selection-mode'
  845. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  846. If you want to use Org-mode together with one of these other modes,
  847. or more generally if you would like to move some Org-mode commands to
  848. other keys, set this variable and configure the keys with the variable
  849. `org-disputed-keys'.
  850. This option is only relevant at load-time of Org-mode, and must be set
  851. *before* org.el is loaded. Changing it requires a restart of Emacs to
  852. become effective."
  853. :group 'org-startup
  854. :type 'boolean)
  855. (defcustom org-use-extra-keys nil
  856. "Non-nil means use extra key sequence definitions for certain commands.
  857. This happens automatically if you run XEmacs or if `window-system'
  858. is nil. This variable lets you do the same manually. You must
  859. set it before loading org.
  860. Example: on Carbon Emacs 22 running graphically, with an external
  861. keyboard on a Powerbook, the default way of setting M-left might
  862. not work for either Alt or ESC. Setting this variable will make
  863. it work for ESC."
  864. :group 'org-startup
  865. :type 'boolean)
  866. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  867. (defcustom org-disputed-keys
  868. '(([(shift up)] . [(meta p)])
  869. ([(shift down)] . [(meta n)])
  870. ([(shift left)] . [(meta -)])
  871. ([(shift right)] . [(meta +)])
  872. ([(control shift right)] . [(meta shift +)])
  873. ([(control shift left)] . [(meta shift -)]))
  874. "Keys for which Org-mode and other modes compete.
  875. This is an alist, cars are the default keys, second element specifies
  876. the alternative to use when `org-replace-disputed-keys' is t.
  877. Keys can be specified in any syntax supported by `define-key'.
  878. The value of this option takes effect only at Org-mode's startup,
  879. therefore you'll have to restart Emacs to apply it after changing."
  880. :group 'org-startup
  881. :type 'alist)
  882. (defun org-key (key)
  883. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  884. Or return the original if not disputed.
  885. Also apply the translations defined in `org-xemacs-key-equivalents'."
  886. (when org-replace-disputed-keys
  887. (let* ((nkey (key-description key))
  888. (x (org-find-if (lambda (x)
  889. (equal (key-description (car x)) nkey))
  890. org-disputed-keys)))
  891. (setq key (if x (cdr x) key))))
  892. (when (featurep 'xemacs)
  893. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  894. key)
  895. (defun org-find-if (predicate seq)
  896. (catch 'exit
  897. (while seq
  898. (if (funcall predicate (car seq))
  899. (throw 'exit (car seq))
  900. (pop seq)))))
  901. (defun org-defkey (keymap key def)
  902. "Define a key, possibly translated, as returned by `org-key'."
  903. (define-key keymap (org-key key) def))
  904. (defcustom org-ellipsis nil
  905. "The ellipsis to use in the Org-mode outline.
  906. When nil, just use the standard three dots.
  907. When a string, use that string instead.
  908. When a face, use the standard 3 dots, but with the specified face.
  909. The change affects only Org-mode (which will then use its own display table).
  910. Changing this requires executing \\[org-mode] in a buffer to become
  911. effective."
  912. :group 'org-startup
  913. :type '(choice (const :tag "Default" nil)
  914. (face :tag "Face" :value org-warning)
  915. (string :tag "String" :value "...#")))
  916. (defvar org-display-table nil
  917. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  918. (defgroup org-keywords nil
  919. "Keywords in Org-mode."
  920. :tag "Org Keywords"
  921. :group 'org)
  922. (defcustom org-closed-keep-when-no-todo nil
  923. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  924. :group 'org-todo
  925. :group 'org-keywords
  926. :version "24.4"
  927. :package-version '(Org . "8.0")
  928. :type 'boolean)
  929. (defgroup org-structure nil
  930. "Options concerning the general structure of Org-mode files."
  931. :tag "Org Structure"
  932. :group 'org)
  933. (defgroup org-reveal-location nil
  934. "Options about how to make context of a location visible."
  935. :tag "Org Reveal Location"
  936. :group 'org-structure)
  937. (defconst org-context-choice
  938. '(choice
  939. (const :tag "Always" t)
  940. (const :tag "Never" nil)
  941. (repeat :greedy t :tag "Individual contexts"
  942. (cons
  943. (choice :tag "Context"
  944. (const agenda)
  945. (const org-goto)
  946. (const occur-tree)
  947. (const tags-tree)
  948. (const link-search)
  949. (const mark-goto)
  950. (const bookmark-jump)
  951. (const isearch)
  952. (const default))
  953. (boolean))))
  954. "Contexts for the reveal options.")
  955. (defcustom org-show-hierarchy-above '((default . t))
  956. "Non-nil means show full hierarchy when revealing a location.
  957. Org-mode often shows locations in an org-mode file which might have
  958. been invisible before. When this is set, the hierarchy of headings
  959. above the exposed location is shown.
  960. Turning this off for example for sparse trees makes them very compact.
  961. Instead of t, this can also be an alist specifying this option for different
  962. contexts. Valid contexts are
  963. agenda when exposing an entry from the agenda
  964. org-goto when using the command `org-goto' on key C-c C-j
  965. occur-tree when using the command `org-occur' on key C-c /
  966. tags-tree when constructing a sparse tree based on tags matches
  967. link-search when exposing search matches associated with a link
  968. mark-goto when exposing the jump goal of a mark
  969. bookmark-jump when exposing a bookmark location
  970. isearch when exiting from an incremental search
  971. default default for all contexts not set explicitly"
  972. :group 'org-reveal-location
  973. :type org-context-choice)
  974. (defcustom org-show-following-heading '((default . nil))
  975. "Non-nil means show following heading when revealing a location.
  976. Org-mode often shows locations in an org-mode file which might have
  977. been invisible before. When this is set, the heading following the
  978. match is shown.
  979. Turning this off for example for sparse trees makes them very compact,
  980. but makes it harder to edit the location of the match. In such a case,
  981. use the command \\[org-reveal] to show more context.
  982. Instead of t, this can also be an alist specifying this option for different
  983. contexts. See `org-show-hierarchy-above' for valid contexts."
  984. :group 'org-reveal-location
  985. :type org-context-choice)
  986. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  987. "Non-nil means show all sibling heading when revealing a location.
  988. Org-mode often shows locations in an org-mode file which might have
  989. been invisible before. When this is set, the sibling of the current entry
  990. heading are all made visible. If `org-show-hierarchy-above' is t,
  991. the same happens on each level of the hierarchy above the current entry.
  992. By default this is on for the isearch context, off for all other contexts.
  993. Turning this off for example for sparse trees makes them very compact,
  994. but makes it harder to edit the location of the match. In such a case,
  995. use the command \\[org-reveal] to show more context.
  996. Instead of t, this can also be an alist specifying this option for different
  997. contexts. See `org-show-hierarchy-above' for valid contexts."
  998. :group 'org-reveal-location
  999. :type org-context-choice
  1000. :version "24.4"
  1001. :package-version '(Org . "8.0"))
  1002. (defcustom org-show-entry-below '((default . nil))
  1003. "Non-nil means show the entry below a headline when revealing a location.
  1004. Org-mode often shows locations in an org-mode file which might have
  1005. been invisible before. When this is set, the text below the headline that is
  1006. exposed is also shown.
  1007. By default this is off for all contexts.
  1008. Instead of t, this can also be an alist specifying this option for different
  1009. contexts. See `org-show-hierarchy-above' for valid contexts."
  1010. :group 'org-reveal-location
  1011. :type org-context-choice)
  1012. (defcustom org-indirect-buffer-display 'other-window
  1013. "How should indirect tree buffers be displayed?
  1014. This applies to indirect buffers created with the commands
  1015. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1016. Valid values are:
  1017. current-window Display in the current window
  1018. other-window Just display in another window.
  1019. dedicated-frame Create one new frame, and re-use it each time.
  1020. new-frame Make a new frame each time. Note that in this case
  1021. previously-made indirect buffers are kept, and you need to
  1022. kill these buffers yourself."
  1023. :group 'org-structure
  1024. :group 'org-agenda-windows
  1025. :type '(choice
  1026. (const :tag "In current window" current-window)
  1027. (const :tag "In current frame, other window" other-window)
  1028. (const :tag "Each time a new frame" new-frame)
  1029. (const :tag "One dedicated frame" dedicated-frame)))
  1030. (defcustom org-use-speed-commands nil
  1031. "Non-nil means activate single letter commands at beginning of a headline.
  1032. This may also be a function to test for appropriate locations where speed
  1033. commands should be active.
  1034. For example, to activate speed commands when the point is on any
  1035. star at the beginning of the headline, you can do this:
  1036. (setq org-use-speed-commands
  1037. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1038. :group 'org-structure
  1039. :type '(choice
  1040. (const :tag "Never" nil)
  1041. (const :tag "At beginning of headline stars" t)
  1042. (function)))
  1043. (defcustom org-speed-commands-user nil
  1044. "Alist of additional speed commands.
  1045. This list will be checked before `org-speed-commands-default'
  1046. when the variable `org-use-speed-commands' is non-nil
  1047. and when the cursor is at the beginning of a headline.
  1048. The car if each entry is a string with a single letter, which must
  1049. be assigned to `self-insert-command' in the global map.
  1050. The cdr is either a command to be called interactively, a function
  1051. to be called, or a form to be evaluated.
  1052. An entry that is just a list with a single string will be interpreted
  1053. as a descriptive headline that will be added when listing the speed
  1054. commands in the Help buffer using the `?' speed command."
  1055. :group 'org-structure
  1056. :type '(repeat :value ("k" . ignore)
  1057. (choice :value ("k" . ignore)
  1058. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1059. (cons :tag "Letter and Command"
  1060. (string :tag "Command letter")
  1061. (choice
  1062. (function)
  1063. (sexp))))))
  1064. (defcustom org-bookmark-names-plist
  1065. '(:last-capture "org-capture-last-stored"
  1066. :last-refile "org-refile-last-stored"
  1067. :last-capture-marker "org-capture-last-stored-marker")
  1068. "Names for bookmarks automatically set by some Org commands.
  1069. This can provide strings as names for a number of bookmarks Org sets
  1070. automatically. The following keys are currently implemented:
  1071. :last-capture
  1072. :last-capture-marker
  1073. :last-refile
  1074. When a key does not show up in the property list, the corresponding bookmark
  1075. is not set."
  1076. :group 'org-structure
  1077. :type 'plist)
  1078. (defgroup org-cycle nil
  1079. "Options concerning visibility cycling in Org-mode."
  1080. :tag "Org Cycle"
  1081. :group 'org-structure)
  1082. (defcustom org-cycle-skip-children-state-if-no-children t
  1083. "Non-nil means skip CHILDREN state in entries that don't have any."
  1084. :group 'org-cycle
  1085. :type 'boolean)
  1086. (defcustom org-cycle-max-level nil
  1087. "Maximum level which should still be subject to visibility cycling.
  1088. Levels higher than this will, for cycling, be treated as text, not a headline.
  1089. When `org-odd-levels-only' is set, a value of N in this variable actually
  1090. means 2N-1 stars as the limiting headline.
  1091. When nil, cycle all levels.
  1092. Note that the limiting level of cycling is also influenced by
  1093. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1094. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1095. than its value."
  1096. :group 'org-cycle
  1097. :type '(choice
  1098. (const :tag "No limit" nil)
  1099. (integer :tag "Maximum level")))
  1100. (defcustom org-hide-block-startup nil
  1101. "Non-nil means entering Org-mode will fold all blocks.
  1102. This can also be set in on a per-file basis with
  1103. #+STARTUP: hideblocks
  1104. #+STARTUP: showblocks"
  1105. :group 'org-startup
  1106. :group 'org-cycle
  1107. :type 'boolean)
  1108. (defcustom org-cycle-global-at-bob nil
  1109. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1110. This makes it possible to do global cycling without having to use S-TAB or
  1111. \\[universal-argument] TAB. For this special case to work, the first line
  1112. of the buffer must not be a headline -- it may be empty or some other text.
  1113. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1114. sure the cursor stays at the beginning of the buffer. When this option is
  1115. nil, don't do anything special at the beginning of the buffer."
  1116. :group 'org-cycle
  1117. :type 'boolean)
  1118. (defcustom org-cycle-level-after-item/entry-creation t
  1119. "Non-nil means cycle entry level or item indentation in new empty entries.
  1120. When the cursor is at the end of an empty headline, i.e., with only stars
  1121. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1122. and then all possible ancestor states, before returning to the original state.
  1123. This makes data entry extremely fast: M-RET to create a new headline,
  1124. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1125. When the cursor is at the end of an empty plain list item, one TAB will
  1126. make it a subitem, two or more tabs will back up to make this an item
  1127. higher up in the item hierarchy."
  1128. :group 'org-cycle
  1129. :type 'boolean)
  1130. (defcustom org-cycle-emulate-tab t
  1131. "Where should `org-cycle' emulate TAB.
  1132. nil Never
  1133. white Only in completely white lines
  1134. whitestart Only at the beginning of lines, before the first non-white char
  1135. t Everywhere except in headlines
  1136. exc-hl-bol Everywhere except at the start of a headline
  1137. If TAB is used in a place where it does not emulate TAB, the current subtree
  1138. visibility is cycled."
  1139. :group 'org-cycle
  1140. :type '(choice (const :tag "Never" nil)
  1141. (const :tag "Only in completely white lines" white)
  1142. (const :tag "Before first char in a line" whitestart)
  1143. (const :tag "Everywhere except in headlines" t)
  1144. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1145. (defcustom org-cycle-separator-lines 2
  1146. "Number of empty lines needed to keep an empty line between collapsed trees.
  1147. If you leave an empty line between the end of a subtree and the following
  1148. headline, this empty line is hidden when the subtree is folded.
  1149. Org-mode will leave (exactly) one empty line visible if the number of
  1150. empty lines is equal or larger to the number given in this variable.
  1151. So the default 2 means at least 2 empty lines after the end of a subtree
  1152. are needed to produce free space between a collapsed subtree and the
  1153. following headline.
  1154. If the number is negative, and the number of empty lines is at least -N,
  1155. all empty lines are shown.
  1156. Special case: when 0, never leave empty lines in collapsed view."
  1157. :group 'org-cycle
  1158. :type 'integer)
  1159. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1160. (defcustom org-pre-cycle-hook nil
  1161. "Hook that is run before visibility cycling is happening.
  1162. The function(s) in this hook must accept a single argument which indicates
  1163. the new state that will be set right after running this hook. The
  1164. argument is a symbol. Before a global state change, it can have the values
  1165. `overview', `content', or `all'. Before a local state change, it can have
  1166. the values `folded', `children', or `subtree'."
  1167. :group 'org-cycle
  1168. :type 'hook)
  1169. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1170. org-cycle-hide-drawers
  1171. org-cycle-hide-inline-tasks
  1172. org-cycle-show-empty-lines
  1173. org-optimize-window-after-visibility-change)
  1174. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1175. The function(s) in this hook must accept a single argument which indicates
  1176. the new state that was set by the most recent `org-cycle' command. The
  1177. argument is a symbol. After a global state change, it can have the values
  1178. `overview', `contents', or `all'. After a local state change, it can have
  1179. the values `folded', `children', or `subtree'."
  1180. :group 'org-cycle
  1181. :type 'hook)
  1182. (defgroup org-edit-structure nil
  1183. "Options concerning structure editing in Org-mode."
  1184. :tag "Org Edit Structure"
  1185. :group 'org-structure)
  1186. (defcustom org-odd-levels-only nil
  1187. "Non-nil means skip even levels and only use odd levels for the outline.
  1188. This has the effect that two stars are being added/taken away in
  1189. promotion/demotion commands. It also influences how levels are
  1190. handled by the exporters.
  1191. Changing it requires restart of `font-lock-mode' to become effective
  1192. for fontification also in regions already fontified.
  1193. You may also set this on a per-file basis by adding one of the following
  1194. lines to the buffer:
  1195. #+STARTUP: odd
  1196. #+STARTUP: oddeven"
  1197. :group 'org-edit-structure
  1198. :group 'org-appearance
  1199. :type 'boolean)
  1200. (defcustom org-adapt-indentation t
  1201. "Non-nil means adapt indentation to outline node level.
  1202. When this variable is set, Org assumes that you write outlines by
  1203. indenting text in each node to align with the headline (after the stars).
  1204. The following issues are influenced by this variable:
  1205. - When this is set and the *entire* text in an entry is indented, the
  1206. indentation is increased by one space in a demotion command, and
  1207. decreased by one in a promotion command. If any line in the entry
  1208. body starts with text at column 0, indentation is not changed at all.
  1209. - Property drawers and planning information is inserted indented when
  1210. this variable s set. When nil, they will not be indented.
  1211. - TAB indents a line relative to context. The lines below a headline
  1212. will be indented when this variable is set.
  1213. Note that this is all about true indentation, by adding and removing
  1214. space characters. See also `org-indent.el' which does level-dependent
  1215. indentation in a virtual way, i.e. at display time in Emacs."
  1216. :group 'org-edit-structure
  1217. :type 'boolean)
  1218. (defcustom org-special-ctrl-a/e nil
  1219. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1220. When t, `C-a' will bring back the cursor to the beginning of the
  1221. headline text, i.e. after the stars and after a possible TODO
  1222. keyword. In an item, this will be the position after bullet and
  1223. check-box, if any. When the cursor is already at that position,
  1224. another `C-a' will bring it to the beginning of the line.
  1225. `C-e' will jump to the end of the headline, ignoring the presence
  1226. of tags in the headline. A second `C-e' will then jump to the
  1227. true end of the line, after any tags. This also means that, when
  1228. this variable is non-nil, `C-e' also will never jump beyond the
  1229. end of the heading of a folded section, i.e. not after the
  1230. ellipses.
  1231. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1232. normally, going to the true line boundary first. Only a directly
  1233. following, identical keypress will bring the cursor to the
  1234. special positions.
  1235. This may also be a cons cell where the behavior for `C-a' and
  1236. `C-e' is set separately."
  1237. :group 'org-edit-structure
  1238. :type '(choice
  1239. (const :tag "off" nil)
  1240. (const :tag "on: after stars/bullet and before tags first" t)
  1241. (const :tag "reversed: true line boundary first" reversed)
  1242. (cons :tag "Set C-a and C-e separately"
  1243. (choice :tag "Special C-a"
  1244. (const :tag "off" nil)
  1245. (const :tag "on: after stars/bullet first" t)
  1246. (const :tag "reversed: before stars/bullet first" reversed))
  1247. (choice :tag "Special C-e"
  1248. (const :tag "off" nil)
  1249. (const :tag "on: before tags first" t)
  1250. (const :tag "reversed: after tags first" reversed)))))
  1251. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1252. (defcustom org-special-ctrl-k nil
  1253. "Non-nil means `C-k' will behave specially in headlines.
  1254. When nil, `C-k' will call the default `kill-line' command.
  1255. When t, the following will happen while the cursor is in the headline:
  1256. - When the cursor is at the beginning of a headline, kill the entire
  1257. line and possible the folded subtree below the line.
  1258. - When in the middle of the headline text, kill the headline up to the tags.
  1259. - When after the headline text, kill the tags."
  1260. :group 'org-edit-structure
  1261. :type 'boolean)
  1262. (defcustom org-ctrl-k-protect-subtree nil
  1263. "Non-nil means, do not delete a hidden subtree with C-k.
  1264. When set to the symbol `error', simply throw an error when C-k is
  1265. used to kill (part-of) a headline that has hidden text behind it.
  1266. Any other non-nil value will result in a query to the user, if it is
  1267. OK to kill that hidden subtree. When nil, kill without remorse."
  1268. :group 'org-edit-structure
  1269. :version "24.1"
  1270. :type '(choice
  1271. (const :tag "Do not protect hidden subtrees" nil)
  1272. (const :tag "Protect hidden subtrees with a security query" t)
  1273. (const :tag "Never kill a hidden subtree with C-k" error)))
  1274. (defcustom org-special-ctrl-o t
  1275. "Non-nil means, make `C-o' insert a row in tables."
  1276. :group 'org-edit-structure
  1277. :type 'boolean)
  1278. (defcustom org-catch-invisible-edits nil
  1279. "Check if in invisible region before inserting or deleting a character.
  1280. Valid values are:
  1281. nil Do not check, so just do invisible edits.
  1282. error Throw an error and do nothing.
  1283. show Make point visible, and do the requested edit.
  1284. show-and-error Make point visible, then throw an error and abort the edit.
  1285. smart Make point visible, and do insertion/deletion if it is
  1286. adjacent to visible text and the change feels predictable.
  1287. Never delete a previously invisible character or add in the
  1288. middle or right after an invisible region. Basically, this
  1289. allows insertion and backward-delete right before ellipses.
  1290. FIXME: maybe in this case we should not even show?"
  1291. :group 'org-edit-structure
  1292. :version "24.1"
  1293. :type '(choice
  1294. (const :tag "Do not check" nil)
  1295. (const :tag "Throw error when trying to edit" error)
  1296. (const :tag "Unhide, but do not do the edit" show-and-error)
  1297. (const :tag "Show invisible part and do the edit" show)
  1298. (const :tag "Be smart and do the right thing" smart)))
  1299. (defcustom org-yank-folded-subtrees t
  1300. "Non-nil means when yanking subtrees, fold them.
  1301. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1302. it starts with a heading and all other headings in it are either children
  1303. or siblings, then fold all the subtrees. However, do this only if no
  1304. text after the yank would be swallowed into a folded tree by this action."
  1305. :group 'org-edit-structure
  1306. :type 'boolean)
  1307. (defcustom org-yank-adjusted-subtrees nil
  1308. "Non-nil means when yanking subtrees, adjust the level.
  1309. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1310. this function for details."
  1311. :group 'org-edit-structure
  1312. :type 'boolean)
  1313. (defcustom org-M-RET-may-split-line '((default . t))
  1314. "Non-nil means M-RET will split the line at the cursor position.
  1315. When nil, it will go to the end of the line before making a
  1316. new line.
  1317. You may also set this option in a different way for different
  1318. contexts. Valid contexts are:
  1319. headline when creating a new headline
  1320. item when creating a new item
  1321. table in a table field
  1322. default the value to be used for all contexts not explicitly
  1323. customized"
  1324. :group 'org-structure
  1325. :group 'org-table
  1326. :type '(choice
  1327. (const :tag "Always" t)
  1328. (const :tag "Never" nil)
  1329. (repeat :greedy t :tag "Individual contexts"
  1330. (cons
  1331. (choice :tag "Context"
  1332. (const headline)
  1333. (const item)
  1334. (const table)
  1335. (const default))
  1336. (boolean)))))
  1337. (defcustom org-insert-heading-respect-content nil
  1338. "Non-nil means insert new headings after the current subtree.
  1339. When nil, the new heading is created directly after the current line.
  1340. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1341. this variable on for the duration of the command."
  1342. :group 'org-structure
  1343. :type 'boolean)
  1344. (defcustom org-blank-before-new-entry '((heading . auto)
  1345. (plain-list-item . auto))
  1346. "Should `org-insert-heading' leave a blank line before new heading/item?
  1347. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1348. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1349. which case Org will look at the surrounding headings/items and try to
  1350. make an intelligent decision whether to insert a blank line or not.
  1351. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1352. the setting here is ignored and no empty line is inserted to avoid breaking
  1353. the list structure."
  1354. :group 'org-edit-structure
  1355. :type '(list
  1356. (cons (const heading)
  1357. (choice (const :tag "Never" nil)
  1358. (const :tag "Always" t)
  1359. (const :tag "Auto" auto)))
  1360. (cons (const plain-list-item)
  1361. (choice (const :tag "Never" nil)
  1362. (const :tag "Always" t)
  1363. (const :tag "Auto" auto)))))
  1364. (defcustom org-insert-heading-hook nil
  1365. "Hook being run after inserting a new heading."
  1366. :group 'org-edit-structure
  1367. :type 'hook)
  1368. (defcustom org-enable-fixed-width-editor t
  1369. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1370. This currently only means they are never auto-wrapped.
  1371. When nil, such lines will be treated like ordinary lines."
  1372. :group 'org-edit-structure
  1373. :type 'boolean)
  1374. (defcustom org-goto-auto-isearch t
  1375. "Non-nil means typing characters in `org-goto' starts incremental search.
  1376. When nil, you can use these keybindings to navigate the buffer:
  1377. q Quit the org-goto interface
  1378. n Go to the next visible heading
  1379. p Go to the previous visible heading
  1380. f Go one heading forward on same level
  1381. b Go one heading backward on same level
  1382. u Go one heading up"
  1383. :group 'org-edit-structure
  1384. :type 'boolean)
  1385. (defgroup org-sparse-trees nil
  1386. "Options concerning sparse trees in Org-mode."
  1387. :tag "Org Sparse Trees"
  1388. :group 'org-structure)
  1389. (defcustom org-highlight-sparse-tree-matches t
  1390. "Non-nil means highlight all matches that define a sparse tree.
  1391. The highlights will automatically disappear the next time the buffer is
  1392. changed by an edit command."
  1393. :group 'org-sparse-trees
  1394. :type 'boolean)
  1395. (defcustom org-remove-highlights-with-change t
  1396. "Non-nil means any change to the buffer will remove temporary highlights.
  1397. Such highlights are created by `org-occur' and `org-clock-display'.
  1398. When nil, `C-c C-c' needs to be used to get rid of the highlights.
  1399. The highlights created by `org-toggle-latex-fragment' always need
  1400. `C-c C-x C-l' to be removed."
  1401. :group 'org-sparse-trees
  1402. :group 'org-time
  1403. :type 'boolean)
  1404. (defcustom org-occur-hook '(org-first-headline-recenter)
  1405. "Hook that is run after `org-occur' has constructed a sparse tree.
  1406. This can be used to recenter the window to show as much of the structure
  1407. as possible."
  1408. :group 'org-sparse-trees
  1409. :type 'hook)
  1410. (defgroup org-imenu-and-speedbar nil
  1411. "Options concerning imenu and speedbar in Org-mode."
  1412. :tag "Org Imenu and Speedbar"
  1413. :group 'org-structure)
  1414. (defcustom org-imenu-depth 2
  1415. "The maximum level for Imenu access to Org-mode headlines.
  1416. This also applied for speedbar access."
  1417. :group 'org-imenu-and-speedbar
  1418. :type 'integer)
  1419. (defgroup org-table nil
  1420. "Options concerning tables in Org-mode."
  1421. :tag "Org Table"
  1422. :group 'org)
  1423. (defcustom org-enable-table-editor 'optimized
  1424. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1425. When nil, such lines will be treated like ordinary lines.
  1426. When equal to the symbol `optimized', the table editor will be optimized to
  1427. do the following:
  1428. - Automatic overwrite mode in front of whitespace in table fields.
  1429. This makes the structure of the table stay in tact as long as the edited
  1430. field does not exceed the column width.
  1431. - Minimize the number of realigns. Normally, the table is aligned each time
  1432. TAB or RET are pressed to move to another field. With optimization this
  1433. happens only if changes to a field might have changed the column width.
  1434. Optimization requires replacing the functions `self-insert-command',
  1435. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1436. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1437. very good at guessing when a re-align will be necessary, but you can always
  1438. force one with \\[org-ctrl-c-ctrl-c].
  1439. If you would like to use the optimized version in Org-mode, but the
  1440. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1441. This variable can be used to turn on and off the table editor during a session,
  1442. but in order to toggle optimization, a restart is required.
  1443. See also the variable `org-table-auto-blank-field'."
  1444. :group 'org-table
  1445. :type '(choice
  1446. (const :tag "off" nil)
  1447. (const :tag "on" t)
  1448. (const :tag "on, optimized" optimized)))
  1449. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1450. (version<= emacs-version "24.1"))
  1451. "Non-nil means cluster self-insert commands for undo when possible.
  1452. If this is set, then, like in the Emacs command loop, 20 consecutive
  1453. characters will be undone together.
  1454. This is configurable, because there is some impact on typing performance."
  1455. :group 'org-table
  1456. :type 'boolean)
  1457. (defcustom org-table-tab-recognizes-table.el t
  1458. "Non-nil means TAB will automatically notice a table.el table.
  1459. When it sees such a table, it moves point into it and - if necessary -
  1460. calls `table-recognize-table'."
  1461. :group 'org-table-editing
  1462. :type 'boolean)
  1463. (defgroup org-link nil
  1464. "Options concerning links in Org-mode."
  1465. :tag "Org Link"
  1466. :group 'org)
  1467. (defvar org-link-abbrev-alist-local nil
  1468. "Buffer-local version of `org-link-abbrev-alist', which see.
  1469. The value of this is taken from the #+LINK lines.")
  1470. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1471. (defcustom org-link-abbrev-alist nil
  1472. "Alist of link abbreviations.
  1473. The car of each element is a string, to be replaced at the start of a link.
  1474. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1475. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1476. [[linkkey:tag][description]]
  1477. The 'linkkey' must be a word word, starting with a letter, followed
  1478. by letters, numbers, '-' or '_'.
  1479. If REPLACE is a string, the tag will simply be appended to create the link.
  1480. If the string contains \"%s\", the tag will be inserted there. If the string
  1481. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1482. at that point (see the function `url-hexify-string'). If the string contains
  1483. the specifier \"%(my-function)\", then the custom function `my-function' will
  1484. be invoked: this function takes the tag as its only argument and must return
  1485. a string.
  1486. REPLACE may also be a function that will be called with the tag as the
  1487. only argument to create the link, which should be returned as a string.
  1488. See the manual for examples."
  1489. :group 'org-link
  1490. :type '(repeat
  1491. (cons
  1492. (string :tag "Protocol")
  1493. (choice
  1494. (string :tag "Format")
  1495. (function)))))
  1496. (defcustom org-descriptive-links t
  1497. "Non-nil means Org will display descriptive links.
  1498. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1499. \"Org Website\", hiding the link itself and just displaying its
  1500. description. When set to `nil', Org will display the full links
  1501. literally.
  1502. You can interactively set the value of this variable by calling
  1503. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1504. :group 'org-link
  1505. :type 'boolean)
  1506. (defcustom org-link-file-path-type 'adaptive
  1507. "How the path name in file links should be stored.
  1508. Valid values are:
  1509. relative Relative to the current directory, i.e. the directory of the file
  1510. into which the link is being inserted.
  1511. absolute Absolute path, if possible with ~ for home directory.
  1512. noabbrev Absolute path, no abbreviation of home directory.
  1513. adaptive Use relative path for files in the current directory and sub-
  1514. directories of it. For other files, use an absolute path."
  1515. :group 'org-link
  1516. :type '(choice
  1517. (const relative)
  1518. (const absolute)
  1519. (const noabbrev)
  1520. (const adaptive)))
  1521. (defvaralias 'org-activate-links 'org-highlight-links)
  1522. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1523. "Types of links that should be highlighted in Org-mode files.
  1524. This is a list of symbols, each one of them leading to the
  1525. highlighting of a certain link type.
  1526. You can still open links that are not highlighted.
  1527. In principle, it does not hurt to turn on highlighting for all
  1528. link types. There may be a small gain when turning off unused
  1529. link types. The types are:
  1530. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1531. angle Links in angular brackets that may contain whitespace like
  1532. <bbdb:Carsten Dominik>.
  1533. plain Plain links in normal text, no whitespace, like http://google.com.
  1534. radio Text that is matched by a radio target, see manual for details.
  1535. tag Tag settings in a headline (link to tag search).
  1536. date Time stamps (link to calendar).
  1537. footnote Footnote labels.
  1538. If you set this variable during an Emacs session, use `org-mode-restart'
  1539. in the Org buffer so that the change takes effect."
  1540. :group 'org-link
  1541. :group 'org-appearance
  1542. :type '(set :greedy t
  1543. (const :tag "Double bracket links" bracket)
  1544. (const :tag "Angular bracket links" angle)
  1545. (const :tag "Plain text links" plain)
  1546. (const :tag "Radio target matches" radio)
  1547. (const :tag "Tags" tag)
  1548. (const :tag "Timestamps" date)
  1549. (const :tag "Footnotes" footnote)))
  1550. (defcustom org-make-link-description-function nil
  1551. "Function to use for generating link descriptions from links.
  1552. When nil, the link location will be used. This function must take
  1553. two parameters: the first one is the link, the second one is the
  1554. description generated by `org-insert-link'. The function should
  1555. return the description to use."
  1556. :group 'org-link
  1557. :type '(choice (const nil) (function)))
  1558. (defgroup org-link-store nil
  1559. "Options concerning storing links in Org-mode."
  1560. :tag "Org Store Link"
  1561. :group 'org-link)
  1562. (defcustom org-url-hexify-p t
  1563. "When non-nil, hexify URL when creating a link."
  1564. :type 'boolean
  1565. :version "24.3"
  1566. :group 'org-link-store)
  1567. (defcustom org-email-link-description-format "Email %c: %.30s"
  1568. "Format of the description part of a link to an email or usenet message.
  1569. The following %-escapes will be replaced by corresponding information:
  1570. %F full \"From\" field
  1571. %f name, taken from \"From\" field, address if no name
  1572. %T full \"To\" field
  1573. %t first name in \"To\" field, address if no name
  1574. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1575. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1576. %s subject
  1577. %d date
  1578. %m message-id.
  1579. You may use normal field width specification between the % and the letter.
  1580. This is for example useful to limit the length of the subject.
  1581. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1582. :group 'org-link-store
  1583. :type 'string)
  1584. (defcustom org-from-is-user-regexp
  1585. (let (r1 r2)
  1586. (when (and user-mail-address (not (string= user-mail-address "")))
  1587. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1588. (when (and user-full-name (not (string= user-full-name "")))
  1589. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1590. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1591. "Regexp matched against the \"From:\" header of an email or usenet message.
  1592. It should match if the message is from the user him/herself."
  1593. :group 'org-link-store
  1594. :type 'regexp)
  1595. (defcustom org-context-in-file-links t
  1596. "Non-nil means file links from `org-store-link' contain context.
  1597. A search string will be added to the file name with :: as separator and
  1598. used to find the context when the link is activated by the command
  1599. `org-open-at-point'. When this option is t, the entire active region
  1600. will be placed in the search string of the file link. If set to a
  1601. positive integer, only the first n lines of context will be stored.
  1602. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1603. negates this setting for the duration of the command."
  1604. :group 'org-link-store
  1605. :type '(choice boolean integer))
  1606. (defcustom org-keep-stored-link-after-insertion nil
  1607. "Non-nil means keep link in list for entire session.
  1608. The command `org-store-link' adds a link pointing to the current
  1609. location to an internal list. These links accumulate during a session.
  1610. The command `org-insert-link' can be used to insert links into any
  1611. Org-mode file (offering completion for all stored links). When this
  1612. option is nil, every link which has been inserted once using \\[org-insert-link]
  1613. will be removed from the list, to make completing the unused links
  1614. more efficient."
  1615. :group 'org-link-store
  1616. :type 'boolean)
  1617. (defgroup org-link-follow nil
  1618. "Options concerning following links in Org-mode."
  1619. :tag "Org Follow Link"
  1620. :group 'org-link)
  1621. (defcustom org-link-translation-function nil
  1622. "Function to translate links with different syntax to Org syntax.
  1623. This can be used to translate links created for example by the Planner
  1624. or emacs-wiki packages to Org syntax.
  1625. The function must accept two parameters, a TYPE containing the link
  1626. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1627. which is everything after the link protocol. It should return a cons
  1628. with possibly modified values of type and path.
  1629. Org contains a function for this, so if you set this variable to
  1630. `org-translate-link-from-planner', you should be able follow many
  1631. links created by planner."
  1632. :group 'org-link-follow
  1633. :type '(choice (const nil) (function)))
  1634. (defcustom org-follow-link-hook nil
  1635. "Hook that is run after a link has been followed."
  1636. :group 'org-link-follow
  1637. :type 'hook)
  1638. (defcustom org-tab-follows-link nil
  1639. "Non-nil means on links TAB will follow the link.
  1640. Needs to be set before org.el is loaded.
  1641. This really should not be used, it does not make sense, and the
  1642. implementation is bad."
  1643. :group 'org-link-follow
  1644. :type 'boolean)
  1645. (defcustom org-return-follows-link nil
  1646. "Non-nil means on links RET will follow the link.
  1647. In tables, the special behavior of RET has precedence."
  1648. :group 'org-link-follow
  1649. :type 'boolean)
  1650. (defcustom org-mouse-1-follows-link
  1651. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1652. "Non-nil means mouse-1 on a link will follow the link.
  1653. A longer mouse click will still set point. Does not work on XEmacs.
  1654. Needs to be set before org.el is loaded."
  1655. :group 'org-link-follow
  1656. :version "24.4"
  1657. :package-version '(Org . "8.3")
  1658. :type '(choice
  1659. (const :tag "A double click follows the link" double)
  1660. (const :tag "Unconditionally follow the link with mouse-1" t)
  1661. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1662. (defcustom org-mark-ring-length 4
  1663. "Number of different positions to be recorded in the ring.
  1664. Changing this requires a restart of Emacs to work correctly."
  1665. :group 'org-link-follow
  1666. :type 'integer)
  1667. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1668. "Non-nil means internal links in Org files must exactly match a headline.
  1669. When nil, the link search tries to match a phrase with all words
  1670. in the search text."
  1671. :group 'org-link-follow
  1672. :version "24.1"
  1673. :type '(choice
  1674. (const :tag "Use fuzzy text search" nil)
  1675. (const :tag "Match only exact headline" t)
  1676. (const :tag "Match exact headline or query to create it"
  1677. query-to-create)))
  1678. (defcustom org-link-frame-setup
  1679. '((vm . vm-visit-folder-other-frame)
  1680. (vm-imap . vm-visit-imap-folder-other-frame)
  1681. (gnus . org-gnus-no-new-news)
  1682. (file . find-file-other-window)
  1683. (wl . wl-other-frame))
  1684. "Setup the frame configuration for following links.
  1685. When following a link with Emacs, it may often be useful to display
  1686. this link in another window or frame. This variable can be used to
  1687. set this up for the different types of links.
  1688. For VM, use any of
  1689. `vm-visit-folder'
  1690. `vm-visit-folder-other-window'
  1691. `vm-visit-folder-other-frame'
  1692. For Gnus, use any of
  1693. `gnus'
  1694. `gnus-other-frame'
  1695. `org-gnus-no-new-news'
  1696. For FILE, use any of
  1697. `find-file'
  1698. `find-file-other-window'
  1699. `find-file-other-frame'
  1700. For Wanderlust use any of
  1701. `wl'
  1702. `wl-other-frame'
  1703. For the calendar, use the variable `calendar-setup'.
  1704. For BBDB, it is currently only possible to display the matches in
  1705. another window."
  1706. :group 'org-link-follow
  1707. :type '(list
  1708. (cons (const vm)
  1709. (choice
  1710. (const vm-visit-folder)
  1711. (const vm-visit-folder-other-window)
  1712. (const vm-visit-folder-other-frame)))
  1713. (cons (const vm-imap)
  1714. (choice
  1715. (const vm-visit-imap-folder)
  1716. (const vm-visit-imap-folder-other-window)
  1717. (const vm-visit-imap-folder-other-frame)))
  1718. (cons (const gnus)
  1719. (choice
  1720. (const gnus)
  1721. (const gnus-other-frame)
  1722. (const org-gnus-no-new-news)))
  1723. (cons (const file)
  1724. (choice
  1725. (const find-file)
  1726. (const find-file-other-window)
  1727. (const find-file-other-frame)))
  1728. (cons (const wl)
  1729. (choice
  1730. (const wl)
  1731. (const wl-other-frame)))))
  1732. (defcustom org-display-internal-link-with-indirect-buffer nil
  1733. "Non-nil means use indirect buffer to display infile links.
  1734. Activating internal links (from one location in a file to another location
  1735. in the same file) normally just jumps to the location. When the link is
  1736. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1737. is displayed in
  1738. another window. When this option is set, the other window actually displays
  1739. an indirect buffer clone of the current buffer, to avoid any visibility
  1740. changes to the current buffer."
  1741. :group 'org-link-follow
  1742. :type 'boolean)
  1743. (defcustom org-open-non-existing-files nil
  1744. "Non-nil means `org-open-file' will open non-existing files.
  1745. When nil, an error will be generated.
  1746. This variable applies only to external applications because they
  1747. might choke on non-existing files. If the link is to a file that
  1748. will be opened in Emacs, the variable is ignored."
  1749. :group 'org-link-follow
  1750. :type 'boolean)
  1751. (defcustom org-open-directory-means-index-dot-org nil
  1752. "Non-nil means a link to a directory really means to index.org.
  1753. When nil, following a directory link will run dired or open a finder/explorer
  1754. window on that directory."
  1755. :group 'org-link-follow
  1756. :type 'boolean)
  1757. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1758. "Non-nil means ask for confirmation before executing shell links.
  1759. Shell links can be dangerous: just think about a link
  1760. [[shell:rm -rf ~/*][Google Search]]
  1761. This link would show up in your Org-mode document as \"Google Search\",
  1762. but really it would remove your entire home directory.
  1763. Therefore we advise against setting this variable to nil.
  1764. Just change it to `y-or-n-p' if you want to confirm with a
  1765. single keystroke rather than having to type \"yes\"."
  1766. :group 'org-link-follow
  1767. :type '(choice
  1768. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1769. (const :tag "with y-or-n (faster)" y-or-n-p)
  1770. (const :tag "no confirmation (dangerous)" nil)))
  1771. (put 'org-confirm-shell-link-function
  1772. 'safe-local-variable
  1773. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1774. (defcustom org-confirm-shell-link-not-regexp ""
  1775. "A regexp to skip confirmation for shell links."
  1776. :group 'org-link-follow
  1777. :version "24.1"
  1778. :type 'regexp)
  1779. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1780. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1781. Elisp links can be dangerous: just think about a link
  1782. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1783. This link would show up in your Org-mode document as \"Google Search\",
  1784. but really it would remove your entire home directory.
  1785. Therefore we advise against setting this variable to nil.
  1786. Just change it to `y-or-n-p' if you want to confirm with a
  1787. single keystroke rather than having to type \"yes\"."
  1788. :group 'org-link-follow
  1789. :type '(choice
  1790. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1791. (const :tag "with y-or-n (faster)" y-or-n-p)
  1792. (const :tag "no confirmation (dangerous)" nil)))
  1793. (put 'org-confirm-shell-link-function
  1794. 'safe-local-variable
  1795. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1796. (defcustom org-confirm-elisp-link-not-regexp ""
  1797. "A regexp to skip confirmation for Elisp links."
  1798. :group 'org-link-follow
  1799. :version "24.1"
  1800. :type 'regexp)
  1801. (defconst org-file-apps-defaults-gnu
  1802. '((remote . emacs)
  1803. (system . mailcap)
  1804. (t . mailcap))
  1805. "Default file applications on a UNIX or GNU/Linux system.
  1806. See `org-file-apps'.")
  1807. (defconst org-file-apps-defaults-macosx
  1808. '((remote . emacs)
  1809. (t . "open %s")
  1810. (system . "open %s")
  1811. ("ps.gz" . "gv %s")
  1812. ("eps.gz" . "gv %s")
  1813. ("dvi" . "xdvi %s")
  1814. ("fig" . "xfig %s"))
  1815. "Default file applications on a MacOS X system.
  1816. The system \"open\" is known as a default, but we use X11 applications
  1817. for some files for which the OS does not have a good default.
  1818. See `org-file-apps'.")
  1819. (defconst org-file-apps-defaults-windowsnt
  1820. (list
  1821. '(remote . emacs)
  1822. (cons t
  1823. (list (if (featurep 'xemacs)
  1824. 'mswindows-shell-execute
  1825. 'w32-shell-execute)
  1826. "open" 'file))
  1827. (cons 'system
  1828. (list (if (featurep 'xemacs)
  1829. 'mswindows-shell-execute
  1830. 'w32-shell-execute)
  1831. "open" 'file)))
  1832. "Default file applications on a Windows NT system.
  1833. The system \"open\" is used for most files.
  1834. See `org-file-apps'.")
  1835. (defcustom org-file-apps
  1836. '((auto-mode . emacs)
  1837. ("\\.mm\\'" . default)
  1838. ("\\.x?html?\\'" . default)
  1839. ("\\.pdf\\'" . default))
  1840. "External applications for opening `file:path' items in a document.
  1841. Org-mode uses system defaults for different file types, but
  1842. you can use this variable to set the application for a given file
  1843. extension. The entries in this list are cons cells where the car identifies
  1844. files and the cdr the corresponding command. Possible values for the
  1845. file identifier are
  1846. \"string\" A string as a file identifier can be interpreted in different
  1847. ways, depending on its contents:
  1848. - Alphanumeric characters only:
  1849. Match links with this file extension.
  1850. Example: (\"pdf\" . \"evince %s\")
  1851. to open PDFs with evince.
  1852. - Regular expression: Match links where the
  1853. filename matches the regexp. If you want to
  1854. use groups here, use shy groups.
  1855. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1856. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1857. to open *.html and *.xhtml with firefox.
  1858. - Regular expression which contains (non-shy) groups:
  1859. Match links where the whole link, including \"::\", and
  1860. anything after that, matches the regexp.
  1861. In a custom command string, %1, %2, etc. are replaced with
  1862. the parts of the link that were matched by the groups.
  1863. For backwards compatibility, if a command string is given
  1864. that does not use any of the group matches, this case is
  1865. handled identically to the second one (i.e. match against
  1866. file name only).
  1867. In a custom lisp form, you can access the group matches with
  1868. (match-string n link).
  1869. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1870. to open [[file:document.pdf::5]] with evince at page 5.
  1871. `directory' Matches a directory
  1872. `remote' Matches a remote file, accessible through tramp or efs.
  1873. Remote files most likely should be visited through Emacs
  1874. because external applications cannot handle such paths.
  1875. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1876. so all files Emacs knows how to handle. Using this with
  1877. command `emacs' will open most files in Emacs. Beware that this
  1878. will also open html files inside Emacs, unless you add
  1879. (\"html\" . default) to the list as well.
  1880. t Default for files not matched by any of the other options.
  1881. `system' The system command to open files, like `open' on Windows
  1882. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1883. that will be selected if you call `C-c C-o' with a double
  1884. \\[universal-argument] \\[universal-argument] prefix.
  1885. Possible values for the command are:
  1886. `emacs' The file will be visited by the current Emacs process.
  1887. `default' Use the default application for this file type, which is the
  1888. association for t in the list, most likely in the system-specific
  1889. part.
  1890. This can be used to overrule an unwanted setting in the
  1891. system-specific variable.
  1892. `system' Use the system command for opening files, like \"open\".
  1893. This command is specified by the entry whose car is `system'.
  1894. Most likely, the system-specific version of this variable
  1895. does define this command, but you can overrule/replace it
  1896. here.
  1897. string A command to be executed by a shell; %s will be replaced
  1898. by the path to the file.
  1899. sexp A Lisp form which will be evaluated. The file path will
  1900. be available in the Lisp variable `file'.
  1901. For more examples, see the system specific constants
  1902. `org-file-apps-defaults-macosx'
  1903. `org-file-apps-defaults-windowsnt'
  1904. `org-file-apps-defaults-gnu'."
  1905. :group 'org-link-follow
  1906. :type '(repeat
  1907. (cons (choice :value ""
  1908. (string :tag "Extension")
  1909. (const :tag "System command to open files" system)
  1910. (const :tag "Default for unrecognized files" t)
  1911. (const :tag "Remote file" remote)
  1912. (const :tag "Links to a directory" directory)
  1913. (const :tag "Any files that have Emacs modes"
  1914. auto-mode))
  1915. (choice :value ""
  1916. (const :tag "Visit with Emacs" emacs)
  1917. (const :tag "Use default" default)
  1918. (const :tag "Use the system command" system)
  1919. (string :tag "Command")
  1920. (sexp :tag "Lisp form")))))
  1921. (defcustom org-doi-server-url "http://dx.doi.org/"
  1922. "The URL of the DOI server."
  1923. :type 'string
  1924. :version "24.3"
  1925. :group 'org-link-follow)
  1926. (defgroup org-refile nil
  1927. "Options concerning refiling entries in Org-mode."
  1928. :tag "Org Refile"
  1929. :group 'org)
  1930. (defcustom org-directory "~/org"
  1931. "Directory with org files.
  1932. This is just a default location to look for Org files. There is no need
  1933. at all to put your files into this directory. It is only used in the
  1934. following situations:
  1935. 1. When a capture template specifies a target file that is not an
  1936. absolute path. The path will then be interpreted relative to
  1937. `org-directory'
  1938. 2. When a capture note is filed away in an interactive way (when exiting the
  1939. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1940. with `org-directory' as the default path."
  1941. :group 'org-refile
  1942. :group 'org-capture
  1943. :type 'directory)
  1944. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1945. "Default target for storing notes.
  1946. Used as a fall back file for org-capture.el, for templates that
  1947. do not specify a target file."
  1948. :group 'org-refile
  1949. :group 'org-capture
  1950. :type '(choice
  1951. (const :tag "Default from remember-data-file" nil)
  1952. file))
  1953. (defcustom org-goto-interface 'outline
  1954. "The default interface to be used for `org-goto'.
  1955. Allowed values are:
  1956. outline The interface shows an outline of the relevant file
  1957. and the correct heading is found by moving through
  1958. the outline or by searching with incremental search.
  1959. outline-path-completion Headlines in the current buffer are offered via
  1960. completion. This is the interface also used by
  1961. the refile command."
  1962. :group 'org-refile
  1963. :type '(choice
  1964. (const :tag "Outline" outline)
  1965. (const :tag "Outline-path-completion" outline-path-completion)))
  1966. (defcustom org-goto-max-level 5
  1967. "Maximum target level when running `org-goto' with refile interface."
  1968. :group 'org-refile
  1969. :type 'integer)
  1970. (defcustom org-reverse-note-order nil
  1971. "Non-nil means store new notes at the beginning of a file or entry.
  1972. When nil, new notes will be filed to the end of a file or entry.
  1973. This can also be a list with cons cells of regular expressions that
  1974. are matched against file names, and values."
  1975. :group 'org-capture
  1976. :group 'org-refile
  1977. :type '(choice
  1978. (const :tag "Reverse always" t)
  1979. (const :tag "Reverse never" nil)
  1980. (repeat :tag "By file name regexp"
  1981. (cons regexp boolean))))
  1982. (defcustom org-log-refile nil
  1983. "Information to record when a task is refiled.
  1984. Possible values are:
  1985. nil Don't add anything
  1986. time Add a time stamp to the task
  1987. note Prompt for a note and add it with template `org-log-note-headings'
  1988. This option can also be set with on a per-file-basis with
  1989. #+STARTUP: nologrefile
  1990. #+STARTUP: logrefile
  1991. #+STARTUP: lognoterefile
  1992. You can have local logging settings for a subtree by setting the LOGGING
  1993. property to one or more of these keywords.
  1994. When bulk-refiling from the agenda, the value `note' is forbidden and
  1995. will temporarily be changed to `time'."
  1996. :group 'org-refile
  1997. :group 'org-progress
  1998. :version "24.1"
  1999. :type '(choice
  2000. (const :tag "No logging" nil)
  2001. (const :tag "Record timestamp" time)
  2002. (const :tag "Record timestamp with note." note)))
  2003. (defcustom org-refile-targets nil
  2004. "Targets for refiling entries with \\[org-refile].
  2005. This is a list of cons cells. Each cell contains:
  2006. - a specification of the files to be considered, either a list of files,
  2007. or a symbol whose function or variable value will be used to retrieve
  2008. a file name or a list of file names. If you use `org-agenda-files' for
  2009. that, all agenda files will be scanned for targets. Nil means consider
  2010. headings in the current buffer.
  2011. - A specification of how to find candidate refile targets. This may be
  2012. any of:
  2013. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2014. This tag has to be present in all target headlines, inheritance will
  2015. not be considered.
  2016. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2017. todo keyword.
  2018. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2019. headlines that are refiling targets.
  2020. - a cons cell (:level . N). Any headline of level N is considered a target.
  2021. Note that, when `org-odd-levels-only' is set, level corresponds to
  2022. order in hierarchy, not to the number of stars.
  2023. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2024. Note that, when `org-odd-levels-only' is set, level corresponds to
  2025. order in hierarchy, not to the number of stars.
  2026. Each element of this list generates a set of possible targets.
  2027. The union of these sets is presented (with completion) to
  2028. the user by `org-refile'.
  2029. You can set the variable `org-refile-target-verify-function' to a function
  2030. to verify each headline found by the simple criteria above.
  2031. When this variable is nil, all top-level headlines in the current buffer
  2032. are used, equivalent to the value `((nil . (:level . 1))'."
  2033. :group 'org-refile
  2034. :type '(repeat
  2035. (cons
  2036. (choice :value org-agenda-files
  2037. (const :tag "All agenda files" org-agenda-files)
  2038. (const :tag "Current buffer" nil)
  2039. (function) (variable) (file))
  2040. (choice :tag "Identify target headline by"
  2041. (cons :tag "Specific tag" (const :value :tag) (string))
  2042. (cons :tag "TODO keyword" (const :value :todo) (string))
  2043. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2044. (cons :tag "Level number" (const :value :level) (integer))
  2045. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2046. (defcustom org-refile-target-verify-function nil
  2047. "Function to verify if the headline at point should be a refile target.
  2048. The function will be called without arguments, with point at the
  2049. beginning of the headline. It should return t and leave point
  2050. where it is if the headline is a valid target for refiling.
  2051. If the target should not be selected, the function must return nil.
  2052. In addition to this, it may move point to a place from where the search
  2053. should be continued. For example, the function may decide that the entire
  2054. subtree of the current entry should be excluded and move point to the end
  2055. of the subtree."
  2056. :group 'org-refile
  2057. :type '(choice
  2058. (const nil)
  2059. (function)))
  2060. (defcustom org-refile-use-cache nil
  2061. "Non-nil means cache refile targets to speed up the process.
  2062. The cache for a particular file will be updated automatically when
  2063. the buffer has been killed, or when any of the marker used for flagging
  2064. refile targets no longer points at a live buffer.
  2065. If you have added new entries to a buffer that might themselves be targets,
  2066. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  2067. find that easier, `C-u C-u C-u C-c C-w'."
  2068. :group 'org-refile
  2069. :version "24.1"
  2070. :type 'boolean)
  2071. (defcustom org-refile-use-outline-path nil
  2072. "Non-nil means provide refile targets as paths.
  2073. So a level 3 headline will be available as level1/level2/level3.
  2074. When the value is `file', also include the file name (without directory)
  2075. into the path. In this case, you can also stop the completion after
  2076. the file name, to get entries inserted as top level in the file.
  2077. When `full-file-path', include the full file path."
  2078. :group 'org-refile
  2079. :type '(choice
  2080. (const :tag "Not" nil)
  2081. (const :tag "Yes" t)
  2082. (const :tag "Start with file name" file)
  2083. (const :tag "Start with full file path" full-file-path)))
  2084. (defcustom org-outline-path-complete-in-steps t
  2085. "Non-nil means complete the outline path in hierarchical steps.
  2086. When Org-mode uses the refile interface to select an outline path
  2087. \(see variable `org-refile-use-outline-path'), the completion of
  2088. the path can be done is a single go, or if can be done in steps down
  2089. the headline hierarchy. Going in steps is probably the best if you
  2090. do not use a special completion package like `ido' or `icicles'.
  2091. However, when using these packages, going in one step can be very
  2092. fast, while still showing the whole path to the entry."
  2093. :group 'org-refile
  2094. :type 'boolean)
  2095. (defcustom org-refile-allow-creating-parent-nodes nil
  2096. "Non-nil means allow to create new nodes as refile targets.
  2097. New nodes are then created by adding \"/new node name\" to the completion
  2098. of an existing node. When the value of this variable is `confirm',
  2099. new node creation must be confirmed by the user (recommended).
  2100. When nil, the completion must match an existing entry.
  2101. Note that, if the new heading is not seen by the criteria
  2102. listed in `org-refile-targets', multiple instances of the same
  2103. heading would be created by trying again to file under the new
  2104. heading."
  2105. :group 'org-refile
  2106. :type '(choice
  2107. (const :tag "Never" nil)
  2108. (const :tag "Always" t)
  2109. (const :tag "Prompt for confirmation" confirm)))
  2110. (defcustom org-refile-active-region-within-subtree nil
  2111. "Non-nil means also refile active region within a subtree.
  2112. By default `org-refile' doesn't allow refiling regions if they
  2113. don't contain a set of subtrees, but it might be convenient to
  2114. do so sometimes: in that case, the first line of the region is
  2115. converted to a headline before refiling."
  2116. :group 'org-refile
  2117. :version "24.1"
  2118. :type 'boolean)
  2119. (defgroup org-todo nil
  2120. "Options concerning TODO items in Org-mode."
  2121. :tag "Org TODO"
  2122. :group 'org)
  2123. (defgroup org-progress nil
  2124. "Options concerning Progress logging in Org-mode."
  2125. :tag "Org Progress"
  2126. :group 'org-time)
  2127. (defvar org-todo-interpretation-widgets
  2128. '((:tag "Sequence (cycling hits every state)" sequence)
  2129. (:tag "Type (cycling directly to DONE)" type))
  2130. "The available interpretation symbols for customizing `org-todo-keywords'.
  2131. Interested libraries should add to this list.")
  2132. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2133. "List of TODO entry keyword sequences and their interpretation.
  2134. \\<org-mode-map>This is a list of sequences.
  2135. Each sequence starts with a symbol, either `sequence' or `type',
  2136. indicating if the keywords should be interpreted as a sequence of
  2137. action steps, or as different types of TODO items. The first
  2138. keywords are states requiring action - these states will select a headline
  2139. for inclusion into the global TODO list Org-mode produces. If one of
  2140. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2141. signify that no further action is necessary. If \"|\" is not found,
  2142. the last keyword is treated as the only DONE state of the sequence.
  2143. The command \\[org-todo] cycles an entry through these states, and one
  2144. additional state where no keyword is present. For details about this
  2145. cycling, see the manual.
  2146. TODO keywords and interpretation can also be set on a per-file basis with
  2147. the special #+SEQ_TODO and #+TYP_TODO lines.
  2148. Each keyword can optionally specify a character for fast state selection
  2149. \(in combination with the variable `org-use-fast-todo-selection')
  2150. and specifiers for state change logging, using the same syntax that
  2151. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2152. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2153. indicates to record a time stamp each time this state is selected.
  2154. Each keyword may also specify if a timestamp or a note should be
  2155. recorded when entering or leaving the state, by adding additional
  2156. characters in the parenthesis after the keyword. This looks like this:
  2157. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2158. record only the time of the state change. With X and Y being either
  2159. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2160. Y when leaving the state if and only if the *target* state does not
  2161. define X. You may omit any of the fast-selection key or X or /Y,
  2162. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2163. For backward compatibility, this variable may also be just a list
  2164. of keywords. In this case the interpretation (sequence or type) will be
  2165. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2166. :group 'org-todo
  2167. :group 'org-keywords
  2168. :type '(choice
  2169. (repeat :tag "Old syntax, just keywords"
  2170. (string :tag "Keyword"))
  2171. (repeat :tag "New syntax"
  2172. (cons
  2173. (choice
  2174. :tag "Interpretation"
  2175. ;;Quick and dirty way to see
  2176. ;;`org-todo-interpretations'. This takes the
  2177. ;;place of item arguments
  2178. :convert-widget
  2179. (lambda (widget)
  2180. (widget-put widget
  2181. :args (mapcar
  2182. (lambda (x)
  2183. (widget-convert
  2184. (cons 'const x)))
  2185. org-todo-interpretation-widgets))
  2186. widget))
  2187. (repeat
  2188. (string :tag "Keyword"))))))
  2189. (defvar org-todo-keywords-1 nil
  2190. "All TODO and DONE keywords active in a buffer.")
  2191. (make-variable-buffer-local 'org-todo-keywords-1)
  2192. (defvar org-todo-keywords-for-agenda nil)
  2193. (defvar org-done-keywords-for-agenda nil)
  2194. (defvar org-todo-keyword-alist-for-agenda nil)
  2195. (defvar org-tag-alist-for-agenda nil
  2196. "Alist of all tags from all agenda files.")
  2197. (defvar org-tag-groups-alist-for-agenda nil
  2198. "Alist of all groups tags from all current agenda files.")
  2199. (defvar org-tag-groups-alist nil)
  2200. (make-variable-buffer-local 'org-tag-groups-alist)
  2201. (defvar org-agenda-contributing-files nil)
  2202. (defvar org-not-done-keywords nil)
  2203. (make-variable-buffer-local 'org-not-done-keywords)
  2204. (defvar org-done-keywords nil)
  2205. (make-variable-buffer-local 'org-done-keywords)
  2206. (defvar org-todo-heads nil)
  2207. (make-variable-buffer-local 'org-todo-heads)
  2208. (defvar org-todo-sets nil)
  2209. (make-variable-buffer-local 'org-todo-sets)
  2210. (defvar org-todo-log-states nil)
  2211. (make-variable-buffer-local 'org-todo-log-states)
  2212. (defvar org-todo-kwd-alist nil)
  2213. (make-variable-buffer-local 'org-todo-kwd-alist)
  2214. (defvar org-todo-key-alist nil)
  2215. (make-variable-buffer-local 'org-todo-key-alist)
  2216. (defvar org-todo-key-trigger nil)
  2217. (make-variable-buffer-local 'org-todo-key-trigger)
  2218. (defcustom org-todo-interpretation 'sequence
  2219. "Controls how TODO keywords are interpreted.
  2220. This variable is in principle obsolete and is only used for
  2221. backward compatibility, if the interpretation of todo keywords is
  2222. not given already in `org-todo-keywords'. See that variable for
  2223. more information."
  2224. :group 'org-todo
  2225. :group 'org-keywords
  2226. :type '(choice (const sequence)
  2227. (const type)))
  2228. (defcustom org-use-fast-todo-selection t
  2229. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2230. This variable describes if and under what circumstances the cycling
  2231. mechanism for TODO keywords will be replaced by a single-key, direct
  2232. selection scheme.
  2233. When nil, fast selection is never used.
  2234. When the symbol `prefix', it will be used when `org-todo' is called
  2235. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2236. `C-u t' in an agenda buffer.
  2237. When t, fast selection is used by default. In this case, the prefix
  2238. argument forces cycling instead.
  2239. In all cases, the special interface is only used if access keys have
  2240. actually been assigned by the user, i.e. if keywords in the configuration
  2241. are followed by a letter in parenthesis, like TODO(t)."
  2242. :group 'org-todo
  2243. :type '(choice
  2244. (const :tag "Never" nil)
  2245. (const :tag "By default" t)
  2246. (const :tag "Only with C-u C-c C-t" prefix)))
  2247. (defcustom org-provide-todo-statistics t
  2248. "Non-nil means update todo statistics after insert and toggle.
  2249. ALL-HEADLINES means update todo statistics by including headlines
  2250. with no TODO keyword as well, counting them as not done.
  2251. A list of TODO keywords means the same, but skip keywords that are
  2252. not in this list.
  2253. When set to a list of two lists, the first list contains keywords
  2254. to consider as TODO keywords, the second list contains keywords
  2255. to consider as DONE keywords.
  2256. When this is set, todo statistics is updated in the parent of the
  2257. current entry each time a todo state is changed."
  2258. :group 'org-todo
  2259. :type '(choice
  2260. (const :tag "Yes, only for TODO entries" t)
  2261. (const :tag "Yes, including all entries" all-headlines)
  2262. (repeat :tag "Yes, for TODOs in this list"
  2263. (string :tag "TODO keyword"))
  2264. (list :tag "Yes, for TODOs and DONEs in these lists"
  2265. (repeat (string :tag "TODO keyword"))
  2266. (repeat (string :tag "DONE keyword")))
  2267. (other :tag "No TODO statistics" nil)))
  2268. (defcustom org-hierarchical-todo-statistics t
  2269. "Non-nil means TODO statistics covers just direct children.
  2270. When nil, all entries in the subtree are considered.
  2271. This has only an effect if `org-provide-todo-statistics' is set.
  2272. To set this to nil for only a single subtree, use a COOKIE_DATA
  2273. property and include the word \"recursive\" into the value."
  2274. :group 'org-todo
  2275. :type 'boolean)
  2276. (defcustom org-after-todo-state-change-hook nil
  2277. "Hook which is run after the state of a TODO item was changed.
  2278. The new state (a string with a TODO keyword, or nil) is available in the
  2279. Lisp variable `org-state'."
  2280. :group 'org-todo
  2281. :type 'hook)
  2282. (defvar org-blocker-hook nil
  2283. "Hook for functions that are allowed to block a state change.
  2284. Functions in this hook should not modify the buffer.
  2285. Each function gets as its single argument a property list,
  2286. see `org-trigger-hook' for more information about this list.
  2287. If any of the functions in this hook returns nil, the state change
  2288. is blocked.")
  2289. (defvar org-trigger-hook nil
  2290. "Hook for functions that are triggered by a state change.
  2291. Each function gets as its single argument a property list with at
  2292. least the following elements:
  2293. (:type type-of-change :position pos-at-entry-start
  2294. :from old-state :to new-state)
  2295. Depending on the type, more properties may be present.
  2296. This mechanism is currently implemented for:
  2297. TODO state changes
  2298. ------------------
  2299. :type todo-state-change
  2300. :from previous state (keyword as a string), or nil, or a symbol
  2301. 'todo' or 'done', to indicate the general type of state.
  2302. :to new state, like in :from")
  2303. (defcustom org-enforce-todo-dependencies nil
  2304. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2305. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2306. be blocked if any prior sibling is not yet done.
  2307. Finally, if the parent is blocked because of ordered siblings of its own,
  2308. the child will also be blocked."
  2309. :set (lambda (var val)
  2310. (set var val)
  2311. (if val
  2312. (add-hook 'org-blocker-hook
  2313. 'org-block-todo-from-children-or-siblings-or-parent)
  2314. (remove-hook 'org-blocker-hook
  2315. 'org-block-todo-from-children-or-siblings-or-parent)))
  2316. :group 'org-todo
  2317. :type 'boolean)
  2318. (defcustom org-enforce-todo-checkbox-dependencies nil
  2319. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2320. When this is nil, checkboxes have no influence on switching TODO states.
  2321. When non-nil, you first need to check off all check boxes before the TODO
  2322. entry can be switched to DONE.
  2323. This variable needs to be set before org.el is loaded, and you need to
  2324. restart Emacs after a change to make the change effective. The only way
  2325. to change is while Emacs is running is through the customize interface."
  2326. :set (lambda (var val)
  2327. (set var val)
  2328. (if val
  2329. (add-hook 'org-blocker-hook
  2330. 'org-block-todo-from-checkboxes)
  2331. (remove-hook 'org-blocker-hook
  2332. 'org-block-todo-from-checkboxes)))
  2333. :group 'org-todo
  2334. :type 'boolean)
  2335. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2336. "Non-nil means inserting a TODO heading is treated as state change.
  2337. So when the command \\[org-insert-todo-heading] is used, state change
  2338. logging will apply if appropriate. When nil, the new TODO item will
  2339. be inserted directly, and no logging will take place."
  2340. :group 'org-todo
  2341. :type 'boolean)
  2342. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2343. "Non-nil means switching TODO states with S-cursor counts as state change.
  2344. This is the default behavior. However, setting this to nil allows a
  2345. convenient way to select a TODO state and bypass any logging associated
  2346. with that."
  2347. :group 'org-todo
  2348. :type 'boolean)
  2349. (defcustom org-todo-state-tags-triggers nil
  2350. "Tag changes that should be triggered by TODO state changes.
  2351. This is a list. Each entry is
  2352. (state-change (tag . flag) .......)
  2353. State-change can be a string with a state, and empty string to indicate the
  2354. state that has no TODO keyword, or it can be one of the symbols `todo'
  2355. or `done', meaning any not-done or done state, respectively."
  2356. :group 'org-todo
  2357. :group 'org-tags
  2358. :type '(repeat
  2359. (cons (choice :tag "When changing to"
  2360. (const :tag "Not-done state" todo)
  2361. (const :tag "Done state" done)
  2362. (string :tag "State"))
  2363. (repeat
  2364. (cons :tag "Tag action"
  2365. (string :tag "Tag")
  2366. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2367. (defcustom org-log-done nil
  2368. "Information to record when a task moves to the DONE state.
  2369. Possible values are:
  2370. nil Don't add anything, just change the keyword
  2371. time Add a time stamp to the task
  2372. note Prompt for a note and add it with template `org-log-note-headings'
  2373. This option can also be set with on a per-file-basis with
  2374. #+STARTUP: nologdone
  2375. #+STARTUP: logdone
  2376. #+STARTUP: lognotedone
  2377. You can have local logging settings for a subtree by setting the LOGGING
  2378. property to one or more of these keywords."
  2379. :group 'org-todo
  2380. :group 'org-progress
  2381. :type '(choice
  2382. (const :tag "No logging" nil)
  2383. (const :tag "Record CLOSED timestamp" time)
  2384. (const :tag "Record CLOSED timestamp with note." note)))
  2385. ;; Normalize old uses of org-log-done.
  2386. (cond
  2387. ((eq org-log-done t) (setq org-log-done 'time))
  2388. ((and (listp org-log-done) (memq 'done org-log-done))
  2389. (setq org-log-done 'note)))
  2390. (defcustom org-log-reschedule nil
  2391. "Information to record when the scheduling date of a tasks is modified.
  2392. Possible values are:
  2393. nil Don't add anything, just change the date
  2394. time Add a time stamp to the task
  2395. note Prompt for a note and add it with template `org-log-note-headings'
  2396. This option can also be set with on a per-file-basis with
  2397. #+STARTUP: nologreschedule
  2398. #+STARTUP: logreschedule
  2399. #+STARTUP: lognotereschedule"
  2400. :group 'org-todo
  2401. :group 'org-progress
  2402. :type '(choice
  2403. (const :tag "No logging" nil)
  2404. (const :tag "Record timestamp" time)
  2405. (const :tag "Record timestamp with note." note)))
  2406. (defcustom org-log-redeadline nil
  2407. "Information to record when the deadline date of a tasks is modified.
  2408. Possible values are:
  2409. nil Don't add anything, just change the date
  2410. time Add a time stamp to the task
  2411. note Prompt for a note and add it with template `org-log-note-headings'
  2412. This option can also be set with on a per-file-basis with
  2413. #+STARTUP: nologredeadline
  2414. #+STARTUP: logredeadline
  2415. #+STARTUP: lognoteredeadline
  2416. You can have local logging settings for a subtree by setting the LOGGING
  2417. property to one or more of these keywords."
  2418. :group 'org-todo
  2419. :group 'org-progress
  2420. :type '(choice
  2421. (const :tag "No logging" nil)
  2422. (const :tag "Record timestamp" time)
  2423. (const :tag "Record timestamp with note." note)))
  2424. (defcustom org-log-note-clock-out nil
  2425. "Non-nil means record a note when clocking out of an item.
  2426. This can also be configured on a per-file basis by adding one of
  2427. the following lines anywhere in the buffer:
  2428. #+STARTUP: lognoteclock-out
  2429. #+STARTUP: nolognoteclock-out"
  2430. :group 'org-todo
  2431. :group 'org-progress
  2432. :type 'boolean)
  2433. (defcustom org-log-done-with-time t
  2434. "Non-nil means the CLOSED time stamp will contain date and time.
  2435. When nil, only the date will be recorded."
  2436. :group 'org-progress
  2437. :type 'boolean)
  2438. (defcustom org-log-note-headings
  2439. '((done . "CLOSING NOTE %t")
  2440. (state . "State %-12s from %-12S %t")
  2441. (note . "Note taken on %t")
  2442. (reschedule . "Rescheduled from %S on %t")
  2443. (delschedule . "Not scheduled, was %S on %t")
  2444. (redeadline . "New deadline from %S on %t")
  2445. (deldeadline . "Removed deadline, was %S on %t")
  2446. (refile . "Refiled on %t")
  2447. (clock-out . ""))
  2448. "Headings for notes added to entries.
  2449. The value is an alist, with the car being a symbol indicating the note
  2450. context, and the cdr is the heading to be used. The heading may also be the
  2451. empty string.
  2452. %t in the heading will be replaced by a time stamp.
  2453. %T will be an active time stamp instead the default inactive one
  2454. %d will be replaced by a short-format time stamp.
  2455. %D will be replaced by an active short-format time stamp.
  2456. %s will be replaced by the new TODO state, in double quotes.
  2457. %S will be replaced by the old TODO state, in double quotes.
  2458. %u will be replaced by the user name.
  2459. %U will be replaced by the full user name.
  2460. In fact, it is not a good idea to change the `state' entry, because
  2461. agenda log mode depends on the format of these entries."
  2462. :group 'org-todo
  2463. :group 'org-progress
  2464. :type '(list :greedy t
  2465. (cons (const :tag "Heading when closing an item" done) string)
  2466. (cons (const :tag
  2467. "Heading when changing todo state (todo sequence only)"
  2468. state) string)
  2469. (cons (const :tag "Heading when just taking a note" note) string)
  2470. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2471. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2472. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2473. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2474. (cons (const :tag "Heading when refiling" refile) string)
  2475. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2476. (unless (assq 'note org-log-note-headings)
  2477. (push '(note . "%t") org-log-note-headings))
  2478. (defcustom org-log-into-drawer nil
  2479. "Non-nil means insert state change notes and time stamps into a drawer.
  2480. When nil, state changes notes will be inserted after the headline and
  2481. any scheduling and clock lines, but not inside a drawer.
  2482. The value of this variable should be the name of the drawer to use.
  2483. LOGBOOK is proposed as the default drawer for this purpose, you can
  2484. also set this to a string to define the drawer of your choice.
  2485. A value of t is also allowed, representing \"LOGBOOK\".
  2486. A value of t or nil can also be set with on a per-file-basis with
  2487. #+STARTUP: logdrawer
  2488. #+STARTUP: nologdrawer
  2489. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2490. will be ignored.
  2491. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2492. a subtree."
  2493. :group 'org-todo
  2494. :group 'org-progress
  2495. :type '(choice
  2496. (const :tag "Not into a drawer" nil)
  2497. (const :tag "LOGBOOK" t)
  2498. (string :tag "Other")))
  2499. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2500. (defun org-log-into-drawer ()
  2501. "Return the value of `org-log-into-drawer', but let properties overrule.
  2502. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2503. used instead of the default value."
  2504. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2505. (cond
  2506. ((not p) org-log-into-drawer)
  2507. ((equal p "nil") nil)
  2508. ((equal p "t") "LOGBOOK")
  2509. (t p))))
  2510. (defcustom org-log-state-notes-insert-after-drawers nil
  2511. "Non-nil means insert state change notes after any drawers in entry.
  2512. Only the drawers that *immediately* follow the headline and the
  2513. deadline/scheduled line are skipped.
  2514. When nil, insert notes right after the heading and perhaps the line
  2515. with deadline/scheduling if present.
  2516. This variable will have no effect if `org-log-into-drawer' is
  2517. set."
  2518. :group 'org-todo
  2519. :group 'org-progress
  2520. :type 'boolean)
  2521. (defcustom org-log-states-order-reversed t
  2522. "Non-nil means the latest state note will be directly after heading.
  2523. When nil, the state change notes will be ordered according to time.
  2524. This option can also be set with on a per-file-basis with
  2525. #+STARTUP: logstatesreversed
  2526. #+STARTUP: nologstatesreversed"
  2527. :group 'org-todo
  2528. :group 'org-progress
  2529. :type 'boolean)
  2530. (defcustom org-todo-repeat-to-state nil
  2531. "The TODO state to which a repeater should return the repeating task.
  2532. By default this is the first task in a TODO sequence, or the previous state
  2533. in a TODO_TYP set. But you can specify another task here.
  2534. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2535. :group 'org-todo
  2536. :version "24.1"
  2537. :type '(choice (const :tag "Head of sequence" nil)
  2538. (string :tag "Specific state")))
  2539. (defcustom org-log-repeat 'time
  2540. "Non-nil means record moving through the DONE state when triggering repeat.
  2541. An auto-repeating task is immediately switched back to TODO when
  2542. marked DONE. If you are not logging state changes (by adding \"@\"
  2543. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2544. record a closing note, there will be no record of the task moving
  2545. through DONE. This variable forces taking a note anyway.
  2546. nil Don't force a record
  2547. time Record a time stamp
  2548. note Prompt for a note and add it with template `org-log-note-headings'
  2549. This option can also be set with on a per-file-basis with
  2550. #+STARTUP: nologrepeat
  2551. #+STARTUP: logrepeat
  2552. #+STARTUP: lognoterepeat
  2553. You can have local logging settings for a subtree by setting the LOGGING
  2554. property to one or more of these keywords."
  2555. :group 'org-todo
  2556. :group 'org-progress
  2557. :type '(choice
  2558. (const :tag "Don't force a record" nil)
  2559. (const :tag "Force recording the DONE state" time)
  2560. (const :tag "Force recording a note with the DONE state" note)))
  2561. (defgroup org-priorities nil
  2562. "Priorities in Org-mode."
  2563. :tag "Org Priorities"
  2564. :group 'org-todo)
  2565. (defcustom org-enable-priority-commands t
  2566. "Non-nil means priority commands are active.
  2567. When nil, these commands will be disabled, so that you never accidentally
  2568. set a priority."
  2569. :group 'org-priorities
  2570. :type 'boolean)
  2571. (defcustom org-highest-priority ?A
  2572. "The highest priority of TODO items. A character like ?A, ?B etc.
  2573. Must have a smaller ASCII number than `org-lowest-priority'."
  2574. :group 'org-priorities
  2575. :type 'character)
  2576. (defcustom org-lowest-priority ?C
  2577. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2578. Must have a larger ASCII number than `org-highest-priority'."
  2579. :group 'org-priorities
  2580. :type 'character)
  2581. (defcustom org-default-priority ?B
  2582. "The default priority of TODO items.
  2583. This is the priority an item gets if no explicit priority is given.
  2584. When starting to cycle on an empty priority the first step in the cycle
  2585. depends on `org-priority-start-cycle-with-default'. The resulting first
  2586. step priority must not exceed the range from `org-highest-priority' to
  2587. `org-lowest-priority' which means that `org-default-priority' has to be
  2588. in this range exclusive or inclusive the range boundaries. Else the
  2589. first step refuses to set the default and the second will fall back
  2590. to (depending on the command used) the highest or lowest priority."
  2591. :group 'org-priorities
  2592. :type 'character)
  2593. (defcustom org-priority-start-cycle-with-default t
  2594. "Non-nil means start with default priority when starting to cycle.
  2595. When this is nil, the first step in the cycle will be (depending on the
  2596. command used) one higher or lower than the default priority.
  2597. See also `org-default-priority'."
  2598. :group 'org-priorities
  2599. :type 'boolean)
  2600. (defcustom org-get-priority-function nil
  2601. "Function to extract the priority from a string.
  2602. The string is normally the headline. If this is nil Org computes the
  2603. priority from the priority cookie like [#A] in the headline. It returns
  2604. an integer, increasing by 1000 for each priority level.
  2605. The user can set a different function here, which should take a string
  2606. as an argument and return the numeric priority."
  2607. :group 'org-priorities
  2608. :version "24.1"
  2609. :type '(choice
  2610. (const nil)
  2611. (function)))
  2612. (defgroup org-time nil
  2613. "Options concerning time stamps and deadlines in Org-mode."
  2614. :tag "Org Time"
  2615. :group 'org)
  2616. (defcustom org-insert-labeled-timestamps-at-point nil
  2617. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2618. When nil, these labeled time stamps are forces into the second line of an
  2619. entry, just after the headline. When scheduling from the global TODO list,
  2620. the time stamp will always be forced into the second line."
  2621. :group 'org-time
  2622. :type 'boolean)
  2623. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2624. "Number of minutes to round time stamps to.
  2625. These are two values, the first applies when first creating a time stamp.
  2626. The second applies when changing it with the commands `S-up' and `S-down'.
  2627. When changing the time stamp, this means that it will change in steps
  2628. of N minutes, as given by the second value.
  2629. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2630. numbers should be factors of 60, so for example 5, 10, 15.
  2631. When this is larger than 1, you can still force an exact time stamp by using
  2632. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2633. and by using a prefix arg to `S-up/down' to specify the exact number
  2634. of minutes to shift."
  2635. :group 'org-time
  2636. :get (lambda (var) ; Make sure both elements are there
  2637. (if (integerp (default-value var))
  2638. (list (default-value var) 5)
  2639. (default-value var)))
  2640. :type '(list
  2641. (integer :tag "when inserting times")
  2642. (integer :tag "when modifying times")))
  2643. ;; Normalize old customizations of this variable.
  2644. (when (integerp org-time-stamp-rounding-minutes)
  2645. (setq org-time-stamp-rounding-minutes
  2646. (list org-time-stamp-rounding-minutes
  2647. org-time-stamp-rounding-minutes)))
  2648. (defcustom org-display-custom-times nil
  2649. "Non-nil means overlay custom formats over all time stamps.
  2650. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2651. To turn this on on a per-file basis, insert anywhere in the file:
  2652. #+STARTUP: customtime"
  2653. :group 'org-time
  2654. :set 'set-default
  2655. :type 'sexp)
  2656. (make-variable-buffer-local 'org-display-custom-times)
  2657. (defcustom org-time-stamp-custom-formats
  2658. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2659. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2660. These are overlaid over the default ISO format if the variable
  2661. `org-display-custom-times' is set. Time like %H:%M should be at the
  2662. end of the second format. The custom formats are also honored by export
  2663. commands, if custom time display is turned on at the time of export."
  2664. :group 'org-time
  2665. :type 'sexp)
  2666. (defun org-time-stamp-format (&optional long inactive)
  2667. "Get the right format for a time string."
  2668. (let ((f (if long (cdr org-time-stamp-formats)
  2669. (car org-time-stamp-formats))))
  2670. (if inactive
  2671. (concat "[" (substring f 1 -1) "]")
  2672. f)))
  2673. (defcustom org-time-clocksum-format
  2674. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2675. "The format string used when creating CLOCKSUM lines.
  2676. This is also used when Org mode generates a time duration.
  2677. The value can be a single format string containing two
  2678. %-sequences, which will be filled with the number of hours and
  2679. minutes in that order.
  2680. Alternatively, the value can be a plist associating any of the
  2681. keys :years, :months, :weeks, :days, :hours or :minutes with
  2682. format strings. The time duration is formatted using only the
  2683. time components that are needed and concatenating the results.
  2684. If a time unit in absent, it falls back to the next smallest
  2685. unit.
  2686. The keys :require-years, :require-months, :require-days,
  2687. :require-weeks, :require-hours, :require-minutes are also
  2688. meaningful. A non-nil value for these keys indicates that the
  2689. corresponding time component should always be included, even if
  2690. its value is 0.
  2691. For example,
  2692. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2693. :require-minutes t)
  2694. means durations longer than a day will be expressed in days,
  2695. hours and minutes, and durations less than a day will always be
  2696. expressed in hours and minutes (even for durations less than an
  2697. hour).
  2698. The value
  2699. \(:days \"%dd\" :minutes \"%dm\")
  2700. means durations longer than a day will be expressed in days and
  2701. minutes, and durations less than a day will be expressed entirely
  2702. in minutes (even for durations longer than an hour)."
  2703. :group 'org-time
  2704. :group 'org-clock
  2705. :version "24.4"
  2706. :package-version '(Org . "8.0")
  2707. :type '(choice (string :tag "Format string")
  2708. (set :tag "Plist"
  2709. (group :inline t (const :tag "Years" :years)
  2710. (string :tag "Format string"))
  2711. (group :inline t
  2712. (const :tag "Always show years" :require-years)
  2713. (const t))
  2714. (group :inline t (const :tag "Months" :months)
  2715. (string :tag "Format string"))
  2716. (group :inline t
  2717. (const :tag "Always show months" :require-months)
  2718. (const t))
  2719. (group :inline t (const :tag "Weeks" :weeks)
  2720. (string :tag "Format string"))
  2721. (group :inline t
  2722. (const :tag "Always show weeks" :require-weeks)
  2723. (const t))
  2724. (group :inline t (const :tag "Days" :days)
  2725. (string :tag "Format string"))
  2726. (group :inline t
  2727. (const :tag "Always show days" :require-days)
  2728. (const t))
  2729. (group :inline t (const :tag "Hours" :hours)
  2730. (string :tag "Format string"))
  2731. (group :inline t
  2732. (const :tag "Always show hours" :require-hours)
  2733. (const t))
  2734. (group :inline t (const :tag "Minutes" :minutes)
  2735. (string :tag "Format string"))
  2736. (group :inline t
  2737. (const :tag "Always show minutes" :require-minutes)
  2738. (const t)))))
  2739. (defcustom org-time-clocksum-use-fractional nil
  2740. "When non-nil, \\[org-clock-display] uses fractional times.
  2741. See `org-time-clocksum-format' for more on time clock formats."
  2742. :group 'org-time
  2743. :group 'org-clock
  2744. :version "24.3"
  2745. :type 'boolean)
  2746. (defcustom org-time-clocksum-use-effort-durations nil
  2747. "When non-nil, \\[org-clock-display] uses effort durations.
  2748. E.g. by default, one day is considered to be a 8 hours effort,
  2749. so a task that has been clocked for 16 hours will be displayed
  2750. as during 2 days in the clock display or in the clocktable.
  2751. See `org-effort-durations' on how to set effort durations
  2752. and `org-time-clocksum-format' for more on time clock formats."
  2753. :group 'org-time
  2754. :group 'org-clock
  2755. :version "24.4"
  2756. :package-version '(Org . "8.0")
  2757. :type 'boolean)
  2758. (defcustom org-time-clocksum-fractional-format "%.2f"
  2759. "The format string used when creating CLOCKSUM lines,
  2760. or when Org mode generates a time duration, if
  2761. `org-time-clocksum-use-fractional' is enabled.
  2762. The value can be a single format string containing one
  2763. %-sequence, which will be filled with the number of hours as
  2764. a float.
  2765. Alternatively, the value can be a plist associating any of the
  2766. keys :years, :months, :weeks, :days, :hours or :minutes with
  2767. a format string. The time duration is formatted using the
  2768. largest time unit which gives a non-zero integer part. If all
  2769. specified formats have zero integer part, the smallest time unit
  2770. is used."
  2771. :group 'org-time
  2772. :type '(choice (string :tag "Format string")
  2773. (set (group :inline t (const :tag "Years" :years)
  2774. (string :tag "Format string"))
  2775. (group :inline t (const :tag "Months" :months)
  2776. (string :tag "Format string"))
  2777. (group :inline t (const :tag "Weeks" :weeks)
  2778. (string :tag "Format string"))
  2779. (group :inline t (const :tag "Days" :days)
  2780. (string :tag "Format string"))
  2781. (group :inline t (const :tag "Hours" :hours)
  2782. (string :tag "Format string"))
  2783. (group :inline t (const :tag "Minutes" :minutes)
  2784. (string :tag "Format string")))))
  2785. (defcustom org-deadline-warning-days 14
  2786. "Number of days before expiration during which a deadline becomes active.
  2787. This variable governs the display in sparse trees and in the agenda.
  2788. When 0 or negative, it means use this number (the absolute value of it)
  2789. even if a deadline has a different individual lead time specified.
  2790. Custom commands can set this variable in the options section."
  2791. :group 'org-time
  2792. :group 'org-agenda-daily/weekly
  2793. :type 'integer)
  2794. (defcustom org-scheduled-delay-days 0
  2795. "Number of days before a scheduled item becomes active.
  2796. This variable governs the display in sparse trees and in the agenda.
  2797. The default value (i.e. 0) means: don't delay scheduled item.
  2798. When negative, it means use this number (the absolute value of it)
  2799. even if a scheduled item has a different individual delay time
  2800. specified.
  2801. Custom commands can set this variable in the options section."
  2802. :group 'org-time
  2803. :group 'org-agenda-daily/weekly
  2804. :version "24.4"
  2805. :package-version '(Org . "8.0")
  2806. :type 'integer)
  2807. (defcustom org-read-date-prefer-future t
  2808. "Non-nil means assume future for incomplete date input from user.
  2809. This affects the following situations:
  2810. 1. The user gives a month but not a year.
  2811. For example, if it is April and you enter \"feb 2\", this will be read
  2812. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2813. 2. The user gives a day, but no month.
  2814. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2815. read this as the third of *next* month. However, if you enter \"17\",
  2816. it will be considered as *this* month.
  2817. If you set this variable to the symbol `time', then also the following
  2818. will work:
  2819. 3. If the user gives a time.
  2820. If the time is before now, it will be interpreted as tomorrow.
  2821. Currently none of this works for ISO week specifications.
  2822. When this option is nil, the current day, month and year will always be
  2823. used as defaults.
  2824. See also `org-agenda-jump-prefer-future'."
  2825. :group 'org-time
  2826. :type '(choice
  2827. (const :tag "Never" nil)
  2828. (const :tag "Check month and day" t)
  2829. (const :tag "Check month, day, and time" time)))
  2830. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2831. "Should the agenda jump command prefer the future for incomplete dates?
  2832. The default is to do the same as configured in `org-read-date-prefer-future'.
  2833. But you can also set a deviating value here.
  2834. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2835. :group 'org-agenda
  2836. :group 'org-time
  2837. :version "24.1"
  2838. :type '(choice
  2839. (const :tag "Use org-read-date-prefer-future"
  2840. org-read-date-prefer-future)
  2841. (const :tag "Never" nil)
  2842. (const :tag "Always" t)))
  2843. (defcustom org-read-date-force-compatible-dates t
  2844. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2845. Depending on the system Emacs is running on, certain dates cannot
  2846. be represented with the type used internally to represent time.
  2847. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2848. correctly. Some systems allow for earlier dates, some for later,
  2849. some for both. One way to find out it to insert any date into an
  2850. Org buffer, putting the cursor on the year and hitting S-up and
  2851. S-down to test the range.
  2852. When this variable is set to t, the date/time prompt will not let
  2853. you specify dates outside the 1970-2037 range, so it is certain that
  2854. these dates will work in whatever version of Emacs you are
  2855. running, and also that you can move a file from one Emacs implementation
  2856. to another. WHenever Org is forcing the year for you, it will display
  2857. a message and beep.
  2858. When this variable is nil, Org will check if the date is
  2859. representable in the specific Emacs implementation you are using.
  2860. If not, it will force a year, usually the current year, and beep
  2861. to remind you. Currently this setting is not recommended because
  2862. the likelihood that you will open your Org files in an Emacs that
  2863. has limited date range is not negligible.
  2864. A workaround for this problem is to use diary sexp dates for time
  2865. stamps outside of this range."
  2866. :group 'org-time
  2867. :version "24.1"
  2868. :type 'boolean)
  2869. (defcustom org-read-date-display-live t
  2870. "Non-nil means display current interpretation of date prompt live.
  2871. This display will be in an overlay, in the minibuffer."
  2872. :group 'org-time
  2873. :type 'boolean)
  2874. (defcustom org-read-date-popup-calendar t
  2875. "Non-nil means pop up a calendar when prompting for a date.
  2876. In the calendar, the date can be selected with mouse-1. However, the
  2877. minibuffer will also be active, and you can simply enter the date as well.
  2878. When nil, only the minibuffer will be available."
  2879. :group 'org-time
  2880. :type 'boolean)
  2881. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2882. 'org-read-date-popup-calendar)
  2883. (make-obsolete-variable
  2884. 'org-read-date-minibuffer-setup-hook
  2885. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2886. (defcustom org-read-date-minibuffer-setup-hook nil
  2887. "Hook to be used to set up keys for the date/time interface.
  2888. Add key definitions to `minibuffer-local-map', which will be a
  2889. temporary copy.
  2890. WARNING: This option is obsolete, you should use
  2891. `org-read-date-minibuffer-local-map' to set up keys."
  2892. :group 'org-time
  2893. :type 'hook)
  2894. (defcustom org-extend-today-until 0
  2895. "The hour when your day really ends. Must be an integer.
  2896. This has influence for the following applications:
  2897. - When switching the agenda to \"today\". It it is still earlier than
  2898. the time given here, the day recognized as TODAY is actually yesterday.
  2899. - When a date is read from the user and it is still before the time given
  2900. here, the current date and time will be assumed to be yesterday, 23:59.
  2901. Also, timestamps inserted in capture templates follow this rule.
  2902. IMPORTANT: This is a feature whose implementation is and likely will
  2903. remain incomplete. Really, it is only here because past midnight seems to
  2904. be the favorite working time of John Wiegley :-)"
  2905. :group 'org-time
  2906. :type 'integer)
  2907. (defcustom org-use-effective-time nil
  2908. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2909. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2910. \"effective time\" of any timestamps between midnight and 8am will be
  2911. 23:59 of the previous day."
  2912. :group 'org-time
  2913. :version "24.1"
  2914. :type 'boolean)
  2915. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2916. "When non-nil, use the last clock out time for `org-todo'.
  2917. Note that this option has precedence over the combined use of
  2918. `org-use-effective-time' and `org-extend-today-until'."
  2919. :group 'org-time
  2920. :version "24.4"
  2921. :package-version '(Org . "8.0")
  2922. :type 'boolean)
  2923. (defcustom org-edit-timestamp-down-means-later nil
  2924. "Non-nil means S-down will increase the time in a time stamp.
  2925. When nil, S-up will increase."
  2926. :group 'org-time
  2927. :type 'boolean)
  2928. (defcustom org-calendar-follow-timestamp-change t
  2929. "Non-nil means make the calendar window follow timestamp changes.
  2930. When a timestamp is modified and the calendar window is visible, it will be
  2931. moved to the new date."
  2932. :group 'org-time
  2933. :type 'boolean)
  2934. (defgroup org-tags nil
  2935. "Options concerning tags in Org-mode."
  2936. :tag "Org Tags"
  2937. :group 'org)
  2938. (defcustom org-tag-alist nil
  2939. "List of tags allowed in Org-mode files.
  2940. When this list is nil, Org-mode will base TAG input on what is already in the
  2941. buffer.
  2942. The value of this variable is an alist, the car of each entry must be a
  2943. keyword as a string, the cdr may be a character that is used to select
  2944. that tag through the fast-tag-selection interface.
  2945. See the manual for details."
  2946. :group 'org-tags
  2947. :type '(repeat
  2948. (choice
  2949. (cons (string :tag "Tag name")
  2950. (character :tag "Access char"))
  2951. (list :tag "Start radio group"
  2952. (const :startgroup)
  2953. (option (string :tag "Group description")))
  2954. (list :tag "Group tags delimiter"
  2955. (const :grouptags))
  2956. (list :tag "End radio group"
  2957. (const :endgroup)
  2958. (option (string :tag "Group description")))
  2959. (const :tag "New line" (:newline)))))
  2960. (defcustom org-tag-persistent-alist nil
  2961. "List of tags that will always appear in all Org-mode files.
  2962. This is in addition to any in buffer settings or customizations
  2963. of `org-tag-alist'.
  2964. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2965. The value of this variable is an alist, the car of each entry must be a
  2966. keyword as a string, the cdr may be a character that is used to select
  2967. that tag through the fast-tag-selection interface.
  2968. See the manual for details.
  2969. To disable these tags on a per-file basis, insert anywhere in the file:
  2970. #+STARTUP: noptag"
  2971. :group 'org-tags
  2972. :type '(repeat
  2973. (choice
  2974. (cons (string :tag "Tag name")
  2975. (character :tag "Access char"))
  2976. (const :tag "Start radio group" (:startgroup))
  2977. (const :tag "Group tags delimiter" (:grouptags))
  2978. (const :tag "End radio group" (:endgroup))
  2979. (const :tag "New line" (:newline)))))
  2980. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2981. "If non-nil, always offer completion for all tags of all agenda files.
  2982. Instead of customizing this variable directly, you might want to
  2983. set it locally for capture buffers, because there no list of
  2984. tags in that file can be created dynamically (there are none).
  2985. (add-hook 'org-capture-mode-hook
  2986. (lambda ()
  2987. (set (make-local-variable
  2988. 'org-complete-tags-always-offer-all-agenda-tags)
  2989. t)))"
  2990. :group 'org-tags
  2991. :version "24.1"
  2992. :type 'boolean)
  2993. (defvar org-file-tags nil
  2994. "List of tags that can be inherited by all entries in the file.
  2995. The tags will be inherited if the variable `org-use-tag-inheritance'
  2996. says they should be.
  2997. This variable is populated from #+FILETAGS lines.")
  2998. (defcustom org-use-fast-tag-selection 'auto
  2999. "Non-nil means use fast tag selection scheme.
  3000. This is a special interface to select and deselect tags with single keys.
  3001. When nil, fast selection is never used.
  3002. When the symbol `auto', fast selection is used if and only if selection
  3003. characters for tags have been configured, either through the variable
  3004. `org-tag-alist' or through a #+TAGS line in the buffer.
  3005. When t, fast selection is always used and selection keys are assigned
  3006. automatically if necessary."
  3007. :group 'org-tags
  3008. :type '(choice
  3009. (const :tag "Always" t)
  3010. (const :tag "Never" nil)
  3011. (const :tag "When selection characters are configured" auto)))
  3012. (defcustom org-fast-tag-selection-single-key nil
  3013. "Non-nil means fast tag selection exits after first change.
  3014. When nil, you have to press RET to exit it.
  3015. During fast tag selection, you can toggle this flag with `C-c'.
  3016. This variable can also have the value `expert'. In this case, the window
  3017. displaying the tags menu is not even shown, until you press C-c again."
  3018. :group 'org-tags
  3019. :type '(choice
  3020. (const :tag "No" nil)
  3021. (const :tag "Yes" t)
  3022. (const :tag "Expert" expert)))
  3023. (defvar org-fast-tag-selection-include-todo nil
  3024. "Non-nil means fast tags selection interface will also offer TODO states.
  3025. This is an undocumented feature, you should not rely on it.")
  3026. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3027. "The column to which tags should be indented in a headline.
  3028. If this number is positive, it specifies the column. If it is negative,
  3029. it means that the tags should be flushright to that column. For example,
  3030. -80 works well for a normal 80 character screen.
  3031. When 0, place tags directly after headline text, with only one space in
  3032. between."
  3033. :group 'org-tags
  3034. :type 'integer)
  3035. (defcustom org-auto-align-tags t
  3036. "Non-nil keeps tags aligned when modifying headlines.
  3037. Some operations (i.e. demoting) change the length of a headline and
  3038. therefore shift the tags around. With this option turned on, after
  3039. each such operation the tags are again aligned to `org-tags-column'."
  3040. :group 'org-tags
  3041. :type 'boolean)
  3042. (defcustom org-use-tag-inheritance t
  3043. "Non-nil means tags in levels apply also for sublevels.
  3044. When nil, only the tags directly given in a specific line apply there.
  3045. This may also be a list of tags that should be inherited, or a regexp that
  3046. matches tags that should be inherited. Additional control is possible
  3047. with the variable `org-tags-exclude-from-inheritance' which gives an
  3048. explicit list of tags to be excluded from inheritance, even if the value of
  3049. `org-use-tag-inheritance' would select it for inheritance.
  3050. If this option is t, a match early-on in a tree can lead to a large
  3051. number of matches in the subtree when constructing the agenda or creating
  3052. a sparse tree. If you only want to see the first match in a tree during
  3053. a search, check out the variable `org-tags-match-list-sublevels'."
  3054. :group 'org-tags
  3055. :type '(choice
  3056. (const :tag "Not" nil)
  3057. (const :tag "Always" t)
  3058. (repeat :tag "Specific tags" (string :tag "Tag"))
  3059. (regexp :tag "Tags matched by regexp")))
  3060. (defcustom org-tags-exclude-from-inheritance nil
  3061. "List of tags that should never be inherited.
  3062. This is a way to exclude a few tags from inheritance. For way to do
  3063. the opposite, to actively allow inheritance for selected tags,
  3064. see the variable `org-use-tag-inheritance'."
  3065. :group 'org-tags
  3066. :type '(repeat (string :tag "Tag")))
  3067. (defun org-tag-inherit-p (tag)
  3068. "Check if TAG is one that should be inherited."
  3069. (cond
  3070. ((member tag org-tags-exclude-from-inheritance) nil)
  3071. ((eq org-use-tag-inheritance t) t)
  3072. ((not org-use-tag-inheritance) nil)
  3073. ((stringp org-use-tag-inheritance)
  3074. (string-match org-use-tag-inheritance tag))
  3075. ((listp org-use-tag-inheritance)
  3076. (member tag org-use-tag-inheritance))
  3077. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3078. (defcustom org-tags-match-list-sublevels t
  3079. "Non-nil means list also sublevels of headlines matching a search.
  3080. This variable applies to tags/property searches, and also to stuck
  3081. projects because this search is based on a tags match as well.
  3082. When set to the symbol `indented', sublevels are indented with
  3083. leading dots.
  3084. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3085. the sublevels of a headline matching a tag search often also match
  3086. the same search. Listing all of them can create very long lists.
  3087. Setting this variable to nil causes subtrees of a match to be skipped.
  3088. This variable is semi-obsolete and probably should always be true. It
  3089. is better to limit inheritance to certain tags using the variables
  3090. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3091. :group 'org-tags
  3092. :type '(choice
  3093. (const :tag "No, don't list them" nil)
  3094. (const :tag "Yes, do list them" t)
  3095. (const :tag "List them, indented with leading dots" indented)))
  3096. (defcustom org-tags-sort-function nil
  3097. "When set, tags are sorted using this function as a comparator."
  3098. :group 'org-tags
  3099. :type '(choice
  3100. (const :tag "No sorting" nil)
  3101. (const :tag "Alphabetical" string<)
  3102. (const :tag "Reverse alphabetical" string>)
  3103. (function :tag "Custom function" nil)))
  3104. (defvar org-tags-history nil
  3105. "History of minibuffer reads for tags.")
  3106. (defvar org-last-tags-completion-table nil
  3107. "The last used completion table for tags.")
  3108. (defvar org-after-tags-change-hook nil
  3109. "Hook that is run after the tags in a line have changed.")
  3110. (defgroup org-properties nil
  3111. "Options concerning properties in Org-mode."
  3112. :tag "Org Properties"
  3113. :group 'org)
  3114. (defcustom org-property-format "%-10s %s"
  3115. "How property key/value pairs should be formatted by `indent-line'.
  3116. When `indent-line' hits a property definition, it will format the line
  3117. according to this format, mainly to make sure that the values are
  3118. lined-up with respect to each other."
  3119. :group 'org-properties
  3120. :type 'string)
  3121. (defcustom org-properties-postprocess-alist nil
  3122. "Alist of properties and functions to adjust inserted values.
  3123. Elements of this alist must be of the form
  3124. ([string] [function])
  3125. where [string] must be a property name and [function] must be a
  3126. lambda expression: this lambda expression must take one argument,
  3127. the value to adjust, and return the new value as a string.
  3128. For example, this element will allow the property \"Remaining\"
  3129. to be updated wrt the relation between the \"Effort\" property
  3130. and the clock summary:
  3131. ((\"Remaining\" (lambda(value)
  3132. (let ((clocksum (org-clock-sum-current-item))
  3133. (effort (org-duration-string-to-minutes
  3134. (org-entry-get (point) \"Effort\"))))
  3135. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3136. :group 'org-properties
  3137. :version "24.1"
  3138. :type '(alist :key-type (string :tag "Property")
  3139. :value-type (function :tag "Function")))
  3140. (defcustom org-use-property-inheritance nil
  3141. "Non-nil means properties apply also for sublevels.
  3142. This setting is chiefly used during property searches. Turning it on can
  3143. cause significant overhead when doing a search, which is why it is not
  3144. on by default.
  3145. When nil, only the properties directly given in the current entry count.
  3146. When t, every property is inherited. The value may also be a list of
  3147. properties that should have inheritance, or a regular expression matching
  3148. properties that should be inherited.
  3149. However, note that some special properties use inheritance under special
  3150. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3151. and the properties ending in \"_ALL\" when they are used as descriptor
  3152. for valid values of a property.
  3153. Note for programmers:
  3154. When querying an entry with `org-entry-get', you can control if inheritance
  3155. should be used. By default, `org-entry-get' looks only at the local
  3156. properties. You can request inheritance by setting the inherit argument
  3157. to t (to force inheritance) or to `selective' (to respect the setting
  3158. in this variable)."
  3159. :group 'org-properties
  3160. :type '(choice
  3161. (const :tag "Not" nil)
  3162. (const :tag "Always" t)
  3163. (repeat :tag "Specific properties" (string :tag "Property"))
  3164. (regexp :tag "Properties matched by regexp")))
  3165. (defun org-property-inherit-p (property)
  3166. "Check if PROPERTY is one that should be inherited."
  3167. (cond
  3168. ((eq org-use-property-inheritance t) t)
  3169. ((not org-use-property-inheritance) nil)
  3170. ((stringp org-use-property-inheritance)
  3171. (string-match org-use-property-inheritance property))
  3172. ((listp org-use-property-inheritance)
  3173. (member property org-use-property-inheritance))
  3174. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3175. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3176. "The default column format, if no other format has been defined.
  3177. This variable can be set on the per-file basis by inserting a line
  3178. #+COLUMNS: %25ITEM ....."
  3179. :group 'org-properties
  3180. :type 'string)
  3181. (defcustom org-columns-ellipses ".."
  3182. "The ellipses to be used when a field in column view is truncated.
  3183. When this is the empty string, as many characters as possible are shown,
  3184. but then there will be no visual indication that the field has been truncated.
  3185. When this is a string of length N, the last N characters of a truncated
  3186. field are replaced by this string. If the column is narrower than the
  3187. ellipses string, only part of the ellipses string will be shown."
  3188. :group 'org-properties
  3189. :type 'string)
  3190. (defcustom org-columns-modify-value-for-display-function nil
  3191. "Function that modifies values for display in column view.
  3192. For example, it can be used to cut out a certain part from a time stamp.
  3193. The function must take 2 arguments:
  3194. column-title The title of the column (*not* the property name)
  3195. value The value that should be modified.
  3196. The function should return the value that should be displayed,
  3197. or nil if the normal value should be used."
  3198. :group 'org-properties
  3199. :type '(choice (const nil) (function)))
  3200. (defconst org-global-properties-fixed
  3201. '(("VISIBILITY_ALL" . "folded children content all")
  3202. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3203. "List of property/value pairs that can be inherited by any entry.
  3204. These are fixed values, for the preset properties. The user variable
  3205. that can be used to add to this list is `org-global-properties'.
  3206. The entries in this list are cons cells where the car is a property
  3207. name and cdr is a string with the value. If the value represents
  3208. multiple items like an \"_ALL\" property, separate the items by
  3209. spaces.")
  3210. (defcustom org-global-properties nil
  3211. "List of property/value pairs that can be inherited by any entry.
  3212. This list will be combined with the constant `org-global-properties-fixed'.
  3213. The entries in this list are cons cells where the car is a property
  3214. name and cdr is a string with the value.
  3215. You can set buffer-local values for the same purpose in the variable
  3216. `org-file-properties' this by adding lines like
  3217. #+PROPERTY: NAME VALUE"
  3218. :group 'org-properties
  3219. :type '(repeat
  3220. (cons (string :tag "Property")
  3221. (string :tag "Value"))))
  3222. (defvar org-file-properties nil
  3223. "List of property/value pairs that can be inherited by any entry.
  3224. Valid for the current buffer.
  3225. This variable is populated from #+PROPERTY lines.")
  3226. (make-variable-buffer-local 'org-file-properties)
  3227. (defgroup org-agenda nil
  3228. "Options concerning agenda views in Org-mode."
  3229. :tag "Org Agenda"
  3230. :group 'org)
  3231. (defvar org-category nil
  3232. "Variable used by org files to set a category for agenda display.
  3233. Such files should use a file variable to set it, for example
  3234. # -*- mode: org; org-category: \"ELisp\"
  3235. or contain a special line
  3236. #+CATEGORY: ELisp
  3237. If the file does not specify a category, then file's base name
  3238. is used instead.")
  3239. (make-variable-buffer-local 'org-category)
  3240. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3241. (defcustom org-agenda-files nil
  3242. "The files to be used for agenda display.
  3243. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3244. \\[org-remove-file]. You can also use customize to edit the list.
  3245. If an entry is a directory, all files in that directory that are matched by
  3246. `org-agenda-file-regexp' will be part of the file list.
  3247. If the value of the variable is not a list but a single file name, then
  3248. the list of agenda files is actually stored and maintained in that file, one
  3249. agenda file per line. In this file paths can be given relative to
  3250. `org-directory'. Tilde expansion and environment variable substitution
  3251. are also made."
  3252. :group 'org-agenda
  3253. :type '(choice
  3254. (repeat :tag "List of files and directories" file)
  3255. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3256. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3257. "Regular expression to match files for `org-agenda-files'.
  3258. If any element in the list in that variable contains a directory instead
  3259. of a normal file, all files in that directory that are matched by this
  3260. regular expression will be included."
  3261. :group 'org-agenda
  3262. :type 'regexp)
  3263. (defcustom org-agenda-text-search-extra-files nil
  3264. "List of extra files to be searched by text search commands.
  3265. These files will be searched in addition to the agenda files by the
  3266. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3267. Note that these files will only be searched for text search commands,
  3268. not for the other agenda views like todo lists, tag searches or the weekly
  3269. agenda. This variable is intended to list notes and possibly archive files
  3270. that should also be searched by these two commands.
  3271. In fact, if the first element in the list is the symbol `agenda-archives',
  3272. then all archive files of all agenda files will be added to the search
  3273. scope."
  3274. :group 'org-agenda
  3275. :type '(set :greedy t
  3276. (const :tag "Agenda Archives" agenda-archives)
  3277. (repeat :inline t (file))))
  3278. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3279. 'org-agenda-text-search-extra-files)
  3280. (defcustom org-agenda-skip-unavailable-files nil
  3281. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3282. A nil value means to remove them, after a query, from the list."
  3283. :group 'org-agenda
  3284. :type 'boolean)
  3285. (defcustom org-calendar-to-agenda-key [?c]
  3286. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3287. The command `org-calendar-goto-agenda' will be bound to this key. The
  3288. default is the character `c' because then `c' can be used to switch back and
  3289. forth between agenda and calendar."
  3290. :group 'org-agenda
  3291. :type 'sexp)
  3292. (defcustom org-calendar-insert-diary-entry-key [?i]
  3293. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3294. This option is irrelevant until `org-agenda-diary-file' has been configured
  3295. to point to an Org-mode file. When that is the case, the command
  3296. `org-agenda-diary-entry' will be bound to the key given here, by default
  3297. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3298. if you want to continue doing this, you need to change this to a different
  3299. key."
  3300. :group 'org-agenda
  3301. :type 'sexp)
  3302. (defcustom org-agenda-diary-file 'diary-file
  3303. "File to which to add new entries with the `i' key in agenda and calendar.
  3304. When this is the symbol `diary-file', the functionality in the Emacs
  3305. calendar will be used to add entries to the `diary-file'. But when this
  3306. points to a file, `org-agenda-diary-entry' will be used instead."
  3307. :group 'org-agenda
  3308. :type '(choice
  3309. (const :tag "The standard Emacs diary file" diary-file)
  3310. (file :tag "Special Org file diary entries")))
  3311. (eval-after-load "calendar"
  3312. '(progn
  3313. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3314. 'org-calendar-goto-agenda)
  3315. (add-hook 'calendar-mode-hook
  3316. (lambda ()
  3317. (unless (eq org-agenda-diary-file 'diary-file)
  3318. (define-key calendar-mode-map
  3319. org-calendar-insert-diary-entry-key
  3320. 'org-agenda-diary-entry))))))
  3321. (defgroup org-latex nil
  3322. "Options for embedding LaTeX code into Org-mode."
  3323. :tag "Org LaTeX"
  3324. :group 'org)
  3325. (defcustom org-format-latex-options
  3326. '(:foreground default :background default :scale 1.0
  3327. :html-foreground "Black" :html-background "Transparent"
  3328. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3329. "Options for creating images from LaTeX fragments.
  3330. This is a property list with the following properties:
  3331. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3332. `default' means use the foreground of the default face.
  3333. `auto' means use the foreground from the text face.
  3334. :background the background color, or \"Transparent\".
  3335. `default' means use the background of the default face.
  3336. `auto' means use the background from the text face.
  3337. :scale a scaling factor for the size of the images, to get more pixels
  3338. :html-foreground, :html-background, :html-scale
  3339. the same numbers for HTML export.
  3340. :matchers a list indicating which matchers should be used to
  3341. find LaTeX fragments. Valid members of this list are:
  3342. \"begin\" find environments
  3343. \"$1\" find single characters surrounded by $.$
  3344. \"$\" find math expressions surrounded by $...$
  3345. \"$$\" find math expressions surrounded by $$....$$
  3346. \"\\(\" find math expressions surrounded by \\(...\\)
  3347. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3348. :group 'org-latex
  3349. :type 'plist)
  3350. (defcustom org-format-latex-signal-error t
  3351. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3352. When nil, just push out a message."
  3353. :group 'org-latex
  3354. :version "24.1"
  3355. :type 'boolean)
  3356. (defcustom org-latex-to-mathml-jar-file nil
  3357. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3358. Use this to specify additional executable file say a jar file.
  3359. When using MathToWeb as the converter, specify the full-path to
  3360. your mathtoweb.jar file."
  3361. :group 'org-latex
  3362. :version "24.1"
  3363. :type '(choice
  3364. (const :tag "None" nil)
  3365. (file :tag "JAR file" :must-match t)))
  3366. (defcustom org-latex-to-mathml-convert-command nil
  3367. "Command to convert LaTeX fragments to MathML.
  3368. Replace format-specifiers in the command as noted below and use
  3369. `shell-command' to convert LaTeX to MathML.
  3370. %j: Executable file in fully expanded form as specified by
  3371. `org-latex-to-mathml-jar-file'.
  3372. %I: Input LaTeX file in fully expanded form
  3373. %o: Output MathML file
  3374. This command is used by `org-create-math-formula'.
  3375. When using MathToWeb as the converter, set this to
  3376. \"java -jar %j -unicode -force -df %o %I\"."
  3377. :group 'org-latex
  3378. :version "24.1"
  3379. :type '(choice
  3380. (const :tag "None" nil)
  3381. (string :tag "\nShell command")))
  3382. (defcustom org-latex-create-formula-image-program 'dvipng
  3383. "Program to convert LaTeX fragments with.
  3384. dvipng Process the LaTeX fragments to dvi file, then convert
  3385. dvi files to png files using dvipng.
  3386. This will also include processing of non-math environments.
  3387. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3388. to convert pdf files to png files"
  3389. :group 'org-latex
  3390. :version "24.1"
  3391. :type '(choice
  3392. (const :tag "dvipng" dvipng)
  3393. (const :tag "imagemagick" imagemagick)))
  3394. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3395. "Path to store latex preview images.
  3396. A relative path here creates many directories relative to the
  3397. processed org files paths. An absolute path puts all preview
  3398. images at the same place."
  3399. :group 'org-latex
  3400. :version "24.3"
  3401. :type 'string)
  3402. (defun org-format-latex-mathml-available-p ()
  3403. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3404. (save-match-data
  3405. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3406. org-latex-to-mathml-convert-command)
  3407. (let ((executable (car (split-string
  3408. org-latex-to-mathml-convert-command))))
  3409. (when (executable-find executable)
  3410. (if (string-match
  3411. "%j" org-latex-to-mathml-convert-command)
  3412. (file-readable-p org-latex-to-mathml-jar-file)
  3413. t))))))
  3414. (defcustom org-format-latex-header "\\documentclass{article}
  3415. \\usepackage[usenames]{color}
  3416. \[PACKAGES]
  3417. \[DEFAULT-PACKAGES]
  3418. \\pagestyle{empty} % do not remove
  3419. % The settings below are copied from fullpage.sty
  3420. \\setlength{\\textwidth}{\\paperwidth}
  3421. \\addtolength{\\textwidth}{-3cm}
  3422. \\setlength{\\oddsidemargin}{1.5cm}
  3423. \\addtolength{\\oddsidemargin}{-2.54cm}
  3424. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3425. \\setlength{\\textheight}{\\paperheight}
  3426. \\addtolength{\\textheight}{-\\headheight}
  3427. \\addtolength{\\textheight}{-\\headsep}
  3428. \\addtolength{\\textheight}{-\\footskip}
  3429. \\addtolength{\\textheight}{-3cm}
  3430. \\setlength{\\topmargin}{1.5cm}
  3431. \\addtolength{\\topmargin}{-2.54cm}"
  3432. "The document header used for processing LaTeX fragments.
  3433. It is imperative that this header make sure that no page number
  3434. appears on the page. The package defined in the variables
  3435. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3436. will either replace the placeholder \"[PACKAGES]\" in this
  3437. header, or they will be appended."
  3438. :group 'org-latex
  3439. :type 'string)
  3440. (defun org-set-packages-alist (var val)
  3441. "Set the packages alist and make sure it has 3 elements per entry."
  3442. (set var (mapcar (lambda (x)
  3443. (if (and (consp x) (= (length x) 2))
  3444. (list (car x) (nth 1 x) t)
  3445. x))
  3446. val)))
  3447. (defun org-get-packages-alist (var)
  3448. "Get the packages alist and make sure it has 3 elements per entry."
  3449. (mapcar (lambda (x)
  3450. (if (and (consp x) (= (length x) 2))
  3451. (list (car x) (nth 1 x) t)
  3452. x))
  3453. (default-value var)))
  3454. (defcustom org-latex-default-packages-alist
  3455. '(("AUTO" "inputenc" t)
  3456. ("T1" "fontenc" t)
  3457. ("" "fixltx2e" nil)
  3458. ("" "graphicx" t)
  3459. ("" "longtable" nil)
  3460. ("" "float" nil)
  3461. ("" "wrapfig" nil)
  3462. ("" "rotating" nil)
  3463. ("normalem" "ulem" t)
  3464. ("" "amsmath" t)
  3465. ("" "textcomp" t)
  3466. ("" "marvosym" t)
  3467. ("" "wasysym" t)
  3468. ("" "amssymb" t)
  3469. ("" "capt-of" nil)
  3470. ("" "hyperref" nil)
  3471. "\\tolerance=1000")
  3472. "Alist of default packages to be inserted in the header.
  3473. Change this only if one of the packages here causes an
  3474. incompatibility with another package you are using.
  3475. The packages in this list are needed by one part or another of
  3476. Org mode to function properly:
  3477. - inputenc, fontenc: for basic font and character selection
  3478. - fixltx2e: Important patches of LaTeX itself
  3479. - graphicx: for including images
  3480. - longtable: For multipage tables
  3481. - float, wrapfig: for figure placement
  3482. - rotating: for sideways figures and tables
  3483. - ulem: for underline and strike-through
  3484. - amsmath: for subscript and superscript and math environments
  3485. - textcomp, marvosymb, wasysym, amssymb: for various symbols used
  3486. for interpreting the entities in `org-entities'. You can skip
  3487. some of these packages if you don't use any of their symbols.
  3488. - capt-of: for captions outside of floats
  3489. - hyperref: for cross references
  3490. Therefore you should not modify this variable unless you know
  3491. what you are doing. The one reason to change it anyway is that
  3492. you might be loading some other package that conflicts with one
  3493. of the default packages. Each element is either a cell or
  3494. a string.
  3495. A cell is of the format:
  3496. \( \"options\" \"package\" SNIPPET-FLAG).
  3497. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3498. when compiling LaTeX snippets into images for inclusion into
  3499. non-LaTeX output.
  3500. A string will be inserted as-is in the header of the document."
  3501. :group 'org-latex
  3502. :group 'org-export-latex
  3503. :set 'org-set-packages-alist
  3504. :get 'org-get-packages-alist
  3505. :version "24.1"
  3506. :type '(repeat
  3507. (choice
  3508. (list :tag "options/package pair"
  3509. (string :tag "options")
  3510. (string :tag "package")
  3511. (boolean :tag "Snippet"))
  3512. (string :tag "A line of LaTeX"))))
  3513. (defcustom org-latex-packages-alist nil
  3514. "Alist of packages to be inserted in every LaTeX header.
  3515. These will be inserted after `org-latex-default-packages-alist'.
  3516. Each element is either a cell or a string.
  3517. A cell is of the format:
  3518. \(\"options\" \"package\" SNIPPET-FLAG)
  3519. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3520. needed when turning LaTeX snippets into images for inclusion into
  3521. non-LaTeX output.
  3522. A string will be inserted as-is in the header of the document.
  3523. Make sure that you only list packages here which:
  3524. - you want in every file;
  3525. - do not conflict with the setup in `org-format-latex-header';
  3526. - do not conflict with the default packages in
  3527. `org-latex-default-packages-alist'."
  3528. :group 'org-latex
  3529. :group 'org-export-latex
  3530. :set 'org-set-packages-alist
  3531. :get 'org-get-packages-alist
  3532. :type '(repeat
  3533. (choice
  3534. (list :tag "options/package pair"
  3535. (string :tag "options")
  3536. (string :tag "package")
  3537. (boolean :tag "Snippet"))
  3538. (string :tag "A line of LaTeX"))))
  3539. (defgroup org-appearance nil
  3540. "Settings for Org-mode appearance."
  3541. :tag "Org Appearance"
  3542. :group 'org)
  3543. (defcustom org-level-color-stars-only nil
  3544. "Non-nil means fontify only the stars in each headline.
  3545. When nil, the entire headline is fontified.
  3546. Changing it requires restart of `font-lock-mode' to become effective
  3547. also in regions already fontified."
  3548. :group 'org-appearance
  3549. :type 'boolean)
  3550. (defcustom org-hide-leading-stars nil
  3551. "Non-nil means hide the first N-1 stars in a headline.
  3552. This works by using the face `org-hide' for these stars. This
  3553. face is white for a light background, and black for a dark
  3554. background. You may have to customize the face `org-hide' to
  3555. make this work.
  3556. Changing it requires restart of `font-lock-mode' to become effective
  3557. also in regions already fontified.
  3558. You may also set this on a per-file basis by adding one of the following
  3559. lines to the buffer:
  3560. #+STARTUP: hidestars
  3561. #+STARTUP: showstars"
  3562. :group 'org-appearance
  3563. :type 'boolean)
  3564. (defcustom org-hidden-keywords nil
  3565. "List of symbols corresponding to keywords to be hidden the org buffer.
  3566. For example, a value '(title) for this list will make the document's title
  3567. appear in the buffer without the initial #+TITLE: keyword."
  3568. :group 'org-appearance
  3569. :version "24.1"
  3570. :type '(set (const :tag "#+AUTHOR" author)
  3571. (const :tag "#+DATE" date)
  3572. (const :tag "#+EMAIL" email)
  3573. (const :tag "#+TITLE" title)))
  3574. (defcustom org-custom-properties nil
  3575. "List of properties (as strings) with a special meaning.
  3576. The default use of these custom properties is to let the user
  3577. hide them with `org-toggle-custom-properties-visibility'."
  3578. :group 'org-properties
  3579. :group 'org-appearance
  3580. :version "24.3"
  3581. :type '(repeat (string :tag "Property Name")))
  3582. (defcustom org-fontify-done-headline nil
  3583. "Non-nil means change the face of a headline if it is marked DONE.
  3584. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3585. When this is non-nil, the headline after the keyword is set to the
  3586. `org-headline-done' as an additional indication."
  3587. :group 'org-appearance
  3588. :type 'boolean)
  3589. (defcustom org-fontify-emphasized-text t
  3590. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3591. Changing this variable requires a restart of Emacs to take effect."
  3592. :group 'org-appearance
  3593. :type 'boolean)
  3594. (defcustom org-fontify-whole-heading-line nil
  3595. "Non-nil means fontify the whole line for headings.
  3596. This is useful when setting a background color for the
  3597. org-level-* faces."
  3598. :group 'org-appearance
  3599. :type 'boolean)
  3600. (defcustom org-highlight-latex-and-related nil
  3601. "Non-nil means highlight LaTeX related syntax in the buffer.
  3602. When non nil, the value should be a list containing any of the
  3603. following symbols:
  3604. `latex' Highlight LaTeX snippets and environments.
  3605. `script' Highlight subscript and superscript.
  3606. `entities' Highlight entities."
  3607. :group 'org-appearance
  3608. :version "24.4"
  3609. :package-version '(Org . "8.0")
  3610. :type '(choice
  3611. (const :tag "No highlighting" nil)
  3612. (set :greedy t :tag "Highlight"
  3613. (const :tag "LaTeX snippets and environments" latex)
  3614. (const :tag "Subscript and superscript" script)
  3615. (const :tag "Entities" entities))))
  3616. (defcustom org-hide-emphasis-markers nil
  3617. "Non-nil mean font-lock should hide the emphasis marker characters."
  3618. :group 'org-appearance
  3619. :type 'boolean)
  3620. (defcustom org-hide-macro-markers nil
  3621. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3622. :group 'org-appearance
  3623. :type 'boolean)
  3624. (defcustom org-pretty-entities nil
  3625. "Non-nil means show entities as UTF8 characters.
  3626. When nil, the \\name form remains in the buffer."
  3627. :group 'org-appearance
  3628. :version "24.1"
  3629. :type 'boolean)
  3630. (defcustom org-pretty-entities-include-sub-superscripts t
  3631. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3632. :group 'org-appearance
  3633. :version "24.1"
  3634. :type 'boolean)
  3635. (defvar org-emph-re nil
  3636. "Regular expression for matching emphasis.
  3637. After a match, the match groups contain these elements:
  3638. 0 The match of the full regular expression, including the characters
  3639. before and after the proper match
  3640. 1 The character before the proper match, or empty at beginning of line
  3641. 2 The proper match, including the leading and trailing markers
  3642. 3 The leading marker like * or /, indicating the type of highlighting
  3643. 4 The text between the emphasis markers, not including the markers
  3644. 5 The character after the match, empty at the end of a line")
  3645. (defvar org-verbatim-re nil
  3646. "Regular expression for matching verbatim text.")
  3647. (defvar org-emphasis-regexp-components) ; defined just below
  3648. (defvar org-emphasis-alist) ; defined just below
  3649. (defun org-set-emph-re (var val)
  3650. "Set variable and compute the emphasis regular expression."
  3651. (set var val)
  3652. (when (and (boundp 'org-emphasis-alist)
  3653. (boundp 'org-emphasis-regexp-components)
  3654. org-emphasis-alist org-emphasis-regexp-components)
  3655. (let* ((e org-emphasis-regexp-components)
  3656. (pre (car e))
  3657. (post (nth 1 e))
  3658. (border (nth 2 e))
  3659. (body (nth 3 e))
  3660. (nl (nth 4 e))
  3661. (body1 (concat body "*?"))
  3662. (markers (mapconcat 'car org-emphasis-alist ""))
  3663. (vmarkers (mapconcat
  3664. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3665. org-emphasis-alist "")))
  3666. ;; make sure special characters appear at the right position in the class
  3667. (if (string-match "\\^" markers)
  3668. (setq markers (concat (replace-match "" t t markers) "^")))
  3669. (if (string-match "-" markers)
  3670. (setq markers (concat (replace-match "" t t markers) "-")))
  3671. (if (string-match "\\^" vmarkers)
  3672. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3673. (if (string-match "-" vmarkers)
  3674. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3675. (if (> nl 0)
  3676. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3677. (int-to-string nl) "\\}")))
  3678. ;; Make the regexp
  3679. (setq org-emph-re
  3680. (concat "\\([" pre "]\\|^\\)"
  3681. "\\("
  3682. "\\([" markers "]\\)"
  3683. "\\("
  3684. "[^" border "]\\|"
  3685. "[^" border "]"
  3686. body1
  3687. "[^" border "]"
  3688. "\\)"
  3689. "\\3\\)"
  3690. "\\([" post "]\\|$\\)"))
  3691. (setq org-verbatim-re
  3692. (concat "\\([" pre "]\\|^\\)"
  3693. "\\("
  3694. "\\([" vmarkers "]\\)"
  3695. "\\("
  3696. "[^" border "]\\|"
  3697. "[^" border "]"
  3698. body1
  3699. "[^" border "]"
  3700. "\\)"
  3701. "\\3\\)"
  3702. "\\([" post "]\\|$\\)")))))
  3703. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3704. ;; set this option proved cumbersome. See this message/thread:
  3705. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3706. (defvar org-emphasis-regexp-components
  3707. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n,\"'" "." 1)
  3708. "Components used to build the regular expression for emphasis.
  3709. This is a list with five entries. Terminology: In an emphasis string
  3710. like \" *strong word* \", we call the initial space PREMATCH, the final
  3711. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3712. and \"trong wor\" is the body. The different components in this variable
  3713. specify what is allowed/forbidden in each part:
  3714. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3715. post Chars allowed as postmatch. End of line will be allowed too.
  3716. border The chars *forbidden* as border characters.
  3717. body-regexp A regexp like \".\" to match a body character. Don't use
  3718. non-shy groups here, and don't allow newline here.
  3719. newline The maximum number of newlines allowed in an emphasis exp.
  3720. You need to reload Org or to restart Emacs after customizing this.")
  3721. (defcustom org-emphasis-alist
  3722. `(("*" bold)
  3723. ("/" italic)
  3724. ("_" underline)
  3725. ("=" org-verbatim verbatim)
  3726. ("~" org-code verbatim)
  3727. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3728. "Alist of characters and faces to emphasize text.
  3729. Text starting and ending with a special character will be emphasized,
  3730. for example *bold*, _underlined_ and /italic/. This variable sets the
  3731. marker characters and the face to be used by font-lock for highlighting
  3732. in Org-mode Emacs buffers.
  3733. You need to reload Org or to restart Emacs after customizing this."
  3734. :group 'org-appearance
  3735. :set 'org-set-emph-re
  3736. :version "24.4"
  3737. :package-version '(Org . "8.0")
  3738. :type '(repeat
  3739. (list
  3740. (string :tag "Marker character")
  3741. (choice
  3742. (face :tag "Font-lock-face")
  3743. (plist :tag "Face property list"))
  3744. (option (const verbatim)))))
  3745. (defvar org-protecting-blocks
  3746. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3747. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3748. This is needed for font-lock setup.")
  3749. ;;; Miscellaneous options
  3750. (defgroup org-completion nil
  3751. "Completion in Org-mode."
  3752. :tag "Org Completion"
  3753. :group 'org)
  3754. (defcustom org-completion-use-ido nil
  3755. "Non-nil means use ido completion wherever possible.
  3756. Note that `ido-mode' must be active for this variable to be relevant.
  3757. If you decide to turn this variable on, you might well want to turn off
  3758. `org-outline-path-complete-in-steps'.
  3759. See also `org-completion-use-iswitchb'."
  3760. :group 'org-completion
  3761. :type 'boolean)
  3762. (defcustom org-completion-use-iswitchb nil
  3763. "Non-nil means use iswitchb completion wherever possible.
  3764. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3765. If you decide to turn this variable on, you might well want to turn off
  3766. `org-outline-path-complete-in-steps'.
  3767. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3768. :group 'org-completion
  3769. :type 'boolean)
  3770. (defcustom org-completion-fallback-command 'hippie-expand
  3771. "The expansion command called by \\[pcomplete] in normal context.
  3772. Normal means, no org-mode-specific context."
  3773. :group 'org-completion
  3774. :type 'function)
  3775. ;;; Functions and variables from their packages
  3776. ;; Declared here to avoid compiler warnings
  3777. ;; XEmacs only
  3778. (defvar outline-mode-menu-heading)
  3779. (defvar outline-mode-menu-show)
  3780. (defvar outline-mode-menu-hide)
  3781. (defvar zmacs-regions) ; XEmacs regions
  3782. ;; Emacs only
  3783. (defvar mark-active)
  3784. ;; Various packages
  3785. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3786. (declare-function calendar-forward-day "cal-move" (arg))
  3787. (declare-function calendar-goto-date "cal-move" (date))
  3788. (declare-function calendar-goto-today "cal-move" ())
  3789. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3790. (defvar calc-embedded-close-formula)
  3791. (defvar calc-embedded-open-formula)
  3792. (declare-function cdlatex-tab "ext:cdlatex" ())
  3793. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3794. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3795. (defvar font-lock-unfontify-region-function)
  3796. (declare-function iswitchb-read-buffer "iswitchb"
  3797. (prompt &optional default require-match start matches-set))
  3798. (defvar iswitchb-temp-buflist)
  3799. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3800. (defvar org-agenda-tags-todo-honor-ignore-options)
  3801. (declare-function org-agenda-skip "org-agenda" ())
  3802. (declare-function
  3803. org-agenda-format-item "org-agenda"
  3804. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3805. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3806. (declare-function org-agenda-change-all-lines "org-agenda"
  3807. (newhead hdmarker &optional fixface just-this))
  3808. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3809. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3810. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3811. (beg end))
  3812. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3813. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3814. "org-agenda" (&optional end))
  3815. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3816. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3817. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3818. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3819. (declare-function org-indent-mode "org-indent" (&optional arg))
  3820. (declare-function parse-time-string "parse-time" (string))
  3821. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3822. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3823. (defvar remember-data-file)
  3824. (defvar texmathp-why)
  3825. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3826. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3827. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3828. ;;;###autoload
  3829. (defun turn-on-orgtbl ()
  3830. "Unconditionally turn on `orgtbl-mode'."
  3831. (require 'org-table)
  3832. (orgtbl-mode 1))
  3833. (defun org-at-table-p (&optional table-type)
  3834. "Return t if the cursor is inside an org-type table.
  3835. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3836. (if org-enable-table-editor
  3837. (save-excursion
  3838. (beginning-of-line 1)
  3839. (looking-at (if table-type org-table-any-line-regexp
  3840. org-table-line-regexp)))
  3841. nil))
  3842. (defsubst org-table-p () (org-at-table-p))
  3843. (defun org-at-table.el-p ()
  3844. "Return t if and only if we are at a table.el table."
  3845. (and (org-at-table-p 'any)
  3846. (save-excursion
  3847. (goto-char (org-table-begin 'any))
  3848. (looking-at org-table1-hline-regexp))))
  3849. (defun org-table-recognize-table.el ()
  3850. "If there is a table.el table nearby, recognize it and move into it."
  3851. (if org-table-tab-recognizes-table.el
  3852. (if (org-at-table.el-p)
  3853. (progn
  3854. (beginning-of-line 1)
  3855. (if (looking-at org-table-dataline-regexp)
  3856. nil
  3857. (if (looking-at org-table1-hline-regexp)
  3858. (progn
  3859. (beginning-of-line 2)
  3860. (if (looking-at org-table-any-border-regexp)
  3861. (beginning-of-line -1)))))
  3862. (if (re-search-forward "|" (org-table-end t) t)
  3863. (progn
  3864. (require 'table)
  3865. (if (table--at-cell-p (point))
  3866. t
  3867. (message "recognizing table.el table...")
  3868. (table-recognize-table)
  3869. (message "recognizing table.el table...done")))
  3870. (error "This should not happen"))
  3871. t)
  3872. nil)
  3873. nil))
  3874. (defun org-at-table-hline-p ()
  3875. "Return t if the cursor is inside a hline in a table."
  3876. (if org-enable-table-editor
  3877. (save-excursion
  3878. (beginning-of-line 1)
  3879. (looking-at org-table-hline-regexp))
  3880. nil))
  3881. (defun org-table-map-tables (function &optional quietly)
  3882. "Apply FUNCTION to the start of all tables in the buffer."
  3883. (save-excursion
  3884. (save-restriction
  3885. (widen)
  3886. (goto-char (point-min))
  3887. (while (re-search-forward org-table-any-line-regexp nil t)
  3888. (unless quietly
  3889. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3890. (beginning-of-line 1)
  3891. (when (and (looking-at org-table-line-regexp)
  3892. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3893. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3894. (save-excursion (funcall function))
  3895. (or (looking-at org-table-line-regexp)
  3896. (forward-char 1)))
  3897. (re-search-forward org-table-any-border-regexp nil 1))))
  3898. (unless quietly (message "Mapping tables: done")))
  3899. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3900. (declare-function org-clock-update-mode-line "org-clock" ())
  3901. (declare-function org-resolve-clocks "org-clock"
  3902. (&optional also-non-dangling-p prompt last-valid))
  3903. (defun org-at-TBLFM-p (&optional pos)
  3904. "Return t when point (or POS) is in #+TBLFM line."
  3905. (save-excursion
  3906. (let ((pos pos)))
  3907. (goto-char (or pos (point)))
  3908. (beginning-of-line 1)
  3909. (looking-at org-TBLFM-regexp)))
  3910. (defvar org-clock-start-time)
  3911. (defvar org-clock-marker (make-marker)
  3912. "Marker recording the last clock-in.")
  3913. (defvar org-clock-hd-marker (make-marker)
  3914. "Marker recording the last clock-in, but the headline position.")
  3915. (defvar org-clock-heading ""
  3916. "The heading of the current clock entry.")
  3917. (defun org-clock-is-active ()
  3918. "Return the buffer where the clock is currently running.
  3919. Return nil if no clock is running."
  3920. (marker-buffer org-clock-marker))
  3921. (defun org-check-running-clock ()
  3922. "Check if the current buffer contains the running clock.
  3923. If yes, offer to stop it and to save the buffer with the changes."
  3924. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3925. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3926. (buffer-name))))
  3927. (org-clock-out)
  3928. (when (y-or-n-p "Save changed buffer?")
  3929. (save-buffer))))
  3930. (defun org-clocktable-try-shift (dir n)
  3931. "Check if this line starts a clock table, if yes, shift the time block."
  3932. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3933. (org-clocktable-shift dir n)))
  3934. ;;;###autoload
  3935. (defun org-clock-persistence-insinuate ()
  3936. "Set up hooks for clock persistence."
  3937. (require 'org-clock)
  3938. (add-hook 'org-mode-hook 'org-clock-load)
  3939. (add-hook 'kill-emacs-hook 'org-clock-save))
  3940. (defgroup org-archive nil
  3941. "Options concerning archiving in Org-mode."
  3942. :tag "Org Archive"
  3943. :group 'org-structure)
  3944. (defcustom org-archive-location "%s_archive::"
  3945. "The location where subtrees should be archived.
  3946. The value of this variable is a string, consisting of two parts,
  3947. separated by a double-colon. The first part is a filename and
  3948. the second part is a headline.
  3949. When the filename is omitted, archiving happens in the same file.
  3950. %s in the filename will be replaced by the current file
  3951. name (without the directory part). Archiving to a different file
  3952. is useful to keep archived entries from contributing to the
  3953. Org-mode Agenda.
  3954. The archived entries will be filed as subtrees of the specified
  3955. headline. When the headline is omitted, the subtrees are simply
  3956. filed away at the end of the file, as top-level entries. Also in
  3957. the heading you can use %s to represent the file name, this can be
  3958. useful when using the same archive for a number of different files.
  3959. Here are a few examples:
  3960. \"%s_archive::\"
  3961. If the current file is Projects.org, archive in file
  3962. Projects.org_archive, as top-level trees. This is the default.
  3963. \"::* Archived Tasks\"
  3964. Archive in the current file, under the top-level headline
  3965. \"* Archived Tasks\".
  3966. \"~/org/archive.org::\"
  3967. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3968. \"~/org/archive.org::* From %s\"
  3969. Archive in file ~/org/archive.org (absolute path), under headlines
  3970. \"From FILENAME\" where file name is the current file name.
  3971. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3972. The \"datetree/\" string is special, signifying to archive
  3973. items to the datetree. Items are placed in either the CLOSED
  3974. date of the item, or the current date if there is no CLOSED date.
  3975. The heading will be a subentry to the current date. There doesn't
  3976. need to be a heading, but there always needs to be a slash after
  3977. datetree. For example, to store archived items directly in the
  3978. datetree, use \"~/org/datetree.org::datetree/\".
  3979. \"basement::** Finished Tasks\"
  3980. Archive in file ./basement (relative path), as level 3 trees
  3981. below the level 2 heading \"** Finished Tasks\".
  3982. You may set this option on a per-file basis by adding to the buffer a
  3983. line like
  3984. #+ARCHIVE: basement::** Finished Tasks
  3985. You may also define it locally for a subtree by setting an ARCHIVE property
  3986. in the entry. If such a property is found in an entry, or anywhere up
  3987. the hierarchy, it will be used."
  3988. :group 'org-archive
  3989. :type 'string)
  3990. (defcustom org-agenda-skip-archived-trees t
  3991. "Non-nil means the agenda will skip any items located in archived trees.
  3992. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3993. variable is no longer recommended, you should leave it at the value t.
  3994. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3995. :group 'org-archive
  3996. :group 'org-agenda-skip
  3997. :type 'boolean)
  3998. (defcustom org-columns-skip-archived-trees t
  3999. "Non-nil means ignore archived trees when creating column view."
  4000. :group 'org-archive
  4001. :group 'org-properties
  4002. :type 'boolean)
  4003. (defcustom org-cycle-open-archived-trees nil
  4004. "Non-nil means `org-cycle' will open archived trees.
  4005. An archived tree is a tree marked with the tag ARCHIVE.
  4006. When nil, archived trees will stay folded. You can still open them with
  4007. normal outline commands like `show-all', but not with the cycling commands."
  4008. :group 'org-archive
  4009. :group 'org-cycle
  4010. :type 'boolean)
  4011. (defcustom org-sparse-tree-open-archived-trees nil
  4012. "Non-nil means sparse tree construction shows matches in archived trees.
  4013. When nil, matches in these trees are highlighted, but the trees are kept in
  4014. collapsed state."
  4015. :group 'org-archive
  4016. :group 'org-sparse-trees
  4017. :type 'boolean)
  4018. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  4019. "The default date type when building a sparse tree.
  4020. When this is nil, a date is a scheduled or a deadline timestamp.
  4021. Otherwise, these types are allowed:
  4022. all: all timestamps
  4023. active: only active timestamps (<...>)
  4024. inactive: only inactive timestamps (<...)
  4025. scheduled: only scheduled timestamps
  4026. deadline: only deadline timestamps"
  4027. :type '(choice (const :tag "Scheduled or deadline" scheduled-or-deadline)
  4028. (const :tag "All timestamps" all)
  4029. (const :tag "Only active timestamps" active)
  4030. (const :tag "Only inactive timestamps" inactive)
  4031. (const :tag "Only scheduled timestamps" scheduled)
  4032. (const :tag "Only deadline timestamps" deadline)
  4033. (const :tag "Only closed timestamps" closed))
  4034. :version "24.3"
  4035. :group 'org-sparse-trees)
  4036. (defun org-cycle-hide-archived-subtrees (state)
  4037. "Re-hide all archived subtrees after a visibility state change."
  4038. (when (and (not org-cycle-open-archived-trees)
  4039. (not (memq state '(overview folded))))
  4040. (save-excursion
  4041. (let* ((globalp (memq state '(contents all)))
  4042. (beg (if globalp (point-min) (point)))
  4043. (end (if globalp (point-max) (org-end-of-subtree t))))
  4044. (org-hide-archived-subtrees beg end)
  4045. (goto-char beg)
  4046. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4047. (message "%s" (substitute-command-keys
  4048. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4049. (defun org-force-cycle-archived ()
  4050. "Cycle subtree even if it is archived."
  4051. (interactive)
  4052. (setq this-command 'org-cycle)
  4053. (let ((org-cycle-open-archived-trees t))
  4054. (call-interactively 'org-cycle)))
  4055. (defun org-hide-archived-subtrees (beg end)
  4056. "Re-hide all archived subtrees after a visibility state change."
  4057. (save-excursion
  4058. (let* ((re (concat ":" org-archive-tag ":")))
  4059. (goto-char beg)
  4060. (while (re-search-forward re end t)
  4061. (when (org-at-heading-p)
  4062. (org-flag-subtree t)
  4063. (org-end-of-subtree t))))))
  4064. (declare-function outline-end-of-heading "outline" ())
  4065. (declare-function outline-flag-region "outline" (from to flag))
  4066. (defun org-flag-subtree (flag)
  4067. (save-excursion
  4068. (org-back-to-heading t)
  4069. (outline-end-of-heading)
  4070. (outline-flag-region (point)
  4071. (progn (org-end-of-subtree t) (point))
  4072. flag)))
  4073. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4074. ;; Declare Column View Code
  4075. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4076. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4077. (declare-function org-columns-compute "org-colview" (property))
  4078. ;; Declare ID code
  4079. (declare-function org-id-store-link "org-id")
  4080. (declare-function org-id-locations-load "org-id")
  4081. (declare-function org-id-locations-save "org-id")
  4082. (defvar org-id-track-globally)
  4083. ;;; Variables for pre-computed regular expressions, all buffer local
  4084. (defvar org-todo-regexp nil
  4085. "Matches any of the TODO state keywords.")
  4086. (make-variable-buffer-local 'org-todo-regexp)
  4087. (defvar org-not-done-regexp nil
  4088. "Matches any of the TODO state keywords except the last one.")
  4089. (make-variable-buffer-local 'org-not-done-regexp)
  4090. (defvar org-not-done-heading-regexp nil
  4091. "Matches a TODO headline that is not done.")
  4092. (make-variable-buffer-local 'org-not-done-regexp)
  4093. (defvar org-todo-line-regexp nil
  4094. "Matches a headline and puts TODO state into group 2 if present.")
  4095. (make-variable-buffer-local 'org-todo-line-regexp)
  4096. (defvar org-complex-heading-regexp nil
  4097. "Matches a headline and puts everything into groups:
  4098. group 1: the stars
  4099. group 2: The todo keyword, maybe
  4100. group 3: Priority cookie
  4101. group 4: True headline
  4102. group 5: Tags")
  4103. (make-variable-buffer-local 'org-complex-heading-regexp)
  4104. (defvar org-complex-heading-regexp-format nil
  4105. "Printf format to make regexp to match an exact headline.
  4106. This regexp will match the headline of any node which has the
  4107. exact headline text that is put into the format, but may have any
  4108. TODO state, priority and tags.")
  4109. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4110. (defvar org-todo-line-tags-regexp nil
  4111. "Matches a headline and puts TODO state into group 2 if present.
  4112. Also put tags into group 4 if tags are present.")
  4113. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4114. (defvar org-ds-keyword-length 12
  4115. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4116. (make-variable-buffer-local 'org-ds-keyword-length)
  4117. (defvar org-deadline-regexp nil
  4118. "Matches the DEADLINE keyword.")
  4119. (make-variable-buffer-local 'org-deadline-regexp)
  4120. (defvar org-deadline-time-regexp nil
  4121. "Matches the DEADLINE keyword together with a time stamp.")
  4122. (make-variable-buffer-local 'org-deadline-time-regexp)
  4123. (defvar org-deadline-time-hour-regexp nil
  4124. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4125. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4126. (defvar org-deadline-line-regexp nil
  4127. "Matches the DEADLINE keyword and the rest of the line.")
  4128. (make-variable-buffer-local 'org-deadline-line-regexp)
  4129. (defvar org-scheduled-regexp nil
  4130. "Matches the SCHEDULED keyword.")
  4131. (make-variable-buffer-local 'org-scheduled-regexp)
  4132. (defvar org-scheduled-time-regexp nil
  4133. "Matches the SCHEDULED keyword together with a time stamp.")
  4134. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4135. (defvar org-scheduled-time-hour-regexp nil
  4136. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4137. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4138. (defvar org-closed-time-regexp nil
  4139. "Matches the CLOSED keyword together with a time stamp.")
  4140. (make-variable-buffer-local 'org-closed-time-regexp)
  4141. (defvar org-keyword-time-regexp nil
  4142. "Matches any of the 4 keywords, together with the time stamp.")
  4143. (make-variable-buffer-local 'org-keyword-time-regexp)
  4144. (defvar org-keyword-time-not-clock-regexp nil
  4145. "Matches any of the 3 keywords, together with the time stamp.")
  4146. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4147. (defvar org-maybe-keyword-time-regexp nil
  4148. "Matches a timestamp, possibly preceded by a keyword.")
  4149. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4150. (defvar org-all-time-keywords nil
  4151. "List of time keywords.")
  4152. (make-variable-buffer-local 'org-all-time-keywords)
  4153. (defconst org-plain-time-of-day-regexp
  4154. (concat
  4155. "\\(\\<[012]?[0-9]"
  4156. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4157. "\\(--?"
  4158. "\\(\\<[012]?[0-9]"
  4159. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4160. "\\)?")
  4161. "Regular expression to match a plain time or time range.
  4162. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4163. groups carry important information:
  4164. 0 the full match
  4165. 1 the first time, range or not
  4166. 8 the second time, if it is a range.")
  4167. (defconst org-plain-time-extension-regexp
  4168. (concat
  4169. "\\(\\<[012]?[0-9]"
  4170. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4171. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4172. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4173. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4174. groups carry important information:
  4175. 0 the full match
  4176. 7 hours of duration
  4177. 9 minutes of duration")
  4178. (defconst org-stamp-time-of-day-regexp
  4179. (concat
  4180. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4181. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4182. "\\(--?"
  4183. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4184. "Regular expression to match a timestamp time or time range.
  4185. After a match, the following groups carry important information:
  4186. 0 the full match
  4187. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4188. 2 the first time, range or not
  4189. 4 the second time, if it is a range.")
  4190. (defconst org-startup-options
  4191. '(("fold" org-startup-folded t)
  4192. ("overview" org-startup-folded t)
  4193. ("nofold" org-startup-folded nil)
  4194. ("showall" org-startup-folded nil)
  4195. ("showeverything" org-startup-folded showeverything)
  4196. ("content" org-startup-folded content)
  4197. ("indent" org-startup-indented t)
  4198. ("noindent" org-startup-indented nil)
  4199. ("hidestars" org-hide-leading-stars t)
  4200. ("showstars" org-hide-leading-stars nil)
  4201. ("odd" org-odd-levels-only t)
  4202. ("oddeven" org-odd-levels-only nil)
  4203. ("align" org-startup-align-all-tables t)
  4204. ("noalign" org-startup-align-all-tables nil)
  4205. ("inlineimages" org-startup-with-inline-images t)
  4206. ("noinlineimages" org-startup-with-inline-images nil)
  4207. ("latexpreview" org-startup-with-latex-preview t)
  4208. ("nolatexpreview" org-startup-with-latex-preview nil)
  4209. ("customtime" org-display-custom-times t)
  4210. ("logdone" org-log-done time)
  4211. ("lognotedone" org-log-done note)
  4212. ("nologdone" org-log-done nil)
  4213. ("lognoteclock-out" org-log-note-clock-out t)
  4214. ("nolognoteclock-out" org-log-note-clock-out nil)
  4215. ("logrepeat" org-log-repeat state)
  4216. ("lognoterepeat" org-log-repeat note)
  4217. ("logdrawer" org-log-into-drawer t)
  4218. ("nologdrawer" org-log-into-drawer nil)
  4219. ("logstatesreversed" org-log-states-order-reversed t)
  4220. ("nologstatesreversed" org-log-states-order-reversed nil)
  4221. ("nologrepeat" org-log-repeat nil)
  4222. ("logreschedule" org-log-reschedule time)
  4223. ("lognotereschedule" org-log-reschedule note)
  4224. ("nologreschedule" org-log-reschedule nil)
  4225. ("logredeadline" org-log-redeadline time)
  4226. ("lognoteredeadline" org-log-redeadline note)
  4227. ("nologredeadline" org-log-redeadline nil)
  4228. ("logrefile" org-log-refile time)
  4229. ("lognoterefile" org-log-refile note)
  4230. ("nologrefile" org-log-refile nil)
  4231. ("fninline" org-footnote-define-inline t)
  4232. ("nofninline" org-footnote-define-inline nil)
  4233. ("fnlocal" org-footnote-section nil)
  4234. ("fnauto" org-footnote-auto-label t)
  4235. ("fnprompt" org-footnote-auto-label nil)
  4236. ("fnconfirm" org-footnote-auto-label confirm)
  4237. ("fnplain" org-footnote-auto-label plain)
  4238. ("fnadjust" org-footnote-auto-adjust t)
  4239. ("nofnadjust" org-footnote-auto-adjust nil)
  4240. ("constcgs" constants-unit-system cgs)
  4241. ("constSI" constants-unit-system SI)
  4242. ("noptag" org-tag-persistent-alist nil)
  4243. ("hideblocks" org-hide-block-startup t)
  4244. ("nohideblocks" org-hide-block-startup nil)
  4245. ("beamer" org-startup-with-beamer-mode t)
  4246. ("entitiespretty" org-pretty-entities t)
  4247. ("entitiesplain" org-pretty-entities nil))
  4248. "Variable associated with STARTUP options for org-mode.
  4249. Each element is a list of three items: the startup options (as written
  4250. in the #+STARTUP line), the corresponding variable, and the value to set
  4251. this variable to if the option is found. An optional forth element PUSH
  4252. means to push this value onto the list in the variable.")
  4253. (defcustom org-group-tags t
  4254. "When non-nil (the default), use group tags.
  4255. This can be turned on/off through `org-toggle-tags-groups'."
  4256. :group 'org-tags
  4257. :group 'org-startup
  4258. :type 'boolean)
  4259. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4260. (defun org-toggle-tags-groups ()
  4261. "Toggle support for group tags.
  4262. Support for group tags is controlled by the option
  4263. `org-group-tags', which is non-nil by default."
  4264. (interactive)
  4265. (setq org-group-tags (not org-group-tags))
  4266. (cond ((and (derived-mode-p 'org-agenda-mode)
  4267. org-group-tags)
  4268. (org-agenda-redo))
  4269. ((derived-mode-p 'org-mode)
  4270. (let ((org-inhibit-startup t)) (org-mode))))
  4271. (message "Groups tags support has been turned %s"
  4272. (if org-group-tags "on" "off")))
  4273. (defun org-set-regexps-and-options-for-tags ()
  4274. "Precompute variables used for tags."
  4275. (when (derived-mode-p 'org-mode)
  4276. (org-set-local 'org-file-tags nil)
  4277. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4278. (splitre "[ \t]+")
  4279. (start 0)
  4280. tags ftags key value)
  4281. (save-excursion
  4282. (save-restriction
  4283. (widen)
  4284. (goto-char (point-min))
  4285. (while (re-search-forward re nil t)
  4286. (setq key (upcase (org-match-string-no-properties 1))
  4287. value (org-match-string-no-properties 2))
  4288. (if (stringp value) (setq value (org-trim value)))
  4289. (cond
  4290. ((equal key "TAGS")
  4291. (setq tags (append tags (if tags '("\\n") nil)
  4292. (org-split-string value splitre))))
  4293. ((equal key "FILETAGS")
  4294. (when (string-match "\\S-" value)
  4295. (setq ftags
  4296. (append
  4297. ftags
  4298. (apply 'append
  4299. (mapcar (lambda (x) (org-split-string x ":"))
  4300. (org-split-string value)))))))))))
  4301. ;; Process the file tags.
  4302. (and ftags (org-set-local 'org-file-tags
  4303. (mapcar 'org-add-prop-inherited ftags)))
  4304. (org-set-local 'org-tag-groups-alist nil)
  4305. ;; Process the tags.
  4306. (when (and (not tags) org-tag-alist)
  4307. (setq tags
  4308. (mapcar
  4309. (lambda (tg) (cond ((eq (car tg) :startgroup) "{")
  4310. ((eq (car tg) :endgroup) "}")
  4311. ((eq (car tg) :grouptags) ":")
  4312. ((eq (car tg) :newline) "\n")
  4313. (t (concat (car tg)
  4314. (if (characterp (cdr tg))
  4315. (format "(%s)" (char-to-string (cdr tg))) "")))))
  4316. org-tag-alist)))
  4317. (let (e tgs g)
  4318. (while (setq e (pop tags))
  4319. (cond
  4320. ((equal e "{")
  4321. (progn (push '(:startgroup) tgs)
  4322. (when (equal (nth 1 tags) ":")
  4323. (push (list (replace-regexp-in-string
  4324. "(.+)$" "" (nth 0 tags)))
  4325. org-tag-groups-alist)
  4326. (setq g 0))))
  4327. ((equal e ":") (push '(:grouptags) tgs))
  4328. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4329. ((equal e "\\n") (push '(:newline) tgs))
  4330. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4331. (push (cons (match-string 1 e)
  4332. (string-to-char (match-string 2 e))) tgs)
  4333. (if (and g (> g 0))
  4334. (setcar org-tag-groups-alist
  4335. (append (car org-tag-groups-alist)
  4336. (list (match-string 1 e)))))
  4337. (if g (setq g (1+ g))))
  4338. (t (push (list e) tgs)
  4339. (if (and g (> g 0))
  4340. (setcar org-tag-groups-alist
  4341. (append (car org-tag-groups-alist) (list e))))
  4342. (if g (setq g (1+ g))))))
  4343. (org-set-local 'org-tag-alist nil)
  4344. (while (setq e (pop tgs))
  4345. (or (and (stringp (car e))
  4346. (assoc (car e) org-tag-alist))
  4347. (push e org-tag-alist)))
  4348. ;; Return a list with tag variables
  4349. (list org-file-tags org-tag-alist org-tag-groups-alist)))))
  4350. (defvar org-ota nil)
  4351. (defun org-set-regexps-and-options ()
  4352. "Precompute regular expressions used in the current buffer."
  4353. (when (derived-mode-p 'org-mode)
  4354. (org-set-local 'org-todo-kwd-alist nil)
  4355. (org-set-local 'org-todo-key-alist nil)
  4356. (org-set-local 'org-todo-key-trigger nil)
  4357. (org-set-local 'org-todo-keywords-1 nil)
  4358. (org-set-local 'org-done-keywords nil)
  4359. (org-set-local 'org-todo-heads nil)
  4360. (org-set-local 'org-todo-sets nil)
  4361. (org-set-local 'org-todo-log-states nil)
  4362. (org-set-local 'org-file-properties nil)
  4363. (let ((re (org-make-options-regexp
  4364. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4365. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4366. "SETUPFILE" "OPTIONS")
  4367. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4368. (splitre "[ \t]+")
  4369. (scripts org-use-sub-superscripts)
  4370. kwds kws0 kwsa key log value cat arch const links hw dws
  4371. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4372. (start 0))
  4373. (save-excursion
  4374. (save-restriction
  4375. (widen)
  4376. (goto-char (point-min))
  4377. (while
  4378. (or (and
  4379. ext-setup-or-nil
  4380. (not org-ota)
  4381. (let (ret)
  4382. (with-temp-buffer
  4383. (insert ext-setup-or-nil)
  4384. (let ((major-mode 'org-mode) org-ota)
  4385. (setq ret (save-match-data
  4386. (org-set-regexps-and-options-for-tags)))))
  4387. ;; Append setupfile tags to existing tags
  4388. (setq org-ota t)
  4389. (setq org-file-tags
  4390. (delq nil (append org-file-tags (nth 0 ret)))
  4391. org-tag-alist
  4392. (delq nil (append org-tag-alist (nth 1 ret)))
  4393. org-tag-groups-alist
  4394. (delq nil (append org-tag-groups-alist (nth 2 ret))))))
  4395. (and ext-setup-or-nil
  4396. (string-match re ext-setup-or-nil start)
  4397. (setq start (match-end 0)))
  4398. (and (setq ext-setup-or-nil nil start 0)
  4399. (re-search-forward re nil t)))
  4400. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4401. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4402. (if (stringp value) (setq value (org-trim value)))
  4403. (cond
  4404. ((equal key "CATEGORY")
  4405. (setq cat value))
  4406. ((member key '("SEQ_TODO" "TODO"))
  4407. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4408. ((equal key "TYP_TODO")
  4409. (push (cons 'type (org-split-string value splitre)) kwds))
  4410. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4411. ;; general TODO-like setup
  4412. (push (cons (intern (downcase (match-string 1 key)))
  4413. (org-split-string value splitre)) kwds))
  4414. ((equal key "COLUMNS")
  4415. (org-set-local 'org-columns-default-format value))
  4416. ((equal key "LINK")
  4417. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4418. (push (cons (match-string 1 value)
  4419. (org-trim (match-string 2 value)))
  4420. links)))
  4421. ((equal key "PRIORITIES")
  4422. (setq prio (org-split-string value " +")))
  4423. ((equal key "PROPERTY")
  4424. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4425. (setq props (org--update-property-plist (match-string 1 value)
  4426. (match-string 2 value)
  4427. props))))
  4428. ((equal key "CONSTANTS")
  4429. (org-table-set-constants))
  4430. ((equal key "STARTUP")
  4431. (let ((opts (org-split-string value splitre))
  4432. l var val)
  4433. (while (setq l (pop opts))
  4434. (when (setq l (assoc l org-startup-options))
  4435. (setq var (nth 1 l) val (nth 2 l))
  4436. (if (not (nth 3 l))
  4437. (set (make-local-variable var) val)
  4438. (if (not (listp (symbol-value var)))
  4439. (set (make-local-variable var) nil))
  4440. (set (make-local-variable var) (symbol-value var))
  4441. (add-to-list var val))))))
  4442. ((equal key "ARCHIVE")
  4443. (setq arch value)
  4444. (remove-text-properties 0 (length arch)
  4445. '(face t fontified t) arch))
  4446. ((equal key "OPTIONS")
  4447. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4448. (setq scripts (read (match-string 2 value)))))
  4449. ((and (equal key "SETUPFILE")
  4450. ;; Prevent checking in Gnus messages
  4451. (not buffer-read-only))
  4452. (setq setup-contents (org-file-contents
  4453. (expand-file-name
  4454. (org-remove-double-quotes value))
  4455. 'noerror))
  4456. (if (not ext-setup-or-nil)
  4457. (setq ext-setup-or-nil setup-contents start 0)
  4458. (setq ext-setup-or-nil
  4459. (concat (substring ext-setup-or-nil 0 start)
  4460. "\n" setup-contents "\n"
  4461. (substring ext-setup-or-nil start)))))))))
  4462. (org-set-local 'org-use-sub-superscripts scripts)
  4463. (when cat
  4464. (org-set-local 'org-category (intern cat))
  4465. (push (cons "CATEGORY" cat) props))
  4466. (when prio
  4467. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4468. (setq prio (mapcar 'string-to-char prio))
  4469. (org-set-local 'org-highest-priority (nth 0 prio))
  4470. (org-set-local 'org-lowest-priority (nth 1 prio))
  4471. (org-set-local 'org-default-priority (nth 2 prio)))
  4472. (and props (org-set-local 'org-file-properties props))
  4473. (and arch (org-set-local 'org-archive-location arch))
  4474. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4475. ;; Process the TODO keywords
  4476. (unless kwds
  4477. ;; Use the global values as if they had been given locally.
  4478. (setq kwds (default-value 'org-todo-keywords))
  4479. (if (stringp (car kwds))
  4480. (setq kwds (list (cons org-todo-interpretation
  4481. (default-value 'org-todo-keywords)))))
  4482. (setq kwds (reverse kwds)))
  4483. (setq kwds (nreverse kwds))
  4484. (let (inter kws kw)
  4485. (while (setq kws (pop kwds))
  4486. (let ((kws (or
  4487. (run-hook-with-args-until-success
  4488. 'org-todo-setup-filter-hook kws)
  4489. kws)))
  4490. (setq inter (pop kws) sep (member "|" kws)
  4491. kws0 (delete "|" (copy-sequence kws))
  4492. kwsa nil
  4493. kws1 (mapcar
  4494. (lambda (x)
  4495. ;; 1 2
  4496. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4497. (progn
  4498. (setq kw (match-string 1 x)
  4499. key (and (match-end 2) (match-string 2 x))
  4500. log (org-extract-log-state-settings x))
  4501. (push (cons kw (and key (string-to-char key))) kwsa)
  4502. (and log (push log org-todo-log-states))
  4503. kw)
  4504. (error "Invalid TODO keyword %s" x)))
  4505. kws0)
  4506. kwsa (if kwsa (append '((:startgroup))
  4507. (nreverse kwsa)
  4508. '((:endgroup))))
  4509. hw (car kws1)
  4510. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4511. tail (list inter hw (car dws) (org-last dws))))
  4512. (add-to-list 'org-todo-heads hw 'append)
  4513. (push kws1 org-todo-sets)
  4514. (setq org-done-keywords (append org-done-keywords dws nil))
  4515. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4516. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4517. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4518. (setq org-todo-sets (nreverse org-todo-sets)
  4519. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4520. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4521. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4522. ;; Compute the regular expressions and other local variables.
  4523. ;; Using `org-outline-regexp-bol' would complicate them much,
  4524. ;; because of the fixed white space at the end of that string.
  4525. (if (not org-done-keywords)
  4526. (setq org-done-keywords (and org-todo-keywords-1
  4527. (list (org-last org-todo-keywords-1)))))
  4528. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4529. (length org-scheduled-string)
  4530. (length org-clock-string)
  4531. (length org-closed-string)))
  4532. org-not-done-keywords
  4533. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4534. org-todo-regexp
  4535. (concat "\\("
  4536. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4537. "\\)")
  4538. org-not-done-regexp
  4539. (concat "\\("
  4540. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4541. "\\)")
  4542. org-not-done-heading-regexp
  4543. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4544. org-todo-line-regexp
  4545. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4546. org-complex-heading-regexp
  4547. (concat "^\\(\\*+\\)"
  4548. "\\(?: +" org-todo-regexp "\\)?"
  4549. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4550. "\\(?: +\\(.*?\\)\\)??"
  4551. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4552. "[ \t]*$")
  4553. org-complex-heading-regexp-format
  4554. (concat "^\\(\\*+\\)"
  4555. "\\(?: +" org-todo-regexp "\\)?"
  4556. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4557. "\\(?: +"
  4558. ;; Stats cookies can be stuck to body.
  4559. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4560. "\\(%s\\)"
  4561. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4562. "\\)"
  4563. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4564. "[ \t]*$")
  4565. org-todo-line-tags-regexp
  4566. (concat "^\\(\\*+\\)"
  4567. "\\(?: +" org-todo-regexp "\\)?"
  4568. "\\(?: +\\(.*?\\)\\)??"
  4569. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4570. "[ \t]*$")
  4571. org-deadline-regexp (concat "\\<" org-deadline-string)
  4572. org-deadline-time-regexp
  4573. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4574. org-deadline-time-hour-regexp
  4575. (concat "\\<" org-deadline-string
  4576. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4577. org-deadline-line-regexp
  4578. (concat "\\<\\(" org-deadline-string "\\).*")
  4579. org-scheduled-regexp
  4580. (concat "\\<" org-scheduled-string)
  4581. org-scheduled-time-regexp
  4582. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4583. org-scheduled-time-hour-regexp
  4584. (concat "\\<" org-scheduled-string
  4585. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4586. org-closed-time-regexp
  4587. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4588. org-keyword-time-regexp
  4589. (concat "\\<\\(" org-scheduled-string
  4590. "\\|" org-deadline-string
  4591. "\\|" org-closed-string
  4592. "\\|" org-clock-string "\\)"
  4593. " *[[<]\\([^]>]+\\)[]>]")
  4594. org-keyword-time-not-clock-regexp
  4595. (concat "\\<\\(" org-scheduled-string
  4596. "\\|" org-deadline-string
  4597. "\\|" org-closed-string
  4598. "\\)"
  4599. " *[[<]\\([^]>]+\\)[]>]")
  4600. org-maybe-keyword-time-regexp
  4601. (concat "\\(\\<\\(" org-scheduled-string
  4602. "\\|" org-deadline-string
  4603. "\\|" org-closed-string
  4604. "\\|" org-clock-string "\\)\\)?"
  4605. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4606. org-all-time-keywords
  4607. (mapcar (lambda (w) (substring w 0 -1))
  4608. (list org-scheduled-string org-deadline-string
  4609. org-clock-string org-closed-string)))
  4610. (setq org-ota nil)
  4611. (org-compute-latex-and-related-regexp))))
  4612. (defun org-file-contents (file &optional noerror)
  4613. "Return the contents of FILE, as a string."
  4614. (if (and file (file-readable-p file))
  4615. (with-temp-buffer
  4616. (insert-file-contents file)
  4617. (buffer-string))
  4618. (funcall (if noerror 'message 'error)
  4619. "Cannot read file \"%s\"%s"
  4620. file
  4621. (let ((from (buffer-file-name (buffer-base-buffer))))
  4622. (if from (concat " (referenced in file \"" from "\")") "")))))
  4623. (defun org-extract-log-state-settings (x)
  4624. "Extract the log state setting from a TODO keyword string.
  4625. This will extract info from a string like \"WAIT(w@/!)\"."
  4626. (let (kw key log1 log2)
  4627. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4628. (setq kw (match-string 1 x)
  4629. key (and (match-end 2) (match-string 2 x))
  4630. log1 (and (match-end 3) (match-string 3 x))
  4631. log2 (and (match-end 4) (match-string 4 x)))
  4632. (and (or log1 log2)
  4633. (list kw
  4634. (and log1 (if (equal log1 "!") 'time 'note))
  4635. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4636. (defun org-remove-keyword-keys (list)
  4637. "Remove a pair of parenthesis at the end of each string in LIST."
  4638. (mapcar (lambda (x)
  4639. (if (string-match "(.*)$" x)
  4640. (substring x 0 (match-beginning 0))
  4641. x))
  4642. list))
  4643. (defun org-assign-fast-keys (alist)
  4644. "Assign fast keys to a keyword-key alist.
  4645. Respect keys that are already there."
  4646. (let (new e (alt ?0))
  4647. (while (setq e (pop alist))
  4648. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4649. (cdr e)) ;; Key already assigned.
  4650. (push e new)
  4651. (let ((clist (string-to-list (downcase (car e))))
  4652. (used (append new alist)))
  4653. (when (= (car clist) ?@)
  4654. (pop clist))
  4655. (while (and clist (rassoc (car clist) used))
  4656. (pop clist))
  4657. (unless clist
  4658. (while (rassoc alt used)
  4659. (incf alt)))
  4660. (push (cons (car e) (or (car clist) alt)) new))))
  4661. (nreverse new)))
  4662. ;;; Some variables used in various places
  4663. (defvar org-window-configuration nil
  4664. "Used in various places to store a window configuration.")
  4665. (defvar org-selected-window nil
  4666. "Used in various places to store a window configuration.")
  4667. (defvar org-finish-function nil
  4668. "Function to be called when `C-c C-c' is used.
  4669. This is for getting out of special buffers like capture.")
  4670. ;; FIXME: Occasionally check by commenting these, to make sure
  4671. ;; no other functions uses these, forgetting to let-bind them.
  4672. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4673. (defvar org-last-state)
  4674. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4675. ;; Defined somewhere in this file, but used before definition.
  4676. (defvar org-entities) ;; defined in org-entities.el
  4677. (defvar org-struct-menu)
  4678. (defvar org-org-menu)
  4679. (defvar org-tbl-menu)
  4680. ;;;; Define the Org-mode
  4681. ;; We use a before-change function to check if a table might need
  4682. ;; an update.
  4683. (defvar org-table-may-need-update t
  4684. "Indicates that a table might need an update.
  4685. This variable is set by `org-before-change-function'.
  4686. `org-table-align' sets it back to nil.")
  4687. (defun org-before-change-function (beg end)
  4688. "Every change indicates that a table might need an update."
  4689. (setq org-table-may-need-update t))
  4690. (defvar org-mode-map)
  4691. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4692. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4693. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4694. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4695. (defvar org-table-buffer-is-an nil)
  4696. (defvar bidi-paragraph-direction)
  4697. (defvar buffer-face-mode-face)
  4698. (require 'outline)
  4699. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4700. (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"))
  4701. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4702. ;; Other stuff we need.
  4703. (require 'time-date)
  4704. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4705. (require 'easymenu)
  4706. (require 'overlay)
  4707. ;; (require 'org-macs) moved higher up in the file before it is first used
  4708. (require 'org-entities)
  4709. ;; (require 'org-compat) moved higher up in the file before it is first used
  4710. (require 'org-faces)
  4711. (require 'org-list)
  4712. (require 'org-pcomplete)
  4713. (require 'org-src)
  4714. (require 'org-footnote)
  4715. (require 'org-macro)
  4716. ;; babel
  4717. (require 'ob)
  4718. ;;;###autoload
  4719. (define-derived-mode org-mode outline-mode "Org"
  4720. "Outline-based notes management and organizer, alias
  4721. \"Carsten's outline-mode for keeping track of everything.\"
  4722. Org-mode develops organizational tasks around a NOTES file which
  4723. contains information about projects as plain text. Org-mode is
  4724. implemented on top of outline-mode, which is ideal to keep the content
  4725. of large files well structured. It supports ToDo items, deadlines and
  4726. time stamps, which magically appear in the diary listing of the Emacs
  4727. calendar. Tables are easily created with a built-in table editor.
  4728. Plain text URL-like links connect to websites, emails (VM), Usenet
  4729. messages (Gnus), BBDB entries, and any files related to the project.
  4730. For printing and sharing of notes, an Org-mode file (or a part of it)
  4731. can be exported as a structured ASCII or HTML file.
  4732. The following commands are available:
  4733. \\{org-mode-map}"
  4734. ;; Get rid of Outline menus, they are not needed
  4735. ;; Need to do this here because define-derived-mode sets up
  4736. ;; the keymap so late. Still, it is a waste to call this each time
  4737. ;; we switch another buffer into org-mode.
  4738. (if (featurep 'xemacs)
  4739. (when (boundp 'outline-mode-menu-heading)
  4740. ;; Assume this is Greg's port, it uses easymenu
  4741. (easy-menu-remove outline-mode-menu-heading)
  4742. (easy-menu-remove outline-mode-menu-show)
  4743. (easy-menu-remove outline-mode-menu-hide))
  4744. (define-key org-mode-map [menu-bar headings] 'undefined)
  4745. (define-key org-mode-map [menu-bar hide] 'undefined)
  4746. (define-key org-mode-map [menu-bar show] 'undefined))
  4747. (org-load-modules-maybe)
  4748. (easy-menu-add org-org-menu)
  4749. (easy-menu-add org-tbl-menu)
  4750. (org-install-agenda-files-menu)
  4751. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4752. (add-to-invisibility-spec '(org-cwidth))
  4753. (add-to-invisibility-spec '(org-hide-block . t))
  4754. (when (featurep 'xemacs)
  4755. (org-set-local 'line-move-ignore-invisible t))
  4756. (org-set-local 'outline-regexp org-outline-regexp)
  4757. (org-set-local 'outline-level 'org-outline-level)
  4758. (setq bidi-paragraph-direction 'left-to-right)
  4759. (when (and org-ellipsis
  4760. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4761. (fboundp 'make-glyph-code))
  4762. (unless org-display-table
  4763. (setq org-display-table (make-display-table)))
  4764. (set-display-table-slot
  4765. org-display-table 4
  4766. (vconcat (mapcar
  4767. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4768. org-ellipsis)))
  4769. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4770. (setq buffer-display-table org-display-table))
  4771. (org-set-regexps-and-options-for-tags)
  4772. (org-set-regexps-and-options)
  4773. (org-set-font-lock-defaults)
  4774. (when (and org-tag-faces (not org-tags-special-faces-re))
  4775. ;; tag faces set outside customize.... force initialization.
  4776. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4777. ;; Calc embedded
  4778. (org-set-local 'calc-embedded-open-mode "# ")
  4779. ;; Modify a few syntax entries
  4780. (modify-syntax-entry ?@ "w")
  4781. (modify-syntax-entry ?\" "\"")
  4782. (modify-syntax-entry ?\\ "_")
  4783. (modify-syntax-entry ?~ "_")
  4784. (if org-startup-truncated (setq truncate-lines t))
  4785. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4786. (org-set-local 'font-lock-unfontify-region-function
  4787. 'org-unfontify-region)
  4788. ;; Activate before-change-function
  4789. (org-set-local 'org-table-may-need-update t)
  4790. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4791. 'local)
  4792. ;; Check for running clock before killing a buffer
  4793. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4794. ;; Initialize macros templates.
  4795. (org-macro-initialize-templates)
  4796. ;; Initialize radio targets.
  4797. (org-update-radio-target-regexp)
  4798. ;; Indentation.
  4799. (org-set-local 'indent-line-function 'org-indent-line)
  4800. (org-set-local 'indent-region-function 'org-indent-region)
  4801. ;; Filling and auto-filling.
  4802. (org-setup-filling)
  4803. ;; Comments.
  4804. (org-setup-comments-handling)
  4805. ;; Initialize cache.
  4806. (org-element-cache-reset)
  4807. ;; Beginning/end of defun
  4808. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4809. (org-set-local 'end-of-defun-function
  4810. (lambda ()
  4811. (if (not (org-at-heading-p))
  4812. (org-forward-element)
  4813. (org-forward-element)
  4814. (forward-char -1))))
  4815. ;; Next error for sparse trees
  4816. (org-set-local 'next-error-function 'org-occur-next-match)
  4817. ;; Make sure dependence stuff works reliably, even for users who set it
  4818. ;; too late :-(
  4819. (if org-enforce-todo-dependencies
  4820. (add-hook 'org-blocker-hook
  4821. 'org-block-todo-from-children-or-siblings-or-parent)
  4822. (remove-hook 'org-blocker-hook
  4823. 'org-block-todo-from-children-or-siblings-or-parent))
  4824. (if org-enforce-todo-checkbox-dependencies
  4825. (add-hook 'org-blocker-hook
  4826. 'org-block-todo-from-checkboxes)
  4827. (remove-hook 'org-blocker-hook
  4828. 'org-block-todo-from-checkboxes))
  4829. ;; Align options lines
  4830. (org-set-local
  4831. 'align-mode-rules-list
  4832. '((org-in-buffer-settings
  4833. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4834. (modes . '(org-mode)))))
  4835. ;; Imenu
  4836. (org-set-local 'imenu-create-index-function
  4837. 'org-imenu-get-tree)
  4838. ;; Make isearch reveal context
  4839. (if (or (featurep 'xemacs)
  4840. (not (boundp 'outline-isearch-open-invisible-function)))
  4841. ;; Emacs 21 and XEmacs make use of the hook
  4842. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4843. ;; Emacs 22 deals with this through a special variable
  4844. (org-set-local 'outline-isearch-open-invisible-function
  4845. (lambda (&rest ignore) (org-show-context 'isearch))))
  4846. ;; Setup the pcomplete hooks
  4847. (set (make-local-variable 'pcomplete-command-completion-function)
  4848. 'org-pcomplete-initial)
  4849. (set (make-local-variable 'pcomplete-command-name-function)
  4850. 'org-command-at-point)
  4851. (set (make-local-variable 'pcomplete-default-completion-function)
  4852. 'ignore)
  4853. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4854. 'org-parse-arguments)
  4855. (set (make-local-variable 'pcomplete-termination-string) "")
  4856. (when (>= emacs-major-version 23)
  4857. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4858. ;; If empty file that did not turn on org-mode automatically, make it to.
  4859. (if (and org-insert-mode-line-in-empty-file
  4860. (org-called-interactively-p 'any)
  4861. (= (point-min) (point-max)))
  4862. (insert "# -*- mode: org -*-\n\n"))
  4863. (unless org-inhibit-startup
  4864. (org-unmodified
  4865. (and org-startup-with-beamer-mode (org-beamer-mode))
  4866. (when org-startup-align-all-tables
  4867. (org-table-map-tables 'org-table-align 'quietly))
  4868. (when org-startup-with-inline-images
  4869. (org-display-inline-images))
  4870. (when org-startup-with-latex-preview
  4871. (org-toggle-latex-fragment))
  4872. (unless org-inhibit-startup-visibility-stuff
  4873. (org-set-startup-visibility))
  4874. (org-refresh-effort-properties)))
  4875. ;; Try to set org-hide correctly
  4876. (let ((foreground (org-find-invisible-foreground)))
  4877. (if foreground
  4878. (set-face-foreground 'org-hide foreground))))
  4879. ;; Update `customize-package-emacs-version-alist'
  4880. (add-to-list 'customize-package-emacs-version-alist
  4881. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4882. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4883. ("8.2.6" . "24.4")))
  4884. (defvar org-mode-transpose-word-syntax-table
  4885. (let ((st (make-syntax-table)))
  4886. (mapc (lambda(c) (modify-syntax-entry
  4887. (string-to-char (car c)) "w p" st))
  4888. org-emphasis-alist)
  4889. st))
  4890. (when (fboundp 'abbrev-table-put)
  4891. (abbrev-table-put org-mode-abbrev-table
  4892. :parents (list text-mode-abbrev-table)))
  4893. (defun org-find-invisible-foreground ()
  4894. (let ((candidates (remove
  4895. "unspecified-bg"
  4896. (nconc
  4897. (list (face-background 'default)
  4898. (face-background 'org-default))
  4899. (mapcar
  4900. (lambda (alist)
  4901. (when (boundp alist)
  4902. (cdr (assoc 'background-color (symbol-value alist)))))
  4903. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4904. (list (face-foreground 'org-hide))))))
  4905. (car (remove nil candidates))))
  4906. (defun org-current-time (&optional rounding-minutes past)
  4907. "Current time, possibly rounded to ROUNDING-MINUTES.
  4908. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4909. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4910. the rounding returns a past time."
  4911. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4912. (car org-time-stamp-rounding-minutes)))
  4913. (time (decode-time)) res)
  4914. (if (< r 1)
  4915. (current-time)
  4916. (setq res
  4917. (apply 'encode-time
  4918. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4919. (nthcdr 2 time))))
  4920. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4921. (seconds-to-time (- (org-float-time res) (* r 60)))
  4922. res))))
  4923. (defun org-today ()
  4924. "Return today date, considering `org-extend-today-until'."
  4925. (time-to-days
  4926. (time-subtract (current-time)
  4927. (list 0 (* 3600 org-extend-today-until) 0))))
  4928. ;;;; Font-Lock stuff, including the activators
  4929. (defvar org-mouse-map (make-sparse-keymap))
  4930. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4931. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4932. (when org-mouse-1-follows-link
  4933. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4934. (when org-tab-follows-link
  4935. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4936. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4937. (require 'font-lock)
  4938. (defconst org-non-link-chars "]\t\n\r<>")
  4939. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4940. "file+sys" "news" "shell" "elisp" "doi" "message"
  4941. "help"))
  4942. (defvar org-link-types-re nil
  4943. "Matches a link that has a url-like prefix like \"http:\"")
  4944. (defvar org-link-re-with-space nil
  4945. "Matches a link with spaces, optional angular brackets around it.")
  4946. (defvar org-link-re-with-space2 nil
  4947. "Matches a link with spaces, optional angular brackets around it.")
  4948. (defvar org-link-re-with-space3 nil
  4949. "Matches a link with spaces, only for internal part in bracket links.")
  4950. (defvar org-angle-link-re nil
  4951. "Matches link with angular brackets, spaces are allowed.")
  4952. (defvar org-plain-link-re nil
  4953. "Matches plain link, without spaces.")
  4954. (defvar org-bracket-link-regexp nil
  4955. "Matches a link in double brackets.")
  4956. (defvar org-bracket-link-analytic-regexp nil
  4957. "Regular expression used to analyze links.
  4958. Here is what the match groups contain after a match:
  4959. 1: http:
  4960. 2: http
  4961. 3: path
  4962. 4: [desc]
  4963. 5: desc")
  4964. (defvar org-bracket-link-analytic-regexp++ nil
  4965. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4966. (defvar org-any-link-re nil
  4967. "Regular expression matching any link.")
  4968. (defconst org-match-sexp-depth 3
  4969. "Number of stacked braces for sub/superscript matching.")
  4970. (defun org-create-multibrace-regexp (left right n)
  4971. "Create a regular expression which will match a balanced sexp.
  4972. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4973. as single character strings.
  4974. The regexp returned will match the entire expression including the
  4975. delimiters. It will also define a single group which contains the
  4976. match except for the outermost delimiters. The maximum depth of
  4977. stacked delimiters is N. Escaping delimiters is not possible."
  4978. (let* ((nothing (concat "[^" left right "]*?"))
  4979. (or "\\|")
  4980. (re nothing)
  4981. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4982. (while (> n 1)
  4983. (setq n (1- n)
  4984. re (concat re or next)
  4985. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4986. (concat left "\\(" re "\\)" right)))
  4987. (defconst org-match-substring-regexp
  4988. (concat
  4989. "\\(\\S-\\)\\([_^]\\)\\("
  4990. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4991. "\\|"
  4992. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4993. "\\|"
  4994. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4995. "The regular expression matching a sub- or superscript.")
  4996. (defconst org-match-substring-with-braces-regexp
  4997. (concat
  4998. "\\(\\S-\\)\\([_^]\\)"
  4999. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  5000. "The regular expression matching a sub- or superscript, forcing braces.")
  5001. (defun org-make-link-regexps ()
  5002. "Update the link regular expressions.
  5003. This should be called after the variable `org-link-types' has changed."
  5004. (let ((types-re (regexp-opt org-link-types t)))
  5005. (setq org-link-types-re
  5006. (concat "\\`" types-re ":")
  5007. org-link-re-with-space
  5008. (concat "<?" types-re ":"
  5009. "\\([^" org-non-link-chars " ]"
  5010. "[^" org-non-link-chars "]*"
  5011. "[^" org-non-link-chars " ]\\)>?")
  5012. org-link-re-with-space2
  5013. (concat "<?" types-re ":"
  5014. "\\([^" org-non-link-chars " ]"
  5015. "[^\t\n\r]*"
  5016. "[^" org-non-link-chars " ]\\)>?")
  5017. org-link-re-with-space3
  5018. (concat "<?" types-re ":"
  5019. "\\([^" org-non-link-chars " ]"
  5020. "[^\t\n\r]*\\)")
  5021. org-angle-link-re
  5022. (concat "<" types-re ":"
  5023. "\\([^" org-non-link-chars " ]"
  5024. "[^" org-non-link-chars "]*"
  5025. "\\)>")
  5026. org-plain-link-re
  5027. (concat
  5028. "\\<" types-re ":"
  5029. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5030. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5031. org-bracket-link-regexp
  5032. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5033. org-bracket-link-analytic-regexp
  5034. (concat
  5035. "\\[\\["
  5036. "\\(" types-re ":\\)?"
  5037. "\\([^]]+\\)"
  5038. "\\]"
  5039. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5040. "\\]")
  5041. org-bracket-link-analytic-regexp++
  5042. (concat
  5043. "\\[\\["
  5044. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5045. "\\([^]]+\\)"
  5046. "\\]"
  5047. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5048. "\\]")
  5049. org-any-link-re
  5050. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5051. org-angle-link-re "\\)\\|\\("
  5052. org-plain-link-re "\\)"))))
  5053. (org-make-link-regexps)
  5054. (defvar org-emph-face nil)
  5055. (defun org-do-emphasis-faces (limit)
  5056. "Run through the buffer and emphasize strings."
  5057. (let (rtn a)
  5058. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5059. (let* ((border (char-after (match-beginning 3)))
  5060. (bre (regexp-quote (char-to-string border))))
  5061. (if (and (not (= border (char-after (match-beginning 4))))
  5062. (not (save-match-data
  5063. (string-match (concat bre ".*" bre)
  5064. (replace-regexp-in-string
  5065. "\n" " "
  5066. (substring (match-string 2) 1 -1))))))
  5067. (progn
  5068. (setq rtn t)
  5069. (setq a (assoc (match-string 3) org-emphasis-alist))
  5070. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5071. 'face
  5072. (nth 1 a))
  5073. (and (nth 2 a)
  5074. (org-remove-flyspell-overlays-in
  5075. (match-beginning 0) (match-end 0)))
  5076. (add-text-properties (match-beginning 2) (match-end 2)
  5077. '(font-lock-multiline t org-emphasis t))
  5078. (when org-hide-emphasis-markers
  5079. (add-text-properties (match-end 4) (match-beginning 5)
  5080. '(invisible org-link))
  5081. (add-text-properties (match-beginning 3) (match-end 3)
  5082. '(invisible org-link))))))
  5083. (goto-char (1+ (match-beginning 0))))
  5084. rtn))
  5085. (defun org-emphasize (&optional char)
  5086. "Insert or change an emphasis, i.e. a font like bold or italic.
  5087. If there is an active region, change that region to a new emphasis.
  5088. If there is no region, just insert the marker characters and position
  5089. the cursor between them.
  5090. CHAR should be the marker character. If it is a space, it means to
  5091. remove the emphasis of the selected region.
  5092. If CHAR is not given (for example in an interactive call) it will be
  5093. prompted for."
  5094. (interactive)
  5095. (let ((erc org-emphasis-regexp-components)
  5096. (prompt "")
  5097. (string "") beg end move c s)
  5098. (if (org-region-active-p)
  5099. (setq beg (region-beginning) end (region-end)
  5100. string (buffer-substring beg end))
  5101. (setq move t))
  5102. (unless char
  5103. (message "Emphasis marker or tag: [%s]"
  5104. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5105. (setq char (read-char-exclusive)))
  5106. (if (equal char ?\ )
  5107. (setq s "" move nil)
  5108. (unless (assoc (char-to-string char) org-emphasis-alist)
  5109. (user-error "No such emphasis marker: \"%c\"" char))
  5110. (setq s (char-to-string char)))
  5111. (while (and (> (length string) 1)
  5112. (equal (substring string 0 1) (substring string -1))
  5113. (assoc (substring string 0 1) org-emphasis-alist))
  5114. (setq string (substring string 1 -1)))
  5115. (setq string (concat s string s))
  5116. (if beg (delete-region beg end))
  5117. (unless (or (bolp)
  5118. (string-match (concat "[" (nth 0 erc) "\n]")
  5119. (char-to-string (char-before (point)))))
  5120. (insert " "))
  5121. (unless (or (eobp)
  5122. (string-match (concat "[" (nth 1 erc) "\n]")
  5123. (char-to-string (char-after (point)))))
  5124. (insert " ") (backward-char 1))
  5125. (insert string)
  5126. (and move (backward-char 1))))
  5127. (defconst org-nonsticky-props
  5128. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5129. (defsubst org-rear-nonsticky-at (pos)
  5130. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5131. (defun org-activate-plain-links (limit)
  5132. "Add link properties for plain links."
  5133. (let (f hl)
  5134. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5135. (not (member 'org-tag
  5136. (get-text-property (1- (match-beginning 0)) 'face)))
  5137. (not (org-in-src-block-p)))
  5138. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5139. (setq f (get-text-property (match-beginning 0) 'face))
  5140. (setq hl (org-match-string-no-properties 0))
  5141. (if (or (eq f 'org-tag)
  5142. (and (listp f) (memq 'org-tag f)))
  5143. nil
  5144. (add-text-properties (match-beginning 0) (match-end 0)
  5145. (list 'mouse-face 'highlight
  5146. 'face 'org-link
  5147. 'htmlize-link `(:uri ,hl)
  5148. 'keymap org-mouse-map))
  5149. (org-rear-nonsticky-at (match-end 0)))
  5150. t)))
  5151. (defun org-activate-code (limit)
  5152. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5153. (progn
  5154. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5155. (remove-text-properties (match-beginning 0) (match-end 0)
  5156. '(display t invisible t intangible t))
  5157. t)))
  5158. (defcustom org-src-fontify-natively t
  5159. "When non-nil, fontify code in code blocks."
  5160. :type 'boolean
  5161. :version "24.4"
  5162. :package-version '(Org . "8.3")
  5163. :group 'org-appearance
  5164. :group 'org-babel)
  5165. (defcustom org-allow-promoting-top-level-subtree nil
  5166. "When non-nil, allow promoting a top level subtree.
  5167. The leading star of the top level headline will be replaced
  5168. by a #."
  5169. :type 'boolean
  5170. :version "24.1"
  5171. :group 'org-appearance)
  5172. (defun org-fontify-meta-lines-and-blocks (limit)
  5173. (condition-case nil
  5174. (org-fontify-meta-lines-and-blocks-1 limit)
  5175. (error (message "org-mode fontification error"))))
  5176. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5177. "Fontify #+ lines and blocks."
  5178. (let ((case-fold-search t))
  5179. (if (re-search-forward
  5180. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5181. limit t)
  5182. (let ((beg (match-beginning 0))
  5183. (block-start (match-end 0))
  5184. (block-end nil)
  5185. (lang (match-string 7))
  5186. (beg1 (line-beginning-position 2))
  5187. (dc1 (downcase (match-string 2)))
  5188. (dc3 (downcase (match-string 3)))
  5189. end end1 quoting block-type ovl)
  5190. (cond
  5191. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5192. ;; a single line of backend-specific content
  5193. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5194. (remove-text-properties (match-beginning 0) (match-end 0)
  5195. '(display t invisible t intangible t))
  5196. (add-text-properties (match-beginning 1) (match-end 3)
  5197. '(font-lock-fontified t face org-meta-line))
  5198. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5199. '(font-lock-fontified t face org-block))
  5200. ; for backend-specific code
  5201. t)
  5202. ((and (match-end 4) (equal dc3 "+begin"))
  5203. ;; Truly a block
  5204. (setq block-type (downcase (match-string 5))
  5205. quoting (member block-type org-protecting-blocks))
  5206. (when (re-search-forward
  5207. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5208. nil t) ;; on purpose, we look further than LIMIT
  5209. (setq end (min (point-max) (match-end 0))
  5210. end1 (min (point-max) (1- (match-beginning 0))))
  5211. (setq block-end (match-beginning 0))
  5212. (when quoting
  5213. (org-remove-flyspell-overlays-in beg1 end1)
  5214. (remove-text-properties beg end
  5215. '(display t invisible t intangible t)))
  5216. (add-text-properties
  5217. beg end '(font-lock-fontified t font-lock-multiline t))
  5218. (add-text-properties beg beg1 '(face org-meta-line))
  5219. (org-remove-flyspell-overlays-in beg beg1)
  5220. (add-text-properties ; For end_src
  5221. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5222. (org-remove-flyspell-overlays-in end1 end)
  5223. (cond
  5224. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5225. (org-src-font-lock-fontify-block lang block-start block-end)
  5226. (add-text-properties beg1 block-end '(src-block t)))
  5227. (quoting
  5228. (add-text-properties beg1 (min (point-max) (1+ end1))
  5229. '(face org-block))) ; end of source block
  5230. ((not org-fontify-quote-and-verse-blocks))
  5231. ((string= block-type "quote")
  5232. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5233. ((string= block-type "verse")
  5234. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5235. (add-text-properties beg beg1 '(face org-block-begin-line))
  5236. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5237. '(face org-block-end-line))
  5238. t))
  5239. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5240. (org-remove-flyspell-overlays-in
  5241. (match-beginning 0)
  5242. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5243. (add-text-properties
  5244. beg (match-end 3)
  5245. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5246. '(font-lock-fontified t invisible t)
  5247. '(font-lock-fontified t face org-document-info-keyword)))
  5248. (add-text-properties
  5249. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5250. (if (string-equal dc1 "+title:")
  5251. '(font-lock-fontified t face org-document-title)
  5252. '(font-lock-fontified t face org-document-info))))
  5253. ((or (equal dc1 "+results")
  5254. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5255. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5256. "+call:" "+header:" "+headers:" "+name:"))
  5257. (and (match-end 4) (equal dc3 "+attr")))
  5258. (org-remove-flyspell-overlays-in
  5259. (match-beginning 0)
  5260. (if (equal "+caption:" dc1) (match-end 2) (match-end 0)))
  5261. (add-text-properties
  5262. beg (match-end 0)
  5263. '(font-lock-fontified t face org-meta-line))
  5264. t)
  5265. ((member dc3 '(" " ""))
  5266. (org-remove-flyspell-overlays-in beg (match-end 0))
  5267. (add-text-properties
  5268. beg (match-end 0)
  5269. '(font-lock-fontified t face font-lock-comment-face)))
  5270. (t ;; just any other in-buffer setting, but not indented
  5271. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5272. (add-text-properties
  5273. beg (match-end 0)
  5274. '(font-lock-fontified t face org-meta-line))
  5275. t))))))
  5276. (defun org-fontify-drawers (limit)
  5277. "Fontify drawers."
  5278. (when (re-search-forward org-drawer-regexp limit t)
  5279. (add-text-properties
  5280. (match-beginning 0) (match-end 0)
  5281. '(font-lock-fontified t face org-special-keyword))
  5282. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5283. t))
  5284. (defun org-fontify-macros (limit)
  5285. "Fontify macros."
  5286. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5287. (add-text-properties
  5288. (match-beginning 0) (match-end 0)
  5289. '(font-lock-fontified t face org-macro))
  5290. (when org-hide-macro-markers
  5291. (add-text-properties (match-end 2) (match-beginning 2)
  5292. '(invisible t))
  5293. (add-text-properties (match-beginning 1) (match-end 1)
  5294. '(invisible t)))
  5295. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5296. t))
  5297. (defun org-activate-angle-links (limit)
  5298. "Add text properties for angle links."
  5299. (if (and (re-search-forward org-angle-link-re limit t)
  5300. (not (org-in-src-block-p)))
  5301. (progn
  5302. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5303. (add-text-properties (match-beginning 0) (match-end 0)
  5304. (list 'mouse-face 'highlight
  5305. 'keymap org-mouse-map))
  5306. (org-rear-nonsticky-at (match-end 0))
  5307. t)))
  5308. (defun org-activate-footnote-links (limit)
  5309. "Add text properties for footnotes."
  5310. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5311. (when fn
  5312. (let* ((beg (nth 1 fn))
  5313. (end (nth 2 fn))
  5314. (label (car fn))
  5315. (referencep (/= (line-beginning-position) beg)))
  5316. (when (and referencep (nth 3 fn))
  5317. (save-excursion
  5318. (goto-char beg)
  5319. (search-forward (or label "fn:"))
  5320. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5321. (add-text-properties beg end
  5322. (list 'mouse-face 'highlight
  5323. 'keymap org-mouse-map
  5324. 'help-echo
  5325. (if referencep "Footnote reference"
  5326. "Footnote definition")
  5327. 'font-lock-fontified t
  5328. 'font-lock-multiline t
  5329. 'face 'org-footnote))))))
  5330. (defun org-activate-bracket-links (limit)
  5331. "Add text properties for bracketed links."
  5332. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5333. (not (org-in-src-block-p)))
  5334. (let* ((hl (org-match-string-no-properties 1))
  5335. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5336. (ip (org-maybe-intangible
  5337. (list 'invisible 'org-link
  5338. 'keymap org-mouse-map 'mouse-face 'highlight
  5339. 'font-lock-multiline t 'help-echo help
  5340. 'htmlize-link `(:uri ,hl))))
  5341. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5342. 'font-lock-multiline t 'help-echo help
  5343. 'htmlize-link `(:uri ,hl))))
  5344. ;; We need to remove the invisible property here. Table narrowing
  5345. ;; may have made some of this invisible.
  5346. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5347. (remove-text-properties (match-beginning 0) (match-end 0)
  5348. '(invisible nil))
  5349. (if (match-end 3)
  5350. (progn
  5351. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5352. (org-rear-nonsticky-at (match-beginning 3))
  5353. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5354. (org-rear-nonsticky-at (match-end 3))
  5355. (add-text-properties (match-end 3) (match-end 0) ip)
  5356. (org-rear-nonsticky-at (match-end 0)))
  5357. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5358. (org-rear-nonsticky-at (match-beginning 1))
  5359. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5360. (org-rear-nonsticky-at (match-end 1))
  5361. (add-text-properties (match-end 1) (match-end 0) ip)
  5362. (org-rear-nonsticky-at (match-end 0)))
  5363. t)))
  5364. (defun org-activate-dates (limit)
  5365. "Add text properties for dates."
  5366. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5367. (not (equal (char-before (match-beginning 0)) 91)))
  5368. (progn
  5369. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5370. (add-text-properties (match-beginning 0) (match-end 0)
  5371. (list 'mouse-face 'highlight
  5372. 'keymap org-mouse-map))
  5373. (org-rear-nonsticky-at (match-end 0))
  5374. (when org-display-custom-times
  5375. (if (match-end 3)
  5376. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5377. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5378. t)))
  5379. (defvar org-target-link-regexp nil
  5380. "Regular expression matching radio targets in plain text.")
  5381. (make-variable-buffer-local 'org-target-link-regexp)
  5382. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5383. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5384. border border border))
  5385. "Regular expression matching a link target.")
  5386. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5387. "Regular expression matching a radio target.")
  5388. (defconst org-any-target-regexp
  5389. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5390. "Regular expression matching any target.")
  5391. (defun org-activate-target-links (limit)
  5392. "Add text properties for target matches."
  5393. (when org-target-link-regexp
  5394. (let ((case-fold-search t))
  5395. (if (re-search-forward org-target-link-regexp limit t)
  5396. (progn
  5397. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5398. (add-text-properties (match-beginning 1) (match-end 1)
  5399. (list 'mouse-face 'highlight
  5400. 'keymap org-mouse-map
  5401. 'help-echo "Radio target link"
  5402. 'org-linked-text t))
  5403. (org-rear-nonsticky-at (match-end 1))
  5404. t)))))
  5405. (defun org-update-radio-target-regexp ()
  5406. "Find all radio targets in this file and update the regular expression.
  5407. Also refresh fontification if needed."
  5408. (interactive)
  5409. (let ((old-regexp org-target-link-regexp)
  5410. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5411. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5412. (targets
  5413. (org-with-wide-buffer
  5414. (goto-char (point-min))
  5415. (let (rtn)
  5416. (while (re-search-forward org-radio-target-regexp nil t)
  5417. ;; Make sure point is really within the object.
  5418. (backward-char)
  5419. (let ((obj (org-element-context)))
  5420. (when (eq (org-element-type obj) 'radio-target)
  5421. (add-to-list 'rtn (org-element-property :value obj)))))
  5422. rtn))))
  5423. (setq org-target-link-regexp
  5424. (and targets
  5425. (concat before-re
  5426. (mapconcat
  5427. (lambda (x)
  5428. (replace-regexp-in-string
  5429. " +" "\\s-+" (regexp-quote x) t t))
  5430. targets
  5431. "\\|")
  5432. after-re)))
  5433. (unless (equal old-regexp org-target-link-regexp)
  5434. ;; Clean-up cache.
  5435. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5436. ((not org-target-link-regexp) old-regexp)
  5437. (t
  5438. (concat before-re
  5439. (mapconcat
  5440. (lambda (re)
  5441. (substring re (length before-re)
  5442. (- (length after-re))))
  5443. (list old-regexp org-target-link-regexp)
  5444. "\\|")
  5445. after-re)))))
  5446. (org-with-wide-buffer
  5447. (goto-char (point-min))
  5448. (while (re-search-forward regexp nil t)
  5449. (org-element-cache-refresh (match-beginning 1)))))
  5450. ;; Re fontify buffer.
  5451. (when (memq 'radio org-highlight-links)
  5452. (org-restart-font-lock)))))
  5453. (defun org-hide-wide-columns (limit)
  5454. (let (s e)
  5455. (setq s (text-property-any (point) (or limit (point-max))
  5456. 'org-cwidth t))
  5457. (when s
  5458. (setq e (next-single-property-change s 'org-cwidth))
  5459. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5460. (goto-char e)
  5461. t)))
  5462. (defvar org-latex-and-related-regexp nil
  5463. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5464. (defun org-compute-latex-and-related-regexp ()
  5465. "Compute regular expression for LaTeX, entities and sub/superscript.
  5466. Result depends on variable `org-highlight-latex-and-related'."
  5467. (org-set-local
  5468. 'org-latex-and-related-regexp
  5469. (let* ((re-sub
  5470. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5471. ((eq org-use-sub-superscripts '{})
  5472. (list org-match-substring-with-braces-regexp))
  5473. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5474. (re-latex
  5475. (when (memq 'latex org-highlight-latex-and-related)
  5476. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5477. (delq nil
  5478. (mapcar (lambda (x)
  5479. (and (member (car x) matchers) (nth 1 x)))
  5480. org-latex-regexps)))))
  5481. (re-entities
  5482. (when (memq 'entities org-highlight-latex-and-related)
  5483. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5484. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5485. (defun org-do-latex-and-related (limit)
  5486. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5487. LIMIT bounds the search for syntax to highlight. Stop at first
  5488. highlighted object, if any. Return t if some highlighting was
  5489. done, nil otherwise."
  5490. (when (org-string-nw-p org-latex-and-related-regexp)
  5491. (catch 'found
  5492. (while (re-search-forward org-latex-and-related-regexp limit t)
  5493. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5494. 'face))
  5495. '(org-code org-verbatim underline))
  5496. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5497. '(?_ ?^))
  5498. 1
  5499. 0)))
  5500. (font-lock-prepend-text-property
  5501. (+ offset (match-beginning 0)) (match-end 0)
  5502. 'face 'org-latex-and-related)
  5503. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5504. '(font-lock-multiline t)))
  5505. (throw 'found t)))
  5506. nil)))
  5507. (defun org-restart-font-lock ()
  5508. "Restart `font-lock-mode', to force refontification."
  5509. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5510. (font-lock-mode -1)
  5511. (font-lock-mode 1)))
  5512. (defun org-activate-tags (limit)
  5513. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5514. (progn
  5515. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5516. (add-text-properties (match-beginning 1) (match-end 1)
  5517. (list 'mouse-face 'highlight
  5518. 'keymap org-mouse-map))
  5519. (org-rear-nonsticky-at (match-end 1))
  5520. t)))
  5521. (defun org-outline-level ()
  5522. "Compute the outline level of the heading at point.
  5523. If this is called at a normal headline, the level is the number of stars.
  5524. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5525. (save-excursion
  5526. (if (not (ignore-errors (org-back-to-heading t)))
  5527. 0
  5528. (looking-at org-outline-regexp)
  5529. (1- (- (match-end 0) (match-beginning 0))))))
  5530. (defvar org-font-lock-keywords nil)
  5531. (defsubst org-re-property (property &optional literal allow-null)
  5532. "Return a regexp matching a PROPERTY line.
  5533. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5534. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5535. non-nil, match properties even without a value.
  5536. Match group 3 is set to the value when it exists. If there is no
  5537. value and ALLOW-NULL is non-nil, it is set to the empty string."
  5538. (concat
  5539. "^\\(?4:[ \t]*\\)"
  5540. (format "\\(?1::\\(?2:%s\\):\\)"
  5541. (if literal property (regexp-quote property)))
  5542. (if allow-null
  5543. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$"
  5544. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$")))
  5545. (defconst org-property-re
  5546. (org-re-property "\\S-+" 'literal t)
  5547. "Regular expression matching a property line.
  5548. There are four matching groups:
  5549. 1: :PROPKEY: including the leading and trailing colon,
  5550. 2: PROPKEY without the leading and trailing colon,
  5551. 3: PROPVAL without leading or trailing spaces,
  5552. 4: the indentation of the current line,
  5553. 5: trailing whitespace.")
  5554. (defvar org-font-lock-hook nil
  5555. "Functions to be called for special font lock stuff.")
  5556. (defvar org-font-lock-set-keywords-hook nil
  5557. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5558. This is called after `org-font-lock-extra-keywords' is defined, but before
  5559. it is installed to be used by font lock. This can be useful if something
  5560. needs to be inserted at a specific position in the font-lock sequence.")
  5561. (defun org-font-lock-hook (limit)
  5562. "Run `org-font-lock-hook' within LIMIT."
  5563. (run-hook-with-args 'org-font-lock-hook limit))
  5564. (defun org-set-font-lock-defaults ()
  5565. "Set font lock defaults for the current buffer."
  5566. (let* ((em org-fontify-emphasized-text)
  5567. (lk org-highlight-links)
  5568. (org-font-lock-extra-keywords
  5569. (list
  5570. ;; Call the hook
  5571. '(org-font-lock-hook)
  5572. ;; Headlines
  5573. `(,(if org-fontify-whole-heading-line
  5574. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5575. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5576. (1 (org-get-level-face 1))
  5577. (2 (org-get-level-face 2))
  5578. (3 (org-get-level-face 3)))
  5579. ;; Table lines
  5580. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5581. (1 'org-table t))
  5582. ;; Table internals
  5583. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5584. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5585. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5586. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5587. ;; Drawers
  5588. '(org-fontify-drawers)
  5589. ;; Properties
  5590. (list org-property-re
  5591. '(1 'org-special-keyword t)
  5592. '(3 'org-property-value t))
  5593. ;; Links
  5594. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5595. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5596. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5597. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5598. (if (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5599. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5600. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5601. ;; Targets.
  5602. (list org-any-target-regexp '(0 'org-target t))
  5603. ;; Diary sexps.
  5604. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5605. ;; Macro
  5606. '(org-fontify-macros)
  5607. '(org-hide-wide-columns (0 nil append))
  5608. ;; TODO keyword
  5609. (list (format org-heading-keyword-regexp-format
  5610. org-todo-regexp)
  5611. '(2 (org-get-todo-face 2) t))
  5612. ;; DONE
  5613. (if org-fontify-done-headline
  5614. (list (format org-heading-keyword-regexp-format
  5615. (concat
  5616. "\\(?:"
  5617. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5618. "\\)"))
  5619. '(2 'org-headline-done t))
  5620. nil)
  5621. ;; Priorities
  5622. '(org-font-lock-add-priority-faces)
  5623. ;; Tags
  5624. '(org-font-lock-add-tag-faces)
  5625. ;; Tags groups
  5626. (if (and org-group-tags org-tag-groups-alist)
  5627. (list (concat org-outline-regexp-bol ".+\\(:"
  5628. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5629. ":\\).*$")
  5630. '(1 'org-tag-group prepend)))
  5631. ;; Special keywords
  5632. (list (concat "\\<" org-comment-string) '(0 'org-special-keyword t))
  5633. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5634. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5635. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5636. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5637. ;; Emphasis
  5638. (if em
  5639. (if (featurep 'xemacs)
  5640. '(org-do-emphasis-faces (0 nil append))
  5641. '(org-do-emphasis-faces)))
  5642. ;; Checkboxes
  5643. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5644. 1 'org-checkbox prepend)
  5645. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5646. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5647. (0 (org-get-checkbox-statistics-face) t)))
  5648. ;; Description list items
  5649. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5650. 1 'org-list-dt prepend)
  5651. ;; ARCHIVEd headings
  5652. (list (concat
  5653. org-outline-regexp-bol
  5654. "\\(.*:" org-archive-tag ":.*\\)")
  5655. '(1 'org-archived prepend))
  5656. ;; Specials
  5657. '(org-do-latex-and-related)
  5658. '(org-fontify-entities)
  5659. '(org-raise-scripts)
  5660. ;; Code
  5661. '(org-activate-code (1 'org-code t))
  5662. ;; COMMENT
  5663. (list (format
  5664. "^\\*\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5665. org-todo-regexp
  5666. org-comment-string)
  5667. '(9 'org-special-keyword t))
  5668. ;; Blocks and meta lines
  5669. '(org-fontify-meta-lines-and-blocks))))
  5670. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5671. (run-hooks 'org-font-lock-set-keywords-hook)
  5672. ;; Now set the full font-lock-keywords
  5673. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5674. (org-set-local 'font-lock-defaults
  5675. '(org-font-lock-keywords t nil nil backward-paragraph))
  5676. (kill-local-variable 'font-lock-keywords) nil))
  5677. (defun org-toggle-pretty-entities ()
  5678. "Toggle the composition display of entities as UTF8 characters."
  5679. (interactive)
  5680. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5681. (org-restart-font-lock)
  5682. (if org-pretty-entities
  5683. (message "Entities are now displayed as UTF8 characters")
  5684. (save-restriction
  5685. (widen)
  5686. (org-decompose-region (point-min) (point-max))
  5687. (message "Entities are now displayed as plain text"))))
  5688. (defvar org-custom-properties-overlays nil
  5689. "List of overlays used for custom properties.")
  5690. (make-variable-buffer-local 'org-custom-properties-overlays)
  5691. (defun org-toggle-custom-properties-visibility ()
  5692. "Display or hide properties in `org-custom-properties'."
  5693. (interactive)
  5694. (if org-custom-properties-overlays
  5695. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5696. (setq org-custom-properties-overlays nil))
  5697. (unless (not org-custom-properties)
  5698. (save-excursion
  5699. (save-restriction
  5700. (widen)
  5701. (goto-char (point-min))
  5702. (while (re-search-forward org-property-re nil t)
  5703. (mapc (lambda(p)
  5704. (when (equal p (substring (match-string 1) 1 -1))
  5705. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5706. (overlay-put o 'invisible t)
  5707. (overlay-put o 'org-custom-property t)
  5708. (push o org-custom-properties-overlays))))
  5709. org-custom-properties)))))))
  5710. (defun org-fontify-entities (limit)
  5711. "Find an entity to fontify."
  5712. (let (ee)
  5713. (when org-pretty-entities
  5714. (catch 'match
  5715. (while (re-search-forward
  5716. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5717. limit t)
  5718. (if (and (not (org-at-comment-p))
  5719. (setq ee (org-entity-get (match-string 1)))
  5720. (= (length (nth 6 ee)) 1))
  5721. (let*
  5722. ((end (if (equal (match-string 2) "{}")
  5723. (match-end 2)
  5724. (match-end 1))))
  5725. (add-text-properties
  5726. (match-beginning 0) end
  5727. (list 'font-lock-fontified t))
  5728. (compose-region (match-beginning 0) end
  5729. (nth 6 ee) nil)
  5730. (backward-char 1)
  5731. (throw 'match t))))
  5732. nil))))
  5733. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5734. "Fontify string S like in Org-mode."
  5735. (with-temp-buffer
  5736. (insert s)
  5737. (let ((org-odd-levels-only odd-levels))
  5738. (org-mode)
  5739. (font-lock-ensure)
  5740. (buffer-string))))
  5741. (defvar org-m nil)
  5742. (defvar org-l nil)
  5743. (defvar org-f nil)
  5744. (defun org-get-level-face (n)
  5745. "Get the right face for match N in font-lock matching of headlines."
  5746. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5747. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5748. (if org-cycle-level-faces
  5749. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5750. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5751. (cond
  5752. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5753. ((eq n 2) org-f)
  5754. (t (if org-level-color-stars-only nil org-f))))
  5755. (defun org-get-todo-face (kwd)
  5756. "Get the right face for a TODO keyword KWD.
  5757. If KWD is a number, get the corresponding match group."
  5758. (if (numberp kwd) (setq kwd (match-string kwd)))
  5759. (or (org-face-from-face-or-color
  5760. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5761. (and (member kwd org-done-keywords) 'org-done)
  5762. 'org-todo))
  5763. (defun org-face-from-face-or-color (context inherit face-or-color)
  5764. "Create a face list that inherits INHERIT, but sets the foreground color.
  5765. When FACE-OR-COLOR is not a string, just return it."
  5766. (if (stringp face-or-color)
  5767. (list :inherit inherit
  5768. (cdr (assoc context org-faces-easy-properties))
  5769. face-or-color)
  5770. face-or-color))
  5771. (defun org-font-lock-add-tag-faces (limit)
  5772. "Add the special tag faces."
  5773. (when (and org-tag-faces org-tags-special-faces-re)
  5774. (while (re-search-forward org-tags-special-faces-re limit t)
  5775. (add-text-properties (match-beginning 1) (match-end 1)
  5776. (list 'face (org-get-tag-face 1)
  5777. 'font-lock-fontified t))
  5778. (backward-char 1))))
  5779. (defun org-font-lock-add-priority-faces (limit)
  5780. "Add the special priority faces."
  5781. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5782. (when (save-match-data (org-at-heading-p))
  5783. (add-text-properties
  5784. (match-beginning 0) (match-end 0)
  5785. (list 'face (or (org-face-from-face-or-color
  5786. 'priority 'org-priority
  5787. (cdr (assoc (char-after (match-beginning 1))
  5788. org-priority-faces)))
  5789. 'org-priority)
  5790. 'font-lock-fontified t)))))
  5791. (defun org-get-tag-face (kwd)
  5792. "Get the right face for a TODO keyword KWD.
  5793. If KWD is a number, get the corresponding match group."
  5794. (if (numberp kwd) (setq kwd (match-string kwd)))
  5795. (or (org-face-from-face-or-color
  5796. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5797. 'org-tag))
  5798. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5799. "Remove fontification and activation overlays from links."
  5800. (font-lock-default-unfontify-region beg end)
  5801. (let* ((buffer-undo-list t)
  5802. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5803. (inhibit-modification-hooks t)
  5804. deactivate-mark buffer-file-name buffer-file-truename)
  5805. (org-decompose-region beg end)
  5806. (remove-text-properties beg end
  5807. '(mouse-face t keymap t org-linked-text t
  5808. invisible t intangible t
  5809. org-emphasis t))
  5810. (org-remove-font-lock-display-properties beg end)))
  5811. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5812. ((raise 0.3) (height 0.7))
  5813. ((raise -0.5))
  5814. ((raise 0.5)))
  5815. "Display properties for showing superscripts and subscripts.")
  5816. (defun org-remove-font-lock-display-properties (beg end)
  5817. "Remove specific display properties that have been added by font lock.
  5818. The will remove the raise properties that are used to show superscripts
  5819. and subscripts."
  5820. (let (next prop)
  5821. (while (< beg end)
  5822. (setq next (next-single-property-change beg 'display nil end)
  5823. prop (get-text-property beg 'display))
  5824. (if (member prop org-script-display)
  5825. (put-text-property beg next 'display nil))
  5826. (setq beg next))))
  5827. (defun org-raise-scripts (limit)
  5828. "Add raise properties to sub/superscripts."
  5829. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5830. (if (re-search-forward
  5831. (if (eq org-use-sub-superscripts t)
  5832. org-match-substring-regexp
  5833. org-match-substring-with-braces-regexp)
  5834. limit t)
  5835. (let* ((pos (point)) table-p comment-p
  5836. (mpos (match-beginning 3))
  5837. (emph-p (get-text-property mpos 'org-emphasis))
  5838. (link-p (get-text-property mpos 'mouse-face))
  5839. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5840. (goto-char (point-at-bol))
  5841. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5842. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5843. (goto-char pos)
  5844. ;; Handle a_b^c
  5845. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5846. (if (or comment-p emph-p link-p keyw-p)
  5847. t
  5848. (put-text-property (match-beginning 3) (match-end 0)
  5849. 'display
  5850. (if (equal (char-after (match-beginning 2)) ?^)
  5851. (nth (if table-p 3 1) org-script-display)
  5852. (nth (if table-p 2 0) org-script-display)))
  5853. (add-text-properties (match-beginning 2) (match-end 2)
  5854. (list 'invisible t
  5855. 'org-dwidth t 'org-dwidth-n 1))
  5856. (if (and (eq (char-after (match-beginning 3)) ?{)
  5857. (eq (char-before (match-end 3)) ?}))
  5858. (progn
  5859. (add-text-properties
  5860. (match-beginning 3) (1+ (match-beginning 3))
  5861. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5862. (add-text-properties
  5863. (1- (match-end 3)) (match-end 3)
  5864. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5865. t)))))
  5866. ;;;; Visibility cycling, including org-goto and indirect buffer
  5867. ;;; Cycling
  5868. (defvar org-cycle-global-status nil)
  5869. (make-variable-buffer-local 'org-cycle-global-status)
  5870. (put 'org-cycle-global-status 'org-state t)
  5871. (defvar org-cycle-subtree-status nil)
  5872. (make-variable-buffer-local 'org-cycle-subtree-status)
  5873. (put 'org-cycle-subtree-status 'org-state t)
  5874. (defvar org-inlinetask-min-level)
  5875. (defun org-unlogged-message (&rest args)
  5876. "Display a message, but avoid logging it in the *Messages* buffer."
  5877. (let ((message-log-max nil))
  5878. (apply 'message args)))
  5879. ;;;###autoload
  5880. (defun org-cycle (&optional arg)
  5881. "TAB-action and visibility cycling for Org-mode.
  5882. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5883. is outline visibility cycling, but it also invokes other actions
  5884. in special contexts.
  5885. - When this function is called with a prefix argument, rotate the entire
  5886. buffer through 3 states (global cycling)
  5887. 1. OVERVIEW: Show only top-level headlines.
  5888. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5889. 3. SHOW ALL: Show everything.
  5890. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5891. determined by the variable `org-startup-folded', and by any VISIBILITY
  5892. properties in the buffer.
  5893. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5894. including any drawers.
  5895. - When inside a table, re-align the table and move to the next field.
  5896. - When point is at the beginning of a headline, rotate the subtree started
  5897. by this line through 3 different states (local cycling)
  5898. 1. FOLDED: Only the main headline is shown.
  5899. 2. CHILDREN: The main headline and the direct children are shown.
  5900. From this state, you can move to one of the children
  5901. and zoom in further.
  5902. 3. SUBTREE: Show the entire subtree, including body text.
  5903. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5904. - When point is at the beginning of an empty headline and the variable
  5905. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5906. of the headline by demoting and promoting it to likely levels. This
  5907. speeds up creation document structure by pressing TAB once or several
  5908. times right after creating a new headline.
  5909. - When there is a numeric prefix, go up to a heading with level ARG, do
  5910. a `show-subtree' and return to the previous cursor position. If ARG
  5911. is negative, go up that many levels.
  5912. - When point is not at the beginning of a headline, execute the global
  5913. binding for TAB, which is re-indenting the line. See the option
  5914. `org-cycle-emulate-tab' for details.
  5915. - Special case: if point is at the beginning of the buffer and there is
  5916. no headline in line 1, this function will act as if called with prefix arg
  5917. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5918. But only if also the variable `org-cycle-global-at-bob' is t."
  5919. (interactive "P")
  5920. (org-load-modules-maybe)
  5921. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5922. (and org-cycle-level-after-item/entry-creation
  5923. (or (org-cycle-level)
  5924. (org-cycle-item-indentation))))
  5925. (let* ((limit-level
  5926. (or org-cycle-max-level
  5927. (and (boundp 'org-inlinetask-min-level)
  5928. org-inlinetask-min-level
  5929. (1- org-inlinetask-min-level))))
  5930. (nstars (and limit-level
  5931. (if org-odd-levels-only
  5932. (and limit-level (1- (* limit-level 2)))
  5933. limit-level)))
  5934. (org-outline-regexp
  5935. (if (not (derived-mode-p 'org-mode))
  5936. outline-regexp
  5937. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5938. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5939. (not (looking-at org-outline-regexp))))
  5940. (org-cycle-hook
  5941. (if bob-special
  5942. (delq 'org-optimize-window-after-visibility-change
  5943. (copy-sequence org-cycle-hook))
  5944. org-cycle-hook))
  5945. (pos (point)))
  5946. (if (or bob-special (equal arg '(4)))
  5947. ;; special case: use global cycling
  5948. (setq arg t))
  5949. (cond
  5950. ((equal arg '(16))
  5951. (setq last-command 'dummy)
  5952. (org-set-startup-visibility)
  5953. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5954. ((equal arg '(64))
  5955. (show-all)
  5956. (org-unlogged-message "Entire buffer visible, including drawers"))
  5957. ;; Try cdlatex TAB completion
  5958. ((org-try-cdlatex-tab))
  5959. ;; Table: enter it or move to the next field.
  5960. ((org-at-table-p 'any)
  5961. (if (org-at-table.el-p)
  5962. (message "Use C-c ' to edit table.el tables")
  5963. (if arg (org-table-edit-field t)
  5964. (org-table-justify-field-maybe)
  5965. (call-interactively 'org-table-next-field))))
  5966. ((run-hook-with-args-until-success
  5967. 'org-tab-after-check-for-table-hook))
  5968. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5969. ((eq arg t) (org-cycle-internal-global))
  5970. ;; Drawers: delegate to `org-flag-drawer'.
  5971. ((save-excursion
  5972. (beginning-of-line 1)
  5973. (looking-at org-drawer-regexp))
  5974. (org-flag-drawer ; toggle block visibility
  5975. (not (get-char-property (match-end 0) 'invisible))))
  5976. ;; Show-subtree, ARG levels up from here.
  5977. ((integerp arg)
  5978. (save-excursion
  5979. (org-back-to-heading)
  5980. (outline-up-heading (if (< arg 0) (- arg)
  5981. (- (funcall outline-level) arg)))
  5982. (org-show-subtree)))
  5983. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5984. ((and (featurep 'org-inlinetask)
  5985. (org-inlinetask-at-task-p)
  5986. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5987. (org-inlinetask-toggle-visibility))
  5988. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5989. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5990. (save-excursion (beginning-of-line 1)
  5991. (looking-at org-outline-regexp)))
  5992. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5993. (org-cycle-internal-local))
  5994. ;; From there: TAB emulation and template completion.
  5995. (buffer-read-only (org-back-to-heading))
  5996. ((run-hook-with-args-until-success
  5997. 'org-tab-after-check-for-cycling-hook))
  5998. ((org-try-structure-completion))
  5999. ((run-hook-with-args-until-success
  6000. 'org-tab-before-tab-emulation-hook))
  6001. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  6002. (or (not (bolp))
  6003. (not (looking-at org-outline-regexp))))
  6004. (call-interactively (global-key-binding "\t")))
  6005. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  6006. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  6007. (or (and (eq org-cycle-emulate-tab 'white)
  6008. (= (match-end 0) (point-at-eol)))
  6009. (and (eq org-cycle-emulate-tab 'whitestart)
  6010. (>= (match-end 0) pos))))
  6011. t
  6012. (eq org-cycle-emulate-tab t))
  6013. (call-interactively (global-key-binding "\t")))
  6014. (t (save-excursion
  6015. (org-back-to-heading)
  6016. (org-cycle)))))))
  6017. (defun org-cycle-internal-global ()
  6018. "Do the global cycling action."
  6019. ;; Hack to avoid display of messages for .org attachments in Gnus
  6020. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6021. (cond
  6022. ((and (eq last-command this-command)
  6023. (eq org-cycle-global-status 'overview))
  6024. ;; We just created the overview - now do table of contents
  6025. ;; This can be slow in very large buffers, so indicate action
  6026. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6027. (unless ga (org-unlogged-message "CONTENTS..."))
  6028. (org-content)
  6029. (unless ga (org-unlogged-message "CONTENTS...done"))
  6030. (setq org-cycle-global-status 'contents)
  6031. (run-hook-with-args 'org-cycle-hook 'contents))
  6032. ((and (eq last-command this-command)
  6033. (eq org-cycle-global-status 'contents))
  6034. ;; We just showed the table of contents - now show everything
  6035. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6036. (show-all)
  6037. (unless ga (org-unlogged-message "SHOW ALL"))
  6038. (setq org-cycle-global-status 'all)
  6039. (run-hook-with-args 'org-cycle-hook 'all))
  6040. (t
  6041. ;; Default action: go to overview
  6042. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6043. (org-overview)
  6044. (unless ga (org-unlogged-message "OVERVIEW"))
  6045. (setq org-cycle-global-status 'overview)
  6046. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6047. (defvar org-called-with-limited-levels nil
  6048. "Non-nil when `org-with-limited-levels' is currently active.")
  6049. (defun org-cycle-internal-local ()
  6050. "Do the local cycling action."
  6051. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6052. ;; First, determine end of headline (EOH), end of subtree or item
  6053. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6054. (save-excursion
  6055. (if (org-at-item-p)
  6056. (progn
  6057. (beginning-of-line)
  6058. (setq struct (org-list-struct))
  6059. (setq eoh (point-at-eol))
  6060. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6061. (setq has-children (org-list-has-child-p (point) struct)))
  6062. (org-back-to-heading)
  6063. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6064. (setq eos (save-excursion (org-end-of-subtree t t)
  6065. (when (bolp) (backward-char)) (point)))
  6066. (setq has-children
  6067. (or (save-excursion
  6068. (let ((level (funcall outline-level)))
  6069. (outline-next-heading)
  6070. (and (org-at-heading-p t)
  6071. (> (funcall outline-level) level))))
  6072. (save-excursion
  6073. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6074. ;; Determine end invisible part of buffer (EOL)
  6075. (beginning-of-line 2)
  6076. ;; XEmacs doesn't have `next-single-char-property-change'
  6077. (if (featurep 'xemacs)
  6078. (while (and (not (eobp)) ;; this is like `next-line'
  6079. (get-char-property (1- (point)) 'invisible))
  6080. (beginning-of-line 2))
  6081. (while (and (not (eobp)) ;; this is like `next-line'
  6082. (get-char-property (1- (point)) 'invisible))
  6083. (goto-char (next-single-char-property-change (point) 'invisible))
  6084. (and (eolp) (beginning-of-line 2))))
  6085. (setq eol (point)))
  6086. ;; Find out what to do next and set `this-command'
  6087. (cond
  6088. ((= eos eoh)
  6089. ;; Nothing is hidden behind this heading
  6090. (unless (org-before-first-heading-p)
  6091. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6092. (org-unlogged-message "EMPTY ENTRY")
  6093. (setq org-cycle-subtree-status nil)
  6094. (save-excursion
  6095. (goto-char eos)
  6096. (outline-next-heading)
  6097. (if (outline-invisible-p) (org-flag-heading nil))))
  6098. ((and (or (>= eol eos)
  6099. (not (string-match "\\S-" (buffer-substring eol eos))))
  6100. (or has-children
  6101. (not (setq children-skipped
  6102. org-cycle-skip-children-state-if-no-children))))
  6103. ;; Entire subtree is hidden in one line: children view
  6104. (unless (org-before-first-heading-p)
  6105. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6106. (if (org-at-item-p)
  6107. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6108. (org-show-entry)
  6109. (org-with-limited-levels (show-children))
  6110. ;; FIXME: This slows down the func way too much.
  6111. ;; How keep drawers hidden in subtree anyway?
  6112. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6113. ;; (org-cycle-hide-drawers 'subtree))
  6114. ;; Fold every list in subtree to top-level items.
  6115. (when (eq org-cycle-include-plain-lists 'integrate)
  6116. (save-excursion
  6117. (org-back-to-heading)
  6118. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6119. (beginning-of-line 1)
  6120. (let* ((struct (org-list-struct))
  6121. (prevs (org-list-prevs-alist struct))
  6122. (end (org-list-get-bottom-point struct)))
  6123. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6124. (org-list-get-all-items (point) struct prevs))
  6125. (goto-char (if (< end eos) end eos)))))))
  6126. (org-unlogged-message "CHILDREN")
  6127. (save-excursion
  6128. (goto-char eos)
  6129. (outline-next-heading)
  6130. (if (outline-invisible-p) (org-flag-heading nil)))
  6131. (setq org-cycle-subtree-status 'children)
  6132. (unless (org-before-first-heading-p)
  6133. (run-hook-with-args 'org-cycle-hook 'children)))
  6134. ((or children-skipped
  6135. (and (eq last-command this-command)
  6136. (eq org-cycle-subtree-status 'children)))
  6137. ;; We just showed the children, or no children are there,
  6138. ;; now show everything.
  6139. (unless (org-before-first-heading-p)
  6140. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6141. (outline-flag-region eoh eos nil)
  6142. (org-unlogged-message
  6143. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6144. (setq org-cycle-subtree-status 'subtree)
  6145. (unless (org-before-first-heading-p)
  6146. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6147. (t
  6148. ;; Default action: hide the subtree.
  6149. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6150. (outline-flag-region eoh eos t)
  6151. (org-unlogged-message "FOLDED")
  6152. (setq org-cycle-subtree-status 'folded)
  6153. (unless (org-before-first-heading-p)
  6154. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6155. ;;;###autoload
  6156. (defun org-global-cycle (&optional arg)
  6157. "Cycle the global visibility. For details see `org-cycle'.
  6158. With \\[universal-argument] prefix arg, switch to startup visibility.
  6159. With a numeric prefix, show all headlines up to that level."
  6160. (interactive "P")
  6161. (let ((org-cycle-include-plain-lists
  6162. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6163. (cond
  6164. ((integerp arg)
  6165. (show-all)
  6166. (hide-sublevels arg)
  6167. (setq org-cycle-global-status 'contents))
  6168. ((equal arg '(4))
  6169. (org-set-startup-visibility)
  6170. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6171. (t
  6172. (org-cycle '(4))))))
  6173. (defun org-set-startup-visibility ()
  6174. "Set the visibility required by startup options and properties."
  6175. (cond
  6176. ((eq org-startup-folded t)
  6177. (org-overview))
  6178. ((eq org-startup-folded 'content)
  6179. (org-content))
  6180. ((or (eq org-startup-folded 'showeverything)
  6181. (eq org-startup-folded nil))
  6182. (show-all)))
  6183. (unless (eq org-startup-folded 'showeverything)
  6184. (if org-hide-block-startup (org-hide-block-all))
  6185. (org-set-visibility-according-to-property 'no-cleanup)
  6186. (org-cycle-hide-archived-subtrees 'all)
  6187. (org-cycle-hide-drawers 'all)
  6188. (org-cycle-show-empty-lines t)))
  6189. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6190. "Switch subtree visibilities according to :VISIBILITY: property."
  6191. (interactive)
  6192. (let (org-show-entry-below state)
  6193. (save-excursion
  6194. (goto-char (point-min))
  6195. (while (re-search-forward
  6196. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6197. nil t)
  6198. (setq state (match-string 1))
  6199. (save-excursion
  6200. (org-back-to-heading t)
  6201. (hide-subtree)
  6202. (org-reveal)
  6203. (cond
  6204. ((equal state '("fold" "folded"))
  6205. (hide-subtree))
  6206. ((equal state "children")
  6207. (org-show-hidden-entry)
  6208. (show-children))
  6209. ((equal state "content")
  6210. (save-excursion
  6211. (save-restriction
  6212. (org-narrow-to-subtree)
  6213. (org-content))))
  6214. ((member state '("all" "showall"))
  6215. (show-subtree)))))
  6216. (unless no-cleanup
  6217. (org-cycle-hide-archived-subtrees 'all)
  6218. (org-cycle-hide-drawers 'all)
  6219. (org-cycle-show-empty-lines 'all)))))
  6220. ;; This function uses outline-regexp instead of the more fundamental
  6221. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6222. ;; buffers, where outline-regexp is needed.
  6223. (defun org-overview ()
  6224. "Switch to overview mode, showing only top-level headlines.
  6225. This shows all headlines with a level equal or greater than the level
  6226. of the first headline in the buffer. This is important, because if the
  6227. first headline is not level one, then (hide-sublevels 1) gives confusing
  6228. results."
  6229. (interactive)
  6230. (save-excursion
  6231. (let ((level
  6232. (save-excursion
  6233. (goto-char (point-min))
  6234. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6235. (progn
  6236. (goto-char (match-beginning 0))
  6237. (funcall outline-level))))))
  6238. (and level (hide-sublevels level)))))
  6239. (defun org-content (&optional arg)
  6240. "Show all headlines in the buffer, like a table of contents.
  6241. With numerical argument N, show content up to level N."
  6242. (interactive "P")
  6243. (org-overview)
  6244. (save-excursion
  6245. ;; Visit all headings and show their offspring
  6246. (and (integerp arg) (org-overview))
  6247. (goto-char (point-max))
  6248. (catch 'exit
  6249. (while (and (progn (condition-case nil
  6250. (outline-previous-visible-heading 1)
  6251. (error (goto-char (point-min))))
  6252. t)
  6253. (looking-at org-outline-regexp))
  6254. (if (integerp arg)
  6255. (show-children (1- arg))
  6256. (show-branches))
  6257. (if (bobp) (throw 'exit nil))))))
  6258. (defun org-optimize-window-after-visibility-change (state)
  6259. "Adjust the window after a change in outline visibility.
  6260. This function is the default value of the hook `org-cycle-hook'."
  6261. (when (get-buffer-window (current-buffer))
  6262. (cond
  6263. ((eq state 'content) nil)
  6264. ((eq state 'all) nil)
  6265. ((eq state 'folded) nil)
  6266. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6267. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6268. (defun org-remove-empty-overlays-at (pos)
  6269. "Remove outline overlays that do not contain non-white stuff."
  6270. (mapc
  6271. (lambda (o)
  6272. (and (eq 'outline (overlay-get o 'invisible))
  6273. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6274. (overlay-end o))))
  6275. (delete-overlay o)))
  6276. (overlays-at pos)))
  6277. (defun org-clean-visibility-after-subtree-move ()
  6278. "Fix visibility issues after moving a subtree."
  6279. ;; First, find a reasonable region to look at:
  6280. ;; Start two siblings above, end three below
  6281. (let* ((beg (save-excursion
  6282. (and (org-get-last-sibling)
  6283. (org-get-last-sibling))
  6284. (point)))
  6285. (end (save-excursion
  6286. (and (org-get-next-sibling)
  6287. (org-get-next-sibling)
  6288. (org-get-next-sibling))
  6289. (if (org-at-heading-p)
  6290. (point-at-eol)
  6291. (point))))
  6292. (level (looking-at "\\*+"))
  6293. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6294. (save-excursion
  6295. (save-restriction
  6296. (narrow-to-region beg end)
  6297. (when re
  6298. ;; Properly fold already folded siblings
  6299. (goto-char (point-min))
  6300. (while (re-search-forward re nil t)
  6301. (if (and (not (outline-invisible-p))
  6302. (save-excursion
  6303. (goto-char (point-at-eol)) (outline-invisible-p)))
  6304. (hide-entry))))
  6305. (org-cycle-show-empty-lines 'overview)
  6306. (org-cycle-hide-drawers 'overview)))))
  6307. (defun org-cycle-show-empty-lines (state)
  6308. "Show empty lines above all visible headlines.
  6309. The region to be covered depends on STATE when called through
  6310. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6311. entire buffer covered. Note that an empty line is only shown if there
  6312. are at least `org-cycle-separator-lines' empty lines before the headline."
  6313. (when (not (= org-cycle-separator-lines 0))
  6314. (save-excursion
  6315. (let* ((n (abs org-cycle-separator-lines))
  6316. (re (cond
  6317. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6318. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6319. (t (let ((ns (number-to-string (- n 2))))
  6320. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6321. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6322. beg end b e)
  6323. (cond
  6324. ((memq state '(overview contents t))
  6325. (setq beg (point-min) end (point-max)))
  6326. ((memq state '(children folded))
  6327. (setq beg (point) end (progn (org-end-of-subtree t t)
  6328. (beginning-of-line 2)
  6329. (point)))))
  6330. (when beg
  6331. (goto-char beg)
  6332. (while (re-search-forward re end t)
  6333. (unless (get-char-property (match-end 1) 'invisible)
  6334. (setq e (match-end 1))
  6335. (if (< org-cycle-separator-lines 0)
  6336. (setq b (save-excursion
  6337. (goto-char (match-beginning 0))
  6338. (org-back-over-empty-lines)
  6339. (if (save-excursion
  6340. (goto-char (max (point-min) (1- (point))))
  6341. (org-at-heading-p))
  6342. (1- (point))
  6343. (point))))
  6344. (setq b (match-beginning 1)))
  6345. (outline-flag-region b e nil)))))))
  6346. ;; Never hide empty lines at the end of the file.
  6347. (save-excursion
  6348. (goto-char (point-max))
  6349. (outline-previous-heading)
  6350. (outline-end-of-heading)
  6351. (if (and (looking-at "[ \t\n]+")
  6352. (= (match-end 0) (point-max)))
  6353. (outline-flag-region (point) (match-end 0) nil))))
  6354. (defun org-show-empty-lines-in-parent ()
  6355. "Move to the parent and re-show empty lines before visible headlines."
  6356. (save-excursion
  6357. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6358. (org-cycle-show-empty-lines context))))
  6359. (defun org-files-list ()
  6360. "Return `org-agenda-files' list, plus all open org-mode files.
  6361. This is useful for operations that need to scan all of a user's
  6362. open and agenda-wise Org files."
  6363. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6364. (dolist (buf (buffer-list))
  6365. (with-current-buffer buf
  6366. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6367. (let ((file (expand-file-name (buffer-file-name))))
  6368. (unless (member file files)
  6369. (push file files))))))
  6370. files))
  6371. (defsubst org-entry-beginning-position ()
  6372. "Return the beginning position of the current entry."
  6373. (save-excursion (outline-back-to-heading t) (point)))
  6374. (defsubst org-entry-end-position ()
  6375. "Return the end position of the current entry."
  6376. (save-excursion (outline-next-heading) (point)))
  6377. (defun org-cycle-hide-drawers (state &optional exceptions)
  6378. "Re-hide all drawers after a visibility state change.
  6379. When non-nil, optional argument EXCEPTIONS is a list of strings
  6380. specifying which drawers should not be hidden."
  6381. (when (and (derived-mode-p 'org-mode)
  6382. (not (memq state '(overview folded contents))))
  6383. (save-excursion
  6384. (let* ((globalp (memq state '(contents all)))
  6385. (beg (if globalp (point-min) (point)))
  6386. (end (if globalp (point-max)
  6387. (if (eq state 'children)
  6388. (save-excursion (outline-next-heading) (point))
  6389. (org-end-of-subtree t)))))
  6390. (goto-char beg)
  6391. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6392. (unless (member-ignore-case (match-string 1) exceptions)
  6393. (let ((drawer (org-element-at-point)))
  6394. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6395. (org-flag-drawer t drawer)
  6396. ;; Make sure to skip drawer entirely or we might flag
  6397. ;; it another time when matching its ending line with
  6398. ;; `org-drawer-regexp'.
  6399. (goto-char (org-element-property :end drawer))))))))))
  6400. (defun org-cycle-hide-inline-tasks (state)
  6401. "Re-hide inline tasks when switching to 'contents or 'children
  6402. visibility state."
  6403. (case state
  6404. (contents
  6405. (when (org-bound-and-true-p org-inlinetask-min-level)
  6406. (hide-sublevels (1- org-inlinetask-min-level))))
  6407. (children
  6408. (when (featurep 'org-inlinetask)
  6409. (save-excursion
  6410. (while (and (outline-next-heading)
  6411. (org-inlinetask-at-task-p))
  6412. (org-inlinetask-toggle-visibility)
  6413. (org-inlinetask-goto-end)))))))
  6414. (defun org-flag-drawer (flag &optional element)
  6415. "When FLAG is non-nil, hide the drawer we are at.
  6416. Otherwise make it visible. When optional argument ELEMENT is
  6417. a parsed drawer, as returned by `org-element-at-point', hide or
  6418. show that drawer instead."
  6419. (when (save-excursion
  6420. (beginning-of-line)
  6421. (org-looking-at-p org-drawer-regexp))
  6422. (let ((drawer (or element (org-element-at-point))))
  6423. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6424. (let ((post (org-element-property :post-affiliated drawer)))
  6425. (save-excursion
  6426. (outline-flag-region
  6427. (progn (goto-char post) (line-end-position))
  6428. (progn (goto-char (org-element-property :end drawer))
  6429. (skip-chars-backward " \r\t\n")
  6430. (line-end-position))
  6431. flag))
  6432. ;; When the drawer is hidden away, make sure point lies in
  6433. ;; a visible part of the buffer.
  6434. (when (and flag (> (line-beginning-position) post))
  6435. (goto-char post)))))))
  6436. (defun org-subtree-end-visible-p ()
  6437. "Is the end of the current subtree visible?"
  6438. (pos-visible-in-window-p
  6439. (save-excursion (org-end-of-subtree t) (point))))
  6440. (defun org-first-headline-recenter ()
  6441. "Move cursor to the first headline and recenter the headline."
  6442. (goto-char (point-min))
  6443. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6444. (set-window-start (selected-window) (point-at-bol))))
  6445. ;;; Saving and restoring visibility
  6446. (defun org-outline-overlay-data (&optional use-markers)
  6447. "Return a list of the locations of all outline overlays.
  6448. These are overlays with the `invisible' property value `outline'.
  6449. The return value is a list of cons cells, with start and stop
  6450. positions for each overlay.
  6451. If USE-MARKERS is set, return the positions as markers."
  6452. (let (beg end)
  6453. (save-excursion
  6454. (save-restriction
  6455. (widen)
  6456. (delq nil
  6457. (mapcar (lambda (o)
  6458. (when (eq (overlay-get o 'invisible) 'outline)
  6459. (setq beg (overlay-start o)
  6460. end (overlay-end o))
  6461. (and beg end (> end beg)
  6462. (if use-markers
  6463. (cons (copy-marker beg)
  6464. (copy-marker end t))
  6465. (cons beg end)))))
  6466. (overlays-in (point-min) (point-max))))))))
  6467. (defun org-set-outline-overlay-data (data)
  6468. "Create visibility overlays for all positions in DATA.
  6469. DATA should have been made by `org-outline-overlay-data'."
  6470. (let (o)
  6471. (save-excursion
  6472. (save-restriction
  6473. (widen)
  6474. (show-all)
  6475. (mapc (lambda (c)
  6476. (outline-flag-region (car c) (cdr c) t))
  6477. data)))))
  6478. ;;; Folding of blocks
  6479. (defvar org-hide-block-overlays nil
  6480. "Overlays hiding blocks.")
  6481. (make-variable-buffer-local 'org-hide-block-overlays)
  6482. (defun org-block-map (function &optional start end)
  6483. "Call FUNCTION at the head of all source blocks in the current buffer.
  6484. Optional arguments START and END can be used to limit the range."
  6485. (let ((start (or start (point-min)))
  6486. (end (or end (point-max))))
  6487. (save-excursion
  6488. (goto-char start)
  6489. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6490. (save-excursion
  6491. (save-match-data
  6492. (goto-char (match-beginning 0))
  6493. (funcall function)))))))
  6494. (defun org-hide-block-toggle-all ()
  6495. "Toggle the visibility of all blocks in the current buffer."
  6496. (org-block-map 'org-hide-block-toggle))
  6497. (defun org-hide-block-all ()
  6498. "Fold all blocks in the current buffer."
  6499. (interactive)
  6500. (org-show-block-all)
  6501. (org-block-map 'org-hide-block-toggle-maybe))
  6502. (defun org-show-block-all ()
  6503. "Unfold all blocks in the current buffer."
  6504. (interactive)
  6505. (mapc 'delete-overlay org-hide-block-overlays)
  6506. (setq org-hide-block-overlays nil))
  6507. (defun org-hide-block-toggle-maybe ()
  6508. "Toggle visibility of block at point.
  6509. Unlike to `org-hide-block-toggle', this function does not throw
  6510. an error. Return a non-nil value when toggling is successful."
  6511. (interactive)
  6512. (ignore-errors (org-hide-block-toggle)))
  6513. (defun org-hide-block-toggle (&optional force)
  6514. "Toggle the visibility of the current block.
  6515. When optional argument FORCE is `off', make block visible. If it
  6516. is non-nil, hide it unconditionally. Throw an error when not at
  6517. a block. Return a non-nil value when toggling is successful."
  6518. (interactive)
  6519. (let ((element (org-element-at-point)))
  6520. (unless (memq (org-element-type element)
  6521. '(center-block comment-block dynamic-block example-block
  6522. export-block quote-block special-block
  6523. src-block verse-block))
  6524. (user-error "Not at a block"))
  6525. (let* ((start (save-excursion
  6526. (goto-char (org-element-property :post-affiliated element))
  6527. (line-end-position)))
  6528. (end (save-excursion
  6529. (goto-char (org-element-property :end element))
  6530. (skip-chars-backward " \r\t\n")
  6531. (line-end-position)))
  6532. (overlays (overlays-at start)))
  6533. (cond
  6534. ;; Do nothing when not before or at the block opening line or
  6535. ;; at the block closing line.
  6536. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6537. ((and (not (eq force 'off))
  6538. (not (memq t (mapcar
  6539. (lambda (o)
  6540. (eq (overlay-get o 'invisible) 'org-hide-block))
  6541. overlays))))
  6542. (let ((ov (make-overlay start end)))
  6543. (overlay-put ov 'invisible 'org-hide-block)
  6544. ;; Make the block accessible to `isearch'.
  6545. (overlay-put
  6546. ov 'isearch-open-invisible
  6547. (lambda (ov)
  6548. (when (memq ov org-hide-block-overlays)
  6549. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6550. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6551. (delete-overlay ov))))
  6552. (push ov org-hide-block-overlays)
  6553. ;; When the block is hidden away, make sure point is left in
  6554. ;; a visible part of the buffer.
  6555. (when (> (line-beginning-position) start)
  6556. (goto-char start)
  6557. (beginning-of-line))
  6558. ;; Signal successful toggling.
  6559. t))
  6560. ((or (not force) (eq force 'off))
  6561. (dolist (ov overlays t)
  6562. (when (memq ov org-hide-block-overlays)
  6563. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6564. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6565. (delete-overlay ov))))))))
  6566. ;; org-tab-after-check-for-cycling-hook
  6567. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6568. ;; Remove overlays when changing major mode
  6569. (add-hook 'org-mode-hook
  6570. (lambda () (org-add-hook 'change-major-mode-hook
  6571. 'org-show-block-all 'append 'local)))
  6572. ;;; Org-goto
  6573. (defvar org-goto-window-configuration nil)
  6574. (defvar org-goto-marker nil)
  6575. (defvar org-goto-map)
  6576. (defun org-goto-map ()
  6577. "Set the keymap `org-goto'."
  6578. (setq org-goto-map
  6579. (let ((map (make-sparse-keymap)))
  6580. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6581. mouse-drag-region universal-argument org-occur))
  6582. cmd)
  6583. (while (setq cmd (pop cmds))
  6584. (substitute-key-definition cmd cmd map global-map)))
  6585. (suppress-keymap map)
  6586. (org-defkey map "\C-m" 'org-goto-ret)
  6587. (org-defkey map [(return)] 'org-goto-ret)
  6588. (org-defkey map [(left)] 'org-goto-left)
  6589. (org-defkey map [(right)] 'org-goto-right)
  6590. (org-defkey map [(control ?g)] 'org-goto-quit)
  6591. (org-defkey map "\C-i" 'org-cycle)
  6592. (org-defkey map [(tab)] 'org-cycle)
  6593. (org-defkey map [(down)] 'outline-next-visible-heading)
  6594. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6595. (if org-goto-auto-isearch
  6596. (if (fboundp 'define-key-after)
  6597. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6598. nil)
  6599. (org-defkey map "q" 'org-goto-quit)
  6600. (org-defkey map "n" 'outline-next-visible-heading)
  6601. (org-defkey map "p" 'outline-previous-visible-heading)
  6602. (org-defkey map "f" 'outline-forward-same-level)
  6603. (org-defkey map "b" 'outline-backward-same-level)
  6604. (org-defkey map "u" 'outline-up-heading))
  6605. (org-defkey map "/" 'org-occur)
  6606. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6607. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6608. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6609. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6610. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6611. map)))
  6612. (defconst org-goto-help
  6613. "Browse buffer copy, to find location or copy text.%s
  6614. RET=jump to location C-g=quit and return to previous location
  6615. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6616. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6617. (defun org-goto (&optional alternative-interface)
  6618. "Look up a different location in the current file, keeping current visibility.
  6619. When you want look-up or go to a different location in a
  6620. document, the fastest way is often to fold the entire buffer and
  6621. then dive into the tree. This method has the disadvantage, that
  6622. the previous location will be folded, which may not be what you
  6623. want.
  6624. This command works around this by showing a copy of the current
  6625. buffer in an indirect buffer, in overview mode. You can dive
  6626. into the tree in that copy, use org-occur and incremental search
  6627. to find a location. When pressing RET or `Q', the command
  6628. returns to the original buffer in which the visibility is still
  6629. unchanged. After RET it will also jump to the location selected
  6630. in the indirect buffer and expose the headline hierarchy above.
  6631. With a prefix argument, use the alternative interface: e.g. if
  6632. `org-goto-interface' is 'outline use 'outline-path-completion."
  6633. (interactive "P")
  6634. (org-goto-map)
  6635. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6636. (org-refile-use-outline-path t)
  6637. (org-refile-target-verify-function nil)
  6638. (interface
  6639. (if (not alternative-interface)
  6640. org-goto-interface
  6641. (if (eq org-goto-interface 'outline)
  6642. 'outline-path-completion
  6643. 'outline)))
  6644. (org-goto-start-pos (point))
  6645. (selected-point
  6646. (if (eq interface 'outline)
  6647. (car (org-get-location (current-buffer) org-goto-help))
  6648. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6649. (org-refile-check-position pa)
  6650. (nth 3 pa)))))
  6651. (if selected-point
  6652. (progn
  6653. (org-mark-ring-push org-goto-start-pos)
  6654. (goto-char selected-point)
  6655. (if (or (outline-invisible-p) (org-invisible-p2))
  6656. (org-show-context 'org-goto)))
  6657. (message "Quit"))))
  6658. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6659. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6660. (defvar org-goto-local-auto-isearch-map) ; defined below
  6661. (defun org-get-location (buf help)
  6662. "Let the user select a location in the Org-mode buffer BUF.
  6663. This function uses a recursive edit. It returns the selected position
  6664. or nil."
  6665. (org-no-popups
  6666. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6667. (isearch-hide-immediately nil)
  6668. (isearch-search-fun-function
  6669. (lambda () 'org-goto-local-search-headings))
  6670. (org-goto-selected-point org-goto-exit-command))
  6671. (save-excursion
  6672. (save-window-excursion
  6673. (delete-other-windows)
  6674. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6675. (org-pop-to-buffer-same-window
  6676. (condition-case nil
  6677. (make-indirect-buffer (current-buffer) "*org-goto*")
  6678. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6679. (with-output-to-temp-buffer "*Org Help*"
  6680. (princ (format help (if org-goto-auto-isearch
  6681. " Just type for auto-isearch."
  6682. " n/p/f/b/u to navigate, q to quit."))))
  6683. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6684. (setq buffer-read-only nil)
  6685. (let ((org-startup-truncated t)
  6686. (org-startup-folded nil)
  6687. (org-startup-align-all-tables nil))
  6688. (org-mode)
  6689. (org-overview))
  6690. (setq buffer-read-only t)
  6691. (if (and (boundp 'org-goto-start-pos)
  6692. (integer-or-marker-p org-goto-start-pos))
  6693. (let ((org-show-hierarchy-above t)
  6694. (org-show-siblings t)
  6695. (org-show-following-heading t))
  6696. (goto-char org-goto-start-pos)
  6697. (and (outline-invisible-p) (org-show-context)))
  6698. (goto-char (point-min)))
  6699. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6700. (message "Select location and press RET")
  6701. (use-local-map org-goto-map)
  6702. (recursive-edit)))
  6703. (kill-buffer "*org-goto*")
  6704. (cons org-goto-selected-point org-goto-exit-command))))
  6705. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6706. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6707. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6708. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6709. (defun org-goto-local-search-headings (string bound noerror)
  6710. "Search and make sure that any matches are in headlines."
  6711. (catch 'return
  6712. (while (if isearch-forward
  6713. (search-forward string bound noerror)
  6714. (search-backward string bound noerror))
  6715. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6716. (and (member :headline context)
  6717. (not (member :tags context))))
  6718. (throw 'return (point))))))
  6719. (defun org-goto-local-auto-isearch ()
  6720. "Start isearch."
  6721. (interactive)
  6722. (goto-char (point-min))
  6723. (let ((keys (this-command-keys)))
  6724. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6725. (isearch-mode t)
  6726. (isearch-process-search-char (string-to-char keys)))))
  6727. (defun org-goto-ret (&optional arg)
  6728. "Finish `org-goto' by going to the new location."
  6729. (interactive "P")
  6730. (setq org-goto-selected-point (point)
  6731. org-goto-exit-command 'return)
  6732. (throw 'exit nil))
  6733. (defun org-goto-left ()
  6734. "Finish `org-goto' by going to the new location."
  6735. (interactive)
  6736. (if (org-at-heading-p)
  6737. (progn
  6738. (beginning-of-line 1)
  6739. (setq org-goto-selected-point (point)
  6740. org-goto-exit-command 'left)
  6741. (throw 'exit nil))
  6742. (user-error "Not on a heading")))
  6743. (defun org-goto-right ()
  6744. "Finish `org-goto' by going to the new location."
  6745. (interactive)
  6746. (if (org-at-heading-p)
  6747. (progn
  6748. (setq org-goto-selected-point (point)
  6749. org-goto-exit-command 'right)
  6750. (throw 'exit nil))
  6751. (user-error "Not on a heading")))
  6752. (defun org-goto-quit ()
  6753. "Finish `org-goto' without cursor motion."
  6754. (interactive)
  6755. (setq org-goto-selected-point nil)
  6756. (setq org-goto-exit-command 'quit)
  6757. (throw 'exit nil))
  6758. ;;; Indirect buffer display of subtrees
  6759. (defvar org-indirect-dedicated-frame nil
  6760. "This is the frame being used for indirect tree display.")
  6761. (defvar org-last-indirect-buffer nil)
  6762. (defun org-tree-to-indirect-buffer (&optional arg)
  6763. "Create indirect buffer and narrow it to current subtree.
  6764. With a numerical prefix ARG, go up to this level and then take that tree.
  6765. If ARG is negative, go up that many levels.
  6766. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6767. indirect buffer previously made with this command, to avoid proliferation of
  6768. indirect buffers. However, when you call the command with a \
  6769. \\[universal-argument] prefix, or
  6770. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6771. is kept so that you can work with several indirect buffers at the same time.
  6772. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6773. \\[universal-argument] prefix also
  6774. requests that a new frame be made for the new buffer, so that the dedicated
  6775. frame is not changed."
  6776. (interactive "P")
  6777. (let ((cbuf (current-buffer))
  6778. (cwin (selected-window))
  6779. (pos (point))
  6780. beg end level heading ibuf)
  6781. (save-excursion
  6782. (org-back-to-heading t)
  6783. (when (numberp arg)
  6784. (setq level (org-outline-level))
  6785. (if (< arg 0) (setq arg (+ level arg)))
  6786. (while (> (setq level (org-outline-level)) arg)
  6787. (org-up-heading-safe)))
  6788. (setq beg (point)
  6789. heading (org-get-heading))
  6790. (org-end-of-subtree t t)
  6791. (if (org-at-heading-p) (backward-char 1))
  6792. (setq end (point)))
  6793. (if (and (buffer-live-p org-last-indirect-buffer)
  6794. (not (eq org-indirect-buffer-display 'new-frame))
  6795. (not arg))
  6796. (kill-buffer org-last-indirect-buffer))
  6797. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6798. org-last-indirect-buffer ibuf)
  6799. (cond
  6800. ((or (eq org-indirect-buffer-display 'new-frame)
  6801. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6802. (select-frame (make-frame))
  6803. (delete-other-windows)
  6804. (org-pop-to-buffer-same-window ibuf)
  6805. (org-set-frame-title heading))
  6806. ((eq org-indirect-buffer-display 'dedicated-frame)
  6807. (raise-frame
  6808. (select-frame (or (and org-indirect-dedicated-frame
  6809. (frame-live-p org-indirect-dedicated-frame)
  6810. org-indirect-dedicated-frame)
  6811. (setq org-indirect-dedicated-frame (make-frame)))))
  6812. (delete-other-windows)
  6813. (org-pop-to-buffer-same-window ibuf)
  6814. (org-set-frame-title (concat "Indirect: " heading)))
  6815. ((eq org-indirect-buffer-display 'current-window)
  6816. (org-pop-to-buffer-same-window ibuf))
  6817. ((eq org-indirect-buffer-display 'other-window)
  6818. (pop-to-buffer ibuf))
  6819. (t (error "Invalid value")))
  6820. (if (featurep 'xemacs)
  6821. (save-excursion (org-mode) (turn-on-font-lock)))
  6822. (narrow-to-region beg end)
  6823. (show-all)
  6824. (goto-char pos)
  6825. (run-hook-with-args 'org-cycle-hook 'all)
  6826. (and (window-live-p cwin) (select-window cwin))))
  6827. (defun org-get-indirect-buffer (&optional buffer heading)
  6828. (setq buffer (or buffer (current-buffer)))
  6829. (let ((n 1) (base (buffer-name buffer)) bname)
  6830. (while (buffer-live-p
  6831. (get-buffer
  6832. (setq bname
  6833. (concat base "-"
  6834. (if heading (concat heading "-" (number-to-string n))
  6835. (number-to-string n))))))
  6836. (setq n (1+ n)))
  6837. (condition-case nil
  6838. (make-indirect-buffer buffer bname 'clone)
  6839. (error (make-indirect-buffer buffer bname)))))
  6840. (defun org-set-frame-title (title)
  6841. "Set the title of the current frame to the string TITLE."
  6842. ;; FIXME: how to name a single frame in XEmacs???
  6843. (unless (featurep 'xemacs)
  6844. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6845. ;;;; Structure editing
  6846. ;;; Inserting headlines
  6847. (defun org-previous-line-empty-p (&optional next)
  6848. "Is the previous line a blank line?
  6849. When NEXT is non-nil, check the next line instead."
  6850. (save-excursion
  6851. (and (not (bobp))
  6852. (or (beginning-of-line (if next 2 0)) t)
  6853. (save-match-data
  6854. (looking-at "[ \t]*$")))))
  6855. (defun org-insert-heading (&optional arg invisible-ok)
  6856. "Insert a new heading or an item with the same depth at point.
  6857. If point is at the beginning of a heading or a list item, insert
  6858. a new heading or a new item above the current one. If point is
  6859. at the beginning of a normal line, turn the line into a heading.
  6860. If point is in the middle of a headline or a list item, split the
  6861. headline or the item and create a new headline/item with the text
  6862. in the current line after point \(see `org-M-RET-may-split-line'
  6863. on how to modify this behavior).
  6864. With one universal prefix argument, set the user option
  6865. `org-insert-heading-respect-content' to t for the duration of
  6866. the command. This modifies the behavior described above in this
  6867. ways: on list items and at the beginning of normal lines, force
  6868. the insertion of a heading after the current subtree.
  6869. With two universal prefix arguments, insert the heading at the
  6870. end of the grandparent subtree. For example, if point is within
  6871. a 2nd-level heading, then it will insert a 2nd-level heading at
  6872. the end of the 1st-level parent heading.
  6873. If point is at the beginning of a headline, insert a sibling
  6874. before the current headline. If point is not at the beginning,
  6875. split the line and create a new headline with the text in the
  6876. current line after point \(see `org-M-RET-may-split-line' on how
  6877. to modify this behavior).
  6878. If point is at the beginning of a normal line, turn this line
  6879. into a heading.
  6880. When INVISIBLE-OK is set, stop at invisible headlines when going
  6881. back. This is important for non-interactive uses of the
  6882. command."
  6883. (interactive "P")
  6884. (if (org-called-interactively-p 'any) (org-reveal))
  6885. (let ((itemp (org-in-item-p))
  6886. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6887. (respect-content (or org-insert-heading-respect-content
  6888. (equal arg '(4))))
  6889. (initial-content "")
  6890. (adjust-empty-lines t))
  6891. (cond
  6892. ((or (= (buffer-size) 0)
  6893. (and (not (save-excursion
  6894. (and (ignore-errors (org-back-to-heading invisible-ok))
  6895. (org-at-heading-p))))
  6896. (or arg (not itemp))))
  6897. ;; At beginning of buffer or so high up that only a heading
  6898. ;; makes sense.
  6899. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6900. ((not respect-content)
  6901. (unless may-split (end-of-line))
  6902. (insert "\n* "))
  6903. ((re-search-forward org-outline-regexp-bol nil t)
  6904. (beginning-of-line)
  6905. (insert "* \n")
  6906. (backward-char))
  6907. (t (goto-char (point-max))
  6908. (insert "\n* ")))
  6909. (run-hooks 'org-insert-heading-hook))
  6910. ((and itemp (not (member arg '((4) (16)))))
  6911. ;; Insert an item
  6912. (org-insert-item))
  6913. (t
  6914. ;; Maybe move at the end of the subtree
  6915. (when (equal arg '(16))
  6916. (org-up-heading-safe)
  6917. (org-end-of-subtree t))
  6918. ;; Insert a heading
  6919. (save-restriction
  6920. (widen)
  6921. (let* ((level nil)
  6922. (on-heading (org-at-heading-p))
  6923. (empty-line-p (if on-heading
  6924. (org-previous-line-empty-p)
  6925. ;; We will decide later
  6926. nil))
  6927. ;; Get a level string to fall back on
  6928. (fix-level
  6929. (if (org-before-first-heading-p) "*"
  6930. (save-excursion
  6931. (org-back-to-heading t)
  6932. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6933. (looking-at org-outline-regexp)
  6934. (make-string (1- (length (match-string 0))) ?*))))
  6935. (stars
  6936. (save-excursion
  6937. (condition-case nil
  6938. (progn
  6939. (org-back-to-heading invisible-ok)
  6940. (when (and (not on-heading)
  6941. (featurep 'org-inlinetask)
  6942. (integerp org-inlinetask-min-level)
  6943. (>= (length (match-string 0))
  6944. org-inlinetask-min-level))
  6945. ;; Find a heading level before the inline task
  6946. (while (and (setq level (org-up-heading-safe))
  6947. (>= level org-inlinetask-min-level)))
  6948. (if (org-at-heading-p)
  6949. (org-back-to-heading invisible-ok)
  6950. (error "This should not happen")))
  6951. (unless (and (save-excursion
  6952. (save-match-data
  6953. (org-backward-heading-same-level
  6954. 1 invisible-ok))
  6955. (= (point) (match-beginning 0)))
  6956. (not (org-previous-line-empty-p t)))
  6957. (setq empty-line-p (or empty-line-p
  6958. (org-previous-line-empty-p))))
  6959. (match-string 0))
  6960. (error (or fix-level "* ")))))
  6961. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6962. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6963. pos hide-previous previous-pos)
  6964. ;; If we insert after content, move there and clean up whitespace
  6965. (when (and respect-content
  6966. (not (org-looking-at-p org-outline-regexp-bol)))
  6967. (if (not (org-before-first-heading-p))
  6968. (org-end-of-subtree nil t)
  6969. (re-search-forward org-outline-regexp-bol)
  6970. (beginning-of-line 0))
  6971. (skip-chars-backward " \r\n")
  6972. (and (not (looking-back "^\\*+"))
  6973. (looking-at "[ \t]+") (replace-match ""))
  6974. (unless (eobp) (forward-char 1))
  6975. (when (looking-at "^\\*")
  6976. (unless (bobp) (backward-char 1))
  6977. (insert "\n")))
  6978. ;; If we are splitting, grab the text that should be moved to the new headline
  6979. (when may-split
  6980. (if (org-on-heading-p)
  6981. ;; This is a heading, we split intelligently (keeping tags)
  6982. (let ((pos (point)))
  6983. (goto-char (point-at-bol))
  6984. (unless (looking-at org-complex-heading-regexp)
  6985. (error "This should not happen"))
  6986. (when (and (match-beginning 4)
  6987. (> pos (match-beginning 4))
  6988. (< pos (match-end 4)))
  6989. (setq initial-content (buffer-substring pos (match-end 4)))
  6990. (goto-char pos)
  6991. (delete-region (point) (match-end 4))
  6992. (if (looking-at "[ \t]*$")
  6993. (replace-match "")
  6994. (insert (make-string (length initial-content) ?\ )))
  6995. (setq initial-content (org-trim initial-content)))
  6996. (goto-char pos))
  6997. ;; a normal line
  6998. (setq initial-content
  6999. (org-trim (buffer-substring (point) (point-at-eol))))
  7000. (delete-region (point) (point-at-eol))))
  7001. ;; If we are at the beginning of the line, insert before it. Else after
  7002. (cond
  7003. ((and (bolp) (looking-at "[ \t]*$")))
  7004. ((and (bolp) (not (looking-at "[ \t]*$")))
  7005. (open-line 1))
  7006. (t
  7007. (goto-char (point-at-eol))
  7008. (insert "\n")))
  7009. ;; Insert the new heading
  7010. (insert stars)
  7011. (just-one-space)
  7012. (insert initial-content)
  7013. (when adjust-empty-lines
  7014. (if (or (not blank)
  7015. (and blank (not (org-previous-line-empty-p))))
  7016. (org-N-empty-lines-before-current (if blank 1 0))))
  7017. (run-hooks 'org-insert-heading-hook)))))))
  7018. (defun org-N-empty-lines-before-current (N)
  7019. "Make the number of empty lines before current exactly N.
  7020. So this will delete or add empty lines."
  7021. (save-excursion
  7022. (beginning-of-line)
  7023. (let ((p (point)))
  7024. (skip-chars-backward " \r\t\n")
  7025. (unless (bolp) (forward-line))
  7026. (delete-region (point) p))
  7027. (when (> N 0) (insert (make-string N ?\n)))))
  7028. (defun org-get-heading (&optional no-tags no-todo)
  7029. "Return the heading of the current entry, without the stars.
  7030. When NO-TAGS is non-nil, don't include tags.
  7031. When NO-TODO is non-nil, don't include TODO keywords."
  7032. (save-excursion
  7033. (org-back-to-heading t)
  7034. (cond
  7035. ((and no-tags no-todo)
  7036. (looking-at org-complex-heading-regexp)
  7037. (match-string 4))
  7038. (no-tags
  7039. (looking-at (concat org-outline-regexp
  7040. "\\(.*?\\)"
  7041. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7042. (match-string 1))
  7043. (no-todo
  7044. (looking-at org-todo-line-regexp)
  7045. (match-string 3))
  7046. (t (looking-at org-heading-regexp)
  7047. (match-string 2)))))
  7048. (defvar orgstruct-mode) ; defined below
  7049. (defun org-heading-components ()
  7050. "Return the components of the current heading.
  7051. This is a list with the following elements:
  7052. - the level as an integer
  7053. - the reduced level, different if `org-odd-levels-only' is set.
  7054. - the TODO keyword, or nil
  7055. - the priority character, like ?A, or nil if no priority is given
  7056. - the headline text itself, or the tags string if no headline text
  7057. - the tags string, or nil."
  7058. (save-excursion
  7059. (org-back-to-heading t)
  7060. (if (let (case-fold-search)
  7061. (looking-at
  7062. (if orgstruct-mode
  7063. org-heading-regexp
  7064. org-complex-heading-regexp)))
  7065. (if orgstruct-mode
  7066. (list (length (match-string 1))
  7067. (org-reduced-level (length (match-string 1)))
  7068. nil
  7069. nil
  7070. (match-string 2)
  7071. nil)
  7072. (list (length (match-string 1))
  7073. (org-reduced-level (length (match-string 1)))
  7074. (org-match-string-no-properties 2)
  7075. (and (match-end 3) (aref (match-string 3) 2))
  7076. (org-match-string-no-properties 4)
  7077. (org-match-string-no-properties 5))))))
  7078. (defun org-get-entry ()
  7079. "Get the entry text, after heading, entire subtree."
  7080. (save-excursion
  7081. (org-back-to-heading t)
  7082. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7083. (defun org-insert-heading-after-current ()
  7084. "Insert a new heading with same level as current, after current subtree."
  7085. (interactive)
  7086. (org-back-to-heading)
  7087. (org-insert-heading)
  7088. (org-move-subtree-down)
  7089. (end-of-line 1))
  7090. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7091. "Insert heading with `org-insert-heading-respect-content' set to t."
  7092. (interactive)
  7093. (org-insert-heading '(4) invisible-ok))
  7094. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7095. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7096. (interactive)
  7097. (org-insert-todo-heading force-state '(4)))
  7098. (defun org-insert-todo-heading (arg &optional force-heading)
  7099. "Insert a new heading with the same level and TODO state as current heading.
  7100. If the heading has no TODO state, or if the state is DONE, use the first
  7101. state (TODO by default). Also with one prefix arg, force first state. With
  7102. two prefix args, force inserting at the end of the parent subtree."
  7103. (interactive "P")
  7104. (when (or force-heading (not (org-insert-item 'checkbox)))
  7105. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7106. force-heading))
  7107. (save-excursion
  7108. (org-back-to-heading)
  7109. (outline-previous-heading)
  7110. (looking-at org-todo-line-regexp))
  7111. (let*
  7112. ((new-mark-x
  7113. (if (or (equal arg '(4))
  7114. (not (match-beginning 2))
  7115. (member (match-string 2) org-done-keywords))
  7116. (car org-todo-keywords-1)
  7117. (match-string 2)))
  7118. (new-mark
  7119. (or
  7120. (run-hook-with-args-until-success
  7121. 'org-todo-get-default-hook new-mark-x nil)
  7122. new-mark-x)))
  7123. (beginning-of-line 1)
  7124. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7125. (if org-treat-insert-todo-heading-as-state-change
  7126. (org-todo new-mark)
  7127. (insert new-mark " "))))
  7128. (when org-provide-todo-statistics
  7129. (org-update-parent-todo-statistics))))
  7130. (defun org-insert-subheading (arg)
  7131. "Insert a new subheading and demote it.
  7132. Works for outline headings and for plain lists alike."
  7133. (interactive "P")
  7134. (org-insert-heading arg)
  7135. (cond
  7136. ((org-at-heading-p) (org-do-demote))
  7137. ((org-at-item-p) (org-indent-item))))
  7138. (defun org-insert-todo-subheading (arg)
  7139. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7140. Works for outline headings and for plain lists alike."
  7141. (interactive "P")
  7142. (org-insert-todo-heading arg)
  7143. (cond
  7144. ((org-at-heading-p) (org-do-demote))
  7145. ((org-at-item-p) (org-indent-item))))
  7146. ;;; Promotion and Demotion
  7147. (defvar org-after-demote-entry-hook nil
  7148. "Hook run after an entry has been demoted.
  7149. The cursor will be at the beginning of the entry.
  7150. When a subtree is being demoted, the hook will be called for each node.")
  7151. (defvar org-after-promote-entry-hook nil
  7152. "Hook run after an entry has been promoted.
  7153. The cursor will be at the beginning of the entry.
  7154. When a subtree is being promoted, the hook will be called for each node.")
  7155. (defun org-promote-subtree ()
  7156. "Promote the entire subtree.
  7157. See also `org-promote'."
  7158. (interactive)
  7159. (save-excursion
  7160. (org-with-limited-levels (org-map-tree 'org-promote)))
  7161. (org-fix-position-after-promote))
  7162. (defun org-demote-subtree ()
  7163. "Demote the entire subtree. See `org-demote'.
  7164. See also `org-promote'."
  7165. (interactive)
  7166. (save-excursion
  7167. (org-with-limited-levels (org-map-tree 'org-demote)))
  7168. (org-fix-position-after-promote))
  7169. (defun org-do-promote ()
  7170. "Promote the current heading higher up the tree.
  7171. If the region is active in `transient-mark-mode', promote all headings
  7172. in the region."
  7173. (interactive)
  7174. (save-excursion
  7175. (if (org-region-active-p)
  7176. (org-map-region 'org-promote (region-beginning) (region-end))
  7177. (org-promote)))
  7178. (org-fix-position-after-promote))
  7179. (defun org-do-demote ()
  7180. "Demote the current heading lower down the tree.
  7181. If the region is active in `transient-mark-mode', demote all headings
  7182. in the region."
  7183. (interactive)
  7184. (save-excursion
  7185. (if (org-region-active-p)
  7186. (org-map-region 'org-demote (region-beginning) (region-end))
  7187. (org-demote)))
  7188. (org-fix-position-after-promote))
  7189. (defun org-fix-position-after-promote ()
  7190. "Make sure that after pro/demotion cursor position is right."
  7191. (let ((pos (point)))
  7192. (when (save-excursion
  7193. (beginning-of-line 1)
  7194. (looking-at org-todo-line-regexp)
  7195. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7196. (cond ((eobp) (insert " "))
  7197. ((eolp) (insert " "))
  7198. ((equal (char-after) ?\ ) (forward-char 1))))))
  7199. (defun org-current-level ()
  7200. "Return the level of the current entry, or nil if before the first headline.
  7201. The level is the number of stars at the beginning of the headline."
  7202. (save-excursion
  7203. (org-with-limited-levels
  7204. (if (ignore-errors (org-back-to-heading t))
  7205. (funcall outline-level)))))
  7206. (defun org-get-previous-line-level ()
  7207. "Return the outline depth of the last headline before the current line.
  7208. Returns 0 for the first headline in the buffer, and nil if before the
  7209. first headline."
  7210. (and (org-current-level)
  7211. (or (and (/= (line-beginning-position) (point-min))
  7212. (save-excursion (beginning-of-line 0) (org-current-level)))
  7213. 0)))
  7214. (defun org-reduced-level (l)
  7215. "Compute the effective level of a heading.
  7216. This takes into account the setting of `org-odd-levels-only'."
  7217. (cond
  7218. ((zerop l) 0)
  7219. (org-odd-levels-only (1+ (floor (/ l 2))))
  7220. (t l)))
  7221. (defun org-level-increment ()
  7222. "Return the number of stars that will be added or removed at a
  7223. time to headlines when structure editing, based on the value of
  7224. `org-odd-levels-only'."
  7225. (if org-odd-levels-only 2 1))
  7226. (defun org-get-valid-level (level &optional change)
  7227. "Rectify a level change under the influence of `org-odd-levels-only'
  7228. LEVEL is a current level, CHANGE is by how much the level should be
  7229. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7230. even level numbers will become the next higher odd number."
  7231. (if org-odd-levels-only
  7232. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7233. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7234. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7235. (max 1 (+ level (or change 0)))))
  7236. (if (boundp 'define-obsolete-function-alias)
  7237. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7238. (define-obsolete-function-alias 'org-get-legal-level
  7239. 'org-get-valid-level)
  7240. (define-obsolete-function-alias 'org-get-legal-level
  7241. 'org-get-valid-level "23.1")))
  7242. (defun org-promote ()
  7243. "Promote the current heading higher up the tree.
  7244. If the region is active in `transient-mark-mode', promote all headings
  7245. in the region."
  7246. (org-back-to-heading t)
  7247. (let* ((level (save-match-data (funcall outline-level)))
  7248. (after-change-functions (remove 'flyspell-after-change-function
  7249. after-change-functions))
  7250. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7251. (diff (abs (- level (length up-head) -1))))
  7252. (cond ((and (= level 1) org-called-with-limited-levels
  7253. org-allow-promoting-top-level-subtree)
  7254. (replace-match "# " nil t))
  7255. ((= level 1)
  7256. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7257. (t (replace-match up-head nil t)))
  7258. ;; Fixup tag positioning
  7259. (unless (= level 1)
  7260. (and org-auto-align-tags (org-set-tags nil 'ignore-column))
  7261. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7262. (run-hooks 'org-after-promote-entry-hook)))
  7263. (defun org-demote ()
  7264. "Demote the current heading lower down the tree.
  7265. If the region is active in `transient-mark-mode', demote all headings
  7266. in the region."
  7267. (org-back-to-heading t)
  7268. (let* ((level (save-match-data (funcall outline-level)))
  7269. (after-change-functions (remove 'flyspell-after-change-function
  7270. after-change-functions))
  7271. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7272. (diff (abs (- level (length down-head) -1))))
  7273. (replace-match down-head nil t)
  7274. ;; Fixup tag positioning
  7275. (and org-auto-align-tags (org-set-tags nil 'ignore-column))
  7276. (if org-adapt-indentation (org-fixup-indentation diff))
  7277. (run-hooks 'org-after-demote-entry-hook)))
  7278. (defun org-cycle-level ()
  7279. "Cycle the level of an empty headline through possible states.
  7280. This goes first to child, then to parent, level, then up the hierarchy.
  7281. After top level, it switches back to sibling level."
  7282. (interactive)
  7283. (let ((org-adapt-indentation nil))
  7284. (when (org-point-at-end-of-empty-headline)
  7285. (setq this-command 'org-cycle-level) ; Only needed for caching
  7286. (let ((cur-level (org-current-level))
  7287. (prev-level (org-get-previous-line-level)))
  7288. (cond
  7289. ;; If first headline in file, promote to top-level.
  7290. ((= prev-level 0)
  7291. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7292. do (org-do-promote)))
  7293. ;; If same level as prev, demote one.
  7294. ((= prev-level cur-level)
  7295. (org-do-demote))
  7296. ;; If parent is top-level, promote to top level if not already.
  7297. ((= prev-level 1)
  7298. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7299. do (org-do-promote)))
  7300. ;; If top-level, return to prev-level.
  7301. ((= cur-level 1)
  7302. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7303. do (org-do-demote)))
  7304. ;; If less than prev-level, promote one.
  7305. ((< cur-level prev-level)
  7306. (org-do-promote))
  7307. ;; If deeper than prev-level, promote until higher than
  7308. ;; prev-level.
  7309. ((> cur-level prev-level)
  7310. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7311. do (org-do-promote))))
  7312. t))))
  7313. (defun org-map-tree (fun)
  7314. "Call FUN for every heading underneath the current one."
  7315. (org-back-to-heading)
  7316. (let ((level (funcall outline-level)))
  7317. (save-excursion
  7318. (funcall fun)
  7319. (while (and (progn
  7320. (outline-next-heading)
  7321. (> (funcall outline-level) level))
  7322. (not (eobp)))
  7323. (funcall fun)))))
  7324. (defun org-map-region (fun beg end)
  7325. "Call FUN for every heading between BEG and END."
  7326. (let ((org-ignore-region t))
  7327. (save-excursion
  7328. (setq end (copy-marker end))
  7329. (goto-char beg)
  7330. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7331. (< (point) end))
  7332. (funcall fun))
  7333. (while (and (progn
  7334. (outline-next-heading)
  7335. (< (point) end))
  7336. (not (eobp)))
  7337. (funcall fun)))))
  7338. (defvar org-property-end-re) ; silence byte-compiler
  7339. (defun org-fixup-indentation (diff)
  7340. "Change the indentation in the current entry by DIFF.
  7341. However, if any line in the current entry has no indentation, or if it
  7342. would end up with no indentation after the change, nothing at all is done."
  7343. (save-excursion
  7344. (let ((end (save-excursion (outline-next-heading)
  7345. (point-marker)))
  7346. (prohibit (if (> diff 0)
  7347. "^\\S-"
  7348. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7349. col)
  7350. (unless (save-excursion (end-of-line 1)
  7351. (re-search-forward prohibit end t))
  7352. (while (and (< (point) end)
  7353. (re-search-forward "^[ \t]+" end t))
  7354. (goto-char (match-end 0))
  7355. (setq col (current-column))
  7356. (if (< diff 0) (replace-match ""))
  7357. (org-indent-to-column (+ diff col))))
  7358. (move-marker end nil))))
  7359. (defun org-convert-to-odd-levels ()
  7360. "Convert an org-mode file with all levels allowed to one with odd levels.
  7361. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7362. level 5 etc."
  7363. (interactive)
  7364. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7365. (let ((outline-level 'org-outline-level)
  7366. (org-odd-levels-only nil) n)
  7367. (save-excursion
  7368. (goto-char (point-min))
  7369. (while (re-search-forward "^\\*\\*+ " nil t)
  7370. (setq n (- (length (match-string 0)) 2))
  7371. (while (>= (setq n (1- n)) 0)
  7372. (org-demote))
  7373. (end-of-line 1))))))
  7374. (defun org-convert-to-oddeven-levels ()
  7375. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7376. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7377. file contains a section with an even level, conversion would
  7378. destroy the structure of the file. An error is signaled in this
  7379. case."
  7380. (interactive)
  7381. (goto-char (point-min))
  7382. ;; First check if there are no even levels
  7383. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7384. (org-show-context t)
  7385. (error "Not all levels are odd in this file. Conversion not possible"))
  7386. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7387. (let ((outline-regexp org-outline-regexp)
  7388. (outline-level 'org-outline-level)
  7389. (org-odd-levels-only nil) n)
  7390. (save-excursion
  7391. (goto-char (point-min))
  7392. (while (re-search-forward "^\\*\\*+ " nil t)
  7393. (setq n (/ (1- (length (match-string 0))) 2))
  7394. (while (>= (setq n (1- n)) 0)
  7395. (org-promote))
  7396. (end-of-line 1))))))
  7397. (defun org-tr-level (n)
  7398. "Make N odd if required."
  7399. (if org-odd-levels-only (1+ (/ n 2)) n))
  7400. ;;; Vertical tree motion, cutting and pasting of subtrees
  7401. (defun org-move-subtree-up (&optional arg)
  7402. "Move the current subtree up past ARG headlines of the same level."
  7403. (interactive "p")
  7404. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7405. (defun org-move-subtree-down (&optional arg)
  7406. "Move the current subtree down past ARG headlines of the same level."
  7407. (interactive "p")
  7408. (setq arg (prefix-numeric-value arg))
  7409. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7410. 'org-get-last-sibling))
  7411. (ins-point (make-marker))
  7412. (cnt (abs arg))
  7413. (col (current-column))
  7414. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7415. ;; Select the tree
  7416. (org-back-to-heading)
  7417. (setq beg0 (point))
  7418. (save-excursion
  7419. (setq ne-beg (org-back-over-empty-lines))
  7420. (setq beg (point)))
  7421. (save-match-data
  7422. (save-excursion (outline-end-of-heading)
  7423. (setq folded (outline-invisible-p)))
  7424. (progn (org-end-of-subtree nil t)
  7425. (unless (eobp) (backward-char))))
  7426. (outline-next-heading)
  7427. (setq ne-end (org-back-over-empty-lines))
  7428. (setq end (point))
  7429. (goto-char beg0)
  7430. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7431. ;; include less whitespace
  7432. (save-excursion
  7433. (goto-char beg)
  7434. (forward-line (- ne-beg ne-end))
  7435. (setq beg (point))))
  7436. ;; Find insertion point, with error handling
  7437. (while (> cnt 0)
  7438. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7439. (progn (goto-char beg0)
  7440. (user-error "Cannot move past superior level or buffer limit")))
  7441. (setq cnt (1- cnt)))
  7442. (if (> arg 0)
  7443. ;; Moving forward - still need to move over subtree
  7444. (progn (org-end-of-subtree t t)
  7445. (save-excursion
  7446. (org-back-over-empty-lines)
  7447. (or (bolp) (newline)))))
  7448. (setq ne-ins (org-back-over-empty-lines))
  7449. (move-marker ins-point (point))
  7450. (setq txt (buffer-substring beg end))
  7451. (org-save-markers-in-region beg end)
  7452. (delete-region beg end)
  7453. (org-remove-empty-overlays-at beg)
  7454. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7455. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7456. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7457. (let ((bbb (point)))
  7458. (insert-before-markers txt)
  7459. (org-reinstall-markers-in-region bbb)
  7460. (move-marker ins-point bbb))
  7461. (or (bolp) (insert "\n"))
  7462. (setq ins-end (point))
  7463. (goto-char ins-point)
  7464. (org-skip-whitespace)
  7465. (when (and (< arg 0)
  7466. (org-first-sibling-p)
  7467. (> ne-ins ne-beg))
  7468. ;; Move whitespace back to beginning
  7469. (save-excursion
  7470. (goto-char ins-end)
  7471. (let ((kill-whole-line t))
  7472. (kill-line (- ne-ins ne-beg)) (point)))
  7473. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7474. (move-marker ins-point nil)
  7475. (if folded
  7476. (hide-subtree)
  7477. (org-show-entry)
  7478. (show-children)
  7479. (org-cycle-hide-drawers 'children))
  7480. (org-clean-visibility-after-subtree-move)
  7481. ;; move back to the initial column we were at
  7482. (move-to-column col)))
  7483. (defvar org-subtree-clip ""
  7484. "Clipboard for cut and paste of subtrees.
  7485. This is actually only a copy of the kill, because we use the normal kill
  7486. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7487. (defvar org-subtree-clip-folded nil
  7488. "Was the last copied subtree folded?
  7489. This is used to fold the tree back after pasting.")
  7490. (defun org-cut-subtree (&optional n)
  7491. "Cut the current subtree into the clipboard.
  7492. With prefix arg N, cut this many sequential subtrees.
  7493. This is a short-hand for marking the subtree and then cutting it."
  7494. (interactive "p")
  7495. (org-copy-subtree n 'cut))
  7496. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7497. "Copy the current subtree it in the clipboard.
  7498. With prefix arg N, copy this many sequential subtrees.
  7499. This is a short-hand for marking the subtree and then copying it.
  7500. If CUT is non-nil, actually cut the subtree.
  7501. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7502. of some markers in the region, even if CUT is non-nil. This is
  7503. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7504. (interactive "p")
  7505. (let (beg end folded (beg0 (point)))
  7506. (if (org-called-interactively-p 'any)
  7507. (org-back-to-heading nil) ; take what looks like a subtree
  7508. (org-back-to-heading t)) ; take what is really there
  7509. (setq beg (point))
  7510. (skip-chars-forward " \t\r\n")
  7511. (save-match-data
  7512. (if nosubtrees
  7513. (outline-next-heading)
  7514. (save-excursion (outline-end-of-heading)
  7515. (setq folded (outline-invisible-p)))
  7516. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7517. (org-end-of-subtree t t)))
  7518. (setq end (point))
  7519. (goto-char beg0)
  7520. (when (> end beg)
  7521. (setq org-subtree-clip-folded folded)
  7522. (when (or cut force-store-markers)
  7523. (org-save-markers-in-region beg end))
  7524. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7525. (setq org-subtree-clip (current-kill 0))
  7526. (message "%s: Subtree(s) with %d characters"
  7527. (if cut "Cut" "Copied")
  7528. (length org-subtree-clip)))))
  7529. (defun org-paste-subtree (&optional level tree for-yank remove)
  7530. "Paste the clipboard as a subtree, with modification of headline level.
  7531. The entire subtree is promoted or demoted in order to match a new headline
  7532. level.
  7533. If the cursor is at the beginning of a headline, the same level as
  7534. that headline is used to paste the tree.
  7535. If not, the new level is derived from the *visible* headings
  7536. before and after the insertion point, and taken to be the inferior headline
  7537. level of the two. So if the previous visible heading is level 3 and the
  7538. next is level 4 (or vice versa), level 4 will be used for insertion.
  7539. This makes sure that the subtree remains an independent subtree and does
  7540. not swallow low level entries.
  7541. You can also force a different level, either by using a numeric prefix
  7542. argument, or by inserting the heading marker by hand. For example, if the
  7543. cursor is after \"*****\", then the tree will be shifted to level 5.
  7544. If optional TREE is given, use this text instead of the kill ring.
  7545. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7546. move back over whitespace before inserting, and move point to the end of
  7547. the inserted text when done.
  7548. When REMOVE is non-nil, remove the subtree from the clipboard."
  7549. (interactive "P")
  7550. (setq tree (or tree (and kill-ring (current-kill 0))))
  7551. (unless (org-kill-is-subtree-p tree)
  7552. (user-error "%s"
  7553. (substitute-command-keys
  7554. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7555. (org-with-limited-levels
  7556. (let* ((visp (not (outline-invisible-p)))
  7557. (txt tree)
  7558. (^re_ "\\(\\*+\\)[ \t]*")
  7559. (old-level (if (string-match org-outline-regexp-bol txt)
  7560. (- (match-end 0) (match-beginning 0) 1)
  7561. -1))
  7562. (force-level (cond (level (prefix-numeric-value level))
  7563. ((and (looking-at "[ \t]*$")
  7564. (string-match
  7565. "^\\*+$" (buffer-substring
  7566. (point-at-bol) (point))))
  7567. (- (match-end 0) (match-beginning 0)))
  7568. ((and (bolp)
  7569. (looking-at org-outline-regexp))
  7570. (- (match-end 0) (point) 1))))
  7571. (previous-level (save-excursion
  7572. (condition-case nil
  7573. (progn
  7574. (outline-previous-visible-heading 1)
  7575. (if (looking-at ^re_)
  7576. (- (match-end 0) (match-beginning 0) 1)
  7577. 1))
  7578. (error 1))))
  7579. (next-level (save-excursion
  7580. (condition-case nil
  7581. (progn
  7582. (or (looking-at org-outline-regexp)
  7583. (outline-next-visible-heading 1))
  7584. (if (looking-at ^re_)
  7585. (- (match-end 0) (match-beginning 0) 1)
  7586. 1))
  7587. (error 1))))
  7588. (new-level (or force-level (max previous-level next-level)))
  7589. (shift (if (or (= old-level -1)
  7590. (= new-level -1)
  7591. (= old-level new-level))
  7592. 0
  7593. (- new-level old-level)))
  7594. (delta (if (> shift 0) -1 1))
  7595. (func (if (> shift 0) 'org-demote 'org-promote))
  7596. (org-odd-levels-only nil)
  7597. beg end newend)
  7598. ;; Remove the forced level indicator
  7599. (if force-level
  7600. (delete-region (point-at-bol) (point)))
  7601. ;; Paste
  7602. (beginning-of-line (if (bolp) 1 2))
  7603. (setq beg (point))
  7604. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7605. (insert-before-markers txt)
  7606. (unless (string-match "\n\\'" txt) (insert "\n"))
  7607. (setq newend (point))
  7608. (org-reinstall-markers-in-region beg)
  7609. (setq end (point))
  7610. (goto-char beg)
  7611. (skip-chars-forward " \t\n\r")
  7612. (setq beg (point))
  7613. (if (and (outline-invisible-p) visp)
  7614. (save-excursion (outline-show-heading)))
  7615. ;; Shift if necessary
  7616. (unless (= shift 0)
  7617. (save-restriction
  7618. (narrow-to-region beg end)
  7619. (while (not (= shift 0))
  7620. (org-map-region func (point-min) (point-max))
  7621. (setq shift (+ delta shift)))
  7622. (goto-char (point-min))
  7623. (setq newend (point-max))))
  7624. (when (or (org-called-interactively-p 'interactive) for-yank)
  7625. (message "Clipboard pasted as level %d subtree" new-level))
  7626. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7627. kill-ring
  7628. (eq org-subtree-clip (current-kill 0))
  7629. org-subtree-clip-folded)
  7630. ;; The tree was folded before it was killed/copied
  7631. (hide-subtree))
  7632. (and for-yank (goto-char newend))
  7633. (and remove (setq kill-ring (cdr kill-ring))))))
  7634. (defun org-kill-is-subtree-p (&optional txt)
  7635. "Check if the current kill is an outline subtree, or a set of trees.
  7636. Returns nil if kill does not start with a headline, or if the first
  7637. headline level is not the largest headline level in the tree.
  7638. So this will actually accept several entries of equal levels as well,
  7639. which is OK for `org-paste-subtree'.
  7640. If optional TXT is given, check this string instead of the current kill."
  7641. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7642. (re (org-get-limited-outline-regexp))
  7643. (^re (concat "^" re))
  7644. (start-level (and kill
  7645. (string-match
  7646. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7647. kill)
  7648. (- (match-end 2) (match-beginning 2) 1)))
  7649. (start (1+ (or (match-beginning 2) -1))))
  7650. (if (not start-level)
  7651. (progn
  7652. nil) ;; does not even start with a heading
  7653. (catch 'exit
  7654. (while (setq start (string-match ^re kill (1+ start)))
  7655. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7656. (throw 'exit nil)))
  7657. t))))
  7658. (defvar org-markers-to-move nil
  7659. "Markers that should be moved with a cut-and-paste operation.
  7660. Those markers are stored together with their positions relative to
  7661. the start of the region.")
  7662. (defun org-save-markers-in-region (beg end)
  7663. "Check markers in region.
  7664. If these markers are between BEG and END, record their position relative
  7665. to BEG, so that after moving the block of text, we can put the markers back
  7666. into place.
  7667. This function gets called just before an entry or tree gets cut from the
  7668. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7669. called immediately, to move the markers with the entries."
  7670. (setq org-markers-to-move nil)
  7671. (when (featurep 'org-clock)
  7672. (org-clock-save-markers-for-cut-and-paste beg end))
  7673. (when (featurep 'org-agenda)
  7674. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7675. (defun org-check-and-save-marker (marker beg end)
  7676. "Check if MARKER is between BEG and END.
  7677. If yes, remember the marker and the distance to BEG."
  7678. (when (and (marker-buffer marker)
  7679. (equal (marker-buffer marker) (current-buffer)))
  7680. (if (and (>= marker beg) (< marker end))
  7681. (push (cons marker (- marker beg)) org-markers-to-move))))
  7682. (defun org-reinstall-markers-in-region (beg)
  7683. "Move all remembered markers to their position relative to BEG."
  7684. (mapc (lambda (x)
  7685. (move-marker (car x) (+ beg (cdr x))))
  7686. org-markers-to-move)
  7687. (setq org-markers-to-move nil))
  7688. (defun org-narrow-to-subtree ()
  7689. "Narrow buffer to the current subtree."
  7690. (interactive)
  7691. (save-excursion
  7692. (save-match-data
  7693. (org-with-limited-levels
  7694. (narrow-to-region
  7695. (progn (org-back-to-heading t) (point))
  7696. (progn (org-end-of-subtree t t)
  7697. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7698. (point)))))))
  7699. (defun org-narrow-to-block ()
  7700. "Narrow buffer to the current block."
  7701. (interactive)
  7702. (let* ((case-fold-search t)
  7703. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7704. "^[ \t]*#\\+end_.*")))
  7705. (if blockp
  7706. (narrow-to-region (car blockp) (cdr blockp))
  7707. (user-error "Not in a block"))))
  7708. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7709. "Clone the task (subtree) at point N times.
  7710. The clones will be inserted as siblings.
  7711. In interactive use, the user will be prompted for the number of
  7712. clones to be produced. If the entry has a timestamp, the user
  7713. will also be prompted for a time shift, which may be a repeater
  7714. as used in time stamps, for example `+3d'. To disable this,
  7715. you can call the function with a universal prefix argument.
  7716. When a valid repeater is given and the entry contains any time
  7717. stamps, the clones will become a sequence in time, with time
  7718. stamps in the subtree shifted for each clone produced. If SHIFT
  7719. is nil or the empty string, time stamps will be left alone. The
  7720. ID property of the original subtree is removed.
  7721. If the original subtree did contain time stamps with a repeater,
  7722. the following will happen:
  7723. - the repeater will be removed in each clone
  7724. - an additional clone will be produced, with the current, unshifted
  7725. date(s) in the entry.
  7726. - the original entry will be placed *after* all the clones, with
  7727. repeater intact.
  7728. - the start days in the repeater in the original entry will be shifted
  7729. to past the last clone.
  7730. In this way you can spell out a number of instances of a repeating task,
  7731. and still retain the repeater to cover future instances of the task."
  7732. (interactive "nNumber of clones to produce: ")
  7733. (let ((shift
  7734. (or shift
  7735. (if (and (not (equal current-prefix-arg '(4)))
  7736. (save-excursion
  7737. (re-search-forward org-ts-regexp-both
  7738. (save-excursion
  7739. (org-end-of-subtree t)
  7740. (point)) t)))
  7741. (read-from-minibuffer
  7742. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7743. ""))) ;; No time shift
  7744. (n-no-remove -1)
  7745. (drawer-re org-drawer-regexp)
  7746. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7747. beg end template task idprop
  7748. shift-n shift-what doshift nmin nmax)
  7749. (if (not (and (integerp n) (> n 0)))
  7750. (user-error "Invalid number of replications %s" n))
  7751. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7752. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7753. shift)))
  7754. (user-error "Invalid shift specification %s" shift))
  7755. (when doshift
  7756. (setq shift-n (string-to-number (match-string 1 shift))
  7757. shift-what (cdr (assoc (match-string 2 shift)
  7758. '(("d" . day) ("w" . week)
  7759. ("m" . month) ("y" . year))))))
  7760. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7761. (setq nmin 1 nmax n)
  7762. (org-back-to-heading t)
  7763. (setq beg (point))
  7764. (setq idprop (org-entry-get nil "ID"))
  7765. (org-end-of-subtree t t)
  7766. (or (bolp) (insert "\n"))
  7767. (setq end (point))
  7768. (setq template (buffer-substring beg end))
  7769. (when (and doshift
  7770. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7771. (delete-region beg end)
  7772. (setq end beg)
  7773. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7774. (goto-char end)
  7775. (loop for n from nmin to nmax do
  7776. ;; prepare clone
  7777. (with-temp-buffer
  7778. (insert template)
  7779. (org-mode)
  7780. (goto-char (point-min))
  7781. (org-show-subtree)
  7782. (and idprop (if org-clone-delete-id
  7783. (org-entry-delete nil "ID")
  7784. (org-id-get-create t)))
  7785. (unless (= n 0)
  7786. (while (re-search-forward org-clock-re nil t)
  7787. (kill-whole-line))
  7788. (goto-char (point-min))
  7789. (while (re-search-forward drawer-re nil t)
  7790. (org-remove-empty-drawer-at (point))))
  7791. (goto-char (point-min))
  7792. (when doshift
  7793. (while (re-search-forward org-ts-regexp-both nil t)
  7794. (org-timestamp-change (* n shift-n) shift-what))
  7795. (unless (= n n-no-remove)
  7796. (goto-char (point-min))
  7797. (while (re-search-forward org-ts-regexp nil t)
  7798. (save-excursion
  7799. (goto-char (match-beginning 0))
  7800. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7801. (delete-region (match-beginning 1) (match-end 1)))))))
  7802. (setq task (buffer-string)))
  7803. (insert task))
  7804. (goto-char beg)))
  7805. ;;; Outline Sorting
  7806. (defun org-sort (with-case)
  7807. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7808. Optional argument WITH-CASE means sort case-sensitively."
  7809. (interactive "P")
  7810. (cond
  7811. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7812. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7813. (t
  7814. (org-call-with-arg 'org-sort-entries with-case))))
  7815. (defun org-sort-remove-invisible (s)
  7816. "Remove invisible links from string S."
  7817. (remove-text-properties 0 (length s) org-rm-props s)
  7818. (while (string-match org-bracket-link-regexp s)
  7819. (setq s (replace-match (if (match-end 2)
  7820. (match-string 3 s)
  7821. (match-string 1 s)) t t s)))
  7822. (let ((st (format " %s " s)))
  7823. (while (string-match org-emph-re st)
  7824. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7825. (setq s (substring st 1 -1)))
  7826. s)
  7827. (defvar org-priority-regexp) ; defined later in the file
  7828. (defvar org-after-sorting-entries-or-items-hook nil
  7829. "Hook that is run after a bunch of entries or items have been sorted.
  7830. When children are sorted, the cursor is in the parent line when this
  7831. hook gets called. When a region or a plain list is sorted, the cursor
  7832. will be in the first entry of the sorted region/list.")
  7833. (defun org-sort-entries
  7834. (&optional with-case sorting-type getkey-func compare-func property)
  7835. "Sort entries on a certain level of an outline tree.
  7836. If there is an active region, the entries in the region are sorted.
  7837. Else, if the cursor is before the first entry, sort the top-level items.
  7838. Else, the children of the entry at point are sorted.
  7839. Sorting can be alphabetically, numerically, by date/time as given by
  7840. a time stamp, by a property, by priority order, or by a custom function.
  7841. The command prompts for the sorting type unless it has been given to the
  7842. function through the SORTING-TYPE argument, which needs to be a character,
  7843. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7844. the precise meaning of each character:
  7845. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7846. c By creation time, which is assumed to be the first inactive time stamp
  7847. at the beginning of a line.
  7848. d By deadline date/time.
  7849. k By clocking time.
  7850. n Numerically, by converting the beginning of the entry/item to a number.
  7851. o By order of TODO keywords.
  7852. p By priority according to the cookie.
  7853. r By the value of a property.
  7854. s By scheduled date/time.
  7855. t By date/time, either the first active time stamp in the entry, or, if
  7856. none exist, by the first inactive one.
  7857. Capital letters will reverse the sort order.
  7858. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7859. called with point at the beginning of the record. It must return either
  7860. a string or a number that should serve as the sorting key for that record.
  7861. Comparing entries ignores case by default. However, with an optional argument
  7862. WITH-CASE, the sorting considers case as well.
  7863. Sorting is done against the visible part of the headlines, it ignores hidden
  7864. links.
  7865. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7866. (interactive "P")
  7867. (let ((case-func (if with-case 'identity 'downcase))
  7868. (cmstr
  7869. ;; The clock marker is lost when using `sort-subr', let's
  7870. ;; store the clocking string.
  7871. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7872. (save-excursion
  7873. (goto-char org-clock-marker)
  7874. (looking-back "^.*") (match-string-no-properties 0))))
  7875. start beg end stars re re2
  7876. txt what tmp)
  7877. ;; Find beginning and end of region to sort
  7878. (cond
  7879. ((org-region-active-p)
  7880. ;; we will sort the region
  7881. (setq end (region-end)
  7882. what "region")
  7883. (goto-char (region-beginning))
  7884. (if (not (org-at-heading-p)) (outline-next-heading))
  7885. (setq start (point)))
  7886. ((or (org-at-heading-p)
  7887. (ignore-errors (progn (org-back-to-heading) t)))
  7888. ;; we will sort the children of the current headline
  7889. (org-back-to-heading)
  7890. (setq start (point)
  7891. end (progn (org-end-of-subtree t t)
  7892. (or (bolp) (insert "\n"))
  7893. (when (>= (org-back-over-empty-lines) 1)
  7894. (forward-line 1))
  7895. (point))
  7896. what "children")
  7897. (goto-char start)
  7898. (show-subtree)
  7899. (outline-next-heading))
  7900. (t
  7901. ;; we will sort the top-level entries in this file
  7902. (goto-char (point-min))
  7903. (or (org-at-heading-p) (outline-next-heading))
  7904. (setq start (point))
  7905. (goto-char (point-max))
  7906. (beginning-of-line 1)
  7907. (when (looking-at ".*?\\S-")
  7908. ;; File ends in a non-white line
  7909. (end-of-line 1)
  7910. (insert "\n"))
  7911. (setq end (point-max))
  7912. (setq what "top-level")
  7913. (goto-char start)
  7914. (show-all)))
  7915. (setq beg (point))
  7916. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7917. (looking-at "\\(\\*+\\)")
  7918. (setq stars (match-string 1)
  7919. re (concat "^" (regexp-quote stars) " +")
  7920. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7921. txt (buffer-substring beg end))
  7922. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7923. (if (and (not (equal stars "*")) (string-match re2 txt))
  7924. (user-error "Region to sort contains a level above the first entry"))
  7925. (unless sorting-type
  7926. (message
  7927. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7928. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  7929. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  7930. what)
  7931. (setq sorting-type (read-char-exclusive))
  7932. (unless getkey-func
  7933. (and (= (downcase sorting-type) ?f)
  7934. (setq getkey-func
  7935. (org-icompleting-read "Sort using function: "
  7936. obarray 'fboundp t nil nil))
  7937. (setq getkey-func (intern getkey-func))))
  7938. (and (= (downcase sorting-type) ?r)
  7939. (not property)
  7940. (setq property
  7941. (org-icompleting-read "Property: "
  7942. (mapcar 'list (org-buffer-property-keys t))
  7943. nil t))))
  7944. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  7945. (message "Sorting entries...")
  7946. (save-restriction
  7947. (narrow-to-region start end)
  7948. (let ((dcst (downcase sorting-type))
  7949. (case-fold-search nil)
  7950. (now (current-time)))
  7951. (sort-subr
  7952. (/= dcst sorting-type)
  7953. ;; This function moves to the beginning character of the "record" to
  7954. ;; be sorted.
  7955. (lambda nil
  7956. (if (re-search-forward re nil t)
  7957. (goto-char (match-beginning 0))
  7958. (goto-char (point-max))))
  7959. ;; This function moves to the last character of the "record" being
  7960. ;; sorted.
  7961. (lambda nil
  7962. (save-match-data
  7963. (condition-case nil
  7964. (outline-forward-same-level 1)
  7965. (error
  7966. (goto-char (point-max))))))
  7967. ;; This function returns the value that gets sorted against.
  7968. (lambda nil
  7969. (cond
  7970. ((= dcst ?n)
  7971. (if (looking-at org-complex-heading-regexp)
  7972. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7973. nil))
  7974. ((= dcst ?a)
  7975. (if (looking-at org-complex-heading-regexp)
  7976. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7977. nil))
  7978. ((= dcst ?k)
  7979. (or (get-text-property (point) :org-clock-minutes) 0))
  7980. ((= dcst ?t)
  7981. (let ((end (save-excursion (outline-next-heading) (point))))
  7982. (if (or (re-search-forward org-ts-regexp end t)
  7983. (re-search-forward org-ts-regexp-both end t))
  7984. (org-time-string-to-seconds (match-string 0))
  7985. (org-float-time now))))
  7986. ((= dcst ?c)
  7987. (let ((end (save-excursion (outline-next-heading) (point))))
  7988. (if (re-search-forward
  7989. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7990. end t)
  7991. (org-time-string-to-seconds (match-string 0))
  7992. (org-float-time now))))
  7993. ((= dcst ?s)
  7994. (let ((end (save-excursion (outline-next-heading) (point))))
  7995. (if (re-search-forward org-scheduled-time-regexp end t)
  7996. (org-time-string-to-seconds (match-string 1))
  7997. (org-float-time now))))
  7998. ((= dcst ?d)
  7999. (let ((end (save-excursion (outline-next-heading) (point))))
  8000. (if (re-search-forward org-deadline-time-regexp end t)
  8001. (org-time-string-to-seconds (match-string 1))
  8002. (org-float-time now))))
  8003. ((= dcst ?p)
  8004. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8005. (string-to-char (match-string 2))
  8006. org-default-priority))
  8007. ((= dcst ?r)
  8008. (or (org-entry-get nil property) ""))
  8009. ((= dcst ?o)
  8010. (if (looking-at org-complex-heading-regexp)
  8011. (let* ((m (match-string 2))
  8012. (s (if (member m org-done-keywords) '- '+)))
  8013. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8014. ((= dcst ?f)
  8015. (if getkey-func
  8016. (progn
  8017. (setq tmp (funcall getkey-func))
  8018. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  8019. tmp)
  8020. (error "Invalid key function `%s'" getkey-func)))
  8021. (t (error "Invalid sorting type `%c'" sorting-type))))
  8022. nil
  8023. (cond
  8024. ((= dcst ?a) 'string<)
  8025. ((= dcst ?f) compare-func)
  8026. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8027. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8028. ;; Reset the clock marker if needed
  8029. (when cmstr
  8030. (save-excursion
  8031. (goto-char start)
  8032. (search-forward cmstr nil t)
  8033. (move-marker org-clock-marker (point))))
  8034. (message "Sorting entries...done")))
  8035. (defun org-do-sort (table what &optional with-case sorting-type)
  8036. "Sort TABLE of WHAT according to SORTING-TYPE.
  8037. The user will be prompted for the SORTING-TYPE if the call to this
  8038. function does not specify it.
  8039. WHAT is only for the prompt, to indicate what is being sorted.
  8040. The sorting key will be extracted from the car of the elements of
  8041. the table.
  8042. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  8043. (unless sorting-type
  8044. (message
  8045. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  8046. what)
  8047. (setq sorting-type (read-char-exclusive)))
  8048. (let ((dcst (downcase sorting-type))
  8049. extractfun comparefun)
  8050. ;; Define the appropriate functions
  8051. (cond
  8052. ((= dcst ?n)
  8053. (setq extractfun 'string-to-number
  8054. comparefun (if (= dcst sorting-type) '< '>)))
  8055. ((= dcst ?a)
  8056. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  8057. (lambda(x) (downcase (org-sort-remove-invisible x))))
  8058. comparefun (if (= dcst sorting-type)
  8059. 'string<
  8060. (lambda (a b) (and (not (string< a b))
  8061. (not (string= a b)))))))
  8062. ((= dcst ?t)
  8063. (setq extractfun
  8064. (lambda (x)
  8065. (cond ((or (string-match org-ts-regexp x)
  8066. (string-match org-ts-regexp-both x))
  8067. (org-float-time
  8068. (org-time-string-to-time (match-string 0 x))))
  8069. ((string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" x)
  8070. (org-hh:mm-string-to-minutes x))
  8071. (t 0)))
  8072. comparefun (if (= dcst sorting-type) '< '>)))
  8073. (t (error "Invalid sorting type `%c'" sorting-type)))
  8074. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  8075. table)
  8076. (lambda (a b) (funcall comparefun (car a) (car b))))))
  8077. ;;; The orgstruct minor mode
  8078. ;; Define a minor mode which can be used in other modes in order to
  8079. ;; integrate the org-mode structure editing commands.
  8080. ;; This is really a hack, because the org-mode structure commands use
  8081. ;; keys which normally belong to the major mode. Here is how it
  8082. ;; works: The minor mode defines all the keys necessary to operate the
  8083. ;; structure commands, but wraps the commands into a function which
  8084. ;; tests if the cursor is currently at a headline or a plain list
  8085. ;; item. If that is the case, the structure command is used,
  8086. ;; temporarily setting many Org-mode variables like regular
  8087. ;; expressions for filling etc. However, when any of those keys is
  8088. ;; used at a different location, function uses `key-binding' to look
  8089. ;; up if the key has an associated command in another currently active
  8090. ;; keymap (minor modes, major mode, global), and executes that
  8091. ;; command. There might be problems if any of the keys is otherwise
  8092. ;; used as a prefix key.
  8093. (defcustom orgstruct-heading-prefix-regexp ""
  8094. "Regexp that matches the custom prefix of Org headlines in
  8095. orgstruct(++)-mode."
  8096. :group 'org
  8097. :version "24.4"
  8098. :package-version '(Org . "8.3")
  8099. :type 'regexp)
  8100. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8101. (defcustom orgstruct-setup-hook nil
  8102. "Hook run after orgstruct-mode-map is filled."
  8103. :group 'org
  8104. :version "24.4"
  8105. :package-version '(Org . "8.0")
  8106. :type 'hook)
  8107. (defvar orgstruct-initialized nil)
  8108. (defvar org-local-vars nil
  8109. "List of local variables, for use by `orgstruct-mode'.")
  8110. ;;;###autoload
  8111. (define-minor-mode orgstruct-mode
  8112. "Toggle the minor mode `orgstruct-mode'.
  8113. This mode is for using Org-mode structure commands in other
  8114. modes. The following keys behave as if Org-mode were active, if
  8115. the cursor is on a headline, or on a plain list item (both as
  8116. defined by Org-mode)."
  8117. nil " OrgStruct" (make-sparse-keymap)
  8118. (funcall (if orgstruct-mode
  8119. 'add-to-invisibility-spec
  8120. 'remove-from-invisibility-spec)
  8121. '(outline . t))
  8122. (when orgstruct-mode
  8123. (org-load-modules-maybe)
  8124. (unless orgstruct-initialized
  8125. (orgstruct-setup)
  8126. (setq orgstruct-initialized t))))
  8127. ;;;###autoload
  8128. (defun turn-on-orgstruct ()
  8129. "Unconditionally turn on `orgstruct-mode'."
  8130. (orgstruct-mode 1))
  8131. (defvar org-fb-vars nil)
  8132. (make-variable-buffer-local 'org-fb-vars)
  8133. (defun orgstruct++-mode (&optional arg)
  8134. "Toggle `orgstruct-mode', the enhanced version of it.
  8135. In addition to setting orgstruct-mode, this also exports all
  8136. indentation and autofilling variables from org-mode into the
  8137. buffer. It will also recognize item context in multiline items."
  8138. (interactive "P")
  8139. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8140. (if (< arg 1)
  8141. (progn (orgstruct-mode -1)
  8142. (mapc (lambda(v)
  8143. (org-set-local (car v)
  8144. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8145. org-fb-vars))
  8146. (orgstruct-mode 1)
  8147. (setq org-fb-vars nil)
  8148. (unless org-local-vars
  8149. (setq org-local-vars (org-get-local-variables)))
  8150. (let (var val)
  8151. (mapc
  8152. (lambda (x)
  8153. (when (string-match
  8154. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8155. (symbol-name (car x)))
  8156. (setq var (car x) val (nth 1 x))
  8157. (push (list var `(quote ,(eval var))) org-fb-vars)
  8158. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8159. org-local-vars)
  8160. (org-set-local 'orgstruct-is-++ t))))
  8161. (defvar orgstruct-is-++ nil
  8162. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8163. (make-variable-buffer-local 'orgstruct-is-++)
  8164. ;;;###autoload
  8165. (defun turn-on-orgstruct++ ()
  8166. "Unconditionally turn on `orgstruct++-mode'."
  8167. (orgstruct++-mode 1))
  8168. (defun orgstruct-error ()
  8169. "Error when there is no default binding for a structure key."
  8170. (interactive)
  8171. (funcall (if (fboundp 'user-error)
  8172. 'user-error
  8173. 'error)
  8174. "This key has no function outside structure elements"))
  8175. (defun orgstruct-setup ()
  8176. "Setup orgstruct keymap."
  8177. (dolist (cell '((org-demote . t)
  8178. (org-metaleft . t)
  8179. (org-metaright . t)
  8180. (org-promote . t)
  8181. (org-shiftmetaleft . t)
  8182. (org-shiftmetaright . t)
  8183. org-backward-element
  8184. org-backward-heading-same-level
  8185. org-ctrl-c-ret
  8186. org-ctrl-c-minus
  8187. org-ctrl-c-star
  8188. org-cycle
  8189. org-forward-heading-same-level
  8190. org-insert-heading
  8191. org-insert-heading-respect-content
  8192. org-kill-note-or-show-branches
  8193. org-mark-subtree
  8194. org-meta-return
  8195. org-metadown
  8196. org-metaup
  8197. org-narrow-to-subtree
  8198. org-promote-subtree
  8199. org-reveal
  8200. org-shiftdown
  8201. org-shiftleft
  8202. org-shiftmetadown
  8203. org-shiftmetaup
  8204. org-shiftright
  8205. org-shifttab
  8206. org-shifttab
  8207. org-shiftup
  8208. org-show-subtree
  8209. org-sort
  8210. org-up-element
  8211. outline-demote
  8212. outline-next-visible-heading
  8213. outline-previous-visible-heading
  8214. outline-promote
  8215. outline-up-heading
  8216. show-children))
  8217. (let ((f (or (car-safe cell) cell))
  8218. (disable-when-heading-prefix (cdr-safe cell)))
  8219. (when (fboundp f)
  8220. (let ((new-bindings))
  8221. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8222. (where-is-internal f outline-mode-map)))
  8223. (push binding new-bindings)
  8224. ;; TODO use local-function-key-map
  8225. (dolist (rep '(("<tab>" . "TAB")
  8226. ("<return>" . "RET")
  8227. ("<escape>" . "ESC")
  8228. ("<delete>" . "DEL")))
  8229. (setq binding (read-kbd-macro
  8230. (let ((case-fold-search))
  8231. (replace-regexp-in-string
  8232. (regexp-quote (cdr rep))
  8233. (car rep)
  8234. (key-description binding)))))
  8235. (pushnew binding new-bindings :test 'equal)))
  8236. (dolist (binding new-bindings)
  8237. (let ((key (lookup-key orgstruct-mode-map binding)))
  8238. (when (or (not key) (numberp key))
  8239. (ignore-errors
  8240. (org-defkey orgstruct-mode-map
  8241. binding
  8242. (orgstruct-make-binding
  8243. f binding disable-when-heading-prefix))))))))))
  8244. (run-hooks 'orgstruct-setup-hook))
  8245. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8246. "Create a function for binding in the structure minor mode.
  8247. FUN is the command to call inside a table. KEY is the key that
  8248. should be checked in for a command to execute outside of tables.
  8249. Non-nil `disable-when-heading-prefix' means to disable the command
  8250. if `orgstruct-heading-prefix-regexp' is not empty."
  8251. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8252. (let ((nname name)
  8253. (i 0))
  8254. (while (fboundp (intern nname))
  8255. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8256. (setq name (intern nname)))
  8257. (eval
  8258. (let ((bindings '((org-heading-regexp
  8259. (concat "^"
  8260. orgstruct-heading-prefix-regexp
  8261. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8262. (org-outline-regexp
  8263. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8264. (org-outline-regexp-bol
  8265. (concat "^" org-outline-regexp))
  8266. (outline-regexp org-outline-regexp)
  8267. (outline-heading-end-regexp "\n")
  8268. (outline-level 'org-outline-level)
  8269. (outline-heading-alist))))
  8270. `(defun ,name (arg)
  8271. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8272. "Outside of structure, run the binding of `"
  8273. (key-description key) "'."
  8274. (when disable-when-heading-prefix
  8275. (concat
  8276. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8277. "back to the default binding due to limitations of Org's implementation of\n"
  8278. "`" (symbol-name fun) "'.")))
  8279. (interactive "p")
  8280. (let* ((disable
  8281. ,(and disable-when-heading-prefix
  8282. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8283. (fallback
  8284. (or disable
  8285. (not
  8286. (let* ,bindings
  8287. (org-context-p 'headline 'item
  8288. ,(when (memq fun
  8289. '(org-insert-heading
  8290. org-insert-heading-respect-content
  8291. org-meta-return))
  8292. '(when orgstruct-is-++
  8293. 'item-body))))))))
  8294. (if fallback
  8295. (let* ((orgstruct-mode)
  8296. (binding
  8297. (let ((key ,key))
  8298. (catch 'exit
  8299. (dolist
  8300. (rep
  8301. '(nil
  8302. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8303. ("<\\([^>]*\\)return>" . "\\1RET")
  8304. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8305. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8306. nil)
  8307. (when rep
  8308. (setq key (read-kbd-macro
  8309. (let ((case-fold-search))
  8310. (replace-regexp-in-string
  8311. (car rep)
  8312. (cdr rep)
  8313. (key-description key))))))
  8314. (when (key-binding key)
  8315. (throw 'exit (key-binding key))))))))
  8316. (if (keymapp binding)
  8317. (org-set-transient-map binding)
  8318. (let ((func (or binding
  8319. (unless disable
  8320. 'orgstruct-error))))
  8321. (when func
  8322. (call-interactively func)))))
  8323. (org-run-like-in-org-mode
  8324. (lambda ()
  8325. (interactive)
  8326. (let* ,bindings
  8327. (call-interactively ',fun)))))))))
  8328. name))
  8329. (defun org-contextualize-keys (alist contexts)
  8330. "Return valid elements in ALIST depending on CONTEXTS.
  8331. `org-agenda-custom-commands' or `org-capture-templates' are the
  8332. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8333. or `org-capture-templates-contexts' are the associated contexts
  8334. definitions."
  8335. (let ((contexts
  8336. ;; normalize contexts
  8337. (mapcar
  8338. (lambda(c) (cond ((listp (cadr c))
  8339. (list (car c) (car c) (cadr c)))
  8340. ((string= "" (cadr c))
  8341. (list (car c) (car c) (caddr c)))
  8342. (t c))) contexts))
  8343. (a alist) c r s)
  8344. ;; loop over all commands or templates
  8345. (while (setq c (pop a))
  8346. (let (vrules repl)
  8347. (cond
  8348. ((not (assoc (car c) contexts))
  8349. (push c r))
  8350. ((and (assoc (car c) contexts)
  8351. (setq vrules (org-contextualize-validate-key
  8352. (car c) contexts)))
  8353. (mapc (lambda (vr)
  8354. (when (not (equal (car vr) (cadr vr)))
  8355. (setq repl vr))) vrules)
  8356. (if (not repl) (push c r)
  8357. (push (cadr repl) s)
  8358. (push
  8359. (cons (car c)
  8360. (cdr (or (assoc (cadr repl) alist)
  8361. (error "Undefined key `%s' as contextual replacement for `%s'"
  8362. (cadr repl) (car c)))))
  8363. r))))))
  8364. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8365. (delq
  8366. nil
  8367. (delete-dups
  8368. (mapcar (lambda (x)
  8369. (let ((tpl (car x)))
  8370. (when (not (delq
  8371. nil
  8372. (mapcar (lambda(y)
  8373. (equal y tpl)) s))) x)))
  8374. (reverse r))))))
  8375. (defun org-contextualize-validate-key (key contexts)
  8376. "Check CONTEXTS for agenda or capture KEY."
  8377. (let (r rr res)
  8378. (while (setq r (pop contexts))
  8379. (mapc
  8380. (lambda (rr)
  8381. (when
  8382. (and (equal key (car r))
  8383. (if (functionp rr) (funcall rr)
  8384. (or (and (eq (car rr) 'in-file)
  8385. (buffer-file-name)
  8386. (string-match (cdr rr) (buffer-file-name)))
  8387. (and (eq (car rr) 'in-mode)
  8388. (string-match (cdr rr) (symbol-name major-mode)))
  8389. (and (eq (car rr) 'in-buffer)
  8390. (string-match (cdr rr) (buffer-name)))
  8391. (when (and (eq (car rr) 'not-in-file)
  8392. (buffer-file-name))
  8393. (not (string-match (cdr rr) (buffer-file-name))))
  8394. (when (eq (car rr) 'not-in-mode)
  8395. (not (string-match (cdr rr) (symbol-name major-mode))))
  8396. (when (eq (car rr) 'not-in-buffer)
  8397. (not (string-match (cdr rr) (buffer-name)))))))
  8398. (push r res)))
  8399. (car (last r))))
  8400. (delete-dups (delq nil res))))
  8401. (defun org-context-p (&rest contexts)
  8402. "Check if local context is any of CONTEXTS.
  8403. Possible values in the list of contexts are `table', `headline', and `item'."
  8404. (let ((pos (point)))
  8405. (goto-char (point-at-bol))
  8406. (prog1 (or (and (memq 'table contexts)
  8407. (looking-at "[ \t]*|"))
  8408. (and (memq 'headline contexts)
  8409. (looking-at org-outline-regexp))
  8410. (and (memq 'item contexts)
  8411. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8412. (and (memq 'item-body contexts)
  8413. (org-in-item-p)))
  8414. (goto-char pos))))
  8415. (defun org-get-local-variables ()
  8416. "Return a list of all local variables in an Org mode buffer."
  8417. (let (varlist)
  8418. (with-current-buffer (get-buffer-create "*Org tmp*")
  8419. (erase-buffer)
  8420. (org-mode)
  8421. (setq varlist (buffer-local-variables)))
  8422. (kill-buffer "*Org tmp*")
  8423. (delq nil
  8424. (mapcar
  8425. (lambda (x)
  8426. (setq x
  8427. (if (symbolp x)
  8428. (list x)
  8429. (list (car x) (cdr x))))
  8430. (if (and (not (get (car x) 'org-state))
  8431. (string-match
  8432. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8433. (symbol-name (car x))))
  8434. x nil))
  8435. varlist))))
  8436. (defun org-clone-local-variables (from-buffer &optional regexp)
  8437. "Clone local variables from FROM-BUFFER.
  8438. Optional argument REGEXP selects variables to clone."
  8439. (mapc
  8440. (lambda (pair)
  8441. (and (symbolp (car pair))
  8442. (or (null regexp)
  8443. (string-match regexp (symbol-name (car pair))))
  8444. (set (make-local-variable (car pair))
  8445. (cdr pair))))
  8446. (buffer-local-variables from-buffer)))
  8447. ;;;###autoload
  8448. (defun org-run-like-in-org-mode (cmd)
  8449. "Run a command, pretending that the current buffer is in Org-mode.
  8450. This will temporarily bind local variables that are typically bound in
  8451. Org-mode to the values they have in Org-mode, and then interactively
  8452. call CMD."
  8453. (org-load-modules-maybe)
  8454. (unless org-local-vars
  8455. (setq org-local-vars (org-get-local-variables)))
  8456. (let (binds)
  8457. (dolist (var org-local-vars)
  8458. (when (or (not (boundp (car var)))
  8459. (eq (symbol-value (car var))
  8460. (default-value (car var))))
  8461. (push (list (car var) `(quote ,(cadr var))) binds)))
  8462. (eval `(let ,binds
  8463. (call-interactively (quote ,cmd))))))
  8464. (defun org-get-category (&optional pos force-refresh)
  8465. "Get the category applying to position POS."
  8466. (save-match-data
  8467. (if force-refresh (org-refresh-category-properties))
  8468. (let ((pos (or pos (point))))
  8469. (or (get-text-property pos 'org-category)
  8470. (progn (org-refresh-category-properties)
  8471. (get-text-property pos 'org-category))))))
  8472. ;;; Refresh properties
  8473. (defun org-refresh-properties (dprop tprop)
  8474. "Refresh buffer text properties.
  8475. DPROP is the drawer property and TPROP is either the
  8476. corresponding text property to set, or an alist with each element
  8477. being a text property (as a symbol) and a function to apply to
  8478. the value of the drawer property."
  8479. (let ((case-fold-search t)
  8480. (inhibit-read-only t))
  8481. (org-with-silent-modifications
  8482. (save-excursion
  8483. (save-restriction
  8484. (widen)
  8485. (goto-char (point-min))
  8486. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8487. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8488. (defun org-refresh-property (tprop p)
  8489. "Refresh the buffer text property TPROP from the drawer property P.
  8490. The refresh happens only for the current tree (not subtree)."
  8491. (save-excursion
  8492. (org-back-to-heading t)
  8493. ;; tprop is a text property symbol
  8494. (if (symbolp tprop)
  8495. (put-text-property
  8496. (point) (or (outline-next-heading) (point-max)) tprop p)
  8497. ;; tprop is an alist with (properties . function) elements
  8498. (mapc (lambda(al)
  8499. (save-excursion
  8500. (put-text-property
  8501. (point-at-bol) (or (outline-next-heading) (point-max))
  8502. (car al)
  8503. (funcall (cdr al) p))))
  8504. tprop))))
  8505. (defun org-refresh-category-properties ()
  8506. "Refresh category text properties in the buffer."
  8507. (let ((case-fold-search t)
  8508. (inhibit-read-only t)
  8509. (def-cat (cond
  8510. ((null org-category)
  8511. (if buffer-file-name
  8512. (file-name-sans-extension
  8513. (file-name-nondirectory buffer-file-name))
  8514. "???"))
  8515. ((symbolp org-category) (symbol-name org-category))
  8516. (t org-category)))
  8517. beg end cat pos optionp)
  8518. (org-with-silent-modifications
  8519. (save-excursion
  8520. (save-restriction
  8521. (widen)
  8522. (goto-char (point-min))
  8523. (put-text-property (point) (point-max) 'org-category def-cat)
  8524. (while (re-search-forward
  8525. "^[ \t]*\\(\\(?:#\\+\\|:\\)CATEGORY:\\)\\(.*\\)" nil t)
  8526. (setq pos (match-end 0)
  8527. optionp (equal (char-after (match-beginning 0)) ?#)
  8528. cat (org-trim (match-string 2)))
  8529. (if optionp
  8530. (setq beg (point-at-bol) end (point-max))
  8531. (org-back-to-heading t)
  8532. (setq beg (point) end (org-end-of-subtree t t)))
  8533. (put-text-property beg end 'org-category cat)
  8534. (goto-char pos)))))))
  8535. (defun org-refresh-stats-properties ()
  8536. "Refresh stats text properties in the buffer."
  8537. (let (stats)
  8538. (org-with-silent-modifications
  8539. (save-excursion
  8540. (save-restriction
  8541. (widen)
  8542. (goto-char (point-min))
  8543. (while (re-search-forward
  8544. (concat org-outline-regexp-bol ".*"
  8545. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8546. nil t)
  8547. (setq stats (cond ((equal (match-string 3) "0") 0)
  8548. ((match-string 2)
  8549. (/ (* (string-to-number (match-string 2)) 100)
  8550. (string-to-number (match-string 3))))
  8551. (t (string-to-number (match-string 1)))))
  8552. (org-back-to-heading t)
  8553. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8554. 'org-stats stats)))))))
  8555. (defun org-refresh-effort-properties ()
  8556. "Refresh effort properties"
  8557. (org-refresh-properties
  8558. org-effort-property
  8559. '((effort . identity)
  8560. (effort-minutes . org-duration-string-to-minutes))))
  8561. ;;;; Link Stuff
  8562. ;;; Link abbreviations
  8563. (defun org-link-expand-abbrev (link)
  8564. "Apply replacements as defined in `org-link-abbrev-alist'."
  8565. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8566. (let* ((key (match-string 1 link))
  8567. (as (or (assoc key org-link-abbrev-alist-local)
  8568. (assoc key org-link-abbrev-alist)))
  8569. (tag (and (match-end 2) (match-string 3 link)))
  8570. rpl)
  8571. (if (not as)
  8572. link
  8573. (setq rpl (cdr as))
  8574. (cond
  8575. ((symbolp rpl) (funcall rpl tag))
  8576. ((string-match "%(\\([^)]+\\))" rpl)
  8577. (replace-match
  8578. (save-match-data
  8579. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8580. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8581. ((string-match "%h" rpl)
  8582. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8583. (t (concat rpl tag)))))
  8584. link))
  8585. ;;; Storing and inserting links
  8586. (defvar org-insert-link-history nil
  8587. "Minibuffer history for links inserted with `org-insert-link'.")
  8588. (defvar org-stored-links nil
  8589. "Contains the links stored with `org-store-link'.")
  8590. (defvar org-store-link-plist nil
  8591. "Plist with info about the most recently link created with `org-store-link'.")
  8592. (defvar org-link-protocols nil
  8593. "Link protocols added to Org-mode using `org-add-link-type'.")
  8594. (defvar org-store-link-functions nil
  8595. "List of functions that are called to create and store a link.
  8596. Each function will be called in turn until one returns a non-nil
  8597. value. Each function should check if it is responsible for creating
  8598. this link (for example by looking at the major mode).
  8599. If not, it must exit and return nil.
  8600. If yes, it should return a non-nil value after a calling
  8601. `org-store-link-props' with a list of properties and values.
  8602. Special properties are:
  8603. :type The link prefix, like \"http\". This must be given.
  8604. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8605. This is obligatory as well.
  8606. :description Optional default description for the second pair
  8607. of brackets in an Org-mode link. The user can still change
  8608. this when inserting this link into an Org-mode buffer.
  8609. In addition to these, any additional properties can be specified
  8610. and then used in capture templates.")
  8611. (defun org-add-link-type (type &optional follow export)
  8612. "Add TYPE to the list of `org-link-types'.
  8613. Re-compute all regular expressions depending on `org-link-types'
  8614. FOLLOW and EXPORT are two functions.
  8615. FOLLOW should take the link path as the single argument and do whatever
  8616. is necessary to follow the link, for example find a file or display
  8617. a mail message.
  8618. EXPORT should format the link path for export to one of the export formats.
  8619. It should be a function accepting three arguments:
  8620. path the path of the link, the text after the prefix (like \"http:\")
  8621. desc the description of the link, if any, or a description added by
  8622. org-export-normalize-links if there is none
  8623. format the export format, a symbol like `html' or `latex' or `ascii'..
  8624. The function may use the FORMAT information to return different values
  8625. depending on the format. The return value will be put literally into
  8626. the exported file. If the return value is nil, this means Org should
  8627. do what it normally does with links which do not have EXPORT defined.
  8628. Org-mode has a built-in default for exporting links. If you are happy with
  8629. this default, there is no need to define an export function for the link
  8630. type. For a simple example of an export function, see `org-bbdb.el'."
  8631. (add-to-list 'org-link-types type t)
  8632. (org-make-link-regexps)
  8633. (if (assoc type org-link-protocols)
  8634. (setcdr (assoc type org-link-protocols) (list follow export))
  8635. (push (list type follow export) org-link-protocols)))
  8636. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8637. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8638. ;;;###autoload
  8639. (defun org-store-link (arg)
  8640. "\\<org-mode-map>Store an org-link to the current location.
  8641. This link is added to `org-stored-links' and can later be inserted
  8642. into an org-buffer with \\[org-insert-link].
  8643. For some link types, a prefix arg is interpreted.
  8644. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8645. For file links, arg negates `org-context-in-file-links'.
  8646. A double prefix arg force skipping storing functions that are not
  8647. part of Org's core.
  8648. A triple prefix arg force storing a link for each line in the
  8649. active region."
  8650. (interactive "P")
  8651. (org-load-modules-maybe)
  8652. (if (and (equal arg '(64)) (org-region-active-p))
  8653. (save-excursion
  8654. (let ((end (region-end)))
  8655. (goto-char (region-beginning))
  8656. (set-mark (point))
  8657. (while (< (point-at-eol) end)
  8658. (move-end-of-line 1) (activate-mark)
  8659. (let (current-prefix-arg)
  8660. (call-interactively 'org-store-link))
  8661. (move-beginning-of-line 2)
  8662. (set-mark (point)))))
  8663. (org-with-limited-levels
  8664. (setq org-store-link-plist nil)
  8665. (let (link cpltxt desc description search
  8666. txt custom-id agenda-link sfuns sfunsn)
  8667. (cond
  8668. ;; Store a link using an external link type
  8669. ((and (not (equal arg '(16)))
  8670. (setq sfuns
  8671. (delq
  8672. nil (mapcar (lambda (f)
  8673. (let (fs) (if (funcall f) (push f fs))))
  8674. org-store-link-functions))
  8675. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8676. (or (and (cdr sfuns)
  8677. (funcall (intern
  8678. (completing-read
  8679. "Which function for creating the link? "
  8680. sfunsn nil t (car sfunsn)))))
  8681. (funcall (caar sfuns)))
  8682. (setq link (plist-get org-store-link-plist :link)
  8683. desc (or (plist-get org-store-link-plist
  8684. :description) link))))
  8685. ;; Store a link from a source code buffer
  8686. ((org-src-edit-buffer-p)
  8687. (let (label gc)
  8688. (while (or (not label)
  8689. (save-excursion
  8690. (save-restriction
  8691. (widen)
  8692. (goto-char (point-min))
  8693. (re-search-forward
  8694. (regexp-quote (format org-coderef-label-format label))
  8695. nil t))))
  8696. (when label (message "Label exists already") (sit-for 2))
  8697. (setq label (read-string "Code line label: " label)))
  8698. (end-of-line 1)
  8699. (setq link (format org-coderef-label-format label))
  8700. (setq gc (- 79 (length link)))
  8701. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8702. (insert link)
  8703. (setq link (concat "(" label ")") desc nil)))
  8704. ;; We are in the agenda, link to referenced location
  8705. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8706. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8707. (get-text-property (point) 'org-marker))))
  8708. (when m
  8709. (org-with-point-at m
  8710. (setq agenda-link
  8711. (if (org-called-interactively-p 'any)
  8712. (call-interactively 'org-store-link)
  8713. (org-store-link nil)))))))
  8714. ((eq major-mode 'calendar-mode)
  8715. (let ((cd (calendar-cursor-to-date)))
  8716. (setq link
  8717. (format-time-string
  8718. (car org-time-stamp-formats)
  8719. (apply 'encode-time
  8720. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8721. nil nil nil))))
  8722. (org-store-link-props :type "calendar" :date cd)))
  8723. ((eq major-mode 'help-mode)
  8724. (setq link (concat "help:" (save-excursion
  8725. (goto-char (point-min))
  8726. (looking-at "^[^ ]+")
  8727. (match-string 0))))
  8728. (org-store-link-props :type "help"))
  8729. ((eq major-mode 'w3-mode)
  8730. (setq cpltxt (if (and (buffer-name)
  8731. (not (string-match "Untitled" (buffer-name))))
  8732. (buffer-name)
  8733. (url-view-url t))
  8734. link (url-view-url t))
  8735. (org-store-link-props :type "w3" :url (url-view-url t)))
  8736. ((eq major-mode 'image-mode)
  8737. (setq cpltxt (concat "file:"
  8738. (abbreviate-file-name buffer-file-name))
  8739. link cpltxt)
  8740. (org-store-link-props :type "image" :file buffer-file-name))
  8741. ;; In dired, store a link to the file of the current line
  8742. ((derived-mode-p 'dired-mode)
  8743. (let ((file (dired-get-filename nil t)))
  8744. (setq file (if file
  8745. (abbreviate-file-name
  8746. (expand-file-name (dired-get-filename nil t)))
  8747. ;; otherwise, no file so use current directory.
  8748. default-directory))
  8749. (setq cpltxt (concat "file:" file)
  8750. link cpltxt)))
  8751. ((setq search (run-hook-with-args-until-success
  8752. 'org-create-file-search-functions))
  8753. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8754. "::" search))
  8755. (setq cpltxt (or description link)))
  8756. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8757. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8758. (cond
  8759. ;; Store a link using the target at point
  8760. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8761. (setq cpltxt
  8762. (concat "file:"
  8763. (abbreviate-file-name
  8764. (buffer-file-name (buffer-base-buffer)))
  8765. "::" (match-string 1))
  8766. link cpltxt))
  8767. ((and (featurep 'org-id)
  8768. (or (eq org-id-link-to-org-use-id t)
  8769. (and (org-called-interactively-p 'any)
  8770. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8771. (and (eq org-id-link-to-org-use-id
  8772. 'create-if-interactive-and-no-custom-id)
  8773. (not custom-id))))
  8774. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8775. ;; Store a link using the ID at point
  8776. (setq link (condition-case nil
  8777. (prog1 (org-id-store-link)
  8778. (setq desc (or (plist-get org-store-link-plist
  8779. :description)
  8780. "")))
  8781. (error
  8782. ;; Probably before first headline, link only to file
  8783. (concat "file:"
  8784. (abbreviate-file-name
  8785. (buffer-file-name (buffer-base-buffer))))))))
  8786. (t
  8787. ;; Just link to current headline
  8788. (setq cpltxt (concat "file:"
  8789. (abbreviate-file-name
  8790. (buffer-file-name (buffer-base-buffer)))))
  8791. ;; Add a context search string
  8792. (when (org-xor org-context-in-file-links arg)
  8793. (let* ((ee (org-element-at-point))
  8794. (et (org-element-type ee))
  8795. (ev (plist-get (cadr ee) :value))
  8796. (ek (plist-get (cadr ee) :key))
  8797. (eok (and (stringp ek) (string-match "name" ek))))
  8798. (setq txt (cond
  8799. ((org-at-heading-p) nil)
  8800. ((and (eq et 'keyword) eok) ev)
  8801. ((org-region-active-p)
  8802. (buffer-substring (region-beginning) (region-end)))))
  8803. (when (or (null txt) (string-match "\\S-" txt))
  8804. (setq cpltxt
  8805. (concat cpltxt "::"
  8806. (condition-case nil
  8807. (org-make-org-heading-search-string txt)
  8808. (error "")))
  8809. desc (or (and (eq et 'keyword) eok ev)
  8810. (nth 4 (ignore-errors (org-heading-components)))
  8811. "NONE")))))
  8812. (if (string-match "::\\'" cpltxt)
  8813. (setq cpltxt (substring cpltxt 0 -2)))
  8814. (setq link cpltxt))))
  8815. ((buffer-file-name (buffer-base-buffer))
  8816. ;; Just link to this file here.
  8817. (setq cpltxt (concat "file:"
  8818. (abbreviate-file-name
  8819. (buffer-file-name (buffer-base-buffer)))))
  8820. ;; Add a context string.
  8821. (when (org-xor org-context-in-file-links arg)
  8822. (setq txt (if (org-region-active-p)
  8823. (buffer-substring (region-beginning) (region-end))
  8824. (buffer-substring (point-at-bol) (point-at-eol))))
  8825. ;; Only use search option if there is some text.
  8826. (when (string-match "\\S-" txt)
  8827. (setq cpltxt
  8828. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8829. desc "NONE")))
  8830. (setq link cpltxt))
  8831. ((org-called-interactively-p 'interactive)
  8832. (user-error "No method for storing a link from this buffer"))
  8833. (t (setq link nil)))
  8834. ;; We're done setting link and desc, clean up
  8835. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8836. (setq link (or link cpltxt)
  8837. desc (or desc cpltxt))
  8838. (cond ((equal desc "NONE") (setq desc nil))
  8839. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8840. (let ((d0 (match-string 3 desc))
  8841. (p0 (match-string 5 desc)))
  8842. (setq desc
  8843. (replace-regexp-in-string
  8844. org-bracket-link-regexp
  8845. (concat (or p0 d0)
  8846. (if (equal (length (match-string 0 desc))
  8847. (length desc)) "*" "")) desc)))))
  8848. ;; Return the link
  8849. (if (not (and (or (org-called-interactively-p 'any)
  8850. executing-kbd-macro) link))
  8851. (or agenda-link (and link (org-make-link-string link desc)))
  8852. (push (list link desc) org-stored-links)
  8853. (message "Stored: %s" (or desc link))
  8854. (when custom-id
  8855. (setq link (concat "file:" (abbreviate-file-name
  8856. (buffer-file-name)) "::#" custom-id))
  8857. (push (list link desc) org-stored-links))
  8858. (car org-stored-links))))))
  8859. (defun org-store-link-props (&rest plist)
  8860. "Store link properties, extract names and addresses."
  8861. (let (x adr)
  8862. (when (setq x (plist-get plist :from))
  8863. (setq adr (mail-extract-address-components x))
  8864. (setq plist (plist-put plist :fromname (car adr)))
  8865. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8866. (when (setq x (plist-get plist :to))
  8867. (setq adr (mail-extract-address-components x))
  8868. (setq plist (plist-put plist :toname (car adr)))
  8869. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8870. (let ((from (plist-get plist :from))
  8871. (to (plist-get plist :to)))
  8872. (when (and from to org-from-is-user-regexp)
  8873. (setq plist
  8874. (plist-put plist :fromto
  8875. (if (string-match org-from-is-user-regexp from)
  8876. (concat "to %t")
  8877. (concat "from %f"))))))
  8878. (setq org-store-link-plist plist))
  8879. (defun org-add-link-props (&rest plist)
  8880. "Add these properties to the link property list."
  8881. (let (key value)
  8882. (while plist
  8883. (setq key (pop plist) value (pop plist))
  8884. (setq org-store-link-plist
  8885. (plist-put org-store-link-plist key value)))))
  8886. (defun org-email-link-description (&optional fmt)
  8887. "Return the description part of an email link.
  8888. This takes information from `org-store-link-plist' and formats it
  8889. according to FMT (default from `org-email-link-description-format')."
  8890. (setq fmt (or fmt org-email-link-description-format))
  8891. (let* ((p org-store-link-plist)
  8892. (to (plist-get p :toaddress))
  8893. (from (plist-get p :fromaddress))
  8894. (table
  8895. (list
  8896. (cons "%c" (plist-get p :fromto))
  8897. (cons "%F" (plist-get p :from))
  8898. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8899. (cons "%T" (plist-get p :to))
  8900. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8901. (cons "%s" (plist-get p :subject))
  8902. (cons "%d" (plist-get p :date))
  8903. (cons "%m" (plist-get p :message-id)))))
  8904. (when (string-match "%c" fmt)
  8905. ;; Check if the user wrote this message
  8906. (if (and org-from-is-user-regexp from to
  8907. (save-match-data (string-match org-from-is-user-regexp from)))
  8908. (setq fmt (replace-match "to %t" t t fmt))
  8909. (setq fmt (replace-match "from %f" t t fmt))))
  8910. (org-replace-escapes fmt table)))
  8911. (defun org-make-org-heading-search-string (&optional string)
  8912. "Make search string for the current headline or STRING."
  8913. (let ((s (or string
  8914. (and (derived-mode-p 'org-mode)
  8915. (save-excursion
  8916. (org-back-to-heading t)
  8917. (org-element-property :raw-value (org-element-at-point))))))
  8918. (lines org-context-in-file-links))
  8919. (or string (setq s (concat "*" s))) ; Add * for headlines
  8920. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8921. (when (and string (integerp lines) (> lines 0))
  8922. (let ((slines (org-split-string s "\n")))
  8923. (when (< lines (length slines))
  8924. (setq s (mapconcat
  8925. 'identity
  8926. (reverse (nthcdr (- (length slines) lines)
  8927. (reverse slines))) "\n")))))
  8928. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8929. (defun org-make-link-string (link &optional description)
  8930. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8931. (unless (string-match "\\S-" link)
  8932. (error "Empty link"))
  8933. (when (and description
  8934. (stringp description)
  8935. (not (string-match "\\S-" description)))
  8936. (setq description nil))
  8937. (when (stringp description)
  8938. ;; Remove brackets from the description, they are fatal.
  8939. (while (string-match "\\[" description)
  8940. (setq description (replace-match "{" t t description)))
  8941. (while (string-match "\\]" description)
  8942. (setq description (replace-match "}" t t description))))
  8943. (when (equal link description)
  8944. ;; No description needed, it is identical
  8945. (setq description nil))
  8946. (when (and (not description)
  8947. (not (string-match (org-image-file-name-regexp) link))
  8948. (not (equal link (org-link-escape link))))
  8949. (setq description (org-extract-attributes link)))
  8950. (setq link
  8951. (cond ((string-match (org-image-file-name-regexp) link) link)
  8952. ((string-match org-link-types-re link)
  8953. (concat (match-string 1 link)
  8954. (org-link-escape (substring link (match-end 1)))))
  8955. (t (org-link-escape link))))
  8956. (concat "[[" link "]"
  8957. (if description (concat "[" description "]") "")
  8958. "]"))
  8959. (defconst org-link-escape-chars
  8960. ;;%20 %5B %5D
  8961. '(?\ ?\[ ?\])
  8962. "List of characters that should be escaped in a link when stored to Org.
  8963. This is the list that is used for internal purposes.")
  8964. (defconst org-link-escape-chars-browser
  8965. ;;%20 %22
  8966. '(?\ ?\")
  8967. "List of characters to be escaped before handing over to the browser.
  8968. If you consider using this constant then you probably want to use
  8969. the function `org-link-escape-browser' instead. See there why
  8970. this constant is a candidate to be removed once Org drops support
  8971. for Emacs 24.1 and 24.2.")
  8972. (defun org-link-escape (text &optional table merge)
  8973. "Return percent escaped representation of TEXT.
  8974. TEXT is a string with the text to escape.
  8975. Optional argument TABLE is a list with characters that should be
  8976. escaped. When nil, `org-link-escape-chars' is used.
  8977. If optional argument MERGE is set, merge TABLE into
  8978. `org-link-escape-chars'."
  8979. ;; Don't escape chars in internal links
  8980. (if (string-match "^\\*[[:alnum:]]+" text)
  8981. text
  8982. (cond
  8983. ((and table merge)
  8984. (mapc (lambda (defchr)
  8985. (unless (member defchr table)
  8986. (setq table (cons defchr table))))
  8987. org-link-escape-chars))
  8988. ((null table)
  8989. (setq table org-link-escape-chars)))
  8990. (mapconcat
  8991. (lambda (char)
  8992. (if (or (member char table)
  8993. (and (or (< char 32) (= char ?\%) (> char 126))
  8994. org-url-hexify-p))
  8995. (mapconcat (lambda (sequence-element)
  8996. (format "%%%.2X" sequence-element))
  8997. (or (encode-coding-char char 'utf-8)
  8998. (error "Unable to percent escape character: %s"
  8999. (char-to-string char))) "")
  9000. (char-to-string char))) text "")))
  9001. (defun org-link-escape-browser (text)
  9002. "Escape some characters before handing over to the browser.
  9003. This function is a candidate to be removed together with the
  9004. constant `org-link-escape-chars-browser' once Org drops support
  9005. for Emacs 24.1 and 24.2. All calls to this function will have to
  9006. be replaced with `url-encode-url' which is available since Emacs
  9007. 24.3.1."
  9008. ;; Example with the Org link
  9009. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9010. ;; to open the browser with +subject:"Release 8.2" filled into the
  9011. ;; query field: In this case the variable TEXT contains the
  9012. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9013. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9014. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9015. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9016. (if (fboundp 'url-encode-url)
  9017. (url-encode-url text)
  9018. (if (org-string-match-p
  9019. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9020. text)
  9021. (org-link-escape text org-link-escape-chars-browser)
  9022. text)))
  9023. (defun org-link-unescape (str)
  9024. "Unhex hexified Unicode strings as returned from the JavaScript function
  9025. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  9026. (unless (and (null str) (string= "" str))
  9027. (let ((pos 0) (case-fold-search t) unhexed)
  9028. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  9029. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  9030. (setq str (replace-match unhexed t t str))
  9031. (setq pos (+ pos (length unhexed))))))
  9032. str)
  9033. (defun org-link-unescape-compound (hex)
  9034. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9035. Note: this function also decodes single byte encodings like
  9036. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9037. (save-match-data
  9038. (let* ((bytes (cdr (split-string hex "%")))
  9039. (ret "")
  9040. (eat 0)
  9041. (sum 0))
  9042. (while bytes
  9043. (let* ((val (string-to-number (pop bytes) 16))
  9044. (shift-xor
  9045. (if (= 0 eat)
  9046. (cond
  9047. ((>= val 252) (cons 6 252))
  9048. ((>= val 248) (cons 5 248))
  9049. ((>= val 240) (cons 4 240))
  9050. ((>= val 224) (cons 3 224))
  9051. ((>= val 192) (cons 2 192))
  9052. (t (cons 0 0)))
  9053. (cons 6 128))))
  9054. (if (>= val 192) (setq eat (car shift-xor)))
  9055. (setq val (logxor val (cdr shift-xor)))
  9056. (setq sum (+ (lsh sum (car shift-xor)) val))
  9057. (if (> eat 0) (setq eat (- eat 1)))
  9058. (cond
  9059. ((= 0 eat) ;multi byte
  9060. (setq ret (concat ret (org-char-to-string sum)))
  9061. (setq sum 0))
  9062. ((not bytes) ; single byte(s)
  9063. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9064. ret)))
  9065. (defun org-link-unescape-single-byte-sequence (hex)
  9066. "Unhexify hex-encoded single byte character sequences."
  9067. (mapconcat (lambda (byte)
  9068. (char-to-string (string-to-number byte 16)))
  9069. (cdr (split-string hex "%")) ""))
  9070. (defun org-xor (a b)
  9071. "Exclusive or."
  9072. (if a (not b) b))
  9073. (defun org-fixup-message-id-for-http (s)
  9074. "Replace special characters in a message id, so it can be used in an http query."
  9075. (when (string-match "%" s)
  9076. (setq s (mapconcat (lambda (c)
  9077. (if (eq c ?%)
  9078. "%25"
  9079. (char-to-string c)))
  9080. s "")))
  9081. (while (string-match "<" s)
  9082. (setq s (replace-match "%3C" t t s)))
  9083. (while (string-match ">" s)
  9084. (setq s (replace-match "%3E" t t s)))
  9085. (while (string-match "@" s)
  9086. (setq s (replace-match "%40" t t s)))
  9087. s)
  9088. (defun org-link-prettify (link)
  9089. "Return a human-readable representation of LINK.
  9090. The car of LINK must be a raw link.
  9091. The cdr of LINK must be either a link description or nil."
  9092. (let ((desc (or (cadr link) "<no description>")))
  9093. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9094. "<" (car link) ">")))
  9095. ;;;###autoload
  9096. (defun org-insert-link-global ()
  9097. "Insert a link like Org-mode does.
  9098. This command can be called in any mode to insert a link in Org-mode syntax."
  9099. (interactive)
  9100. (org-load-modules-maybe)
  9101. (org-run-like-in-org-mode 'org-insert-link))
  9102. (defun org-insert-all-links (arg &optional pre post)
  9103. "Insert all links in `org-stored-links'.
  9104. When a universal prefix, do not delete the links from `org-stored-links'.
  9105. When `ARG' is a number, insert the last N link(s).
  9106. `PRE' and `POST' are optional arguments to define a string to
  9107. prepend or to append."
  9108. (interactive "P")
  9109. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9110. (links (copy-seq org-stored-links))
  9111. (pr (or pre "- "))
  9112. (po (or post "\n"))
  9113. (cnt 1) l)
  9114. (if (null org-stored-links)
  9115. (message "No link to insert")
  9116. (while (and (or (listp arg) (>= arg cnt))
  9117. (setq l (if (listp arg)
  9118. (pop links)
  9119. (pop org-stored-links))))
  9120. (setq cnt (1+ cnt))
  9121. (insert pr)
  9122. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9123. (insert po)))))
  9124. (defun org-insert-last-stored-link (arg)
  9125. "Insert the last link stored in `org-stored-links'."
  9126. (interactive "p")
  9127. (org-insert-all-links arg "" "\n"))
  9128. (defun org-link-fontify-links-to-this-file ()
  9129. "Fontify links to the current file in `org-stored-links'."
  9130. (let ((f (buffer-file-name)) a b)
  9131. (setq a (mapcar (lambda(l)
  9132. (let ((ll (car l)))
  9133. (when (and (string-match "^file:\\(.+\\)::" ll)
  9134. (equal f (expand-file-name (match-string 1 ll))))
  9135. ll)))
  9136. org-stored-links))
  9137. (when (featurep 'org-id)
  9138. (setq b (mapcar (lambda(l)
  9139. (let ((ll (car l)))
  9140. (when (and (string-match "^id:\\(.+\\)$" ll)
  9141. (equal f (expand-file-name
  9142. (or (org-id-find-id-file
  9143. (match-string 1 ll)) ""))))
  9144. ll)))
  9145. org-stored-links)))
  9146. (mapcar (lambda(l)
  9147. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9148. (delq nil (append a b)))))
  9149. (defvar org-link-links-in-this-file nil)
  9150. (defun org-insert-link (&optional complete-file link-location default-description)
  9151. "Insert a link. At the prompt, enter the link.
  9152. Completion can be used to insert any of the link protocol prefixes like
  9153. http or ftp in use.
  9154. The history can be used to select a link previously stored with
  9155. `org-store-link'. When the empty string is entered (i.e. if you just
  9156. press RET at the prompt), the link defaults to the most recently
  9157. stored link. As SPC triggers completion in the minibuffer, you need to
  9158. use M-SPC or C-q SPC to force the insertion of a space character.
  9159. You will also be prompted for a description, and if one is given, it will
  9160. be displayed in the buffer instead of the link.
  9161. If there is already a link at point, this command will allow you to edit link
  9162. and description parts.
  9163. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9164. be selected using completion. The path to the file will be relative to the
  9165. current directory if the file is in the current directory or a subdirectory.
  9166. Otherwise, the link will be the absolute path as completed in the minibuffer
  9167. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9168. option `org-link-file-path-type'.
  9169. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9170. the current directory or below.
  9171. With three \\[universal-argument] prefixes, negate the meaning of
  9172. `org-keep-stored-link-after-insertion'.
  9173. If `org-make-link-description-function' is non-nil, this function will be
  9174. called with the link target, and the result will be the default
  9175. link description.
  9176. If the LINK-LOCATION parameter is non-nil, this value will be
  9177. used as the link location instead of reading one interactively.
  9178. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9179. be used as the default description."
  9180. (interactive "P")
  9181. (let* ((wcf (current-window-configuration))
  9182. (origbuf (current-buffer))
  9183. (region (if (org-region-active-p)
  9184. (buffer-substring (region-beginning) (region-end))))
  9185. (remove (and region (list (region-beginning) (region-end))))
  9186. (desc region)
  9187. tmphist ; byte-compile incorrectly complains about this
  9188. (link link-location)
  9189. (abbrevs org-link-abbrev-alist-local)
  9190. entry file all-prefixes auto-desc)
  9191. (cond
  9192. (link-location) ; specified by arg, just use it.
  9193. ((org-in-regexp org-bracket-link-regexp 1)
  9194. ;; We do have a link at point, and we are going to edit it.
  9195. (setq remove (list (match-beginning 0) (match-end 0)))
  9196. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9197. (setq link (read-string "Link: "
  9198. (org-link-unescape
  9199. (org-match-string-no-properties 1)))))
  9200. ((or (org-in-regexp org-angle-link-re)
  9201. (org-in-regexp org-plain-link-re))
  9202. ;; Convert to bracket link
  9203. (setq remove (list (match-beginning 0) (match-end 0))
  9204. link (read-string "Link: "
  9205. (org-remove-angle-brackets (match-string 0)))))
  9206. ((member complete-file '((4) (16)))
  9207. ;; Completing read for file names.
  9208. (setq link (org-file-complete-link complete-file)))
  9209. (t
  9210. ;; Read link, with completion for stored links.
  9211. (org-link-fontify-links-to-this-file)
  9212. (org-switch-to-buffer-other-window "*Org Links*")
  9213. (with-current-buffer "*Org Links*"
  9214. (erase-buffer)
  9215. (insert "Insert a link.
  9216. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9217. (when org-stored-links
  9218. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9219. (insert (mapconcat 'org-link-prettify
  9220. (reverse org-stored-links) "\n")))
  9221. (goto-char (point-min)))
  9222. (let ((cw (selected-window)))
  9223. (select-window (get-buffer-window "*Org Links*" 'visible))
  9224. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9225. (unless (pos-visible-in-window-p (point-max))
  9226. (org-fit-window-to-buffer))
  9227. (and (window-live-p cw) (select-window cw)))
  9228. ;; Fake a link history, containing the stored links.
  9229. (setq tmphist (append (mapcar 'car org-stored-links)
  9230. org-insert-link-history))
  9231. (setq all-prefixes (append (mapcar 'car abbrevs)
  9232. (mapcar 'car org-link-abbrev-alist)
  9233. org-link-types))
  9234. (unwind-protect
  9235. (progn
  9236. (setq link
  9237. (org-completing-read
  9238. "Link: "
  9239. (append
  9240. (mapcar (lambda (x) (concat x ":"))
  9241. all-prefixes)
  9242. (mapcar 'car org-stored-links))
  9243. nil nil nil
  9244. 'tmphist
  9245. (caar org-stored-links)))
  9246. (if (not (string-match "\\S-" link))
  9247. (user-error "No link selected"))
  9248. (mapc (lambda(l)
  9249. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9250. org-stored-links)
  9251. (if (or (member link all-prefixes)
  9252. (and (equal ":" (substring link -1))
  9253. (member (substring link 0 -1) all-prefixes)
  9254. (setq link (substring link 0 -1))))
  9255. (setq link (with-current-buffer origbuf
  9256. (org-link-try-special-completion link)))))
  9257. (set-window-configuration wcf)
  9258. (kill-buffer "*Org Links*"))
  9259. (setq entry (assoc link org-stored-links))
  9260. (or entry (push link org-insert-link-history))
  9261. (setq desc (or desc (nth 1 entry)))))
  9262. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9263. (not org-keep-stored-link-after-insertion))
  9264. (setq org-stored-links (delq (assoc link org-stored-links)
  9265. org-stored-links)))
  9266. (if (and (string-match org-plain-link-re link)
  9267. (not (string-match org-ts-regexp link)))
  9268. ;; URL-like link, normalize the use of angular brackets.
  9269. (setq link (org-remove-angle-brackets link)))
  9270. ;; Check if we are linking to the current file with a search
  9271. ;; option If yes, simplify the link by using only the search
  9272. ;; option.
  9273. (when (and buffer-file-name
  9274. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9275. (let* ((path (match-string 1 link))
  9276. (case-fold-search nil)
  9277. (search (match-string 2 link)))
  9278. (save-match-data
  9279. (if (equal (file-truename buffer-file-name) (file-truename path))
  9280. ;; We are linking to this same file, with a search option
  9281. (setq link search)))))
  9282. ;; Check if we can/should use a relative path. If yes, simplify the link
  9283. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9284. (let* ((type (match-string 1 link))
  9285. (path (match-string 2 link))
  9286. (origpath path)
  9287. (case-fold-search nil))
  9288. (cond
  9289. ((or (eq org-link-file-path-type 'absolute)
  9290. (equal complete-file '(16)))
  9291. (setq path (abbreviate-file-name (expand-file-name path))))
  9292. ((eq org-link-file-path-type 'noabbrev)
  9293. (setq path (expand-file-name path)))
  9294. ((eq org-link-file-path-type 'relative)
  9295. (setq path (file-relative-name path)))
  9296. (t
  9297. (save-match-data
  9298. (if (string-match (concat "^" (regexp-quote
  9299. (expand-file-name
  9300. (file-name-as-directory
  9301. default-directory))))
  9302. (expand-file-name path))
  9303. ;; We are linking a file with relative path name.
  9304. (setq path (substring (expand-file-name path)
  9305. (match-end 0)))
  9306. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9307. (setq link (concat type path))
  9308. (if (equal desc origpath)
  9309. (setq desc path))))
  9310. (if org-make-link-description-function
  9311. (setq desc
  9312. (or (condition-case nil
  9313. (funcall org-make-link-description-function link desc)
  9314. (error (progn (message "Can't get link description from `%s'"
  9315. (symbol-name org-make-link-description-function))
  9316. (sit-for 2) nil)))
  9317. (read-string "Description: " default-description)))
  9318. (if default-description (setq desc default-description)
  9319. (setq desc (or (and auto-desc desc)
  9320. (read-string "Description: " desc)))))
  9321. (unless (string-match "\\S-" desc) (setq desc nil))
  9322. (if remove (apply 'delete-region remove))
  9323. (insert (org-make-link-string link desc))))
  9324. (defun org-link-try-special-completion (type)
  9325. "If there is completion support for link type TYPE, offer it."
  9326. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9327. (if (functionp fun)
  9328. (funcall fun)
  9329. (read-string "Link (no completion support): " (concat type ":")))))
  9330. (defun org-file-complete-link (&optional arg)
  9331. "Create a file link using completion."
  9332. (let (file link)
  9333. (setq file (org-iread-file-name "File: "))
  9334. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9335. (pwd1 (file-name-as-directory (abbreviate-file-name
  9336. (expand-file-name ".")))))
  9337. (cond
  9338. ((equal arg '(16))
  9339. (setq link (concat
  9340. "file:"
  9341. (abbreviate-file-name (expand-file-name file)))))
  9342. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9343. (setq link (concat "file:" (match-string 1 file))))
  9344. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9345. (expand-file-name file))
  9346. (setq link (concat
  9347. "file:" (match-string 1 (expand-file-name file)))))
  9348. (t (setq link (concat "file:" file)))))
  9349. link))
  9350. (defun org-iread-file-name (&rest args)
  9351. "Read-file-name using `ido-mode' speedup if available.
  9352. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9353. See `read-file-name' for a description of parameters."
  9354. (org-without-partial-completion
  9355. (if (and org-completion-use-ido
  9356. (fboundp 'ido-read-file-name)
  9357. (boundp 'ido-mode) ido-mode
  9358. (listp (second args)))
  9359. (let ((ido-enter-matching-directory nil))
  9360. (apply 'ido-read-file-name args))
  9361. (apply 'read-file-name args))))
  9362. (defun org-completing-read (&rest args)
  9363. "Completing-read with SPACE being a normal character."
  9364. (let ((enable-recursive-minibuffers t)
  9365. (minibuffer-local-completion-map
  9366. (copy-keymap minibuffer-local-completion-map)))
  9367. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9368. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9369. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9370. (apply 'org-icompleting-read args)))
  9371. (defun org-completing-read-no-i (&rest args)
  9372. (let (org-completion-use-ido org-completion-use-iswitchb)
  9373. (apply 'org-completing-read args)))
  9374. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9375. "Use iswitch as a completing-read replacement to choose from choices.
  9376. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9377. from."
  9378. (let* ((iswitchb-use-virtual-buffers nil)
  9379. (iswitchb-make-buflist-hook
  9380. (lambda ()
  9381. (setq iswitchb-temp-buflist choices))))
  9382. (iswitchb-read-buffer prompt)))
  9383. (defun org-icompleting-read (&rest args)
  9384. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9385. (org-without-partial-completion
  9386. (if (and org-completion-use-ido
  9387. (fboundp 'ido-completing-read)
  9388. (boundp 'ido-mode) ido-mode
  9389. (listp (second args)))
  9390. (let ((ido-enter-matching-directory nil))
  9391. (apply 'ido-completing-read (concat (car args))
  9392. (if (consp (car (nth 1 args)))
  9393. (mapcar 'car (nth 1 args))
  9394. (nth 1 args))
  9395. (cddr args)))
  9396. (if (and org-completion-use-iswitchb
  9397. (boundp 'iswitchb-mode) iswitchb-mode
  9398. (listp (second args)))
  9399. (apply 'org-iswitchb-completing-read (concat (car args))
  9400. (if (consp (car (nth 1 args)))
  9401. (mapcar 'car (nth 1 args))
  9402. (nth 1 args))
  9403. (cddr args))
  9404. (apply 'completing-read args)))))
  9405. (defun org-extract-attributes (s)
  9406. "Extract the attributes cookie from a string and set as text property."
  9407. (let (a attr (start 0) key value)
  9408. (save-match-data
  9409. (when (string-match "{{\\([^}]+\\)}}$" s)
  9410. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9411. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9412. (setq key (match-string 1 a) value (match-string 2 a)
  9413. start (match-end 0)
  9414. attr (plist-put attr (intern key) value))))
  9415. (org-add-props s nil 'org-attr attr))
  9416. s))
  9417. ;;; Opening/following a link
  9418. (defvar org-link-search-failed nil)
  9419. (defvar org-open-link-functions nil
  9420. "Hook for functions finding a plain text link.
  9421. These functions must take a single argument, the link content.
  9422. They will be called for links that look like [[link text][description]]
  9423. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9424. like a filename (e.g. \"./blue.png\").
  9425. These functions will be called *before* Org attempts to resolve the
  9426. link by doing text searches in the current buffer - so if you want a
  9427. link \"[[target]]\" to still find \"<<target>>\", your function should
  9428. handle this as a special case.
  9429. When the function does handle the link, it must return a non-nil value.
  9430. If it decides that it is not responsible for this link, it must return
  9431. nil to indicate that that Org-mode can continue with other options
  9432. like exact and fuzzy text search.")
  9433. (defun org-next-link (&optional search-backward)
  9434. "Move forward to the next link.
  9435. If the link is in hidden text, expose it."
  9436. (interactive "P")
  9437. (when (and org-link-search-failed (eq this-command last-command))
  9438. (goto-char (point-min))
  9439. (message "Link search wrapped back to beginning of buffer"))
  9440. (setq org-link-search-failed nil)
  9441. (let* ((pos (point))
  9442. (ct (org-context))
  9443. (a (assoc :link ct))
  9444. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9445. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9446. ((looking-at org-any-link-re)
  9447. ;; Don't stay stuck at link without an org-link face
  9448. (forward-char (if search-backward -1 1))))
  9449. (if (funcall srch-fun org-any-link-re nil t)
  9450. (progn
  9451. (goto-char (match-beginning 0))
  9452. (if (outline-invisible-p) (org-show-context)))
  9453. (goto-char pos)
  9454. (setq org-link-search-failed t)
  9455. (message "No further link found"))))
  9456. (defun org-previous-link ()
  9457. "Move backward to the previous link.
  9458. If the link is in hidden text, expose it."
  9459. (interactive)
  9460. (funcall 'org-next-link t))
  9461. (defun org-translate-link (s)
  9462. "Translate a link string if a translation function has been defined."
  9463. (if (and org-link-translation-function
  9464. (fboundp org-link-translation-function)
  9465. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9466. (progn
  9467. (setq s (funcall org-link-translation-function
  9468. (match-string 1 s) (match-string 2 s)))
  9469. (concat (car s) ":" (cdr s)))
  9470. s))
  9471. (defun org-translate-link-from-planner (type path)
  9472. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9473. This is still an experimental function, your mileage may vary."
  9474. (cond
  9475. ((member type '("http" "https" "news" "ftp"))
  9476. ;; standard Internet links are the same.
  9477. nil)
  9478. ((and (equal type "irc") (string-match "^//" path))
  9479. ;; Planner has two / at the beginning of an irc link, we have 1.
  9480. ;; We should have zero, actually....
  9481. (setq path (substring path 1)))
  9482. ((and (equal type "lisp") (string-match "^/" path))
  9483. ;; Planner has a slash, we do not.
  9484. (setq type "elisp" path (substring path 1)))
  9485. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9486. ;; A typical message link. Planner has the id after the final slash,
  9487. ;; we separate it with a hash mark
  9488. (setq path (concat (match-string 1 path) "#"
  9489. (org-remove-angle-brackets (match-string 2 path))))))
  9490. (cons type path))
  9491. (defun org-find-file-at-mouse (ev)
  9492. "Open file link or URL at mouse."
  9493. (interactive "e")
  9494. (mouse-set-point ev)
  9495. (org-open-at-point 'in-emacs))
  9496. (defun org-open-at-mouse (ev)
  9497. "Open file link or URL at mouse.
  9498. See the docstring of `org-open-file' for details."
  9499. (interactive "e")
  9500. (mouse-set-point ev)
  9501. (if (eq major-mode 'org-agenda-mode)
  9502. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9503. (org-open-at-point))
  9504. (defvar org-window-config-before-follow-link nil
  9505. "The window configuration before following a link.
  9506. This is saved in case the need arises to restore it.")
  9507. (defvar org-open-link-marker (make-marker)
  9508. "Marker pointing to the location where `org-open-at-point' was called.")
  9509. ;;;###autoload
  9510. (defun org-open-at-point-global ()
  9511. "Follow a link like Org-mode does.
  9512. This command can be called in any mode to follow a link that has
  9513. Org-mode syntax."
  9514. (interactive)
  9515. (org-run-like-in-org-mode 'org-open-at-point))
  9516. ;;;###autoload
  9517. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9518. "Open a link in the string S, as if it was in Org-mode."
  9519. (interactive "sLink: \nP")
  9520. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9521. (with-temp-buffer
  9522. (let ((org-inhibit-startup (not reference-buffer)))
  9523. (org-mode)
  9524. (insert s)
  9525. (goto-char (point-min))
  9526. (when reference-buffer
  9527. (setq org-link-abbrev-alist-local
  9528. (with-current-buffer reference-buffer
  9529. org-link-abbrev-alist-local)))
  9530. (org-open-at-point arg reference-buffer)))))
  9531. (defvar org-open-at-point-functions nil
  9532. "Hook that is run when following a link at point.
  9533. Functions in this hook must return t if they identify and follow
  9534. a link at point. If they don't find anything interesting at point,
  9535. they must return nil.")
  9536. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9537. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9538. (defun org-open-at-point (&optional arg reference-buffer)
  9539. "Open link, timestamp, footnote or tags at point.
  9540. When point is on a link, follow it. Normally, files will be
  9541. opened by an appropriate application. If the optional prefix
  9542. argument ARG is non-nil, Emacs will visit the file. With
  9543. a double prefix argument, try to open outside of Emacs, in the
  9544. application the system uses for this file type.
  9545. When point is on a timestamp, open the agenda at the day
  9546. specified.
  9547. When point is a footnote definition, move to the first reference
  9548. found. If it is on a reference, move to the associated
  9549. definition.
  9550. When point is on a headline, display a list of every link in the
  9551. entry, so it is possible to pick one, or all, of them. If point
  9552. is on a tag, call `org-tags-view' instead.
  9553. When optional argument REFERENCE-BUFFER is non-nil, it should
  9554. specify a buffer from where the link search should happen. This
  9555. is used internally by `org-open-link-from-string'.
  9556. On top of syntactically correct links, this function will open
  9557. the link at point in comments or comment blocks and the first
  9558. link in a property drawer line."
  9559. (interactive "P")
  9560. ;; On a code block, open block's results.
  9561. (unless (call-interactively 'org-babel-open-src-block-result)
  9562. (org-load-modules-maybe)
  9563. (move-marker org-open-link-marker (point))
  9564. (setq org-window-config-before-follow-link (current-window-configuration))
  9565. (org-remove-occur-highlights nil nil t)
  9566. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9567. (let* ((context (org-element-context)) type value)
  9568. ;; On an unsupported type, check if point is contained within
  9569. ;; a support one.
  9570. (while (and (not (memq (setq type (org-element-type context))
  9571. '(comment comment-block
  9572. headline inlinetask link
  9573. footnote-definition footnote-reference
  9574. node-property timestamp)))
  9575. (setq context (org-element-property :parent context))))
  9576. (setq value (org-element-property :value context))
  9577. (cond
  9578. ;; Blank lines at the beginning of buffer: bail out.
  9579. ((not context) (user-error "No link found"))
  9580. ;; Exception n°1: links in property drawers
  9581. ((eq type 'node-property)
  9582. (org-open-link-from-string
  9583. (and (string-match org-any-link-re value)
  9584. (match-string-no-properties 0 value))))
  9585. ;; Exception n°2: links in comments.
  9586. ((memq type '(comment comment-block))
  9587. (save-excursion
  9588. (skip-chars-forward "\\S-" (point-at-eol))
  9589. (let ((string-rear (replace-regexp-in-string
  9590. "^[ \t]*# [ \t]*" ""
  9591. (buffer-substring (point) (line-beginning-position))))
  9592. (string-front (buffer-substring (point) (line-end-position))))
  9593. (with-temp-buffer
  9594. (let ((org-inhibit-startup t)) (org-mode))
  9595. (insert value)
  9596. (goto-char (point-min))
  9597. (when (and (search-forward string-rear nil t)
  9598. (search-forward string-front (line-end-position) t))
  9599. (goto-char (match-beginning 0))
  9600. (org-open-at-point)
  9601. (when (string= string-rear "") (forward-char)))))))
  9602. ;; On a headline or an inlinetask, but not on a timestamp,
  9603. ;; a link, a footnote reference or on tags.
  9604. ((and (memq type '(headline inlinetask))
  9605. ;; Not on tags.
  9606. (progn (save-excursion (beginning-of-line)
  9607. (looking-at org-complex-heading-regexp))
  9608. (or (not (match-beginning 5))
  9609. (< (point) (match-beginning 5)))))
  9610. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9611. (links (car data))
  9612. (links-end (cdr data)))
  9613. (if links
  9614. (dolist (link (if (stringp links) (list links) links))
  9615. (search-forward link nil links-end)
  9616. (goto-char (match-beginning 0))
  9617. (org-open-at-point))
  9618. (require 'org-attach)
  9619. (org-attach-reveal 'if-exists))))
  9620. ;; Do nothing on white spaces after an object, unless point
  9621. ;; is right after it.
  9622. ((> (point)
  9623. (save-excursion
  9624. (goto-char (org-element-property :end context))
  9625. (skip-chars-backward " \t")
  9626. (point)))
  9627. (user-error "No link found"))
  9628. ((eq type 'timestamp) (org-follow-timestamp-link))
  9629. ;; On tags within a headline or an inlinetask.
  9630. ((and (memq type '(headline inlinetask))
  9631. (progn (save-excursion (beginning-of-line)
  9632. (looking-at org-complex-heading-regexp))
  9633. (and (match-beginning 5)
  9634. (>= (point) (match-beginning 5)))))
  9635. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9636. ((eq type 'link)
  9637. (let ((type (org-element-property :type context))
  9638. (path (org-link-unescape (org-element-property :path context))))
  9639. ;; Switch back to REFERENCE-BUFFER needed when called in
  9640. ;; a temporary buffer through `org-open-link-from-string'.
  9641. (with-current-buffer (or reference-buffer (current-buffer))
  9642. (cond
  9643. ((equal type "file")
  9644. (if (string-match "[*?{]" (file-name-nondirectory path))
  9645. (dired path)
  9646. ;; Look into `org-link-protocols' in order to find
  9647. ;; a DEDICATED-FUNCTION to open file. The function
  9648. ;; will be applied on raw link instead of parsed
  9649. ;; link due to the limitation in `org-add-link-type'
  9650. ;; ("open" function called with a single argument).
  9651. ;; If no such function is found, fallback to
  9652. ;; `org-open-file'.
  9653. ;;
  9654. ;; Note : "file+emacs" and "file+sys" types are
  9655. ;; hard-coded in order to escape the previous
  9656. ;; limitation.
  9657. (let* ((option (org-element-property :search-option context))
  9658. (app (org-element-property :application context))
  9659. (dedicated-function
  9660. (nth 1 (assoc app org-link-protocols))))
  9661. (if dedicated-function
  9662. (funcall dedicated-function
  9663. (concat path
  9664. (and option (concat "::" option))))
  9665. (apply 'org-open-file
  9666. path
  9667. (cond (arg)
  9668. ((equal app "emacs") 'emacs)
  9669. ((equal app "sys") 'system))
  9670. (cond ((not option) nil)
  9671. ((org-string-match-p "\\`[0-9]+\\'" option)
  9672. (list (string-to-number option)))
  9673. (t (list nil
  9674. (org-link-unescape option)))))))))
  9675. ((assoc type org-link-protocols)
  9676. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9677. ((equal type "help")
  9678. (let ((f-or-v (intern path)))
  9679. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9680. ((boundp f-or-v) (describe-variable f-or-v))
  9681. (t (error "Not a known function or variable")))))
  9682. ((member type '("http" "https" "ftp" "mailto" "news"))
  9683. (browse-url (org-link-escape-browser (concat type ":" path))))
  9684. ((equal type "doi")
  9685. (browse-url
  9686. (org-link-escape-browser (concat org-doi-server-url path))))
  9687. ((equal type "message") (browse-url (concat type ":" path)))
  9688. ((equal type "shell")
  9689. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9690. (cmd path))
  9691. (if (or (and (org-string-nw-p
  9692. org-confirm-shell-link-not-regexp)
  9693. (string-match
  9694. org-confirm-shell-link-not-regexp cmd))
  9695. (not org-confirm-shell-link-function)
  9696. (funcall org-confirm-shell-link-function
  9697. (format "Execute \"%s\" in shell? "
  9698. (org-add-props cmd nil
  9699. 'face 'org-warning))))
  9700. (progn
  9701. (message "Executing %s" cmd)
  9702. (shell-command cmd buf)
  9703. (when (featurep 'midnight)
  9704. (setq clean-buffer-list-kill-buffer-names
  9705. (cons buf
  9706. clean-buffer-list-kill-buffer-names))))
  9707. (user-error "Abort"))))
  9708. ((equal type "elisp")
  9709. (let ((cmd path))
  9710. (if (or (and (org-string-nw-p
  9711. org-confirm-elisp-link-not-regexp)
  9712. (org-string-match-p
  9713. org-confirm-elisp-link-not-regexp cmd))
  9714. (not org-confirm-elisp-link-function)
  9715. (funcall org-confirm-elisp-link-function
  9716. (format "Execute \"%s\" as elisp? "
  9717. (org-add-props cmd nil
  9718. 'face 'org-warning))))
  9719. (message "%s => %s" cmd
  9720. (if (eq (string-to-char cmd) ?\()
  9721. (eval (read cmd))
  9722. (call-interactively (read cmd))))
  9723. (user-error "Abort"))))
  9724. ((equal type "id")
  9725. (require 'ord-id)
  9726. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9727. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9728. (unless (run-hook-with-args-until-success
  9729. 'org-open-link-functions path)
  9730. (if (not arg) (org-mark-ring-push)
  9731. (switch-to-buffer-other-window
  9732. (org-get-buffer-for-internal-link (current-buffer))))
  9733. (let ((cmd `(org-link-search
  9734. ,(if (member type '("custom-id" "coderef"))
  9735. (org-element-property :raw-link context)
  9736. path)
  9737. ,(cond ((equal arg '(4)) 'occur)
  9738. ((equal arg '(16)) 'org-occur))
  9739. ,(org-element-property :begin context))))
  9740. (condition-case nil
  9741. (let ((org-link-search-inhibit-query t))
  9742. (eval cmd))
  9743. (error (progn (widen) (eval cmd)))))))
  9744. (t (browse-url-at-point))))))
  9745. ;; On a footnote reference or at a footnote definition's label.
  9746. ((or (eq type 'footnote-reference)
  9747. (and (eq type 'footnote-definition)
  9748. (save-excursion
  9749. ;; Do not validate action when point is on the
  9750. ;; spaces right after the footnote label, in
  9751. ;; order to be on par with behaviour on links.
  9752. (skip-chars-forward " \t")
  9753. (let ((begin
  9754. (org-element-property :contents-begin context)))
  9755. (if begin (< (point) begin)
  9756. (= (org-element-property :post-affiliated context)
  9757. (line-beginning-position)))))))
  9758. (org-footnote-action))
  9759. (t (user-error "No link found")))))
  9760. (move-marker org-open-link-marker nil)
  9761. (run-hook-with-args 'org-follow-link-hook)))
  9762. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9763. "Offer links in the current entry and return the selected link.
  9764. If there is only one link, return it.
  9765. If NTH is an integer, return the NTH link found.
  9766. If ZERO is a string, check also this string for a link, and if
  9767. there is one, return it."
  9768. (with-current-buffer buffer
  9769. (save-excursion
  9770. (save-restriction
  9771. (widen)
  9772. (goto-char marker)
  9773. (let ((cnt ?0)
  9774. (in-emacs (if (integerp nth) nil nth))
  9775. have-zero end links link c)
  9776. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9777. (push (match-string 0 zero) links)
  9778. (setq cnt (1- cnt) have-zero t))
  9779. (save-excursion
  9780. (org-back-to-heading t)
  9781. (setq end (save-excursion (outline-next-heading) (point)))
  9782. (while (re-search-forward org-any-link-re end t)
  9783. (push (match-string 0) links))
  9784. (setq links (org-uniquify (reverse links))))
  9785. (cond
  9786. ((null links)
  9787. (message "No links"))
  9788. ((equal (length links) 1)
  9789. (setq link (car links)))
  9790. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9791. (setq link (nth (if have-zero nth (1- nth)) links)))
  9792. (t ; we have to select a link
  9793. (save-excursion
  9794. (save-window-excursion
  9795. (delete-other-windows)
  9796. (with-output-to-temp-buffer "*Select Link*"
  9797. (mapc (lambda (l)
  9798. (if (not (string-match org-bracket-link-regexp l))
  9799. (princ (format "[%c] %s\n" (incf cnt)
  9800. (org-remove-angle-brackets l)))
  9801. (if (match-end 3)
  9802. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9803. (match-string 3 l) (match-string 1 l)))
  9804. (princ (format "[%c] %s\n" (incf cnt)
  9805. (match-string 1 l))))))
  9806. links))
  9807. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9808. (message "Select link to open, RET to open all:")
  9809. (setq c (read-char-exclusive))
  9810. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9811. (when (equal c ?q) (user-error "Abort"))
  9812. (if (equal c ?\C-m)
  9813. (setq link links)
  9814. (setq nth (- c ?0))
  9815. (if have-zero (setq nth (1+ nth)))
  9816. (unless (and (integerp nth) (>= (length links) nth))
  9817. (user-error "Invalid link selection"))
  9818. (setq link (nth (1- nth) links)))))
  9819. (cons link end))))))
  9820. ;; TODO: These functions are deprecated since `org-open-at-point'
  9821. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9822. (defun org-open-file-with-system (path)
  9823. "Open file at PATH using the system way of opening it."
  9824. (org-open-file path 'system))
  9825. (defun org-open-file-with-emacs (path)
  9826. "Open file at PATH in Emacs."
  9827. (org-open-file path 'emacs))
  9828. ;;; File search
  9829. (defvar org-create-file-search-functions nil
  9830. "List of functions to construct the right search string for a file link.
  9831. These functions are called in turn with point at the location to
  9832. which the link should point.
  9833. A function in the hook should first test if it would like to
  9834. handle this file type, for example by checking the `major-mode'
  9835. or the file extension. If it decides not to handle this file, it
  9836. should just return nil to give other functions a chance. If it
  9837. does handle the file, it must return the search string to be used
  9838. when following the link. The search string will be part of the
  9839. file link, given after a double colon, and `org-open-at-point'
  9840. will automatically search for it. If special measures must be
  9841. taken to make the search successful, another function should be
  9842. added to the companion hook `org-execute-file-search-functions',
  9843. which see.
  9844. A function in this hook may also use `setq' to set the variable
  9845. `description' to provide a suggestion for the descriptive text to
  9846. be used for this link when it gets inserted into an Org-mode
  9847. buffer with \\[org-insert-link].")
  9848. (defvar org-execute-file-search-functions nil
  9849. "List of functions to execute a file search triggered by a link.
  9850. Functions added to this hook must accept a single argument, the
  9851. search string that was part of the file link, the part after the
  9852. double colon. The function must first check if it would like to
  9853. handle this search, for example by checking the `major-mode' or
  9854. the file extension. If it decides not to handle this search, it
  9855. should just return nil to give other functions a chance. If it
  9856. does handle the search, it must return a non-nil value to keep
  9857. other functions from trying.
  9858. Each function can access the current prefix argument through the
  9859. variable `current-prefix-arg'. Note that a single prefix is used
  9860. to force opening a link in Emacs, so it may be good to only use a
  9861. numeric or double prefix to guide the search function.
  9862. In case this is needed, a function in this hook can also restore
  9863. the window configuration before `org-open-at-point' was called using:
  9864. (set-window-configuration org-window-config-before-follow-link)")
  9865. (defun org-link-search (s &optional type avoid-pos stealth)
  9866. "Search for a link search option.
  9867. If S is surrounded by forward slashes, it is interpreted as a
  9868. regular expression. In org-mode files, this will create an `org-occur'
  9869. sparse tree. In ordinary files, `occur' will be used to list matches.
  9870. If the current buffer is in `dired-mode', grep will be used to search
  9871. in all files. If AVOID-POS is given, ignore matches near that position.
  9872. When optional argument STEALTH is non-nil, do not modify
  9873. visibility around point, thus ignoring
  9874. `org-show-hierarchy-above', `org-show-following-heading' and
  9875. `org-show-siblings' variables."
  9876. (let ((case-fold-search t)
  9877. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9878. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9879. (append '(("") (" ") ("\t") ("\n"))
  9880. org-emphasis-alist)
  9881. "\\|") "\\)"))
  9882. (pos (point))
  9883. (pre nil) (post nil)
  9884. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9885. (cond
  9886. ;; First check if there are any special search functions
  9887. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9888. ;; Now try the builtin stuff
  9889. ((and (equal (string-to-char s0) ?#)
  9890. (> (length s0) 1)
  9891. (save-excursion
  9892. (goto-char (point-min))
  9893. (and
  9894. (re-search-forward
  9895. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9896. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9897. (setq type 'dedicated
  9898. pos (match-beginning 0))))
  9899. ;; There is an exact target for this
  9900. (goto-char pos)
  9901. (org-back-to-heading t)))
  9902. ((save-excursion
  9903. (goto-char (point-min))
  9904. (and
  9905. (re-search-forward
  9906. (concat "<<" (regexp-quote s0) ">>") nil t)
  9907. (setq type 'dedicated
  9908. pos (match-beginning 0))))
  9909. ;; There is an exact target for this
  9910. (goto-char pos))
  9911. ((save-excursion
  9912. (goto-char (point-min))
  9913. (and
  9914. (re-search-forward
  9915. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9916. (setq type 'dedicated pos (match-beginning 0))))
  9917. ;; Found an element with a matching #+name affiliated keyword.
  9918. (goto-char pos))
  9919. ((and (string-match "^(\\(.*\\))$" s0)
  9920. (save-excursion
  9921. (goto-char (point-min))
  9922. (and
  9923. (re-search-forward
  9924. (concat "[^[]" (regexp-quote
  9925. (format org-coderef-label-format
  9926. (match-string 1 s0))))
  9927. nil t)
  9928. (setq type 'dedicated
  9929. pos (1+ (match-beginning 0))))))
  9930. ;; There is a coderef target for this
  9931. (goto-char pos))
  9932. ((string-match "^/\\(.*\\)/$" s)
  9933. ;; A regular expression
  9934. (cond
  9935. ((derived-mode-p 'org-mode)
  9936. (org-occur (match-string 1 s)))
  9937. (t (org-do-occur (match-string 1 s)))))
  9938. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9939. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9940. (goto-char (point-min))
  9941. (cond
  9942. ((let (case-fold-search)
  9943. (re-search-forward (format org-complex-heading-regexp-format
  9944. (regexp-quote s))
  9945. nil t))
  9946. ;; OK, found a match
  9947. (setq type 'dedicated)
  9948. (goto-char (match-beginning 0)))
  9949. ((and (not org-link-search-inhibit-query)
  9950. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9951. (y-or-n-p "No match - create this as a new heading? "))
  9952. (goto-char (point-max))
  9953. (or (bolp) (newline))
  9954. (insert "* " s "\n")
  9955. (beginning-of-line 0))
  9956. (t
  9957. (goto-char pos)
  9958. (error "No match"))))
  9959. (t
  9960. ;; A normal search string
  9961. (when (equal (string-to-char s) ?*)
  9962. ;; Anchor on headlines, post may include tags.
  9963. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9964. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9965. s (substring s 1)))
  9966. (remove-text-properties
  9967. 0 (length s)
  9968. '(face nil mouse-face nil keymap nil fontified nil) s)
  9969. ;; Make a series of regular expressions to find a match
  9970. (setq words (org-split-string s "[ \n\r\t]+")
  9971. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9972. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9973. "\\)" markers)
  9974. re2a_ (concat "\\(" (mapconcat 'downcase words
  9975. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9976. re2a (concat "[ \t\r\n]" re2a_)
  9977. re4_ (concat "\\(" (mapconcat 'downcase words
  9978. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9979. re4 (concat "[^a-zA-Z_]" re4_)
  9980. re1 (concat pre re2 post)
  9981. re3 (concat pre (if pre re4_ re4) post)
  9982. re5 (concat pre ".*" re4)
  9983. re2 (concat pre re2)
  9984. re2a (concat pre (if pre re2a_ re2a))
  9985. re4 (concat pre (if pre re4_ re4))
  9986. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9987. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9988. re5 "\\)"))
  9989. (cond
  9990. ((eq type 'org-occur) (org-occur reall))
  9991. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9992. (t (goto-char (point-min))
  9993. (setq type 'fuzzy)
  9994. (if (or (and (org-search-not-self 1 re0 nil t)
  9995. (setq type 'dedicated))
  9996. (org-search-not-self 1 re1 nil t)
  9997. (org-search-not-self 1 re2 nil t)
  9998. (org-search-not-self 1 re2a nil t)
  9999. (org-search-not-self 1 re3 nil t)
  10000. (org-search-not-self 1 re4 nil t)
  10001. (org-search-not-self 1 re5 nil t))
  10002. (goto-char (match-beginning 1))
  10003. (goto-char pos)
  10004. (error "No match"))))))
  10005. (and (derived-mode-p 'org-mode)
  10006. (not stealth)
  10007. (org-show-context 'link-search))
  10008. type))
  10009. (defun org-search-not-self (group &rest args)
  10010. "Execute `re-search-forward', but only accept matches that do not
  10011. enclose the position of `org-open-link-marker'."
  10012. (let ((m org-open-link-marker))
  10013. (catch 'exit
  10014. (while (apply 're-search-forward args)
  10015. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  10016. (goto-char (match-end group))
  10017. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  10018. (> (match-beginning 0) (marker-position m))
  10019. (< (match-end 0) (marker-position m)))
  10020. (save-match-data
  10021. (or (not (org-in-regexp
  10022. org-bracket-link-analytic-regexp 1))
  10023. (not (match-end 4)) ; no description
  10024. (and (<= (match-beginning 4) (point))
  10025. (>= (match-end 4) (point))))))
  10026. (throw 'exit (point))))))))
  10027. (defun org-get-buffer-for-internal-link (buffer)
  10028. "Return a buffer to be used for displaying the link target of internal links."
  10029. (cond
  10030. ((not org-display-internal-link-with-indirect-buffer)
  10031. buffer)
  10032. ((string-match "(Clone)$" (buffer-name buffer))
  10033. (message "Buffer is already a clone, not making another one")
  10034. ;; we also do not modify visibility in this case
  10035. buffer)
  10036. (t ; make a new indirect buffer for displaying the link
  10037. (let* ((bn (buffer-name buffer))
  10038. (ibn (concat bn "(Clone)"))
  10039. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10040. (with-current-buffer ib (org-overview))
  10041. ib))))
  10042. (defun org-do-occur (regexp &optional cleanup)
  10043. "Call the Emacs command `occur'.
  10044. If CLEANUP is non-nil, remove the printout of the regular expression
  10045. in the *Occur* buffer. This is useful if the regex is long and not useful
  10046. to read."
  10047. (occur regexp)
  10048. (when cleanup
  10049. (let ((cwin (selected-window)) win beg end)
  10050. (when (setq win (get-buffer-window "*Occur*"))
  10051. (select-window win))
  10052. (goto-char (point-min))
  10053. (when (re-search-forward "match[a-z]+" nil t)
  10054. (setq beg (match-end 0))
  10055. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10056. (setq end (1- (match-beginning 0)))))
  10057. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10058. (goto-char (point-min))
  10059. (select-window cwin))))
  10060. ;;; The mark ring for links jumps
  10061. (defvar org-mark-ring nil
  10062. "Mark ring for positions before jumps in Org-mode.")
  10063. (defvar org-mark-ring-last-goto nil
  10064. "Last position in the mark ring used to go back.")
  10065. ;; Fill and close the ring
  10066. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10067. (loop for i from 1 to org-mark-ring-length do
  10068. (push (make-marker) org-mark-ring))
  10069. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10070. org-mark-ring)
  10071. (defun org-mark-ring-push (&optional pos buffer)
  10072. "Put the current position or POS into the mark ring and rotate it."
  10073. (interactive)
  10074. (setq pos (or pos (point)))
  10075. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10076. (move-marker (car org-mark-ring)
  10077. (or pos (point))
  10078. (or buffer (current-buffer)))
  10079. (message "%s"
  10080. (substitute-command-keys
  10081. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10082. (defun org-mark-ring-goto (&optional n)
  10083. "Jump to the previous position in the mark ring.
  10084. With prefix arg N, jump back that many stored positions. When
  10085. called several times in succession, walk through the entire ring.
  10086. Org-mode commands jumping to a different position in the current file,
  10087. or to another Org-mode file, automatically push the old position
  10088. onto the ring."
  10089. (interactive "p")
  10090. (let (p m)
  10091. (if (eq last-command this-command)
  10092. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10093. (setq p org-mark-ring))
  10094. (setq org-mark-ring-last-goto p)
  10095. (setq m (car p))
  10096. (org-pop-to-buffer-same-window (marker-buffer m))
  10097. (goto-char m)
  10098. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10099. (defun org-remove-angle-brackets (s)
  10100. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10101. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10102. s)
  10103. (defun org-add-angle-brackets (s)
  10104. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10105. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10106. s)
  10107. (defun org-remove-double-quotes (s)
  10108. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10109. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10110. s)
  10111. ;;; Following specific links
  10112. (defun org-follow-timestamp-link ()
  10113. "Open an agenda view for the time-stamp date/range at point."
  10114. (cond
  10115. ((org-at-date-range-p t)
  10116. (let ((org-agenda-start-on-weekday)
  10117. (t1 (match-string 1))
  10118. (t2 (match-string 2)) tt1 tt2)
  10119. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10120. tt2 (time-to-days (org-time-string-to-time t2)))
  10121. (let ((org-agenda-buffer-tmp-name
  10122. (format "*Org Agenda(a:%s)"
  10123. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10124. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10125. ((org-at-timestamp-p t)
  10126. (let ((org-agenda-buffer-tmp-name
  10127. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10128. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10129. (substring (match-string 1) 0 10)))
  10130. 1)))
  10131. (t (error "This should not happen"))))
  10132. ;;; Following file links
  10133. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10134. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10135. (declare-function mailcap-mime-info
  10136. "mailcap" (string &optional request no-decode))
  10137. (defvar org-wait nil)
  10138. (defun org-open-file (path &optional in-emacs line search)
  10139. "Open the file at PATH.
  10140. First, this expands any special file name abbreviations. Then the
  10141. configuration variable `org-file-apps' is checked if it contains an
  10142. entry for this file type, and if yes, the corresponding command is launched.
  10143. If no application is found, Emacs simply visits the file.
  10144. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10145. With a double \\[universal-argument] \\[universal-argument] \
  10146. prefix arg, Org tries to avoid opening in Emacs
  10147. and to use an external application to visit the file.
  10148. Optional LINE specifies a line to go to, optional SEARCH a string
  10149. to search for. If LINE or SEARCH is given, the file will be
  10150. opened in Emacs, unless an entry from org-file-apps that makes
  10151. use of groups in a regexp matches.
  10152. If you want to change the way frames are used when following a
  10153. link, please customize `org-link-frame-setup'.
  10154. If the file does not exist, an error is thrown."
  10155. (let* ((file (if (equal path "")
  10156. buffer-file-name
  10157. (substitute-in-file-name (expand-file-name path))))
  10158. (file-apps (append org-file-apps (org-default-apps)))
  10159. (apps (org-remove-if
  10160. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10161. (apps-dlink (org-remove-if-not
  10162. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10163. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10164. (dirp (if remp nil (file-directory-p file)))
  10165. (file (if (and dirp org-open-directory-means-index-dot-org)
  10166. (concat (file-name-as-directory file) "index.org")
  10167. file))
  10168. (a-m-a-p (assq 'auto-mode apps))
  10169. (dfile (downcase file))
  10170. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10171. (link (cond ((and (eq line nil)
  10172. (eq search nil))
  10173. file)
  10174. (line
  10175. (concat file "::" (number-to-string line)))
  10176. (search
  10177. (concat file "::" search))))
  10178. (dlink (downcase link))
  10179. (old-buffer (current-buffer))
  10180. (old-pos (point))
  10181. (old-mode major-mode)
  10182. ext cmd link-match-data)
  10183. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10184. (setq ext (match-string 1 dfile))
  10185. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10186. (setq ext (match-string 1 dfile))))
  10187. (cond
  10188. ((member in-emacs '((16) system))
  10189. (setq cmd (cdr (assoc 'system apps))))
  10190. (in-emacs (setq cmd 'emacs))
  10191. (t
  10192. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10193. (and dirp (cdr (assoc 'directory apps)))
  10194. ; first, try matching against apps-dlink
  10195. ; if we get a match here, store the match data for later
  10196. (let ((match (assoc-default dlink apps-dlink
  10197. 'string-match)))
  10198. (if match
  10199. (progn (setq link-match-data (match-data))
  10200. match)
  10201. (progn (setq in-emacs (or in-emacs line search))
  10202. nil))) ; if we have no match in apps-dlink,
  10203. ; always open the file in emacs if line or search
  10204. ; is given (for backwards compatibility)
  10205. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10206. 'string-match)
  10207. (cdr (assoc ext apps))
  10208. (cdr (assoc t apps))))))
  10209. (when (eq cmd 'system)
  10210. (setq cmd (cdr (assoc 'system apps))))
  10211. (when (eq cmd 'default)
  10212. (setq cmd (cdr (assoc t apps))))
  10213. (when (eq cmd 'mailcap)
  10214. (require 'mailcap)
  10215. (mailcap-parse-mailcaps)
  10216. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10217. (command (mailcap-mime-info mime-type)))
  10218. (if (stringp command)
  10219. (setq cmd command)
  10220. (setq cmd 'emacs))))
  10221. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10222. (not (file-exists-p file))
  10223. (not org-open-non-existing-files))
  10224. (user-error "No such file: %s" file))
  10225. (cond
  10226. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10227. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10228. (while (string-match "['\"]%s['\"]" cmd)
  10229. (setq cmd (replace-match "%s" t t cmd)))
  10230. (while (string-match "%s" cmd)
  10231. (setq cmd (replace-match
  10232. (save-match-data
  10233. (shell-quote-argument
  10234. (convert-standard-filename file)))
  10235. t t cmd)))
  10236. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10237. (save-match-data
  10238. (let ((match-index 1)
  10239. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10240. (set-match-data link-match-data)
  10241. (while (<= match-index number-of-groups)
  10242. (let ((regex (concat "%" (number-to-string match-index)))
  10243. (replace-with (match-string match-index dlink)))
  10244. (while (string-match regex cmd)
  10245. (setq cmd (replace-match replace-with t t cmd))))
  10246. (setq match-index (+ match-index 1)))))
  10247. (save-window-excursion
  10248. (message "Running %s...done" cmd)
  10249. (start-process-shell-command cmd nil cmd)
  10250. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10251. ((or (stringp cmd)
  10252. (eq cmd 'emacs))
  10253. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10254. (widen)
  10255. (if line (progn (org-goto-line line)
  10256. (if (derived-mode-p 'org-mode)
  10257. (org-reveal)))
  10258. (if search (org-link-search search))))
  10259. ((consp cmd)
  10260. (let ((file (convert-standard-filename file)))
  10261. (save-match-data
  10262. (set-match-data link-match-data)
  10263. (eval cmd))))
  10264. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10265. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10266. (or (not (equal old-buffer (current-buffer)))
  10267. (not (equal old-pos (point))))
  10268. (org-mark-ring-push old-pos old-buffer))))
  10269. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10270. "This function returns non-nil if `entry' uses a regular
  10271. expression which should be matched against the whole link by
  10272. org-open-file.
  10273. It assumes that is the case when the entry uses a regular
  10274. expression which has at least one grouping construct and the
  10275. action is either a lisp form or a command string containing
  10276. '%1', i.e. using at least one subexpression match as a
  10277. parameter."
  10278. (let ((selector (car entry))
  10279. (action (cdr entry)))
  10280. (if (stringp selector)
  10281. (and (> (regexp-opt-depth selector) 0)
  10282. (or (and (stringp action)
  10283. (string-match "%[0-9]" action))
  10284. (consp action)))
  10285. nil)))
  10286. (defun org-default-apps ()
  10287. "Return the default applications for this operating system."
  10288. (cond
  10289. ((eq system-type 'darwin)
  10290. org-file-apps-defaults-macosx)
  10291. ((eq system-type 'windows-nt)
  10292. org-file-apps-defaults-windowsnt)
  10293. (t org-file-apps-defaults-gnu)))
  10294. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10295. "Convert extensions to regular expressions in the cars of LIST.
  10296. Also, weed out any non-string entries, because the return value is used
  10297. only for regexp matching.
  10298. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10299. point to the symbol `emacs', indicating that the file should
  10300. be opened in Emacs."
  10301. (append
  10302. (delq nil
  10303. (mapcar (lambda (x)
  10304. (if (not (stringp (car x)))
  10305. nil
  10306. (if (string-match "\\W" (car x))
  10307. x
  10308. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10309. list))
  10310. (if add-auto-mode
  10311. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10312. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10313. (defun org-file-remote-p (file)
  10314. "Test whether FILE specifies a location on a remote system.
  10315. Return non-nil if the location is indeed remote.
  10316. For example, the filename \"/user@host:/foo\" specifies a location
  10317. on the system \"/user@host:\"."
  10318. (cond ((fboundp 'file-remote-p)
  10319. (file-remote-p file))
  10320. ((fboundp 'tramp-handle-file-remote-p)
  10321. (tramp-handle-file-remote-p file))
  10322. ((and (boundp 'ange-ftp-name-format)
  10323. (string-match (car ange-ftp-name-format) file))
  10324. t)))
  10325. ;;;; Refiling
  10326. (defun org-get-org-file ()
  10327. "Read a filename, with default directory `org-directory'."
  10328. (let ((default (or org-default-notes-file remember-data-file)))
  10329. (read-file-name (format "File name [%s]: " default)
  10330. (file-name-as-directory org-directory)
  10331. default)))
  10332. (defun org-notes-order-reversed-p ()
  10333. "Check if the current file should receive notes in reversed order."
  10334. (cond
  10335. ((not org-reverse-note-order) nil)
  10336. ((eq t org-reverse-note-order) t)
  10337. ((not (listp org-reverse-note-order)) nil)
  10338. (t (catch 'exit
  10339. (let ((all org-reverse-note-order)
  10340. entry)
  10341. (while (setq entry (pop all))
  10342. (if (string-match (car entry) buffer-file-name)
  10343. (throw 'exit (cdr entry))))
  10344. nil)))))
  10345. (defvar org-refile-target-table nil
  10346. "The list of refile targets, created by `org-refile'.")
  10347. (defvar org-agenda-new-buffers nil
  10348. "Buffers created to visit agenda files.")
  10349. (defvar org-refile-cache nil
  10350. "Cache for refile targets.")
  10351. (defvar org-refile-markers nil
  10352. "All the markers used for caching refile locations.")
  10353. (defun org-refile-marker (pos)
  10354. "Get a new refile marker, but only if caching is in use."
  10355. (if (not org-refile-use-cache)
  10356. pos
  10357. (let ((m (make-marker)))
  10358. (move-marker m pos)
  10359. (push m org-refile-markers)
  10360. m)))
  10361. (defun org-refile-cache-clear ()
  10362. "Clear the refile cache and disable all the markers."
  10363. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10364. (setq org-refile-markers nil)
  10365. (setq org-refile-cache nil)
  10366. (message "Refile cache has been cleared"))
  10367. (defun org-refile-cache-check-set (set)
  10368. "Check if all the markers in the cache still have live buffers."
  10369. (let (marker)
  10370. (catch 'exit
  10371. (while (and set (setq marker (nth 3 (pop set))))
  10372. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10373. (when (and marker (null (marker-buffer marker)))
  10374. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10375. (sit-for 3)
  10376. (throw 'exit nil)))
  10377. t)))
  10378. (defun org-refile-cache-put (set &rest identifiers)
  10379. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10380. (let* ((key (sha1 (prin1-to-string identifiers)))
  10381. (entry (assoc key org-refile-cache)))
  10382. (if entry
  10383. (setcdr entry set)
  10384. (push (cons key set) org-refile-cache))))
  10385. (defun org-refile-cache-get (&rest identifiers)
  10386. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10387. (cond
  10388. ((not org-refile-cache) nil)
  10389. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10390. (t
  10391. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10392. org-refile-cache))))
  10393. (and set (org-refile-cache-check-set set) set)))))
  10394. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10395. "Produce a table with refile targets."
  10396. (let ((case-fold-search nil)
  10397. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10398. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10399. targets tgs txt re files f desc descre fast-path-p level pos0)
  10400. (message "Getting targets...")
  10401. (with-current-buffer (or default-buffer (current-buffer))
  10402. (while (setq entry (pop entries))
  10403. (setq files (car entry) desc (cdr entry))
  10404. (setq fast-path-p nil)
  10405. (cond
  10406. ((null files) (setq files (list (current-buffer))))
  10407. ((eq files 'org-agenda-files)
  10408. (setq files (org-agenda-files 'unrestricted)))
  10409. ((and (symbolp files) (fboundp files))
  10410. (setq files (funcall files)))
  10411. ((and (symbolp files) (boundp files))
  10412. (setq files (symbol-value files))))
  10413. (if (stringp files) (setq files (list files)))
  10414. (cond
  10415. ((eq (car desc) :tag)
  10416. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10417. ((eq (car desc) :todo)
  10418. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10419. ((eq (car desc) :regexp)
  10420. (setq descre (cdr desc)))
  10421. ((eq (car desc) :level)
  10422. (setq descre (concat "^\\*\\{" (number-to-string
  10423. (if org-odd-levels-only
  10424. (1- (* 2 (cdr desc)))
  10425. (cdr desc)))
  10426. "\\}[ \t]")))
  10427. ((eq (car desc) :maxlevel)
  10428. (setq fast-path-p t)
  10429. (setq descre (concat "^\\*\\{1," (number-to-string
  10430. (if org-odd-levels-only
  10431. (1- (* 2 (cdr desc)))
  10432. (cdr desc)))
  10433. "\\}[ \t]")))
  10434. (t (error "Bad refiling target description %s" desc)))
  10435. (while (setq f (pop files))
  10436. (with-current-buffer
  10437. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10438. (or
  10439. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10440. (progn
  10441. (if (bufferp f) (setq f (buffer-file-name
  10442. (buffer-base-buffer f))))
  10443. (setq f (and f (expand-file-name f)))
  10444. (if (eq org-refile-use-outline-path 'file)
  10445. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10446. (save-excursion
  10447. (save-restriction
  10448. (widen)
  10449. (goto-char (point-min))
  10450. (while (re-search-forward descre nil t)
  10451. (goto-char (setq pos0 (point-at-bol)))
  10452. (catch 'next
  10453. (when org-refile-target-verify-function
  10454. (save-match-data
  10455. (or (funcall org-refile-target-verify-function)
  10456. (throw 'next t))))
  10457. (when (and (looking-at org-complex-heading-regexp)
  10458. (not (member (match-string 4) excluded-entries))
  10459. (match-string 4))
  10460. (setq level (org-reduced-level
  10461. (- (match-end 1) (match-beginning 1)))
  10462. txt (org-link-display-format (match-string 4))
  10463. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10464. re (format org-complex-heading-regexp-format
  10465. (regexp-quote (match-string 4))))
  10466. (when org-refile-use-outline-path
  10467. (setq txt (mapconcat
  10468. 'org-protect-slash
  10469. (append
  10470. (if (eq org-refile-use-outline-path
  10471. 'file)
  10472. (list (file-name-nondirectory
  10473. (buffer-file-name
  10474. (buffer-base-buffer))))
  10475. (if (eq org-refile-use-outline-path
  10476. 'full-file-path)
  10477. (list (buffer-file-name
  10478. (buffer-base-buffer)))))
  10479. (org-get-outline-path fast-path-p
  10480. level txt)
  10481. (list txt))
  10482. "/")))
  10483. (push (list txt f re (org-refile-marker (point)))
  10484. tgs)))
  10485. (when (= (point) pos0)
  10486. ;; verification function has not moved point
  10487. (goto-char (point-at-eol))))))))
  10488. (when org-refile-use-cache
  10489. (org-refile-cache-put tgs (buffer-file-name) descre))
  10490. (setq targets (append tgs targets))))))
  10491. (message "Getting targets...done")
  10492. (nreverse targets)))
  10493. (defun org-protect-slash (s)
  10494. (while (string-match "/" s)
  10495. (setq s (replace-match "\\" t t s)))
  10496. s)
  10497. (defvar org-olpa (make-vector 20 nil))
  10498. (defun org-get-outline-path (&optional fastp level heading)
  10499. "Return the outline path to the current entry, as a list.
  10500. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10501. routine which makes outline path derivations for an entire file,
  10502. avoiding backtracing. Refile target collection makes use of that."
  10503. (if fastp
  10504. (progn
  10505. (if (> level 19)
  10506. (error "Outline path failure, more than 19 levels"))
  10507. (loop for i from level upto 19 do
  10508. (aset org-olpa i nil))
  10509. (prog1
  10510. (delq nil (append org-olpa nil))
  10511. (aset org-olpa level heading)))
  10512. (let (rtn case-fold-search)
  10513. (save-excursion
  10514. (save-restriction
  10515. (widen)
  10516. (while (org-up-heading-safe)
  10517. (when (looking-at org-complex-heading-regexp)
  10518. (push (org-trim
  10519. (replace-regexp-in-string
  10520. ;; Remove statistical/checkboxes cookies
  10521. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10522. (org-match-string-no-properties 4)))
  10523. rtn)))
  10524. rtn)))))
  10525. (defun org-format-outline-path (path &optional width prefix separator)
  10526. "Format the outline path PATH for display.
  10527. WIDTH is the maximum number of characters that is available.
  10528. PREFIX is a prefix to be included in the returned string,
  10529. such as the file name.
  10530. SEPARATOR is inserted between the different parts of the path,
  10531. the default is \"/\"."
  10532. (setq width (or width 79))
  10533. (if prefix (setq width (- width (length prefix))))
  10534. (if (not path)
  10535. (or prefix "")
  10536. (let* ((nsteps (length path))
  10537. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10538. (maxwidth (if (<= total-width width)
  10539. 10000 ;; everything fits
  10540. ;; we need to shorten the level headings
  10541. (/ (- width nsteps) nsteps)))
  10542. (org-odd-levels-only nil)
  10543. (n 0)
  10544. (total (1+ (length prefix))))
  10545. (setq maxwidth (max maxwidth 10))
  10546. (concat prefix
  10547. (if prefix (or separator "/"))
  10548. (mapconcat
  10549. (lambda (h)
  10550. (setq n (1+ n))
  10551. (if (and (= n nsteps) (< maxwidth 10000))
  10552. (setq maxwidth (- total-width total)))
  10553. (if (< (length h) maxwidth)
  10554. (progn (setq total (+ total (length h) 1)) h)
  10555. (setq h (substring h 0 (- maxwidth 2))
  10556. total (+ total maxwidth 1))
  10557. (if (string-match "[ \t]+\\'" h)
  10558. (setq h (substring h 0 (match-beginning 0))))
  10559. (setq h (concat h "..")))
  10560. (org-add-props h nil 'face
  10561. (nth (% (1- n) org-n-level-faces)
  10562. org-level-faces))
  10563. h)
  10564. path (or separator "/"))))))
  10565. (defun org-display-outline-path (&optional file current separator just-return-string)
  10566. "Display the current outline path in the echo area.
  10567. If FILE is non-nil, prepend the output with the file name.
  10568. If CURRENT is non-nil, append the current heading to the output.
  10569. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10570. the different parts of the path and defaults to \"/\".
  10571. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10572. (interactive "P")
  10573. (let* (case-fold-search
  10574. (bfn (buffer-file-name (buffer-base-buffer)))
  10575. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10576. res)
  10577. (if current (setq path (append path
  10578. (save-excursion
  10579. (org-back-to-heading t)
  10580. (if (looking-at org-complex-heading-regexp)
  10581. (list (match-string 4)))))))
  10582. (setq res
  10583. (org-format-outline-path
  10584. path
  10585. (1- (frame-width))
  10586. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10587. separator))
  10588. (if just-return-string
  10589. (org-no-properties res)
  10590. (org-unlogged-message "%s" res))))
  10591. (defvar org-refile-history nil
  10592. "History for refiling operations.")
  10593. (defvar org-after-refile-insert-hook nil
  10594. "Hook run after `org-refile' has inserted its stuff at the new location.
  10595. Note that this is still *before* the stuff will be removed from
  10596. the *old* location.")
  10597. (defvar org-capture-last-stored-marker)
  10598. (defvar org-refile-keep nil
  10599. "Non-nil means `org-refile' will copy instead of refile.")
  10600. (defun org-copy ()
  10601. "Like `org-refile', but copy."
  10602. (interactive)
  10603. (let ((org-refile-keep t))
  10604. (funcall 'org-refile nil nil nil "Copy")))
  10605. (defun org-refile (&optional arg default-buffer rfloc msg)
  10606. "Move the entry or entries at point to another heading.
  10607. The list of target headings is compiled using the information in
  10608. `org-refile-targets', which see.
  10609. At the target location, the entry is filed as a subitem of the
  10610. target heading. Depending on `org-reverse-note-order', the new
  10611. subitem will either be the first or the last subitem.
  10612. If there is an active region, all entries in that region will be
  10613. refiled. However, the region must fulfill the requirement that
  10614. the first heading sets the top-level of the moved text.
  10615. With prefix arg ARG, the command will only visit the target
  10616. location and not actually move anything.
  10617. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10618. refiling operation has put the subtree.
  10619. With a numeric prefix argument of `2', refile to the running clock.
  10620. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10621. being set to `t' and copy to the target location, don't move it.
  10622. Beware that keeping refiled entries may result in duplicated ID
  10623. properties.
  10624. RFLOC can be a refile location obtained in a different way.
  10625. MSG is a string to replace \"Refile\" in the default prompt with
  10626. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10627. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10628. If you are using target caching (see `org-refile-use-cache'), you
  10629. have to clear the target cache in order to find new targets.
  10630. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10631. prefix argument (`C-u C-u C-u C-c C-w')."
  10632. (interactive "P")
  10633. (if (member arg '(0 (64)))
  10634. (org-refile-cache-clear)
  10635. (let* ((actionmsg (cond (msg msg)
  10636. ((equal arg 3) "Refile (and keep)")
  10637. (t "Refile")))
  10638. (cbuf (current-buffer))
  10639. (regionp (org-region-active-p))
  10640. (region-start (and regionp (region-beginning)))
  10641. (region-end (and regionp (region-end)))
  10642. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10643. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10644. pos it nbuf file re level reversed)
  10645. (setq last-command nil)
  10646. (when regionp
  10647. (goto-char region-start)
  10648. (or (bolp) (goto-char (point-at-bol)))
  10649. (setq region-start (point))
  10650. (unless (or (org-kill-is-subtree-p
  10651. (buffer-substring region-start region-end))
  10652. (prog1 org-refile-active-region-within-subtree
  10653. (let ((s (point-at-eol)))
  10654. (org-toggle-heading)
  10655. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10656. (user-error "The region is not a (sequence of) subtree(s)")))
  10657. (if (equal arg '(16))
  10658. (org-refile-goto-last-stored)
  10659. (when (or
  10660. (and (equal arg 2)
  10661. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10662. (prog1
  10663. (setq it (list (or org-clock-heading "running clock")
  10664. (buffer-file-name
  10665. (marker-buffer org-clock-hd-marker))
  10666. ""
  10667. (marker-position org-clock-hd-marker)))
  10668. (setq arg nil)))
  10669. (setq it (or rfloc
  10670. (let (heading-text)
  10671. (save-excursion
  10672. (unless (and arg (listp arg))
  10673. (org-back-to-heading t)
  10674. (setq heading-text
  10675. (replace-regexp-in-string
  10676. org-bracket-link-regexp
  10677. "\\3"
  10678. (nth 4 (org-heading-components)))))
  10679. (org-refile-get-location
  10680. (cond ((and arg (listp arg)) "Goto")
  10681. (regionp (concat actionmsg " region to"))
  10682. (t (concat actionmsg " subtree \""
  10683. heading-text "\" to")))
  10684. default-buffer
  10685. (and (not (equal '(4) arg))
  10686. org-refile-allow-creating-parent-nodes)
  10687. arg))))))
  10688. (setq file (nth 1 it)
  10689. re (nth 2 it)
  10690. pos (nth 3 it))
  10691. (if (and (not arg)
  10692. pos
  10693. (equal (buffer-file-name) file)
  10694. (if regionp
  10695. (and (>= pos region-start)
  10696. (<= pos region-end))
  10697. (and (>= pos (point))
  10698. (< pos (save-excursion
  10699. (org-end-of-subtree t t))))))
  10700. (error "Cannot refile to position inside the tree or region"))
  10701. (setq nbuf (or (find-buffer-visiting file)
  10702. (find-file-noselect file)))
  10703. (if (and arg (not (equal arg 3)))
  10704. (progn
  10705. (org-pop-to-buffer-same-window nbuf)
  10706. (goto-char pos)
  10707. (org-show-context 'org-goto))
  10708. (if regionp
  10709. (progn
  10710. (org-kill-new (buffer-substring region-start region-end))
  10711. (org-save-markers-in-region region-start region-end))
  10712. (org-copy-subtree 1 nil t))
  10713. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10714. (find-file-noselect file)))
  10715. (setq reversed (org-notes-order-reversed-p))
  10716. (save-excursion
  10717. (save-restriction
  10718. (widen)
  10719. (if pos
  10720. (progn
  10721. (goto-char pos)
  10722. (looking-at org-outline-regexp)
  10723. (setq level (org-get-valid-level (funcall outline-level) 1))
  10724. (goto-char
  10725. (if reversed
  10726. (or (outline-next-heading) (point-max))
  10727. (or (save-excursion (org-get-next-sibling))
  10728. (org-end-of-subtree t t)
  10729. (point-max)))))
  10730. (setq level 1)
  10731. (if (not reversed)
  10732. (goto-char (point-max))
  10733. (goto-char (point-min))
  10734. (or (outline-next-heading) (goto-char (point-max)))))
  10735. (if (not (bolp)) (newline))
  10736. (org-paste-subtree level nil nil t)
  10737. (when org-log-refile
  10738. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10739. (unless (eq org-log-refile 'note)
  10740. (save-excursion (org-add-log-note))))
  10741. (and org-auto-align-tags
  10742. (let ((org-loop-over-headlines-in-active-region nil))
  10743. (org-set-tags nil t)))
  10744. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10745. :last-refile)))
  10746. (when bookmark-name
  10747. (with-demoted-errors
  10748. (bookmark-set bookmark-name))))
  10749. ;; If we are refiling for capture, make sure that the
  10750. ;; last-capture pointers point here
  10751. (when (org-bound-and-true-p org-refile-for-capture)
  10752. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10753. :last-capture-marker)))
  10754. (when bookmark-name
  10755. (with-demoted-errors
  10756. (bookmark-set bookmark-name))))
  10757. (move-marker org-capture-last-stored-marker (point)))
  10758. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10759. (run-hooks 'org-after-refile-insert-hook))))
  10760. (unless org-refile-keep
  10761. (if regionp
  10762. (delete-region (point) (+ (point) (- region-end region-start)))
  10763. (delete-region
  10764. (and (org-back-to-heading t) (point))
  10765. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10766. (when (featurep 'org-inlinetask)
  10767. (org-inlinetask-remove-END-maybe))
  10768. (setq org-markers-to-move nil)
  10769. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10770. (defun org-refile-goto-last-stored ()
  10771. "Go to the location where the last refile was stored."
  10772. (interactive)
  10773. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10774. (message "This is the location of the last refile"))
  10775. (defun org-refile--get-location (refloc tbl)
  10776. "When user refile to REFLOC, find the associated target in TBL.
  10777. Also check `org-refile-target-table'."
  10778. (car (delq
  10779. nil
  10780. (mapcar
  10781. (lambda (r) (or (assoc r tbl)
  10782. (assoc r org-refile-target-table)))
  10783. (list (replace-regexp-in-string "/$" "" refloc)
  10784. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10785. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10786. no-exclude)
  10787. "Prompt the user for a refile location, using PROMPT.
  10788. PROMPT should not be suffixed with a colon and a space, because
  10789. this function appends the default value from
  10790. `org-refile-history' automatically, if that is not empty.
  10791. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10792. this is used for the GOTO interface."
  10793. (let ((org-refile-targets org-refile-targets)
  10794. (org-refile-use-outline-path org-refile-use-outline-path)
  10795. excluded-entries)
  10796. (when (and (derived-mode-p 'org-mode)
  10797. (not org-refile-use-cache)
  10798. (not no-exclude))
  10799. (org-map-tree
  10800. (lambda()
  10801. (setq excluded-entries
  10802. (append excluded-entries (list (org-get-heading t t)))))))
  10803. (setq org-refile-target-table
  10804. (org-refile-get-targets default-buffer excluded-entries)))
  10805. (unless org-refile-target-table
  10806. (user-error "No refile targets"))
  10807. (let* ((cbuf (current-buffer))
  10808. (partial-completion-mode nil)
  10809. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10810. (cfunc (if (and org-refile-use-outline-path
  10811. org-outline-path-complete-in-steps)
  10812. 'org-olpath-completing-read
  10813. 'org-icompleting-read))
  10814. (extra (if org-refile-use-outline-path "/" ""))
  10815. (cbnex (concat (buffer-name) extra))
  10816. (filename (and cfn (expand-file-name cfn)))
  10817. (tbl (mapcar
  10818. (lambda (x)
  10819. (if (and (not (member org-refile-use-outline-path
  10820. '(file full-file-path)))
  10821. (not (equal filename (nth 1 x))))
  10822. (cons (concat (car x) extra " ("
  10823. (file-name-nondirectory (nth 1 x)) ")")
  10824. (cdr x))
  10825. (cons (concat (car x) extra) (cdr x))))
  10826. org-refile-target-table))
  10827. (completion-ignore-case t)
  10828. cdef
  10829. (prompt (concat prompt
  10830. (or (and (car org-refile-history)
  10831. (concat " (default " (car org-refile-history) ")"))
  10832. (and (assoc cbnex tbl) (setq cdef cbnex)
  10833. (concat " (default " cbnex ")"))) ": "))
  10834. pa answ parent-target child parent old-hist)
  10835. (setq old-hist org-refile-history)
  10836. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10837. nil 'org-refile-history (or cdef (car org-refile-history))))
  10838. (if (setq pa (org-refile--get-location answ tbl))
  10839. (progn
  10840. (org-refile-check-position pa)
  10841. (when (or (not org-refile-history)
  10842. (not (eq old-hist org-refile-history))
  10843. (not (equal (car pa) (car org-refile-history))))
  10844. (setq org-refile-history
  10845. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10846. org-refile-history
  10847. (cdr org-refile-history))))
  10848. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10849. (pop org-refile-history)))
  10850. pa)
  10851. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10852. (progn
  10853. (setq parent (match-string 1 answ)
  10854. child (match-string 2 answ))
  10855. (setq parent-target (org-refile--get-location parent tbl))
  10856. (when (and parent-target
  10857. (or (eq new-nodes t)
  10858. (and (eq new-nodes 'confirm)
  10859. (y-or-n-p (format "Create new node \"%s\"? "
  10860. child)))))
  10861. (org-refile-new-child parent-target child)))
  10862. (user-error "Invalid target location")))))
  10863. (declare-function org-string-nw-p "org-macs" (s))
  10864. (defun org-refile-check-position (refile-pointer)
  10865. "Check if the refile pointer matches the headline to which it points."
  10866. (let* ((file (nth 1 refile-pointer))
  10867. (re (nth 2 refile-pointer))
  10868. (pos (nth 3 refile-pointer))
  10869. buffer)
  10870. (if (and (not (markerp pos)) (not file))
  10871. (user-error "Please indicate a target file in the refile path")
  10872. (when (org-string-nw-p re)
  10873. (setq buffer (if (markerp pos)
  10874. (marker-buffer pos)
  10875. (or (find-buffer-visiting file)
  10876. (find-file-noselect file))))
  10877. (with-current-buffer buffer
  10878. (save-excursion
  10879. (save-restriction
  10880. (widen)
  10881. (goto-char pos)
  10882. (beginning-of-line 1)
  10883. (unless (org-looking-at-p re)
  10884. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10885. (defun org-refile-new-child (parent-target child)
  10886. "Use refile target PARENT-TARGET to add new CHILD below it."
  10887. (unless parent-target
  10888. (error "Cannot find parent for new node"))
  10889. (let ((file (nth 1 parent-target))
  10890. (pos (nth 3 parent-target))
  10891. level)
  10892. (with-current-buffer (or (find-buffer-visiting file)
  10893. (find-file-noselect file))
  10894. (save-excursion
  10895. (save-restriction
  10896. (widen)
  10897. (if pos
  10898. (goto-char pos)
  10899. (goto-char (point-max))
  10900. (if (not (bolp)) (newline)))
  10901. (when (looking-at org-outline-regexp)
  10902. (setq level (funcall outline-level))
  10903. (org-end-of-subtree t t))
  10904. (org-back-over-empty-lines)
  10905. (insert "\n" (make-string
  10906. (if pos (org-get-valid-level level 1) 1) ?*)
  10907. " " child "\n")
  10908. (beginning-of-line 0)
  10909. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10910. (defun org-olpath-completing-read (prompt collection &rest args)
  10911. "Read an outline path like a file name."
  10912. (let ((thetable collection)
  10913. (org-completion-use-ido nil) ; does not work with ido.
  10914. (org-completion-use-iswitchb nil)) ; or iswitchb
  10915. (apply
  10916. 'org-icompleting-read prompt
  10917. (lambda (string predicate &optional flag)
  10918. (let (rtn r f (l (length string)))
  10919. (cond
  10920. ((eq flag nil)
  10921. ;; try completion
  10922. (try-completion string thetable))
  10923. ((eq flag t)
  10924. ;; all-completions
  10925. (setq rtn (all-completions string thetable predicate))
  10926. (mapcar
  10927. (lambda (x)
  10928. (setq r (substring x l))
  10929. (if (string-match " ([^)]*)$" x)
  10930. (setq f (match-string 0 x))
  10931. (setq f ""))
  10932. (if (string-match "/" r)
  10933. (concat string (substring r 0 (match-end 0)) f)
  10934. x))
  10935. rtn))
  10936. ((eq flag 'lambda)
  10937. ;; exact match?
  10938. (assoc string thetable)))))
  10939. args)))
  10940. ;;;; Dynamic blocks
  10941. (defun org-find-dblock (name)
  10942. "Find the first dynamic block with name NAME in the buffer.
  10943. If not found, stay at current position and return nil."
  10944. (let ((case-fold-search t) pos)
  10945. (save-excursion
  10946. (goto-char (point-min))
  10947. (setq pos (and (re-search-forward
  10948. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10949. (match-beginning 0))))
  10950. (if pos (goto-char pos))
  10951. pos))
  10952. (defun org-create-dblock (plist)
  10953. "Create a dynamic block section, with parameters taken from PLIST.
  10954. PLIST must contain a :name entry which is used as the name of the block."
  10955. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10956. (end-of-line 1)
  10957. (newline))
  10958. (let ((col (current-column))
  10959. (name (plist-get plist :name)))
  10960. (insert "#+BEGIN: " name)
  10961. (while plist
  10962. (if (eq (car plist) :name)
  10963. (setq plist (cddr plist))
  10964. (insert " " (prin1-to-string (pop plist)))))
  10965. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10966. (beginning-of-line -2)))
  10967. (defun org-prepare-dblock ()
  10968. "Prepare dynamic block for refresh.
  10969. This empties the block, puts the cursor at the insert position and returns
  10970. the property list including an extra property :name with the block name."
  10971. (unless (looking-at org-dblock-start-re)
  10972. (user-error "Not at a dynamic block"))
  10973. (let* ((begdel (1+ (match-end 0)))
  10974. (name (org-no-properties (match-string 1)))
  10975. (params (append (list :name name)
  10976. (read (concat "(" (match-string 3) ")")))))
  10977. (save-excursion
  10978. (beginning-of-line 1)
  10979. (skip-chars-forward " \t")
  10980. (setq params (plist-put params :indentation-column (current-column))))
  10981. (unless (re-search-forward org-dblock-end-re nil t)
  10982. (error "Dynamic block not terminated"))
  10983. (setq params
  10984. (append params
  10985. (list :content (buffer-substring
  10986. begdel (match-beginning 0)))))
  10987. (delete-region begdel (match-beginning 0))
  10988. (goto-char begdel)
  10989. (open-line 1)
  10990. params))
  10991. (defun org-map-dblocks (&optional command)
  10992. "Apply COMMAND to all dynamic blocks in the current buffer.
  10993. If COMMAND is not given, use `org-update-dblock'."
  10994. (let ((cmd (or command 'org-update-dblock)))
  10995. (save-excursion
  10996. (goto-char (point-min))
  10997. (while (re-search-forward org-dblock-start-re nil t)
  10998. (goto-char (match-beginning 0))
  10999. (save-excursion
  11000. (condition-case nil
  11001. (funcall cmd)
  11002. (error (message "Error during update of dynamic block"))))
  11003. (unless (re-search-forward org-dblock-end-re nil t)
  11004. (error "Dynamic block not terminated"))))))
  11005. (defun org-dblock-update (&optional arg)
  11006. "User command for updating dynamic blocks.
  11007. Update the dynamic block at point. With prefix ARG, update all dynamic
  11008. blocks in the buffer."
  11009. (interactive "P")
  11010. (if arg
  11011. (org-update-all-dblocks)
  11012. (or (looking-at org-dblock-start-re)
  11013. (org-beginning-of-dblock))
  11014. (org-update-dblock)))
  11015. (defun org-update-dblock ()
  11016. "Update the dynamic block at point.
  11017. This means to empty the block, parse for parameters and then call
  11018. the correct writing function."
  11019. (interactive)
  11020. (save-excursion
  11021. (let* ((win (selected-window))
  11022. (pos (point))
  11023. (line (org-current-line))
  11024. (params (org-prepare-dblock))
  11025. (name (plist-get params :name))
  11026. (indent (plist-get params :indentation-column))
  11027. (cmd (intern (concat "org-dblock-write:" name))))
  11028. (message "Updating dynamic block `%s' at line %d..." name line)
  11029. (funcall cmd params)
  11030. (message "Updating dynamic block `%s' at line %d...done" name line)
  11031. (goto-char pos)
  11032. (when (and indent (> indent 0))
  11033. (setq indent (make-string indent ?\ ))
  11034. (save-excursion
  11035. (select-window win)
  11036. (org-beginning-of-dblock)
  11037. (forward-line 1)
  11038. (while (not (looking-at org-dblock-end-re))
  11039. (insert indent)
  11040. (beginning-of-line 2))
  11041. (when (looking-at org-dblock-end-re)
  11042. (and (looking-at "[ \t]+")
  11043. (replace-match ""))
  11044. (insert indent)))))))
  11045. (defun org-beginning-of-dblock ()
  11046. "Find the beginning of the dynamic block at point.
  11047. Error if there is no such block at point."
  11048. (let ((pos (point))
  11049. beg)
  11050. (end-of-line 1)
  11051. (if (and (re-search-backward org-dblock-start-re nil t)
  11052. (setq beg (match-beginning 0))
  11053. (re-search-forward org-dblock-end-re nil t)
  11054. (> (match-end 0) pos))
  11055. (goto-char beg)
  11056. (goto-char pos)
  11057. (error "Not in a dynamic block"))))
  11058. (defun org-update-all-dblocks ()
  11059. "Update all dynamic blocks in the buffer.
  11060. This function can be used in a hook."
  11061. (interactive)
  11062. (when (derived-mode-p 'org-mode)
  11063. (org-map-dblocks 'org-update-dblock)))
  11064. ;;;; Completion
  11065. (declare-function org-export-backend-name "org-export" (cl-x))
  11066. (declare-function org-export-backend-options "org-export" (cl-x))
  11067. (defun org-get-export-keywords ()
  11068. "Return a list of all currently understood export keywords.
  11069. Export keywords include options, block names, attributes and
  11070. keywords relative to each registered export back-end."
  11071. (let (keywords)
  11072. (dolist (backend
  11073. (org-bound-and-true-p org-export--registered-backends)
  11074. (delq nil keywords))
  11075. ;; Back-end name (for keywords, like #+LATEX:)
  11076. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11077. (dolist (option-entry (org-export-backend-options backend))
  11078. ;; Back-end options.
  11079. (push (nth 1 option-entry) keywords)))))
  11080. (defconst org-options-keywords
  11081. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11082. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11083. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11084. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11085. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11086. (defcustom org-structure-template-alist
  11087. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11088. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11089. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11090. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11091. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11092. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11093. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX")
  11094. ("L" "#+LaTeX: ")
  11095. ("h" "#+BEGIN_HTML\n?\n#+END_HTML")
  11096. ("H" "#+HTML: ")
  11097. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  11098. ("A" "#+ASCII: ")
  11099. ("i" "#+INDEX: ?")
  11100. ("I" "#+INCLUDE: %file ?"))
  11101. "Structure completion elements.
  11102. This is a list of abbreviation keys and values. The value gets inserted
  11103. if you type `<' followed by the key and then press the completion key,
  11104. usually `TAB'. %file will be replaced by a file name after prompting
  11105. for the file using completion. The cursor will be placed at the position
  11106. of the `?` in the template.
  11107. There are two templates for each key, the first uses the original Org syntax,
  11108. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11109. the default when the /org-mtags.el/ module has been loaded. See also the
  11110. variable `org-mtags-prefer-muse-templates'."
  11111. :group 'org-completion
  11112. :type '(repeat
  11113. (list
  11114. (string :tag "Key")
  11115. (string :tag "Template")))
  11116. :version 24.4
  11117. :package-version '(Org . "8.3"))
  11118. (defun org-try-structure-completion ()
  11119. "Try to complete a structure template before point.
  11120. This looks for strings like \"<e\" on an otherwise empty line and
  11121. expands them."
  11122. (let ((l (buffer-substring (point-at-bol) (point)))
  11123. a)
  11124. (when (and (looking-at "[ \t]*$")
  11125. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11126. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11127. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11128. (match-beginning 1)) a)
  11129. t)))
  11130. (defun org-complete-expand-structure-template (start cell)
  11131. "Expand a structure template."
  11132. (let ((rpl (nth 1 cell))
  11133. (ind ""))
  11134. (delete-region start (point))
  11135. (when (string-match "\\`[ \t]*#\\+" rpl)
  11136. (cond
  11137. ((bolp))
  11138. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11139. (setq ind (buffer-substring (point-at-bol) (point))))
  11140. (t (newline))))
  11141. (setq start (point))
  11142. (if (string-match "%file" rpl)
  11143. (setq rpl (replace-match
  11144. (concat
  11145. "\""
  11146. (save-match-data
  11147. (abbreviate-file-name (read-file-name "Include file: ")))
  11148. "\"")
  11149. t t rpl)))
  11150. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11151. (concat "\n" ind)))
  11152. (insert rpl)
  11153. (if (re-search-backward "\\?" start t) (delete-char 1))))
  11154. ;;;; TODO, DEADLINE, Comments
  11155. (defun org-toggle-comment ()
  11156. "Change the COMMENT state of an entry."
  11157. (interactive)
  11158. (save-excursion
  11159. (org-back-to-heading)
  11160. (looking-at org-complex-heading-regexp)
  11161. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11162. (skip-chars-forward " \t")
  11163. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11164. (if (org-in-commented-heading-p t)
  11165. (delete-region (point)
  11166. (progn (search-forward " " (line-end-position) 'move)
  11167. (skip-chars-forward " \t")
  11168. (point)))
  11169. (insert org-comment-string)
  11170. (unless (eolp) (insert " ")))))
  11171. (defvar org-last-todo-state-is-todo nil
  11172. "This is non-nil when the last TODO state change led to a TODO state.
  11173. If the last change removed the TODO tag or switched to DONE, then
  11174. this is nil.")
  11175. (defvar org-setting-tags nil) ; dynamically skipped
  11176. (defvar org-todo-setup-filter-hook nil
  11177. "Hook for functions that pre-filter todo specs.
  11178. Each function takes a todo spec and returns either nil or the spec
  11179. transformed into canonical form." )
  11180. (defvar org-todo-get-default-hook nil
  11181. "Hook for functions that get a default item for todo.
  11182. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11183. nil or a string to be used for the todo mark." )
  11184. (defvar org-agenda-headline-snapshot-before-repeat)
  11185. (defun org-current-effective-time ()
  11186. "Return current time adjusted for `org-extend-today-until' variable."
  11187. (let* ((ct (org-current-time))
  11188. (dct (decode-time ct))
  11189. (ct1
  11190. (cond
  11191. (org-use-last-clock-out-time-as-effective-time
  11192. (or (org-clock-get-last-clock-out-time) ct))
  11193. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11194. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11195. (t ct))))
  11196. ct1))
  11197. (defun org-todo-yesterday (&optional arg)
  11198. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11199. (interactive "P")
  11200. (if (eq major-mode 'org-agenda-mode)
  11201. (apply 'org-agenda-todo-yesterday arg)
  11202. (let* ((hour (third (decode-time
  11203. (org-current-time))))
  11204. (org-extend-today-until (1+ hour)))
  11205. (org-todo arg))))
  11206. (defvar org-block-entry-blocking ""
  11207. "First entry preventing the TODO state change.")
  11208. (defun org-cancel-repeater ()
  11209. "Cancel a repeater by setting its numeric value to zero."
  11210. (interactive)
  11211. (save-excursion
  11212. (org-back-to-heading t)
  11213. (let ((bound1 (point))
  11214. (bound0 (save-excursion (outline-next-heading) (point))))
  11215. (when (re-search-forward
  11216. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11217. org-deadline-time-regexp "\\)\\|\\("
  11218. org-ts-regexp "\\)")
  11219. bound0 t)
  11220. (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
  11221. (replace-match "0" t nil nil 1))))))
  11222. (defun org-todo (&optional arg)
  11223. "Change the TODO state of an item.
  11224. The state of an item is given by a keyword at the start of the heading,
  11225. like
  11226. *** TODO Write paper
  11227. *** DONE Call mom
  11228. The different keywords are specified in the variable `org-todo-keywords'.
  11229. By default the available states are \"TODO\" and \"DONE\".
  11230. So for this example: when the item starts with TODO, it is changed to DONE.
  11231. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11232. DONE are present, add TODO at the beginning of the heading.
  11233. With \\[universal-argument] prefix arg, use completion to determine the new \
  11234. state.
  11235. With numeric prefix arg, switch to that state.
  11236. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11237. keywords (nextset).
  11238. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11239. With a numeric prefix arg of 0, inhibit note taking for the change.
  11240. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11241. When called through ELisp, arg is also interpreted in the following way:
  11242. 'none -> empty state
  11243. \"\"(empty string) -> switch to empty state
  11244. 'done -> switch to DONE
  11245. 'nextset -> switch to the next set of keywords
  11246. 'previousset -> switch to the previous set of keywords
  11247. \"WAITING\" -> switch to the specified keyword, but only if it
  11248. really is a member of `org-todo-keywords'."
  11249. (interactive "P")
  11250. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11251. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11252. 'region-start-level 'region))
  11253. org-loop-over-headlines-in-active-region)
  11254. (org-map-entries
  11255. `(org-todo ,arg)
  11256. org-loop-over-headlines-in-active-region
  11257. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11258. (if (equal arg '(16)) (setq arg 'nextset))
  11259. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11260. (let ((org-blocker-hook org-blocker-hook)
  11261. commentp
  11262. case-fold-search)
  11263. (when (equal arg '(64))
  11264. (setq arg nil org-blocker-hook nil))
  11265. (when (and org-blocker-hook
  11266. (or org-inhibit-blocking
  11267. (org-entry-get nil "NOBLOCKING")))
  11268. (setq org-blocker-hook nil))
  11269. (save-excursion
  11270. (catch 'exit
  11271. (org-back-to-heading t)
  11272. (when (org-in-commented-heading-p t)
  11273. (org-toggle-comment)
  11274. (setq commentp t))
  11275. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11276. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11277. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11278. (let* ((match-data (match-data))
  11279. (startpos (point-at-bol))
  11280. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11281. (org-log-done org-log-done)
  11282. (org-log-repeat org-log-repeat)
  11283. (org-todo-log-states org-todo-log-states)
  11284. (org-inhibit-logging
  11285. (if (equal arg 0)
  11286. (progn (setq arg nil) 'note) org-inhibit-logging))
  11287. (this (match-string 1))
  11288. (hl-pos (match-beginning 0))
  11289. (head (org-get-todo-sequence-head this))
  11290. (ass (assoc head org-todo-kwd-alist))
  11291. (interpret (nth 1 ass))
  11292. (done-word (nth 3 ass))
  11293. (final-done-word (nth 4 ass))
  11294. (org-last-state (or this ""))
  11295. (completion-ignore-case t)
  11296. (member (member this org-todo-keywords-1))
  11297. (tail (cdr member))
  11298. (org-state (cond
  11299. ((and org-todo-key-trigger
  11300. (or (and (equal arg '(4))
  11301. (eq org-use-fast-todo-selection 'prefix))
  11302. (and (not arg) org-use-fast-todo-selection
  11303. (not (eq org-use-fast-todo-selection
  11304. 'prefix)))))
  11305. ;; Use fast selection
  11306. (org-fast-todo-selection))
  11307. ((and (equal arg '(4))
  11308. (or (not org-use-fast-todo-selection)
  11309. (not org-todo-key-trigger)))
  11310. ;; Read a state with completion
  11311. (org-icompleting-read
  11312. "State: " (mapcar 'list org-todo-keywords-1)
  11313. nil t))
  11314. ((eq arg 'right)
  11315. (if this
  11316. (if tail (car tail) nil)
  11317. (car org-todo-keywords-1)))
  11318. ((eq arg 'left)
  11319. (if (equal member org-todo-keywords-1)
  11320. nil
  11321. (if this
  11322. (nth (- (length org-todo-keywords-1)
  11323. (length tail) 2)
  11324. org-todo-keywords-1)
  11325. (org-last org-todo-keywords-1))))
  11326. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11327. (setq arg nil))) ; hack to fall back to cycling
  11328. (arg
  11329. ;; user or caller requests a specific state
  11330. (cond
  11331. ((equal arg "") nil)
  11332. ((eq arg 'none) nil)
  11333. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11334. ((eq arg 'nextset)
  11335. (or (car (cdr (member head org-todo-heads)))
  11336. (car org-todo-heads)))
  11337. ((eq arg 'previousset)
  11338. (let ((org-todo-heads (reverse org-todo-heads)))
  11339. (or (car (cdr (member head org-todo-heads)))
  11340. (car org-todo-heads))))
  11341. ((car (member arg org-todo-keywords-1)))
  11342. ((stringp arg)
  11343. (user-error "State `%s' not valid in this file" arg))
  11344. ((nth (1- (prefix-numeric-value arg))
  11345. org-todo-keywords-1))))
  11346. ((null member) (or head (car org-todo-keywords-1)))
  11347. ((equal this final-done-word) nil) ;; -> make empty
  11348. ((null tail) nil) ;; -> first entry
  11349. ((memq interpret '(type priority))
  11350. (if (eq this-command last-command)
  11351. (car tail)
  11352. (if (> (length tail) 0)
  11353. (or done-word (car org-done-keywords))
  11354. nil)))
  11355. (t
  11356. (car tail))))
  11357. (org-state (or
  11358. (run-hook-with-args-until-success
  11359. 'org-todo-get-default-hook org-state org-last-state)
  11360. org-state))
  11361. (next (if org-state (concat " " org-state " ") " "))
  11362. (change-plist (list :type 'todo-state-change :from this :to org-state
  11363. :position startpos))
  11364. dolog now-done-p)
  11365. (when org-blocker-hook
  11366. (setq org-last-todo-state-is-todo
  11367. (not (member this org-done-keywords)))
  11368. (unless (save-excursion
  11369. (save-match-data
  11370. (org-with-wide-buffer
  11371. (run-hook-with-args-until-failure
  11372. 'org-blocker-hook change-plist))))
  11373. (if (org-called-interactively-p 'interactive)
  11374. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11375. this org-state org-block-entry-blocking)
  11376. ;; fail silently
  11377. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11378. this org-state org-block-entry-blocking)
  11379. (throw 'exit nil))))
  11380. (store-match-data match-data)
  11381. (replace-match next t t)
  11382. (cond ((equal this org-state)
  11383. (message "TODO state was already %s" (org-trim next)))
  11384. ((pos-visible-in-window-p hl-pos)
  11385. (message "TODO state changed to %s" (org-trim next))))
  11386. (unless head
  11387. (setq head (org-get-todo-sequence-head org-state)
  11388. ass (assoc head org-todo-kwd-alist)
  11389. interpret (nth 1 ass)
  11390. done-word (nth 3 ass)
  11391. final-done-word (nth 4 ass)))
  11392. (when (memq arg '(nextset previousset))
  11393. (message "Keyword-Set %d/%d: %s"
  11394. (- (length org-todo-sets) -1
  11395. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11396. (length org-todo-sets)
  11397. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11398. (setq org-last-todo-state-is-todo
  11399. (not (member org-state org-done-keywords)))
  11400. (setq now-done-p (and (member org-state org-done-keywords)
  11401. (not (member this org-done-keywords))))
  11402. (and logging (org-local-logging logging))
  11403. (when (and (or org-todo-log-states org-log-done)
  11404. (not (eq org-inhibit-logging t))
  11405. (not (memq arg '(nextset previousset))))
  11406. ;; we need to look at recording a time and note
  11407. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11408. (nth 2 (assoc this org-todo-log-states))))
  11409. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11410. (setq dolog 'time))
  11411. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11412. (and org-state
  11413. (member org-state org-not-done-keywords)
  11414. (not (member this org-not-done-keywords))))
  11415. ;; This is now a todo state and was not one before
  11416. ;; If there was a CLOSED time stamp, get rid of it.
  11417. (org-add-planning-info nil nil 'closed))
  11418. (when (and now-done-p org-log-done)
  11419. ;; It is now done, and it was not done before
  11420. (org-add-planning-info 'closed (org-current-effective-time))
  11421. (if (and (not dolog) (eq 'note org-log-done))
  11422. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11423. (when (and org-state dolog)
  11424. ;; This is a non-nil state, and we need to log it
  11425. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11426. ;; Fixup tag positioning
  11427. (org-todo-trigger-tag-changes org-state)
  11428. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11429. (when org-provide-todo-statistics
  11430. (org-update-parent-todo-statistics))
  11431. (run-hooks 'org-after-todo-state-change-hook)
  11432. (if (and arg (not (member org-state org-done-keywords)))
  11433. (setq head (org-get-todo-sequence-head org-state)))
  11434. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11435. ;; Do we need to trigger a repeat?
  11436. (when now-done-p
  11437. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11438. ;; This is for the agenda, take a snapshot of the headline.
  11439. (save-match-data
  11440. (setq org-agenda-headline-snapshot-before-repeat
  11441. (org-get-heading))))
  11442. (org-auto-repeat-maybe org-state))
  11443. ;; Fixup cursor location if close to the keyword
  11444. (if (and (outline-on-heading-p)
  11445. (not (bolp))
  11446. (save-excursion (beginning-of-line 1)
  11447. (looking-at org-todo-line-regexp))
  11448. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11449. (progn
  11450. (goto-char (or (match-end 2) (match-end 1)))
  11451. (and (looking-at " ") (just-one-space))))
  11452. (when org-trigger-hook
  11453. (save-excursion
  11454. (run-hook-with-args 'org-trigger-hook change-plist)))
  11455. (when commentp (org-toggle-comment))))))))
  11456. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11457. "Block turning an entry into a TODO, using the hierarchy.
  11458. This checks whether the current task should be blocked from state
  11459. changes. Such blocking occurs when:
  11460. 1. The task has children which are not all in a completed state.
  11461. 2. A task has a parent with the property :ORDERED:, and there
  11462. are siblings prior to the current task with incomplete
  11463. status.
  11464. 3. The parent of the task is blocked because it has siblings that should
  11465. be done first, or is child of a block grandparent TODO entry."
  11466. (if (not org-enforce-todo-dependencies)
  11467. t ; if locally turned off don't block
  11468. (catch 'dont-block
  11469. ;; If this is not a todo state change, or if this entry is already DONE,
  11470. ;; do not block
  11471. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11472. (member (plist-get change-plist :from)
  11473. (cons 'done org-done-keywords))
  11474. (member (plist-get change-plist :to)
  11475. (cons 'todo org-not-done-keywords))
  11476. (not (plist-get change-plist :to)))
  11477. (throw 'dont-block t))
  11478. ;; If this task has children, and any are undone, it's blocked
  11479. (save-excursion
  11480. (org-back-to-heading t)
  11481. (let ((this-level (funcall outline-level)))
  11482. (outline-next-heading)
  11483. (let ((child-level (funcall outline-level)))
  11484. (while (and (not (eobp))
  11485. (> child-level this-level))
  11486. ;; this todo has children, check whether they are all
  11487. ;; completed
  11488. (if (and (not (org-entry-is-done-p))
  11489. (org-entry-is-todo-p))
  11490. (progn (setq org-block-entry-blocking (org-get-heading))
  11491. (throw 'dont-block nil)))
  11492. (outline-next-heading)
  11493. (setq child-level (funcall outline-level))))))
  11494. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11495. ;; any previous siblings are undone, it's blocked
  11496. (save-excursion
  11497. (org-back-to-heading t)
  11498. (let* ((pos (point))
  11499. (parent-pos (and (org-up-heading-safe) (point))))
  11500. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11501. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11502. (forward-line 1)
  11503. (re-search-forward org-not-done-heading-regexp pos t))
  11504. (setq org-block-entry-blocking (match-string 0))
  11505. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11506. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11507. (while t
  11508. (goto-char parent-pos)
  11509. (if (not (looking-at org-not-done-heading-regexp))
  11510. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11511. (setq pos (point))
  11512. (setq parent-pos (and (org-up-heading-safe) (point)))
  11513. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11514. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11515. (forward-line 1)
  11516. (re-search-forward org-not-done-heading-regexp pos t)
  11517. (setq org-block-entry-blocking (org-get-heading)))
  11518. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11519. (defcustom org-track-ordered-property-with-tag nil
  11520. "Should the ORDERED property also be shown as a tag?
  11521. The ORDERED property decides if an entry should require subtasks to be
  11522. completed in sequence. Since a property is not very visible, setting
  11523. this option means that toggling the ORDERED property with the command
  11524. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11525. not relevant for the behavior, but it makes things more visible.
  11526. Note that toggling the tag with tags commands will not change the property
  11527. and therefore not influence behavior!
  11528. This can be t, meaning the tag ORDERED should be used, It can also be a
  11529. string to select a different tag for this task."
  11530. :group 'org-todo
  11531. :type '(choice
  11532. (const :tag "No tracking" nil)
  11533. (const :tag "Track with ORDERED tag" t)
  11534. (string :tag "Use other tag")))
  11535. (defun org-toggle-ordered-property ()
  11536. "Toggle the ORDERED property of the current entry.
  11537. For better visibility, you can track the value of this property with a tag.
  11538. See variable `org-track-ordered-property-with-tag'."
  11539. (interactive)
  11540. (let* ((t1 org-track-ordered-property-with-tag)
  11541. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11542. (save-excursion
  11543. (org-back-to-heading)
  11544. (if (org-entry-get nil "ORDERED")
  11545. (progn
  11546. (org-delete-property "ORDERED")
  11547. (and tag (org-toggle-tag tag 'off))
  11548. (message "Subtasks can be completed in arbitrary order"))
  11549. (org-entry-put nil "ORDERED" "t")
  11550. (and tag (org-toggle-tag tag 'on))
  11551. (message "Subtasks must be completed in sequence")))))
  11552. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11553. (defun org-block-todo-from-checkboxes (change-plist)
  11554. "Block turning an entry into a TODO, using checkboxes.
  11555. This checks whether the current task should be blocked from state
  11556. changes because there are unchecked boxes in this entry."
  11557. (if (not org-enforce-todo-checkbox-dependencies)
  11558. t ; if locally turned off don't block
  11559. (catch 'dont-block
  11560. ;; If this is not a todo state change, or if this entry is already DONE,
  11561. ;; do not block
  11562. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11563. (member (plist-get change-plist :from)
  11564. (cons 'done org-done-keywords))
  11565. (member (plist-get change-plist :to)
  11566. (cons 'todo org-not-done-keywords))
  11567. (not (plist-get change-plist :to)))
  11568. (throw 'dont-block t))
  11569. ;; If this task has checkboxes that are not checked, it's blocked
  11570. (save-excursion
  11571. (org-back-to-heading t)
  11572. (let ((beg (point)) end)
  11573. (outline-next-heading)
  11574. (setq end (point))
  11575. (goto-char beg)
  11576. (if (org-list-search-forward
  11577. (concat (org-item-beginning-re)
  11578. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11579. "\\[[- ]\\]")
  11580. end t)
  11581. (progn
  11582. (if (boundp 'org-blocked-by-checkboxes)
  11583. (setq org-blocked-by-checkboxes t))
  11584. (throw 'dont-block nil)))))
  11585. t))) ; do not block
  11586. (defun org-entry-blocked-p ()
  11587. "Is the current entry blocked?"
  11588. (org-with-silent-modifications
  11589. (if (org-entry-get nil "NOBLOCKING")
  11590. nil ;; Never block this entry
  11591. (not (run-hook-with-args-until-failure
  11592. 'org-blocker-hook
  11593. (list :type 'todo-state-change
  11594. :position (point)
  11595. :from 'todo
  11596. :to 'done))))))
  11597. (defun org-update-statistics-cookies (all)
  11598. "Update the statistics cookie, either from TODO or from checkboxes.
  11599. This should be called with the cursor in a line with a statistics cookie."
  11600. (interactive "P")
  11601. (if all
  11602. (progn
  11603. (org-update-checkbox-count 'all)
  11604. (org-map-entries 'org-update-parent-todo-statistics))
  11605. (if (not (org-at-heading-p))
  11606. (org-update-checkbox-count)
  11607. (let ((pos (point-marker))
  11608. end l1 l2)
  11609. (ignore-errors (org-back-to-heading t))
  11610. (if (not (org-at-heading-p))
  11611. (org-update-checkbox-count)
  11612. (setq l1 (org-outline-level))
  11613. (setq end (save-excursion
  11614. (outline-next-heading)
  11615. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11616. (point)))
  11617. (if (and (save-excursion
  11618. (re-search-forward
  11619. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11620. (not (save-excursion (re-search-forward
  11621. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11622. (org-update-checkbox-count)
  11623. (if (and l2 (> l2 l1))
  11624. (progn
  11625. (goto-char end)
  11626. (org-update-parent-todo-statistics))
  11627. (goto-char pos)
  11628. (beginning-of-line 1)
  11629. (while (re-search-forward
  11630. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11631. (point-at-eol) t)
  11632. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11633. (goto-char pos)
  11634. (move-marker pos nil)))))
  11635. (defvar org-entry-property-inherited-from) ;; defined below
  11636. (defun org-update-parent-todo-statistics ()
  11637. "Update any statistics cookie in the parent of the current headline.
  11638. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11639. the entire subtree and this will travel up the hierarchy and update
  11640. statistics everywhere."
  11641. (let* ((prop (save-excursion (org-up-heading-safe)
  11642. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11643. (recursive (or (not org-hierarchical-todo-statistics)
  11644. (and prop (string-match "\\<recursive\\>" prop))))
  11645. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11646. 0))
  11647. (first t)
  11648. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11649. level ltoggle l1 new ndel
  11650. (cnt-all 0) (cnt-done 0) is-percent kwd
  11651. checkbox-beg ov ovs ove cookie-present)
  11652. (catch 'exit
  11653. (save-excursion
  11654. (beginning-of-line 1)
  11655. (setq ltoggle (funcall outline-level))
  11656. ;; Three situations are to consider:
  11657. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11658. ;; to the top-level ancestor on the headline;
  11659. ;; 2. If parent has "recursive" property, repeat up to the
  11660. ;; headline setting that property, taking inheritance into
  11661. ;; account;
  11662. ;; 3. Else, move up to direct parent and proceed only once.
  11663. (while (and (setq level (org-up-heading-safe))
  11664. (or recursive first)
  11665. (>= (point) lim))
  11666. (setq first nil cookie-present nil)
  11667. (unless (and level
  11668. (not (string-match
  11669. "\\<checkbox\\>"
  11670. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11671. "")))))
  11672. (throw 'exit nil))
  11673. (while (re-search-forward box-re (point-at-eol) t)
  11674. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11675. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11676. (save-match-data
  11677. (unless (outline-next-heading) (throw 'exit nil))
  11678. (while (and (looking-at org-complex-heading-regexp)
  11679. (> (setq l1 (length (match-string 1))) level))
  11680. (setq kwd (and (or recursive (= l1 ltoggle))
  11681. (match-string 2)))
  11682. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11683. (and (eq org-provide-todo-statistics t)
  11684. (or (member kwd org-done-keywords)))
  11685. (and (listp org-provide-todo-statistics)
  11686. (stringp (car org-provide-todo-statistics))
  11687. (or (member kwd org-provide-todo-statistics)
  11688. (member kwd org-done-keywords)))
  11689. (and (listp org-provide-todo-statistics)
  11690. (listp (car org-provide-todo-statistics))
  11691. (or (member kwd (car org-provide-todo-statistics))
  11692. (and (member kwd org-done-keywords)
  11693. (member kwd (cadr org-provide-todo-statistics))))))
  11694. (setq cnt-all (1+ cnt-all))
  11695. (if (eq org-provide-todo-statistics t)
  11696. (and kwd (setq cnt-all (1+ cnt-all)))))
  11697. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11698. (member kwd org-done-keywords))
  11699. (and (listp org-provide-todo-statistics)
  11700. (listp (car org-provide-todo-statistics))
  11701. (member kwd org-done-keywords)
  11702. (member kwd (cadr org-provide-todo-statistics)))
  11703. (and (listp org-provide-todo-statistics)
  11704. (stringp (car org-provide-todo-statistics))
  11705. (member kwd org-done-keywords)))
  11706. (setq cnt-done (1+ cnt-done)))
  11707. (outline-next-heading)))
  11708. (setq new
  11709. (if is-percent
  11710. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11711. (format "[%d/%d]" cnt-done cnt-all))
  11712. ndel (- (match-end 0) checkbox-beg))
  11713. ;; handle overlays when updating cookie from column view
  11714. (when (setq ov (car (overlays-at checkbox-beg)))
  11715. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11716. (delete-overlay ov))
  11717. (goto-char checkbox-beg)
  11718. (insert new)
  11719. (delete-region (point) (+ (point) ndel))
  11720. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11721. (when ov (move-overlay ov ovs ove)))
  11722. (when cookie-present
  11723. (run-hook-with-args 'org-after-todo-statistics-hook
  11724. cnt-done (- cnt-all cnt-done))))))
  11725. (run-hooks 'org-todo-statistics-hook)))
  11726. (defvar org-after-todo-statistics-hook nil
  11727. "Hook that is called after a TODO statistics cookie has been updated.
  11728. Each function is called with two arguments: the number of not-done entries
  11729. and the number of done entries.
  11730. For example, the following function, when added to this hook, will switch
  11731. an entry to DONE when all children are done, and back to TODO when new
  11732. entries are set to a TODO status. Note that this hook is only called
  11733. when there is a statistics cookie in the headline!
  11734. (defun org-summary-todo (n-done n-not-done)
  11735. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11736. (let (org-log-done org-log-states) ; turn off logging
  11737. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11738. ")
  11739. (defvar org-todo-statistics-hook nil
  11740. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11741. This hook runs even if there is no statistics cookie present, in which case
  11742. `org-after-todo-statistics-hook' would not run.")
  11743. (defun org-todo-trigger-tag-changes (state)
  11744. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11745. (let ((l org-todo-state-tags-triggers)
  11746. changes)
  11747. (when (or (not state) (equal state ""))
  11748. (setq changes (append changes (cdr (assoc "" l)))))
  11749. (when (and (stringp state) (> (length state) 0))
  11750. (setq changes (append changes (cdr (assoc state l)))))
  11751. (when (member state org-not-done-keywords)
  11752. (setq changes (append changes (cdr (assoc 'todo l)))))
  11753. (when (member state org-done-keywords)
  11754. (setq changes (append changes (cdr (assoc 'done l)))))
  11755. (dolist (c changes)
  11756. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11757. (defun org-local-logging (value)
  11758. "Get logging settings from a property VALUE."
  11759. (let* (words w a)
  11760. ;; directly set the variables, they are already local.
  11761. (setq org-log-done nil
  11762. org-log-repeat nil
  11763. org-todo-log-states nil)
  11764. (setq words (org-split-string value))
  11765. (while (setq w (pop words))
  11766. (cond
  11767. ((setq a (assoc w org-startup-options))
  11768. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11769. (set (nth 1 a) (nth 2 a))))
  11770. ((setq a (org-extract-log-state-settings w))
  11771. (and (member (car a) org-todo-keywords-1)
  11772. (push a org-todo-log-states)))))))
  11773. (defun org-get-todo-sequence-head (kwd)
  11774. "Return the head of the TODO sequence to which KWD belongs.
  11775. If KWD is not set, check if there is a text property remembering the
  11776. right sequence."
  11777. (let (p)
  11778. (cond
  11779. ((not kwd)
  11780. (or (get-text-property (point-at-bol) 'org-todo-head)
  11781. (progn
  11782. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11783. nil (point-at-eol)))
  11784. (get-text-property p 'org-todo-head))))
  11785. ((not (member kwd org-todo-keywords-1))
  11786. (car org-todo-keywords-1))
  11787. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11788. (defun org-fast-todo-selection ()
  11789. "Fast TODO keyword selection with single keys.
  11790. Returns the new TODO keyword, or nil if no state change should occur."
  11791. (let* ((fulltable org-todo-key-alist)
  11792. (done-keywords org-done-keywords) ;; needed for the faces.
  11793. (maxlen (apply 'max (mapcar
  11794. (lambda (x)
  11795. (if (stringp (car x)) (string-width (car x)) 0))
  11796. fulltable)))
  11797. (expert nil)
  11798. (fwidth (+ maxlen 3 1 3))
  11799. (ncol (/ (- (window-width) 4) fwidth))
  11800. tg cnt e c tbl
  11801. groups ingroup)
  11802. (save-excursion
  11803. (save-window-excursion
  11804. (if expert
  11805. (set-buffer (get-buffer-create " *Org todo*"))
  11806. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11807. (erase-buffer)
  11808. (org-set-local 'org-done-keywords done-keywords)
  11809. (setq tbl fulltable cnt 0)
  11810. (while (setq e (pop tbl))
  11811. (cond
  11812. ((equal e '(:startgroup))
  11813. (push '() groups) (setq ingroup t)
  11814. (when (not (= cnt 0))
  11815. (setq cnt 0)
  11816. (insert "\n"))
  11817. (insert "{ "))
  11818. ((equal e '(:endgroup))
  11819. (setq ingroup nil cnt 0)
  11820. (insert "}\n"))
  11821. ((equal e '(:newline))
  11822. (when (not (= cnt 0))
  11823. (setq cnt 0)
  11824. (insert "\n")
  11825. (setq e (car tbl))
  11826. (while (equal (car tbl) '(:newline))
  11827. (insert "\n")
  11828. (setq tbl (cdr tbl)))))
  11829. (t
  11830. (setq tg (car e) c (cdr e))
  11831. (if ingroup (push tg (car groups)))
  11832. (setq tg (org-add-props tg nil 'face
  11833. (org-get-todo-face tg)))
  11834. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11835. (insert "[" c "] " tg (make-string
  11836. (- fwidth 4 (length tg)) ?\ ))
  11837. (when (= (setq cnt (1+ cnt)) ncol)
  11838. (insert "\n")
  11839. (if ingroup (insert " "))
  11840. (setq cnt 0)))))
  11841. (insert "\n")
  11842. (goto-char (point-min))
  11843. (if (not expert) (org-fit-window-to-buffer))
  11844. (message "[a-z..]:Set [SPC]:clear")
  11845. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11846. (cond
  11847. ((or (= c ?\C-g)
  11848. (and (= c ?q) (not (rassoc c fulltable))))
  11849. (setq quit-flag t))
  11850. ((= c ?\ ) nil)
  11851. ((setq e (rassoc c fulltable) tg (car e))
  11852. tg)
  11853. (t (setq quit-flag t)))))))
  11854. (defun org-entry-is-todo-p ()
  11855. (member (org-get-todo-state) org-not-done-keywords))
  11856. (defun org-entry-is-done-p ()
  11857. (member (org-get-todo-state) org-done-keywords))
  11858. (defun org-get-todo-state ()
  11859. "Return the TODO keyword of the current subtree."
  11860. (save-excursion
  11861. (org-back-to-heading t)
  11862. (and (looking-at org-todo-line-regexp)
  11863. (match-end 2)
  11864. (match-string 2))))
  11865. (defun org-at-date-range-p (&optional inactive-ok)
  11866. "Is the cursor inside a date range?"
  11867. (interactive)
  11868. (save-excursion
  11869. (catch 'exit
  11870. (let ((pos (point)))
  11871. (skip-chars-backward "^[<\r\n")
  11872. (skip-chars-backward "<[")
  11873. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11874. (>= (match-end 0) pos)
  11875. (throw 'exit t))
  11876. (skip-chars-backward "^<[\r\n")
  11877. (skip-chars-backward "<[")
  11878. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11879. (>= (match-end 0) pos)
  11880. (throw 'exit t)))
  11881. nil)))
  11882. (defun org-get-repeat (&optional tagline)
  11883. "Check if there is a deadline/schedule with repeater in this entry."
  11884. (save-match-data
  11885. (save-excursion
  11886. (org-back-to-heading t)
  11887. (and (re-search-forward (if tagline
  11888. (concat tagline "\\s-*" org-repeat-re)
  11889. org-repeat-re)
  11890. (org-entry-end-position) t)
  11891. (match-string-no-properties 1)))))
  11892. (defvar org-last-changed-timestamp)
  11893. (defvar org-last-inserted-timestamp)
  11894. (defvar org-log-post-message)
  11895. (defvar org-log-note-purpose)
  11896. (defvar org-log-note-how nil)
  11897. (defvar org-log-note-extra)
  11898. (defun org-auto-repeat-maybe (done-word)
  11899. "Check if the current headline contains a repeated deadline/schedule.
  11900. If yes, set TODO state back to what it was and change the base date
  11901. of repeating deadline/scheduled time stamps to new date.
  11902. This function is run automatically after each state change to a DONE state."
  11903. ;; last-state is dynamically scoped into this function
  11904. (let* ((repeat (org-get-repeat))
  11905. (aa (assoc org-last-state org-todo-kwd-alist))
  11906. (interpret (nth 1 aa))
  11907. (head (nth 2 aa))
  11908. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11909. (msg "Entry repeats: ")
  11910. (org-log-done nil)
  11911. (org-todo-log-states nil)
  11912. re type n what ts time to-state)
  11913. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11914. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11915. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11916. org-todo-repeat-to-state))
  11917. (unless (and to-state (member to-state org-todo-keywords-1))
  11918. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11919. (org-todo to-state)
  11920. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11921. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11922. (org-time-stamp-format t t))))
  11923. (when org-log-repeat
  11924. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11925. (memq 'org-add-log-note post-command-hook))
  11926. ;; OK, we are already setup for some record
  11927. (if (eq org-log-repeat 'note)
  11928. ;; make sure we take a note, not only a time stamp
  11929. (setq org-log-note-how 'note))
  11930. ;; Set up for taking a record
  11931. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11932. org-last-state
  11933. 'findpos org-log-repeat)))
  11934. (org-back-to-heading t)
  11935. (org-add-planning-info nil nil 'closed)
  11936. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11937. org-deadline-time-regexp "\\)\\|\\("
  11938. org-ts-regexp "\\)"))
  11939. (while (re-search-forward
  11940. re (save-excursion (outline-next-heading) (point)) t)
  11941. (setq type (if (match-end 1) org-scheduled-string
  11942. (if (match-end 3) org-deadline-string "Plain:"))
  11943. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11944. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11945. (setq n (string-to-number (match-string 2 ts))
  11946. what (match-string 3 ts))
  11947. (if (equal what "w") (setq n (* n 7) what "d"))
  11948. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11949. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11950. ;; Preparation, see if we need to modify the start date for the change
  11951. (when (match-end 1)
  11952. (setq time (save-match-data (org-time-string-to-time ts)))
  11953. (cond
  11954. ((equal (match-string 1 ts) ".")
  11955. ;; Shift starting date to today
  11956. (org-timestamp-change
  11957. (- (org-today) (time-to-days time))
  11958. 'day))
  11959. ((equal (match-string 1 ts) "+")
  11960. (let ((nshiftmax 10) (nshift 0))
  11961. (while (or (= nshift 0)
  11962. (<= (time-to-days time)
  11963. (time-to-days (current-time))))
  11964. (when (= (incf nshift) nshiftmax)
  11965. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11966. (user-error "Abort")))
  11967. (org-timestamp-change n (cdr (assoc what whata)))
  11968. (org-at-timestamp-p t)
  11969. (setq ts (match-string 1))
  11970. (setq time (save-match-data (org-time-string-to-time ts)))))
  11971. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11972. ;; rematch, so that we have everything in place for the real shift
  11973. (org-at-timestamp-p t)
  11974. (setq ts (match-string 1))
  11975. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11976. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11977. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11978. (setq org-log-post-message msg)
  11979. (message "%s" msg))))
  11980. (defun org-show-todo-tree (arg)
  11981. "Make a compact tree which shows all headlines marked with TODO.
  11982. The tree will show the lines where the regexp matches, and all higher
  11983. headlines above the match.
  11984. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11985. With a numeric prefix N, construct a sparse tree for the Nth element
  11986. of `org-todo-keywords-1'."
  11987. (interactive "P")
  11988. (let ((case-fold-search nil)
  11989. (kwd-re
  11990. (cond ((null arg) org-not-done-regexp)
  11991. ((equal arg '(4))
  11992. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11993. (mapcar 'list org-todo-keywords-1))))
  11994. (concat "\\("
  11995. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11996. "\\)\\>")))
  11997. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11998. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11999. org-todo-keywords-1)))
  12000. (t (user-error "Invalid prefix argument: %s" arg)))))
  12001. (message "%d TODO entries found"
  12002. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12003. (defun org-deadline (arg &optional time)
  12004. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12005. With one universal prefix argument, remove any deadline from the item.
  12006. With two universal prefix arguments, prompt for a warning delay.
  12007. With argument TIME, set the deadline at the corresponding date. TIME
  12008. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12009. (interactive "P")
  12010. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12011. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12012. 'region-start-level 'region))
  12013. org-loop-over-headlines-in-active-region)
  12014. (org-map-entries
  12015. `(org-deadline ',arg ,time)
  12016. org-loop-over-headlines-in-active-region
  12017. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12018. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12019. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12020. (repeater (and old-date
  12021. (string-match
  12022. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12023. old-date)
  12024. (match-string 1 old-date))))
  12025. (cond
  12026. ((equal arg '(4))
  12027. (when (and old-date org-log-redeadline)
  12028. (org-add-log-setup 'deldeadline nil old-date 'findpos
  12029. org-log-redeadline))
  12030. (org-remove-timestamp-with-keyword org-deadline-string)
  12031. (message "Item no longer has a deadline."))
  12032. ((equal arg '(16))
  12033. (save-excursion
  12034. (org-back-to-heading t)
  12035. (if (re-search-forward
  12036. org-deadline-time-regexp
  12037. (save-excursion (outline-next-heading) (point)) t)
  12038. (let* ((rpl0 (match-string 1))
  12039. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12040. (replace-match
  12041. (concat org-deadline-string
  12042. " <" rpl
  12043. (format " -%dd"
  12044. (abs
  12045. (- (time-to-days
  12046. (save-match-data
  12047. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12048. (time-to-days old-date-time))))
  12049. ">") t t))
  12050. (user-error "No deadline information to update"))))
  12051. (t
  12052. (org-add-planning-info 'deadline time 'closed)
  12053. (when (and old-date org-log-redeadline
  12054. (not (equal old-date
  12055. (substring org-last-inserted-timestamp 1 -1))))
  12056. (org-add-log-setup 'redeadline nil old-date 'findpos
  12057. org-log-redeadline))
  12058. (when repeater
  12059. (save-excursion
  12060. (org-back-to-heading t)
  12061. (when (re-search-forward (concat org-deadline-string " "
  12062. org-last-inserted-timestamp)
  12063. (save-excursion
  12064. (outline-next-heading) (point)) t)
  12065. (goto-char (1- (match-end 0)))
  12066. (insert " " repeater)
  12067. (setq org-last-inserted-timestamp
  12068. (concat (substring org-last-inserted-timestamp 0 -1)
  12069. " " repeater
  12070. (substring org-last-inserted-timestamp -1))))))
  12071. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12072. (defun org-schedule (arg &optional time)
  12073. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12074. With one universal prefix argument, remove any scheduling date from the item.
  12075. With two universal prefix arguments, prompt for a delay cookie.
  12076. With argument TIME, scheduled at the corresponding date. TIME can
  12077. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12078. (interactive "P")
  12079. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12080. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12081. 'region-start-level 'region))
  12082. org-loop-over-headlines-in-active-region)
  12083. (org-map-entries
  12084. `(org-schedule ',arg ,time)
  12085. org-loop-over-headlines-in-active-region
  12086. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12087. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12088. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12089. (repeater (and old-date
  12090. (string-match
  12091. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12092. old-date)
  12093. (match-string 1 old-date))))
  12094. (cond
  12095. ((equal arg '(4))
  12096. (progn
  12097. (when (and old-date org-log-reschedule)
  12098. (org-add-log-setup 'delschedule nil old-date 'findpos
  12099. org-log-reschedule))
  12100. (org-remove-timestamp-with-keyword org-scheduled-string)
  12101. (message "Item is no longer scheduled.")))
  12102. ((equal arg '(16))
  12103. (save-excursion
  12104. (org-back-to-heading t)
  12105. (if (re-search-forward
  12106. org-scheduled-time-regexp
  12107. (save-excursion (outline-next-heading) (point)) t)
  12108. (let* ((rpl0 (match-string 1))
  12109. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12110. (replace-match
  12111. (concat org-scheduled-string
  12112. " <" rpl
  12113. (format " -%dd"
  12114. (abs
  12115. (- (time-to-days
  12116. (save-match-data
  12117. (org-read-date nil t nil "Delay until" old-date-time)))
  12118. (time-to-days old-date-time))))
  12119. ">") t t))
  12120. (user-error "No scheduled information to update"))))
  12121. (t
  12122. (org-add-planning-info 'scheduled time 'closed)
  12123. (when (and old-date org-log-reschedule
  12124. (not (equal old-date
  12125. (substring org-last-inserted-timestamp 1 -1))))
  12126. (org-add-log-setup 'reschedule nil old-date 'findpos
  12127. org-log-reschedule))
  12128. (when repeater
  12129. (save-excursion
  12130. (org-back-to-heading t)
  12131. (when (re-search-forward (concat org-scheduled-string " "
  12132. org-last-inserted-timestamp)
  12133. (save-excursion
  12134. (outline-next-heading) (point)) t)
  12135. (goto-char (1- (match-end 0)))
  12136. (insert " " repeater)
  12137. (setq org-last-inserted-timestamp
  12138. (concat (substring org-last-inserted-timestamp 0 -1)
  12139. " " repeater
  12140. (substring org-last-inserted-timestamp -1))))))
  12141. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12142. (defun org-get-scheduled-time (pom &optional inherit)
  12143. "Get the scheduled time as a time tuple, of a format suitable
  12144. for calling org-schedule with, or if there is no scheduling,
  12145. returns nil."
  12146. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12147. (when time
  12148. (apply 'encode-time (org-parse-time-string time)))))
  12149. (defun org-get-deadline-time (pom &optional inherit)
  12150. "Get the deadline as a time tuple, of a format suitable for
  12151. calling org-deadline with, or if there is no scheduling, returns
  12152. nil."
  12153. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12154. (when time
  12155. (apply 'encode-time (org-parse-time-string time)))))
  12156. (defun org-remove-timestamp-with-keyword (keyword)
  12157. "Remove all time stamps with KEYWORD in the current entry."
  12158. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12159. beg)
  12160. (save-excursion
  12161. (org-back-to-heading t)
  12162. (setq beg (point))
  12163. (outline-next-heading)
  12164. (while (re-search-backward re beg t)
  12165. (replace-match "")
  12166. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12167. (equal (char-before) ?\ ))
  12168. (backward-delete-char 1)
  12169. (if (string-match "^[ \t]*$" (buffer-substring
  12170. (point-at-bol) (point-at-eol)))
  12171. (delete-region (point-at-bol)
  12172. (min (point-max) (1+ (point-at-eol))))))))))
  12173. (defvar org-time-was-given) ; dynamically scoped parameter
  12174. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12175. (defun org-add-planning-info (what &optional time &rest remove)
  12176. "Insert new timestamp with keyword in the line directly after the headline.
  12177. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  12178. If non is given, the user is prompted for a date.
  12179. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  12180. be removed."
  12181. (interactive)
  12182. (let (org-time-was-given org-end-time-was-given ts
  12183. end default-time default-input)
  12184. (catch 'exit
  12185. (when (and (memq what '(scheduled deadline))
  12186. (or (not time)
  12187. (and (stringp time)
  12188. (string-match "^[-+]+[0-9]" time))))
  12189. ;; Try to get a default date/time from existing timestamp
  12190. (save-excursion
  12191. (org-back-to-heading t)
  12192. (setq end (save-excursion (outline-next-heading) (point)))
  12193. (when (re-search-forward (if (eq what 'scheduled)
  12194. org-scheduled-time-regexp
  12195. org-deadline-time-regexp)
  12196. end t)
  12197. (setq ts (match-string 1)
  12198. default-time
  12199. (apply 'encode-time (org-parse-time-string ts))
  12200. default-input (and ts (org-get-compact-tod ts))))))
  12201. (when what
  12202. (setq time
  12203. (if (stringp time)
  12204. ;; This is a string (relative or absolute), set proper date
  12205. (apply 'encode-time
  12206. (org-read-date-analyze
  12207. time default-time (decode-time default-time)))
  12208. ;; If necessary, get the time from the user
  12209. (or time (org-read-date nil 'to-time nil nil
  12210. default-time default-input)))))
  12211. (when (and org-insert-labeled-timestamps-at-point
  12212. (member what '(scheduled deadline)))
  12213. (insert
  12214. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  12215. (org-insert-time-stamp time org-time-was-given
  12216. nil nil nil (list org-end-time-was-given))
  12217. (setq what nil))
  12218. (save-excursion
  12219. (save-restriction
  12220. (let (col list elt ts buffer-invisibility-spec)
  12221. (org-back-to-heading t)
  12222. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  12223. (goto-char (match-end 1))
  12224. (setq col (current-column))
  12225. (goto-char (match-end 0))
  12226. (if (eobp) (insert "\n") (forward-char 1))
  12227. (when (and (not what)
  12228. (not (looking-at
  12229. (concat "[ \t]*"
  12230. org-keyword-time-not-clock-regexp))))
  12231. ;; Nothing to add, nothing to remove...... :-)
  12232. (throw 'exit nil))
  12233. (if (and (not (looking-at org-outline-regexp))
  12234. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  12235. "[^\r\n]*"))
  12236. (not (equal (match-string 1) org-clock-string)))
  12237. (narrow-to-region (match-beginning 0) (match-end 0))
  12238. (insert-before-markers "\n")
  12239. (backward-char 1)
  12240. (narrow-to-region (point) (point))
  12241. (and org-adapt-indentation (org-indent-to-column col)))
  12242. ;; Check if we have to remove something.
  12243. (setq list (cons what remove))
  12244. (while list
  12245. (setq elt (pop list))
  12246. (when (or (and (eq elt 'scheduled)
  12247. (re-search-forward org-scheduled-time-regexp nil t))
  12248. (and (eq elt 'deadline)
  12249. (re-search-forward org-deadline-time-regexp nil t))
  12250. (and (eq elt 'closed)
  12251. (re-search-forward org-closed-time-regexp nil t)))
  12252. (replace-match "")
  12253. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  12254. (and (looking-at "[ \t]+") (replace-match ""))
  12255. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  12256. (when what
  12257. (insert
  12258. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  12259. (cond ((eq what 'scheduled) org-scheduled-string)
  12260. ((eq what 'deadline) org-deadline-string)
  12261. ((eq what 'closed) org-closed-string))
  12262. " ")
  12263. (setq ts (org-insert-time-stamp
  12264. time
  12265. (or org-time-was-given
  12266. (and (eq what 'closed) org-log-done-with-time))
  12267. (eq what 'closed)
  12268. nil nil (list org-end-time-was-given)))
  12269. (insert
  12270. (if (not (or (bolp) (eq (char-before) ?\ )
  12271. (memq (char-after) '(32 10))
  12272. (eobp))) " " ""))
  12273. (end-of-line 1))
  12274. (goto-char (point-min))
  12275. (widen)
  12276. (if (and (looking-at "[ \t]*\n")
  12277. (equal (char-before) ?\n))
  12278. (delete-region (1- (point)) (point-at-eol)))
  12279. ts))))))
  12280. (defvar org-log-note-marker (make-marker))
  12281. (defvar org-log-note-purpose nil)
  12282. (defvar org-log-note-state nil)
  12283. (defvar org-log-note-previous-state nil)
  12284. (defvar org-log-note-extra nil)
  12285. (defvar org-log-note-window-configuration nil)
  12286. (defvar org-log-note-return-to (make-marker))
  12287. (defvar org-log-note-effective-time nil
  12288. "Remembered current time so that dynamically scoped
  12289. `org-extend-today-until' affects tha timestamps in state change
  12290. log")
  12291. (defvar org-log-post-message nil
  12292. "Message to be displayed after a log note has been stored.
  12293. The auto-repeater uses this.")
  12294. (defun org-add-note ()
  12295. "Add a note to the current entry.
  12296. This is done in the same way as adding a state change note."
  12297. (interactive)
  12298. (org-add-log-setup 'note nil nil 'findpos nil))
  12299. (defvar org-property-end-re)
  12300. (defun org-add-log-setup (&optional purpose state prev-state
  12301. findpos how extra)
  12302. "Set up the post command hook to take a note.
  12303. If this is about to TODO state change, the new state is expected in STATE.
  12304. When FINDPOS is non-nil, find the correct position for the note in
  12305. the current entry. If not, assume that it can be inserted at point.
  12306. HOW is an indicator what kind of note should be created.
  12307. EXTRA is additional text that will be inserted into the notes buffer."
  12308. (let* ((org-log-into-drawer (org-log-into-drawer))
  12309. (drawer (cond ((stringp org-log-into-drawer)
  12310. org-log-into-drawer)
  12311. (org-log-into-drawer "LOGBOOK"))))
  12312. (save-restriction
  12313. (save-excursion
  12314. (when findpos
  12315. (org-back-to-heading t)
  12316. (narrow-to-region (point) (save-excursion
  12317. (outline-next-heading) (point)))
  12318. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12319. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12320. "[^\r\n]*\\)?"))
  12321. (goto-char (match-end 0))
  12322. (cond
  12323. (drawer
  12324. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12325. nil t)
  12326. (progn
  12327. (goto-char (match-end 0))
  12328. (or org-log-states-order-reversed
  12329. (and (re-search-forward org-property-end-re nil t)
  12330. (goto-char (1- (match-beginning 0))))))
  12331. (insert "\n:" drawer ":\n:END:")
  12332. (beginning-of-line 0)
  12333. (org-indent-line)
  12334. (beginning-of-line 2)
  12335. (org-indent-line)
  12336. (end-of-line 0)))
  12337. ((and org-log-state-notes-insert-after-drawers
  12338. (save-excursion
  12339. (forward-line) (looking-at org-drawer-regexp)))
  12340. (forward-line)
  12341. (while (looking-at org-drawer-regexp)
  12342. (goto-char (match-end 0))
  12343. (re-search-forward org-property-end-re (point-max) t)
  12344. (forward-line))
  12345. (forward-line -1)))
  12346. (unless org-log-states-order-reversed
  12347. (and (= (char-after) ?\n) (forward-char 1))
  12348. (org-skip-over-state-notes)
  12349. (skip-chars-backward " \t\n\r")))
  12350. (move-marker org-log-note-marker (point))
  12351. (setq org-log-note-purpose purpose
  12352. org-log-note-state state
  12353. org-log-note-previous-state prev-state
  12354. org-log-note-how how
  12355. org-log-note-extra extra
  12356. org-log-note-effective-time (org-current-effective-time))
  12357. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12358. (defun org-skip-over-state-notes ()
  12359. "Skip past the list of State notes in an entry."
  12360. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12361. (when (ignore-errors (goto-char (org-in-item-p)))
  12362. (let* ((struct (org-list-struct))
  12363. (prevs (org-list-prevs-alist struct)))
  12364. (while (looking-at "[ \t]*- State")
  12365. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12366. (org-list-get-item-end (point) struct)))))))
  12367. (defun org-add-log-note (&optional purpose)
  12368. "Pop up a window for taking a note, and add this note later at point."
  12369. (remove-hook 'post-command-hook 'org-add-log-note)
  12370. (setq org-log-note-window-configuration (current-window-configuration))
  12371. (delete-other-windows)
  12372. (move-marker org-log-note-return-to (point))
  12373. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12374. (goto-char org-log-note-marker)
  12375. (org-switch-to-buffer-other-window "*Org Note*")
  12376. (erase-buffer)
  12377. (if (memq org-log-note-how '(time state))
  12378. (let (current-prefix-arg) (org-store-log-note))
  12379. (let ((org-inhibit-startup t)) (org-mode))
  12380. (insert (format "# Insert note for %s.
  12381. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12382. (cond
  12383. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12384. ((eq org-log-note-purpose 'done) "closed todo item")
  12385. ((eq org-log-note-purpose 'state)
  12386. (format "state change from \"%s\" to \"%s\""
  12387. (or org-log-note-previous-state "")
  12388. (or org-log-note-state "")))
  12389. ((eq org-log-note-purpose 'reschedule)
  12390. "rescheduling")
  12391. ((eq org-log-note-purpose 'delschedule)
  12392. "no longer scheduled")
  12393. ((eq org-log-note-purpose 'redeadline)
  12394. "changing deadline")
  12395. ((eq org-log-note-purpose 'deldeadline)
  12396. "removing deadline")
  12397. ((eq org-log-note-purpose 'refile)
  12398. "refiling")
  12399. ((eq org-log-note-purpose 'note)
  12400. "this entry")
  12401. (t (error "This should not happen")))))
  12402. (if org-log-note-extra (insert org-log-note-extra))
  12403. (org-set-local 'org-finish-function 'org-store-log-note)
  12404. (run-hooks 'org-log-buffer-setup-hook)))
  12405. (defvar org-note-abort nil) ; dynamically scoped
  12406. (defun org-store-log-note ()
  12407. "Finish taking a log note, and insert it to where it belongs."
  12408. (let ((txt (buffer-string)))
  12409. (kill-buffer (current-buffer))
  12410. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12411. lines ind bul)
  12412. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12413. (setq txt (replace-match "" t t txt)))
  12414. (if (string-match "\\s-+\\'" txt)
  12415. (setq txt (replace-match "" t t txt)))
  12416. (setq lines (org-split-string txt "\n"))
  12417. (when (and note (string-match "\\S-" note))
  12418. (setq note
  12419. (org-replace-escapes
  12420. note
  12421. (list (cons "%u" (user-login-name))
  12422. (cons "%U" user-full-name)
  12423. (cons "%t" (format-time-string
  12424. (org-time-stamp-format 'long 'inactive)
  12425. org-log-note-effective-time))
  12426. (cons "%T" (format-time-string
  12427. (org-time-stamp-format 'long nil)
  12428. org-log-note-effective-time))
  12429. (cons "%d" (format-time-string
  12430. (org-time-stamp-format nil 'inactive)
  12431. org-log-note-effective-time))
  12432. (cons "%D" (format-time-string
  12433. (org-time-stamp-format nil nil)
  12434. org-log-note-effective-time))
  12435. (cons "%s" (if org-log-note-state
  12436. (concat "\"" org-log-note-state "\"")
  12437. ""))
  12438. (cons "%S" (if org-log-note-previous-state
  12439. (concat "\"" org-log-note-previous-state "\"")
  12440. "\"\"")))))
  12441. (if lines (setq note (concat note " \\\\")))
  12442. (push note lines))
  12443. (when (or current-prefix-arg org-note-abort)
  12444. (when org-log-into-drawer
  12445. (org-remove-empty-drawer-at org-log-note-marker))
  12446. (setq lines nil))
  12447. (when lines
  12448. (with-current-buffer (marker-buffer org-log-note-marker)
  12449. (save-excursion
  12450. (goto-char org-log-note-marker)
  12451. (move-marker org-log-note-marker nil)
  12452. (end-of-line 1)
  12453. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12454. (setq ind (save-excursion
  12455. (if (ignore-errors (goto-char (org-in-item-p)))
  12456. (let ((struct (org-list-struct)))
  12457. (org-list-get-ind
  12458. (org-list-get-top-point struct) struct))
  12459. (skip-chars-backward " \r\t\n")
  12460. (cond
  12461. ((and (org-at-heading-p)
  12462. org-adapt-indentation)
  12463. (1+ (org-current-level)))
  12464. ((org-at-heading-p) 0)
  12465. (t (org-get-indentation))))))
  12466. (setq bul (org-list-bullet-string "-"))
  12467. (org-indent-line-to ind)
  12468. (insert bul (pop lines))
  12469. (let ((ind-body (+ (length bul) ind)))
  12470. (while lines
  12471. (insert "\n")
  12472. (org-indent-line-to ind-body)
  12473. (insert (pop lines))))
  12474. (message "Note stored")
  12475. (org-back-to-heading t)
  12476. (org-cycle-hide-drawers 'children))
  12477. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12478. ;; from within `org-add-log-note' because `buffer-undo-list'
  12479. ;; is then modified outside of `org-with-remote-undo'.
  12480. (when (eq this-command 'org-agenda-todo)
  12481. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12482. ;; Don't add undo information when called from `org-agenda-todo'
  12483. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12484. (set-window-configuration org-log-note-window-configuration)
  12485. (with-current-buffer (marker-buffer org-log-note-return-to)
  12486. (goto-char org-log-note-return-to))
  12487. (move-marker org-log-note-return-to nil)
  12488. (and org-log-post-message (message "%s" org-log-post-message))))
  12489. (defun org-remove-empty-drawer-at (pos)
  12490. "Remove an empty drawer at position POS.
  12491. POS may also be a marker."
  12492. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12493. (org-with-wide-buffer
  12494. (goto-char pos)
  12495. (let ((drawer (org-element-at-point)))
  12496. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12497. (not (org-element-property :contents-begin drawer)))
  12498. (delete-region (org-element-property :begin drawer)
  12499. (progn (goto-char (org-element-property :end drawer))
  12500. (skip-chars-backward " \r\t\n")
  12501. (forward-line)
  12502. (point))))))))
  12503. (defvar org-ts-type nil)
  12504. (defun org-sparse-tree (&optional arg type)
  12505. "Create a sparse tree, prompt for the details.
  12506. This command can create sparse trees. You first need to select the type
  12507. of match used to create the tree:
  12508. t Show all TODO entries.
  12509. T Show entries with a specific TODO keyword.
  12510. m Show entries selected by a tags/property match.
  12511. p Enter a property name and its value (both with completion on existing
  12512. names/values) and show entries with that property.
  12513. r Show entries matching a regular expression (`/' can be used as well).
  12514. b Show deadlines and scheduled items before a date.
  12515. a Show deadlines and scheduled items after a date.
  12516. d Show deadlines due within `org-deadline-warning-days'.
  12517. D Show deadlines and scheduled items between a date range."
  12518. (interactive "P")
  12519. (setq type (or type org-sparse-tree-default-date-type))
  12520. (setq org-ts-type type)
  12521. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12522. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12523. [c]ycle through date types: %s"
  12524. (case type
  12525. (all "all timestamps")
  12526. (scheduled "only scheduled")
  12527. (deadline "only deadline")
  12528. (active "only active timestamps")
  12529. (inactive "only inactive timestamps")
  12530. (scheduled-or-deadline "scheduled/deadline")
  12531. (closed "with a closed time-stamp")
  12532. (otherwise "scheduled/deadline")))
  12533. (let ((answer (read-char-exclusive)))
  12534. (case answer
  12535. (?c
  12536. (org-sparse-tree
  12537. arg
  12538. (cadr (memq type '(scheduled-or-deadline all scheduled deadline active
  12539. inactive closed)))))
  12540. (?d (call-interactively 'org-check-deadlines))
  12541. (?b (call-interactively 'org-check-before-date))
  12542. (?a (call-interactively 'org-check-after-date))
  12543. (?D (call-interactively 'org-check-dates-range))
  12544. (?t (call-interactively 'org-show-todo-tree))
  12545. (?T (org-show-todo-tree '(4)))
  12546. (?m (call-interactively 'org-match-sparse-tree))
  12547. ((?p ?P)
  12548. (let* ((kwd (org-icompleting-read
  12549. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12550. (value (org-icompleting-read
  12551. "Value: " (mapcar 'list (org-property-values kwd)))))
  12552. (unless (string-match "\\`{.*}\\'" value)
  12553. (setq value (concat "\"" value "\"")))
  12554. (org-match-sparse-tree arg (concat kwd "=" value))))
  12555. ((?r ?R ?/) (call-interactively 'org-occur))
  12556. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12557. (defvar org-occur-highlights nil
  12558. "List of overlays used for occur matches.")
  12559. (make-variable-buffer-local 'org-occur-highlights)
  12560. (defvar org-occur-parameters nil
  12561. "Parameters of the active org-occur calls.
  12562. This is a list, each call to org-occur pushes as cons cell,
  12563. containing the regular expression and the callback, onto the list.
  12564. The list can contain several entries if `org-occur' has been called
  12565. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12566. will only contain one set of parameters. When the highlights are
  12567. removed (for example with `C-c C-c', or with the next edit (depending
  12568. on `org-remove-highlights-with-change'), this variable is emptied
  12569. as well.")
  12570. (make-variable-buffer-local 'org-occur-parameters)
  12571. (defun org-occur (regexp &optional keep-previous callback)
  12572. "Make a compact tree which shows all matches of REGEXP.
  12573. The tree will show the lines where the regexp matches, and all higher
  12574. headlines above the match. It will also show the heading after the match,
  12575. to make sure editing the matching entry is easy.
  12576. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12577. call to `org-occur' will be kept, to allow stacking of calls to this
  12578. command.
  12579. If CALLBACK is non-nil, it is a function which is called to confirm
  12580. that the match should indeed be shown."
  12581. (interactive "sRegexp: \nP")
  12582. (when (equal regexp "")
  12583. (user-error "Regexp cannot be empty"))
  12584. (unless keep-previous
  12585. (org-remove-occur-highlights nil nil t))
  12586. (push (cons regexp callback) org-occur-parameters)
  12587. (let ((cnt 0))
  12588. (save-excursion
  12589. (goto-char (point-min))
  12590. (if (or (not keep-previous) ; do not want to keep
  12591. (not org-occur-highlights)) ; no previous matches
  12592. ;; hide everything
  12593. (org-overview))
  12594. (while (re-search-forward regexp nil t)
  12595. (when (or (not callback)
  12596. (save-match-data (funcall callback)))
  12597. (setq cnt (1+ cnt))
  12598. (when org-highlight-sparse-tree-matches
  12599. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12600. (org-show-context 'occur-tree))))
  12601. (when org-remove-highlights-with-change
  12602. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12603. nil 'local))
  12604. (unless org-sparse-tree-open-archived-trees
  12605. (org-hide-archived-subtrees (point-min) (point-max)))
  12606. (run-hooks 'org-occur-hook)
  12607. (if (org-called-interactively-p 'interactive)
  12608. (message "%d match(es) for regexp %s" cnt regexp))
  12609. cnt))
  12610. (defun org-occur-next-match (&optional n reset)
  12611. "Function for `next-error-function' to find sparse tree matches.
  12612. N is the number of matches to move, when negative move backwards.
  12613. RESET is entirely ignored - this function always goes back to the
  12614. starting point when no match is found."
  12615. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12616. (search-func (if (< n 0)
  12617. 'previous-single-char-property-change
  12618. 'next-single-char-property-change))
  12619. (n (abs n))
  12620. (pos (point))
  12621. p1)
  12622. (catch 'exit
  12623. (while (setq p1 (funcall search-func (point) 'org-type))
  12624. (when (equal p1 limit)
  12625. (goto-char pos)
  12626. (user-error "No more matches"))
  12627. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12628. (setq n (1- n))
  12629. (when (= n 0)
  12630. (goto-char p1)
  12631. (throw 'exit (point))))
  12632. (goto-char p1))
  12633. (goto-char p1)
  12634. (user-error "No more matches"))))
  12635. (defun org-show-context (&optional key)
  12636. "Make sure point and context are visible.
  12637. How much context is shown depends upon the variables
  12638. `org-show-hierarchy-above', `org-show-following-heading',
  12639. `org-show-entry-below' and `org-show-siblings'."
  12640. (let ((heading-p (org-at-heading-p t))
  12641. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12642. (following-p (org-get-alist-option org-show-following-heading key))
  12643. (entry-p (org-get-alist-option org-show-entry-below key))
  12644. (siblings-p (org-get-alist-option org-show-siblings key)))
  12645. ;; Show heading or entry text
  12646. (if (and heading-p (not entry-p))
  12647. (org-flag-heading nil) ; only show the heading
  12648. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12649. (org-show-hidden-entry))) ; show entire entry
  12650. (when following-p
  12651. ;; Show next sibling, or heading below text
  12652. (save-excursion
  12653. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12654. (org-flag-heading nil))))
  12655. (when siblings-p (org-show-siblings))
  12656. (when hierarchy-p
  12657. ;; show all higher headings, possibly with siblings
  12658. (save-excursion
  12659. (while (and (ignore-errors (progn (org-up-heading-all 1) t))
  12660. (not (bobp)))
  12661. (org-flag-heading nil)
  12662. (when siblings-p (org-show-siblings)))))))
  12663. (defvar org-reveal-start-hook nil
  12664. "Hook run before revealing a location.")
  12665. (defun org-reveal (&optional siblings)
  12666. "Show current entry, hierarchy above it, and the following headline.
  12667. This can be used to show a consistent set of context around locations
  12668. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12669. not t for the search context.
  12670. With optional argument SIBLINGS, on each level of the hierarchy all
  12671. siblings are shown. This repairs the tree structure to what it would
  12672. look like when opened with hierarchical calls to `org-cycle'.
  12673. With double optional argument \\[universal-argument] \\[universal-argument], \
  12674. go to the parent and show the
  12675. entire tree."
  12676. (interactive "P")
  12677. (run-hooks 'org-reveal-start-hook)
  12678. (let ((org-show-hierarchy-above t)
  12679. (org-show-following-heading t)
  12680. (org-show-siblings (if siblings t org-show-siblings)))
  12681. (org-show-context nil))
  12682. (when (equal siblings '(16))
  12683. (save-excursion
  12684. (when (org-up-heading-safe)
  12685. (org-show-subtree)
  12686. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12687. (defun org-highlight-new-match (beg end)
  12688. "Highlight from BEG to END and mark the highlight is an occur headline."
  12689. (let ((ov (make-overlay beg end)))
  12690. (overlay-put ov 'face 'secondary-selection)
  12691. (overlay-put ov 'org-type 'org-occur)
  12692. (push ov org-occur-highlights)))
  12693. (defun org-remove-occur-highlights (&optional beg end noremove)
  12694. "Remove the occur highlights from the buffer.
  12695. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12696. from the `before-change-functions' in the current buffer."
  12697. (interactive)
  12698. (unless org-inhibit-highlight-removal
  12699. (mapc 'delete-overlay org-occur-highlights)
  12700. (setq org-occur-highlights nil)
  12701. (setq org-occur-parameters nil)
  12702. (unless noremove
  12703. (remove-hook 'before-change-functions
  12704. 'org-remove-occur-highlights 'local))))
  12705. ;;;; Priorities
  12706. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12707. "Regular expression matching the priority indicator.")
  12708. (defvar org-remove-priority-next-time nil)
  12709. (defun org-priority-up ()
  12710. "Increase the priority of the current item."
  12711. (interactive)
  12712. (org-priority 'up))
  12713. (defun org-priority-down ()
  12714. "Decrease the priority of the current item."
  12715. (interactive)
  12716. (org-priority 'down))
  12717. (defun org-priority (&optional action show)
  12718. "Change the priority of an item.
  12719. ACTION can be `set', `up', `down', or a character."
  12720. (interactive "P")
  12721. (if (equal action '(4))
  12722. (org-show-priority)
  12723. (unless org-enable-priority-commands
  12724. (user-error "Priority commands are disabled"))
  12725. (setq action (or action 'set))
  12726. (let (current new news have remove)
  12727. (save-excursion
  12728. (org-back-to-heading t)
  12729. (if (looking-at org-priority-regexp)
  12730. (setq current (string-to-char (match-string 2))
  12731. have t))
  12732. (cond
  12733. ((eq action 'remove)
  12734. (setq remove t new ?\ ))
  12735. ((or (eq action 'set)
  12736. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12737. (if (not (eq action 'set))
  12738. (setq new action)
  12739. (message "Priority %c-%c, SPC to remove: "
  12740. org-highest-priority org-lowest-priority)
  12741. (save-match-data
  12742. (setq new (read-char-exclusive))))
  12743. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12744. (= (upcase org-lowest-priority) org-lowest-priority))
  12745. (setq new (upcase new)))
  12746. (cond ((equal new ?\ ) (setq remove t))
  12747. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12748. (user-error "Priority must be between `%c' and `%c'"
  12749. org-highest-priority org-lowest-priority))))
  12750. ((eq action 'up)
  12751. (setq new (if have
  12752. (1- current) ; normal cycling
  12753. ;; last priority was empty
  12754. (if (eq last-command this-command)
  12755. org-lowest-priority ; wrap around empty to lowest
  12756. ;; default
  12757. (if org-priority-start-cycle-with-default
  12758. org-default-priority
  12759. (1- org-default-priority))))))
  12760. ((eq action 'down)
  12761. (setq new (if have
  12762. (1+ current) ; normal cycling
  12763. ;; last priority was empty
  12764. (if (eq last-command this-command)
  12765. org-highest-priority ; wrap around empty to highest
  12766. ;; default
  12767. (if org-priority-start-cycle-with-default
  12768. org-default-priority
  12769. (1+ org-default-priority))))))
  12770. (t (user-error "Invalid action")))
  12771. (if (or (< (upcase new) org-highest-priority)
  12772. (> (upcase new) org-lowest-priority))
  12773. (if (and (memq action '(up down))
  12774. (not have) (not (eq last-command this-command)))
  12775. ;; `new' is from default priority
  12776. (error
  12777. "The default can not be set, see `org-default-priority' why")
  12778. ;; normal cycling: `new' is beyond highest/lowest priority
  12779. ;; and is wrapped around to the empty priority
  12780. (setq remove t)))
  12781. (setq news (format "%c" new))
  12782. (if have
  12783. (if remove
  12784. (replace-match "" t t nil 1)
  12785. (replace-match news t t nil 2))
  12786. (if remove
  12787. (user-error "No priority cookie found in line")
  12788. (let ((case-fold-search nil))
  12789. (looking-at org-todo-line-regexp))
  12790. (if (match-end 2)
  12791. (progn
  12792. (goto-char (match-end 2))
  12793. (insert " [#" news "]"))
  12794. (goto-char (match-beginning 3))
  12795. (insert "[#" news "] "))))
  12796. (org-set-tags nil 'align))
  12797. (if remove
  12798. (message "Priority removed")
  12799. (message "Priority of current item set to %s" news)))))
  12800. (defun org-show-priority ()
  12801. "Show the priority of the current item.
  12802. This priority is composed of the main priority given with the [#A] cookies,
  12803. and by additional input from the age of a schedules or deadline entry."
  12804. (interactive)
  12805. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12806. (org-get-at-bol 'priority)
  12807. (save-excursion
  12808. (save-match-data
  12809. (beginning-of-line)
  12810. (and (looking-at org-heading-regexp)
  12811. (org-get-priority (match-string 0))))))))
  12812. (message "Priority is %d" (if pri pri -1000))))
  12813. (defun org-get-priority (s)
  12814. "Find priority cookie and return priority."
  12815. (save-match-data
  12816. (if (functionp org-get-priority-function)
  12817. (funcall org-get-priority-function)
  12818. (if (not (string-match org-priority-regexp s))
  12819. (* 1000 (- org-lowest-priority org-default-priority))
  12820. (* 1000 (- org-lowest-priority
  12821. (string-to-char (match-string 2 s))))))))
  12822. ;;;; Tags
  12823. (defvar org-agenda-archives-mode)
  12824. (defvar org-map-continue-from nil
  12825. "Position from where mapping should continue.
  12826. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12827. (defvar org-scanner-tags nil
  12828. "The current tag list while the tags scanner is running.")
  12829. (defvar org-trust-scanner-tags nil
  12830. "Should `org-get-tags-at' use the tags for the scanner.
  12831. This is for internal dynamical scoping only.
  12832. When this is non-nil, the function `org-get-tags-at' will return the value
  12833. of `org-scanner-tags' instead of building the list by itself. This
  12834. can lead to large speed-ups when the tags scanner is used in a file with
  12835. many entries, and when the list of tags is retrieved, for example to
  12836. obtain a list of properties. Building the tags list for each entry in such
  12837. a file becomes an N^2 operation - but with this variable set, it scales
  12838. as N.")
  12839. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12840. "Scan headline tags with inheritance and produce output ACTION.
  12841. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12842. or `agenda' to produce an entry list for an agenda view. It can also be
  12843. a Lisp form or a function that should be called at each matched headline, in
  12844. this case the return value is a list of all return values from these calls.
  12845. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12846. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12847. only lines with a not-done TODO keyword are included in the output.
  12848. This should be the same variable that was scoped into
  12849. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12850. START-LEVEL can be a string with asterisks, reducing the scope to
  12851. headlines matching this string."
  12852. (require 'org-agenda)
  12853. (let* ((re (concat "^"
  12854. (if start-level
  12855. ;; Get the correct level to match
  12856. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12857. org-outline-regexp)
  12858. " *\\(\\<\\("
  12859. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12860. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12861. (props (list 'face 'default
  12862. 'done-face 'org-agenda-done
  12863. 'undone-face 'default
  12864. 'mouse-face 'highlight
  12865. 'org-not-done-regexp org-not-done-regexp
  12866. 'org-todo-regexp org-todo-regexp
  12867. 'org-complex-heading-regexp org-complex-heading-regexp
  12868. 'help-echo
  12869. (format "mouse-2 or RET jump to org file %s"
  12870. (abbreviate-file-name
  12871. (or (buffer-file-name (buffer-base-buffer))
  12872. (buffer-name (buffer-base-buffer)))))))
  12873. (org-map-continue-from nil)
  12874. lspos tags tags-list
  12875. (tags-alist (list (cons 0 org-file-tags)))
  12876. (llast 0) rtn rtn1 level category i txt
  12877. todo marker entry priority)
  12878. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12879. (setq action (list 'lambda nil action)))
  12880. (save-excursion
  12881. (goto-char (point-min))
  12882. (when (eq action 'sparse-tree)
  12883. (org-overview)
  12884. (org-remove-occur-highlights))
  12885. (while (let (case-fold-search)
  12886. (re-search-forward re nil t))
  12887. (setq org-map-continue-from nil)
  12888. (catch :skip
  12889. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12890. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12891. (goto-char (setq lspos (match-beginning 0)))
  12892. (setq level (org-reduced-level (org-outline-level))
  12893. category (org-get-category))
  12894. (setq i llast llast level)
  12895. ;; remove tag lists from same and sublevels
  12896. (while (>= i level)
  12897. (when (setq entry (assoc i tags-alist))
  12898. (setq tags-alist (delete entry tags-alist)))
  12899. (setq i (1- i)))
  12900. ;; add the next tags
  12901. (when tags
  12902. (setq tags (org-split-string tags ":")
  12903. tags-alist
  12904. (cons (cons level tags) tags-alist)))
  12905. ;; compile tags for current headline
  12906. (setq tags-list
  12907. (if org-use-tag-inheritance
  12908. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12909. tags)
  12910. org-scanner-tags tags-list)
  12911. (when org-use-tag-inheritance
  12912. (setcdr (car tags-alist)
  12913. (mapcar (lambda (x)
  12914. (setq x (copy-sequence x))
  12915. (org-add-prop-inherited x))
  12916. (cdar tags-alist))))
  12917. (when (and tags org-use-tag-inheritance
  12918. (or (not (eq t org-use-tag-inheritance))
  12919. org-tags-exclude-from-inheritance))
  12920. ;; selective inheritance, remove uninherited ones
  12921. (setcdr (car tags-alist)
  12922. (org-remove-uninherited-tags (cdar tags-alist))))
  12923. (when (and
  12924. ;; eval matcher only when the todo condition is OK
  12925. (and (or (not todo-only) (member todo org-not-done-keywords))
  12926. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12927. (eval matcher)))
  12928. ;; Call the skipper, but return t if it does not skip,
  12929. ;; so that the `and' form continues evaluating
  12930. (progn
  12931. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12932. t)
  12933. ;; Check if timestamps are deselecting this entry
  12934. (or (not todo-only)
  12935. (and (member todo org-not-done-keywords)
  12936. (or (not org-agenda-tags-todo-honor-ignore-options)
  12937. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12938. ;; select this headline
  12939. (cond
  12940. ((eq action 'sparse-tree)
  12941. (and org-highlight-sparse-tree-matches
  12942. (org-get-heading) (match-end 0)
  12943. (org-highlight-new-match
  12944. (match-beginning 1) (match-end 1)))
  12945. (org-show-context 'tags-tree))
  12946. ((eq action 'agenda)
  12947. (setq txt (org-agenda-format-item
  12948. ""
  12949. (concat
  12950. (if (eq org-tags-match-list-sublevels 'indented)
  12951. (make-string (1- level) ?.) "")
  12952. (org-get-heading))
  12953. level category
  12954. tags-list)
  12955. priority (org-get-priority txt))
  12956. (goto-char lspos)
  12957. (setq marker (org-agenda-new-marker))
  12958. (org-add-props txt props
  12959. 'org-marker marker 'org-hd-marker marker 'org-category category
  12960. 'todo-state todo
  12961. 'priority priority 'type "tagsmatch")
  12962. (push txt rtn))
  12963. ((functionp action)
  12964. (setq org-map-continue-from nil)
  12965. (save-excursion
  12966. (setq rtn1 (funcall action))
  12967. (push rtn1 rtn)))
  12968. (t (user-error "Invalid action")))
  12969. ;; if we are to skip sublevels, jump to end of subtree
  12970. (unless org-tags-match-list-sublevels
  12971. (org-end-of-subtree t)
  12972. (backward-char 1))))
  12973. ;; Get the correct position from where to continue
  12974. (if org-map-continue-from
  12975. (goto-char org-map-continue-from)
  12976. (and (= (point) lspos) (end-of-line 1)))))
  12977. (when (and (eq action 'sparse-tree)
  12978. (not org-sparse-tree-open-archived-trees))
  12979. (org-hide-archived-subtrees (point-min) (point-max)))
  12980. (nreverse rtn)))
  12981. (defun org-remove-uninherited-tags (tags)
  12982. "Remove all tags that are not inherited from the list TAGS."
  12983. (cond
  12984. ((eq org-use-tag-inheritance t)
  12985. (if org-tags-exclude-from-inheritance
  12986. (org-delete-all org-tags-exclude-from-inheritance tags)
  12987. tags))
  12988. ((not org-use-tag-inheritance) nil)
  12989. ((stringp org-use-tag-inheritance)
  12990. (delq nil (mapcar
  12991. (lambda (x)
  12992. (if (and (string-match org-use-tag-inheritance x)
  12993. (not (member x org-tags-exclude-from-inheritance)))
  12994. x nil))
  12995. tags)))
  12996. ((listp org-use-tag-inheritance)
  12997. (delq nil (mapcar
  12998. (lambda (x)
  12999. (if (member x org-use-tag-inheritance) x nil))
  13000. tags)))))
  13001. (defun org-match-sparse-tree (&optional todo-only match)
  13002. "Create a sparse tree according to tags string MATCH.
  13003. MATCH can contain positive and negative selection of tags, like
  13004. \"+WORK+URGENT-WITHBOSS\".
  13005. If optional argument TODO-ONLY is non-nil, only select lines that are
  13006. also TODO lines."
  13007. (interactive "P")
  13008. (org-agenda-prepare-buffers (list (current-buffer)))
  13009. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13010. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13011. (defvar org-cached-props nil)
  13012. (defun org-cached-entry-get (pom property)
  13013. (if (or (eq t org-use-property-inheritance)
  13014. (and (stringp org-use-property-inheritance)
  13015. (string-match org-use-property-inheritance property))
  13016. (and (listp org-use-property-inheritance)
  13017. (member property org-use-property-inheritance)))
  13018. ;; Caching is not possible, check it directly
  13019. (org-entry-get pom property 'inherit)
  13020. ;; Get all properties, so that we can do complicated checks easily
  13021. (cdr (assoc property (or org-cached-props
  13022. (setq org-cached-props
  13023. (org-entry-properties pom)))))))
  13024. (defun org-global-tags-completion-table (&optional files)
  13025. "Return the list of all tags in all agenda buffer/files.
  13026. Optional FILES argument is a list of files which can be used
  13027. instead of the agenda files."
  13028. (save-excursion
  13029. (org-uniquify
  13030. (delq nil
  13031. (apply 'append
  13032. (mapcar
  13033. (lambda (file)
  13034. (set-buffer (find-file-noselect file))
  13035. (append (org-get-buffer-tags)
  13036. (mapcar (lambda (x) (if (stringp (car-safe x))
  13037. (list (car-safe x)) nil))
  13038. org-tag-alist)))
  13039. (if (and files (car files))
  13040. files
  13041. (org-agenda-files))))))))
  13042. (defun org-make-tags-matcher (match)
  13043. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13044. The variable `todo-only' is scoped dynamically into this function.
  13045. It will be set to t if the matcher restricts matching to TODO entries,
  13046. otherwise will not be touched.
  13047. Returns a cons of the selection string MATCH and the constructed
  13048. lisp form implementing the matcher. The matcher is to be evaluated
  13049. at an Org entry, with point on the headline, and returns t if the
  13050. entry matches the selection string MATCH. The returned lisp form
  13051. references two variables with information about the entry, which
  13052. must be bound around the form's evaluation: todo, the TODO keyword
  13053. at the entry (or nil of none); and tags-list, the list of all tags
  13054. at the entry including inherited ones. Additionally, the category
  13055. of the entry (if any) must be specified as the text property
  13056. 'org-category on the headline.
  13057. See also `org-scan-tags'.
  13058. "
  13059. (declare (special todo-only))
  13060. (unless (boundp 'todo-only)
  13061. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13062. (unless match
  13063. ;; Get a new match request, with completion against the global
  13064. ;; tags table and the local tags in current buffer
  13065. (let ((org-last-tags-completion-table
  13066. (org-uniquify
  13067. (delq nil (append (org-get-buffer-tags)
  13068. (org-global-tags-completion-table))))))
  13069. (setq match (org-completing-read-no-i
  13070. "Match: " 'org-tags-completion-function nil nil nil
  13071. 'org-tags-history))))
  13072. ;; Parse the string and create a lisp form
  13073. (let ((match0 match)
  13074. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13075. minus tag mm
  13076. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13077. orterms term orlist re-p str-p level-p level-op time-p
  13078. prop-p pn pv po gv rest (start 0) (ss 0))
  13079. ;; Expand group tags
  13080. (setq match (org-tags-expand match))
  13081. ;; Check if there is a TODO part of this match, which would be the
  13082. ;; part after a "/". TO make sure that this slash is not part of
  13083. ;; a property value to be matched against, we also check that there
  13084. ;; is no " after that slash.
  13085. ;; First, find the last slash
  13086. (while (string-match "/+" match ss)
  13087. (setq start (match-beginning 0) ss (match-end 0)))
  13088. (if (and (string-match "/+" match start)
  13089. (not (save-match-data (string-match "\"" match start))))
  13090. ;; match contains also a todo-matching request
  13091. (progn
  13092. (setq tagsmatch (substring match 0 (match-beginning 0))
  13093. todomatch (substring match (match-end 0)))
  13094. (if (string-match "^!" todomatch)
  13095. (setq todo-only t todomatch (substring todomatch 1)))
  13096. (if (string-match "^\\s-*$" todomatch)
  13097. (setq todomatch nil)))
  13098. ;; only matching tags
  13099. (setq tagsmatch match todomatch nil))
  13100. ;; Make the tags matcher
  13101. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13102. (setq tagsmatcher t)
  13103. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13104. (while (setq term (pop orterms))
  13105. (while (and (equal (substring term -1) "\\") orterms)
  13106. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13107. (while (string-match re term)
  13108. (setq rest (substring term (match-end 0))
  13109. minus (and (match-end 1)
  13110. (equal (match-string 1 term) "-"))
  13111. tag (save-match-data (replace-regexp-in-string
  13112. "\\\\-" "-"
  13113. (match-string 2 term)))
  13114. re-p (equal (string-to-char tag) ?{)
  13115. level-p (match-end 4)
  13116. prop-p (match-end 5)
  13117. mm (cond
  13118. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13119. (level-p
  13120. (setq level-op (org-op-to-function (match-string 3 term)))
  13121. `(,level-op level ,(string-to-number
  13122. (match-string 4 term))))
  13123. (prop-p
  13124. (setq pn (match-string 5 term)
  13125. po (match-string 6 term)
  13126. pv (match-string 7 term)
  13127. re-p (equal (string-to-char pv) ?{)
  13128. str-p (equal (string-to-char pv) ?\")
  13129. time-p (save-match-data
  13130. (string-match "^\"[[<].*[]>]\"$" pv))
  13131. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13132. (if time-p (setq pv (org-matcher-time pv)))
  13133. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13134. (cond
  13135. ((equal pn "CATEGORY")
  13136. (setq gv '(get-text-property (point) 'org-category)))
  13137. ((equal pn "TODO")
  13138. (setq gv 'todo))
  13139. (t
  13140. (setq gv `(org-cached-entry-get nil ,pn))))
  13141. (if re-p
  13142. (if (eq po 'org<>)
  13143. `(not (string-match ,pv (or ,gv "")))
  13144. `(string-match ,pv (or ,gv "")))
  13145. (if str-p
  13146. `(,po (or ,gv "") ,pv)
  13147. `(,po (string-to-number (or ,gv ""))
  13148. ,(string-to-number pv) ))))
  13149. (t `(member ,tag tags-list)))
  13150. mm (if minus (list 'not mm) mm)
  13151. term rest)
  13152. (push mm tagsmatcher))
  13153. (push (if (> (length tagsmatcher) 1)
  13154. (cons 'and tagsmatcher)
  13155. (car tagsmatcher))
  13156. orlist)
  13157. (setq tagsmatcher nil))
  13158. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13159. (setq tagsmatcher
  13160. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13161. ;; Make the todo matcher
  13162. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13163. (setq todomatcher t)
  13164. (setq orterms (org-split-string todomatch "|") orlist nil)
  13165. (while (setq term (pop orterms))
  13166. (while (string-match re term)
  13167. (setq minus (and (match-end 1)
  13168. (equal (match-string 1 term) "-"))
  13169. kwd (match-string 2 term)
  13170. re-p (equal (string-to-char kwd) ?{)
  13171. term (substring term (match-end 0))
  13172. mm (if re-p
  13173. `(string-match ,(substring kwd 1 -1) todo)
  13174. (list 'equal 'todo kwd))
  13175. mm (if minus (list 'not mm) mm))
  13176. (push mm todomatcher))
  13177. (push (if (> (length todomatcher) 1)
  13178. (cons 'and todomatcher)
  13179. (car todomatcher))
  13180. orlist)
  13181. (setq todomatcher nil))
  13182. (setq todomatcher (if (> (length orlist) 1)
  13183. (cons 'or orlist) (car orlist))))
  13184. ;; Return the string and lisp forms of the matcher
  13185. (setq matcher (if todomatcher
  13186. (list 'and tagsmatcher todomatcher)
  13187. tagsmatcher))
  13188. (when todo-only
  13189. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13190. matcher)))
  13191. (cons match0 matcher)))
  13192. (defun org-tags-expand (match &optional single-as-list downcased)
  13193. "Expand group tags in MATCH.
  13194. This replaces every group tag in MATCH with a regexp tag search.
  13195. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13196. will be replaced like this:
  13197. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  13198. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  13199. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  13200. Replacing by a regexp preserves the structure of the match.
  13201. E.g., this expansion
  13202. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13203. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13204. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13205. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13206. assumed to be a single group tag, and the function will return
  13207. the list of tags in this group.
  13208. When DOWNCASE is non-nil, expand downcased TAGS."
  13209. (if org-group-tags
  13210. (let* ((case-fold-search t)
  13211. (stable org-mode-syntax-table)
  13212. (tal (or org-tag-groups-alist-for-agenda
  13213. org-tag-groups-alist))
  13214. (tal (if downcased
  13215. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  13216. (tml (mapcar 'car tal))
  13217. (rtnmatch match) rpl)
  13218. ;; @ and _ are allowed as word-components in tags
  13219. (modify-syntax-entry ?@ "w" stable)
  13220. (modify-syntax-entry ?_ "w" stable)
  13221. (while (and tml
  13222. (with-syntax-table stable
  13223. (string-match
  13224. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13225. (regexp-opt tml) "\\>\\)") rtnmatch)))
  13226. (let* ((dir (match-string 1 rtnmatch))
  13227. (tag (match-string 2 rtnmatch))
  13228. (tag (if downcased (downcase tag) tag)))
  13229. (setq tml (delete tag tml))
  13230. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  13231. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  13232. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  13233. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  13234. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  13235. (if single-as-list
  13236. (or (reverse rpl) (list rtnmatch))
  13237. rtnmatch))
  13238. (if single-as-list (list (if downcased (downcase match) match))
  13239. match)))
  13240. (defun org-op-to-function (op &optional stringp)
  13241. "Turn an operator into the appropriate function."
  13242. (setq op
  13243. (cond
  13244. ((equal op "<" ) '(< string< org-time<))
  13245. ((equal op ">" ) '(> org-string> org-time>))
  13246. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13247. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13248. ((member op '("=" "==")) '(= string= org-time=))
  13249. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13250. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13251. (defun org<> (a b) (not (= a b)))
  13252. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13253. (defun org-string>= (a b) (not (string< a b)))
  13254. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13255. (defun org-string<> (a b) (not (string= a b)))
  13256. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13257. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13258. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13259. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13260. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13261. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13262. (defun org-2ft (s)
  13263. "Convert S to a floating point time.
  13264. If S is already a number, just return it. If it is a string, parse
  13265. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13266. (cond
  13267. ((numberp s) s)
  13268. ((stringp s)
  13269. (condition-case nil
  13270. (float-time (apply 'encode-time (org-parse-time-string s)))
  13271. (error 0.)))
  13272. (t 0.)))
  13273. (defun org-time-today ()
  13274. "Time in seconds today at 0:00.
  13275. Returns the float number of seconds since the beginning of the
  13276. epoch to the beginning of today (00:00)."
  13277. (float-time (apply 'encode-time
  13278. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13279. (defun org-matcher-time (s)
  13280. "Interpret a time comparison value."
  13281. (save-match-data
  13282. (cond
  13283. ((string= s "<now>") (float-time))
  13284. ((string= s "<today>") (org-time-today))
  13285. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13286. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13287. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13288. (+ (org-time-today)
  13289. (* (string-to-number (match-string 1 s))
  13290. (cdr (assoc (match-string 2 s)
  13291. '(("d" . 86400.0) ("w" . 604800.0)
  13292. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13293. (t (org-2ft s)))))
  13294. (defun org-match-any-p (re list)
  13295. "Does re match any element of list?"
  13296. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13297. (delq nil list))
  13298. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13299. (defvar org-tags-overlay (make-overlay 1 1))
  13300. (org-detach-overlay org-tags-overlay)
  13301. (defun org-get-local-tags-at (&optional pos)
  13302. "Get a list of tags defined in the current headline."
  13303. (org-get-tags-at pos 'local))
  13304. (defun org-get-local-tags ()
  13305. "Get a list of tags defined in the current headline."
  13306. (org-get-tags-at nil 'local))
  13307. (defun org-get-tags-at (&optional pos local)
  13308. "Get a list of all headline tags applicable at POS.
  13309. POS defaults to point. If tags are inherited, the list contains
  13310. the targets in the same sequence as the headlines appear, i.e.
  13311. the tags of the current headline come last.
  13312. When LOCAL is non-nil, only return tags from the current headline,
  13313. ignore inherited ones."
  13314. (interactive)
  13315. (if (and org-trust-scanner-tags
  13316. (or (not pos) (equal pos (point)))
  13317. (not local))
  13318. org-scanner-tags
  13319. (let (tags ltags lastpos parent)
  13320. (save-excursion
  13321. (save-restriction
  13322. (widen)
  13323. (goto-char (or pos (point)))
  13324. (save-match-data
  13325. (catch 'done
  13326. (condition-case nil
  13327. (progn
  13328. (org-back-to-heading t)
  13329. (while (not (equal lastpos (point)))
  13330. (setq lastpos (point))
  13331. (when (looking-at
  13332. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13333. (setq ltags (org-split-string
  13334. (org-match-string-no-properties 1) ":"))
  13335. (when parent
  13336. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13337. (setq tags (append
  13338. (if parent
  13339. (org-remove-uninherited-tags ltags)
  13340. ltags)
  13341. tags)))
  13342. (or org-use-tag-inheritance (throw 'done t))
  13343. (if local (throw 'done t))
  13344. (or (org-up-heading-safe) (error nil))
  13345. (setq parent t)))
  13346. (error nil)))))
  13347. (if local
  13348. tags
  13349. (reverse (delete-dups
  13350. (reverse (append
  13351. (org-remove-uninherited-tags
  13352. org-file-tags) tags)))))))))
  13353. (defun org-add-prop-inherited (s)
  13354. (add-text-properties 0 (length s) '(inherited t) s)
  13355. s)
  13356. (defun org-toggle-tag (tag &optional onoff)
  13357. "Toggle the tag TAG for the current line.
  13358. If ONOFF is `on' or `off', don't toggle but set to this state."
  13359. (let (res current)
  13360. (save-excursion
  13361. (org-back-to-heading t)
  13362. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13363. (point-at-eol) t)
  13364. (progn
  13365. (setq current (match-string 1))
  13366. (replace-match ""))
  13367. (setq current ""))
  13368. (setq current (nreverse (org-split-string current ":")))
  13369. (cond
  13370. ((eq onoff 'on)
  13371. (setq res t)
  13372. (or (member tag current) (push tag current)))
  13373. ((eq onoff 'off)
  13374. (or (not (member tag current)) (setq current (delete tag current))))
  13375. (t (if (member tag current)
  13376. (setq current (delete tag current))
  13377. (setq res t)
  13378. (push tag current))))
  13379. (end-of-line 1)
  13380. (if current
  13381. (progn
  13382. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13383. (org-set-tags nil t))
  13384. (delete-horizontal-space))
  13385. (run-hooks 'org-after-tags-change-hook))
  13386. res))
  13387. (defun org-align-tags-here (to-col)
  13388. ;; Assumes that this is a headline
  13389. "Align tags on the current headline to TO-COL."
  13390. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13391. (beginning-of-line 1)
  13392. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13393. (< pos (match-beginning 2)))
  13394. (progn
  13395. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13396. (goto-char (match-beginning 1))
  13397. (insert " ")
  13398. (delete-region (point) (1+ (match-beginning 2)))
  13399. (setq ncol (max (current-column)
  13400. (1+ col)
  13401. (if (> to-col 0)
  13402. to-col
  13403. (- (abs to-col) tags-l))))
  13404. (setq p (point))
  13405. (insert (make-string (- ncol (current-column)) ?\ ))
  13406. (setq ncol (current-column))
  13407. (when indent-tabs-mode (tabify p (point-at-eol)))
  13408. (org-move-to-column (min ncol col)))
  13409. (goto-char pos))))
  13410. (defun org-set-tags-command (&optional arg just-align)
  13411. "Call the set-tags command for the current entry."
  13412. (interactive "P")
  13413. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13414. (org-set-tags arg just-align)
  13415. (save-excursion
  13416. (unless (and (org-region-active-p)
  13417. org-loop-over-headlines-in-active-region)
  13418. (org-back-to-heading t))
  13419. (org-set-tags arg just-align))))
  13420. (defun org-set-tags-to (data)
  13421. "Set the tags of the current entry to DATA, replacing the current tags.
  13422. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13423. If DATA is nil or the empty string, any tags will be removed."
  13424. (interactive "sTags: ")
  13425. (setq data
  13426. (cond
  13427. ((eq data nil) "")
  13428. ((equal data "") "")
  13429. ((stringp data)
  13430. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13431. ":"))
  13432. ((listp data)
  13433. (concat ":" (mapconcat 'identity data ":") ":"))))
  13434. (when data
  13435. (save-excursion
  13436. (org-back-to-heading t)
  13437. (when (looking-at org-complex-heading-regexp)
  13438. (if (match-end 5)
  13439. (progn
  13440. (goto-char (match-beginning 5))
  13441. (insert data)
  13442. (delete-region (point) (point-at-eol))
  13443. (org-set-tags nil 'align))
  13444. (goto-char (point-at-eol))
  13445. (insert " " data)
  13446. (org-set-tags nil 'align)))
  13447. (beginning-of-line 1)
  13448. (if (looking-at ".*?\\([ \t]+\\)$")
  13449. (delete-region (match-beginning 1) (match-end 1))))))
  13450. (defun org-align-all-tags ()
  13451. "Align the tags i all headings."
  13452. (interactive)
  13453. (save-excursion
  13454. (or (ignore-errors (org-back-to-heading t))
  13455. (outline-next-heading))
  13456. (if (org-at-heading-p)
  13457. (org-set-tags t)
  13458. (message "No headings"))))
  13459. (defvar org-indent-indentation-per-level)
  13460. (defun org-set-tags (&optional arg just-align)
  13461. "Set the tags for the current headline.
  13462. With prefix ARG, realign all tags in headings in the current buffer.
  13463. When JUST-ALIGN is non-nil, only align tags."
  13464. (interactive "P")
  13465. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13466. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13467. 'region-start-level 'region))
  13468. org-loop-over-headlines-in-active-region)
  13469. (org-map-entries
  13470. ;; We don't use ARG and JUST-ALIGN here because these args
  13471. ;; are not useful when looping over headlines.
  13472. `(org-set-tags)
  13473. org-loop-over-headlines-in-active-region
  13474. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13475. (let* ((re org-outline-regexp-bol)
  13476. (current (unless arg (org-get-tags-string)))
  13477. (col (current-column))
  13478. (org-setting-tags t)
  13479. table current-tags inherited-tags ; computed below when needed
  13480. tags p0 c0 c1 rpl di tc level)
  13481. (if arg
  13482. (save-excursion
  13483. (goto-char (point-min))
  13484. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13485. (while (re-search-forward re nil t)
  13486. (org-set-tags nil t)
  13487. (end-of-line 1)))
  13488. (message "All tags realigned to column %d" org-tags-column))
  13489. (if just-align
  13490. (setq tags current)
  13491. ;; Get a new set of tags from the user
  13492. (save-excursion
  13493. (setq table (append org-tag-persistent-alist
  13494. (or org-tag-alist (org-get-buffer-tags))
  13495. (and
  13496. org-complete-tags-always-offer-all-agenda-tags
  13497. (org-global-tags-completion-table
  13498. (org-agenda-files))))
  13499. org-last-tags-completion-table table
  13500. current-tags (org-split-string current ":")
  13501. inherited-tags (nreverse
  13502. (nthcdr (length current-tags)
  13503. (nreverse (org-get-tags-at))))
  13504. tags
  13505. (if (or (eq t org-use-fast-tag-selection)
  13506. (and org-use-fast-tag-selection
  13507. (delq nil (mapcar 'cdr table))))
  13508. (org-fast-tag-selection
  13509. current-tags inherited-tags table
  13510. (if org-fast-tag-selection-include-todo
  13511. org-todo-key-alist))
  13512. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13513. (org-trim
  13514. (org-icompleting-read "Tags: "
  13515. 'org-tags-completion-function
  13516. nil nil current 'org-tags-history))))))
  13517. (while (string-match "[-+&]+" tags)
  13518. ;; No boolean logic, just a list
  13519. (setq tags (replace-match ":" t t tags))))
  13520. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13521. (if org-tags-sort-function
  13522. (setq tags (mapconcat 'identity
  13523. (sort (org-split-string
  13524. tags (org-re "[^[:alnum:]_@#%]+"))
  13525. org-tags-sort-function) ":")))
  13526. (if (string-match "\\`[\t ]*\\'" tags)
  13527. (setq tags "")
  13528. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13529. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13530. ;; Insert new tags at the correct column
  13531. (beginning-of-line 1)
  13532. (setq level (or (and (looking-at org-outline-regexp)
  13533. (- (match-end 0) (point) 1))
  13534. 1))
  13535. (cond
  13536. ((and (equal current "") (equal tags "")))
  13537. ((re-search-forward
  13538. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13539. (point-at-eol) t)
  13540. (if (equal tags "")
  13541. (setq rpl "")
  13542. (goto-char (match-beginning 0))
  13543. (setq c0 (current-column)
  13544. ;; compute offset for the case of org-indent-mode active
  13545. di (if (org-bound-and-true-p org-indent-mode)
  13546. (* (1- org-indent-indentation-per-level) (1- level))
  13547. 0)
  13548. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13549. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13550. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (string-width tags))))
  13551. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13552. (replace-match rpl t t)
  13553. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13554. tags)
  13555. (t (error "Tags alignment failed")))
  13556. (org-move-to-column col)
  13557. (unless just-align
  13558. (run-hooks 'org-after-tags-change-hook))))))
  13559. (defun org-change-tag-in-region (beg end tag off)
  13560. "Add or remove TAG for each entry in the region.
  13561. This works in the agenda, and also in an org-mode buffer."
  13562. (interactive
  13563. (list (region-beginning) (region-end)
  13564. (let ((org-last-tags-completion-table
  13565. (if (derived-mode-p 'org-mode)
  13566. (org-uniquify
  13567. (delq nil (append (org-get-buffer-tags)
  13568. (org-global-tags-completion-table))))
  13569. (org-global-tags-completion-table))))
  13570. (org-icompleting-read
  13571. "Tag: " 'org-tags-completion-function nil nil nil
  13572. 'org-tags-history))
  13573. (progn
  13574. (message "[s]et or [r]emove? ")
  13575. (equal (read-char-exclusive) ?r))))
  13576. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13577. (let ((agendap (equal major-mode 'org-agenda-mode))
  13578. l1 l2 m buf pos newhead (cnt 0))
  13579. (goto-char end)
  13580. (setq l2 (1- (org-current-line)))
  13581. (goto-char beg)
  13582. (setq l1 (org-current-line))
  13583. (loop for l from l1 to l2 do
  13584. (org-goto-line l)
  13585. (setq m (get-text-property (point) 'org-hd-marker))
  13586. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13587. (and agendap m))
  13588. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13589. pos (if agendap m (point)))
  13590. (with-current-buffer buf
  13591. (save-excursion
  13592. (save-restriction
  13593. (goto-char pos)
  13594. (setq cnt (1+ cnt))
  13595. (org-toggle-tag tag (if off 'off 'on))
  13596. (setq newhead (org-get-heading)))))
  13597. (and agendap (org-agenda-change-all-lines newhead m))))
  13598. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13599. (defun org-tags-completion-function (string predicate &optional flag)
  13600. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13601. (confirm (lambda (x) (stringp (car x)))))
  13602. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13603. (setq s1 (match-string 1 string)
  13604. s2 (match-string 2 string))
  13605. (setq s1 "" s2 string))
  13606. (cond
  13607. ((eq flag nil)
  13608. ;; try completion
  13609. (setq rtn (try-completion s2 ctable confirm))
  13610. (if (stringp rtn)
  13611. (setq rtn
  13612. (concat s1 s2 (substring rtn (length s2))
  13613. (if (and org-add-colon-after-tag-completion
  13614. (assoc rtn ctable))
  13615. ":" ""))))
  13616. rtn)
  13617. ((eq flag t)
  13618. ;; all-completions
  13619. (all-completions s2 ctable confirm))
  13620. ((eq flag 'lambda)
  13621. ;; exact match?
  13622. (assoc s2 ctable)))))
  13623. (defun org-fast-tag-insert (kwd tags face &optional end)
  13624. "Insert KDW, and the TAGS, the latter with face FACE.
  13625. Also insert END."
  13626. (insert (format "%-12s" (concat kwd ":"))
  13627. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13628. (or end "")))
  13629. (defun org-fast-tag-show-exit (flag)
  13630. (save-excursion
  13631. (org-goto-line 3)
  13632. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13633. (replace-match ""))
  13634. (when flag
  13635. (end-of-line 1)
  13636. (org-move-to-column (- (window-width) 19) t)
  13637. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13638. (defun org-set-current-tags-overlay (current prefix)
  13639. "Add an overlay to CURRENT tag with PREFIX."
  13640. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13641. (if (featurep 'xemacs)
  13642. (org-overlay-display org-tags-overlay (concat prefix s)
  13643. 'secondary-selection)
  13644. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13645. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13646. (defvar org-last-tag-selection-key nil)
  13647. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13648. "Fast tag selection with single keys.
  13649. CURRENT is the current list of tags in the headline, INHERITED is the
  13650. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13651. possibly with grouping information. TODO-TABLE is a similar table with
  13652. TODO keywords, should these have keys assigned to them.
  13653. If the keys are nil, a-z are automatically assigned.
  13654. Returns the new tags string, or nil to not change the current settings."
  13655. (let* ((fulltable (append table todo-table))
  13656. (maxlen (apply 'max (mapcar
  13657. (lambda (x)
  13658. (if (stringp (car x)) (string-width (car x)) 0))
  13659. fulltable)))
  13660. (buf (current-buffer))
  13661. (expert (eq org-fast-tag-selection-single-key 'expert))
  13662. (buffer-tags nil)
  13663. (fwidth (+ maxlen 3 1 3))
  13664. (ncol (/ (- (window-width) 4) fwidth))
  13665. (i-face 'org-done)
  13666. (c-face 'org-todo)
  13667. tg cnt e c char c1 c2 ntable tbl rtn
  13668. ov-start ov-end ov-prefix
  13669. (exit-after-next org-fast-tag-selection-single-key)
  13670. (done-keywords org-done-keywords)
  13671. groups ingroup)
  13672. (save-excursion
  13673. (beginning-of-line 1)
  13674. (if (looking-at
  13675. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13676. (setq ov-start (match-beginning 1)
  13677. ov-end (match-end 1)
  13678. ov-prefix "")
  13679. (setq ov-start (1- (point-at-eol))
  13680. ov-end (1+ ov-start))
  13681. (skip-chars-forward "^\n\r")
  13682. (setq ov-prefix
  13683. (concat
  13684. (buffer-substring (1- (point)) (point))
  13685. (if (> (current-column) org-tags-column)
  13686. " "
  13687. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13688. (move-overlay org-tags-overlay ov-start ov-end)
  13689. (save-window-excursion
  13690. (if expert
  13691. (set-buffer (get-buffer-create " *Org tags*"))
  13692. (delete-other-windows)
  13693. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13694. (org-switch-to-buffer-other-window " *Org tags*"))
  13695. (erase-buffer)
  13696. (org-set-local 'org-done-keywords done-keywords)
  13697. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13698. (org-fast-tag-insert "Current" current c-face "\n\n")
  13699. (org-fast-tag-show-exit exit-after-next)
  13700. (org-set-current-tags-overlay current ov-prefix)
  13701. (setq tbl fulltable char ?a cnt 0)
  13702. (while (setq e (pop tbl))
  13703. (cond
  13704. ((equal (car e) :startgroup)
  13705. (push '() groups) (setq ingroup t)
  13706. (when (not (= cnt 0))
  13707. (setq cnt 0)
  13708. (insert "\n"))
  13709. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13710. ((equal (car e) :endgroup)
  13711. (setq ingroup nil cnt 0)
  13712. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13713. ((equal e '(:newline))
  13714. (when (not (= cnt 0))
  13715. (setq cnt 0)
  13716. (insert "\n")
  13717. (setq e (car tbl))
  13718. (while (equal (car tbl) '(:newline))
  13719. (insert "\n")
  13720. (setq tbl (cdr tbl)))))
  13721. ((equal e '(:grouptags)) nil)
  13722. (t
  13723. (setq tg (copy-sequence (car e)) c2 nil)
  13724. (if (cdr e)
  13725. (setq c (cdr e))
  13726. ;; automatically assign a character.
  13727. (setq c1 (string-to-char
  13728. (downcase (substring
  13729. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13730. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13731. (while (or (rassoc char ntable) (rassoc char table))
  13732. (setq char (1+ char)))
  13733. (setq c2 c1))
  13734. (setq c (or c2 char)))
  13735. (if ingroup (push tg (car groups)))
  13736. (setq tg (org-add-props tg nil 'face
  13737. (cond
  13738. ((not (assoc tg table))
  13739. (org-get-todo-face tg))
  13740. ((member tg current) c-face)
  13741. ((member tg inherited) i-face))))
  13742. (if (equal (caar tbl) :grouptags)
  13743. (org-add-props tg nil 'face 'org-tag-group))
  13744. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13745. (insert "[" c "] " tg (make-string
  13746. (- fwidth 4 (length tg)) ?\ ))
  13747. (push (cons tg c) ntable)
  13748. (when (= (setq cnt (1+ cnt)) ncol)
  13749. (insert "\n")
  13750. (if ingroup (insert " "))
  13751. (setq cnt 0)))))
  13752. (setq ntable (nreverse ntable))
  13753. (insert "\n")
  13754. (goto-char (point-min))
  13755. (if (not expert) (org-fit-window-to-buffer))
  13756. (setq rtn
  13757. (catch 'exit
  13758. (while t
  13759. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13760. (if (not groups) "no " "")
  13761. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13762. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13763. (setq org-last-tag-selection-key c)
  13764. (cond
  13765. ((= c ?\r) (throw 'exit t))
  13766. ((= c ?!)
  13767. (setq groups (not groups))
  13768. (goto-char (point-min))
  13769. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13770. ((= c ?\C-c)
  13771. (if (not expert)
  13772. (org-fast-tag-show-exit
  13773. (setq exit-after-next (not exit-after-next)))
  13774. (setq expert nil)
  13775. (delete-other-windows)
  13776. (set-window-buffer (split-window-vertically) " *Org tags*")
  13777. (org-switch-to-buffer-other-window " *Org tags*")
  13778. (org-fit-window-to-buffer)))
  13779. ((or (= c ?\C-g)
  13780. (and (= c ?q) (not (rassoc c ntable))))
  13781. (org-detach-overlay org-tags-overlay)
  13782. (setq quit-flag t))
  13783. ((= c ?\ )
  13784. (setq current nil)
  13785. (if exit-after-next (setq exit-after-next 'now)))
  13786. ((= c ?\t)
  13787. (condition-case nil
  13788. (setq tg (org-icompleting-read
  13789. "Tag: "
  13790. (or buffer-tags
  13791. (with-current-buffer buf
  13792. (org-get-buffer-tags)))))
  13793. (quit (setq tg "")))
  13794. (when (string-match "\\S-" tg)
  13795. (add-to-list 'buffer-tags (list tg))
  13796. (if (member tg current)
  13797. (setq current (delete tg current))
  13798. (push tg current)))
  13799. (if exit-after-next (setq exit-after-next 'now)))
  13800. ((setq e (rassoc c todo-table) tg (car e))
  13801. (with-current-buffer buf
  13802. (save-excursion (org-todo tg)))
  13803. (if exit-after-next (setq exit-after-next 'now)))
  13804. ((setq e (rassoc c ntable) tg (car e))
  13805. (if (member tg current)
  13806. (setq current (delete tg current))
  13807. (loop for g in groups do
  13808. (if (member tg g)
  13809. (mapc (lambda (x)
  13810. (setq current (delete x current)))
  13811. g)))
  13812. (push tg current))
  13813. (if exit-after-next (setq exit-after-next 'now))))
  13814. ;; Create a sorted list
  13815. (setq current
  13816. (sort current
  13817. (lambda (a b)
  13818. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13819. (if (eq exit-after-next 'now) (throw 'exit t))
  13820. (goto-char (point-min))
  13821. (beginning-of-line 2)
  13822. (delete-region (point) (point-at-eol))
  13823. (org-fast-tag-insert "Current" current c-face)
  13824. (org-set-current-tags-overlay current ov-prefix)
  13825. (while (re-search-forward
  13826. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13827. (setq tg (match-string 1))
  13828. (add-text-properties
  13829. (match-beginning 1) (match-end 1)
  13830. (list 'face
  13831. (cond
  13832. ((member tg current) c-face)
  13833. ((member tg inherited) i-face)
  13834. (t (get-text-property (match-beginning 1) 'face))))))
  13835. (goto-char (point-min)))))
  13836. (org-detach-overlay org-tags-overlay)
  13837. (if rtn
  13838. (mapconcat 'identity current ":")
  13839. nil))))
  13840. (defun org-get-tags-string ()
  13841. "Get the TAGS string in the current headline."
  13842. (unless (org-at-heading-p t)
  13843. (user-error "Not on a heading"))
  13844. (save-excursion
  13845. (beginning-of-line 1)
  13846. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13847. (org-match-string-no-properties 1)
  13848. "")))
  13849. (defun org-get-tags ()
  13850. "Get the list of tags specified in the current headline."
  13851. (org-split-string (org-get-tags-string) ":"))
  13852. (defun org-get-buffer-tags ()
  13853. "Get a table of all tags used in the buffer, for completion."
  13854. (let (tags)
  13855. (save-excursion
  13856. (goto-char (point-min))
  13857. (while (re-search-forward
  13858. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13859. (when (equal (char-after (point-at-bol 0)) ?*)
  13860. (mapc (lambda (x) (add-to-list 'tags x))
  13861. (org-split-string (org-match-string-no-properties 1) ":")))))
  13862. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13863. (mapcar 'list tags)))
  13864. ;;;; The mapping API
  13865. (defun org-map-entries (func &optional match scope &rest skip)
  13866. "Call FUNC at each headline selected by MATCH in SCOPE.
  13867. FUNC is a function or a lisp form. The function will be called without
  13868. arguments, with the cursor positioned at the beginning of the headline.
  13869. The return values of all calls to the function will be collected and
  13870. returned as a list.
  13871. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13872. does not need to preserve point. After evaluation, the cursor will be
  13873. moved to the end of the line (presumably of the headline of the
  13874. processed entry) and search continues from there. Under some
  13875. circumstances, this may not produce the wanted results. For example,
  13876. if you have removed (e.g. archived) the current (sub)tree it could
  13877. mean that the next entry will be skipped entirely. In such cases, you
  13878. can specify the position from where search should continue by making
  13879. FUNC set the variable `org-map-continue-from' to the desired buffer
  13880. position.
  13881. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13882. Only headlines that are matched by this query will be considered during
  13883. the iteration. When MATCH is nil or t, all headlines will be
  13884. visited by the iteration.
  13885. SCOPE determines the scope of this command. It can be any of:
  13886. nil The current buffer, respecting the restriction if any
  13887. tree The subtree started with the entry at point
  13888. region The entries within the active region, if any
  13889. region-start-level
  13890. The entries within the active region, but only those at
  13891. the same level than the first one.
  13892. file The current buffer, without restriction
  13893. file-with-archives
  13894. The current buffer, and any archives associated with it
  13895. agenda All agenda files
  13896. agenda-with-archives
  13897. All agenda files with any archive files associated with them
  13898. \(file1 file2 ...)
  13899. If this is a list, all files in the list will be scanned
  13900. The remaining args are treated as settings for the skipping facilities of
  13901. the scanner. The following items can be given here:
  13902. archive skip trees with the archive tag
  13903. comment skip trees with the COMMENT keyword
  13904. function or Emacs Lisp form:
  13905. will be used as value for `org-agenda-skip-function', so
  13906. whenever the function returns a position, FUNC will not be
  13907. called for that entry and search will continue from the
  13908. position returned
  13909. If your function needs to retrieve the tags including inherited tags
  13910. at the *current* entry, you can use the value of the variable
  13911. `org-scanner-tags' which will be much faster than getting the value
  13912. with `org-get-tags-at'. If your function gets properties with
  13913. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13914. to t around the call to `org-entry-properties' to get the same speedup.
  13915. Note that if your function moves around to retrieve tags and properties at
  13916. a *different* entry, you cannot use these techniques."
  13917. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13918. (not (org-region-active-p)))
  13919. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13920. (org-agenda-skip-archived-trees (memq 'archive skip))
  13921. (org-agenda-skip-comment-trees (memq 'comment skip))
  13922. (org-agenda-skip-function
  13923. (car (org-delete-all '(comment archive) skip)))
  13924. (org-tags-match-list-sublevels t)
  13925. (start-level (eq scope 'region-start-level))
  13926. matcher file res
  13927. org-todo-keywords-for-agenda
  13928. org-done-keywords-for-agenda
  13929. org-todo-keyword-alist-for-agenda
  13930. org-tag-alist-for-agenda
  13931. todo-only)
  13932. (cond
  13933. ((eq match t) (setq matcher t))
  13934. ((eq match nil) (setq matcher t))
  13935. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13936. (save-excursion
  13937. (save-restriction
  13938. (cond ((eq scope 'tree)
  13939. (org-back-to-heading t)
  13940. (org-narrow-to-subtree)
  13941. (setq scope nil))
  13942. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13943. (org-region-active-p))
  13944. ;; If needed, set start-level to a string like "2"
  13945. (when start-level
  13946. (save-excursion
  13947. (goto-char (region-beginning))
  13948. (unless (org-at-heading-p) (outline-next-heading))
  13949. (setq start-level (org-current-level))))
  13950. (narrow-to-region (region-beginning)
  13951. (save-excursion
  13952. (goto-char (region-end))
  13953. (unless (and (bolp) (org-at-heading-p))
  13954. (outline-next-heading))
  13955. (point)))
  13956. (setq scope nil)))
  13957. (if (not scope)
  13958. (progn
  13959. (org-agenda-prepare-buffers
  13960. (list (buffer-file-name (current-buffer))))
  13961. (setq res (org-scan-tags func matcher todo-only start-level)))
  13962. ;; Get the right scope
  13963. (cond
  13964. ((and scope (listp scope) (symbolp (car scope)))
  13965. (setq scope (eval scope)))
  13966. ((eq scope 'agenda)
  13967. (setq scope (org-agenda-files t)))
  13968. ((eq scope 'agenda-with-archives)
  13969. (setq scope (org-agenda-files t))
  13970. (setq scope (org-add-archive-files scope)))
  13971. ((eq scope 'file)
  13972. (setq scope (list (buffer-file-name))))
  13973. ((eq scope 'file-with-archives)
  13974. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13975. (org-agenda-prepare-buffers scope)
  13976. (while (setq file (pop scope))
  13977. (with-current-buffer (org-find-base-buffer-visiting file)
  13978. (save-excursion
  13979. (save-restriction
  13980. (widen)
  13981. (goto-char (point-min))
  13982. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13983. res)))
  13984. ;;; Properties API
  13985. (defconst org-special-properties
  13986. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOSED" "PRIORITY"
  13987. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13988. "The special properties valid in Org-mode.
  13989. These are properties that are not defined in the property drawer,
  13990. but in some other way.")
  13991. (defconst org-default-properties
  13992. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13993. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13994. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13995. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13996. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  13997. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13998. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13999. "Some properties that are used by Org mode for various purposes.
  14000. Being in this list makes sure that they are offered for completion.")
  14001. (defun org--update-property-plist (key val props)
  14002. "Associate KEY to VAL in alist PROPS.
  14003. Modifications are made by side-effect. Return new alist."
  14004. (let* ((appending (string= (substring key -1) "+"))
  14005. (key (if appending (substring key 0 -1) key))
  14006. (old (assoc-string key props t)))
  14007. (if (not old) (cons (cons key val) props)
  14008. (setcdr old (if appending (concat (cdr old) " " val) val))
  14009. props)))
  14010. (defun org-get-property-block (&optional beg force)
  14011. "Return the (beg . end) range of the body of the property drawer.
  14012. BEG is the beginning of the current subtree, or of the part
  14013. before the first headline. If it is not given, it will be found.
  14014. If the drawer does not exist, create it if FORCE is non-nil, or
  14015. return nil."
  14016. (org-with-wide-buffer
  14017. (when beg (goto-char beg))
  14018. (unless (org-before-first-heading-p)
  14019. (let ((beg (cond (beg)
  14020. ((or (not (featurep 'org-inlinetask))
  14021. (org-inlinetask-in-task-p))
  14022. (org-back-to-heading t))
  14023. (t (org-with-limited-levels (org-back-to-heading t))))))
  14024. (forward-line)
  14025. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14026. (cond ((looking-at org-property-drawer-re)
  14027. (forward-line)
  14028. (cons (point) (progn (goto-char (match-end 0))
  14029. (line-beginning-position))))
  14030. (force
  14031. (org-insert-property-drawer)
  14032. (let ((pos (save-excursion (search-forward ":END:")
  14033. (line-beginning-position))))
  14034. (cons pos pos))))))))
  14035. (defun org-at-property-p ()
  14036. "Non-nil when point is inside a property drawer.
  14037. See `org-property-re' for match data, if applicable."
  14038. (save-excursion
  14039. (beginning-of-line)
  14040. (and (looking-at org-property-re)
  14041. (let ((property-drawer (save-match-data (org-get-property-block))))
  14042. (and property-drawer (< (point) (cdr property-drawer)))))))
  14043. (defun org-property-action ()
  14044. "Do an action on properties."
  14045. (interactive)
  14046. (unless (org-at-property-p) (user-error "Not at a property"))
  14047. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14048. (let ((c (read-char-exclusive)))
  14049. (case c
  14050. (?s (call-interactively #'org-set-property))
  14051. (?d (call-interactively #'org-delete-property))
  14052. (?D (call-interactively #'org-delete-property-globally))
  14053. (?c (call-interactively #'org-compute-property-at-point))
  14054. (otherwise (user-error "No such property action %c" c)))))
  14055. (defun org-inc-effort ()
  14056. "Increment the value of the effort property in the current entry."
  14057. (interactive)
  14058. (org-set-effort nil t))
  14059. (defvar org-clock-effort) ; Defined in org-clock.el.
  14060. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14061. (defun org-set-effort (&optional value increment)
  14062. "Set the effort property of the current entry.
  14063. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14064. 10th allowed value.
  14065. When INCREMENT is non-nil, set the property to the next allowed value."
  14066. (interactive "P")
  14067. (if (equal value 0) (setq value 10))
  14068. (let* ((completion-ignore-case t)
  14069. (prop org-effort-property)
  14070. (cur (org-entry-get nil prop))
  14071. (allowed (org-property-get-allowed-values nil prop 'table))
  14072. (existing (mapcar 'list (org-property-values prop)))
  14073. (heading (nth 4 (org-heading-components)))
  14074. rpl
  14075. (val (cond
  14076. ((stringp value) value)
  14077. ((and allowed (integerp value))
  14078. (or (car (nth (1- value) allowed))
  14079. (car (org-last allowed))))
  14080. ((and allowed increment)
  14081. (or (caadr (member (list cur) allowed))
  14082. (user-error "Allowed effort values are not set")))
  14083. (allowed
  14084. (message "Select 1-9,0, [RET%s]: %s"
  14085. (if cur (concat "=" cur) "")
  14086. (mapconcat 'car allowed " "))
  14087. (setq rpl (read-char-exclusive))
  14088. (if (equal rpl ?\r)
  14089. cur
  14090. (setq rpl (- rpl ?0))
  14091. (if (equal rpl 0) (setq rpl 10))
  14092. (if (and (> rpl 0) (<= rpl (length allowed)))
  14093. (car (nth (1- rpl) allowed))
  14094. (org-completing-read "Effort: " allowed nil))))
  14095. (t
  14096. (let (org-completion-use-ido org-completion-use-iswitchb)
  14097. (org-completing-read
  14098. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14099. (concat "[" cur "]") "")
  14100. ": ")
  14101. existing nil nil "" nil cur))))))
  14102. (unless (equal (org-entry-get nil prop) val)
  14103. (org-entry-put nil prop val))
  14104. (org-refresh-property
  14105. '((effort . identity)
  14106. (effort-minutes . org-duration-string-to-minutes))
  14107. val)
  14108. (when (string= heading org-clock-current-task)
  14109. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14110. (org-clock-update-mode-line))
  14111. (message "%s is now %s" prop val)))
  14112. (defun org-entry-properties (&optional pom which)
  14113. "Get all properties of the current entry.
  14114. When POM is a buffer position, get all properties from the entry
  14115. there instead.
  14116. This includes the TODO keyword, the tags, time strings for
  14117. deadline, scheduled, and clocking, and any additional properties
  14118. defined in the entry.
  14119. If WHICH is nil or `all', get all properties. If WHICH is
  14120. `special' or `standard', only get that subclass. If WHICH is
  14121. a string, only get that property.
  14122. Return value is an alist. Keys are properties, as upcased
  14123. strings."
  14124. (org-with-point-at pom
  14125. (when (and (derived-mode-p 'org-mode)
  14126. (ignore-errors (org-back-to-heading t)))
  14127. (catch 'exit
  14128. (let* ((beg (point))
  14129. (specific (and (stringp which) (upcase which)))
  14130. (which (cond ((not specific) which)
  14131. ((member specific org-special-properties) 'special)
  14132. (t 'standard)))
  14133. props)
  14134. ;; Get the special properties, like TODO and TAGS.
  14135. (when (memq which '(nil all special))
  14136. (when (or (not specific) (string= specific "CLOCKSUM"))
  14137. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14138. (when clocksum
  14139. (push (cons "CLOCKSUM"
  14140. (org-columns-number-to-string
  14141. (/ (float clocksum) 60.) 'add_times))
  14142. props)))
  14143. (when specific (throw 'exit props)))
  14144. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14145. (let ((clocksumt (get-text-property (point)
  14146. :org-clock-minutes-today)))
  14147. (when clocksumt
  14148. (push (cons "CLOCKSUM_T"
  14149. (org-columns-number-to-string
  14150. (/ (float clocksumt) 60.) 'add_times))
  14151. props)))
  14152. (when specific (throw 'exit props)))
  14153. (when (or (not specific) (string= specific "TODO"))
  14154. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14155. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14156. (when specific (throw 'exit props)))
  14157. (when (or (not specific) (string= specific "PRIORITY"))
  14158. (when (looking-at org-priority-regexp)
  14159. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14160. (when specific (throw 'exit props)))
  14161. (when (or (not specific) (string= specific "FILE"))
  14162. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14163. props)
  14164. (when specific (throw 'exit props)))
  14165. (when (or (not specific) (string= specific "TAGS"))
  14166. (let ((value (org-string-nw-p (org-get-tags-string))))
  14167. (when value (push (cons "TAGS" value) props)))
  14168. (when specific (throw 'exit props)))
  14169. (when (or (not specific) (string= specific "ALLTAGS"))
  14170. (let ((value (org-get-tags-at)))
  14171. (when value
  14172. (push (cons "ALLTAGS"
  14173. (format ":%s:" (mapconcat #'identity value ":")))
  14174. props)))
  14175. (when specific (throw 'exit props)))
  14176. (when (or (not specific) (string= specific "BLOCKED"))
  14177. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14178. (when specific (throw 'exit props)))
  14179. (when (or (not specific)
  14180. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14181. (forward-line)
  14182. (when (org-looking-at-p org-planning-line-re)
  14183. (end-of-line)
  14184. (let ((bol (line-beginning-position)))
  14185. ;; Backward compatibility: time keywords used to be
  14186. ;; configurable (before 8.3). Make sure we get the
  14187. ;; correct keyword.
  14188. (dolist (k (if (not specific)
  14189. (list org-closed-string
  14190. org-deadline-string
  14191. org-scheduled-string)
  14192. (list (cond ((string= specific "CLOSED")
  14193. org-closed-string)
  14194. ((string= specific "DEADLINE")
  14195. org-deadline-string)
  14196. (t org-scheduled-string)))))
  14197. (save-excursion
  14198. (when (search-backward k bol t)
  14199. (goto-char (match-end 0))
  14200. (skip-chars-forward " \t")
  14201. (and (looking-at org-ts-regexp-both)
  14202. (push (cons specific (match-string 0)) props)))))))
  14203. (when specific (throw 'exit props)))
  14204. (when (or (not specific)
  14205. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14206. (let ((find-ts
  14207. (lambda (end ts)
  14208. (let ((regexp (if (or (string= specific "TIMESTAMP")
  14209. (assoc "TIMESTAMP_IA" ts))
  14210. org-ts-regexp
  14211. org-ts-regexp-both)))
  14212. (catch 'next
  14213. (while (re-search-forward regexp end t)
  14214. (backward-char)
  14215. (let ((object (org-element-context)))
  14216. ;; Accept to match timestamps in node
  14217. ;; properties, too.
  14218. (when (memq (org-element-type object)
  14219. '(node-property timestamp))
  14220. (let ((type
  14221. (org-element-property :type object)))
  14222. (cond
  14223. ((and (memq type '(active active-range))
  14224. (not (equal specific "TIMESTAMP_IA")))
  14225. (unless (assoc "TIMESTAMP" ts)
  14226. (push (cons "TIMESTAMP"
  14227. (org-element-property
  14228. :raw-value object))
  14229. ts)
  14230. (when specific (throw 'exit ts))))
  14231. ((and (memq type '(inactive inactive-range))
  14232. (not (string= specific "TIMESTAMP")))
  14233. (unless (assoc "TIMESTAMP_IA" ts)
  14234. (push (cons "TIMESTAMP_IA"
  14235. (org-element-property
  14236. :raw-value object))
  14237. ts)
  14238. (when specific (throw 'exit ts))))))
  14239. ;; Both timestamp types are found,
  14240. ;; move to next part.
  14241. (when (= (length ts) 2) (throw 'next ts)))))
  14242. ts)))))
  14243. (goto-char beg)
  14244. ;; First look for timestamps within headline.
  14245. (let ((ts (funcall find-ts (line-end-position) nil)))
  14246. (if (= (length ts) 2) (setq props (nconc ts props))
  14247. (forward-line)
  14248. ;; Then find timestamps in the section, skipping
  14249. ;; planning line.
  14250. (when (org-looking-at-p org-planning-line-re)
  14251. (forward-line))
  14252. (let ((end (save-excursion (outline-next-heading))))
  14253. (setq props (nconc (funcall find-ts end ts) props))))))))
  14254. ;; Get the standard properties, like :PROP:.
  14255. (when (memq which '(nil all standard))
  14256. ;; If we are looking after a specific property, delegate
  14257. ;; to `org-entry-get', which is faster. However, make an
  14258. ;; exception for "CATEGORY", since it can be also set
  14259. ;; through keywords (i.e. #+CATEGORY).
  14260. (if (and specific (not (equal specific "CATEGORY")))
  14261. (let ((value (org-entry-get beg specific nil t)))
  14262. (throw 'exit (and value (list (cons specific value)))))
  14263. (let ((range (org-get-property-block beg)))
  14264. (when range
  14265. (let ((end (cdr range)) seen-base)
  14266. (goto-char (car range))
  14267. ;; Unlike to `org--update-property-plist', we
  14268. ;; handle the case where base values is found
  14269. ;; after its extension. We also forbid standard
  14270. ;; properties to be named as special properties.
  14271. (while (re-search-forward org-property-re end t)
  14272. (let* ((key (upcase (org-match-string-no-properties 2)))
  14273. (extendp (org-string-match-p "\\+\\'" key))
  14274. (key-base (if extendp (substring key 0 -1) key))
  14275. (value (org-match-string-no-properties 3)))
  14276. (cond
  14277. ((member-ignore-case key-base org-special-properties))
  14278. (extendp
  14279. (setq props
  14280. (org--update-property-plist key value props)))
  14281. ((member key seen-base))
  14282. (t (push key seen-base)
  14283. (let ((p (assoc-string key props t)))
  14284. (if p (setcdr p (concat value " " (cdr p)))
  14285. (push (cons key value) props))))))))))))
  14286. (unless (assoc "CATEGORY" props)
  14287. (push (cons "CATEGORY" (org-get-category)) props)
  14288. (when (string= specific "CATEGORY") (throw 'exit props)))
  14289. ;; Return value.
  14290. (append (get-text-property beg 'org-summaries) props))))))
  14291. (defun org-entry-get (pom property &optional inherit literal-nil)
  14292. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14293. If INHERIT is non-nil and the entry does not have the property,
  14294. then also check higher levels of the hierarchy. If INHERIT is
  14295. the symbol `selective', use inheritance only if the setting in
  14296. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14297. If the property is present but empty, the return value is the
  14298. empty string. If the property is not present at all, nil is
  14299. returned. In any other case, return the value as a string.
  14300. Search is case-insensitive.
  14301. If LITERAL-NIL is set, return the string value \"nil\" as
  14302. a string, do not interpret it as the list atom nil. This is used
  14303. for inheritance when a \"nil\" value can supersede a non-nil
  14304. value higher up the hierarchy."
  14305. (org-with-point-at pom
  14306. (cond
  14307. ((and inherit
  14308. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14309. (org-entry-get-with-inheritance property literal-nil))
  14310. ((member-ignore-case property org-special-properties)
  14311. ;; We need a special property. Use `org-entry-properties' to
  14312. ;; retrieve it, but specify the wanted property.
  14313. (cdr (assoc-string property (org-entry-properties nil property))))
  14314. (t
  14315. (let ((range (org-get-property-block)))
  14316. (when range
  14317. (let* ((case-fold-search t)
  14318. (end (cdr range))
  14319. (props
  14320. (let ((global
  14321. (or (assoc-string property org-file-properties t)
  14322. (assoc-string property org-global-properties t)
  14323. (assoc-string
  14324. property org-global-properties-fixed t))))
  14325. ;; Make sure to not re-use GLOBAL as
  14326. ;; `org--update-property-plist' would alter it by
  14327. ;; side-effect.
  14328. (and global (list (cons property (cdr global))))))
  14329. (find-value
  14330. (lambda (key)
  14331. (when (re-search-forward (org-re-property key nil t) end t)
  14332. (setq props
  14333. (org--update-property-plist
  14334. key (org-match-string-no-properties 3) props))))))
  14335. (goto-char (car range))
  14336. ;; Find base value.
  14337. (save-excursion (funcall find-value property))
  14338. ;; Find additional values.
  14339. (let ((property+ (concat property "+")))
  14340. (while (funcall find-value property+)))
  14341. ;; Return final value.
  14342. (let ((val (cdr (assoc-string property props t))))
  14343. (if literal-nil val (org-not-nil val))))))))))
  14344. (defun org-property-or-variable-value (var &optional inherit)
  14345. "Check if there is a property fixing the value of VAR.
  14346. If yes, return this value. If not, return the current value of the variable."
  14347. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14348. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14349. (read prop)
  14350. (symbol-value var))))
  14351. (defun org-entry-delete (pom property)
  14352. "Delete the property PROPERTY from entry at point-or-marker POM."
  14353. (unless (member property org-special-properties)
  14354. (org-with-point-at pom
  14355. (let ((range (org-get-property-block)))
  14356. (when range
  14357. (let ((begin (car range))
  14358. (end (copy-marker (cdr range))))
  14359. (goto-char begin)
  14360. (when (re-search-forward (org-re-property property nil t) end t)
  14361. (delete-region (match-beginning 0) (line-beginning-position 2))
  14362. ;; If drawer is empty, remove it altogether.
  14363. (when (= begin end)
  14364. (delete-region (line-beginning-position 0)
  14365. (line-beginning-position 2)))
  14366. (set-marker end nil))))))))
  14367. ;; Multi-values properties are properties that contain multiple values
  14368. ;; These values are assumed to be single words, separated by whitespace.
  14369. (defun org-entry-add-to-multivalued-property (pom property value)
  14370. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14371. (let* ((old (org-entry-get pom property))
  14372. (values (and old (org-split-string old "[ \t]"))))
  14373. (setq value (org-entry-protect-space value))
  14374. (unless (member value values)
  14375. (setq values (append values (list value)))
  14376. (org-entry-put pom property
  14377. (mapconcat 'identity values " ")))))
  14378. (defun org-entry-remove-from-multivalued-property (pom property value)
  14379. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14380. (let* ((old (org-entry-get pom property))
  14381. (values (and old (org-split-string old "[ \t]"))))
  14382. (setq value (org-entry-protect-space value))
  14383. (when (member value values)
  14384. (setq values (delete value values))
  14385. (org-entry-put pom property
  14386. (mapconcat 'identity values " ")))))
  14387. (defun org-entry-member-in-multivalued-property (pom property value)
  14388. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14389. (let* ((old (org-entry-get pom property))
  14390. (values (and old (org-split-string old "[ \t]"))))
  14391. (setq value (org-entry-protect-space value))
  14392. (member value values)))
  14393. (defun org-entry-get-multivalued-property (pom property)
  14394. "Return a list of values in a multivalued property."
  14395. (let* ((value (org-entry-get pom property))
  14396. (values (and value (org-split-string value "[ \t]"))))
  14397. (mapcar 'org-entry-restore-space values)))
  14398. (defun org-entry-put-multivalued-property (pom property &rest values)
  14399. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14400. VALUES should be a list of strings. Spaces will be protected."
  14401. (org-entry-put pom property
  14402. (mapconcat 'org-entry-protect-space values " "))
  14403. (let* ((value (org-entry-get pom property))
  14404. (values (and value (org-split-string value "[ \t]"))))
  14405. (mapcar 'org-entry-restore-space values)))
  14406. (defun org-entry-protect-space (s)
  14407. "Protect spaces and newline in string S."
  14408. (while (string-match " " s)
  14409. (setq s (replace-match "%20" t t s)))
  14410. (while (string-match "\n" s)
  14411. (setq s (replace-match "%0A" t t s)))
  14412. s)
  14413. (defun org-entry-restore-space (s)
  14414. "Restore spaces and newline in string S."
  14415. (while (string-match "%20" s)
  14416. (setq s (replace-match " " t t s)))
  14417. (while (string-match "%0A" s)
  14418. (setq s (replace-match "\n" t t s)))
  14419. s)
  14420. (defvar org-entry-property-inherited-from (make-marker)
  14421. "Marker pointing to the entry from where a property was inherited.
  14422. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14423. location of the entry where the inheritance search matched. If there was
  14424. no match, the marker will point nowhere.
  14425. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14426. is set.")
  14427. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14428. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14429. The search will stop at the first ancestor which has the property defined.
  14430. If the value found is \"nil\", return nil to show that the property
  14431. should be considered as undefined (this is the meaning of nil here).
  14432. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14433. (move-marker org-entry-property-inherited-from nil)
  14434. (let (value)
  14435. (org-with-wide-buffer
  14436. (catch 'exit
  14437. (while t
  14438. (when (setq value (org-entry-get nil property nil literal-nil))
  14439. (org-back-to-heading t)
  14440. (move-marker org-entry-property-inherited-from (point))
  14441. (throw 'exit nil))
  14442. (or (org-up-heading-safe) (throw 'exit nil)))))
  14443. (unless value
  14444. (setq value
  14445. (cdr (or (assoc-string property org-file-properties t)
  14446. (assoc-string property org-global-properties t)
  14447. (assoc-string property org-global-properties-fixed t)))))
  14448. (if literal-nil value (org-not-nil value))))
  14449. (defvar org-property-changed-functions nil
  14450. "Hook called when the value of a property has changed.
  14451. Each hook function should accept two arguments, the name of the property
  14452. and the new value.")
  14453. (defun org-entry-put (pom property value)
  14454. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14455. If the value is `nil', it is converted to the empty string. If
  14456. it is not a string, an error is raised.
  14457. PROPERTY can be any regular property (see
  14458. `org-special-properties'). It can also be \"TODO\",
  14459. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14460. For the last two properties, VALUE may have any of the special
  14461. values \"earlier\" and \"later\". The function then increases or
  14462. decreases scheduled or deadline date by one day."
  14463. (cond ((null value) (setq value ""))
  14464. ((not (stringp value)) (error "Properties values should be strings")))
  14465. (org-with-point-at pom
  14466. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14467. (org-back-to-heading t)
  14468. (org-with-limited-levels (org-back-to-heading t)))
  14469. (let ((beg (point)))
  14470. (cond
  14471. ((equal property "TODO")
  14472. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14473. ((not (member value org-todo-keywords-1))
  14474. (user-error "\"%s\" is not a valid TODO state" value)))
  14475. (org-todo value)
  14476. (org-set-tags nil 'align))
  14477. ((equal property "PRIORITY")
  14478. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14479. (org-set-tags nil 'align))
  14480. ((equal property "SCHEDULED")
  14481. (forward-line)
  14482. (if (and (org-looking-at-p org-planning-line-re)
  14483. (re-search-forward
  14484. org-scheduled-time-regexp (line-end-position) t))
  14485. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14486. ((string= value "later") (org-timestamp-change 1 'day))
  14487. ((string= value "") (org-schedule '(4)))
  14488. (t (org-schedule nil value)))
  14489. (if (member value '("earlier" "later" ""))
  14490. (call-interactively #'org-schedule)
  14491. (org-schedule nil value))))
  14492. ((equal property "DEADLINE")
  14493. (forward-line)
  14494. (if (and (org-looking-at-p org-planning-line-re)
  14495. (re-search-forward
  14496. org-deadline-time-regexp (line-end-position) t))
  14497. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14498. ((string= value "later") (org-timestamp-change 1 'day))
  14499. ((string= value "") (org-deadline '(4)))
  14500. (t (org-deadline nil value)))
  14501. (if (member value '("earlier" "later" ""))
  14502. (call-interactively #'org-deadline)
  14503. (org-deadline nil value))))
  14504. ((member property org-special-properties)
  14505. (error "The %s property cannot be set with `org-entry-put'" property))
  14506. (t
  14507. (let* ((range (org-get-property-block beg 'force))
  14508. (end (cdr range))
  14509. (case-fold-search t))
  14510. (goto-char (car range))
  14511. (if (re-search-forward (org-re-property property nil t) end t)
  14512. (progn (delete-region (match-beginning 0) (match-end 0))
  14513. (goto-char (match-beginning 0)))
  14514. (goto-char end)
  14515. (insert "\n")
  14516. (backward-char))
  14517. (insert ":" property ":")
  14518. (when value (insert " " value))
  14519. (org-indent-line)))))
  14520. (run-hook-with-args 'org-property-changed-functions property value)))
  14521. (defun org-buffer-property-keys (&optional specials defaults columns)
  14522. "Get all property keys in the current buffer.
  14523. When SPECIALS is non-nil, also list the special properties that
  14524. reflect things like tags and TODO state.
  14525. When DEFAULTS is non-nil, also include properties that has
  14526. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14527. DESCRIPTION, LOCATION, and LOGGING and others.
  14528. When COLUMNS in non-nil, also include property names given in
  14529. COLUMN formats in the current buffer."
  14530. (let ((case-fold-search t)
  14531. (props (append
  14532. (and specials org-special-properties)
  14533. (and defaults (cons org-effort-property org-default-properties))
  14534. nil)))
  14535. (org-with-wide-buffer
  14536. (goto-char (point-min))
  14537. (while (re-search-forward org-property-start-re nil t)
  14538. (let ((range (org-get-property-block)))
  14539. (catch 'skip
  14540. (unless range
  14541. (when (and (not (org-before-first-heading-p))
  14542. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14543. (line-beginning-position))))
  14544. (org-get-property-block nil t))
  14545. (throw 'skip nil))
  14546. (goto-char (car range))
  14547. (let ((begin (car range))
  14548. (end (cdr range)))
  14549. ;; Make sure that found property block is not located
  14550. ;; before current point, as it would generate an infloop.
  14551. ;; It can happen, for example, in the following
  14552. ;; situation:
  14553. ;;
  14554. ;; * Headline
  14555. ;; :PROPERTIES:
  14556. ;; ...
  14557. ;; :END:
  14558. ;; *************** Inlinetask
  14559. ;; #+BEGIN_EXAMPLE
  14560. ;; :PROPERTIES:
  14561. ;; #+END_EXAMPLE
  14562. ;;
  14563. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14564. (while (< (point) end)
  14565. (let ((p (progn (looking-at org-property-re)
  14566. (org-match-string-no-properties 2))))
  14567. ;; Only add true property name, not extension symbol.
  14568. (add-to-list 'props
  14569. (if (not (org-string-match-p "\\+\\'" p)) p
  14570. (substring p 0 -1))))
  14571. (forward-line))))
  14572. (outline-next-heading)))
  14573. (when columns
  14574. (goto-char (point-min))
  14575. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14576. (let ((element (org-element-at-point)))
  14577. (when (memq (org-element-type element) '(keyword node-property))
  14578. (let ((value (org-element-property :value element))
  14579. (start 0))
  14580. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14581. (setq start (match-end 0))
  14582. (let ((p (org-match-string-no-properties 1 value)))
  14583. (unless (member-ignore-case
  14584. p (cons "ITEM" org-special-properties))
  14585. (add-to-list 'props p))))))))))
  14586. (sort props (lambda (a b) (string< (upcase a) (upcase b))))))
  14587. (defun org-property-values (key)
  14588. "List all non-nil values of property KEY in current buffer."
  14589. (org-with-wide-buffer
  14590. (goto-char (point-min))
  14591. (let ((case-fold-search t)
  14592. (re (org-re-property key))
  14593. values)
  14594. (while (re-search-forward re nil t)
  14595. (add-to-list 'values (org-entry-get (point) key)))
  14596. values)))
  14597. (defun org-insert-property-drawer ()
  14598. "Insert a property drawer into the current entry."
  14599. (org-with-wide-buffer
  14600. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14601. (org-back-to-heading t)
  14602. (org-with-limited-levels (org-back-to-heading t)))
  14603. (forward-line)
  14604. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14605. (unless (org-looking-at-p org-property-drawer-re)
  14606. (let ((inhibit-read-only t))
  14607. (unless (bolp) (insert "\n"))
  14608. (let ((begin (point)))
  14609. (insert ":PROPERTIES:\n:END:\n")
  14610. (org-indent-region begin (point)))))))
  14611. (defun org-insert-drawer (&optional arg drawer)
  14612. "Insert a drawer at point.
  14613. When optional argument ARG is non-nil, insert a property drawer.
  14614. Optional argument DRAWER, when non-nil, is a string representing
  14615. drawer's name. Otherwise, the user is prompted for a name.
  14616. If a region is active, insert the drawer around that region
  14617. instead.
  14618. Point is left between drawer's boundaries."
  14619. (interactive "P")
  14620. (let* ((drawer (if arg "PROPERTIES"
  14621. (or drawer (read-from-minibuffer "Drawer: ")))))
  14622. (cond
  14623. ;; With C-u, fall back on `org-insert-property-drawer'
  14624. (arg (org-insert-property-drawer))
  14625. ;; Check validity of suggested drawer's name.
  14626. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14627. (user-error "Invalid drawer name"))
  14628. ;; With an active region, insert a drawer at point.
  14629. ((not (org-region-active-p))
  14630. (progn
  14631. (unless (bolp) (insert "\n"))
  14632. (insert (format ":%s:\n\n:END:\n" drawer))
  14633. (forward-line -2)))
  14634. ;; Otherwise, insert the drawer at point
  14635. (t
  14636. (let ((rbeg (region-beginning))
  14637. (rend (copy-marker (region-end))))
  14638. (unwind-protect
  14639. (progn
  14640. (goto-char rbeg)
  14641. (beginning-of-line)
  14642. (when (save-excursion
  14643. (re-search-forward org-outline-regexp-bol rend t))
  14644. (user-error "Drawers cannot contain headlines"))
  14645. ;; Position point at the beginning of the first
  14646. ;; non-blank line in region. Insert drawer's opening
  14647. ;; there, then indent it.
  14648. (org-skip-whitespace)
  14649. (beginning-of-line)
  14650. (insert ":" drawer ":\n")
  14651. (forward-line -1)
  14652. (indent-for-tab-command)
  14653. ;; Move point to the beginning of the first blank line
  14654. ;; after the last non-blank line in region. Insert
  14655. ;; drawer's closing, then indent it.
  14656. (goto-char rend)
  14657. (skip-chars-backward " \r\t\n")
  14658. (insert "\n:END:")
  14659. (deactivate-mark t)
  14660. (indent-for-tab-command)
  14661. (unless (eolp) (insert "\n")))
  14662. ;; Clear marker, whatever the outcome of insertion is.
  14663. (set-marker rend nil)))))))
  14664. (defvar org-property-set-functions-alist nil
  14665. "Property set function alist.
  14666. Each entry should have the following format:
  14667. (PROPERTY . READ-FUNCTION)
  14668. The read function will be called with the same argument as
  14669. `org-completing-read'.")
  14670. (defun org-set-property-function (property)
  14671. "Get the function that should be used to set PROPERTY.
  14672. This is computed according to `org-property-set-functions-alist'."
  14673. (or (cdr (assoc property org-property-set-functions-alist))
  14674. 'org-completing-read))
  14675. (defun org-read-property-value (property)
  14676. "Read PROPERTY value from user."
  14677. (let* ((completion-ignore-case t)
  14678. (allowed (org-property-get-allowed-values nil property 'table))
  14679. (cur (org-entry-get nil property))
  14680. (prompt (concat property " value"
  14681. (if (and cur (string-match "\\S-" cur))
  14682. (concat " [" cur "]") "") ": "))
  14683. (set-function (org-set-property-function property))
  14684. (val (if allowed
  14685. (funcall set-function prompt allowed nil
  14686. (not (get-text-property 0 'org-unrestricted
  14687. (caar allowed))))
  14688. (let (org-completion-use-ido org-completion-use-iswitchb)
  14689. (funcall set-function prompt
  14690. (mapcar 'list (org-property-values property))
  14691. nil nil "" nil cur)))))
  14692. (org-trim val)))
  14693. (defvar org-last-set-property nil)
  14694. (defvar org-last-set-property-value nil)
  14695. (defun org-read-property-name ()
  14696. "Read a property name."
  14697. (let* ((completion-ignore-case t)
  14698. (keys (org-buffer-property-keys nil t t))
  14699. (default-prop (or (save-excursion
  14700. (save-match-data
  14701. (beginning-of-line)
  14702. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14703. (null (string= (match-string 1) "END"))
  14704. (match-string 1))))
  14705. org-last-set-property))
  14706. (property (org-icompleting-read
  14707. (concat "Property"
  14708. (if default-prop (concat " [" default-prop "]") "")
  14709. ": ")
  14710. (mapcar 'list keys)
  14711. nil nil nil nil
  14712. default-prop)))
  14713. (if (member property keys)
  14714. property
  14715. (or (cdr (assoc (downcase property)
  14716. (mapcar (lambda (x) (cons (downcase x) x))
  14717. keys)))
  14718. property))))
  14719. (defun org-set-property-and-value (use-last)
  14720. "Allow to set [PROPERTY]: [value] direction from prompt.
  14721. When use-default, don't even ask, just use the last
  14722. \"[PROPERTY]: [value]\" string from the history."
  14723. (interactive "P")
  14724. (let* ((completion-ignore-case t)
  14725. (pv (or (and use-last org-last-set-property-value)
  14726. (org-completing-read
  14727. "Enter a \"[Property]: [value]\" pair: "
  14728. nil nil nil nil nil
  14729. org-last-set-property-value)))
  14730. prop val)
  14731. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14732. (setq prop (match-string 1 pv)
  14733. val (match-string 2 pv))
  14734. (org-set-property prop val))))
  14735. (defun org-set-property (property value)
  14736. "In the current entry, set PROPERTY to VALUE.
  14737. When called interactively, this will prompt for a property name, offering
  14738. completion on existing and default properties. And then it will prompt
  14739. for a value, offering completion either on allowed values (via an inherited
  14740. xxx_ALL property) or on existing values in other instances of this property
  14741. in the current file."
  14742. (interactive (list nil nil))
  14743. (let* ((property (or property (org-read-property-name)))
  14744. (value (or value (org-read-property-value property)))
  14745. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14746. (setq org-last-set-property property)
  14747. (setq org-last-set-property-value (concat property ": " value))
  14748. ;; Possibly postprocess the inserted value:
  14749. (when fn (setq value (funcall fn value)))
  14750. (unless (equal (org-entry-get nil property) value)
  14751. (org-entry-put nil property value))))
  14752. (defun org-delete-property (property)
  14753. "In the current entry, delete PROPERTY."
  14754. (interactive
  14755. (let* ((completion-ignore-case t)
  14756. (cat (org-entry-get (point) "CATEGORY"))
  14757. (props0 (org-entry-properties nil 'standard))
  14758. (props (if cat props0
  14759. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14760. (prop (if (< 1 (length props))
  14761. (org-icompleting-read "Property: " props nil t)
  14762. (caar props))))
  14763. (list prop)))
  14764. (if (not property)
  14765. (message "No property to delete in this entry")
  14766. (org-entry-delete nil property)
  14767. (message "Property \"%s\" deleted" property)))
  14768. (defun org-delete-property-globally (property)
  14769. "Remove PROPERTY globally, from all entries."
  14770. (interactive
  14771. (let* ((completion-ignore-case t)
  14772. (prop (org-icompleting-read
  14773. "Globally remove property: "
  14774. (mapcar 'list (org-buffer-property-keys)))))
  14775. (list prop)))
  14776. (save-excursion
  14777. (save-restriction
  14778. (widen)
  14779. (goto-char (point-min))
  14780. (let ((cnt 0))
  14781. (while (re-search-forward
  14782. (org-re-property property)
  14783. nil t)
  14784. (setq cnt (1+ cnt))
  14785. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14786. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14787. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14788. (defun org-compute-property-at-point ()
  14789. "Compute the property at point.
  14790. This looks for an enclosing column format, extracts the operator and
  14791. then applies it to the property in the column format's scope."
  14792. (interactive)
  14793. (unless (org-at-property-p)
  14794. (user-error "Not at a property"))
  14795. (let ((prop (org-match-string-no-properties 2)))
  14796. (org-columns-get-format-and-top-level)
  14797. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14798. (user-error "No operator defined for property %s" prop))
  14799. (org-columns-compute prop)))
  14800. (defvar org-property-allowed-value-functions nil
  14801. "Hook for functions supplying allowed values for a specific property.
  14802. The functions must take a single argument, the name of the property, and
  14803. return a flat list of allowed values. If \":ETC\" is one of
  14804. the values, this means that these values are intended as defaults for
  14805. completion, but that other values should be allowed too.
  14806. The functions must return nil if they are not responsible for this
  14807. property.")
  14808. (defun org-property-get-allowed-values (pom property &optional table)
  14809. "Get allowed values for the property PROPERTY.
  14810. When TABLE is non-nil, return an alist that can directly be used for
  14811. completion."
  14812. (let (vals)
  14813. (cond
  14814. ((equal property "TODO")
  14815. (setq vals (org-with-point-at pom
  14816. (append org-todo-keywords-1 '("")))))
  14817. ((equal property "PRIORITY")
  14818. (let ((n org-lowest-priority))
  14819. (while (>= n org-highest-priority)
  14820. (push (char-to-string n) vals)
  14821. (setq n (1- n)))))
  14822. ((member property org-special-properties))
  14823. ((setq vals (run-hook-with-args-until-success
  14824. 'org-property-allowed-value-functions property)))
  14825. (t
  14826. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14827. (when (and vals (string-match "\\S-" vals))
  14828. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14829. (setq vals (mapcar (lambda (x)
  14830. (cond ((stringp x) x)
  14831. ((numberp x) (number-to-string x))
  14832. ((symbolp x) (symbol-name x))
  14833. (t "???")))
  14834. vals)))))
  14835. (when (member ":ETC" vals)
  14836. (setq vals (remove ":ETC" vals))
  14837. (org-add-props (car vals) '(org-unrestricted t)))
  14838. (if table (mapcar 'list vals) vals)))
  14839. (defun org-property-previous-allowed-value (&optional previous)
  14840. "Switch to the next allowed value for this property."
  14841. (interactive)
  14842. (org-property-next-allowed-value t))
  14843. (defun org-property-next-allowed-value (&optional previous)
  14844. "Switch to the next allowed value for this property."
  14845. (interactive)
  14846. (unless (org-at-property-p)
  14847. (user-error "Not at a property"))
  14848. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14849. (key (match-string 2))
  14850. (value (match-string 3))
  14851. (allowed (or (org-property-get-allowed-values (point) key)
  14852. (and (member value '("[ ]" "[-]" "[X]"))
  14853. '("[ ]" "[X]"))))
  14854. (heading (save-match-data (nth 4 (org-heading-components))))
  14855. nval)
  14856. (unless allowed
  14857. (user-error "Allowed values for this property have not been defined"))
  14858. (if previous (setq allowed (reverse allowed)))
  14859. (if (member value allowed)
  14860. (setq nval (car (cdr (member value allowed)))))
  14861. (setq nval (or nval (car allowed)))
  14862. (if (equal nval value)
  14863. (user-error "Only one allowed value for this property"))
  14864. (org-at-property-p)
  14865. (replace-match (concat " :" key ": " nval) t t)
  14866. (org-indent-line)
  14867. (beginning-of-line 1)
  14868. (skip-chars-forward " \t")
  14869. (when (equal prop org-effort-property)
  14870. (org-refresh-property
  14871. '((effort . identity)
  14872. (effort-minutes . org-duration-string-to-minutes))
  14873. nval)
  14874. (when (string= org-clock-current-task heading)
  14875. (setq org-clock-effort nval)
  14876. (org-clock-update-mode-line)))
  14877. (run-hook-with-args 'org-property-changed-functions key nval)))
  14878. (defun org-find-olp (path &optional this-buffer)
  14879. "Return a marker pointing to the entry at outline path OLP.
  14880. If anything goes wrong, throw an error.
  14881. You can wrap this call to catch the error like this:
  14882. (condition-case msg
  14883. (org-mobile-locate-entry (match-string 4))
  14884. (error (nth 1 msg)))
  14885. The return value will then be either a string with the error message,
  14886. or a marker if everything is OK.
  14887. If THIS-BUFFER is set, the outline path does not contain a file,
  14888. only headings."
  14889. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14890. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14891. (level 1)
  14892. (lmin 1)
  14893. (lmax 1)
  14894. limit re end found pos heading cnt flevel)
  14895. (unless buffer (error "File not found :%s" file))
  14896. (with-current-buffer buffer
  14897. (save-excursion
  14898. (save-restriction
  14899. (widen)
  14900. (setq limit (point-max))
  14901. (goto-char (point-min))
  14902. (while (setq heading (pop path))
  14903. (setq re (format org-complex-heading-regexp-format
  14904. (regexp-quote heading)))
  14905. (setq cnt 0 pos (point))
  14906. (while (re-search-forward re end t)
  14907. (setq level (- (match-end 1) (match-beginning 1)))
  14908. (if (and (>= level lmin) (<= level lmax))
  14909. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14910. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14911. lmax heading))
  14912. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14913. lmax heading))
  14914. (goto-char found)
  14915. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14916. (setq end (save-excursion (org-end-of-subtree t t))))
  14917. (when (org-at-heading-p)
  14918. (point-marker)))))))
  14919. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14920. "Find node HEADING in BUFFER.
  14921. Return a marker to the heading if it was found, or nil if not.
  14922. If POS-ONLY is set, return just the position instead of a marker.
  14923. The heading text must match exact, but it may have a TODO keyword,
  14924. a priority cookie and tags in the standard locations."
  14925. (with-current-buffer (or buffer (current-buffer))
  14926. (save-excursion
  14927. (save-restriction
  14928. (widen)
  14929. (goto-char (point-min))
  14930. (let (case-fold-search)
  14931. (if (re-search-forward
  14932. (format org-complex-heading-regexp-format
  14933. (regexp-quote heading)) nil t)
  14934. (if pos-only
  14935. (match-beginning 0)
  14936. (move-marker (make-marker) (match-beginning 0)))))))))
  14937. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14938. "Find Org node headline HEADING in all .org files in directory DIR.
  14939. When the target headline is found, return a marker to this location."
  14940. (let ((files (directory-files (or dir default-directory)
  14941. t "\\`[^.#].*\\.org\\'"))
  14942. file visiting m buffer)
  14943. (catch 'found
  14944. (while (setq file (pop files))
  14945. (message "trying %s" file)
  14946. (setq visiting (org-find-base-buffer-visiting file))
  14947. (setq buffer (or visiting (find-file-noselect file)))
  14948. (setq m (org-find-exact-headline-in-buffer
  14949. heading buffer))
  14950. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14951. (and m (throw 'found m))))))
  14952. (defun org-find-entry-with-id (ident)
  14953. "Locate the entry that contains the ID property with exact value IDENT.
  14954. IDENT can be a string, a symbol or a number, this function will search for
  14955. the string representation of it.
  14956. Return the position where this entry starts, or nil if there is no such entry."
  14957. (interactive "sID: ")
  14958. (let ((id (cond
  14959. ((stringp ident) ident)
  14960. ((symbol-name ident) (symbol-name ident))
  14961. ((numberp ident) (number-to-string ident))
  14962. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14963. (case-fold-search nil))
  14964. (save-excursion
  14965. (save-restriction
  14966. (widen)
  14967. (goto-char (point-min))
  14968. (when (re-search-forward
  14969. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14970. nil t)
  14971. (org-back-to-heading t)
  14972. (point))))))
  14973. ;;;; Timestamps
  14974. (defvar org-last-changed-timestamp nil)
  14975. (defvar org-last-inserted-timestamp nil
  14976. "The last time stamp inserted with `org-insert-time-stamp'.")
  14977. (defvar org-ts-what) ; dynamically scoped parameter
  14978. (defun org-time-stamp (arg &optional inactive)
  14979. "Prompt for a date/time and insert a time stamp.
  14980. If the user specifies a time like HH:MM or if this command is
  14981. called with at least one prefix argument, the time stamp contains
  14982. the date and the time. Otherwise, only the date is be included.
  14983. All parts of a date not specified by the user is filled in from
  14984. the current date/time. So if you just press return without
  14985. typing anything, the time stamp will represent the current
  14986. date/time.
  14987. If there is already a timestamp at the cursor, it will be
  14988. modified.
  14989. With two universal prefix arguments, insert an active timestamp
  14990. with the current time without prompting the user.
  14991. When called from lisp, the timestamp is inactive if INACTIVE is
  14992. non-nil."
  14993. (interactive "P")
  14994. (let* ((ts nil)
  14995. (default-time
  14996. ;; Default time is either today, or, when entering a range,
  14997. ;; the range start.
  14998. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14999. (save-excursion
  15000. (re-search-backward
  15001. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  15002. (- (point) 20) t)))
  15003. (apply 'encode-time (org-parse-time-string (match-string 1)))
  15004. (current-time)))
  15005. (default-input (and ts (org-get-compact-tod ts)))
  15006. (repeater (save-excursion
  15007. (save-match-data
  15008. (beginning-of-line)
  15009. (when (re-search-forward
  15010. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  15011. (save-excursion (progn (end-of-line) (point))) t)
  15012. (match-string 0)))))
  15013. org-time-was-given org-end-time-was-given time)
  15014. (cond
  15015. ((and (org-at-timestamp-p t)
  15016. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15017. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15018. (insert "--")
  15019. (setq time (let ((this-command this-command))
  15020. (org-read-date arg 'totime nil nil
  15021. default-time default-input inactive)))
  15022. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15023. ((org-at-timestamp-p t)
  15024. (setq time (let ((this-command this-command))
  15025. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  15026. (when (org-at-timestamp-p t) ; just to get the match data
  15027. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  15028. (replace-match "")
  15029. (setq org-last-changed-timestamp
  15030. (org-insert-time-stamp
  15031. time (or org-time-was-given arg)
  15032. inactive nil nil (list org-end-time-was-given)))
  15033. (when repeater (goto-char (1- (point))) (insert " " repeater)
  15034. (setq org-last-changed-timestamp
  15035. (concat (substring org-last-inserted-timestamp 0 -1)
  15036. " " repeater ">"))))
  15037. (message "Timestamp updated"))
  15038. ((equal arg '(16))
  15039. (org-insert-time-stamp (current-time) t inactive))
  15040. (t
  15041. (setq time (let ((this-command this-command))
  15042. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  15043. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  15044. nil nil (list org-end-time-was-given))))))
  15045. ;; FIXME: can we use this for something else, like computing time differences?
  15046. (defun org-get-compact-tod (s)
  15047. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15048. (let* ((t1 (match-string 1 s))
  15049. (h1 (string-to-number (match-string 2 s)))
  15050. (m1 (string-to-number (match-string 3 s)))
  15051. (t2 (and (match-end 4) (match-string 5 s)))
  15052. (h2 (and t2 (string-to-number (match-string 6 s))))
  15053. (m2 (and t2 (string-to-number (match-string 7 s))))
  15054. dh dm)
  15055. (if (not t2)
  15056. t1
  15057. (setq dh (- h2 h1) dm (- m2 m1))
  15058. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15059. (concat t1 "+" (number-to-string dh)
  15060. (and (/= 0 dm) (format ":%02d" dm)))))))
  15061. (defun org-time-stamp-inactive (&optional arg)
  15062. "Insert an inactive time stamp.
  15063. An inactive time stamp is enclosed in square brackets instead of angle
  15064. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15065. does not link to the calendar and cannot be changed with the S-cursor keys.
  15066. So these are more for recording a certain time/date."
  15067. (interactive "P")
  15068. (org-time-stamp arg 'inactive))
  15069. (defvar org-date-ovl (make-overlay 1 1))
  15070. (overlay-put org-date-ovl 'face 'org-date-selected)
  15071. (org-detach-overlay org-date-ovl)
  15072. (defvar org-ans1) ; dynamically scoped parameter
  15073. (defvar org-ans2) ; dynamically scoped parameter
  15074. (defvar org-plain-time-of-day-regexp) ; defined below
  15075. (defvar org-overriding-default-time nil) ; dynamically scoped
  15076. (defvar org-read-date-overlay nil)
  15077. (defvar org-dcst nil) ; dynamically scoped
  15078. (defvar org-read-date-history nil)
  15079. (defvar org-read-date-final-answer nil)
  15080. (defvar org-read-date-analyze-futurep nil)
  15081. (defvar org-read-date-analyze-forced-year nil)
  15082. (defvar org-read-date-inactive)
  15083. (defvar org-read-date-minibuffer-local-map
  15084. (let* ((org-replace-disputed-keys nil)
  15085. (map (make-sparse-keymap)))
  15086. (set-keymap-parent map minibuffer-local-map)
  15087. (org-defkey map (kbd ".")
  15088. (lambda () (interactive)
  15089. ;; Are we at the beginning of the prompt?
  15090. (if (looking-back "^[^:]+: ")
  15091. (org-eval-in-calendar '(calendar-goto-today))
  15092. (insert "."))))
  15093. (org-defkey map (kbd "C-.")
  15094. (lambda () (interactive)
  15095. (org-eval-in-calendar '(calendar-goto-today))))
  15096. (org-defkey map [(meta shift left)]
  15097. (lambda () (interactive)
  15098. (org-eval-in-calendar '(calendar-backward-month 1))))
  15099. (org-defkey map [(meta shift right)]
  15100. (lambda () (interactive)
  15101. (org-eval-in-calendar '(calendar-forward-month 1))))
  15102. (org-defkey map [(meta shift up)]
  15103. (lambda () (interactive)
  15104. (org-eval-in-calendar '(calendar-backward-year 1))))
  15105. (org-defkey map [(meta shift down)]
  15106. (lambda () (interactive)
  15107. (org-eval-in-calendar '(calendar-forward-year 1))))
  15108. (org-defkey map [?\e (shift left)]
  15109. (lambda () (interactive)
  15110. (org-eval-in-calendar '(calendar-backward-month 1))))
  15111. (org-defkey map [?\e (shift right)]
  15112. (lambda () (interactive)
  15113. (org-eval-in-calendar '(calendar-forward-month 1))))
  15114. (org-defkey map [?\e (shift up)]
  15115. (lambda () (interactive)
  15116. (org-eval-in-calendar '(calendar-backward-year 1))))
  15117. (org-defkey map [?\e (shift down)]
  15118. (lambda () (interactive)
  15119. (org-eval-in-calendar '(calendar-forward-year 1))))
  15120. (org-defkey map [(shift up)]
  15121. (lambda () (interactive)
  15122. (org-eval-in-calendar '(calendar-backward-week 1))))
  15123. (org-defkey map [(shift down)]
  15124. (lambda () (interactive)
  15125. (org-eval-in-calendar '(calendar-forward-week 1))))
  15126. (org-defkey map [(shift left)]
  15127. (lambda () (interactive)
  15128. (org-eval-in-calendar '(calendar-backward-day 1))))
  15129. (org-defkey map [(shift right)]
  15130. (lambda () (interactive)
  15131. (org-eval-in-calendar '(calendar-forward-day 1))))
  15132. (org-defkey map "!"
  15133. (lambda () (interactive)
  15134. (org-eval-in-calendar '(diary-view-entries))
  15135. (message "")))
  15136. (org-defkey map ">"
  15137. (lambda () (interactive)
  15138. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15139. (org-defkey map "<"
  15140. (lambda () (interactive)
  15141. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15142. (org-defkey map "\C-v"
  15143. (lambda () (interactive)
  15144. (org-eval-in-calendar
  15145. '(calendar-scroll-left-three-months 1))))
  15146. (org-defkey map "\M-v"
  15147. (lambda () (interactive)
  15148. (org-eval-in-calendar
  15149. '(calendar-scroll-right-three-months 1))))
  15150. map)
  15151. "Keymap for minibuffer commands when using `org-read-date'.")
  15152. (defvar org-def)
  15153. (defvar org-defdecode)
  15154. (defvar org-with-time)
  15155. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15156. default-time default-input inactive)
  15157. "Read a date, possibly a time, and make things smooth for the user.
  15158. The prompt will suggest to enter an ISO date, but you can also enter anything
  15159. which will at least partially be understood by `parse-time-string'.
  15160. Unrecognized parts of the date will default to the current day, month, year,
  15161. hour and minute. If this command is called to replace a timestamp at point,
  15162. or to enter the second timestamp of a range, the default time is taken
  15163. from the existing stamp. Furthermore, the command prefers the future,
  15164. so if you are giving a date where the year is not given, and the day-month
  15165. combination is already past in the current year, it will assume you
  15166. mean next year. For details, see the manual. A few examples:
  15167. 3-2-5 --> 2003-02-05
  15168. feb 15 --> currentyear-02-15
  15169. 2/15 --> currentyear-02-15
  15170. sep 12 9 --> 2009-09-12
  15171. 12:45 --> today 12:45
  15172. 22 sept 0:34 --> currentyear-09-22 0:34
  15173. 12 --> currentyear-currentmonth-12
  15174. Fri --> nearest Friday after today
  15175. -Tue --> last Tuesday
  15176. etc.
  15177. Furthermore you can specify a relative date by giving, as the *first* thing
  15178. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15179. change in days weeks, months, years.
  15180. With a single plus or minus, the date is relative to today. With a double
  15181. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15182. +4d --> four days from today
  15183. +4 --> same as above
  15184. +2w --> two weeks from today
  15185. ++5 --> five days from default date
  15186. The function understands only English month and weekday abbreviations.
  15187. While prompting, a calendar is popped up - you can also select the
  15188. date with the mouse (button 1). The calendar shows a period of three
  15189. months. To scroll it to other months, use the keys `>' and `<'.
  15190. If you don't like the calendar, turn it off with
  15191. \(setq org-read-date-popup-calendar nil)
  15192. With optional argument TO-TIME, the date will immediately be converted
  15193. to an internal time.
  15194. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15195. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15196. still enter a time, and this function will inform the calling routine
  15197. about this change. The calling routine may then choose to change the
  15198. format used to insert the time stamp into the buffer to include the time.
  15199. With optional argument FROM-STRING, read from this string instead from
  15200. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15201. the time/date that is used for everything that is not specified by the
  15202. user."
  15203. (require 'parse-time)
  15204. (let* ((org-time-stamp-rounding-minutes
  15205. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15206. (org-dcst org-display-custom-times)
  15207. (ct (org-current-time))
  15208. (org-def (or org-overriding-default-time default-time ct))
  15209. (org-defdecode (decode-time org-def))
  15210. (dummy (progn
  15211. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15212. (setcar (nthcdr 2 org-defdecode) -1)
  15213. (setcar (nthcdr 1 org-defdecode) 59)
  15214. (setq org-def (apply 'encode-time org-defdecode)
  15215. org-defdecode (decode-time org-def)))))
  15216. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15217. (calendar-frame-setup nil)
  15218. (calendar-setup nil)
  15219. (calendar-move-hook nil)
  15220. (calendar-view-diary-initially-flag nil)
  15221. (calendar-view-holidays-initially-flag nil)
  15222. (timestr (format-time-string
  15223. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15224. (prompt (concat (if prompt (concat prompt " ") "")
  15225. (format "Date+time [%s]: " timestr)))
  15226. ans (org-ans0 "") org-ans1 org-ans2 final)
  15227. (cond
  15228. (from-string (setq ans from-string))
  15229. (org-read-date-popup-calendar
  15230. (save-excursion
  15231. (save-window-excursion
  15232. (calendar)
  15233. (org-eval-in-calendar '(setq cursor-type nil) t)
  15234. (unwind-protect
  15235. (progn
  15236. (calendar-forward-day (- (time-to-days org-def)
  15237. (calendar-absolute-from-gregorian
  15238. (calendar-current-date))))
  15239. (org-eval-in-calendar nil t)
  15240. (let* ((old-map (current-local-map))
  15241. (map (copy-keymap calendar-mode-map))
  15242. (minibuffer-local-map
  15243. (copy-keymap org-read-date-minibuffer-local-map)))
  15244. (org-defkey map (kbd "RET") 'org-calendar-select)
  15245. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15246. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15247. (unwind-protect
  15248. (progn
  15249. (use-local-map map)
  15250. (setq org-read-date-inactive inactive)
  15251. (add-hook 'post-command-hook 'org-read-date-display)
  15252. (setq org-ans0 (read-string prompt default-input
  15253. 'org-read-date-history nil))
  15254. ;; org-ans0: from prompt
  15255. ;; org-ans1: from mouse click
  15256. ;; org-ans2: from calendar motion
  15257. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15258. (remove-hook 'post-command-hook 'org-read-date-display)
  15259. (use-local-map old-map)
  15260. (when org-read-date-overlay
  15261. (delete-overlay org-read-date-overlay)
  15262. (setq org-read-date-overlay nil)))))
  15263. (bury-buffer "*Calendar*")))))
  15264. (t ; Naked prompt only
  15265. (unwind-protect
  15266. (setq ans (read-string prompt default-input
  15267. 'org-read-date-history timestr))
  15268. (when org-read-date-overlay
  15269. (delete-overlay org-read-date-overlay)
  15270. (setq org-read-date-overlay nil)))))
  15271. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15272. (when org-read-date-analyze-forced-year
  15273. (message "Year was forced into %s"
  15274. (if org-read-date-force-compatible-dates
  15275. "compatible range (1970-2037)"
  15276. "range representable on this machine"))
  15277. (ding))
  15278. ;; One round trip to get rid of 34th of August and stuff like that....
  15279. (setq final (decode-time (apply 'encode-time final)))
  15280. (setq org-read-date-final-answer ans)
  15281. (if to-time
  15282. (apply 'encode-time final)
  15283. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15284. (format "%04d-%02d-%02d %02d:%02d"
  15285. (nth 5 final) (nth 4 final) (nth 3 final)
  15286. (nth 2 final) (nth 1 final))
  15287. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15288. (defun org-read-date-display ()
  15289. "Display the current date prompt interpretation in the minibuffer."
  15290. (when org-read-date-display-live
  15291. (when org-read-date-overlay
  15292. (delete-overlay org-read-date-overlay))
  15293. (when (minibufferp (current-buffer))
  15294. (save-excursion
  15295. (end-of-line 1)
  15296. (while (not (equal (buffer-substring
  15297. (max (point-min) (- (point) 4)) (point))
  15298. " "))
  15299. (insert " ")))
  15300. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15301. " " (or org-ans1 org-ans2)))
  15302. (org-end-time-was-given nil)
  15303. (f (org-read-date-analyze ans org-def org-defdecode))
  15304. (fmts (if org-dcst
  15305. org-time-stamp-custom-formats
  15306. org-time-stamp-formats))
  15307. (fmt (if (or org-with-time
  15308. (and (boundp 'org-time-was-given) org-time-was-given))
  15309. (cdr fmts)
  15310. (car fmts)))
  15311. (txt (format-time-string fmt (apply 'encode-time f)))
  15312. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15313. (txt (concat "=> " txt)))
  15314. (when (and org-end-time-was-given
  15315. (string-match org-plain-time-of-day-regexp txt))
  15316. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15317. org-end-time-was-given
  15318. (substring txt (match-end 0)))))
  15319. (when org-read-date-analyze-futurep
  15320. (setq txt (concat txt " (=>F)")))
  15321. (setq org-read-date-overlay
  15322. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15323. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15324. (defun org-read-date-analyze (ans org-def org-defdecode)
  15325. "Analyze the combined answer of the date prompt."
  15326. ;; FIXME: cleanup and comment
  15327. (let ((nowdecode (decode-time (current-time)))
  15328. delta deltan deltaw deltadef year month day
  15329. hour minute second wday pm h2 m2 tl wday1
  15330. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15331. (setq org-read-date-analyze-futurep nil
  15332. org-read-date-analyze-forced-year nil)
  15333. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15334. (setq ans "+0"))
  15335. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15336. (setq ans (replace-match "" t t ans)
  15337. deltan (car delta)
  15338. deltaw (nth 1 delta)
  15339. deltadef (nth 2 delta)))
  15340. ;; Check if there is an iso week date in there. If yes, store the
  15341. ;; info and postpone interpreting it until the rest of the parsing
  15342. ;; is done.
  15343. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15344. (setq iso-year (if (match-end 1)
  15345. (org-small-year-to-year
  15346. (string-to-number (match-string 1 ans))))
  15347. iso-weekday (if (match-end 3)
  15348. (string-to-number (match-string 3 ans)))
  15349. iso-week (string-to-number (match-string 2 ans)))
  15350. (setq ans (replace-match "" t t ans)))
  15351. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15352. (when (string-match
  15353. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15354. (setq year (if (match-end 2)
  15355. (string-to-number (match-string 2 ans))
  15356. (progn (setq kill-year t)
  15357. (string-to-number (format-time-string "%Y"))))
  15358. month (string-to-number (match-string 3 ans))
  15359. day (string-to-number (match-string 4 ans)))
  15360. (if (< year 100) (setq year (+ 2000 year)))
  15361. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15362. t nil ans)))
  15363. ;; Help matching dotted european dates
  15364. (when (string-match
  15365. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15366. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15367. (setq kill-year t)
  15368. (string-to-number (format-time-string "%Y")))
  15369. day (string-to-number (match-string 1 ans))
  15370. month (string-to-number (match-string 2 ans))
  15371. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15372. t nil ans)))
  15373. ;; Help matching american dates, like 5/30 or 5/30/7
  15374. (when (string-match
  15375. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15376. (setq year (if (match-end 4)
  15377. (string-to-number (match-string 4 ans))
  15378. (progn (setq kill-year t)
  15379. (string-to-number (format-time-string "%Y"))))
  15380. month (string-to-number (match-string 1 ans))
  15381. day (string-to-number (match-string 2 ans)))
  15382. (if (< year 100) (setq year (+ 2000 year)))
  15383. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15384. t nil ans)))
  15385. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15386. ;; If there is a time with am/pm, and *no* time without it, we convert
  15387. ;; so that matching will be successful.
  15388. (loop for i from 1 to 2 do ; twice, for end time as well
  15389. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15390. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15391. (setq hour (string-to-number (match-string 1 ans))
  15392. minute (if (match-end 3)
  15393. (string-to-number (match-string 3 ans))
  15394. 0)
  15395. pm (equal ?p
  15396. (string-to-char (downcase (match-string 4 ans)))))
  15397. (if (and (= hour 12) (not pm))
  15398. (setq hour 0)
  15399. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15400. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15401. t t ans))))
  15402. ;; Check if a time range is given as a duration
  15403. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15404. (setq hour (string-to-number (match-string 1 ans))
  15405. h2 (+ hour (string-to-number (match-string 3 ans)))
  15406. minute (string-to-number (match-string 2 ans))
  15407. m2 (+ minute (if (match-end 5) (string-to-number
  15408. (match-string 5 ans))0)))
  15409. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15410. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15411. t t ans)))
  15412. ;; Check if there is a time range
  15413. (when (boundp 'org-end-time-was-given)
  15414. (setq org-time-was-given nil)
  15415. (when (and (string-match org-plain-time-of-day-regexp ans)
  15416. (match-end 8))
  15417. (setq org-end-time-was-given (match-string 8 ans))
  15418. (setq ans (concat (substring ans 0 (match-beginning 7))
  15419. (substring ans (match-end 7))))))
  15420. (setq tl (parse-time-string ans)
  15421. day (or (nth 3 tl) (nth 3 org-defdecode))
  15422. month (or (nth 4 tl)
  15423. (if (and org-read-date-prefer-future
  15424. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15425. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15426. (nth 4 org-defdecode)))
  15427. year (or (and (not kill-year) (nth 5 tl))
  15428. (if (and org-read-date-prefer-future
  15429. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15430. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15431. (nth 5 org-defdecode)))
  15432. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15433. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15434. second (or (nth 0 tl) 0)
  15435. wday (nth 6 tl))
  15436. (when (and (eq org-read-date-prefer-future 'time)
  15437. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15438. (equal day (nth 3 nowdecode))
  15439. (equal month (nth 4 nowdecode))
  15440. (equal year (nth 5 nowdecode))
  15441. (nth 2 tl)
  15442. (or (< (nth 2 tl) (nth 2 nowdecode))
  15443. (and (= (nth 2 tl) (nth 2 nowdecode))
  15444. (nth 1 tl)
  15445. (< (nth 1 tl) (nth 1 nowdecode)))))
  15446. (setq day (1+ day)
  15447. futurep t))
  15448. ;; Special date definitions below
  15449. (cond
  15450. (iso-week
  15451. ;; There was an iso week
  15452. (require 'cal-iso)
  15453. (setq futurep nil)
  15454. (setq year (or iso-year year)
  15455. day (or iso-weekday wday 1)
  15456. wday nil ; to make sure that the trigger below does not match
  15457. iso-date (calendar-gregorian-from-absolute
  15458. (calendar-absolute-from-iso
  15459. (list iso-week day year))))
  15460. ; FIXME: Should we also push ISO weeks into the future?
  15461. ; (when (and org-read-date-prefer-future
  15462. ; (not iso-year)
  15463. ; (< (calendar-absolute-from-gregorian iso-date)
  15464. ; (time-to-days (current-time))))
  15465. ; (setq year (1+ year)
  15466. ; iso-date (calendar-gregorian-from-absolute
  15467. ; (calendar-absolute-from-iso
  15468. ; (list iso-week day year)))))
  15469. (setq month (car iso-date)
  15470. year (nth 2 iso-date)
  15471. day (nth 1 iso-date)))
  15472. (deltan
  15473. (setq futurep nil)
  15474. (unless deltadef
  15475. (let ((now (decode-time (current-time))))
  15476. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15477. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15478. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15479. ((equal deltaw "m") (setq month (+ month deltan)))
  15480. ((equal deltaw "y") (setq year (+ year deltan)))))
  15481. ((and wday (not (nth 3 tl)))
  15482. ;; Weekday was given, but no day, so pick that day in the week
  15483. ;; on or after the derived date.
  15484. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15485. (unless (equal wday wday1)
  15486. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15487. (if (and (boundp 'org-time-was-given)
  15488. (nth 2 tl))
  15489. (setq org-time-was-given t))
  15490. (if (< year 100) (setq year (+ 2000 year)))
  15491. ;; Check of the date is representable
  15492. (if org-read-date-force-compatible-dates
  15493. (progn
  15494. (if (< year 1970)
  15495. (setq year 1970 org-read-date-analyze-forced-year t))
  15496. (if (> year 2037)
  15497. (setq year 2037 org-read-date-analyze-forced-year t)))
  15498. (condition-case nil
  15499. (ignore (encode-time second minute hour day month year))
  15500. (error
  15501. (setq year (nth 5 org-defdecode))
  15502. (setq org-read-date-analyze-forced-year t))))
  15503. (setq org-read-date-analyze-futurep futurep)
  15504. (list second minute hour day month year)))
  15505. (defvar parse-time-weekdays)
  15506. (defun org-read-date-get-relative (s today default)
  15507. "Check string S for special relative date string.
  15508. TODAY and DEFAULT are internal times, for today and for a default.
  15509. Return shift list (N what def-flag)
  15510. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15511. N is the number of WHATs to shift.
  15512. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15513. the DEFAULT date rather than TODAY."
  15514. (require 'parse-time)
  15515. (when (and
  15516. (string-match
  15517. (concat
  15518. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15519. "\\([0-9]+\\)?"
  15520. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15521. "\\([ \t]\\|$\\)") s)
  15522. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15523. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15524. (string-to-char (substring (match-string 1 s) -1))
  15525. ?+))
  15526. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15527. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15528. (what (if (match-end 3) (match-string 3 s) "d"))
  15529. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15530. (date (if rel default today))
  15531. (wday (nth 6 (decode-time date)))
  15532. delta)
  15533. (if wday1
  15534. (progn
  15535. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15536. (if (= delta 0) (setq delta 7))
  15537. (if (= dir ?-)
  15538. (progn
  15539. (setq delta (- delta 7))
  15540. (if (= delta 0) (setq delta -7))))
  15541. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15542. (list delta "d" rel))
  15543. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15544. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15545. "Turn a user-specified date into the internal representation.
  15546. The internal representation needed by the calendar is (month day year).
  15547. This is a wrapper to handle the brain-dead convention in calendar that
  15548. user function argument order change dependent on argument order."
  15549. (if (boundp 'calendar-date-style)
  15550. (cond
  15551. ((eq calendar-date-style 'american)
  15552. (list arg1 arg2 arg3))
  15553. ((eq calendar-date-style 'european)
  15554. (list arg2 arg1 arg3))
  15555. ((eq calendar-date-style 'iso)
  15556. (list arg2 arg3 arg1)))
  15557. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15558. (if (org-bound-and-true-p european-calendar-style)
  15559. (list arg2 arg1 arg3)
  15560. (list arg1 arg2 arg3)))))
  15561. (defun org-eval-in-calendar (form &optional keepdate)
  15562. "Eval FORM in the calendar window and return to current window.
  15563. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15564. otherwise stick to the current value of `org-ans2'."
  15565. (let ((sf (selected-frame))
  15566. (sw (selected-window)))
  15567. (select-window (get-buffer-window "*Calendar*" t))
  15568. (eval form)
  15569. (when (and (not keepdate) (calendar-cursor-to-date))
  15570. (let* ((date (calendar-cursor-to-date))
  15571. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15572. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15573. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15574. (select-window sw)
  15575. (org-select-frame-set-input-focus sf)))
  15576. (defun org-calendar-select ()
  15577. "Return to `org-read-date' with the date currently selected.
  15578. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15579. (interactive)
  15580. (when (calendar-cursor-to-date)
  15581. (let* ((date (calendar-cursor-to-date))
  15582. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15583. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15584. (if (active-minibuffer-window) (exit-minibuffer))))
  15585. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15586. "Insert a date stamp for the date given by the internal TIME.
  15587. WITH-HM means use the stamp format that includes the time of the day.
  15588. INACTIVE means use square brackets instead of angular ones, so that the
  15589. stamp will not contribute to the agenda.
  15590. PRE and POST are optional strings to be inserted before and after the
  15591. stamp.
  15592. The command returns the inserted time stamp."
  15593. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15594. stamp)
  15595. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15596. (insert-before-markers (or pre ""))
  15597. (when (listp extra)
  15598. (setq extra (car extra))
  15599. (if (and (stringp extra)
  15600. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15601. (setq extra (format "-%02d:%02d"
  15602. (string-to-number (match-string 1 extra))
  15603. (string-to-number (match-string 2 extra))))
  15604. (setq extra nil)))
  15605. (when extra
  15606. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15607. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15608. (insert-before-markers (or post ""))
  15609. (setq org-last-inserted-timestamp stamp)))
  15610. (defun org-toggle-time-stamp-overlays ()
  15611. "Toggle the use of custom time stamp formats."
  15612. (interactive)
  15613. (setq org-display-custom-times (not org-display-custom-times))
  15614. (unless org-display-custom-times
  15615. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15616. (while (setq p (next-single-property-change p 'display))
  15617. (if (and (get-text-property p 'display)
  15618. (eq (get-text-property p 'face) 'org-date))
  15619. (remove-text-properties
  15620. p (setq p (next-single-property-change p 'display))
  15621. '(display t))))
  15622. (set-buffer-modified-p bmp)))
  15623. (if (featurep 'xemacs)
  15624. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15625. (org-restart-font-lock)
  15626. (setq org-table-may-need-update t)
  15627. (if org-display-custom-times
  15628. (message "Time stamps are overlaid with custom format")
  15629. (message "Time stamp overlays removed")))
  15630. (defun org-display-custom-time (beg end)
  15631. "Overlay modified time stamp format over timestamp between BEG and END."
  15632. (let* ((ts (buffer-substring beg end))
  15633. t1 w1 with-hm tf time str w2 (off 0))
  15634. (save-match-data
  15635. (setq t1 (org-parse-time-string ts t))
  15636. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15637. (setq off (- (match-end 0) (match-beginning 0)))))
  15638. (setq end (- end off))
  15639. (setq w1 (- end beg)
  15640. with-hm (and (nth 1 t1) (nth 2 t1))
  15641. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15642. time (org-fix-decoded-time t1)
  15643. str (org-add-props
  15644. (format-time-string
  15645. (substring tf 1 -1) (apply 'encode-time time))
  15646. nil 'mouse-face 'highlight)
  15647. w2 (length str))
  15648. (if (not (= w2 w1))
  15649. (add-text-properties (1+ beg) (+ 2 beg)
  15650. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15651. (if (featurep 'xemacs)
  15652. (progn
  15653. (put-text-property beg end 'invisible t)
  15654. (put-text-property beg end 'end-glyph (make-glyph str)))
  15655. (put-text-property beg end 'display str))))
  15656. (defun org-translate-time (string)
  15657. "Translate all timestamps in STRING to custom format.
  15658. But do this only if the variable `org-display-custom-times' is set."
  15659. (when org-display-custom-times
  15660. (save-match-data
  15661. (let* ((start 0)
  15662. (re org-ts-regexp-both)
  15663. t1 with-hm inactive tf time str beg end)
  15664. (while (setq start (string-match re string start))
  15665. (setq beg (match-beginning 0)
  15666. end (match-end 0)
  15667. t1 (save-match-data
  15668. (org-parse-time-string (substring string beg end) t))
  15669. with-hm (and (nth 1 t1) (nth 2 t1))
  15670. inactive (equal (substring string beg (1+ beg)) "[")
  15671. tf (funcall (if with-hm 'cdr 'car)
  15672. org-time-stamp-custom-formats)
  15673. time (org-fix-decoded-time t1)
  15674. str (format-time-string
  15675. (concat
  15676. (if inactive "[" "<") (substring tf 1 -1)
  15677. (if inactive "]" ">"))
  15678. (apply 'encode-time time))
  15679. string (replace-match str t t string)
  15680. start (+ start (length str)))))))
  15681. string)
  15682. (defun org-fix-decoded-time (time)
  15683. "Set 0 instead of nil for the first 6 elements of time.
  15684. Don't touch the rest."
  15685. (let ((n 0))
  15686. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15687. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15688. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15689. "Difference between TIMESTAMP-STRING and now in days.
  15690. If SECONDS is non-nil, return the difference in seconds."
  15691. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15692. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15693. (funcall fdiff (current-time)))))
  15694. (defun org-deadline-close (timestamp-string &optional ndays)
  15695. "Is the time in TIMESTAMP-STRING close to the current date?"
  15696. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15697. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15698. (not (org-entry-is-done-p))))
  15699. (defun org-get-wdays (ts &optional delay zero-delay)
  15700. "Get the deadline lead time appropriate for timestring TS.
  15701. When DELAY is non-nil, get the delay time for scheduled items
  15702. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15703. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15704. don't try to find the delay cookie in the scheduled timestamp."
  15705. (let ((tv (if delay org-scheduled-delay-days
  15706. org-deadline-warning-days)))
  15707. (cond
  15708. ((or (and delay (< tv 0))
  15709. (and delay zero-delay (<= tv 0))
  15710. (and (not delay) (<= tv 0)))
  15711. ;; Enforce this value no matter what
  15712. (- tv))
  15713. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15714. ;; lead time is specified.
  15715. (floor (* (string-to-number (match-string 1 ts))
  15716. (cdr (assoc (match-string 2 ts)
  15717. '(("d" . 1) ("w" . 7)
  15718. ("m" . 30.4) ("y" . 365.25)
  15719. ("h" . 0.041667)))))))
  15720. ;; go for the default.
  15721. (t tv))))
  15722. (defun org-calendar-select-mouse (ev)
  15723. "Return to `org-read-date' with the date currently selected.
  15724. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15725. (interactive "e")
  15726. (mouse-set-point ev)
  15727. (when (calendar-cursor-to-date)
  15728. (let* ((date (calendar-cursor-to-date))
  15729. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15730. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15731. (if (active-minibuffer-window) (exit-minibuffer))))
  15732. (defun org-check-deadlines (ndays)
  15733. "Check if there are any deadlines due or past due.
  15734. A deadline is considered due if it happens within `org-deadline-warning-days'
  15735. days from today's date. If the deadline appears in an entry marked DONE,
  15736. it is not shown. The prefix arg NDAYS can be used to test that many
  15737. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15738. (interactive "P")
  15739. (let* ((org-warn-days
  15740. (cond
  15741. ((equal ndays '(4)) 100000)
  15742. (ndays (prefix-numeric-value ndays))
  15743. (t (abs org-deadline-warning-days))))
  15744. (case-fold-search nil)
  15745. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15746. (callback
  15747. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15748. (message "%d deadlines past-due or due within %d days"
  15749. (org-occur regexp nil callback)
  15750. org-warn-days)))
  15751. (defsubst org-re-timestamp (type)
  15752. "Return a regexp for timestamp TYPE.
  15753. Allowed values for TYPE are:
  15754. all: all timestamps
  15755. active: only active timestamps (<...>)
  15756. inactive: only inactive timestamps ([...])
  15757. scheduled: only scheduled timestamps
  15758. deadline: only deadline timestamps
  15759. closed: only closed time-stamps
  15760. When TYPE is nil, fall back on returning a regexp that matches
  15761. both scheduled and deadline timestamps."
  15762. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15763. ((eq type 'active) org-ts-regexp)
  15764. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15765. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15766. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15767. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15768. ((eq type 'scheduled-or-deadline)
  15769. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15770. (defun org-check-before-date (date)
  15771. "Check if there are deadlines or scheduled entries before DATE."
  15772. (interactive (list (org-read-date)))
  15773. (let ((case-fold-search nil)
  15774. (regexp (org-re-timestamp org-ts-type))
  15775. (callback
  15776. (lambda () (time-less-p
  15777. (org-time-string-to-time (match-string 1))
  15778. (org-time-string-to-time date)))))
  15779. (message "%d entries before %s"
  15780. (org-occur regexp nil callback) date)))
  15781. (defun org-check-after-date (date)
  15782. "Check if there are deadlines or scheduled entries after DATE."
  15783. (interactive (list (org-read-date)))
  15784. (let ((case-fold-search nil)
  15785. (regexp (org-re-timestamp org-ts-type))
  15786. (callback
  15787. (lambda () (not
  15788. (time-less-p
  15789. (org-time-string-to-time (match-string 1))
  15790. (org-time-string-to-time date))))))
  15791. (message "%d entries after %s"
  15792. (org-occur regexp nil callback) date)))
  15793. (defun org-check-dates-range (start-date end-date)
  15794. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15795. (interactive (list (org-read-date nil nil nil "Range starts")
  15796. (org-read-date nil nil nil "Range end")))
  15797. (let ((case-fold-search nil)
  15798. (regexp (org-re-timestamp org-ts-type))
  15799. (callback
  15800. (lambda ()
  15801. (let ((match (match-string 1)))
  15802. (and
  15803. (not (time-less-p
  15804. (org-time-string-to-time match)
  15805. (org-time-string-to-time start-date)))
  15806. (time-less-p
  15807. (org-time-string-to-time match)
  15808. (org-time-string-to-time end-date)))))))
  15809. (message "%d entries between %s and %s"
  15810. (org-occur regexp nil callback) start-date end-date)))
  15811. (defun org-evaluate-time-range (&optional to-buffer)
  15812. "Evaluate a time range by computing the difference between start and end.
  15813. Normally the result is just printed in the echo area, but with prefix arg
  15814. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15815. If the time range is actually in a table, the result is inserted into the
  15816. next column.
  15817. For time difference computation, a year is assumed to be exactly 365
  15818. days in order to avoid rounding problems."
  15819. (interactive "P")
  15820. (or
  15821. (org-clock-update-time-maybe)
  15822. (save-excursion
  15823. (unless (org-at-date-range-p t)
  15824. (goto-char (point-at-bol))
  15825. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15826. (if (not (org-at-date-range-p t))
  15827. (user-error "Not at a time-stamp range, and none found in current line")))
  15828. (let* ((ts1 (match-string 1))
  15829. (ts2 (match-string 2))
  15830. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15831. (match-end (match-end 0))
  15832. (time1 (org-time-string-to-time ts1))
  15833. (time2 (org-time-string-to-time ts2))
  15834. (t1 (org-float-time time1))
  15835. (t2 (org-float-time time2))
  15836. (diff (abs (- t2 t1)))
  15837. (negative (< (- t2 t1) 0))
  15838. ;; (ys (floor (* 365 24 60 60)))
  15839. (ds (* 24 60 60))
  15840. (hs (* 60 60))
  15841. (fy "%dy %dd %02d:%02d")
  15842. (fy1 "%dy %dd")
  15843. (fd "%dd %02d:%02d")
  15844. (fd1 "%dd")
  15845. (fh "%02d:%02d")
  15846. y d h m align)
  15847. (if havetime
  15848. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15849. y 0
  15850. d (floor (/ diff ds)) diff (mod diff ds)
  15851. h (floor (/ diff hs)) diff (mod diff hs)
  15852. m (floor (/ diff 60)))
  15853. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15854. y 0
  15855. d (floor (+ (/ diff ds) 0.5))
  15856. h 0 m 0))
  15857. (if (not to-buffer)
  15858. (message "%s" (org-make-tdiff-string y d h m))
  15859. (if (org-at-table-p)
  15860. (progn
  15861. (goto-char match-end)
  15862. (setq align t)
  15863. (and (looking-at " *|") (goto-char (match-end 0))))
  15864. (goto-char match-end))
  15865. (if (looking-at
  15866. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15867. (replace-match ""))
  15868. (if negative (insert " -"))
  15869. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15870. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15871. (insert " " (format fh h m))))
  15872. (if align (org-table-align))
  15873. (message "Time difference inserted")))))
  15874. (defun org-make-tdiff-string (y d h m)
  15875. (let ((fmt "")
  15876. (l nil))
  15877. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15878. l (push y l)))
  15879. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15880. l (push d l)))
  15881. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15882. l (push h l)))
  15883. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15884. l (push m l)))
  15885. (apply 'format fmt (nreverse l))))
  15886. (defun org-time-string-to-time (s &optional buffer pos)
  15887. "Convert a timestamp string into internal time."
  15888. (condition-case errdata
  15889. (apply 'encode-time (org-parse-time-string s))
  15890. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15891. s (if (not (and buffer pos))
  15892. ""
  15893. (format " at %d in buffer `%s'" pos buffer))
  15894. (cdr errdata)))))
  15895. (defun org-time-string-to-seconds (s)
  15896. "Convert a timestamp string to a number of seconds."
  15897. (org-float-time (org-time-string-to-time s)))
  15898. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15899. "Convert a time stamp to an absolute day number.
  15900. If there is a specifier for a cyclic time stamp, get the closest
  15901. date to DAYNR.
  15902. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15903. The variable `date' is bound by the calendar when this is called."
  15904. (cond
  15905. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15906. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15907. daynr
  15908. (+ daynr 1000)))
  15909. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15910. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15911. (time-to-days (current-time))) (match-string 0 s)
  15912. prefer show-all))
  15913. (t (time-to-days
  15914. (condition-case errdata
  15915. (apply 'encode-time (org-parse-time-string s))
  15916. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15917. s (if (not (and buffer pos))
  15918. ""
  15919. (format " at %d in buffer `%s'" pos buffer))
  15920. (cdr errdata))))))))
  15921. (defun org-days-to-iso-week (days)
  15922. "Return the iso week number."
  15923. (require 'cal-iso)
  15924. (car (calendar-iso-from-absolute days)))
  15925. (defun org-small-year-to-year (year)
  15926. "Convert 2-digit years into 4-digit years.
  15927. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15928. The year 2000 cannot be abbreviated. Any year larger than 99
  15929. is returned unchanged."
  15930. (if (< year 38)
  15931. (setq year (+ 2000 year))
  15932. (if (< year 100)
  15933. (setq year (+ 1900 year))))
  15934. year)
  15935. (defun org-time-from-absolute (d)
  15936. "Return the time corresponding to date D.
  15937. D may be an absolute day number, or a calendar-type list (month day year)."
  15938. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15939. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15940. (defun org-calendar-holiday ()
  15941. "List of holidays, for Diary display in Org-mode."
  15942. (require 'holidays)
  15943. (let ((hl (funcall
  15944. (if (fboundp 'calendar-check-holidays)
  15945. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15946. (if hl (mapconcat 'identity hl "; "))))
  15947. (defun org-diary-sexp-entry (sexp entry date)
  15948. "Process a SEXP diary ENTRY for DATE."
  15949. (require 'diary-lib)
  15950. (let ((result (if calendar-debug-sexp
  15951. (let ((stack-trace-on-error t))
  15952. (eval (car (read-from-string sexp))))
  15953. (condition-case nil
  15954. (eval (car (read-from-string sexp)))
  15955. (error
  15956. (beep)
  15957. (message "Bad sexp at line %d in %s: %s"
  15958. (org-current-line)
  15959. (buffer-file-name) sexp)
  15960. (sleep-for 2))))))
  15961. (cond ((stringp result) (split-string result "; "))
  15962. ((and (consp result)
  15963. (not (consp (cdr result)))
  15964. (stringp (cdr result))) (cdr result))
  15965. ((and (consp result)
  15966. (stringp (car result))) result)
  15967. (result entry))))
  15968. (defun org-diary-to-ical-string (frombuf)
  15969. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15970. This uses the icalendar.el library."
  15971. (let* ((tmpdir (if (featurep 'xemacs)
  15972. (temp-directory)
  15973. temporary-file-directory))
  15974. (tmpfile (make-temp-name
  15975. (expand-file-name "orgics" tmpdir)))
  15976. buf rtn b e)
  15977. (with-current-buffer frombuf
  15978. (icalendar-export-region (point-min) (point-max) tmpfile)
  15979. (setq buf (find-buffer-visiting tmpfile))
  15980. (set-buffer buf)
  15981. (goto-char (point-min))
  15982. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15983. (setq b (match-beginning 0)))
  15984. (goto-char (point-max))
  15985. (if (re-search-backward "^END:VEVENT" nil t)
  15986. (setq e (match-end 0)))
  15987. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15988. (kill-buffer buf)
  15989. (delete-file tmpfile)
  15990. rtn))
  15991. (defun org-closest-date (start current change prefer show-all)
  15992. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15993. When PREFER is `past', return a date that is either CURRENT or past.
  15994. When PREFER is `future', return a date that is either CURRENT or future.
  15995. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15996. ;; Make the proper lists from the dates
  15997. (catch 'exit
  15998. (let ((a1 '(("h" . hour)
  15999. ("d" . day)
  16000. ("w" . week)
  16001. ("m" . month)
  16002. ("y" . year)))
  16003. (shour (nth 2 (org-parse-time-string start)))
  16004. dn dw sday cday n1 n2 n0
  16005. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16006. (setq start (org-date-to-gregorian start)
  16007. current (org-date-to-gregorian
  16008. (if show-all
  16009. current
  16010. (time-to-days (current-time))))
  16011. sday (calendar-absolute-from-gregorian start)
  16012. cday (calendar-absolute-from-gregorian current))
  16013. (if (<= cday sday) (throw 'exit sday))
  16014. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  16015. (setq dn (string-to-number (match-string 1 change))
  16016. dw (cdr (assoc (match-string 2 change) a1)))
  16017. (user-error "Invalid change specifier: %s" change))
  16018. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16019. (cond
  16020. ((eq dw 'hour)
  16021. (let ((missing-hours
  16022. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  16023. dn)))
  16024. (setq n1 (if (zerop missing-hours) cday
  16025. (- cday (1+ (floor (/ missing-hours 24)))))
  16026. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  16027. ((eq dw 'day)
  16028. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16029. n2 (+ n1 dn)))
  16030. ((eq dw 'year)
  16031. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16032. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16033. (setq date1 (list m d y1)
  16034. n1 (calendar-absolute-from-gregorian date1)
  16035. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16036. n2 (calendar-absolute-from-gregorian date2)))
  16037. ((eq dw 'month)
  16038. ;; approx number of month between the two dates
  16039. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16040. ;; How often does dn fit in there?
  16041. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16042. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16043. m (+ m nm)
  16044. ny (floor (/ m 12))
  16045. y (+ y ny)
  16046. m (- m (* ny 12)))
  16047. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16048. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16049. (setq m2 (+ m dn) y2 y)
  16050. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16051. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16052. (while (<= n2 cday)
  16053. (setq n1 n2 m m2 y y2)
  16054. (setq m2 (+ m dn) y2 y)
  16055. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16056. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16057. ;; Make sure n1 is the earlier date
  16058. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  16059. (if show-all
  16060. (cond
  16061. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16062. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16063. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16064. (cond
  16065. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16066. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16067. (t (if (= cday n1) n1 n2)))))))
  16068. (defun org-date-to-gregorian (date)
  16069. "Turn any specification of DATE into a Gregorian date for the calendar."
  16070. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16071. ((and (listp date) (= (length date) 3)) date)
  16072. ((stringp date)
  16073. (setq date (org-parse-time-string date))
  16074. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16075. ((listp date)
  16076. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16077. (defun org-parse-time-string (s &optional nodefault)
  16078. "Parse the standard Org-mode time string.
  16079. This should be a lot faster than the normal `parse-time-string'.
  16080. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16081. hour and minute fields will be nil if not given."
  16082. (cond ((string-match org-ts-regexp0 s)
  16083. (list 0
  16084. (if (or (match-beginning 8) (not nodefault))
  16085. (string-to-number (or (match-string 8 s) "0")))
  16086. (if (or (match-beginning 7) (not nodefault))
  16087. (string-to-number (or (match-string 7 s) "0")))
  16088. (string-to-number (match-string 4 s))
  16089. (string-to-number (match-string 3 s))
  16090. (string-to-number (match-string 2 s))
  16091. nil nil nil))
  16092. ((string-match "^<[^>]+>$" s)
  16093. (decode-time (seconds-to-time (org-matcher-time s))))
  16094. (t (error "Not a standard Org-mode time string: %s" s))))
  16095. (defun org-timestamp-up (&optional arg)
  16096. "Increase the date item at the cursor by one.
  16097. If the cursor is on the year, change the year. If it is on the month,
  16098. the day or the time, change that.
  16099. With prefix ARG, change by that many units."
  16100. (interactive "p")
  16101. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16102. (defun org-timestamp-down (&optional arg)
  16103. "Decrease the date item at the cursor by one.
  16104. If the cursor is on the year, change the year. If it is on the month,
  16105. the day or the time, change that.
  16106. With prefix ARG, change by that many units."
  16107. (interactive "p")
  16108. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16109. (defun org-timestamp-up-day (&optional arg)
  16110. "Increase the date in the time stamp by one day.
  16111. With prefix ARG, change that many days."
  16112. (interactive "p")
  16113. (if (and (not (org-at-timestamp-p t))
  16114. (org-at-heading-p))
  16115. (org-todo 'up)
  16116. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16117. (defun org-timestamp-down-day (&optional arg)
  16118. "Decrease the date in the time stamp by one day.
  16119. With prefix ARG, change that many days."
  16120. (interactive "p")
  16121. (if (and (not (org-at-timestamp-p t))
  16122. (org-at-heading-p))
  16123. (org-todo 'down)
  16124. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16125. (defun org-at-timestamp-p (&optional inactive-ok)
  16126. "Determine if the cursor is in or at a timestamp."
  16127. (interactive)
  16128. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16129. (pos (point))
  16130. (ans (or (looking-at tsr)
  16131. (save-excursion
  16132. (skip-chars-backward "^[<\n\r\t")
  16133. (if (> (point) (point-min)) (backward-char 1))
  16134. (and (looking-at tsr)
  16135. (> (- (match-end 0) pos) -1))))))
  16136. (and ans
  16137. (boundp 'org-ts-what)
  16138. (setq org-ts-what
  16139. (cond
  16140. ((= pos (match-beginning 0)) 'bracket)
  16141. ;; Point is considered to be "on the bracket" whether
  16142. ;; it's really on it or right after it.
  16143. ((= pos (1- (match-end 0))) 'bracket)
  16144. ((= pos (match-end 0)) 'after)
  16145. ((org-pos-in-match-range pos 2) 'year)
  16146. ((org-pos-in-match-range pos 3) 'month)
  16147. ((org-pos-in-match-range pos 7) 'hour)
  16148. ((org-pos-in-match-range pos 8) 'minute)
  16149. ((or (org-pos-in-match-range pos 4)
  16150. (org-pos-in-match-range pos 5)) 'day)
  16151. ((and (> pos (or (match-end 8) (match-end 5)))
  16152. (< pos (match-end 0)))
  16153. (- pos (or (match-end 8) (match-end 5))))
  16154. (t 'day))))
  16155. ans))
  16156. (defun org-toggle-timestamp-type ()
  16157. "Toggle the type (<active> or [inactive]) of a time stamp."
  16158. (interactive)
  16159. (when (org-at-timestamp-p t)
  16160. (let ((beg (match-beginning 0)) (end (match-end 0))
  16161. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16162. (save-excursion
  16163. (goto-char beg)
  16164. (while (re-search-forward "[][<>]" end t)
  16165. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16166. t t)))
  16167. (message "Timestamp is now %sactive"
  16168. (if (equal (char-after beg) ?<) "" "in")))))
  16169. (defun org-at-clock-log-p nil
  16170. "Is the cursor on the clock log line?"
  16171. (save-excursion
  16172. (move-beginning-of-line 1)
  16173. (looking-at org-clock-line-re)))
  16174. (defvar org-clock-history) ; defined in org-clock.el
  16175. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16176. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16177. "Change the date in the time stamp at point.
  16178. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16179. `year', `minute', `second'. If WHAT is not given, the cursor position
  16180. in the timestamp determines what will be changed.
  16181. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16182. (let ((origin (point)) origin-cat
  16183. with-hm inactive
  16184. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16185. org-ts-what
  16186. extra rem
  16187. ts time time0 fixnext clrgx)
  16188. (if (not (org-at-timestamp-p t))
  16189. (user-error "Not at a timestamp"))
  16190. (if (and (not what) (eq org-ts-what 'bracket))
  16191. (org-toggle-timestamp-type)
  16192. ;; Point isn't on brackets. Remember the part of the time-stamp
  16193. ;; the point was in. Indeed, size of time-stamps may change,
  16194. ;; but point must be kept in the same category nonetheless.
  16195. (setq origin-cat org-ts-what)
  16196. (if (and (not what) (not (eq org-ts-what 'day))
  16197. org-display-custom-times
  16198. (get-text-property (point) 'display)
  16199. (not (get-text-property (1- (point)) 'display)))
  16200. (setq org-ts-what 'day))
  16201. (setq org-ts-what (or what org-ts-what)
  16202. inactive (= (char-after (match-beginning 0)) ?\[)
  16203. ts (match-string 0))
  16204. (replace-match "")
  16205. (when (string-match
  16206. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16207. ts)
  16208. (setq extra (match-string 1 ts))
  16209. (if suppress-tmp-delay
  16210. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16211. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16212. (setq with-hm t))
  16213. (setq time0 (org-parse-time-string ts))
  16214. (when (and updown
  16215. (eq org-ts-what 'minute)
  16216. (not current-prefix-arg))
  16217. ;; This looks like s-up and s-down. Change by one rounding step.
  16218. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16219. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16220. (setcar (cdr time0) (+ (nth 1 time0)
  16221. (if (> n 0) (- rem) (- dm rem))))))
  16222. (setq time
  16223. (encode-time (or (car time0) 0)
  16224. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16225. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16226. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16227. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16228. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16229. (nthcdr 6 time0)))
  16230. (when (and (member org-ts-what '(hour minute))
  16231. extra
  16232. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16233. (setq extra (org-modify-ts-extra
  16234. extra
  16235. (if (eq org-ts-what 'hour) 2 5)
  16236. n dm)))
  16237. (when (integerp org-ts-what)
  16238. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16239. (if (eq what 'calendar)
  16240. (let ((cal-date (org-get-date-from-calendar)))
  16241. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16242. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16243. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16244. (setcar time0 (or (car time0) 0))
  16245. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16246. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16247. (setq time (apply 'encode-time time0))))
  16248. ;; Insert the new time-stamp, and ensure point stays in the same
  16249. ;; category as before (i.e. not after the last position in that
  16250. ;; category).
  16251. (let ((pos (point)))
  16252. ;; Stay before inserted string. `save-excursion' is of no use.
  16253. (setq org-last-changed-timestamp
  16254. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16255. (goto-char pos))
  16256. (save-match-data
  16257. (looking-at org-ts-regexp3)
  16258. (goto-char (cond
  16259. ;; `day' category ends before `hour' if any, or at
  16260. ;; the end of the day name.
  16261. ((eq origin-cat 'day)
  16262. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16263. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16264. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16265. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16266. ;; `year' and `month' have both fixed size: point
  16267. ;; couldn't have moved into another part.
  16268. (t origin))))
  16269. ;; Update clock if on a CLOCK line.
  16270. (org-clock-update-time-maybe)
  16271. ;; Maybe adjust the closest clock in `org-clock-history'
  16272. (when org-clock-adjust-closest
  16273. (if (not (and (org-at-clock-log-p)
  16274. (< 1 (length (delq nil (mapcar 'marker-position
  16275. org-clock-history))))))
  16276. (message "No clock to adjust")
  16277. (cond ((save-excursion ; fix previous clock?
  16278. (re-search-backward org-ts-regexp0 nil t)
  16279. (org-looking-back (concat org-clock-string " \\[")))
  16280. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16281. ((save-excursion ; fix next clock?
  16282. (re-search-backward org-ts-regexp0 nil t)
  16283. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16284. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16285. (save-window-excursion
  16286. ;; Find closest clock to point, adjust the previous/next one in history
  16287. (let* ((p (save-excursion (org-back-to-heading t)))
  16288. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16289. (clfixnth
  16290. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16291. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16292. (if (not clfixpos)
  16293. (message "No clock to adjust")
  16294. (save-excursion
  16295. (org-goto-marker-or-bmk clfixpos)
  16296. (org-show-subtree)
  16297. (when (re-search-forward clrgx nil t)
  16298. (goto-char (match-beginning 1))
  16299. (let (org-clock-adjust-closest)
  16300. (org-timestamp-change n org-ts-what updown))
  16301. (message "Clock adjusted in %s for heading: %s"
  16302. (file-name-nondirectory (buffer-file-name))
  16303. (org-get-heading t t)))))))))
  16304. ;; Try to recenter the calendar window, if any.
  16305. (if (and org-calendar-follow-timestamp-change
  16306. (get-buffer-window "*Calendar*" t)
  16307. (memq org-ts-what '(day month year)))
  16308. (org-recenter-calendar (time-to-days time))))))
  16309. (defun org-modify-ts-extra (s pos n dm)
  16310. "Change the different parts of the lead-time and repeat fields in timestamp."
  16311. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16312. ng h m new rem)
  16313. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16314. (cond
  16315. ((or (org-pos-in-match-range pos 2)
  16316. (org-pos-in-match-range pos 3))
  16317. (setq m (string-to-number (match-string 3 s))
  16318. h (string-to-number (match-string 2 s)))
  16319. (if (org-pos-in-match-range pos 2)
  16320. (setq h (+ h n))
  16321. (setq n (* dm (org-no-warnings (signum n))))
  16322. (when (not (= 0 (setq rem (% m dm))))
  16323. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16324. (setq m (+ m n)))
  16325. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16326. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16327. (setq h (min 24 (max 0 h)))
  16328. (setq ng 1 new (format "-%02d:%02d" h m)))
  16329. ((org-pos-in-match-range pos 6)
  16330. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16331. ((org-pos-in-match-range pos 5)
  16332. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16333. ((org-pos-in-match-range pos 9)
  16334. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16335. ((org-pos-in-match-range pos 8)
  16336. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16337. (when ng
  16338. (setq s (concat
  16339. (substring s 0 (match-beginning ng))
  16340. new
  16341. (substring s (match-end ng))))))
  16342. s))
  16343. (defun org-recenter-calendar (date)
  16344. "If the calendar is visible, recenter it to DATE."
  16345. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16346. (when cwin
  16347. (let ((calendar-move-hook nil))
  16348. (with-selected-window cwin
  16349. (calendar-goto-date (if (listp date) date
  16350. (calendar-gregorian-from-absolute date))))))))
  16351. (defun org-goto-calendar (&optional arg)
  16352. "Go to the Emacs calendar at the current date.
  16353. If there is a time stamp in the current line, go to that date.
  16354. A prefix ARG can be used to force the current date."
  16355. (interactive "P")
  16356. (let ((tsr org-ts-regexp) diff
  16357. (calendar-move-hook nil)
  16358. (calendar-view-holidays-initially-flag nil)
  16359. (calendar-view-diary-initially-flag nil))
  16360. (if (or (org-at-timestamp-p)
  16361. (save-excursion
  16362. (beginning-of-line 1)
  16363. (looking-at (concat ".*" tsr))))
  16364. (let ((d1 (time-to-days (current-time)))
  16365. (d2 (time-to-days
  16366. (org-time-string-to-time (match-string 1)))))
  16367. (setq diff (- d2 d1))))
  16368. (calendar)
  16369. (calendar-goto-today)
  16370. (if (and diff (not arg)) (calendar-forward-day diff))))
  16371. (defun org-get-date-from-calendar ()
  16372. "Return a list (month day year) of date at point in calendar."
  16373. (with-current-buffer "*Calendar*"
  16374. (save-match-data
  16375. (calendar-cursor-to-date))))
  16376. (defun org-date-from-calendar ()
  16377. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16378. If there is already a time stamp at the cursor position, update it."
  16379. (interactive)
  16380. (if (org-at-timestamp-p t)
  16381. (org-timestamp-change 0 'calendar)
  16382. (let ((cal-date (org-get-date-from-calendar)))
  16383. (org-insert-time-stamp
  16384. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16385. (defcustom org-effort-durations
  16386. `(("h" . 60)
  16387. ("d" . ,(* 60 8))
  16388. ("w" . ,(* 60 8 5))
  16389. ("m" . ,(* 60 8 5 4))
  16390. ("y" . ,(* 60 8 5 40)))
  16391. "Conversion factor to minutes for an effort modifier.
  16392. Each entry has the form (MODIFIER . MINUTES).
  16393. In an effort string, a number followed by MODIFIER is multiplied
  16394. by the specified number of MINUTES to obtain an effort in
  16395. minutes.
  16396. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16397. effort string \"2hours\" is equivalent to 120 minutes."
  16398. :group 'org-agenda
  16399. :version "24.1"
  16400. :type '(alist :key-type (string :tag "Modifier")
  16401. :value-type (number :tag "Minutes")))
  16402. (defun org-minutes-to-clocksum-string (m)
  16403. "Format number of minutes as a clocksum string.
  16404. The format is determined by `org-time-clocksum-format',
  16405. `org-time-clocksum-use-fractional' and
  16406. `org-time-clocksum-fractional-format' and
  16407. `org-time-clocksum-use-effort-durations'."
  16408. (let ((clocksum "")
  16409. (m (round m)) ; Don't allow fractions of minutes
  16410. h d w mo y fmt n)
  16411. (setq h (if org-time-clocksum-use-effort-durations
  16412. (cdr (assoc "h" org-effort-durations)) 60)
  16413. d (if org-time-clocksum-use-effort-durations
  16414. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16415. w (if org-time-clocksum-use-effort-durations
  16416. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16417. mo (if org-time-clocksum-use-effort-durations
  16418. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16419. y (if org-time-clocksum-use-effort-durations
  16420. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16421. ;; fractional format
  16422. (if org-time-clocksum-use-fractional
  16423. (cond
  16424. ;; single format string
  16425. ((stringp org-time-clocksum-fractional-format)
  16426. (format org-time-clocksum-fractional-format (/ m (float h))))
  16427. ;; choice of fractional formats for different time units
  16428. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16429. (> (/ (truncate m) (* y d h)) 0))
  16430. (format fmt (/ m (* y d (float h)))))
  16431. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16432. (> (/ (truncate m) (* mo d h)) 0))
  16433. (format fmt (/ m (* mo d (float h)))))
  16434. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16435. (> (/ (truncate m) (* w d h)) 0))
  16436. (format fmt (/ m (* w d (float h)))))
  16437. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16438. (> (/ (truncate m) (* d h)) 0))
  16439. (format fmt (/ m (* d (float h)))))
  16440. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16441. (> (/ (truncate m) h) 0))
  16442. (format fmt (/ m (float h))))
  16443. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16444. (format fmt m))
  16445. ;; fall back to smallest time unit with a format
  16446. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16447. (format fmt (/ m (float h))))
  16448. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16449. (format fmt (/ m (* d (float h)))))
  16450. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16451. (format fmt (/ m (* w d (float h)))))
  16452. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16453. (format fmt (/ m (* mo d (float h)))))
  16454. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16455. (format fmt (/ m (* y d (float h))))))
  16456. ;; standard (non-fractional) format, with single format string
  16457. (if (stringp org-time-clocksum-format)
  16458. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16459. ;; separate formats components
  16460. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16461. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16462. (plist-get org-time-clocksum-format :require-years))
  16463. (setq clocksum (concat clocksum (format fmt n))
  16464. m (- m (* n y d h))))
  16465. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16466. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16467. (plist-get org-time-clocksum-format :require-months))
  16468. (setq clocksum (concat clocksum (format fmt n))
  16469. m (- m (* n mo d h))))
  16470. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16471. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16472. (plist-get org-time-clocksum-format :require-weeks))
  16473. (setq clocksum (concat clocksum (format fmt n))
  16474. m (- m (* n w d h))))
  16475. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16476. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16477. (plist-get org-time-clocksum-format :require-days))
  16478. (setq clocksum (concat clocksum (format fmt n))
  16479. m (- m (* n d h))))
  16480. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16481. (or (> (setq n (/ (truncate m) h)) 0)
  16482. (plist-get org-time-clocksum-format :require-hours))
  16483. (setq clocksum (concat clocksum (format fmt n))
  16484. m (- m (* n h))))
  16485. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16486. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16487. (setq clocksum (concat clocksum (format fmt m))))
  16488. ;; return formatted time duration
  16489. clocksum))))
  16490. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16491. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16492. "Org mode version 8.0")
  16493. (defun org-hours-to-clocksum-string (n)
  16494. (org-minutes-to-clocksum-string (* n 60)))
  16495. (defun org-hh:mm-string-to-minutes (s)
  16496. "Convert a string H:MM to a number of minutes.
  16497. If the string is just a number, interpret it as minutes.
  16498. In fact, the first hh:mm or number in the string will be taken,
  16499. there can be extra stuff in the string.
  16500. If no number is found, the return value is 0."
  16501. (cond
  16502. ((integerp s) s)
  16503. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16504. (+ (* (string-to-number (match-string 1 s)) 60)
  16505. (string-to-number (match-string 2 s))))
  16506. ((string-match "\\([0-9]+\\)" s)
  16507. (string-to-number (match-string 1 s)))
  16508. (t 0)))
  16509. (defcustom org-image-actual-width t
  16510. "Should we use the actual width of images when inlining them?
  16511. When set to `t', always use the image width.
  16512. When set to a number, use imagemagick (when available) to set
  16513. the image's width to this value.
  16514. When set to a number in a list, try to get the width from any
  16515. #+ATTR.* keyword if it matches a width specification like
  16516. #+ATTR_HTML: :width 300px
  16517. and fall back on that number if none is found.
  16518. When set to nil, try to get the width from an #+ATTR.* keyword
  16519. and fall back on the original width if none is found.
  16520. This requires Emacs >= 24.1, build with imagemagick support."
  16521. :group 'org-appearance
  16522. :version "24.4"
  16523. :package-version '(Org . "8.0")
  16524. :type '(choice
  16525. (const :tag "Use the image width" t)
  16526. (integer :tag "Use a number of pixels")
  16527. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16528. (const :tag "Use #+ATTR* or don't resize" nil)))
  16529. (defcustom org-agenda-inhibit-startup nil
  16530. "Inhibit startup when preparing agenda buffers.
  16531. When this variable is `t', the initialization of the Org agenda
  16532. buffers is inhibited: e.g. the visibility state is not set, the
  16533. tables are not re-aligned, etc."
  16534. :type 'boolean
  16535. :version "24.3"
  16536. :group 'org-agenda)
  16537. (define-obsolete-variable-alias
  16538. 'org-agenda-ignore-drawer-properties
  16539. 'org-agenda-ignore-properties "25.1")
  16540. (defcustom org-agenda-ignore-properties nil
  16541. "Avoid updating text properties when building the agenda.
  16542. Properties are used to prepare buffers for effort estimates,
  16543. appointments, statistics and subtree-local categories.
  16544. If you don't use these in the agenda, you can add them to this
  16545. list and agenda building will be a bit faster.
  16546. The value is a list, with zero or more of the symbols `effort', `appt',
  16547. `stats' or `category'."
  16548. :type '(set :greedy t
  16549. (const effort)
  16550. (const appt)
  16551. (const stats)
  16552. (const category))
  16553. :version "25.1"
  16554. :package-version '(Org . "8.3")
  16555. :group 'org-agenda)
  16556. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16557. "Convert a duration string S to minutes.
  16558. A bare number is interpreted as minutes, modifiers can be set by
  16559. customizing `org-effort-durations' (which see).
  16560. Entries containing a colon are interpreted as H:MM by
  16561. `org-hh:mm-string-to-minutes'."
  16562. (let ((result 0)
  16563. (re (concat "\\([0-9.]+\\) *\\("
  16564. (regexp-opt (mapcar 'car org-effort-durations))
  16565. "\\)")))
  16566. (while (string-match re s)
  16567. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16568. (string-to-number (match-string 1 s))))
  16569. (setq s (replace-match "" nil t s)))
  16570. (setq result (floor result))
  16571. (incf result (org-hh:mm-string-to-minutes s))
  16572. (if output-to-string (number-to-string result) result)))
  16573. ;;;; Files
  16574. (defun org-save-all-org-buffers ()
  16575. "Save all Org-mode buffers without user confirmation."
  16576. (interactive)
  16577. (message "Saving all Org-mode buffers...")
  16578. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16579. (when (featurep 'org-id) (org-id-locations-save))
  16580. (message "Saving all Org-mode buffers... done"))
  16581. (defun org-revert-all-org-buffers ()
  16582. "Revert all Org-mode buffers.
  16583. Prompt for confirmation when there are unsaved changes.
  16584. Be sure you know what you are doing before letting this function
  16585. overwrite your changes.
  16586. This function is useful in a setup where one tracks org files
  16587. with a version control system, to revert on one machine after pulling
  16588. changes from another. I believe the procedure must be like this:
  16589. 1. M-x org-save-all-org-buffers
  16590. 2. Pull changes from the other machine, resolve conflicts
  16591. 3. M-x org-revert-all-org-buffers"
  16592. (interactive)
  16593. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16594. (user-error "Abort"))
  16595. (save-excursion
  16596. (save-window-excursion
  16597. (mapc
  16598. (lambda (b)
  16599. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16600. (with-current-buffer b buffer-file-name))
  16601. (org-pop-to-buffer-same-window b)
  16602. (revert-buffer t 'no-confirm)))
  16603. (buffer-list))
  16604. (when (and (featurep 'org-id) org-id-track-globally)
  16605. (org-id-locations-load)))))
  16606. ;;;; Agenda files
  16607. ;;;###autoload
  16608. (defun org-switchb (&optional arg)
  16609. "Switch between Org buffers.
  16610. With one prefix argument, restrict available buffers to files.
  16611. With two prefix arguments, restrict available buffers to agenda files.
  16612. Defaults to `iswitchb' for buffer name completion.
  16613. Set `org-completion-use-ido' to make it use ido instead."
  16614. (interactive "P")
  16615. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16616. ((equal arg '(16)) (org-buffer-list 'agenda))
  16617. (t (org-buffer-list))))
  16618. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16619. (org-completion-use-ido org-completion-use-ido))
  16620. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16621. (setq org-completion-use-iswitchb t))
  16622. (org-pop-to-buffer-same-window
  16623. (org-icompleting-read "Org buffer: "
  16624. (mapcar 'list (mapcar 'buffer-name blist))
  16625. nil t))))
  16626. ;;; Define some older names previously used for this functionality
  16627. ;;;###autoload
  16628. (defalias 'org-ido-switchb 'org-switchb)
  16629. ;;;###autoload
  16630. (defalias 'org-iswitchb 'org-switchb)
  16631. (defun org-buffer-list (&optional predicate exclude-tmp)
  16632. "Return a list of Org buffers.
  16633. PREDICATE can be `export', `files' or `agenda'.
  16634. export restrict the list to Export buffers.
  16635. files restrict the list to buffers visiting Org files.
  16636. agenda restrict the list to buffers visiting agenda files.
  16637. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16638. (let* ((bfn nil)
  16639. (agenda-files (and (eq predicate 'agenda)
  16640. (mapcar 'file-truename (org-agenda-files t))))
  16641. (filter
  16642. (cond
  16643. ((eq predicate 'files)
  16644. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16645. ((eq predicate 'export)
  16646. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16647. ((eq predicate 'agenda)
  16648. (lambda (b)
  16649. (with-current-buffer b
  16650. (and (derived-mode-p 'org-mode)
  16651. (setq bfn (buffer-file-name b))
  16652. (member (file-truename bfn) agenda-files)))))
  16653. (t (lambda (b) (with-current-buffer b
  16654. (or (derived-mode-p 'org-mode)
  16655. (string-match "\*Org .*Export"
  16656. (buffer-name b)))))))))
  16657. (delq nil
  16658. (mapcar
  16659. (lambda(b)
  16660. (if (and (funcall filter b)
  16661. (or (not exclude-tmp)
  16662. (not (string-match "tmp" (buffer-name b)))))
  16663. b
  16664. nil))
  16665. (buffer-list)))))
  16666. (defun org-agenda-files (&optional unrestricted archives)
  16667. "Get the list of agenda files.
  16668. Optional UNRESTRICTED means return the full list even if a restriction
  16669. is currently in place.
  16670. When ARCHIVES is t, include all archive files that are really being
  16671. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16672. `org-agenda-archives-mode' is t."
  16673. (let ((files
  16674. (cond
  16675. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16676. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16677. ((listp org-agenda-files) org-agenda-files)
  16678. (t (error "Invalid value of `org-agenda-files'")))))
  16679. (setq files (apply 'append
  16680. (mapcar (lambda (f)
  16681. (if (file-directory-p f)
  16682. (directory-files
  16683. f t org-agenda-file-regexp)
  16684. (list f)))
  16685. files)))
  16686. (when org-agenda-skip-unavailable-files
  16687. (setq files (delq nil
  16688. (mapcar (function
  16689. (lambda (file)
  16690. (and (file-readable-p file) file)))
  16691. files))))
  16692. (when (or (eq archives t)
  16693. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16694. (setq files (org-add-archive-files files)))
  16695. files))
  16696. (defun org-agenda-file-p (&optional file)
  16697. "Return non-nil, if FILE is an agenda file.
  16698. If FILE is omitted, use the file associated with the current
  16699. buffer."
  16700. (let ((fname (or file (buffer-file-name))))
  16701. (and fname
  16702. (member (file-truename fname)
  16703. (mapcar #'file-truename (org-agenda-files t))))))
  16704. (defun org-edit-agenda-file-list ()
  16705. "Edit the list of agenda files.
  16706. Depending on setup, this either uses customize to edit the variable
  16707. `org-agenda-files', or it visits the file that is holding the list. In the
  16708. latter case, the buffer is set up in a way that saving it automatically kills
  16709. the buffer and restores the previous window configuration."
  16710. (interactive)
  16711. (if (stringp org-agenda-files)
  16712. (let ((cw (current-window-configuration)))
  16713. (find-file org-agenda-files)
  16714. (org-set-local 'org-window-configuration cw)
  16715. (org-add-hook 'after-save-hook
  16716. (lambda ()
  16717. (set-window-configuration
  16718. (prog1 org-window-configuration
  16719. (kill-buffer (current-buffer))))
  16720. (org-install-agenda-files-menu)
  16721. (message "New agenda file list installed"))
  16722. nil 'local)
  16723. (message "%s" (substitute-command-keys
  16724. "Edit list and finish with \\[save-buffer]")))
  16725. (customize-variable 'org-agenda-files)))
  16726. (defun org-store-new-agenda-file-list (list)
  16727. "Set new value for the agenda file list and save it correctly."
  16728. (if (stringp org-agenda-files)
  16729. (let ((fe (org-read-agenda-file-list t)) b u)
  16730. (while (setq b (find-buffer-visiting org-agenda-files))
  16731. (kill-buffer b))
  16732. (with-temp-file org-agenda-files
  16733. (insert
  16734. (mapconcat
  16735. (lambda (f) ;; Keep un-expanded entries.
  16736. (if (setq u (assoc f fe))
  16737. (cdr u)
  16738. f))
  16739. list "\n")
  16740. "\n")))
  16741. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16742. (org-insert-mode-line-in-empty-file nil))
  16743. (setq org-agenda-files list)
  16744. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16745. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16746. "Read the list of agenda files from a file.
  16747. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16748. filenames, used by `org-store-new-agenda-file-list' to write back
  16749. un-expanded file names."
  16750. (when (file-directory-p org-agenda-files)
  16751. (error "`org-agenda-files' cannot be a single directory"))
  16752. (when (stringp org-agenda-files)
  16753. (with-temp-buffer
  16754. (insert-file-contents org-agenda-files)
  16755. (mapcar
  16756. (lambda (f)
  16757. (let ((e (expand-file-name (substitute-in-file-name f)
  16758. org-directory)))
  16759. (if pair-with-expansion
  16760. (cons e f)
  16761. e)))
  16762. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16763. ;;;###autoload
  16764. (defun org-cycle-agenda-files ()
  16765. "Cycle through the files in `org-agenda-files'.
  16766. If the current buffer visits an agenda file, find the next one in the list.
  16767. If the current buffer does not, find the first agenda file."
  16768. (interactive)
  16769. (let* ((fs (org-agenda-files t))
  16770. (files (append fs (list (car fs))))
  16771. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16772. file)
  16773. (unless files (user-error "No agenda files"))
  16774. (catch 'exit
  16775. (while (setq file (pop files))
  16776. (if (equal (file-truename file) tcf)
  16777. (when (car files)
  16778. (find-file (car files))
  16779. (throw 'exit t))))
  16780. (find-file (car fs)))
  16781. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16782. (defun org-agenda-file-to-front (&optional to-end)
  16783. "Move/add the current file to the top of the agenda file list.
  16784. If the file is not present in the list, it is added to the front. If it is
  16785. present, it is moved there. With optional argument TO-END, add/move to the
  16786. end of the list."
  16787. (interactive "P")
  16788. (let ((org-agenda-skip-unavailable-files nil)
  16789. (file-alist (mapcar (lambda (x)
  16790. (cons (file-truename x) x))
  16791. (org-agenda-files t)))
  16792. (ctf (file-truename
  16793. (or buffer-file-name
  16794. (user-error "Please save the current buffer to a file"))))
  16795. x had)
  16796. (setq x (assoc ctf file-alist) had x)
  16797. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16798. (if to-end
  16799. (setq file-alist (append (delq x file-alist) (list x)))
  16800. (setq file-alist (cons x (delq x file-alist))))
  16801. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16802. (org-install-agenda-files-menu)
  16803. (message "File %s to %s of agenda file list"
  16804. (if had "moved" "added") (if to-end "end" "front"))))
  16805. (defun org-remove-file (&optional file)
  16806. "Remove current file from the list of files in variable `org-agenda-files'.
  16807. These are the files which are being checked for agenda entries.
  16808. Optional argument FILE means use this file instead of the current."
  16809. (interactive)
  16810. (let* ((org-agenda-skip-unavailable-files nil)
  16811. (file (or file buffer-file-name
  16812. (user-error "Current buffer does not visit a file")))
  16813. (true-file (file-truename file))
  16814. (afile (abbreviate-file-name file))
  16815. (files (delq nil (mapcar
  16816. (lambda (x)
  16817. (if (equal true-file
  16818. (file-truename x))
  16819. nil x))
  16820. (org-agenda-files t)))))
  16821. (if (not (= (length files) (length (org-agenda-files t))))
  16822. (progn
  16823. (org-store-new-agenda-file-list files)
  16824. (org-install-agenda-files-menu)
  16825. (message "Removed file: %s" afile))
  16826. (message "File was not in list: %s (not removed)" afile))))
  16827. (defun org-file-menu-entry (file)
  16828. (vector file (list 'find-file file) t))
  16829. (defun org-check-agenda-file (file)
  16830. "Make sure FILE exists. If not, ask user what to do."
  16831. (when (not (file-exists-p file))
  16832. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16833. (abbreviate-file-name file))
  16834. (let ((r (downcase (read-char-exclusive))))
  16835. (cond
  16836. ((equal r ?r)
  16837. (org-remove-file file)
  16838. (throw 'nextfile t))
  16839. (t (user-error "Abort"))))))
  16840. (defun org-get-agenda-file-buffer (file)
  16841. "Get an agenda buffer visiting FILE.
  16842. If the buffer needs to be created, add it to the list of buffers
  16843. which might be released later."
  16844. (let ((buf (org-find-base-buffer-visiting file)))
  16845. (if buf
  16846. buf ; just return it
  16847. ;; Make a new buffer and remember it
  16848. (setq buf (find-file-noselect file))
  16849. (if buf (push buf org-agenda-new-buffers))
  16850. buf)))
  16851. (defun org-release-buffers (blist)
  16852. "Release all buffers in list, asking the user for confirmation when needed.
  16853. When a buffer is unmodified, it is just killed. When modified, it is saved
  16854. \(if the user agrees) and then killed."
  16855. (let (buf file)
  16856. (while (setq buf (pop blist))
  16857. (setq file (buffer-file-name buf))
  16858. (when (and (buffer-modified-p buf)
  16859. file
  16860. (y-or-n-p (format "Save file %s? " file)))
  16861. (with-current-buffer buf (save-buffer)))
  16862. (kill-buffer buf))))
  16863. (defun org-agenda-prepare-buffers (files)
  16864. "Create buffers for all agenda files, protect archived trees and comments."
  16865. (interactive)
  16866. (let ((pa '(:org-archived t))
  16867. (pc '(:org-comment t))
  16868. (pall '(:org-archived t :org-comment t))
  16869. (inhibit-read-only t)
  16870. (org-inhibit-startup org-agenda-inhibit-startup)
  16871. (rea (concat ":" org-archive-tag ":"))
  16872. file re pos)
  16873. (setq org-tag-alist-for-agenda nil
  16874. org-tag-groups-alist-for-agenda nil)
  16875. (save-excursion
  16876. (save-restriction
  16877. (while (setq file (pop files))
  16878. (catch 'nextfile
  16879. (if (bufferp file)
  16880. (set-buffer file)
  16881. (org-check-agenda-file file)
  16882. (set-buffer (org-get-agenda-file-buffer file)))
  16883. (widen)
  16884. (org-set-regexps-and-options-for-tags)
  16885. (setq pos (point))
  16886. (goto-char (point-min))
  16887. (let ((case-fold-search t))
  16888. (when (search-forward "#+setupfile" nil t)
  16889. ;; Don't set all regexps and options systematically as
  16890. ;; this is only run for setting agenda tags from setup
  16891. ;; file
  16892. (org-set-regexps-and-options)))
  16893. (or (memq 'category org-agenda-ignore-properties)
  16894. (org-refresh-category-properties))
  16895. (or (memq 'stats org-agenda-ignore-properties)
  16896. (org-refresh-stats-properties))
  16897. (or (memq 'effort org-agenda-ignore-properties)
  16898. (org-refresh-effort-properties))
  16899. (or (memq 'appt org-agenda-ignore-properties)
  16900. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16901. (setq org-todo-keywords-for-agenda
  16902. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16903. (setq org-done-keywords-for-agenda
  16904. (append org-done-keywords-for-agenda org-done-keywords))
  16905. (setq org-todo-keyword-alist-for-agenda
  16906. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16907. (setq org-tag-alist-for-agenda
  16908. (org-uniquify
  16909. (append org-tag-alist-for-agenda
  16910. org-tag-alist
  16911. org-tag-persistent-alist)))
  16912. (if org-group-tags
  16913. (setq org-tag-groups-alist-for-agenda
  16914. (org-uniquify-alist
  16915. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16916. (org-with-silent-modifications
  16917. (save-excursion
  16918. (remove-text-properties (point-min) (point-max) pall)
  16919. (when org-agenda-skip-archived-trees
  16920. (goto-char (point-min))
  16921. (while (re-search-forward rea nil t)
  16922. (if (org-at-heading-p t)
  16923. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16924. (goto-char (point-min))
  16925. (setq re (format "^\\* .*\\<%s\\>" org-comment-string))
  16926. (while (re-search-forward re nil t)
  16927. (when (save-match-data (org-in-commented-heading-p t))
  16928. (add-text-properties
  16929. (match-beginning 0) (org-end-of-subtree t) pc)))))
  16930. (goto-char pos)))))
  16931. (setq org-todo-keywords-for-agenda
  16932. (org-uniquify org-todo-keywords-for-agenda))
  16933. (setq org-todo-keyword-alist-for-agenda
  16934. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16935. ;;;; CDLaTeX minor mode
  16936. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16937. "Keymap for the minor `org-cdlatex-mode'.")
  16938. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16939. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16940. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16941. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16942. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  16943. (defvar org-cdlatex-texmathp-advice-is-done nil
  16944. "Flag remembering if we have applied the advice to texmathp already.")
  16945. (define-minor-mode org-cdlatex-mode
  16946. "Toggle the minor `org-cdlatex-mode'.
  16947. This mode supports entering LaTeX environment and math in LaTeX fragments
  16948. in Org-mode.
  16949. \\{org-cdlatex-mode-map}"
  16950. nil " OCDL" nil
  16951. (when org-cdlatex-mode
  16952. (require 'cdlatex)
  16953. (run-hooks 'cdlatex-mode-hook)
  16954. (cdlatex-compute-tables))
  16955. (unless org-cdlatex-texmathp-advice-is-done
  16956. (setq org-cdlatex-texmathp-advice-is-done t)
  16957. (defadvice texmathp (around org-math-always-on activate)
  16958. "Always return t in org-mode buffers.
  16959. This is because we want to insert math symbols without dollars even outside
  16960. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16961. an embedded LaTeX fragment, let texmathp do its job.
  16962. \\[org-cdlatex-mode-map]"
  16963. (interactive)
  16964. (let (p)
  16965. (cond
  16966. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16967. ((eq this-command 'cdlatex-math-symbol)
  16968. (setq ad-return-value t
  16969. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16970. (t
  16971. (let ((p (org-inside-LaTeX-fragment-p)))
  16972. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16973. (setq ad-return-value t
  16974. texmathp-why '("Org-mode embedded math" . 0))
  16975. (if p ad-do-it)))))))))
  16976. (defun turn-on-org-cdlatex ()
  16977. "Unconditionally turn on `org-cdlatex-mode'."
  16978. (org-cdlatex-mode 1))
  16979. (defun org-try-cdlatex-tab ()
  16980. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16981. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16982. - inside a LaTeX fragment, or
  16983. - after the first word in a line, where an abbreviation expansion could
  16984. insert a LaTeX environment."
  16985. (when org-cdlatex-mode
  16986. (cond
  16987. ;; Before any word on the line: No expansion possible.
  16988. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16989. ;; Just after first word on the line: Expand it. Make sure it
  16990. ;; cannot happen on headlines, though.
  16991. ((save-excursion
  16992. (skip-chars-backward "a-zA-Z0-9*")
  16993. (skip-chars-backward " \t")
  16994. (and (bolp) (not (org-at-heading-p))))
  16995. (cdlatex-tab) t)
  16996. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16997. (defun org-cdlatex-underscore-caret (&optional arg)
  16998. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16999. Revert to the normal definition outside of these fragments."
  17000. (interactive "P")
  17001. (if (org-inside-LaTeX-fragment-p)
  17002. (call-interactively 'cdlatex-sub-superscript)
  17003. (let (org-cdlatex-mode)
  17004. (call-interactively (key-binding (vector last-input-event))))))
  17005. (defun org-cdlatex-math-modify (&optional arg)
  17006. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17007. Revert to the normal definition outside of these fragments."
  17008. (interactive "P")
  17009. (if (org-inside-LaTeX-fragment-p)
  17010. (call-interactively 'cdlatex-math-modify)
  17011. (let (org-cdlatex-mode)
  17012. (call-interactively (key-binding (vector last-input-event))))))
  17013. (defun org-cdlatex-environment-indent (&optional environment item)
  17014. "Execute `cdlatex-environment' and indent the inserted environment."
  17015. (interactive)
  17016. (cdlatex-environment environment item)
  17017. (let ((element (org-element-at-point)))
  17018. (org-indent-region (org-element-property :begin element)
  17019. (org-element-property :end element))))
  17020. ;;;; LaTeX fragments
  17021. (defun org-inside-LaTeX-fragment-p ()
  17022. "Test if point is inside a LaTeX fragment.
  17023. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17024. sequence appearing also before point.
  17025. Even though the matchers for math are configurable, this function assumes
  17026. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17027. delimiters are skipped when they have been removed by customization.
  17028. The return value is nil, or a cons cell with the delimiter and the
  17029. position of this delimiter.
  17030. This function does a reasonably good job, but can locally be fooled by
  17031. for example currency specifications. For example it will assume being in
  17032. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17033. fragments that are properly closed, but during editing, we have to live
  17034. with the uncertainty caused by missing closing delimiters. This function
  17035. looks only before point, not after."
  17036. (catch 'exit
  17037. (let ((pos (point))
  17038. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17039. (lim (progn
  17040. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17041. (point)))
  17042. dd-on str (start 0) m re)
  17043. (goto-char pos)
  17044. (when dodollar
  17045. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17046. re (nth 1 (assoc "$" org-latex-regexps)))
  17047. (while (string-match re str start)
  17048. (cond
  17049. ((= (match-end 0) (length str))
  17050. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17051. ((= (match-end 0) (- (length str) 5))
  17052. (throw 'exit nil))
  17053. (t (setq start (match-end 0))))))
  17054. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17055. (goto-char pos)
  17056. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17057. (and (match-beginning 2) (throw 'exit nil))
  17058. ;; count $$
  17059. (while (re-search-backward "\\$\\$" lim t)
  17060. (setq dd-on (not dd-on)))
  17061. (goto-char pos)
  17062. (if dd-on (cons "$$" m))))))
  17063. (defun org-inside-latex-macro-p ()
  17064. "Is point inside a LaTeX macro or its arguments?"
  17065. (save-match-data
  17066. (org-in-regexp
  17067. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17068. (defvar org-latex-fragment-image-overlays nil
  17069. "List of overlays carrying the images of latex fragments.")
  17070. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  17071. (defun org-remove-latex-fragment-image-overlays ()
  17072. "Remove all overlays with LaTeX fragment images in current buffer."
  17073. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  17074. (setq org-latex-fragment-image-overlays nil))
  17075. (define-obsolete-function-alias
  17076. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17077. (defun org-toggle-latex-fragment (&optional subtree)
  17078. "Preview the LaTeX fragment at point, or all locally or globally.
  17079. If the cursor is in a LaTeX fragment, create the image and overlay
  17080. it over the source code. If there is no fragment at point, display
  17081. all fragments in the current text, from one headline to the next. With
  17082. prefix SUBTREE, display all fragments in the current subtree. With a
  17083. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  17084. the cursor is before the first headline,
  17085. display all fragments in the buffer.
  17086. The images can be removed again with \\[org-toggle-latex-fragment]."
  17087. (interactive "P")
  17088. (unless buffer-file-name
  17089. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17090. (if org-latex-fragment-image-overlays
  17091. (progn (org-remove-latex-fragment-image-overlays)
  17092. (message "LaTeX fragments images removed"))
  17093. (when (display-graphic-p)
  17094. (org-remove-latex-fragment-image-overlays)
  17095. (save-excursion
  17096. (save-restriction
  17097. (let (beg end at msg)
  17098. (cond
  17099. ((or (equal subtree '(16))
  17100. (not (save-excursion
  17101. (re-search-backward org-outline-regexp-bol nil t))))
  17102. (setq beg (point-min) end (point-max)
  17103. msg "Creating images for buffer...%s"))
  17104. ((equal subtree '(4))
  17105. (org-back-to-heading)
  17106. (setq beg (point) end (org-end-of-subtree t)
  17107. msg "Creating images for subtree...%s"))
  17108. (t
  17109. (if (setq at (org-inside-LaTeX-fragment-p))
  17110. (goto-char (max (point-min) (- (cdr at) 2)))
  17111. (org-back-to-heading))
  17112. (setq beg (point) end (progn (outline-next-heading) (point))
  17113. msg (if at "Creating image...%s"
  17114. "Creating images for entry...%s"))))
  17115. (message msg "")
  17116. (narrow-to-region beg end)
  17117. (goto-char beg)
  17118. (org-format-latex
  17119. (concat org-latex-preview-ltxpng-directory
  17120. (file-name-sans-extension
  17121. (file-name-nondirectory buffer-file-name)))
  17122. default-directory 'overlays msg at 'forbuffer
  17123. org-latex-create-formula-image-program)
  17124. (message msg "done. Use `C-c C-x C-l' to remove images.")))))))
  17125. (defun org-format-latex (prefix &optional dir overlays msg at
  17126. forbuffer processing-type)
  17127. "Replace LaTeX fragments with links to an image, and produce images.
  17128. Some of the options can be changed using the variable
  17129. `org-format-latex-options'."
  17130. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17131. (let* ((prefixnodir (file-name-nondirectory prefix))
  17132. (absprefix (expand-file-name prefix dir))
  17133. (todir (file-name-directory absprefix))
  17134. (opt org-format-latex-options)
  17135. (optnew org-format-latex-options)
  17136. (matchers (plist-get opt :matchers))
  17137. (re-list org-latex-regexps)
  17138. (cnt 0) txt hash link beg end re e checkdir
  17139. string
  17140. m n block-type block linkfile movefile ov)
  17141. ;; Check the different regular expressions
  17142. (while (setq e (pop re-list))
  17143. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  17144. block (if block-type "\n\n" ""))
  17145. (when (member m matchers)
  17146. (goto-char (point-min))
  17147. (while (re-search-forward re nil t)
  17148. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  17149. (or (not overlays)
  17150. (not (eq (get-char-property (match-beginning n)
  17151. 'org-overlay-type)
  17152. 'org-latex-overlay))))
  17153. (cond
  17154. ((eq processing-type 'verbatim))
  17155. ((eq processing-type 'mathjax)
  17156. ;; Prepare for MathJax processing.
  17157. (setq string (match-string n))
  17158. (when (member m '("$" "$1"))
  17159. (save-excursion
  17160. (delete-region (match-beginning n) (match-end n))
  17161. (goto-char (match-beginning n))
  17162. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  17163. ((or (eq processing-type 'dvipng)
  17164. (eq processing-type 'imagemagick))
  17165. ;; Process to an image.
  17166. (setq txt (match-string n)
  17167. beg (match-beginning n) end (match-end n)
  17168. cnt (1+ cnt))
  17169. (let ((face (face-at-point))
  17170. (fg (plist-get opt :foreground))
  17171. (bg (plist-get opt :background))
  17172. ;; Ensure full list is printed.
  17173. print-length print-level)
  17174. (when forbuffer
  17175. ;; Get the colors from the face at point.
  17176. (goto-char beg)
  17177. (when (eq fg 'auto)
  17178. (setq fg (face-attribute face :foreground nil 'default)))
  17179. (when (eq bg 'auto)
  17180. (setq bg (face-attribute face :background nil 'default)))
  17181. (setq optnew (copy-sequence opt))
  17182. (plist-put optnew :foreground fg)
  17183. (plist-put optnew :background bg))
  17184. (setq hash (sha1 (prin1-to-string
  17185. (list org-format-latex-header
  17186. org-latex-default-packages-alist
  17187. org-latex-packages-alist
  17188. org-format-latex-options
  17189. forbuffer txt fg bg)))
  17190. linkfile (format "%s_%s.png" prefix hash)
  17191. movefile (format "%s_%s.png" absprefix hash)))
  17192. (setq link (concat block "[[file:" linkfile "]]" block))
  17193. (if msg (message msg cnt))
  17194. (goto-char beg)
  17195. (unless checkdir ; Ensure the directory exists.
  17196. (setq checkdir t)
  17197. (or (file-directory-p todir) (make-directory todir t)))
  17198. (unless (file-exists-p movefile)
  17199. (org-create-formula-image
  17200. txt movefile optnew forbuffer processing-type))
  17201. (if overlays
  17202. (progn
  17203. (mapc (lambda (o)
  17204. (if (eq (overlay-get o 'org-overlay-type)
  17205. 'org-latex-overlay)
  17206. (delete-overlay o)))
  17207. (overlays-in beg end))
  17208. (setq ov (make-overlay beg end))
  17209. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  17210. (if (featurep 'xemacs)
  17211. (progn
  17212. (overlay-put ov 'invisible t)
  17213. (overlay-put
  17214. ov 'end-glyph
  17215. (make-glyph (vector 'png :file movefile))))
  17216. (overlay-put
  17217. ov 'display
  17218. (list 'image :type 'png :file movefile :ascent 'center)))
  17219. (push ov org-latex-fragment-image-overlays)
  17220. (goto-char end))
  17221. (delete-region beg end)
  17222. (insert (org-add-props link
  17223. (list 'org-latex-src
  17224. (replace-regexp-in-string
  17225. "\"" "" txt)
  17226. 'org-latex-src-embed-type
  17227. (if block-type 'paragraph 'character))))))
  17228. ((eq processing-type 'mathml)
  17229. ;; Process to MathML
  17230. (unless (save-match-data (org-format-latex-mathml-available-p))
  17231. (user-error "LaTeX to MathML converter not configured"))
  17232. (setq txt (match-string n)
  17233. beg (match-beginning n) end (match-end n)
  17234. cnt (1+ cnt))
  17235. (if msg (message msg cnt))
  17236. (goto-char beg)
  17237. (delete-region beg end)
  17238. (insert (org-format-latex-as-mathml
  17239. txt block-type prefix dir)))
  17240. (t
  17241. (error "Unknown conversion type %s for LaTeX fragments"
  17242. processing-type)))))))))
  17243. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17244. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17245. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17246. sucessful, return the portion between \"<math...> </math>\"
  17247. elements otherwise return nil. When MATHML-FILE is specified,
  17248. write the results in to that file. When invoked as an
  17249. interactive command, prompt for LATEX-FRAG, with initial value
  17250. set to the current active region and echo the results for user
  17251. inspection."
  17252. (interactive (list (let ((frag (when (org-region-active-p)
  17253. (buffer-substring-no-properties
  17254. (region-beginning) (region-end)))))
  17255. (read-string "LaTeX Fragment: " frag nil frag))))
  17256. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17257. (let* ((tmp-in-file (file-relative-name
  17258. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17259. (ignore (write-region latex-frag nil tmp-in-file))
  17260. (tmp-out-file (file-relative-name
  17261. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17262. (cmd (format-spec
  17263. org-latex-to-mathml-convert-command
  17264. `((?j . ,(shell-quote-argument
  17265. (expand-file-name org-latex-to-mathml-jar-file)))
  17266. (?I . ,(shell-quote-argument tmp-in-file))
  17267. (?o . ,(shell-quote-argument tmp-out-file)))))
  17268. mathml shell-command-output)
  17269. (when (org-called-interactively-p 'any)
  17270. (unless (org-format-latex-mathml-available-p)
  17271. (user-error "LaTeX to MathML converter not configured")))
  17272. (message "Running %s" cmd)
  17273. (setq shell-command-output (shell-command-to-string cmd))
  17274. (setq mathml
  17275. (when (file-readable-p tmp-out-file)
  17276. (with-current-buffer (find-file-noselect tmp-out-file t)
  17277. (goto-char (point-min))
  17278. (when (re-search-forward
  17279. (concat
  17280. (regexp-quote
  17281. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  17282. "\\(.\\|\n\\)*"
  17283. (regexp-quote "</math>")) nil t)
  17284. (prog1 (match-string 0) (kill-buffer))))))
  17285. (cond
  17286. (mathml
  17287. (setq mathml
  17288. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17289. (when mathml-file
  17290. (write-region mathml nil mathml-file))
  17291. (when (org-called-interactively-p 'any)
  17292. (message mathml)))
  17293. ((message "LaTeX to MathML conversion failed")
  17294. (message shell-command-output)))
  17295. (delete-file tmp-in-file)
  17296. (when (file-exists-p tmp-out-file)
  17297. (delete-file tmp-out-file))
  17298. mathml))
  17299. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17300. prefix &optional dir)
  17301. "Use `org-create-math-formula' but check local cache first."
  17302. (let* ((absprefix (expand-file-name prefix dir))
  17303. (print-length nil) (print-level nil)
  17304. (formula-id (concat
  17305. "formula-"
  17306. (sha1
  17307. (prin1-to-string
  17308. (list latex-frag
  17309. org-latex-to-mathml-convert-command)))))
  17310. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17311. (formula-cache-dir (file-name-directory formula-cache)))
  17312. (unless (file-directory-p formula-cache-dir)
  17313. (make-directory formula-cache-dir t))
  17314. (unless (file-exists-p formula-cache)
  17315. (org-create-math-formula latex-frag formula-cache))
  17316. (if (file-exists-p formula-cache)
  17317. ;; Successful conversion. Return the link to MathML file.
  17318. (org-add-props
  17319. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17320. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17321. 'org-latex-src-embed-type (if latex-frag-type
  17322. 'paragraph 'character)))
  17323. ;; Failed conversion. Return the LaTeX fragment verbatim
  17324. latex-frag)))
  17325. (defun org-create-formula-image (string tofile options buffer &optional type)
  17326. "Create an image from LaTeX source using dvipng or convert.
  17327. This function calls either `org-create-formula-image-with-dvipng'
  17328. or `org-create-formula-image-with-imagemagick' depending on the
  17329. value of `org-latex-create-formula-image-program' or on the value
  17330. of the optional TYPE variable.
  17331. Note: ultimately these two function should be combined as they
  17332. share a good deal of logic."
  17333. (org-check-external-command
  17334. "latex" "needed to convert LaTeX fragments to images")
  17335. (funcall
  17336. (case (or type org-latex-create-formula-image-program)
  17337. ('dvipng
  17338. (org-check-external-command
  17339. "dvipng" "needed to convert LaTeX fragments to images")
  17340. 'org-create-formula-image-with-dvipng)
  17341. ('imagemagick
  17342. (org-check-external-command
  17343. "convert" "you need to install imagemagick")
  17344. 'org-create-formula-image-with-imagemagick)
  17345. (t (error
  17346. "Invalid value of `org-latex-create-formula-image-program'")))
  17347. string tofile options buffer))
  17348. (declare-function org-export-get-backend "ox" (name))
  17349. (declare-function org-export--get-global-options "ox" (&optional backend))
  17350. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17351. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17352. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17353. (defun org-create-formula--latex-header ()
  17354. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17355. (let ((info (org-combine-plists (org-export--get-global-options
  17356. (org-export-get-backend 'latex))
  17357. (org-export--get-inbuffer-options
  17358. (org-export-get-backend 'latex)))))
  17359. (org-latex-guess-babel-language
  17360. (org-latex-guess-inputenc
  17361. (org-splice-latex-header
  17362. org-format-latex-header
  17363. org-latex-default-packages-alist
  17364. org-latex-packages-alist t
  17365. (plist-get info :latex-header)))
  17366. info)))
  17367. ;; This function borrows from Ganesh Swami's latex2png.el
  17368. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17369. "This calls dvipng."
  17370. (require 'ox-latex)
  17371. (let* ((tmpdir (if (featurep 'xemacs)
  17372. (temp-directory)
  17373. temporary-file-directory))
  17374. (texfilebase (make-temp-name
  17375. (expand-file-name "orgtex" tmpdir)))
  17376. (texfile (concat texfilebase ".tex"))
  17377. (dvifile (concat texfilebase ".dvi"))
  17378. (pngfile (concat texfilebase ".png"))
  17379. (fnh (if (featurep 'xemacs)
  17380. (font-height (face-font 'default))
  17381. (face-attribute 'default :height nil)))
  17382. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17383. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17384. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17385. "Black"))
  17386. (bg (or (plist-get options (if buffer :background :html-background))
  17387. "Transparent")))
  17388. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17389. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17390. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17391. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17392. (let ((latex-header (org-create-formula--latex-header)))
  17393. (with-temp-file texfile
  17394. (insert latex-header)
  17395. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17396. (let ((dir default-directory))
  17397. (ignore-errors
  17398. (cd tmpdir)
  17399. (call-process "latex" nil nil nil texfile))
  17400. (cd dir))
  17401. (if (not (file-exists-p dvifile))
  17402. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17403. (ignore-errors
  17404. (if (featurep 'xemacs)
  17405. (call-process "dvipng" nil nil nil
  17406. "-fg" fg "-bg" bg
  17407. "-T" "tight"
  17408. "-o" pngfile
  17409. dvifile)
  17410. (call-process "dvipng" nil nil nil
  17411. "-fg" fg "-bg" bg
  17412. "-D" dpi
  17413. ;;"-x" scale "-y" scale
  17414. "-T" "tight"
  17415. "-o" pngfile
  17416. dvifile)))
  17417. (if (not (file-exists-p pngfile))
  17418. (if org-format-latex-signal-error
  17419. (error "Failed to create png file from %s" texfile)
  17420. (message "Failed to create png file from %s" texfile)
  17421. nil)
  17422. ;; Use the requested file name and clean up
  17423. (copy-file pngfile tofile 'replace)
  17424. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17425. (if (file-exists-p (concat texfilebase e))
  17426. (delete-file (concat texfilebase e))))
  17427. pngfile))))
  17428. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17429. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17430. "This calls convert, which is included into imagemagick."
  17431. (require 'ox-latex)
  17432. (let* ((tmpdir (if (featurep 'xemacs)
  17433. (temp-directory)
  17434. temporary-file-directory))
  17435. (texfilebase (make-temp-name
  17436. (expand-file-name "orgtex" tmpdir)))
  17437. (texfile (concat texfilebase ".tex"))
  17438. (pdffile (concat texfilebase ".pdf"))
  17439. (pngfile (concat texfilebase ".png"))
  17440. (fnh (if (featurep 'xemacs)
  17441. (font-height (face-font 'default))
  17442. (face-attribute 'default :height nil)))
  17443. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17444. (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
  17445. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17446. "black"))
  17447. (bg (or (plist-get options (if buffer :background :html-background))
  17448. "white")))
  17449. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17450. (setq fg (org-latex-color-format fg)))
  17451. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17452. (setq bg (org-latex-color-format
  17453. (if (string= bg "Transparent") "white" bg))))
  17454. (let ((latex-header (org-create-formula--latex-header)))
  17455. (with-temp-file texfile
  17456. (insert latex-header)
  17457. (insert "\n\\begin{document}\n"
  17458. "\\definecolor{fg}{rgb}{" fg "}\n"
  17459. "\\definecolor{bg}{rgb}{" bg "}\n"
  17460. "\n\\pagecolor{bg}\n"
  17461. "\n{\\color{fg}\n"
  17462. string
  17463. "\n}\n"
  17464. "\n\\end{document}\n")))
  17465. (org-latex-compile texfile t)
  17466. (if (not (file-exists-p pdffile))
  17467. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17468. (ignore-errors
  17469. (if (featurep 'xemacs)
  17470. (call-process "convert" nil nil nil
  17471. "-density" "96"
  17472. "-trim"
  17473. "-antialias"
  17474. pdffile
  17475. "-quality" "100"
  17476. ;; "-sharpen" "0x1.0"
  17477. pngfile)
  17478. (call-process "convert" nil nil nil
  17479. "-density" dpi
  17480. "-trim"
  17481. "-antialias"
  17482. pdffile
  17483. "-quality" "100"
  17484. ;; "-sharpen" "0x1.0"
  17485. pngfile)))
  17486. (if (not (file-exists-p pngfile))
  17487. (if org-format-latex-signal-error
  17488. (error "Failed to create png file from %s" texfile)
  17489. (message "Failed to create png file from %s" texfile)
  17490. nil)
  17491. ;; Use the requested file name and clean up
  17492. (copy-file pngfile tofile 'replace)
  17493. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17494. (if (file-exists-p (concat texfilebase e))
  17495. (delete-file (concat texfilebase e))))
  17496. pngfile))))
  17497. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17498. "Fill a LaTeX header template TPL.
  17499. In the template, the following place holders will be recognized:
  17500. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17501. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17502. [PACKAGES] \\usepackage statements for PKG
  17503. [NO-PACKAGES] do not include PKG
  17504. [EXTRA] the string EXTRA
  17505. [NO-EXTRA] do not include EXTRA
  17506. For backward compatibility, if both the positive and the negative place
  17507. holder is missing, the positive one (without the \"NO-\") will be
  17508. assumed to be present at the end of the template.
  17509. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17510. EXTRA is a string.
  17511. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17512. (let (rpl (end ""))
  17513. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17514. (setq rpl (if (or (match-end 1) (not def-pkg))
  17515. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17516. tpl (replace-match rpl t t tpl))
  17517. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17518. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17519. (setq rpl (if (or (match-end 1) (not pkg))
  17520. "" (org-latex-packages-to-string pkg snippets-p t))
  17521. tpl (replace-match rpl t t tpl))
  17522. (if pkg (setq end
  17523. (concat end "\n"
  17524. (org-latex-packages-to-string pkg snippets-p)))))
  17525. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17526. (setq rpl (if (or (match-end 1) (not extra))
  17527. "" (concat extra "\n"))
  17528. tpl (replace-match rpl t t tpl))
  17529. (if (and extra (string-match "\\S-" extra))
  17530. (setq end (concat end "\n" extra))))
  17531. (if (string-match "\\S-" end)
  17532. (concat tpl "\n" end)
  17533. tpl)))
  17534. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17535. "Turn an alist of packages into a string with the \\usepackage macros."
  17536. (setq pkg (mapconcat (lambda(p)
  17537. (cond
  17538. ((stringp p) p)
  17539. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17540. (format "%% Package %s omitted" (cadr p)))
  17541. ((equal "" (car p))
  17542. (format "\\usepackage{%s}" (cadr p)))
  17543. (t
  17544. (format "\\usepackage[%s]{%s}"
  17545. (car p) (cadr p)))))
  17546. pkg
  17547. "\n"))
  17548. (if newline (concat pkg "\n") pkg))
  17549. (defun org-dvipng-color (attr)
  17550. "Return a RGB color specification for dvipng."
  17551. (apply 'format "rgb %s %s %s"
  17552. (mapcar 'org-normalize-color
  17553. (if (featurep 'xemacs)
  17554. (color-rgb-components
  17555. (face-property 'default
  17556. (cond ((eq attr :foreground) 'foreground)
  17557. ((eq attr :background) 'background))))
  17558. (color-values (face-attribute 'default attr nil))))))
  17559. (defun org-dvipng-color-format (color-name)
  17560. "Convert COLOR-NAME to a RGB color value for dvipng."
  17561. (apply 'format "rgb %s %s %s"
  17562. (mapcar 'org-normalize-color
  17563. (color-values color-name))))
  17564. (defun org-latex-color (attr)
  17565. "Return a RGB color for the LaTeX color package."
  17566. (apply 'format "%s,%s,%s"
  17567. (mapcar 'org-normalize-color
  17568. (if (featurep 'xemacs)
  17569. (color-rgb-components
  17570. (face-property 'default
  17571. (cond ((eq attr :foreground) 'foreground)
  17572. ((eq attr :background) 'background))))
  17573. (color-values (face-attribute 'default attr nil))))))
  17574. (defun org-latex-color-format (color-name)
  17575. "Convert COLOR-NAME to a RGB color value."
  17576. (apply 'format "%s,%s,%s"
  17577. (mapcar 'org-normalize-color
  17578. (color-values color-name))))
  17579. (defun org-normalize-color (value)
  17580. "Return string to be used as color value for an RGB component."
  17581. (format "%g" (/ value 65535.0)))
  17582. ;; Image display
  17583. (defvar org-inline-image-overlays nil)
  17584. (make-variable-buffer-local 'org-inline-image-overlays)
  17585. (defun org-toggle-inline-images (&optional include-linked)
  17586. "Toggle the display of inline images.
  17587. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17588. (interactive "P")
  17589. (if org-inline-image-overlays
  17590. (progn
  17591. (org-remove-inline-images)
  17592. (message "Inline image display turned off"))
  17593. (org-display-inline-images include-linked)
  17594. (if (and (org-called-interactively-p)
  17595. org-inline-image-overlays)
  17596. (message "%d images displayed inline"
  17597. (length org-inline-image-overlays))
  17598. (message "No images to display inline"))))
  17599. (defun org-redisplay-inline-images ()
  17600. "Refresh the display of inline images."
  17601. (interactive)
  17602. (if (not org-inline-image-overlays)
  17603. (org-toggle-inline-images)
  17604. (org-toggle-inline-images)
  17605. (org-toggle-inline-images)))
  17606. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17607. "Display inline images.
  17608. An inline image is a link which follows either of these
  17609. conventions:
  17610. 1. Its path is a file with an extension matching return value
  17611. from `image-file-name-regexp' and it has no contents.
  17612. 2. Its description consists in a single link of the previous
  17613. type.
  17614. When optional argument INCLUDE-LINKED is non-nil, also links with
  17615. a text description part will be inlined. This can be nice for
  17616. a quick look at those images, but it does not reflect what
  17617. exported files will look like.
  17618. When optional argument REFRESH is non-nil, refresh existing
  17619. images between BEG and END. This will create new image displays
  17620. only if necessary. BEG and END default to the buffer
  17621. boundaries."
  17622. (interactive "P")
  17623. (when (display-graphic-p)
  17624. (unless refresh
  17625. (org-remove-inline-images)
  17626. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17627. (org-with-wide-buffer
  17628. (goto-char (or beg (point-min)))
  17629. (let ((case-fold-search t)
  17630. (file-extension-re (org-image-file-name-regexp)))
  17631. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17632. (let ((link (save-match-data (org-element-context))))
  17633. ;; Check if we're at an inline image.
  17634. (when (and (equal (org-element-property :type link) "file")
  17635. (or include-linked
  17636. (not (org-element-property :contents-begin link)))
  17637. (let ((parent (org-element-property :parent link)))
  17638. (or (not (eq (org-element-type parent) 'link))
  17639. (not (cdr (org-element-contents parent)))))
  17640. (org-string-match-p file-extension-re
  17641. (org-element-property :path link)))
  17642. (let ((file (expand-file-name (org-element-property :path link))))
  17643. (when (file-exists-p file)
  17644. (let ((width
  17645. ;; Apply `org-image-actual-width' specifications.
  17646. (cond
  17647. ((not (image-type-available-p 'imagemagick)) nil)
  17648. ((eq org-image-actual-width t) nil)
  17649. ((listp org-image-actual-width)
  17650. (or
  17651. ;; First try to find a width among
  17652. ;; attributes associated to the paragraph
  17653. ;; containing link.
  17654. (let ((paragraph
  17655. (let ((e link))
  17656. (while (and (setq e (org-element-property
  17657. :parent e))
  17658. (not (eq (org-element-type e)
  17659. 'paragraph))))
  17660. e)))
  17661. (when paragraph
  17662. (save-excursion
  17663. (goto-char (org-element-property :begin paragraph))
  17664. (when
  17665. (re-search-forward
  17666. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17667. (org-element-property
  17668. :post-affiliated paragraph)
  17669. t)
  17670. (string-to-number (match-string 1))))))
  17671. ;; Otherwise, fall-back to provided number.
  17672. (car org-image-actual-width)))
  17673. ((numberp org-image-actual-width)
  17674. org-image-actual-width)))
  17675. (old (get-char-property-and-overlay
  17676. (org-element-property :begin link)
  17677. 'org-image-overlay)))
  17678. (if (and (car-safe old) refresh)
  17679. (image-refresh (overlay-get (cdr old) 'display))
  17680. (let ((image (create-image file
  17681. (and width 'imagemagick)
  17682. nil
  17683. :width width)))
  17684. (when image
  17685. (let* ((link
  17686. ;; If inline image is the description
  17687. ;; of another link, be sure to
  17688. ;; consider the latter as the one to
  17689. ;; apply the overlay on.
  17690. (let ((parent
  17691. (org-element-property :parent link)))
  17692. (if (eq (org-element-type parent) 'link)
  17693. parent
  17694. link)))
  17695. (ov (make-overlay
  17696. (org-element-property :begin link)
  17697. (progn
  17698. (goto-char
  17699. (org-element-property :end link))
  17700. (skip-chars-backward " \t")
  17701. (point)))))
  17702. (overlay-put ov 'display image)
  17703. (overlay-put ov 'face 'default)
  17704. (overlay-put ov 'org-image-overlay t)
  17705. (overlay-put
  17706. ov 'modification-hooks
  17707. (list 'org-display-inline-remove-overlay))
  17708. (push ov org-inline-image-overlays)))))))))))))))
  17709. (define-obsolete-function-alias
  17710. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17711. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17712. "Remove inline-display overlay if a corresponding region is modified."
  17713. (let ((inhibit-modification-hooks t))
  17714. (when (and ov after)
  17715. (delete ov org-inline-image-overlays)
  17716. (delete-overlay ov))))
  17717. (defun org-remove-inline-images ()
  17718. "Remove inline display of images."
  17719. (interactive)
  17720. (mapc 'delete-overlay org-inline-image-overlays)
  17721. (setq org-inline-image-overlays nil))
  17722. ;;;; Key bindings
  17723. ;; Outline functions from `outline-mode-prefix-map'
  17724. ;; that can be remapped in Org:
  17725. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17726. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17727. (define-key org-mode-map [remap outline-forward-same-level]
  17728. 'org-forward-heading-same-level)
  17729. (define-key org-mode-map [remap outline-backward-same-level]
  17730. 'org-backward-heading-same-level)
  17731. (define-key org-mode-map [remap show-branches]
  17732. 'org-kill-note-or-show-branches)
  17733. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17734. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17735. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17736. ;; Outline functions from `outline-mode-prefix-map' that can not
  17737. ;; be remapped in Org:
  17738. ;;
  17739. ;; - the column "key binding" shows whether the Outline function is still
  17740. ;; available in Org mode on the same key that it has been bound to in
  17741. ;; Outline mode:
  17742. ;; - "overridden": key used for a different functionality in Org mode
  17743. ;; - else: key still bound to the same Outline function in Org mode
  17744. ;;
  17745. ;; | Outline function | key binding | Org replacement |
  17746. ;; |------------------------------------+-------------+-----------------------|
  17747. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17748. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17749. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17750. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17751. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17752. ;; | `show-entry' | overridden | no replacement |
  17753. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17754. ;; | `show-branches' | `C-c C-k' | still same function |
  17755. ;; | `show-subtree' | overridden | visibility cycling |
  17756. ;; | `show-all' | overridden | no replacement |
  17757. ;; | `hide-subtree' | overridden | visibility cycling |
  17758. ;; | `hide-body' | overridden | no replacement |
  17759. ;; | `hide-entry' | overridden | visibility cycling |
  17760. ;; | `hide-leaves' | overridden | no replacement |
  17761. ;; | `hide-sublevels' | overridden | no replacement |
  17762. ;; | `hide-other' | overridden | no replacement |
  17763. ;; Make `C-c C-x' a prefix key
  17764. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17765. ;; TAB key with modifiers
  17766. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17767. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17768. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17769. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17770. ;; The following line is necessary under Suse GNU/Linux
  17771. (unless (featurep 'xemacs)
  17772. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17773. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17774. (define-key org-mode-map [backtab] 'org-shifttab)
  17775. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17776. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17777. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17778. ;; Cursor keys with modifiers
  17779. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17780. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17781. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17782. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17783. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  17784. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  17785. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17786. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17787. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17788. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17789. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17790. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17791. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17792. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17793. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17794. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17795. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17796. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17797. ;; Babel keys
  17798. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17799. (mapc (lambda (pair)
  17800. (define-key org-babel-map (car pair) (cdr pair)))
  17801. org-babel-key-bindings)
  17802. ;;; Extra keys for tty access.
  17803. ;; We only set them when really needed because otherwise the
  17804. ;; menus don't show the simple keys
  17805. (when (or org-use-extra-keys
  17806. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17807. (not window-system))
  17808. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17809. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17810. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17811. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17812. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17813. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17814. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17815. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17816. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17817. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17818. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17819. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17820. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17821. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17822. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17823. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17824. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17825. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17826. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17827. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17828. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17829. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17830. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17831. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17832. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17833. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17834. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17835. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17836. ;; All the other keys
  17837. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17838. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17839. (if (boundp 'narrow-map)
  17840. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17841. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17842. (if (boundp 'narrow-map)
  17843. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17844. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17845. (if (boundp 'narrow-map)
  17846. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17847. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17848. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17849. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17850. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17851. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17852. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17853. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17854. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17855. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17856. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17857. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17858. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17859. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17860. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17861. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17862. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17863. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17864. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17865. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17866. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17867. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17868. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17869. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17870. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17871. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17872. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17873. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17874. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17875. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17876. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17877. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17878. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17879. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17880. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17881. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17882. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17883. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17884. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  17885. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17886. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17887. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17888. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17889. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17890. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17891. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17892. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17893. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17894. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17895. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17896. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17897. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17898. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17899. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17900. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17901. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17902. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17903. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17904. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17905. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17906. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17907. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17908. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17909. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  17910. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  17911. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  17912. (org-defkey org-mode-map "\C-m" 'org-return)
  17913. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17914. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17915. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17916. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17917. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17918. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17919. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17920. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  17921. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  17922. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17923. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17924. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17925. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17926. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17927. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17928. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17929. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  17930. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17931. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17932. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17933. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17934. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17935. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17936. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17937. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17938. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17939. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17940. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17941. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17942. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17943. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17944. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17945. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17946. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17947. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17948. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17949. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17950. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17951. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  17952. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17953. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17954. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17955. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17956. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17957. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17958. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17959. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17960. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17961. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17962. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17963. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17964. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17965. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17966. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17967. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17968. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17969. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17970. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17971. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17972. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17973. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17974. (when (featurep 'xemacs)
  17975. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17976. (defconst org-speed-commands-default
  17977. '(
  17978. ("Outline Navigation")
  17979. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17980. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17981. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17982. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17983. ("F" . org-next-block)
  17984. ("B" . org-previous-block)
  17985. ("u" . (org-speed-move-safe 'outline-up-heading))
  17986. ("j" . org-goto)
  17987. ("g" . (org-refile t))
  17988. ("Outline Visibility")
  17989. ("c" . org-cycle)
  17990. ("C" . org-shifttab)
  17991. (" " . org-display-outline-path)
  17992. ("s" . org-narrow-to-subtree)
  17993. ("=" . org-columns)
  17994. ("Outline Structure Editing")
  17995. ("U" . org-metaup)
  17996. ("D" . org-metadown)
  17997. ("r" . org-metaright)
  17998. ("l" . org-metaleft)
  17999. ("R" . org-shiftmetaright)
  18000. ("L" . org-shiftmetaleft)
  18001. ("i" . (progn (forward-char 1) (call-interactively
  18002. 'org-insert-heading-respect-content)))
  18003. ("^" . org-sort)
  18004. ("w" . org-refile)
  18005. ("a" . org-archive-subtree-default-with-confirmation)
  18006. ("@" . org-mark-subtree)
  18007. ("#" . org-toggle-comment)
  18008. ("Clock Commands")
  18009. ("I" . org-clock-in)
  18010. ("O" . org-clock-out)
  18011. ("Meta Data Editing")
  18012. ("t" . org-todo)
  18013. ("," . (org-priority))
  18014. ("0" . (org-priority ?\ ))
  18015. ("1" . (org-priority ?A))
  18016. ("2" . (org-priority ?B))
  18017. ("3" . (org-priority ?C))
  18018. (":" . org-set-tags-command)
  18019. ("e" . org-set-effort)
  18020. ("E" . org-inc-effort)
  18021. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18022. (org-entry-put (point) "APPT_WARNTIME" m)))
  18023. ("Agenda Views etc")
  18024. ("v" . org-agenda)
  18025. ("/" . org-sparse-tree)
  18026. ("Misc")
  18027. ("o" . org-open-at-point)
  18028. ("?" . org-speed-command-help)
  18029. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18030. (">" . (org-agenda-remove-restriction-lock))
  18031. )
  18032. "The default speed commands.")
  18033. (defun org-print-speed-command (e)
  18034. (if (> (length (car e)) 1)
  18035. (progn
  18036. (princ "\n")
  18037. (princ (car e))
  18038. (princ "\n")
  18039. (princ (make-string (length (car e)) ?-))
  18040. (princ "\n"))
  18041. (princ (car e))
  18042. (princ " ")
  18043. (if (symbolp (cdr e))
  18044. (princ (symbol-name (cdr e)))
  18045. (prin1 (cdr e)))
  18046. (princ "\n")))
  18047. (defun org-speed-command-help ()
  18048. "Show the available speed commands."
  18049. (interactive)
  18050. (if (not org-use-speed-commands)
  18051. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18052. (with-output-to-temp-buffer "*Help*"
  18053. (princ "User-defined Speed commands\n===========================\n")
  18054. (mapc 'org-print-speed-command org-speed-commands-user)
  18055. (princ "\n")
  18056. (princ "Built-in Speed commands\n=======================\n")
  18057. (mapc 'org-print-speed-command org-speed-commands-default))
  18058. (with-current-buffer "*Help*"
  18059. (setq truncate-lines t))))
  18060. (defun org-speed-move-safe (cmd)
  18061. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18062. If not, return to the original position and throw an error."
  18063. (interactive)
  18064. (let ((pos (point)))
  18065. (call-interactively cmd)
  18066. (unless (and (bolp) (org-at-heading-p))
  18067. (goto-char pos)
  18068. (error "Boundary reached while executing %s" cmd))))
  18069. (defvar org-self-insert-command-undo-counter 0)
  18070. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18071. (defvar org-speed-command nil)
  18072. (define-obsolete-function-alias
  18073. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18074. (defun org-speed-command-activate (keys)
  18075. "Hook for activating single-letter speed commands.
  18076. `org-speed-commands-default' specifies a minimal command set.
  18077. Use `org-speed-commands-user' for further customization."
  18078. (when (or (and (bolp) (looking-at org-outline-regexp))
  18079. (and (functionp org-use-speed-commands)
  18080. (funcall org-use-speed-commands)))
  18081. (cdr (assoc keys (append org-speed-commands-user
  18082. org-speed-commands-default)))))
  18083. (define-obsolete-function-alias
  18084. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18085. (defun org-babel-speed-command-activate (keys)
  18086. "Hook for activating single-letter code block commands."
  18087. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18088. (cdr (assoc keys org-babel-key-bindings))))
  18089. (defcustom org-speed-command-hook
  18090. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18091. "Hook for activating speed commands at strategic locations.
  18092. Hook functions are called in sequence until a valid handler is
  18093. found.
  18094. Each hook takes a single argument, a user-pressed command key
  18095. which is also a `self-insert-command' from the global map.
  18096. Within the hook, examine the cursor position and the command key
  18097. and return nil or a valid handler as appropriate. Handler could
  18098. be one of an interactive command, a function, or a form.
  18099. Set `org-use-speed-commands' to non-nil value to enable this
  18100. hook. The default setting is `org-speed-command-activate'."
  18101. :group 'org-structure
  18102. :version "24.1"
  18103. :type 'hook)
  18104. (defun org-self-insert-command (N)
  18105. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18106. If the cursor is in a table looking at whitespace, the whitespace is
  18107. overwritten, and the table is not marked as requiring realignment."
  18108. (interactive "p")
  18109. (org-check-before-invisible-edit 'insert)
  18110. (cond
  18111. ((and org-use-speed-commands
  18112. (let ((kv (this-command-keys-vector)))
  18113. (setq org-speed-command
  18114. (run-hook-with-args-until-success
  18115. 'org-speed-command-hook
  18116. (make-string 1 (aref kv (1- (length kv))))))))
  18117. (cond
  18118. ((commandp org-speed-command)
  18119. (setq this-command org-speed-command)
  18120. (call-interactively org-speed-command))
  18121. ((functionp org-speed-command)
  18122. (funcall org-speed-command))
  18123. ((and org-speed-command (listp org-speed-command))
  18124. (eval org-speed-command))
  18125. (t (let (org-use-speed-commands)
  18126. (call-interactively 'org-self-insert-command)))))
  18127. ((and
  18128. (org-table-p)
  18129. (progn
  18130. ;; check if we blank the field, and if that triggers align
  18131. (and (featurep 'org-table) org-table-auto-blank-field
  18132. (member last-command
  18133. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas-expand))
  18134. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  18135. ;; got extra space, this field does not determine column width
  18136. (let (org-table-may-need-update) (org-table-blank-field))
  18137. ;; no extra space, this field may determine column width
  18138. (org-table-blank-field)))
  18139. t)
  18140. (eq N 1)
  18141. (looking-at "[^|\n]* |"))
  18142. (let (org-table-may-need-update)
  18143. (goto-char (1- (match-end 0)))
  18144. (backward-delete-char 1)
  18145. (goto-char (match-beginning 0))
  18146. (self-insert-command N)))
  18147. (t
  18148. (setq org-table-may-need-update t)
  18149. (self-insert-command N)
  18150. (org-fix-tags-on-the-fly)
  18151. (if org-self-insert-cluster-for-undo
  18152. (if (not (eq last-command 'org-self-insert-command))
  18153. (setq org-self-insert-command-undo-counter 1)
  18154. (if (>= org-self-insert-command-undo-counter 20)
  18155. (setq org-self-insert-command-undo-counter 1)
  18156. (and (> org-self-insert-command-undo-counter 0)
  18157. buffer-undo-list (listp buffer-undo-list)
  18158. (not (cadr buffer-undo-list)) ; remove nil entry
  18159. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18160. (setq org-self-insert-command-undo-counter
  18161. (1+ org-self-insert-command-undo-counter))))))))
  18162. (defun org-check-before-invisible-edit (kind)
  18163. "Check is editing if kind KIND would be dangerous with invisible text around.
  18164. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18165. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18166. (if (and org-catch-invisible-edits
  18167. (or (not (boundp 'visible-mode)) (not visible-mode))
  18168. (or (get-char-property (point) 'invisible)
  18169. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18170. ;; OK, we need to take a closer look
  18171. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18172. (invisible-before-point (if (bobp) nil (get-char-property
  18173. (1- (point)) 'invisible)))
  18174. (border-and-ok-direction
  18175. (or
  18176. ;; Check if we are acting predictably before invisible text
  18177. (and invisible-at-point (not invisible-before-point)
  18178. (memq kind '(insert delete-backward)))
  18179. ;; Check if we are acting predictably after invisible text
  18180. ;; This works not well, and I have turned it off. It seems
  18181. ;; better to always show and stop after invisible text.
  18182. ;; (and (not invisible-at-point) invisible-before-point
  18183. ;; (memq kind '(insert delete)))
  18184. )))
  18185. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18186. (memq invisible-before-point '(outline org-hide-block t)))
  18187. (if (eq org-catch-invisible-edits 'error)
  18188. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18189. (if (and org-custom-properties-overlays
  18190. (y-or-n-p "Display invisible properties in this buffer? "))
  18191. (org-toggle-custom-properties-visibility)
  18192. ;; Make the area visible
  18193. (save-excursion
  18194. (if invisible-before-point
  18195. (goto-char (previous-single-char-property-change
  18196. (point) 'invisible)))
  18197. (show-subtree))
  18198. (cond
  18199. ((eq org-catch-invisible-edits 'show)
  18200. ;; That's it, we do the edit after showing
  18201. (message
  18202. "Unfolding invisible region around point before editing")
  18203. (sit-for 1))
  18204. ((and (eq org-catch-invisible-edits 'smart)
  18205. border-and-ok-direction)
  18206. (message "Unfolding invisible region around point before editing"))
  18207. (t
  18208. ;; Don't do the edit, make the user repeat it in full visibility
  18209. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18210. (defun org-fix-tags-on-the-fly ()
  18211. (when (and (equal (char-after (point-at-bol)) ?*)
  18212. (org-at-heading-p))
  18213. (org-align-tags-here org-tags-column)))
  18214. (defun org-delete-backward-char (N)
  18215. "Like `delete-backward-char', insert whitespace at field end in tables.
  18216. When deleting backwards, in tables this function will insert whitespace in
  18217. front of the next \"|\" separator, to keep the table aligned. The table will
  18218. still be marked for re-alignment if the field did fill the entire column,
  18219. because, in this case the deletion might narrow the column."
  18220. (interactive "p")
  18221. (save-match-data
  18222. (org-check-before-invisible-edit 'delete-backward)
  18223. (if (and (org-table-p)
  18224. (eq N 1)
  18225. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18226. (looking-at ".*?|"))
  18227. (let ((pos (point))
  18228. (noalign (looking-at "[^|\n\r]* |"))
  18229. (c org-table-may-need-update))
  18230. (backward-delete-char N)
  18231. (if (not overwrite-mode)
  18232. (progn
  18233. (skip-chars-forward "^|")
  18234. (insert " ")
  18235. (goto-char (1- pos))))
  18236. ;; noalign: if there were two spaces at the end, this field
  18237. ;; does not determine the width of the column.
  18238. (if noalign (setq org-table-may-need-update c)))
  18239. (backward-delete-char N)
  18240. (org-fix-tags-on-the-fly))))
  18241. (defun org-delete-char (N)
  18242. "Like `delete-char', but insert whitespace at field end in tables.
  18243. When deleting characters, in tables this function will insert whitespace in
  18244. front of the next \"|\" separator, to keep the table aligned. The table will
  18245. still be marked for re-alignment if the field did fill the entire column,
  18246. because, in this case the deletion might narrow the column."
  18247. (interactive "p")
  18248. (save-match-data
  18249. (org-check-before-invisible-edit 'delete)
  18250. (if (and (org-table-p)
  18251. (not (bolp))
  18252. (not (= (char-after) ?|))
  18253. (eq N 1))
  18254. (if (looking-at ".*?|")
  18255. (let ((pos (point))
  18256. (noalign (looking-at "[^|\n\r]* |"))
  18257. (c org-table-may-need-update))
  18258. (replace-match
  18259. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18260. (goto-char pos)
  18261. ;; noalign: if there were two spaces at the end, this field
  18262. ;; does not determine the width of the column.
  18263. (if noalign (setq org-table-may-need-update c)))
  18264. (delete-char N))
  18265. (delete-char N)
  18266. (org-fix-tags-on-the-fly))))
  18267. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18268. (put 'org-self-insert-command 'delete-selection
  18269. (lambda ()
  18270. (not (run-hook-with-args-until-success
  18271. 'self-insert-uses-region-functions))))
  18272. (put 'orgtbl-self-insert-command 'delete-selection
  18273. (lambda ()
  18274. (not (run-hook-with-args-until-success
  18275. 'self-insert-uses-region-functions))))
  18276. (put 'org-delete-char 'delete-selection 'supersede)
  18277. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18278. (put 'org-yank 'delete-selection 'yank)
  18279. ;; Make `flyspell-mode' delay after some commands
  18280. (put 'org-self-insert-command 'flyspell-delayed t)
  18281. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18282. (put 'org-delete-char 'flyspell-delayed t)
  18283. (put 'org-delete-backward-char 'flyspell-delayed t)
  18284. ;; Make pabbrev-mode expand after org-mode commands
  18285. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18286. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18287. (defun org-remap (map &rest commands)
  18288. "In MAP, remap the functions given in COMMANDS.
  18289. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18290. (let (new old)
  18291. (while commands
  18292. (setq old (pop commands) new (pop commands))
  18293. (if (fboundp 'command-remapping)
  18294. (org-defkey map (vector 'remap old) new)
  18295. (substitute-key-definition old new map global-map)))))
  18296. (defun org-transpose-words ()
  18297. "Transpose words for Org.
  18298. This uses the `org-mode-transpose-word-syntax-table' syntax
  18299. table, which interprets characters in `org-emphasis-alist' as
  18300. word constituents."
  18301. (interactive)
  18302. (with-syntax-table org-mode-transpose-word-syntax-table
  18303. (call-interactively 'transpose-words)))
  18304. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18305. (when (eq org-enable-table-editor 'optimized)
  18306. ;; If the user wants maximum table support, we need to hijack
  18307. ;; some standard editing functions
  18308. (org-remap org-mode-map
  18309. 'self-insert-command 'org-self-insert-command
  18310. 'delete-char 'org-delete-char
  18311. 'delete-backward-char 'org-delete-backward-char)
  18312. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18313. (defvar org-ctrl-c-ctrl-c-hook nil
  18314. "Hook for functions attaching themselves to `C-c C-c'.
  18315. This can be used to add additional functionality to the C-c C-c
  18316. key which executes context-dependent commands. This hook is run
  18317. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18318. run after the last test.
  18319. Each function will be called with no arguments. The function
  18320. must check if the context is appropriate for it to act. If yes,
  18321. it should do its thing and then return a non-nil value. If the
  18322. context is wrong, just do nothing and return nil.")
  18323. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18324. "Hook for functions attaching themselves to `C-c C-c'.
  18325. This can be used to add additional functionality to the C-c C-c
  18326. key which executes context-dependent commands. This hook is run
  18327. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18328. before the first test.
  18329. Each function will be called with no arguments. The function
  18330. must check if the context is appropriate for it to act. If yes,
  18331. it should do its thing and then return a non-nil value. If the
  18332. context is wrong, just do nothing and return nil.")
  18333. (defvar org-tab-first-hook nil
  18334. "Hook for functions to attach themselves to TAB.
  18335. See `org-ctrl-c-ctrl-c-hook' for more information.
  18336. This hook runs as the first action when TAB is pressed, even before
  18337. `org-cycle' messes around with the `outline-regexp' to cater for
  18338. inline tasks and plain list item folding.
  18339. If any function in this hook returns t, any other actions that
  18340. would have been caused by TAB (such as table field motion or visibility
  18341. cycling) will not occur.")
  18342. (defvar org-tab-after-check-for-table-hook nil
  18343. "Hook for functions to attach themselves to TAB.
  18344. See `org-ctrl-c-ctrl-c-hook' for more information.
  18345. This hook runs after it has been established that the cursor is not in a
  18346. table, but before checking if the cursor is in a headline or if global cycling
  18347. should be done.
  18348. If any function in this hook returns t, not other actions like visibility
  18349. cycling will be done.")
  18350. (defvar org-tab-after-check-for-cycling-hook nil
  18351. "Hook for functions to attach themselves to TAB.
  18352. See `org-ctrl-c-ctrl-c-hook' for more information.
  18353. This hook runs after it has been established that not table field motion and
  18354. not visibility should be done because of current context. This is probably
  18355. the place where a package like yasnippets can hook in.")
  18356. (defvar org-tab-before-tab-emulation-hook nil
  18357. "Hook for functions to attach themselves to TAB.
  18358. See `org-ctrl-c-ctrl-c-hook' for more information.
  18359. This hook runs after every other options for TAB have been exhausted, but
  18360. before indentation and \t insertion takes place.")
  18361. (defvar org-metaleft-hook nil
  18362. "Hook for functions attaching themselves to `M-left'.
  18363. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18364. (defvar org-metaright-hook nil
  18365. "Hook for functions attaching themselves to `M-right'.
  18366. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18367. (defvar org-metaup-hook nil
  18368. "Hook for functions attaching themselves to `M-up'.
  18369. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18370. (defvar org-metadown-hook nil
  18371. "Hook for functions attaching themselves to `M-down'.
  18372. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18373. (defvar org-shiftmetaleft-hook nil
  18374. "Hook for functions attaching themselves to `M-S-left'.
  18375. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18376. (defvar org-shiftmetaright-hook nil
  18377. "Hook for functions attaching themselves to `M-S-right'.
  18378. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18379. (defvar org-shiftmetaup-hook nil
  18380. "Hook for functions attaching themselves to `M-S-up'.
  18381. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18382. (defvar org-shiftmetadown-hook nil
  18383. "Hook for functions attaching themselves to `M-S-down'.
  18384. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18385. (defvar org-metareturn-hook nil
  18386. "Hook for functions attaching themselves to `M-RET'.
  18387. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18388. (defvar org-shiftup-hook nil
  18389. "Hook for functions attaching themselves to `S-up'.
  18390. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18391. (defvar org-shiftup-final-hook nil
  18392. "Hook for functions attaching themselves to `S-up'.
  18393. This one runs after all other options except shift-select have been excluded.
  18394. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18395. (defvar org-shiftdown-hook nil
  18396. "Hook for functions attaching themselves to `S-down'.
  18397. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18398. (defvar org-shiftdown-final-hook nil
  18399. "Hook for functions attaching themselves to `S-down'.
  18400. This one runs after all other options except shift-select have been excluded.
  18401. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18402. (defvar org-shiftleft-hook nil
  18403. "Hook for functions attaching themselves to `S-left'.
  18404. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18405. (defvar org-shiftleft-final-hook nil
  18406. "Hook for functions attaching themselves to `S-left'.
  18407. This one runs after all other options except shift-select have been excluded.
  18408. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18409. (defvar org-shiftright-hook nil
  18410. "Hook for functions attaching themselves to `S-right'.
  18411. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18412. (defvar org-shiftright-final-hook nil
  18413. "Hook for functions attaching themselves to `S-right'.
  18414. This one runs after all other options except shift-select have been excluded.
  18415. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18416. (defun org-modifier-cursor-error ()
  18417. "Throw an error, a modified cursor command was applied in wrong context."
  18418. (user-error "This command is active in special context like tables, headlines or items"))
  18419. (defun org-shiftselect-error ()
  18420. "Throw an error because Shift-Cursor command was applied in wrong context."
  18421. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18422. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18423. (user-error "This command works only in special context like headlines or timestamps")))
  18424. (defun org-call-for-shift-select (cmd)
  18425. (let ((this-command-keys-shift-translated t))
  18426. (call-interactively cmd)))
  18427. (defun org-shifttab (&optional arg)
  18428. "Global visibility cycling or move to previous table field.
  18429. Call `org-table-previous-field' within a table.
  18430. When ARG is nil, cycle globally through visibility states.
  18431. When ARG is a numeric prefix, show contents of this level."
  18432. (interactive "P")
  18433. (cond
  18434. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18435. ((integerp arg)
  18436. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18437. (message "Content view to level: %d" arg)
  18438. (org-content (prefix-numeric-value arg2))
  18439. (org-cycle-show-empty-lines t)
  18440. (setq org-cycle-global-status 'overview)))
  18441. (t (call-interactively 'org-global-cycle))))
  18442. (defun org-shiftmetaleft ()
  18443. "Promote subtree or delete table column.
  18444. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18445. `org-table-delete-column', depending on context. See the
  18446. individual commands for more information."
  18447. (interactive)
  18448. (cond
  18449. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18450. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18451. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18452. ((if (not (org-region-active-p)) (org-at-item-p)
  18453. (save-excursion (goto-char (region-beginning))
  18454. (org-at-item-p)))
  18455. (call-interactively 'org-outdent-item-tree))
  18456. (t (org-modifier-cursor-error))))
  18457. (defun org-shiftmetaright ()
  18458. "Demote subtree or insert table column.
  18459. Calls `org-demote-subtree', `org-indent-item-tree', or
  18460. `org-table-insert-column', depending on context. See the
  18461. individual commands for more information."
  18462. (interactive)
  18463. (cond
  18464. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18465. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18466. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18467. ((if (not (org-region-active-p)) (org-at-item-p)
  18468. (save-excursion (goto-char (region-beginning))
  18469. (org-at-item-p)))
  18470. (call-interactively 'org-indent-item-tree))
  18471. (t (org-modifier-cursor-error))))
  18472. (defun org-shiftmetaup (&optional arg)
  18473. "Drag the line at point up.
  18474. In a table, kill the current row.
  18475. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18476. but also adjust the previous clocked item in the clock history.
  18477. Everywhere else, drag the line at point up."
  18478. (interactive "P")
  18479. (cond
  18480. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18481. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18482. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18483. (call-interactively 'org-timestamp-up)))
  18484. (t (call-interactively 'org-drag-line-backward))))
  18485. (defun org-shiftmetadown (&optional arg)
  18486. "Drag the line at point down.
  18487. In a table, insert an empty row at the current line.
  18488. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18489. but also adjust the previous clocked item in the clock history.
  18490. Everywhere else, drag the line at point down."
  18491. (interactive "P")
  18492. (cond
  18493. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18494. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18495. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18496. (call-interactively 'org-timestamp-down)))
  18497. (t (call-interactively 'org-drag-line-forward))))
  18498. (defsubst org-hidden-tree-error ()
  18499. (user-error
  18500. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18501. (defun org-metaleft (&optional arg)
  18502. "Promote heading or move table column to left.
  18503. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18504. With no specific context, calls the Emacs default `backward-word'.
  18505. See the individual commands for more information."
  18506. (interactive "P")
  18507. (cond
  18508. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18509. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18510. ((org-with-limited-levels
  18511. (or (org-at-heading-p)
  18512. (and (org-region-active-p)
  18513. (save-excursion
  18514. (goto-char (region-beginning))
  18515. (org-at-heading-p)))))
  18516. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18517. (call-interactively 'org-do-promote))
  18518. ;; At an inline task.
  18519. ((org-at-heading-p)
  18520. (call-interactively 'org-inlinetask-promote))
  18521. ((or (org-at-item-p)
  18522. (and (org-region-active-p)
  18523. (save-excursion
  18524. (goto-char (region-beginning))
  18525. (org-at-item-p))))
  18526. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18527. (call-interactively 'org-outdent-item))
  18528. (t (call-interactively 'backward-word))))
  18529. (defun org-metaright (&optional arg)
  18530. "Demote a subtree, a list item or move table column to right.
  18531. In front of a drawer or a block keyword, indent it correctly.
  18532. With no specific context, calls the Emacs default `forward-word'.
  18533. See the individual commands for more information."
  18534. (interactive "P")
  18535. (cond
  18536. ((run-hook-with-args-until-success 'org-metaright-hook))
  18537. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18538. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18539. ((org-at-block-p) (call-interactively 'org-indent-block))
  18540. ((org-with-limited-levels
  18541. (or (org-at-heading-p)
  18542. (and (org-region-active-p)
  18543. (save-excursion
  18544. (goto-char (region-beginning))
  18545. (org-at-heading-p)))))
  18546. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18547. (call-interactively 'org-do-demote))
  18548. ;; At an inline task.
  18549. ((org-at-heading-p)
  18550. (call-interactively 'org-inlinetask-demote))
  18551. ((or (org-at-item-p)
  18552. (and (org-region-active-p)
  18553. (save-excursion
  18554. (goto-char (region-beginning))
  18555. (org-at-item-p))))
  18556. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18557. (call-interactively 'org-indent-item))
  18558. (t (call-interactively 'forward-word))))
  18559. (defun org-check-for-hidden (what)
  18560. "Check if there are hidden headlines/items in the current visual line.
  18561. WHAT can be either `headlines' or `items'. If the current line is
  18562. an outline or item heading and it has a folded subtree below it,
  18563. this function returns t, nil otherwise."
  18564. (let ((re (cond
  18565. ((eq what 'headlines) org-outline-regexp-bol)
  18566. ((eq what 'items) (org-item-beginning-re))
  18567. (t (error "This should not happen"))))
  18568. beg end)
  18569. (save-excursion
  18570. (catch 'exit
  18571. (unless (org-region-active-p)
  18572. (setq beg (point-at-bol))
  18573. (beginning-of-line 2)
  18574. (while (and (not (eobp)) ;; this is like `next-line'
  18575. (get-char-property (1- (point)) 'invisible))
  18576. (beginning-of-line 2))
  18577. (setq end (point))
  18578. (goto-char beg)
  18579. (goto-char (point-at-eol))
  18580. (setq end (max end (point)))
  18581. (while (re-search-forward re end t)
  18582. (if (get-char-property (match-beginning 0) 'invisible)
  18583. (throw 'exit t))))
  18584. nil))))
  18585. (defun org-metaup (&optional arg)
  18586. "Move subtree up or move table row up.
  18587. Calls `org-move-subtree-up' or `org-table-move-row' or
  18588. `org-move-item-up', depending on context. See the individual commands
  18589. for more information."
  18590. (interactive "P")
  18591. (cond
  18592. ((run-hook-with-args-until-success 'org-metaup-hook))
  18593. ((org-region-active-p)
  18594. (let* ((a (min (region-beginning) (region-end)))
  18595. (b (1- (max (region-beginning) (region-end))))
  18596. (c (save-excursion (goto-char a)
  18597. (move-beginning-of-line 0)))
  18598. (d (save-excursion (goto-char a)
  18599. (move-end-of-line 0) (point))))
  18600. (transpose-regions a b c d)
  18601. (goto-char c)))
  18602. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18603. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18604. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18605. (t (org-drag-element-backward))))
  18606. (defun org-metadown (&optional arg)
  18607. "Move subtree down or move table row down.
  18608. Calls `org-move-subtree-down' or `org-table-move-row' or
  18609. `org-move-item-down', depending on context. See the individual
  18610. commands for more information."
  18611. (interactive "P")
  18612. (cond
  18613. ((run-hook-with-args-until-success 'org-metadown-hook))
  18614. ((org-region-active-p)
  18615. (let* ((a (min (region-beginning) (region-end)))
  18616. (b (max (region-beginning) (region-end)))
  18617. (c (save-excursion (goto-char b)
  18618. (move-beginning-of-line 1)))
  18619. (d (save-excursion (goto-char b)
  18620. (move-end-of-line 1) (1+ (point)))))
  18621. (transpose-regions a b c d)
  18622. (goto-char d)))
  18623. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18624. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18625. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18626. (t (org-drag-element-forward))))
  18627. (defun org-shiftup (&optional arg)
  18628. "Increase item in timestamp or increase priority of current headline.
  18629. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18630. depending on context. See the individual commands for more information."
  18631. (interactive "P")
  18632. (cond
  18633. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18634. ((and org-support-shift-select (org-region-active-p))
  18635. (org-call-for-shift-select 'previous-line))
  18636. ((org-at-timestamp-p t)
  18637. (call-interactively (if org-edit-timestamp-down-means-later
  18638. 'org-timestamp-down 'org-timestamp-up)))
  18639. ((and (not (eq org-support-shift-select 'always))
  18640. org-enable-priority-commands
  18641. (org-at-heading-p))
  18642. (call-interactively 'org-priority-up))
  18643. ((and (not org-support-shift-select) (org-at-item-p))
  18644. (call-interactively 'org-previous-item))
  18645. ((org-clocktable-try-shift 'up arg))
  18646. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18647. (org-support-shift-select
  18648. (org-call-for-shift-select 'previous-line))
  18649. (t (org-shiftselect-error))))
  18650. (defun org-shiftdown (&optional arg)
  18651. "Decrease item in timestamp or decrease priority of current headline.
  18652. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18653. depending on context. See the individual commands for more information."
  18654. (interactive "P")
  18655. (cond
  18656. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18657. ((and org-support-shift-select (org-region-active-p))
  18658. (org-call-for-shift-select 'next-line))
  18659. ((org-at-timestamp-p t)
  18660. (call-interactively (if org-edit-timestamp-down-means-later
  18661. 'org-timestamp-up 'org-timestamp-down)))
  18662. ((and (not (eq org-support-shift-select 'always))
  18663. org-enable-priority-commands
  18664. (org-at-heading-p))
  18665. (call-interactively 'org-priority-down))
  18666. ((and (not org-support-shift-select) (org-at-item-p))
  18667. (call-interactively 'org-next-item))
  18668. ((org-clocktable-try-shift 'down arg))
  18669. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18670. (org-support-shift-select
  18671. (org-call-for-shift-select 'next-line))
  18672. (t (org-shiftselect-error))))
  18673. (defun org-shiftright (&optional arg)
  18674. "Cycle the thing at point or in the current line, depending on context.
  18675. Depending on context, this does one of the following:
  18676. - switch a timestamp at point one day into the future
  18677. - on a headline, switch to the next TODO keyword.
  18678. - on an item, switch entire list to the next bullet type
  18679. - on a property line, switch to the next allowed value
  18680. - on a clocktable definition line, move time block into the future"
  18681. (interactive "P")
  18682. (cond
  18683. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18684. ((and org-support-shift-select (org-region-active-p))
  18685. (org-call-for-shift-select 'forward-char))
  18686. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18687. ((and (not (eq org-support-shift-select 'always))
  18688. (org-at-heading-p))
  18689. (let ((org-inhibit-logging
  18690. (not org-treat-S-cursor-todo-selection-as-state-change))
  18691. (org-inhibit-blocking
  18692. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18693. (org-call-with-arg 'org-todo 'right)))
  18694. ((or (and org-support-shift-select
  18695. (not (eq org-support-shift-select 'always))
  18696. (org-at-item-bullet-p))
  18697. (and (not org-support-shift-select) (org-at-item-p)))
  18698. (org-call-with-arg 'org-cycle-list-bullet nil))
  18699. ((and (not (eq org-support-shift-select 'always))
  18700. (org-at-property-p))
  18701. (call-interactively 'org-property-next-allowed-value))
  18702. ((org-clocktable-try-shift 'right arg))
  18703. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18704. (org-support-shift-select
  18705. (org-call-for-shift-select 'forward-char))
  18706. (t (org-shiftselect-error))))
  18707. (defun org-shiftleft (&optional arg)
  18708. "Cycle the thing at point or in the current line, depending on context.
  18709. Depending on context, this does one of the following:
  18710. - switch a timestamp at point one day into the past
  18711. - on a headline, switch to the previous TODO keyword.
  18712. - on an item, switch entire list to the previous bullet type
  18713. - on a property line, switch to the previous allowed value
  18714. - on a clocktable definition line, move time block into the past"
  18715. (interactive "P")
  18716. (cond
  18717. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18718. ((and org-support-shift-select (org-region-active-p))
  18719. (org-call-for-shift-select 'backward-char))
  18720. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18721. ((and (not (eq org-support-shift-select 'always))
  18722. (org-at-heading-p))
  18723. (let ((org-inhibit-logging
  18724. (not org-treat-S-cursor-todo-selection-as-state-change))
  18725. (org-inhibit-blocking
  18726. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18727. (org-call-with-arg 'org-todo 'left)))
  18728. ((or (and org-support-shift-select
  18729. (not (eq org-support-shift-select 'always))
  18730. (org-at-item-bullet-p))
  18731. (and (not org-support-shift-select) (org-at-item-p)))
  18732. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18733. ((and (not (eq org-support-shift-select 'always))
  18734. (org-at-property-p))
  18735. (call-interactively 'org-property-previous-allowed-value))
  18736. ((org-clocktable-try-shift 'left arg))
  18737. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18738. (org-support-shift-select
  18739. (org-call-for-shift-select 'backward-char))
  18740. (t (org-shiftselect-error))))
  18741. (defun org-shiftcontrolright ()
  18742. "Switch to next TODO set."
  18743. (interactive)
  18744. (cond
  18745. ((and org-support-shift-select (org-region-active-p))
  18746. (org-call-for-shift-select 'forward-word))
  18747. ((and (not (eq org-support-shift-select 'always))
  18748. (org-at-heading-p))
  18749. (org-call-with-arg 'org-todo 'nextset))
  18750. (org-support-shift-select
  18751. (org-call-for-shift-select 'forward-word))
  18752. (t (org-shiftselect-error))))
  18753. (defun org-shiftcontrolleft ()
  18754. "Switch to previous TODO set."
  18755. (interactive)
  18756. (cond
  18757. ((and org-support-shift-select (org-region-active-p))
  18758. (org-call-for-shift-select 'backward-word))
  18759. ((and (not (eq org-support-shift-select 'always))
  18760. (org-at-heading-p))
  18761. (org-call-with-arg 'org-todo 'previousset))
  18762. (org-support-shift-select
  18763. (org-call-for-shift-select 'backward-word))
  18764. (t (org-shiftselect-error))))
  18765. (defun org-shiftcontrolup (&optional n)
  18766. "Change timestamps synchronously up in CLOCK log lines.
  18767. Optional argument N tells to change by that many units."
  18768. (interactive "P")
  18769. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18770. (let (org-support-shift-select)
  18771. (org-clock-timestamps-up n))
  18772. (user-error "Not at a clock log")))
  18773. (defun org-shiftcontroldown (&optional n)
  18774. "Change timestamps synchronously down in CLOCK log lines.
  18775. Optional argument N tells to change by that many units."
  18776. (interactive "P")
  18777. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18778. (let (org-support-shift-select)
  18779. (org-clock-timestamps-down n))
  18780. (user-error "Not at a clock log")))
  18781. (defun org-increase-number-at-point (&optional inc)
  18782. "Increment the number at point.
  18783. With an optional prefix numeric argument INC, increment using
  18784. this numeric value."
  18785. (interactive "p")
  18786. (if (not (number-at-point))
  18787. (user-error "Not on a number")
  18788. (unless inc (setq inc 1))
  18789. (let ((pos (point))
  18790. (beg (skip-chars-backward "-+^/*0-9eE."))
  18791. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  18792. (setq nap (buffer-substring-no-properties
  18793. (+ pos beg) (+ pos beg end)))
  18794. (delete-region (+ pos beg) (+ pos beg end))
  18795. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  18796. (when (org-at-table-p)
  18797. (org-table-align)
  18798. (org-table-end-of-field 1))))
  18799. (defun org-decrease-number-at-point (&optional inc)
  18800. "Decrement the number at point.
  18801. With an optional prefix numeric argument INC, decrement using
  18802. this numeric value."
  18803. (interactive "p")
  18804. (org-increase-number-at-point (- inc)))
  18805. (defun org-ctrl-c-ret ()
  18806. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18807. (interactive)
  18808. (cond
  18809. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18810. (t (call-interactively 'org-insert-heading))))
  18811. (defun org-find-visible ()
  18812. (let ((s (point)))
  18813. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18814. (get-char-property s 'invisible)))
  18815. s))
  18816. (defun org-find-invisible ()
  18817. (let ((s (point)))
  18818. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18819. (not (get-char-property s 'invisible))))
  18820. s))
  18821. (defun org-copy-visible (beg end)
  18822. "Copy the visible parts of the region."
  18823. (interactive "r")
  18824. (let (snippets s)
  18825. (save-excursion
  18826. (save-restriction
  18827. (narrow-to-region beg end)
  18828. (setq s (goto-char (point-min)))
  18829. (while (not (= (point) (point-max)))
  18830. (goto-char (org-find-invisible))
  18831. (push (buffer-substring s (point)) snippets)
  18832. (setq s (goto-char (org-find-visible))))))
  18833. (kill-new (apply 'concat (nreverse snippets)))))
  18834. (defun org-copy-special ()
  18835. "Copy region in table or copy current subtree.
  18836. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18837. See the individual commands for more information."
  18838. (interactive)
  18839. (call-interactively
  18840. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18841. (defun org-cut-special ()
  18842. "Cut region in table or cut current subtree.
  18843. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18844. See the individual commands for more information."
  18845. (interactive)
  18846. (call-interactively
  18847. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18848. (defun org-paste-special (arg)
  18849. "Paste rectangular region into table, or past subtree relative to level.
  18850. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18851. See the individual commands for more information."
  18852. (interactive "P")
  18853. (if (org-at-table-p)
  18854. (org-table-paste-rectangle)
  18855. (org-paste-subtree arg)))
  18856. (defsubst org-in-fixed-width-region-p ()
  18857. "Is point in a fixed-width region?"
  18858. (save-match-data
  18859. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18860. (defun org-edit-special (&optional arg)
  18861. "Call a special editor for the element at point.
  18862. When at a table, call the formula editor with `org-table-edit-formulas'.
  18863. When in a source code block, call `org-edit-src-code'.
  18864. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18865. When at an #+INCLUDE keyword, visit the included file.
  18866. On a link, call `ffap' to visit the link at point.
  18867. Otherwise, return a user error."
  18868. (interactive "P")
  18869. (let ((element (org-element-at-point)))
  18870. (assert (not buffer-read-only) nil
  18871. "Buffer is read-only: %s" (buffer-name))
  18872. (case (org-element-type element)
  18873. (src-block
  18874. (if (not arg) (org-edit-src-code)
  18875. (let* ((info (org-babel-get-src-block-info))
  18876. (lang (nth 0 info))
  18877. (params (nth 2 info))
  18878. (session (cdr (assq :session params))))
  18879. (if (not session) (org-edit-src-code)
  18880. ;; At a src-block with a session and function called with
  18881. ;; an ARG: switch to the buffer related to the inferior
  18882. ;; process.
  18883. (switch-to-buffer
  18884. (funcall (intern (concat "org-babel-prep-session:" lang))
  18885. session params))))))
  18886. (keyword
  18887. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18888. (org-open-link-from-string
  18889. (format "[[%s]]"
  18890. (expand-file-name
  18891. (let ((value (org-element-property :value element)))
  18892. (cond ((not (org-string-nw-p value))
  18893. (user-error "No file to edit"))
  18894. ((string-match "\\`\"\\(.*?\\)\"" value)
  18895. (match-string 1 value))
  18896. ((string-match "\\`[^ \t\"]\\S-*" value)
  18897. (match-string 0 value))
  18898. (t (user-error "No valid file specified")))))))
  18899. (user-error "No special environment to edit here")))
  18900. (table
  18901. (if (eq (org-element-property :type element) 'table.el)
  18902. (org-edit-src-code)
  18903. (call-interactively 'org-table-edit-formulas)))
  18904. ;; Only Org tables contain `table-row' type elements.
  18905. (table-row (call-interactively 'org-table-edit-formulas))
  18906. ((example-block export-block) (org-edit-src-code))
  18907. (fixed-width (org-edit-fixed-width-region))
  18908. (otherwise
  18909. ;; No notable element at point. Though, we may be at a link,
  18910. ;; which is an object. Thus, scan deeper.
  18911. (if (eq (org-element-type (org-element-context element)) 'link)
  18912. (call-interactively 'ffap)
  18913. (user-error "No special environment to edit here"))))))
  18914. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18915. (defun org-ctrl-c-ctrl-c (&optional arg)
  18916. "Set tags in headline, or update according to changed information at point.
  18917. This command does many different things, depending on context:
  18918. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18919. this is what we do.
  18920. - If the cursor is on a statistics cookie, update it.
  18921. - If the cursor is in a headline, prompt for tags and insert them
  18922. into the current line, aligned to `org-tags-column'. When called
  18923. with prefix arg, realign all tags in the current buffer.
  18924. - If the cursor is in one of the special #+KEYWORD lines, this
  18925. triggers scanning the buffer for these lines and updating the
  18926. information.
  18927. - If the cursor is inside a table, realign the table. This command
  18928. works even if the automatic table editor has been turned off.
  18929. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18930. the entire table.
  18931. - If the cursor is at a footnote reference or definition, jump to
  18932. the corresponding definition or references, respectively.
  18933. - If the cursor is a the beginning of a dynamic block, update it.
  18934. - If the current buffer is a capture buffer, close note and file it.
  18935. - If the cursor is on a <<<target>>>, update radio targets and
  18936. corresponding links in this buffer.
  18937. - If the cursor is on a numbered item in a plain list, renumber the
  18938. ordered list.
  18939. - If the cursor is on a checkbox, toggle it.
  18940. - If the cursor is on a code block, evaluate it. The variable
  18941. `org-confirm-babel-evaluate' can be used to control prompting
  18942. before code block evaluation, by default every code block
  18943. evaluation requires confirmation. Code block evaluation can be
  18944. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18945. (interactive "P")
  18946. (cond
  18947. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18948. org-occur-highlights)
  18949. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18950. (org-remove-occur-highlights)
  18951. (message "Temporary highlights/overlays removed from current buffer"))
  18952. ((and (local-variable-p 'org-finish-function (current-buffer))
  18953. (fboundp org-finish-function))
  18954. (funcall org-finish-function))
  18955. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18956. (t
  18957. (let* ((context (org-element-context)) (type (org-element-type context)))
  18958. ;; Test if point is within a blank line.
  18959. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18960. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18961. (user-error "C-c C-c can do nothing useful at this location"))
  18962. (case type
  18963. ;; When at a link, act according to the parent instead.
  18964. (link (setq context (org-element-property :parent context))
  18965. (setq type (org-element-type context)))
  18966. ;; Unsupported object types: refer to the first supported
  18967. ;; element or object containing it.
  18968. ((bold code entity export-snippet inline-babel-call inline-src-block
  18969. italic latex-fragment line-break macro strike-through subscript
  18970. superscript underline verbatim)
  18971. (while (and (setq context (org-element-property :parent context))
  18972. (not (memq (setq type (org-element-type context))
  18973. '(radio-target paragraph verse-block
  18974. table-cell)))))))
  18975. ;; For convenience: at the first line of a paragraph on the
  18976. ;; same line as an item, apply function on that item instead.
  18977. (when (eq type 'paragraph)
  18978. (let ((parent (org-element-property :parent context)))
  18979. (when (and (eq (org-element-type parent) 'item)
  18980. (= (point-at-bol) (org-element-property :begin parent)))
  18981. (setq context parent type 'item))))
  18982. ;; Act according to type of element or object at point.
  18983. (case type
  18984. (clock (org-clock-update-time-maybe))
  18985. (dynamic-block
  18986. (save-excursion
  18987. (goto-char (org-element-property :post-affiliated context))
  18988. (org-update-dblock)))
  18989. (footnote-definition
  18990. (goto-char (org-element-property :post-affiliated context))
  18991. (call-interactively 'org-footnote-action))
  18992. (footnote-reference (call-interactively 'org-footnote-action))
  18993. ((headline inlinetask)
  18994. (save-excursion (goto-char (org-element-property :begin context))
  18995. (call-interactively 'org-set-tags)))
  18996. (item
  18997. ;; At an item: a double C-u set checkbox to "[-]"
  18998. ;; unconditionally, whereas a single one will toggle its
  18999. ;; presence. Without an universal argument, if the item
  19000. ;; has a checkbox, toggle it. Otherwise repair the list.
  19001. (let* ((box (org-element-property :checkbox context))
  19002. (struct (org-element-property :structure context))
  19003. (old-struct (copy-tree struct))
  19004. (parents (org-list-parents-alist struct))
  19005. (prevs (org-list-prevs-alist struct))
  19006. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19007. (org-list-set-checkbox
  19008. (org-element-property :begin context) struct
  19009. (cond ((equal arg '(16)) "[-]")
  19010. ((and (not box) (equal arg '(4))) "[ ]")
  19011. ((or (not box) (equal arg '(4))) nil)
  19012. ((eq box 'on) "[ ]")
  19013. (t "[X]")))
  19014. ;; Mimic `org-list-write-struct' but with grabbing
  19015. ;; a return value from `org-list-struct-fix-box'.
  19016. (org-list-struct-fix-ind struct parents 2)
  19017. (org-list-struct-fix-item-end struct)
  19018. (org-list-struct-fix-bul struct prevs)
  19019. (org-list-struct-fix-ind struct parents)
  19020. (let ((block-item
  19021. (org-list-struct-fix-box struct parents prevs orderedp)))
  19022. (if (and box (equal struct old-struct))
  19023. (if (equal arg '(16))
  19024. (message "Checkboxes already reset")
  19025. (user-error "Cannot toggle this checkbox: %s"
  19026. (if (eq box 'on)
  19027. "all subitems checked"
  19028. "unchecked subitems")))
  19029. (org-list-struct-apply-struct struct old-struct)
  19030. (org-update-checkbox-count-maybe))
  19031. (when block-item
  19032. (message "Checkboxes were removed due to empty box at line %d"
  19033. (org-current-line block-item))))))
  19034. (keyword
  19035. (let ((org-inhibit-startup-visibility-stuff t)
  19036. (org-startup-align-all-tables nil))
  19037. (when (boundp 'org-table-coordinate-overlays)
  19038. (mapc 'delete-overlay org-table-coordinate-overlays)
  19039. (setq org-table-coordinate-overlays nil))
  19040. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19041. (message "Local setup has been refreshed"))
  19042. (plain-list
  19043. ;; At a plain list, with a double C-u argument, set
  19044. ;; checkboxes of each item to "[-]", whereas a single one
  19045. ;; will toggle their presence according to the state of the
  19046. ;; first item in the list. Without an argument, repair the
  19047. ;; list.
  19048. (let* ((begin (org-element-property :contents-begin context))
  19049. (beginm (move-marker (make-marker) begin))
  19050. (struct (org-element-property :structure context))
  19051. (old-struct (copy-tree struct))
  19052. (first-box (save-excursion
  19053. (goto-char begin)
  19054. (looking-at org-list-full-item-re)
  19055. (match-string-no-properties 3)))
  19056. (new-box (cond ((equal arg '(16)) "[-]")
  19057. ((equal arg '(4)) (unless first-box "[ ]"))
  19058. ((equal first-box "[X]") "[ ]")
  19059. (t "[X]"))))
  19060. (cond
  19061. (arg
  19062. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  19063. (org-list-get-all-items
  19064. begin struct (org-list-prevs-alist struct))))
  19065. ((and first-box (eq (point) begin))
  19066. ;; For convenience, when point is at bol on the first
  19067. ;; item of the list and no argument is provided, simply
  19068. ;; toggle checkbox of that item, if any.
  19069. (org-list-set-checkbox begin struct new-box)))
  19070. (org-list-write-struct
  19071. struct (org-list-parents-alist struct) old-struct)
  19072. (org-update-checkbox-count-maybe)
  19073. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19074. ((property-drawer node-property)
  19075. (call-interactively 'org-property-action))
  19076. ((radio-target target)
  19077. (call-interactively 'org-update-radio-target-regexp))
  19078. (statistics-cookie
  19079. (call-interactively 'org-update-statistics-cookies))
  19080. ((table table-cell table-row)
  19081. ;; At a table, recalculate every field and align it. Also
  19082. ;; send the table if necessary. If the table has
  19083. ;; a `table.el' type, just give up. At a table row or
  19084. ;; cell, maybe recalculate line but always align table.
  19085. (if (eq (org-element-property :type context) 'table.el)
  19086. (message "Use C-c ' to edit table.el tables")
  19087. (let ((org-enable-table-editor t))
  19088. (if (or (eq type 'table)
  19089. ;; Check if point is at a TBLFM line.
  19090. (and (eq type 'table-row)
  19091. (= (point) (org-element-property :end context))))
  19092. (save-excursion
  19093. (if (org-at-TBLFM-p)
  19094. (progn (require 'org-table)
  19095. (org-table-calc-current-TBLFM))
  19096. (goto-char (org-element-property :contents-begin context))
  19097. (org-call-with-arg 'org-table-recalculate (or arg t))
  19098. (orgtbl-send-table 'maybe)))
  19099. (org-table-maybe-eval-formula)
  19100. (cond (arg (call-interactively 'org-table-recalculate))
  19101. ((org-table-maybe-recalculate-line))
  19102. (t (org-table-align)))))))
  19103. (timestamp (org-timestamp-change 0 'day))
  19104. (otherwise
  19105. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19106. (user-error
  19107. "C-c C-c can do nothing useful at this location")))))))))
  19108. (defun org-mode-restart ()
  19109. (interactive)
  19110. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19111. (funcall major-mode)
  19112. (hack-local-variables)
  19113. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19114. (org-indent-mode -1)))
  19115. (message "%s restarted" major-mode))
  19116. (defun org-kill-note-or-show-branches ()
  19117. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19118. (interactive)
  19119. (if (not org-finish-function)
  19120. (progn
  19121. (hide-subtree)
  19122. (call-interactively 'show-branches))
  19123. (let ((org-note-abort t))
  19124. (funcall org-finish-function))))
  19125. (defun org-open-line (n)
  19126. "Insert a new row in tables, call `open-line' elsewhere.
  19127. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19128. (interactive "*p")
  19129. (cond
  19130. ((not org-special-ctrl-o)
  19131. (open-line n))
  19132. ((org-at-table-p)
  19133. (org-table-insert-row))
  19134. (t
  19135. (open-line n))))
  19136. (defun org-return (&optional indent)
  19137. "Goto next table row or insert a newline.
  19138. Calls `org-table-next-row' or `newline', depending on context.
  19139. See the individual commands for more information."
  19140. (interactive)
  19141. (let (org-ts-what)
  19142. (cond
  19143. ((or (bobp) (org-in-src-block-p))
  19144. (if indent (newline-and-indent) (newline)))
  19145. ((org-at-table-p)
  19146. (org-table-justify-field-maybe)
  19147. (call-interactively 'org-table-next-row))
  19148. ;; when `newline-and-indent' is called within a list, make sure
  19149. ;; text moved stays inside the item.
  19150. ((and (org-in-item-p) indent)
  19151. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  19152. (progn
  19153. (save-match-data (newline))
  19154. (org-indent-line-to (length (match-string 0))))
  19155. (let ((ind (org-get-indentation)))
  19156. (newline)
  19157. (if (org-looking-back org-list-end-re)
  19158. (org-indent-line)
  19159. (org-indent-line-to ind)))))
  19160. ((and org-return-follows-link
  19161. (org-at-timestamp-p t)
  19162. (not (eq org-ts-what 'after)))
  19163. (org-follow-timestamp-link))
  19164. ((and org-return-follows-link
  19165. (let ((tprop (get-text-property (point) 'face)))
  19166. (or (eq tprop 'org-link)
  19167. (and (listp tprop) (memq 'org-link tprop)))))
  19168. (call-interactively 'org-open-at-point))
  19169. ((and (org-at-heading-p)
  19170. (looking-at
  19171. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  19172. (org-show-entry)
  19173. (end-of-line 1)
  19174. (newline))
  19175. (t (if indent (newline-and-indent) (newline))))))
  19176. (defun org-return-indent ()
  19177. "Goto next table row or insert a newline and indent.
  19178. Calls `org-table-next-row' or `newline-and-indent', depending on
  19179. context. See the individual commands for more information."
  19180. (interactive)
  19181. (org-return t))
  19182. (defun org-ctrl-c-star ()
  19183. "Compute table, or change heading status of lines.
  19184. Calls `org-table-recalculate' or `org-toggle-heading',
  19185. depending on context."
  19186. (interactive)
  19187. (cond
  19188. ((org-at-table-p)
  19189. (call-interactively 'org-table-recalculate))
  19190. (t
  19191. ;; Convert all lines in region to list items
  19192. (call-interactively 'org-toggle-heading))))
  19193. (defun org-ctrl-c-minus ()
  19194. "Insert separator line in table or modify bullet status of line.
  19195. Also turns a plain line or a region of lines into list items.
  19196. Calls `org-table-insert-hline', `org-toggle-item', or
  19197. `org-cycle-list-bullet', depending on context."
  19198. (interactive)
  19199. (cond
  19200. ((org-at-table-p)
  19201. (call-interactively 'org-table-insert-hline))
  19202. ((org-region-active-p)
  19203. (call-interactively 'org-toggle-item))
  19204. ((org-in-item-p)
  19205. (call-interactively 'org-cycle-list-bullet))
  19206. (t
  19207. (call-interactively 'org-toggle-item))))
  19208. (defun org-toggle-item (arg)
  19209. "Convert headings or normal lines to items, items to normal lines.
  19210. If there is no active region, only the current line is considered.
  19211. If the first non blank line in the region is a headline, convert
  19212. all headlines to items, shifting text accordingly.
  19213. If it is an item, convert all items to normal lines.
  19214. If it is normal text, change region into a list of items.
  19215. With a prefix argument ARG, change the region in a single item."
  19216. (interactive "P")
  19217. (let ((shift-text
  19218. (function
  19219. ;; Shift text in current section to IND, from point to END.
  19220. ;; The function leaves point to END line.
  19221. (lambda (ind end)
  19222. (let ((min-i 1000) (end (copy-marker end)))
  19223. ;; First determine the minimum indentation (MIN-I) of
  19224. ;; the text.
  19225. (save-excursion
  19226. (catch 'exit
  19227. (while (< (point) end)
  19228. (let ((i (org-get-indentation)))
  19229. (cond
  19230. ;; Skip blank lines and inline tasks.
  19231. ((looking-at "^[ \t]*$"))
  19232. ((looking-at org-outline-regexp-bol))
  19233. ;; We can't find less than 0 indentation.
  19234. ((zerop i) (throw 'exit (setq min-i 0)))
  19235. ((< i min-i) (setq min-i i))))
  19236. (forward-line))))
  19237. ;; Then indent each line so that a line indented to
  19238. ;; MIN-I becomes indented to IND. Ignore blank lines
  19239. ;; and inline tasks in the process.
  19240. (let ((delta (- ind min-i)))
  19241. (while (< (point) end)
  19242. (unless (or (looking-at "^[ \t]*$")
  19243. (looking-at org-outline-regexp-bol))
  19244. (org-indent-line-to (+ (org-get-indentation) delta)))
  19245. (forward-line)))))))
  19246. (skip-blanks
  19247. (function
  19248. ;; Return beginning of first non-blank line, starting from
  19249. ;; line at POS.
  19250. (lambda (pos)
  19251. (save-excursion
  19252. (goto-char pos)
  19253. (skip-chars-forward " \r\t\n")
  19254. (point-at-bol)))))
  19255. beg end)
  19256. ;; Determine boundaries of changes.
  19257. (if (org-region-active-p)
  19258. (setq beg (funcall skip-blanks (region-beginning))
  19259. end (copy-marker (region-end)))
  19260. (setq beg (funcall skip-blanks (point-at-bol))
  19261. end (copy-marker (point-at-eol))))
  19262. ;; Depending on the starting line, choose an action on the text
  19263. ;; between BEG and END.
  19264. (org-with-limited-levels
  19265. (save-excursion
  19266. (goto-char beg)
  19267. (cond
  19268. ;; Case 1. Start at an item: de-itemize. Note that it only
  19269. ;; happens when a region is active: `org-ctrl-c-minus'
  19270. ;; would call `org-cycle-list-bullet' otherwise.
  19271. ((org-at-item-p)
  19272. (while (< (point) end)
  19273. (when (org-at-item-p)
  19274. (skip-chars-forward " \t")
  19275. (delete-region (point) (match-end 0)))
  19276. (forward-line)))
  19277. ;; Case 2. Start at an heading: convert to items.
  19278. ((org-at-heading-p)
  19279. (let* ((bul (org-list-bullet-string "-"))
  19280. (bul-len (length bul))
  19281. (done (org-entry-is-done-p))
  19282. (todo (org-entry-is-todo-p))
  19283. ;; Indentation of the first heading. It should be
  19284. ;; relative to the indentation of its parent, if any.
  19285. (start-ind (save-excursion
  19286. (cond
  19287. ((not org-adapt-indentation) 0)
  19288. ((not (outline-previous-heading)) 0)
  19289. (t (length (match-string 0))))))
  19290. ;; Level of first heading. Further headings will be
  19291. ;; compared to it to determine hierarchy in the list.
  19292. (ref-level (org-reduced-level (org-outline-level))))
  19293. (when (or done todo) (org-todo ""))
  19294. (while (< (point) end)
  19295. (let* ((level (org-reduced-level (org-outline-level)))
  19296. (delta (max 0 (- level ref-level))))
  19297. ;; If current headline is less indented than the first
  19298. ;; one, set it as reference, in order to preserve
  19299. ;; subtrees.
  19300. (when (< level ref-level) (setq ref-level level))
  19301. (replace-match bul t t)
  19302. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19303. (when (or done todo)
  19304. (let* ((struct (org-list-struct))
  19305. (old (copy-tree struct)))
  19306. (org-list-set-checkbox (line-beginning-position)
  19307. struct
  19308. (if done "[X]" "[ ]"))
  19309. (org-list-write-struct struct
  19310. (org-list-parents-alist struct)
  19311. old)))
  19312. ;; Ensure all text down to END (or SECTION-END) belongs
  19313. ;; to the newly created item.
  19314. (let ((section-end (save-excursion
  19315. (or (outline-next-heading) (point)))))
  19316. (forward-line)
  19317. (funcall shift-text
  19318. (+ start-ind (* (1+ delta) bul-len))
  19319. (min end section-end)))))))
  19320. ;; Case 3. Normal line with ARG: make the first line of region
  19321. ;; an item, and shift indentation of others lines to
  19322. ;; set them as item's body.
  19323. (arg (let* ((bul (org-list-bullet-string "-"))
  19324. (bul-len (length bul))
  19325. (ref-ind (org-get-indentation)))
  19326. (skip-chars-forward " \t")
  19327. (insert bul)
  19328. (forward-line)
  19329. (while (< (point) end)
  19330. ;; Ensure that lines less indented than first one
  19331. ;; still get included in item body.
  19332. (funcall shift-text
  19333. (+ ref-ind bul-len)
  19334. (min end (save-excursion (or (outline-next-heading)
  19335. (point)))))
  19336. (forward-line))))
  19337. ;; Case 4. Normal line without ARG: turn each non-item line
  19338. ;; into an item.
  19339. (t
  19340. (while (< (point) end)
  19341. (unless (or (org-at-heading-p) (org-at-item-p))
  19342. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19343. (replace-match
  19344. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19345. (forward-line))))))))
  19346. (defun org-toggle-heading (&optional nstars)
  19347. "Convert headings to normal text, or items or text to headings.
  19348. If there is no active region, only convert the current line.
  19349. With a \\[universal-argument] prefix, convert the whole list at
  19350. point into heading.
  19351. In a region:
  19352. - If the first non blank line is a headline, remove the stars
  19353. from all headlines in the region.
  19354. - If it is a normal line, turn each and every normal line (i.e.,
  19355. not an heading or an item) in the region into headings. If you
  19356. want to convert only the first line of this region, use one
  19357. universal prefix argument.
  19358. - If it is a plain list item, turn all plain list items into headings.
  19359. When converting a line into a heading, the number of stars is chosen
  19360. such that the lines become children of the current entry. However,
  19361. when a numeric prefix argument is given, its value determines the
  19362. number of stars to add."
  19363. (interactive "P")
  19364. (let ((skip-blanks
  19365. (function
  19366. ;; Return beginning of first non-blank line, starting from
  19367. ;; line at POS.
  19368. (lambda (pos)
  19369. (save-excursion
  19370. (goto-char pos)
  19371. (while (org-at-comment-p) (forward-line))
  19372. (skip-chars-forward " \r\t\n")
  19373. (point-at-bol)))))
  19374. beg end toggled)
  19375. ;; Determine boundaries of changes. If a universal prefix has
  19376. ;; been given, put the list in a region. If region ends at a bol,
  19377. ;; do not consider the last line to be in the region.
  19378. (when (and current-prefix-arg (org-at-item-p))
  19379. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19380. (org-mark-element))
  19381. (if (org-region-active-p)
  19382. (setq beg (funcall skip-blanks (region-beginning))
  19383. end (copy-marker (save-excursion
  19384. (goto-char (region-end))
  19385. (if (bolp) (point) (point-at-eol)))))
  19386. (setq beg (funcall skip-blanks (point-at-bol))
  19387. end (copy-marker (point-at-eol))))
  19388. ;; Ensure inline tasks don't count as headings.
  19389. (org-with-limited-levels
  19390. (save-excursion
  19391. (goto-char beg)
  19392. (cond
  19393. ;; Case 1. Started at an heading: de-star headings.
  19394. ((org-at-heading-p)
  19395. (while (< (point) end)
  19396. (when (org-at-heading-p t)
  19397. (looking-at org-outline-regexp) (replace-match "")
  19398. (setq toggled t))
  19399. (forward-line)))
  19400. ;; Case 2. Started at an item: change items into headlines.
  19401. ;; One star will be added by `org-list-to-subtree'.
  19402. ((org-at-item-p)
  19403. (let* ((stars (make-string
  19404. ;; subtract the star that will be added again by
  19405. ;; `org-list-to-subtree'
  19406. (if (numberp nstars) (1- nstars)
  19407. (or (org-current-level) 0))
  19408. ?*))
  19409. (add-stars
  19410. (cond (nstars "") ; stars from prefix only
  19411. ((equal stars "") "") ; before first heading
  19412. (org-odd-levels-only "*") ; inside heading, odd
  19413. (t "")))) ; inside heading, oddeven
  19414. (while (< (point) end)
  19415. (when (org-at-item-p)
  19416. ;; Pay attention to cases when region ends before list.
  19417. (let* ((struct (org-list-struct))
  19418. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19419. (save-restriction
  19420. (narrow-to-region (point) list-end)
  19421. (insert
  19422. (org-list-to-subtree
  19423. (org-list-parse-list t)
  19424. '(:istart (concat stars add-stars (funcall get-stars depth))
  19425. :icount (concat stars add-stars (funcall get-stars depth)))))))
  19426. (setq toggled t))
  19427. (forward-line))))
  19428. ;; Case 3. Started at normal text: make every line an heading,
  19429. ;; skipping headlines and items.
  19430. (t (let* ((stars
  19431. (make-string
  19432. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19433. (add-stars
  19434. (cond (nstars "") ; stars from prefix only
  19435. ((equal stars "") "*") ; before first heading
  19436. (org-odd-levels-only "**") ; inside heading, odd
  19437. (t "*"))) ; inside heading, oddeven
  19438. (rpl (concat stars add-stars " "))
  19439. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19440. (while (< (point) (if (equal nstars '(4)) lend end))
  19441. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19442. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19443. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19444. (forward-line)))))))
  19445. (unless toggled (message "Cannot toggle heading from here"))))
  19446. (defun org-meta-return (&optional arg)
  19447. "Insert a new heading or wrap a region in a table.
  19448. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19449. on context. See the individual commands for more information."
  19450. (interactive "P")
  19451. (org-check-before-invisible-edit 'insert)
  19452. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19453. (let* ((element (org-element-at-point))
  19454. (type (org-element-type element)))
  19455. (when (eq type 'table-row)
  19456. (setq element (org-element-property :parent element))
  19457. (setq type 'table))
  19458. (if (and (eq type 'table)
  19459. (eq (org-element-property :type element) 'org)
  19460. (>= (point) (org-element-property :contents-begin element))
  19461. (< (point) (org-element-property :contents-end element)))
  19462. (call-interactively 'org-table-wrap-region)
  19463. (call-interactively 'org-insert-heading)))))
  19464. ;;; Menu entries
  19465. (defsubst org-in-subtree-not-table-p ()
  19466. "Are we in a subtree and not in a table?"
  19467. (and (not (org-before-first-heading-p))
  19468. (not (org-at-table-p))))
  19469. ;; Define the Org-mode menus
  19470. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19471. '("Tbl"
  19472. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19473. ["Next Field" org-cycle (org-at-table-p)]
  19474. ["Previous Field" org-shifttab (org-at-table-p)]
  19475. ["Next Row" org-return (org-at-table-p)]
  19476. "--"
  19477. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19478. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19479. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19480. "--"
  19481. ("Column"
  19482. ["Move Column Left" org-metaleft (org-at-table-p)]
  19483. ["Move Column Right" org-metaright (org-at-table-p)]
  19484. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19485. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19486. ("Row"
  19487. ["Move Row Up" org-metaup (org-at-table-p)]
  19488. ["Move Row Down" org-metadown (org-at-table-p)]
  19489. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19490. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19491. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19492. "--"
  19493. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19494. ("Rectangle"
  19495. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19496. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19497. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19498. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19499. "--"
  19500. ("Calculate"
  19501. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19502. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19503. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19504. "--"
  19505. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19506. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19507. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19508. "--"
  19509. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19510. "--"
  19511. ["Sum Column/Rectangle" org-table-sum
  19512. (or (org-at-table-p) (org-region-active-p))]
  19513. ["Which Column?" org-table-current-column (org-at-table-p)])
  19514. ["Debug Formulas"
  19515. org-table-toggle-formula-debugger
  19516. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19517. ["Show Col/Row Numbers"
  19518. org-table-toggle-coordinate-overlays
  19519. :style toggle
  19520. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19521. "--"
  19522. ["Create" org-table-create (and (not (org-at-table-p))
  19523. org-enable-table-editor)]
  19524. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19525. ["Import from File" org-table-import (not (org-at-table-p))]
  19526. ["Export to File" org-table-export (org-at-table-p)]
  19527. "--"
  19528. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19529. "--"
  19530. ("Plot"
  19531. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19532. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19533. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19534. '("Org"
  19535. ("Show/Hide"
  19536. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19537. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19538. ["Sparse Tree..." org-sparse-tree t]
  19539. ["Reveal Context" org-reveal t]
  19540. ["Show All" show-all t]
  19541. "--"
  19542. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19543. "--"
  19544. ["New Heading" org-insert-heading t]
  19545. ("Navigate Headings"
  19546. ["Up" outline-up-heading t]
  19547. ["Next" outline-next-visible-heading t]
  19548. ["Previous" outline-previous-visible-heading t]
  19549. ["Next Same Level" outline-forward-same-level t]
  19550. ["Previous Same Level" outline-backward-same-level t]
  19551. "--"
  19552. ["Jump" org-goto t])
  19553. ("Edit Structure"
  19554. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19555. "--"
  19556. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19557. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19558. "--"
  19559. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19560. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19561. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19562. "--"
  19563. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19564. "--"
  19565. ["Copy visible text" org-copy-visible t]
  19566. "--"
  19567. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19568. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19569. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19570. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19571. "--"
  19572. ["Sort Region/Children" org-sort t]
  19573. "--"
  19574. ["Convert to odd levels" org-convert-to-odd-levels t]
  19575. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19576. ("Editing"
  19577. ["Emphasis..." org-emphasize t]
  19578. ["Edit Source Example" org-edit-special t]
  19579. "--"
  19580. ["Footnote new/jump" org-footnote-action t]
  19581. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19582. ("Archive"
  19583. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19584. "--"
  19585. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19586. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19587. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19588. )
  19589. "--"
  19590. ("Hyperlinks"
  19591. ["Store Link (Global)" org-store-link t]
  19592. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19593. ["Insert Link" org-insert-link t]
  19594. ["Follow Link" org-open-at-point t]
  19595. "--"
  19596. ["Next link" org-next-link t]
  19597. ["Previous link" org-previous-link t]
  19598. "--"
  19599. ["Descriptive Links"
  19600. org-toggle-link-display
  19601. :style radio
  19602. :selected org-descriptive-links
  19603. ]
  19604. ["Literal Links"
  19605. org-toggle-link-display
  19606. :style radio
  19607. :selected (not org-descriptive-links)])
  19608. "--"
  19609. ("TODO Lists"
  19610. ["TODO/DONE/-" org-todo t]
  19611. ("Select keyword"
  19612. ["Next keyword" org-shiftright (org-at-heading-p)]
  19613. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19614. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19615. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19616. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19617. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19618. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19619. "--"
  19620. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19621. :selected org-enforce-todo-dependencies :style toggle :active t]
  19622. "Settings for tree at point"
  19623. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19624. :selected (org-entry-get nil "ORDERED")
  19625. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19626. ["Do Children parallel" org-toggle-ordered-property :style radio
  19627. :selected (not (org-entry-get nil "ORDERED"))
  19628. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19629. "--"
  19630. ["Set Priority" org-priority t]
  19631. ["Priority Up" org-shiftup t]
  19632. ["Priority Down" org-shiftdown t]
  19633. "--"
  19634. ["Get news from all feeds" org-feed-update-all t]
  19635. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19636. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19637. ("TAGS and Properties"
  19638. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19639. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19640. "--"
  19641. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19642. ["Column view of properties" org-columns t]
  19643. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19644. ("Dates and Scheduling"
  19645. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19646. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19647. ("Change Date"
  19648. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19649. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19650. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19651. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19652. ["Compute Time Range" org-evaluate-time-range t]
  19653. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19654. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19655. "--"
  19656. ["Custom time format" org-toggle-time-stamp-overlays
  19657. :style radio :selected org-display-custom-times]
  19658. "--"
  19659. ["Goto Calendar" org-goto-calendar t]
  19660. ["Date from Calendar" org-date-from-calendar t]
  19661. "--"
  19662. ["Start/Restart Timer" org-timer-start t]
  19663. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19664. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19665. ["Insert Timer String" org-timer t]
  19666. ["Insert Timer Item" org-timer-item t])
  19667. ("Logging work"
  19668. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19669. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19670. ["Clock out" org-clock-out t]
  19671. ["Clock cancel" org-clock-cancel t]
  19672. "--"
  19673. ["Mark as default task" org-clock-mark-default-task t]
  19674. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19675. ["Goto running clock" org-clock-goto t]
  19676. "--"
  19677. ["Display times" org-clock-display t]
  19678. ["Create clock table" org-clock-report t]
  19679. "--"
  19680. ["Record DONE time"
  19681. (progn (setq org-log-done (not org-log-done))
  19682. (message "Switching to %s will %s record a timestamp"
  19683. (car org-done-keywords)
  19684. (if org-log-done "automatically" "not")))
  19685. :style toggle :selected org-log-done])
  19686. "--"
  19687. ["Agenda Command..." org-agenda t]
  19688. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19689. ("File List for Agenda")
  19690. ("Special views current file"
  19691. ["TODO Tree" org-show-todo-tree t]
  19692. ["Check Deadlines" org-check-deadlines t]
  19693. ["Timeline" org-timeline t]
  19694. ["Tags/Property tree" org-match-sparse-tree t])
  19695. "--"
  19696. ["Export/Publish..." org-export-dispatch t]
  19697. ("LaTeX"
  19698. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19699. :selected org-cdlatex-mode]
  19700. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19701. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19702. ["Modify math symbol" org-cdlatex-math-modify
  19703. (org-inside-LaTeX-fragment-p)]
  19704. ["Insert citation" org-reftex-citation t]
  19705. "--"
  19706. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19707. "--"
  19708. ("MobileOrg"
  19709. ["Push Files and Views" org-mobile-push t]
  19710. ["Get Captured and Flagged" org-mobile-pull t]
  19711. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19712. "--"
  19713. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19714. "--"
  19715. ("Documentation"
  19716. ["Show Version" org-version t]
  19717. ["Info Documentation" org-info t])
  19718. ("Customize"
  19719. ["Browse Org Group" org-customize t]
  19720. "--"
  19721. ["Expand This Menu" org-create-customize-menu
  19722. (fboundp 'customize-menu-create)])
  19723. ["Send bug report" org-submit-bug-report t]
  19724. "--"
  19725. ("Refresh/Reload"
  19726. ["Refresh setup current buffer" org-mode-restart t]
  19727. ["Reload Org (after update)" org-reload t]
  19728. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19729. ))
  19730. (defun org-info (&optional node)
  19731. "Read documentation for Org-mode in the info system.
  19732. With optional NODE, go directly to that node."
  19733. (interactive)
  19734. (info (format "(org)%s" (or node ""))))
  19735. ;;;###autoload
  19736. (defun org-submit-bug-report ()
  19737. "Submit a bug report on Org-mode via mail.
  19738. Don't hesitate to report any problems or inaccurate documentation.
  19739. If you don't have setup sending mail from (X)Emacs, please copy the
  19740. output buffer into your mail program, as it gives us important
  19741. information about your Org-mode version and configuration."
  19742. (interactive)
  19743. (require 'reporter)
  19744. (org-load-modules-maybe)
  19745. (org-require-autoloaded-modules)
  19746. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19747. (reporter-submit-bug-report
  19748. "emacs-orgmode@gnu.org"
  19749. (org-version nil 'full)
  19750. (let (list)
  19751. (save-window-excursion
  19752. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19753. (delete-other-windows)
  19754. (erase-buffer)
  19755. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19756. We would like to add your full Org-mode and Outline configuration to the
  19757. bug report. This greatly simplifies the work of the maintainer and
  19758. other experts on the mailing list.
  19759. HOWEVER, some variables you have customized may contain private
  19760. information. The names of customers, colleagues, or friends, might
  19761. appear in the form of file names, tags, todo states, or search strings.
  19762. If you answer yes to the prompt, you might want to check and remove
  19763. such private information before sending the email.")
  19764. (add-text-properties (point-min) (point-max) '(face org-warning))
  19765. (when (yes-or-no-p "Include your Org-mode configuration ")
  19766. (mapatoms
  19767. (lambda (v)
  19768. (and (boundp v)
  19769. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19770. (or (and (symbol-value v)
  19771. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19772. (and
  19773. (get v 'custom-type) (get v 'standard-value)
  19774. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19775. (push v list)))))
  19776. (kill-buffer (get-buffer "*Warn about privacy*"))
  19777. list))
  19778. nil nil
  19779. "Remember to cover the basics, that is, what you expected to happen and
  19780. what in fact did happen. You don't know how to make a good report? See
  19781. http://orgmode.org/manual/Feedback.html#Feedback
  19782. Your bug report will be posted to the Org-mode mailing list.
  19783. ------------------------------------------------------------------------")
  19784. (save-excursion
  19785. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19786. (replace-match "\\1Bug: \\3 [\\2]")))))
  19787. (defun org-install-agenda-files-menu ()
  19788. (let ((bl (buffer-list)))
  19789. (save-excursion
  19790. (while bl
  19791. (set-buffer (pop bl))
  19792. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19793. (when (derived-mode-p 'org-mode)
  19794. (easy-menu-change
  19795. '("Org") "File List for Agenda"
  19796. (append
  19797. (list
  19798. ["Edit File List" (org-edit-agenda-file-list) t]
  19799. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19800. ["Remove Current File from List" org-remove-file t]
  19801. ["Cycle through agenda files" org-cycle-agenda-files t]
  19802. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19803. "--")
  19804. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19805. ;;;; Documentation
  19806. (defun org-require-autoloaded-modules ()
  19807. (interactive)
  19808. (mapc 'require
  19809. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19810. org-table org-timer)))
  19811. ;;;###autoload
  19812. (defun org-reload (&optional uncompiled)
  19813. "Reload all org lisp files.
  19814. With prefix arg UNCOMPILED, load the uncompiled versions."
  19815. (interactive "P")
  19816. (require 'loadhist)
  19817. (let* ((org-dir (org-find-library-dir "org"))
  19818. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19819. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19820. (remove-re (mapconcat 'identity
  19821. (mapcar (lambda (f) (concat "^" f "$"))
  19822. (list (if (featurep 'xemacs)
  19823. "org-colview"
  19824. "org-colview-xemacs")
  19825. "org" "org-loaddefs" "org-version"))
  19826. "\\|"))
  19827. (feats (delete-dups
  19828. (mapcar 'file-name-sans-extension
  19829. (mapcar 'file-name-nondirectory
  19830. (delq nil
  19831. (mapcar 'feature-file
  19832. features))))))
  19833. (lfeat (append
  19834. (sort
  19835. (setq feats
  19836. (delq nil (mapcar
  19837. (lambda (f)
  19838. (if (and (string-match feature-re f)
  19839. (not (string-match remove-re f)))
  19840. f nil))
  19841. feats)))
  19842. 'string-lessp)
  19843. (list "org-version" "org")))
  19844. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19845. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19846. load-uncore load-misses)
  19847. (setq load-misses
  19848. (delq 't
  19849. (mapcar (lambda (f)
  19850. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19851. (and (string= org-dir contrib-dir)
  19852. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19853. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19854. (add-to-list 'load-uncore f 'append)
  19855. 't)
  19856. f))
  19857. lfeat)))
  19858. (if load-uncore
  19859. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19860. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19861. (if load-misses
  19862. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19863. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19864. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19865. ;;;###autoload
  19866. (defun org-customize ()
  19867. "Call the customize function with org as argument."
  19868. (interactive)
  19869. (org-load-modules-maybe)
  19870. (org-require-autoloaded-modules)
  19871. (customize-browse 'org))
  19872. (defun org-create-customize-menu ()
  19873. "Create a full customization menu for Org-mode, insert it into the menu."
  19874. (interactive)
  19875. (org-load-modules-maybe)
  19876. (org-require-autoloaded-modules)
  19877. (if (fboundp 'customize-menu-create)
  19878. (progn
  19879. (easy-menu-change
  19880. '("Org") "Customize"
  19881. `(["Browse Org group" org-customize t]
  19882. "--"
  19883. ,(customize-menu-create 'org)
  19884. ["Set" Custom-set t]
  19885. ["Save" Custom-save t]
  19886. ["Reset to Current" Custom-reset-current t]
  19887. ["Reset to Saved" Custom-reset-saved t]
  19888. ["Reset to Standard Settings" Custom-reset-standard t]))
  19889. (message "\"Org\"-menu now contains full customization menu"))
  19890. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19891. ;;;; Miscellaneous stuff
  19892. ;;; Generally useful functions
  19893. (defsubst org-get-at-eol (property n)
  19894. "Get text property PROPERTY at the end of line less N characters."
  19895. (get-text-property (- (point-at-eol) n) property))
  19896. (defun org-find-text-property-in-string (prop s)
  19897. "Return the first non-nil value of property PROP in string S."
  19898. (or (get-text-property 0 prop s)
  19899. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19900. prop s)))
  19901. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19902. "Display the given MESSAGE as a warning."
  19903. (if (fboundp 'display-warning)
  19904. (display-warning 'org message
  19905. (if (featurep 'xemacs) 'warning :warning))
  19906. (let ((buf (get-buffer-create "*Org warnings*")))
  19907. (with-current-buffer buf
  19908. (goto-char (point-max))
  19909. (insert "Warning (Org): " message)
  19910. (unless (bolp)
  19911. (newline)))
  19912. (display-buffer buf)
  19913. (sit-for 0))))
  19914. (defun org-eval (form)
  19915. "Eval FORM and return result."
  19916. (condition-case error
  19917. (eval form)
  19918. (error (format "%%![Error: %s]" error))))
  19919. (defun org-in-clocktable-p ()
  19920. "Check if the cursor is in a clocktable."
  19921. (let ((pos (point)) start)
  19922. (save-excursion
  19923. (end-of-line 1)
  19924. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19925. (setq start (match-beginning 0))
  19926. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19927. (>= (match-end 0) pos)
  19928. start))))
  19929. (defun org-in-verbatim-emphasis ()
  19930. (save-match-data
  19931. (and (org-in-regexp org-emph-re 2)
  19932. (>= (point) (match-beginning 3))
  19933. (<= (point) (match-end 4))
  19934. (member (match-string 3) '("=" "~")))))
  19935. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19936. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19937. (if (and marker (marker-buffer marker)
  19938. (buffer-live-p (marker-buffer marker)))
  19939. (progn
  19940. (org-pop-to-buffer-same-window (marker-buffer marker))
  19941. (if (or (> marker (point-max)) (< marker (point-min)))
  19942. (widen))
  19943. (goto-char marker)
  19944. (org-show-context 'org-goto))
  19945. (if bookmark
  19946. (bookmark-jump bookmark)
  19947. (error "Cannot find location"))))
  19948. (defun org-quote-csv-field (s)
  19949. "Quote field for inclusion in CSV material."
  19950. (if (string-match "[\",]" s)
  19951. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19952. s))
  19953. (defun org-force-self-insert (N)
  19954. "Needed to enforce self-insert under remapping."
  19955. (interactive "p")
  19956. (self-insert-command N))
  19957. (defun org-string-width (s)
  19958. "Compute width of string, ignoring invisible characters.
  19959. This ignores character with invisibility property `org-link', and also
  19960. characters with property `org-cwidth', because these will become invisible
  19961. upon the next fontification round."
  19962. (let (b l)
  19963. (when (or (eq t buffer-invisibility-spec)
  19964. (assq 'org-link buffer-invisibility-spec))
  19965. (while (setq b (text-property-any 0 (length s)
  19966. 'invisible 'org-link s))
  19967. (setq s (concat (substring s 0 b)
  19968. (substring s (or (next-single-property-change
  19969. b 'invisible s) (length s)))))))
  19970. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19971. (setq s (concat (substring s 0 b)
  19972. (substring s (or (next-single-property-change
  19973. b 'org-cwidth s) (length s))))))
  19974. (setq l (string-width s) b -1)
  19975. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19976. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19977. l))
  19978. (defun org-shorten-string (s maxlength)
  19979. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19980. If the string is shorter or has length MAXLENGTH, just return the
  19981. original string. If it is longer, the functions finds a space in the
  19982. string, breaks this string off at that locations and adds three dots
  19983. as ellipsis. Including the ellipsis, the string will not be longer
  19984. than MAXLENGTH. If finding a good breaking point in the string does
  19985. not work, the string is just chopped off in the middle of a word
  19986. if necessary."
  19987. (if (<= (length s) maxlength)
  19988. s
  19989. (let* ((n (max (- maxlength 4) 1))
  19990. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19991. (if (string-match re s)
  19992. (concat (match-string 1 s) "...")
  19993. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19994. (defun org-get-indentation (&optional line)
  19995. "Get the indentation of the current line, interpreting tabs.
  19996. When LINE is given, assume it represents a line and compute its indentation."
  19997. (if line
  19998. (if (string-match "^ *" (org-remove-tabs line))
  19999. (match-end 0))
  20000. (save-excursion
  20001. (beginning-of-line 1)
  20002. (skip-chars-forward " \t")
  20003. (current-column))))
  20004. (defun org-get-string-indentation (s)
  20005. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20006. (let ((n -1) (i 0) (w tab-width) c)
  20007. (catch 'exit
  20008. (while (< (setq n (1+ n)) (length s))
  20009. (setq c (aref s n))
  20010. (cond ((= c ?\ ) (setq i (1+ i)))
  20011. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20012. (t (throw 'exit t)))))
  20013. i))
  20014. (defun org-remove-tabs (s &optional width)
  20015. "Replace tabulators in S with spaces.
  20016. Assumes that s is a single line, starting in column 0."
  20017. (setq width (or width tab-width))
  20018. (while (string-match "\t" s)
  20019. (setq s (replace-match
  20020. (make-string
  20021. (- (* width (/ (+ (match-beginning 0) width) width))
  20022. (match-beginning 0)) ?\ )
  20023. t t s)))
  20024. s)
  20025. (defun org-fix-indentation (line ind)
  20026. "Fix indentation in LINE.
  20027. IND is a cons cell with target and minimum indentation.
  20028. If the current indentation in LINE is smaller than the minimum,
  20029. leave it alone. If it is larger than ind, set it to the target."
  20030. (let* ((l (org-remove-tabs line))
  20031. (i (org-get-indentation l))
  20032. (i1 (car ind)) (i2 (cdr ind)))
  20033. (if (>= i i2) (setq l (substring line i2)))
  20034. (if (> i1 0)
  20035. (concat (make-string i1 ?\ ) l)
  20036. l)))
  20037. (defun org-remove-indentation (code &optional n)
  20038. "Remove the maximum common indentation from the lines in CODE.
  20039. N may optionally be the number of spaces to remove."
  20040. (with-temp-buffer
  20041. (insert code)
  20042. (org-do-remove-indentation n)
  20043. (buffer-string)))
  20044. (defun org-do-remove-indentation (&optional n)
  20045. "Remove the maximum common indentation from the buffer."
  20046. (untabify (point-min) (point-max))
  20047. (let ((min 10000) re)
  20048. (if n
  20049. (setq min n)
  20050. (goto-char (point-min))
  20051. (while (re-search-forward "^ *[^ \n]" nil t)
  20052. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  20053. (unless (or (= min 0) (= min 10000))
  20054. (setq re (format "^ \\{%d\\}" min))
  20055. (goto-char (point-min))
  20056. (while (re-search-forward re nil t)
  20057. (replace-match "")
  20058. (end-of-line 1))
  20059. min)))
  20060. (defun org-fill-template (template alist)
  20061. "Find each %key of ALIST in TEMPLATE and replace it."
  20062. (let ((case-fold-search nil)
  20063. entry key value)
  20064. (setq alist (sort (copy-sequence alist)
  20065. (lambda (a b) (< (length (car a)) (length (car b))))))
  20066. (while (setq entry (pop alist))
  20067. (setq template
  20068. (replace-regexp-in-string
  20069. (concat "%" (regexp-quote (car entry)))
  20070. (or (cdr entry) "") template t t)))
  20071. template))
  20072. (defun org-base-buffer (buffer)
  20073. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20074. (if (not buffer)
  20075. buffer
  20076. (or (buffer-base-buffer buffer)
  20077. buffer)))
  20078. (defun org-wrap (string &optional width lines)
  20079. "Wrap string to either a number of lines, or a width in characters.
  20080. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20081. that costs. If there is a word longer than WIDTH, the text is actually
  20082. wrapped to the length of that word.
  20083. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20084. many lines, whatever width that takes.
  20085. The return value is a list of lines, without newlines at the end."
  20086. (let* ((words (org-split-string string "[ \t\n]+"))
  20087. (maxword (apply 'max (mapcar 'org-string-width words)))
  20088. w ll)
  20089. (cond (width
  20090. (org-do-wrap words (max maxword width)))
  20091. (lines
  20092. (setq w maxword)
  20093. (setq ll (org-do-wrap words maxword))
  20094. (if (<= (length ll) lines)
  20095. ll
  20096. (setq ll words)
  20097. (while (> (length ll) lines)
  20098. (setq w (1+ w))
  20099. (setq ll (org-do-wrap words w)))
  20100. ll))
  20101. (t (error "Cannot wrap this")))))
  20102. (defun org-do-wrap (words width)
  20103. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20104. (let (lines line)
  20105. (while words
  20106. (setq line (pop words))
  20107. (while (and words (< (+ (length line) (length (car words))) width))
  20108. (setq line (concat line " " (pop words))))
  20109. (setq lines (push line lines)))
  20110. (nreverse lines)))
  20111. (defun org-split-string (string &optional separators)
  20112. "Splits STRING into substrings at SEPARATORS.
  20113. No empty strings are returned if there are matches at the beginning
  20114. and end of string."
  20115. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  20116. (start 0)
  20117. notfirst
  20118. (list nil))
  20119. (while (and (string-match rexp string
  20120. (if (and notfirst
  20121. (= start (match-beginning 0))
  20122. (< start (length string)))
  20123. (1+ start) start))
  20124. (< (match-beginning 0) (length string)))
  20125. (setq notfirst t)
  20126. (or (eq (match-beginning 0) 0)
  20127. (and (eq (match-beginning 0) (match-end 0))
  20128. (eq (match-beginning 0) start))
  20129. (setq list
  20130. (cons (substring string start (match-beginning 0))
  20131. list)))
  20132. (setq start (match-end 0)))
  20133. (or (eq start (length string))
  20134. (setq list
  20135. (cons (substring string start)
  20136. list)))
  20137. (nreverse list)))
  20138. (defun org-quote-vert (s)
  20139. "Replace \"|\" with \"\\vert\"."
  20140. (while (string-match "|" s)
  20141. (setq s (replace-match "\\vert" t t s)))
  20142. s)
  20143. (defun org-uuidgen-p (s)
  20144. "Is S an ID created by UUIDGEN?"
  20145. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20146. (defun org-in-src-block-p (&optional inside)
  20147. "Whether point is in a code source block.
  20148. When INSIDE is non-nil, don't consider we are within a src block
  20149. when point is at #+BEGIN_SRC or #+END_SRC."
  20150. (let ((case-fold-search t) ov)
  20151. (or (and (eq (get-char-property (point) 'src-block) t))
  20152. (and (not inside)
  20153. (save-match-data
  20154. (save-excursion
  20155. (beginning-of-line)
  20156. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20157. (defun org-context ()
  20158. "Return a list of contexts of the current cursor position.
  20159. If several contexts apply, all are returned.
  20160. Each context entry is a list with a symbol naming the context, and
  20161. two positions indicating start and end of the context. Possible
  20162. contexts are:
  20163. :headline anywhere in a headline
  20164. :headline-stars on the leading stars in a headline
  20165. :todo-keyword on a TODO keyword (including DONE) in a headline
  20166. :tags on the TAGS in a headline
  20167. :priority on the priority cookie in a headline
  20168. :item on the first line of a plain list item
  20169. :item-bullet on the bullet/number of a plain list item
  20170. :checkbox on the checkbox in a plain list item
  20171. :table in an org-mode table
  20172. :table-special on a special filed in a table
  20173. :table-table in a table.el table
  20174. :clocktable in a clocktable
  20175. :src-block in a source block
  20176. :link on a hyperlink
  20177. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20178. :target on a <<target>>
  20179. :radio-target on a <<<radio-target>>>
  20180. :latex-fragment on a LaTeX fragment
  20181. :latex-preview on a LaTeX fragment with overlaid preview image
  20182. This function expects the position to be visible because it uses font-lock
  20183. faces as a help to recognize the following contexts: :table-special, :link,
  20184. and :keyword."
  20185. (let* ((f (get-text-property (point) 'face))
  20186. (faces (if (listp f) f (list f)))
  20187. (case-fold-search t)
  20188. (p (point)) clist o)
  20189. ;; First the large context
  20190. (cond
  20191. ((org-at-heading-p t)
  20192. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20193. (when (progn
  20194. (beginning-of-line 1)
  20195. (looking-at org-todo-line-tags-regexp))
  20196. (push (org-point-in-group p 1 :headline-stars) clist)
  20197. (push (org-point-in-group p 2 :todo-keyword) clist)
  20198. (push (org-point-in-group p 4 :tags) clist))
  20199. (goto-char p)
  20200. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20201. (if (looking-at "\\[#[A-Z0-9]\\]")
  20202. (push (org-point-in-group p 0 :priority) clist)))
  20203. ((org-at-item-p)
  20204. (push (org-point-in-group p 2 :item-bullet) clist)
  20205. (push (list :item (point-at-bol)
  20206. (save-excursion (org-end-of-item) (point)))
  20207. clist)
  20208. (and (org-at-item-checkbox-p)
  20209. (push (org-point-in-group p 0 :checkbox) clist)))
  20210. ((org-at-table-p)
  20211. (push (list :table (org-table-begin) (org-table-end)) clist)
  20212. (if (memq 'org-formula faces)
  20213. (push (list :table-special
  20214. (previous-single-property-change p 'face)
  20215. (next-single-property-change p 'face)) clist)))
  20216. ((org-at-table-p 'any)
  20217. (push (list :table-table) clist)))
  20218. (goto-char p)
  20219. (let ((case-fold-search t))
  20220. ;; New the "medium" contexts: clocktables, source blocks
  20221. (cond ((org-in-clocktable-p)
  20222. (push (list :clocktable
  20223. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20224. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20225. (match-beginning 1))
  20226. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20227. (match-end 0))) clist))
  20228. ((org-in-src-block-p)
  20229. (push (list :src-block
  20230. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20231. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20232. (match-beginning 1))
  20233. (and (search-forward "#+END_SRC" nil t)
  20234. (match-beginning 0))) clist))))
  20235. (goto-char p)
  20236. ;; Now the small context
  20237. (cond
  20238. ((org-at-timestamp-p)
  20239. (push (org-point-in-group p 0 :timestamp) clist))
  20240. ((memq 'org-link faces)
  20241. (push (list :link
  20242. (previous-single-property-change p 'face)
  20243. (next-single-property-change p 'face)) clist))
  20244. ((memq 'org-special-keyword faces)
  20245. (push (list :keyword
  20246. (previous-single-property-change p 'face)
  20247. (next-single-property-change p 'face)) clist))
  20248. ((org-at-target-p)
  20249. (push (org-point-in-group p 0 :target) clist)
  20250. (goto-char (1- (match-beginning 0)))
  20251. (if (looking-at org-radio-target-regexp)
  20252. (push (org-point-in-group p 0 :radio-target) clist))
  20253. (goto-char p))
  20254. ((setq o (car (delq nil
  20255. (mapcar
  20256. (lambda (x)
  20257. (if (memq x org-latex-fragment-image-overlays) x))
  20258. (overlays-at (point))))))
  20259. (push (list :latex-fragment
  20260. (overlay-start o) (overlay-end o)) clist)
  20261. (push (list :latex-preview
  20262. (overlay-start o) (overlay-end o)) clist))
  20263. ((org-inside-LaTeX-fragment-p)
  20264. ;; FIXME: positions wrong.
  20265. (push (list :latex-fragment (point) (point)) clist)))
  20266. (setq clist (nreverse (delq nil clist)))
  20267. clist))
  20268. ;; FIXME: Compare with at-regexp-p Do we need both?
  20269. (defun org-in-regexp (re &optional nlines visually)
  20270. "Check if point is inside a match of regexp.
  20271. Normally only the current line is checked, but you can include NLINES extra
  20272. lines both before and after point into the search.
  20273. If VISUALLY is set, require that the cursor is not after the match but
  20274. really on, so that the block visually is on the match."
  20275. (catch 'exit
  20276. (let ((pos (point))
  20277. (eol (point-at-eol (+ 1 (or nlines 0))))
  20278. (inc (if visually 1 0)))
  20279. (save-excursion
  20280. (beginning-of-line (- 1 (or nlines 0)))
  20281. (while (re-search-forward re eol t)
  20282. (if (and (<= (match-beginning 0) pos)
  20283. (>= (+ inc (match-end 0)) pos))
  20284. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20285. (defun org-at-regexp-p (regexp)
  20286. "Is point inside a match of REGEXP in the current line?"
  20287. (catch 'exit
  20288. (save-excursion
  20289. (let ((pos (point)) (end (point-at-eol)))
  20290. (beginning-of-line 1)
  20291. (while (re-search-forward regexp end t)
  20292. (if (and (<= (match-beginning 0) pos)
  20293. (>= (match-end 0) pos))
  20294. (throw 'exit t)))
  20295. nil))))
  20296. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20297. "Non-nil when point is between matches of START-RE and END-RE.
  20298. Also return a non-nil value when point is on one of the matches.
  20299. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20300. buffer positions. Default values are the positions of headlines
  20301. surrounding the point.
  20302. The functions returns a cons cell whose car (resp. cdr) is the
  20303. position before START-RE (resp. after END-RE)."
  20304. (save-match-data
  20305. (let ((pos (point))
  20306. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20307. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20308. beg end)
  20309. (save-excursion
  20310. ;; Point is on a block when on START-RE or if START-RE can be
  20311. ;; found before it...
  20312. (and (or (org-at-regexp-p start-re)
  20313. (re-search-backward start-re limit-up t))
  20314. (setq beg (match-beginning 0))
  20315. ;; ... and END-RE after it...
  20316. (goto-char (match-end 0))
  20317. (re-search-forward end-re limit-down t)
  20318. (> (setq end (match-end 0)) pos)
  20319. ;; ... without another START-RE in-between.
  20320. (goto-char (match-beginning 0))
  20321. (not (re-search-backward start-re (1+ beg) t))
  20322. ;; Return value.
  20323. (cons beg end))))))
  20324. (defun org-in-block-p (names)
  20325. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20326. NAMES is a list of strings containing names of blocks.
  20327. Return first block name matched, or nil. Beware that in case of
  20328. nested blocks, the returned name may not belong to the closest
  20329. block from point."
  20330. (save-match-data
  20331. (catch 'exit
  20332. (let ((case-fold-search t)
  20333. (lim-up (save-excursion (outline-previous-heading)))
  20334. (lim-down (save-excursion (outline-next-heading))))
  20335. (mapc (lambda (name)
  20336. (let ((n (regexp-quote name)))
  20337. (when (org-between-regexps-p
  20338. (concat "^[ \t]*#\\+begin_" n)
  20339. (concat "^[ \t]*#\\+end_" n)
  20340. lim-up lim-down)
  20341. (throw 'exit n))))
  20342. names))
  20343. nil)))
  20344. (defun org-in-drawer-p ()
  20345. "Non-nil if point is within a drawer.
  20346. If point is within a drawer, return it, as parsed data."
  20347. (let ((element (save-match-data (org-element-at-point))))
  20348. (while (and element (not (memq (org-element-type element)
  20349. '(drawer property-drawer))))
  20350. (setq element (org-element-property :parent element)))
  20351. element))
  20352. (defun org-occur-in-agenda-files (regexp &optional nlines)
  20353. "Call `multi-occur' with buffers for all agenda files."
  20354. (interactive "sOrg-files matching: \np")
  20355. (let* ((files (org-agenda-files))
  20356. (tnames (mapcar 'file-truename files))
  20357. (extra org-agenda-text-search-extra-files)
  20358. f)
  20359. (when (eq (car extra) 'agenda-archives)
  20360. (setq extra (cdr extra))
  20361. (setq files (org-add-archive-files files)))
  20362. (while (setq f (pop extra))
  20363. (unless (member (file-truename f) tnames)
  20364. (add-to-list 'files f 'append)
  20365. (add-to-list 'tnames (file-truename f) 'append)))
  20366. (multi-occur
  20367. (mapcar (lambda (x)
  20368. (with-current-buffer
  20369. (or (get-file-buffer x) (find-file-noselect x))
  20370. (widen)
  20371. (current-buffer)))
  20372. files)
  20373. regexp)))
  20374. (if (boundp 'occur-mode-find-occurrence-hook)
  20375. ;; Emacs 23
  20376. (add-hook 'occur-mode-find-occurrence-hook
  20377. (lambda ()
  20378. (when (derived-mode-p 'org-mode)
  20379. (org-reveal))))
  20380. ;; Emacs 22
  20381. (defadvice occur-mode-goto-occurrence
  20382. (after org-occur-reveal activate)
  20383. (and (derived-mode-p 'org-mode) (org-reveal)))
  20384. (defadvice occur-mode-goto-occurrence-other-window
  20385. (after org-occur-reveal activate)
  20386. (and (derived-mode-p 'org-mode) (org-reveal)))
  20387. (defadvice occur-mode-display-occurrence
  20388. (after org-occur-reveal activate)
  20389. (when (derived-mode-p 'org-mode)
  20390. (let ((pos (occur-mode-find-occurrence)))
  20391. (with-current-buffer (marker-buffer pos)
  20392. (save-excursion
  20393. (goto-char pos)
  20394. (org-reveal)))))))
  20395. (defun org-occur-link-in-agenda-files ()
  20396. "Create a link and search for it in the agendas.
  20397. The link is not stored in `org-stored-links', it is just created
  20398. for the search purpose."
  20399. (interactive)
  20400. (let ((link (condition-case nil
  20401. (org-store-link nil)
  20402. (error "Unable to create a link to here"))))
  20403. (org-occur-in-agenda-files (regexp-quote link))))
  20404. (defun org-reverse-string (string)
  20405. "Return the reverse of STRING."
  20406. (apply 'string (reverse (string-to-list string))))
  20407. ;; defsubst org-uniquify must be defined before first use
  20408. (defun org-uniquify-alist (alist)
  20409. "Merge elements of ALIST with the same key.
  20410. For example, in this alist:
  20411. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20412. => '((a 1 3) (b 2))
  20413. merge (a 1) and (a 3) into (a 1 3).
  20414. The function returns the new ALIST."
  20415. (let (rtn)
  20416. (mapc
  20417. (lambda (e)
  20418. (let (n)
  20419. (if (not (assoc (car e) rtn))
  20420. (push e rtn)
  20421. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20422. (setq rtn (assq-delete-all (car e) rtn))
  20423. (push n rtn))))
  20424. alist)
  20425. rtn))
  20426. (defun org-delete-all (elts list)
  20427. "Remove all elements in ELTS from LIST."
  20428. (while elts
  20429. (setq list (delete (pop elts) list)))
  20430. list)
  20431. (defun org-count (cl-item cl-seq)
  20432. "Count the number of occurrences of ITEM in SEQ.
  20433. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20434. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20435. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20436. (while (< cl-start cl-end)
  20437. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20438. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20439. (setq cl-start (1+ cl-start)))
  20440. cl-count))
  20441. (defun org-remove-if (predicate seq)
  20442. "Remove everything from SEQ that fulfills PREDICATE."
  20443. (let (res e)
  20444. (while seq
  20445. (setq e (pop seq))
  20446. (if (not (funcall predicate e)) (push e res)))
  20447. (nreverse res)))
  20448. (defun org-remove-if-not (predicate seq)
  20449. "Remove everything from SEQ that does not fulfill PREDICATE."
  20450. (let (res e)
  20451. (while seq
  20452. (setq e (pop seq))
  20453. (if (funcall predicate e) (push e res)))
  20454. (nreverse res)))
  20455. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20456. "Reduce two-argument FUNCTION across SEQ.
  20457. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20458. \":initial-value\" removed."
  20459. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20460. (cadr (memq :initial-value cl-keys)))
  20461. (cl-seq (pop cl-seq))
  20462. (t (funcall cl-func)))))
  20463. (while cl-seq
  20464. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20465. cl-accum))
  20466. (defun org-every (pred seq)
  20467. "Return true if PREDICATE is true of every element of SEQ.
  20468. Adapted from `every' in cl.el."
  20469. (catch 'org-every
  20470. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20471. t))
  20472. (defun org-some (pred seq)
  20473. "Return true if PREDICATE is true of any element of SEQ.
  20474. Adapted from `some' in cl.el."
  20475. (catch 'org-some
  20476. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20477. nil))
  20478. (defun org-back-over-empty-lines ()
  20479. "Move backwards over whitespace, to the beginning of the first empty line.
  20480. Returns the number of empty lines passed."
  20481. (let ((pos (point)))
  20482. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20483. (skip-chars-backward " \t\n\r")
  20484. (unless (eobp)
  20485. (forward-line -1)))
  20486. (beginning-of-line 2)
  20487. (goto-char (min (point) pos))
  20488. (count-lines (point) pos)))
  20489. (defun org-skip-whitespace ()
  20490. (skip-chars-forward " \t\n\r"))
  20491. (defun org-point-in-group (point group &optional context)
  20492. "Check if POINT is in match-group GROUP.
  20493. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20494. match. If the match group does not exist or point is not inside it,
  20495. return nil."
  20496. (and (match-beginning group)
  20497. (>= point (match-beginning group))
  20498. (<= point (match-end group))
  20499. (if context
  20500. (list context (match-beginning group) (match-end group))
  20501. t)))
  20502. (defun org-switch-to-buffer-other-window (&rest args)
  20503. "Switch to buffer in a second window on the current frame.
  20504. In particular, do not allow pop-up frames.
  20505. Returns the newly created buffer."
  20506. (org-no-popups
  20507. (apply 'switch-to-buffer-other-window args)))
  20508. (defun org-combine-plists (&rest plists)
  20509. "Create a single property list from all plists in PLISTS.
  20510. The process starts by copying the first list, and then setting properties
  20511. from the other lists. Settings in the last list are the most significant
  20512. ones and overrule settings in the other lists."
  20513. (let ((rtn (copy-sequence (pop plists)))
  20514. p v ls)
  20515. (while plists
  20516. (setq ls (pop plists))
  20517. (while ls
  20518. (setq p (pop ls) v (pop ls))
  20519. (setq rtn (plist-put rtn p v))))
  20520. rtn))
  20521. (defun org-replace-escapes (string table)
  20522. "Replace %-escapes in STRING with values in TABLE.
  20523. TABLE is an association list with keys like \"%a\" and string values.
  20524. The sequences in STRING may contain normal field width and padding information,
  20525. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20526. so values can contain further %-escapes if they are define later in TABLE."
  20527. (let ((tbl (copy-alist table))
  20528. (case-fold-search nil)
  20529. (pchg 0)
  20530. e re rpl)
  20531. (while (setq e (pop tbl))
  20532. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20533. (when (and (cdr e) (string-match re (cdr e)))
  20534. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20535. (safe "SREF"))
  20536. (add-text-properties 0 3 (list 'sref sref) safe)
  20537. (setcdr e (replace-match safe t t (cdr e)))))
  20538. (while (string-match re string)
  20539. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20540. (cdr e)))
  20541. (setq string (replace-match rpl t t string))))
  20542. (while (setq pchg (next-property-change pchg string))
  20543. (let ((sref (get-text-property pchg 'sref string)))
  20544. (when (and sref (string-match "SREF" string pchg))
  20545. (setq string (replace-match sref t t string)))))
  20546. string))
  20547. (defun org-sublist (list start end)
  20548. "Return a section of LIST, from START to END.
  20549. Counting starts at 1."
  20550. (let (rtn (c start))
  20551. (setq list (nthcdr (1- start) list))
  20552. (while (and list (<= c end))
  20553. (push (pop list) rtn)
  20554. (setq c (1+ c)))
  20555. (nreverse rtn)))
  20556. (defun org-find-base-buffer-visiting (file)
  20557. "Like `find-buffer-visiting' but always return the base buffer and
  20558. not an indirect buffer."
  20559. (let ((buf (or (get-file-buffer file)
  20560. (find-buffer-visiting file))))
  20561. (if buf
  20562. (or (buffer-base-buffer buf) buf)
  20563. nil)))
  20564. (defun org-image-file-name-regexp (&optional extensions)
  20565. "Return regexp matching the file names of images.
  20566. If EXTENSIONS is given, only match these."
  20567. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20568. (image-file-name-regexp)
  20569. (let ((image-file-name-extensions
  20570. (or extensions
  20571. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20572. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20573. (concat "\\."
  20574. (regexp-opt (nconc (mapcar 'upcase
  20575. image-file-name-extensions)
  20576. image-file-name-extensions)
  20577. t)
  20578. "\\'"))))
  20579. (defun org-file-image-p (file &optional extensions)
  20580. "Return non-nil if FILE is an image."
  20581. (save-match-data
  20582. (string-match (org-image-file-name-regexp extensions) file)))
  20583. (defun org-get-cursor-date (&optional with-time)
  20584. "Return the date at cursor in as a time.
  20585. This works in the calendar and in the agenda, anywhere else it just
  20586. returns the current time.
  20587. If WITH-TIME is non-nil, returns the time of the event at point (in
  20588. the agenda) or the current time of the day."
  20589. (let (date day defd tp tm hod mod)
  20590. (when with-time
  20591. (setq tp (get-text-property (point) 'time))
  20592. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20593. (setq hod (string-to-number (match-string 1 tp))
  20594. mod (string-to-number (match-string 2 tp))))
  20595. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20596. mod (nth 1 (decode-time (current-time))))))
  20597. (cond
  20598. ((eq major-mode 'calendar-mode)
  20599. (setq date (calendar-cursor-to-date)
  20600. defd (encode-time 0 (or mod 0) (or hod 0)
  20601. (nth 1 date) (nth 0 date) (nth 2 date))))
  20602. ((eq major-mode 'org-agenda-mode)
  20603. (setq day (get-text-property (point) 'day))
  20604. (if day
  20605. (setq date (calendar-gregorian-from-absolute day)
  20606. defd (encode-time 0 (or mod 0) (or hod 0)
  20607. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20608. (or defd (current-time))))
  20609. (defun org-mark-subtree (&optional up)
  20610. "Mark the current subtree.
  20611. This puts point at the start of the current subtree, and mark at
  20612. the end. If a numeric prefix UP is given, move up into the
  20613. hierarchy of headlines by UP levels before marking the subtree."
  20614. (interactive "P")
  20615. (org-with-limited-levels
  20616. (cond ((org-at-heading-p) (beginning-of-line))
  20617. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20618. (t (outline-previous-visible-heading 1))))
  20619. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20620. (if (org-called-interactively-p 'any)
  20621. (call-interactively 'org-mark-element)
  20622. (org-mark-element)))
  20623. ;;; Indentation
  20624. (defun org--get-expected-indentation (element contentsp)
  20625. "Expected indentation column for current line, according to ELEMENT.
  20626. ELEMENT is an element containing point. CONTENTSP is non-nil
  20627. when indentation is to be computed according to contents of
  20628. ELEMENT."
  20629. (let ((type (org-element-type element))
  20630. (start (org-element-property :begin element)))
  20631. (org-with-wide-buffer
  20632. (cond
  20633. (contentsp
  20634. (case type
  20635. (footnote-definition 0)
  20636. ((headline inlinetask nil)
  20637. (if (not org-adapt-indentation) 0
  20638. (let ((level (org-current-level)))
  20639. (if level (1+ level) 0))))
  20640. (item
  20641. (org-list-item-body-column
  20642. (org-element-property :post-affiliated element)))
  20643. (plain-list
  20644. (save-excursion
  20645. (goto-char (org-element-property :post-affiliated element))
  20646. (org-get-indentation)))
  20647. (otherwise
  20648. (goto-char start)
  20649. (org-get-indentation))))
  20650. ((memq type '(headline inlinetask nil))
  20651. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  20652. (org--get-expected-indentation element t)
  20653. 0))
  20654. ((eq type 'footnote-definition) 0)
  20655. ;; First paragraph of a footnote definition or an item.
  20656. ;; Indent like parent.
  20657. ((< (line-beginning-position) start)
  20658. (org--get-expected-indentation
  20659. (org-element-property :parent element) t))
  20660. ;; At first line: indent according to previous sibling, if any,
  20661. ;; ignoring footnote definitions and inline tasks, or parent's
  20662. ;; contents.
  20663. ((= (line-beginning-position) start)
  20664. (catch 'exit
  20665. (while t
  20666. (if (= (point-min) start) (throw 'exit 0)
  20667. (goto-char (1- start))
  20668. (let* ((previous (org-element-at-point))
  20669. (parent previous))
  20670. (while (and parent (<= (org-element-property :end parent) start))
  20671. (setq previous parent
  20672. parent (org-element-property :parent parent)))
  20673. (cond
  20674. ((not previous) (throw 'exit 0))
  20675. ((> (org-element-property :end previous) start)
  20676. (throw 'exit (org--get-expected-indentation previous t)))
  20677. ((memq (org-element-type previous)
  20678. '(footnote-definition inlinetask))
  20679. (setq start (org-element-property :begin previous)))
  20680. (t (goto-char (org-element-property :begin previous))
  20681. (throw 'exit
  20682. (if (bolp) (org-get-indentation)
  20683. ;; At first paragraph in an item or
  20684. ;; a footnote definition.
  20685. (org--get-expected-indentation
  20686. (org-element-property :parent previous) t))))))))))
  20687. ;; Otherwise, move to the first non-blank line above.
  20688. (t
  20689. (beginning-of-line)
  20690. (let ((pos (point)))
  20691. (skip-chars-backward " \r\t\n")
  20692. (cond
  20693. ;; Two blank lines end a footnote definition or a plain
  20694. ;; list. When we indent an empty line after them, the
  20695. ;; containing list or footnote definition is over, so it
  20696. ;; qualifies as a previous sibling. Therefore, we indent
  20697. ;; like its first line.
  20698. ((and (memq type '(footnote-definition plain-list))
  20699. (> (count-lines (point) pos) 2))
  20700. (goto-char start)
  20701. (org-get-indentation))
  20702. ;; Line above is the first one of a paragraph at the
  20703. ;; beginning of an item or a footnote definition. Indent
  20704. ;; like parent.
  20705. ((< (line-beginning-position) start)
  20706. (org--get-expected-indentation
  20707. (org-element-property :parent element) t))
  20708. ;; POS is after contents in a greater element. Indent like
  20709. ;; the beginning of the element.
  20710. ;;
  20711. ;; As a special case, if point is at the end of a footnote
  20712. ;; definition or an item, indent like the very last element
  20713. ;; within.
  20714. ((let ((cend (org-element-property :contents-end element)))
  20715. (and cend (<= cend pos)))
  20716. (if (memq type '(footnote-definition item plain-list))
  20717. (org--get-expected-indentation (org-element-at-point) nil)
  20718. (goto-char start)
  20719. (org-get-indentation)))
  20720. ;; In any other case, indent like the current line.
  20721. (t (org-get-indentation)))))))))
  20722. (defun org--align-node-property ()
  20723. "Align node property at point.
  20724. Alignment is done according to `org-property-format', which see."
  20725. (when (save-excursion
  20726. (beginning-of-line)
  20727. (looking-at org-property-re))
  20728. (replace-match
  20729. (concat (match-string 4)
  20730. (org-trim
  20731. (format org-property-format (match-string 1) (match-string 3))))
  20732. t t)))
  20733. (defun org-indent-line ()
  20734. "Indent line depending on context.
  20735. Indentation is done according to the following rules:
  20736. - Footnote definitions, headlines and inline tasks have to
  20737. start at column 0.
  20738. - On the very first line of an element, consider, in order, the
  20739. next rules until one matches:
  20740. 1. If there's a sibling element before, ignoring footnote
  20741. definitions and inline tasks, indent like its first line.
  20742. 2. If element has a parent, indent like its contents. More
  20743. precisely, if parent is an item, indent after the
  20744. description part, if any, or the bullet (see
  20745. ``org-list-description-max-indent'). Else, indent like
  20746. parent's first line.
  20747. 3. Otherwise, indent relatively to current level, if
  20748. `org-adapt-indentation' is non-nil, or to left margin.
  20749. - On a blank line at the end of a plain list, an item, or
  20750. a footnote definition, indent like the very last element
  20751. within.
  20752. - In the code part of a source block, use language major mode
  20753. to indent current line if `org-src-tab-acts-natively' is
  20754. non-nil. If it is nil, do nothing.
  20755. - Otherwise, indent like the first non-blank line above.
  20756. The function doesn't indent an item as it could break the whole
  20757. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  20758. \\[org-shiftmetaright].
  20759. Also align node properties according to `org-property-format'."
  20760. (interactive)
  20761. (cond
  20762. (orgstruct-is-++
  20763. (let ((indent-line-function
  20764. (cadadr (assq 'indent-line-function org-fb-vars))))
  20765. (indent-according-to-mode)))
  20766. ((org-at-heading-p) 'noindent)
  20767. (t
  20768. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  20769. (type (org-element-type element)))
  20770. (cond ((and (memq type '(plain-list item))
  20771. (= (line-beginning-position)
  20772. (org-element-property :post-affiliated element)))
  20773. 'noindent)
  20774. ((and (eq type 'src-block)
  20775. org-src-tab-acts-natively
  20776. (> (line-beginning-position)
  20777. (org-element-property :post-affiliated element))
  20778. (< (line-beginning-position)
  20779. (org-with-wide-buffer
  20780. (goto-char (org-element-property :end element))
  20781. (skip-chars-backward " \r\t\n")
  20782. (line-beginning-position))))
  20783. (let ((org-src-strip-leading-and-trailing-blank-lines nil))
  20784. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB"))))
  20785. (t
  20786. (let ((column (org--get-expected-indentation element nil)))
  20787. ;; Preserve current column.
  20788. (if (<= (current-column) (current-indentation))
  20789. (org-indent-line-to column)
  20790. (save-excursion (org-indent-line-to column))))
  20791. ;; Align node property. Also preserve current column.
  20792. (when (eq type 'node-property)
  20793. (let ((column (current-column)))
  20794. (org--align-node-property)
  20795. (org-move-to-column column)))))))))
  20796. (defun org-indent-region (start end)
  20797. "Indent each non-blank line in the region.
  20798. Called from a program, START and END specify the region to
  20799. indent. The function will not indent contents of example blocks,
  20800. verse blocks and export blocks as leading white spaces are
  20801. assumed to be significant there."
  20802. (interactive "r")
  20803. (save-excursion
  20804. (goto-char start)
  20805. (skip-chars-forward " \r\t\n")
  20806. (unless (eobp) (beginning-of-line))
  20807. (let ((indent-to
  20808. (lambda (ind pos)
  20809. ;; Set IND as indentation for all lines between point and
  20810. ;; POS or END, whichever comes first. Blank lines are
  20811. ;; ignored. Leave point after POS once done.
  20812. (let ((limit (copy-marker (min end pos))))
  20813. (while (< (point) limit)
  20814. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  20815. (forward-line))
  20816. (set-marker limit nil))))
  20817. (end (copy-marker end)))
  20818. (while (< (point) end)
  20819. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  20820. (let* ((element (org-element-at-point))
  20821. (type (org-element-type element))
  20822. (element-end (copy-marker (org-element-property :end element)))
  20823. (ind (org--get-expected-indentation element nil)))
  20824. (cond
  20825. ((or (memq type '(paragraph table table-row))
  20826. (not (or (org-element-property :contents-begin element)
  20827. (memq type
  20828. '(example-block export-block src-block)))))
  20829. ;; Elements here are indented as a single block. Also
  20830. ;; align node properties.
  20831. (when (eq type 'node-property)
  20832. (org--align-node-property)
  20833. (beginning-of-line))
  20834. (funcall indent-to ind element-end))
  20835. (t
  20836. ;; Elements in this category consist of three parts:
  20837. ;; before the contents, the contents, and after the
  20838. ;; contents. The contents are treated specially,
  20839. ;; according to the element type, or not indented at
  20840. ;; all. Other parts are indented as a single block.
  20841. (let* ((post (copy-marker
  20842. (org-element-property :post-affiliated element)))
  20843. (cbeg
  20844. (copy-marker
  20845. (cond
  20846. ((not (org-element-property :contents-begin element))
  20847. ;; Fake contents for source blocks.
  20848. (org-with-wide-buffer
  20849. (goto-char post)
  20850. (forward-line)
  20851. (point)))
  20852. ((memq type '(footnote-definition item plain-list))
  20853. ;; Contents in these elements could start on
  20854. ;; the same line as the beginning of the
  20855. ;; element. Make sure we start indenting
  20856. ;; from the second line.
  20857. (org-with-wide-buffer
  20858. (goto-char post)
  20859. (end-of-line)
  20860. (skip-chars-forward " \r\t\n")
  20861. (if (eobp) (point) (line-beginning-position))))
  20862. (t (org-element-property :contents-begin element)))))
  20863. (cend (copy-marker
  20864. (or (org-element-property :contents-end element)
  20865. ;; Fake contents for source blocks.
  20866. (org-with-wide-buffer
  20867. (goto-char element-end)
  20868. (skip-chars-backward " \r\t\n")
  20869. (line-beginning-position))))))
  20870. ;; Do not change items indentation individually as it
  20871. ;; might break the list as a whole. On the other
  20872. ;; hand, when at a plain list, indent it as a whole.
  20873. (cond ((eq type 'plain-list)
  20874. (let ((offset (- ind (org-get-indentation))))
  20875. (unless (zerop offset)
  20876. (indent-rigidly (org-element-property :begin element)
  20877. (org-element-property :end element)
  20878. offset))
  20879. (goto-char cbeg)))
  20880. ((eq type 'item) (goto-char cbeg))
  20881. (t (funcall indent-to ind cbeg)))
  20882. (when (< (point) end)
  20883. (case type
  20884. ((example-block export-block verse-block))
  20885. (src-block
  20886. ;; In a source block, indent source code
  20887. ;; according to language major mode, but only if
  20888. ;; `org-src-tab-acts-natively' is non-nil.
  20889. (when (and (< (point) end) org-src-tab-acts-natively)
  20890. (ignore-errors
  20891. (let (org-src-strip-leading-and-trailing-blank-lines
  20892. ;; Region boundaries in edit buffer.
  20893. (start (1+ (- (point) cbeg)))
  20894. (end (- (min cend end) cbeg)))
  20895. (org-babel-do-in-edit-buffer
  20896. (indent-region start end))))))
  20897. (t (org-indent-region (point) (min cend end))))
  20898. (goto-char (min cend end))
  20899. (when (< (point) end) (funcall indent-to ind element-end)))
  20900. (set-marker post nil)
  20901. (set-marker cbeg nil)
  20902. (set-marker cend nil))))
  20903. (set-marker element-end nil))))
  20904. (set-marker end nil))))
  20905. (defun org-indent-drawer ()
  20906. "Indent the drawer at point."
  20907. (interactive)
  20908. (unless (save-excursion
  20909. (beginning-of-line)
  20910. (org-looking-at-p org-drawer-regexp))
  20911. (user-error "Not at a drawer"))
  20912. (let ((element (org-element-at-point)))
  20913. (unless (memq (org-element-type element) '(drawer property-drawer))
  20914. (user-error "Not at a drawer"))
  20915. (org-with-wide-buffer
  20916. (org-indent-region (org-element-property :begin element)
  20917. (org-element-property :end element))))
  20918. (message "Drawer at point indented"))
  20919. (defun org-indent-block ()
  20920. "Indent the block at point."
  20921. (interactive)
  20922. (unless (save-excursion
  20923. (beginning-of-line)
  20924. (let ((case-fold-search t))
  20925. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  20926. (user-error "Not at a block"))
  20927. (let ((element (org-element-at-point)))
  20928. (unless (memq (org-element-type element)
  20929. '(comment-block center-block dynamic-block example-block
  20930. export-block quote-block special-block
  20931. src-block verse-block))
  20932. (user-error "Not at a block"))
  20933. (org-with-wide-buffer
  20934. (org-indent-region (org-element-property :begin element)
  20935. (org-element-property :end element))))
  20936. (message "Block at point indented"))
  20937. ;;; Filling
  20938. ;; We use our own fill-paragraph and auto-fill functions.
  20939. ;; `org-fill-paragraph' relies on adaptive filling and context
  20940. ;; checking. Appropriate `fill-prefix' is computed with
  20941. ;; `org-adaptive-fill-function'.
  20942. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20943. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20944. ;; `org-adaptive-fill-function' value. Internally,
  20945. ;; `org-comment-line-break-function' breaks the line.
  20946. ;; `org-setup-filling' installs filling and auto-filling related
  20947. ;; variables during `org-mode' initialization.
  20948. (defvar org-element-paragraph-separate) ; org-element.el
  20949. (defun org-setup-filling ()
  20950. (require 'org-element)
  20951. ;; Prevent auto-fill from inserting unwanted new items.
  20952. (when (boundp 'fill-nobreak-predicate)
  20953. (org-set-local
  20954. 'fill-nobreak-predicate
  20955. (org-uniquify
  20956. (append fill-nobreak-predicate
  20957. '(org-fill-line-break-nobreak-p
  20958. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20959. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  20960. (org-set-local 'paragraph-start paragraph-ending)
  20961. (org-set-local 'paragraph-separate paragraph-ending))
  20962. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20963. (org-set-local 'auto-fill-inhibit-regexp nil)
  20964. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20965. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20966. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20967. (defun org-fill-line-break-nobreak-p ()
  20968. "Non-nil when a new line at point would create an Org line break."
  20969. (save-excursion
  20970. (skip-chars-backward "[ \t]")
  20971. (skip-chars-backward "\\\\")
  20972. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20973. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20974. "Non-nil when a new line at point would split a timestamp."
  20975. (and (org-at-timestamp-p t)
  20976. (not (looking-at org-ts-regexp-both))))
  20977. (declare-function message-in-body-p "message" ())
  20978. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20979. (defun org-adaptive-fill-function ()
  20980. "Compute a fill prefix for the current line.
  20981. Return fill prefix, as a string, or nil if current line isn't
  20982. meant to be filled. For convenience, if `adaptive-fill-regexp'
  20983. matches in paragraphs or comments, use it."
  20984. (catch 'exit
  20985. (when (derived-mode-p 'message-mode)
  20986. (save-excursion
  20987. (beginning-of-line)
  20988. (cond ((or (not (message-in-body-p))
  20989. (looking-at orgtbl-line-start-regexp))
  20990. (throw 'exit nil))
  20991. ((looking-at message-cite-prefix-regexp)
  20992. (throw 'exit (match-string-no-properties 0)))
  20993. ((looking-at org-outline-regexp)
  20994. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  20995. (org-with-wide-buffer
  20996. (unless (org-at-heading-p)
  20997. (let* ((p (line-beginning-position))
  20998. (element (save-excursion
  20999. (beginning-of-line)
  21000. (org-element-at-point)))
  21001. (type (org-element-type element))
  21002. (post-affiliated (org-element-property :post-affiliated element)))
  21003. (unless (< p post-affiliated)
  21004. (case type
  21005. (comment
  21006. (save-excursion
  21007. (beginning-of-line)
  21008. (looking-at "[ \t]*")
  21009. (concat (match-string 0) "# ")))
  21010. (footnote-definition "")
  21011. ((item plain-list)
  21012. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21013. (paragraph
  21014. ;; Fill prefix is usually the same as the current line,
  21015. ;; unless the paragraph is at the beginning of an item.
  21016. (let ((parent (org-element-property :parent element)))
  21017. (save-excursion
  21018. (beginning-of-line)
  21019. (cond ((eq (org-element-type parent) 'item)
  21020. (make-string (org-list-item-body-column
  21021. (org-element-property :begin parent))
  21022. ?\s))
  21023. ((and adaptive-fill-regexp
  21024. ;; Locally disable
  21025. ;; `adaptive-fill-function' to let
  21026. ;; `fill-context-prefix' handle
  21027. ;; `adaptive-fill-regexp' variable.
  21028. (let (adaptive-fill-function)
  21029. (fill-context-prefix
  21030. post-affiliated
  21031. (org-element-property :end element)))))
  21032. ((looking-at "[ \t]+") (match-string 0))
  21033. (t "")))))
  21034. (comment-block
  21035. ;; Only fill contents if P is within block boundaries.
  21036. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21037. (forward-line)
  21038. (point)))
  21039. (cend (save-excursion
  21040. (goto-char (org-element-property :end element))
  21041. (skip-chars-backward " \r\t\n")
  21042. (line-beginning-position))))
  21043. (when (and (>= p cbeg) (< p cend))
  21044. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21045. (match-string 0)
  21046. "")))))))))))
  21047. (declare-function message-goto-body "message" ())
  21048. (defvar message-cite-prefix-regexp) ; From message.el
  21049. (defun org-fill-paragraph (&optional justify)
  21050. "Fill element at point, when applicable.
  21051. This function only applies to comment blocks, comments, example
  21052. blocks and paragraphs. Also, as a special case, re-align table
  21053. when point is at one.
  21054. If JUSTIFY is non-nil (interactively, with prefix argument),
  21055. justify as well. If `sentence-end-double-space' is non-nil, then
  21056. period followed by one space does not end a sentence, so don't
  21057. break a line there. The variable `fill-column' controls the
  21058. width for filling.
  21059. For convenience, when point is at a plain list, an item or
  21060. a footnote definition, try to fill the first paragraph within."
  21061. (interactive)
  21062. (if (and (derived-mode-p 'message-mode)
  21063. (or (not (message-in-body-p))
  21064. (save-excursion (move-beginning-of-line 1)
  21065. (looking-at message-cite-prefix-regexp))))
  21066. ;; First ensure filling is correct in message-mode.
  21067. (let ((fill-paragraph-function
  21068. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  21069. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  21070. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  21071. (paragraph-separate
  21072. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  21073. (fill-paragraph nil))
  21074. (with-syntax-table org-mode-transpose-word-syntax-table
  21075. ;; Move to end of line in order to get the first paragraph
  21076. ;; within a plain list or a footnote definition.
  21077. (let ((element (save-excursion
  21078. (end-of-line)
  21079. (or (ignore-errors (org-element-at-point))
  21080. (user-error "An element cannot be parsed line %d"
  21081. (line-number-at-pos (point)))))))
  21082. ;; First check if point is in a blank line at the beginning of
  21083. ;; the buffer. In that case, ignore filling.
  21084. (case (org-element-type element)
  21085. ;; Use major mode filling function is src blocks.
  21086. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21087. ;; Align Org tables, leave table.el tables as-is.
  21088. (table-row (org-table-align) t)
  21089. (table
  21090. (when (eq (org-element-property :type element) 'org)
  21091. (save-excursion
  21092. (goto-char (org-element-property :post-affiliated element))
  21093. (org-table-align)))
  21094. t)
  21095. (paragraph
  21096. ;; Paragraphs may contain `line-break' type objects.
  21097. (let ((beg (max (point-min)
  21098. (org-element-property :contents-begin element)))
  21099. (end (min (point-max)
  21100. (org-element-property :contents-end element))))
  21101. ;; Do nothing if point is at an affiliated keyword.
  21102. (if (< (line-end-position) beg) t
  21103. (when (derived-mode-p 'message-mode)
  21104. ;; In `message-mode', do not fill following citation
  21105. ;; in current paragraph nor text before message body.
  21106. (let ((body-start (save-excursion (message-goto-body))))
  21107. (when body-start (setq beg (max body-start beg))))
  21108. (when (save-excursion
  21109. (re-search-forward
  21110. (concat "^" message-cite-prefix-regexp) end t))
  21111. (setq end (match-beginning 0))))
  21112. ;; Fill paragraph, taking line breaks into account.
  21113. (save-excursion
  21114. (goto-char beg)
  21115. (let ((cuts (list beg)))
  21116. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21117. (when (eq 'line-break
  21118. (org-element-type
  21119. (save-excursion (backward-char)
  21120. (org-element-context))))
  21121. (push (point) cuts)))
  21122. (dolist (c (delq end cuts))
  21123. (fill-region-as-paragraph c end justify)
  21124. (setq end c))))
  21125. t)))
  21126. ;; Contents of `comment-block' type elements should be
  21127. ;; filled as plain text, but only if point is within block
  21128. ;; markers.
  21129. (comment-block
  21130. (let* ((case-fold-search t)
  21131. (beg (save-excursion
  21132. (goto-char (org-element-property :begin element))
  21133. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21134. (forward-line)
  21135. (point)))
  21136. (end (save-excursion
  21137. (goto-char (org-element-property :end element))
  21138. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21139. (line-beginning-position))))
  21140. (if (or (< (point) beg) (> (point) end)) t
  21141. (fill-region-as-paragraph
  21142. (save-excursion (end-of-line)
  21143. (re-search-backward "^[ \t]*$" beg 'move)
  21144. (line-beginning-position))
  21145. (save-excursion (beginning-of-line)
  21146. (re-search-forward "^[ \t]*$" end 'move)
  21147. (line-beginning-position))
  21148. justify))))
  21149. ;; Fill comments.
  21150. (comment
  21151. (let ((begin (org-element-property :post-affiliated element))
  21152. (end (org-element-property :end element)))
  21153. (when (and (>= (point) begin) (<= (point) end))
  21154. (let ((begin (save-excursion
  21155. (end-of-line)
  21156. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21157. (progn (forward-line) (point))
  21158. begin)))
  21159. (end (save-excursion
  21160. (end-of-line)
  21161. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21162. (1- (line-beginning-position))
  21163. (skip-chars-backward " \r\t\n")
  21164. (line-end-position)))))
  21165. ;; Do not fill comments when at a blank line.
  21166. (when (> end begin)
  21167. (let ((fill-prefix
  21168. (save-excursion
  21169. (beginning-of-line)
  21170. (looking-at "[ \t]*#")
  21171. (let ((comment-prefix (match-string 0)))
  21172. (goto-char (match-end 0))
  21173. (if (looking-at adaptive-fill-regexp)
  21174. (concat comment-prefix (match-string 0))
  21175. (concat comment-prefix " "))))))
  21176. (save-excursion
  21177. (fill-region-as-paragraph begin end justify))))))
  21178. t))
  21179. ;; Ignore every other element.
  21180. (otherwise t))))))
  21181. (defun org-auto-fill-function ()
  21182. "Auto-fill function."
  21183. ;; Check if auto-filling is meaningful.
  21184. (let ((fc (current-fill-column)))
  21185. (when (and fc (> (current-column) fc))
  21186. (let* ((fill-prefix (org-adaptive-fill-function))
  21187. ;; Enforce empty fill prefix, if required. Otherwise, it
  21188. ;; will be computed again.
  21189. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21190. (when fill-prefix (do-auto-fill))))))
  21191. (defun org-comment-line-break-function (&optional soft)
  21192. "Break line at point and indent, continuing comment if within one.
  21193. The inserted newline is marked hard if variable
  21194. `use-hard-newlines' is true, unless optional argument SOFT is
  21195. non-nil."
  21196. (if soft (insert-and-inherit ?\n) (newline 1))
  21197. (save-excursion (forward-char -1) (delete-horizontal-space))
  21198. (delete-horizontal-space)
  21199. (indent-to-left-margin)
  21200. (insert-before-markers-and-inherit fill-prefix))
  21201. ;;; Fixed Width Areas
  21202. (defun org-toggle-fixed-width ()
  21203. "Toggle fixed-width markup.
  21204. Add or remove fixed-width markup on current line, whenever it
  21205. makes sense. Return an error otherwise.
  21206. If a region is active and if it contains only fixed-width areas
  21207. or blank lines, remove all fixed-width markup in it. If the
  21208. region contains anything else, convert all non-fixed-width lines
  21209. to fixed-width ones.
  21210. Blank lines at the end of the region are ignored unless the
  21211. region only contains such lines."
  21212. (interactive)
  21213. (if (not (org-region-active-p))
  21214. ;; No region:
  21215. ;;
  21216. ;; Remove fixed width marker only in a fixed-with element.
  21217. ;;
  21218. ;; Add fixed width maker in paragraphs, in blank lines after
  21219. ;; elements or at the beginning of a headline or an inlinetask,
  21220. ;; and before any one-line elements (e.g., a clock).
  21221. (progn
  21222. (beginning-of-line)
  21223. (let* ((element (org-element-at-point))
  21224. (type (org-element-type element)))
  21225. (cond
  21226. ((and (eq type 'fixed-width)
  21227. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21228. (replace-match
  21229. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21230. ((and (memq type '(babel-call clock comment diary-sexp headline
  21231. horizontal-rule keyword paragraph
  21232. planning))
  21233. (<= (org-element-property :post-affiliated element) (point)))
  21234. (skip-chars-forward " \t")
  21235. (insert ": "))
  21236. ((and (org-looking-at-p "[ \t]*$")
  21237. (or (eq type 'inlinetask)
  21238. (save-excursion
  21239. (skip-chars-forward " \r\t\n")
  21240. (<= (org-element-property :end element) (point)))))
  21241. (delete-region (point) (line-end-position))
  21242. (org-indent-line)
  21243. (insert ": "))
  21244. (t (user-error "Cannot insert a fixed-width line here")))))
  21245. ;; Region active.
  21246. (let* ((begin (save-excursion
  21247. (goto-char (region-beginning))
  21248. (line-beginning-position)))
  21249. (end (copy-marker
  21250. (save-excursion
  21251. (goto-char (region-end))
  21252. (unless (eolp) (beginning-of-line))
  21253. (if (save-excursion (re-search-backward "\\S-" begin t))
  21254. (progn (skip-chars-backward " \r\t\n") (point))
  21255. (point)))))
  21256. (all-fixed-width-p
  21257. (catch 'not-all-p
  21258. (save-excursion
  21259. (goto-char begin)
  21260. (skip-chars-forward " \r\t\n")
  21261. (when (eobp) (throw 'not-all-p nil))
  21262. (while (< (point) end)
  21263. (let ((element (org-element-at-point)))
  21264. (if (eq (org-element-type element) 'fixed-width)
  21265. (goto-char (org-element-property :end element))
  21266. (throw 'not-all-p nil))))
  21267. t))))
  21268. (if all-fixed-width-p
  21269. (save-excursion
  21270. (goto-char begin)
  21271. (while (< (point) end)
  21272. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21273. (replace-match
  21274. "" nil nil nil
  21275. (if (= (line-end-position) (match-end 0)) 0 1)))
  21276. (forward-line)))
  21277. (let ((min-ind (point-max)))
  21278. ;; Find minimum indentation across all lines.
  21279. (save-excursion
  21280. (goto-char begin)
  21281. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21282. (setq min-ind 0)
  21283. (catch 'zerop
  21284. (while (< (point) end)
  21285. (unless (org-looking-at-p "[ \t]*$")
  21286. (let ((ind (org-get-indentation)))
  21287. (setq min-ind (min min-ind ind))
  21288. (when (zerop ind) (throw 'zerop t))))
  21289. (forward-line)))))
  21290. ;; Loop over all lines and add fixed-width markup everywhere
  21291. ;; but in fixed-width lines.
  21292. (save-excursion
  21293. (goto-char begin)
  21294. (while (< (point) end)
  21295. (cond
  21296. ((org-at-heading-p)
  21297. (insert ": ")
  21298. (forward-line)
  21299. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21300. (insert ":")
  21301. (forward-line)))
  21302. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21303. (let* ((element (org-element-at-point))
  21304. (element-end (org-element-property :end element)))
  21305. (if (eq (org-element-type element) 'fixed-width)
  21306. (progn (goto-char element-end)
  21307. (skip-chars-backward " \r\t\n")
  21308. (forward-line))
  21309. (let ((limit (min end element-end)))
  21310. (while (< (point) limit)
  21311. (org-move-to-column min-ind t)
  21312. (insert ": ")
  21313. (forward-line))))))
  21314. (t
  21315. (org-move-to-column min-ind t)
  21316. (insert ": ")
  21317. (forward-line)))))))
  21318. (set-marker end nil))))
  21319. ;;; Comments
  21320. ;; Org comments syntax is quite complex. It requires the entire line
  21321. ;; to be just a comment. Also, even with the right syntax at the
  21322. ;; beginning of line, some some elements (i.e. verse-block or
  21323. ;; example-block) don't accept comments. Usual Emacs comment commands
  21324. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21325. ;; Org still relies on `comment-dwim', but cannot trust
  21326. ;; `comment-only-p'. So, `comment-region-function' and
  21327. ;; `uncomment-region-function' both point
  21328. ;; to`org-comment-or-uncomment-region'. Eventually,
  21329. ;; `org-insert-comment' takes care of insertion of comments at the
  21330. ;; beginning of line.
  21331. ;; `org-setup-comments-handling' install comments related variables
  21332. ;; during `org-mode' initialization.
  21333. (defun org-setup-comments-handling ()
  21334. (interactive)
  21335. (org-set-local 'comment-use-syntax nil)
  21336. (org-set-local 'comment-start "# ")
  21337. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21338. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  21339. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  21340. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  21341. (defun org-insert-comment ()
  21342. "Insert an empty comment above current line.
  21343. If the line is empty, insert comment at its beginning. When
  21344. point is within a source block, comment according to the related
  21345. major mode."
  21346. (if (let ((element (org-element-at-point)))
  21347. (and (eq (org-element-type element) 'src-block)
  21348. (< (save-excursion
  21349. (goto-char (org-element-property :post-affiliated element))
  21350. (line-end-position))
  21351. (point))
  21352. (> (save-excursion
  21353. (goto-char (org-element-property :end element))
  21354. (skip-chars-backward " \r\t\n")
  21355. (line-beginning-position))
  21356. (point))))
  21357. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21358. (beginning-of-line)
  21359. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21360. (open-line 1))
  21361. (org-indent-line)
  21362. (insert "# ")))
  21363. (defvar comment-empty-lines) ; From newcomment.el.
  21364. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  21365. "Comment or uncomment each non-blank line in the region.
  21366. Uncomment each non-blank line between BEG and END if it only
  21367. contains commented lines. Otherwise, comment them. If region is
  21368. strictly within a source block, use appropriate comment syntax."
  21369. (if (let ((element (org-element-at-point)))
  21370. (and (eq (org-element-type element) 'src-block)
  21371. (< (save-excursion
  21372. (goto-char (org-element-property :post-affiliated element))
  21373. (line-end-position))
  21374. beg)
  21375. (>= (save-excursion
  21376. (goto-char (org-element-property :end element))
  21377. (skip-chars-backward " \r\t\n")
  21378. (line-beginning-position))
  21379. end)))
  21380. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21381. (save-restriction
  21382. ;; Restrict region
  21383. (narrow-to-region (save-excursion (goto-char beg)
  21384. (skip-chars-forward " \r\t\n" end)
  21385. (line-beginning-position))
  21386. (save-excursion (goto-char end)
  21387. (skip-chars-backward " \r\t\n" beg)
  21388. (line-end-position)))
  21389. (let ((uncommentp
  21390. ;; UNCOMMENTP is non-nil when every non blank line between
  21391. ;; BEG and END is a comment.
  21392. (save-excursion
  21393. (goto-char (point-min))
  21394. (while (and (not (eobp))
  21395. (let ((element (org-element-at-point)))
  21396. (and (eq (org-element-type element) 'comment)
  21397. (goto-char (min (point-max)
  21398. (org-element-property
  21399. :end element)))))))
  21400. (eobp))))
  21401. (if uncommentp
  21402. ;; Only blank lines and comments in region: uncomment it.
  21403. (save-excursion
  21404. (goto-char (point-min))
  21405. (while (not (eobp))
  21406. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21407. (replace-match "" nil nil nil 1))
  21408. (forward-line)))
  21409. ;; Comment each line in region.
  21410. (let ((min-indent (point-max)))
  21411. ;; First find the minimum indentation across all lines.
  21412. (save-excursion
  21413. (goto-char (point-min))
  21414. (while (and (not (eobp)) (not (zerop min-indent)))
  21415. (unless (looking-at "[ \t]*$")
  21416. (setq min-indent (min min-indent (current-indentation))))
  21417. (forward-line)))
  21418. ;; Then loop over all lines.
  21419. (save-excursion
  21420. (goto-char (point-min))
  21421. (while (not (eobp))
  21422. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21423. ;; Don't get fooled by invisible text (e.g. link path)
  21424. ;; when moving to column MIN-INDENT.
  21425. (let ((buffer-invisibility-spec nil))
  21426. (org-move-to-column min-indent t))
  21427. (insert comment-start))
  21428. (forward-line)))))))))
  21429. (defun org-comment-dwim (arg)
  21430. "Call `comment-dwim' within a source edit buffer if needed."
  21431. (interactive "*P")
  21432. (if (org-in-src-block-p)
  21433. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21434. (call-interactively 'comment-dwim)))
  21435. ;;; Planning
  21436. ;; This section contains tools to operate on timestamp objects, as
  21437. ;; returned by, e.g. `org-element-context'.
  21438. (defun org-timestamp-has-time-p (timestamp)
  21439. "Non-nil when TIMESTAMP has a time specified."
  21440. (org-element-property :hour-start timestamp))
  21441. (defun org-timestamp-format (timestamp format &optional end utc)
  21442. "Format a TIMESTAMP element into a string.
  21443. FORMAT is a format specifier to be passed to
  21444. `format-time-string'.
  21445. When optional argument END is non-nil, use end of date-range or
  21446. time-range, if possible.
  21447. When optional argument UTC is non-nil, time will be expressed as
  21448. Universal Time."
  21449. (format-time-string
  21450. format
  21451. (apply 'encode-time
  21452. (cons 0
  21453. (mapcar
  21454. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21455. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21456. '(:minute-start :hour-start :day-start :month-start
  21457. :year-start)))))
  21458. utc))
  21459. (defun org-timestamp-split-range (timestamp &optional end)
  21460. "Extract a timestamp object from a date or time range.
  21461. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  21462. the end of the range. Otherwise, extract its start.
  21463. Return a new timestamp object sharing the same parent as
  21464. TIMESTAMP."
  21465. (let ((type (org-element-property :type timestamp)))
  21466. (if (memq type '(active inactive diary)) timestamp
  21467. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  21468. ;; Set new type.
  21469. (org-element-put-property
  21470. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21471. ;; Copy start properties over end properties if END is
  21472. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21473. (let ((p-alist '((:minute-start . :minute-end)
  21474. (:hour-start . :hour-end)
  21475. (:day-start . :day-end)
  21476. (:month-start . :month-end)
  21477. (:year-start . :year-end))))
  21478. (dolist (p-cell p-alist)
  21479. (org-element-put-property
  21480. split-ts
  21481. (funcall (if end 'car 'cdr) p-cell)
  21482. (org-element-property
  21483. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  21484. ;; Eventually refresh `:raw-value'.
  21485. (org-element-put-property split-ts :raw-value nil)
  21486. (org-element-put-property
  21487. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21488. (defun org-timestamp-translate (timestamp &optional boundary)
  21489. "Apply `org-translate-time' on a TIMESTAMP object.
  21490. When optional argument BOUNDARY is non-nil, it is either the
  21491. symbol `start' or `end'. In this case, only translate the
  21492. starting or ending part of TIMESTAMP if it is a date or time
  21493. range. Otherwise, translate both parts."
  21494. (if (and (not boundary)
  21495. (memq (org-element-property :type timestamp)
  21496. '(active-range inactive-range)))
  21497. (concat
  21498. (org-translate-time
  21499. (org-element-property :raw-value
  21500. (org-timestamp-split-range timestamp)))
  21501. "--"
  21502. (org-translate-time
  21503. (org-element-property :raw-value
  21504. (org-timestamp-split-range timestamp t))))
  21505. (org-translate-time
  21506. (org-element-property
  21507. :raw-value
  21508. (if (not boundary) timestamp
  21509. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  21510. ;;; Other stuff.
  21511. (defun org-reftex-citation ()
  21512. "Use reftex-citation to insert a citation into the buffer.
  21513. This looks for a line like
  21514. #+BIBLIOGRAPHY: foo plain option:-d
  21515. and derives from it that foo.bib is the bibliography file relevant
  21516. for this document. It then installs the necessary environment for RefTeX
  21517. to work in this buffer and calls `reftex-citation' to insert a citation
  21518. into the buffer.
  21519. Export of such citations to both LaTeX and HTML is handled by the contributed
  21520. package ox-bibtex by Taru Karttunen."
  21521. (interactive)
  21522. (let ((reftex-docstruct-symbol 'rds)
  21523. (reftex-cite-format "\\cite{%l}")
  21524. rds bib)
  21525. (save-excursion
  21526. (save-restriction
  21527. (widen)
  21528. (let ((case-fold-search t)
  21529. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21530. (if (not (save-excursion
  21531. (or (re-search-forward re nil t)
  21532. (re-search-backward re nil t))))
  21533. (user-error "No bibliography defined in file")
  21534. (setq bib (concat (match-string 1) ".bib")
  21535. rds (list (list 'bib bib)))))))
  21536. (call-interactively 'reftex-citation)))
  21537. ;;;; Functions extending outline functionality
  21538. (defun org-beginning-of-line (&optional arg)
  21539. "Go to the beginning of the current line. If that is invisible, continue
  21540. to a visible line beginning. This makes the function of C-a more intuitive.
  21541. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21542. first attempt, and only move to after the tags when the cursor is already
  21543. beyond the end of the headline."
  21544. (interactive "P")
  21545. (let ((pos (point))
  21546. (special (if (consp org-special-ctrl-a/e)
  21547. (car org-special-ctrl-a/e)
  21548. org-special-ctrl-a/e))
  21549. deactivate-mark refpos)
  21550. (if (org-bound-and-true-p visual-line-mode)
  21551. (beginning-of-visual-line 1)
  21552. (beginning-of-line 1))
  21553. (if (and arg (fboundp 'move-beginning-of-line))
  21554. (call-interactively 'move-beginning-of-line)
  21555. (if (bobp)
  21556. nil
  21557. (backward-char 1)
  21558. (if (org-truely-invisible-p)
  21559. (while (and (not (bobp)) (org-truely-invisible-p))
  21560. (backward-char 1)
  21561. (beginning-of-line 1))
  21562. (forward-char 1))))
  21563. (when special
  21564. (cond
  21565. ((and (looking-at org-complex-heading-regexp)
  21566. (= (char-after (match-end 1)) ?\ ))
  21567. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21568. (point-at-eol)))
  21569. (goto-char
  21570. (if (eq special t)
  21571. (cond ((> pos refpos) refpos)
  21572. ((= pos (point)) refpos)
  21573. (t (point)))
  21574. (cond ((> pos (point)) (point))
  21575. ((not (eq last-command this-command)) (point))
  21576. (t refpos)))))
  21577. ((org-at-item-p)
  21578. ;; Being at an item and not looking at an the item means point
  21579. ;; was previously moved to beginning of a visual line, which
  21580. ;; doesn't contain the item. Therefore, do nothing special,
  21581. ;; just stay here.
  21582. (when (looking-at org-list-full-item-re)
  21583. ;; Set special position at first white space character after
  21584. ;; bullet, and check-box, if any.
  21585. (let ((after-bullet
  21586. (let ((box (match-end 3)))
  21587. (if (not box) (match-end 1)
  21588. (let ((after (char-after box)))
  21589. (if (and after (= after ? )) (1+ box) box))))))
  21590. ;; Special case: Move point to special position when
  21591. ;; currently after it or at beginning of line.
  21592. (if (eq special t)
  21593. (when (or (> pos after-bullet) (= (point) pos))
  21594. (goto-char after-bullet))
  21595. ;; Reversed case: Move point to special position when
  21596. ;; point was already at beginning of line and command is
  21597. ;; repeated.
  21598. (when (and (= (point) pos) (eq last-command this-command))
  21599. (goto-char after-bullet))))))))
  21600. (org-no-warnings
  21601. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21602. (setq disable-point-adjustment
  21603. (or (not (invisible-p (point)))
  21604. (not (invisible-p (max (point-min) (1- (point))))))))
  21605. (defun org-end-of-line (&optional arg)
  21606. "Go to the end of the line.
  21607. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21608. tags on the first attempt, and only move to after the tags when
  21609. the cursor is already beyond the end of the headline."
  21610. (interactive "P")
  21611. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21612. org-special-ctrl-a/e))
  21613. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21614. 'end-of-visual-line)
  21615. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21616. (t 'end-of-line)))
  21617. deactivate-mark)
  21618. (if (or (not special) arg) (call-interactively move-fun)
  21619. (let* ((element (save-excursion (beginning-of-line)
  21620. (org-element-at-point)))
  21621. (type (org-element-type element)))
  21622. (cond
  21623. ((memq type '(headline inlinetask))
  21624. (let ((pos (point)))
  21625. (beginning-of-line 1)
  21626. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21627. (if (eq special t)
  21628. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21629. (goto-char (match-beginning 1))
  21630. (goto-char (match-end 0)))
  21631. (if (or (< pos (match-end 0))
  21632. (not (eq this-command last-command)))
  21633. (goto-char (match-end 0))
  21634. (goto-char (match-beginning 1))))
  21635. (call-interactively move-fun))))
  21636. ((outline-invisible-p (line-end-position))
  21637. ;; If element is hidden, `move-end-of-line' would put point
  21638. ;; after it. Use `end-of-line' to stay on current line.
  21639. (call-interactively 'end-of-line))
  21640. (t (call-interactively move-fun)))))
  21641. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21642. (setq disable-point-adjustment
  21643. (or (not (invisible-p (point)))
  21644. (not (invisible-p (max (point-min) (1- (point))))))))
  21645. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21646. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21647. (defun org-backward-sentence (&optional arg)
  21648. "Go to beginning of sentence, or beginning of table field.
  21649. This will call `backward-sentence' or `org-table-beginning-of-field',
  21650. depending on context."
  21651. (interactive "P")
  21652. (cond
  21653. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  21654. (t (call-interactively 'backward-sentence))))
  21655. (defun org-forward-sentence (&optional arg)
  21656. "Go to end of sentence, or end of table field.
  21657. This will call `forward-sentence' or `org-table-end-of-field',
  21658. depending on context."
  21659. (interactive "P")
  21660. (cond
  21661. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  21662. (t (call-interactively 'forward-sentence))))
  21663. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21664. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21665. (defun org-kill-line (&optional arg)
  21666. "Kill line, to tags or end of line."
  21667. (interactive "P")
  21668. (cond
  21669. ((or (not org-special-ctrl-k)
  21670. (bolp)
  21671. (not (org-at-heading-p)))
  21672. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21673. org-ctrl-k-protect-subtree)
  21674. (if (or (eq org-ctrl-k-protect-subtree 'error)
  21675. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  21676. (user-error "C-k aborted as it would kill a hidden subtree")))
  21677. (call-interactively
  21678. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21679. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21680. (kill-region (point) (match-beginning 1))
  21681. (org-set-tags nil t))
  21682. (t (kill-region (point) (point-at-eol)))))
  21683. (define-key org-mode-map "\C-k" 'org-kill-line)
  21684. (defun org-yank (&optional arg)
  21685. "Yank. If the kill is a subtree, treat it specially.
  21686. This command will look at the current kill and check if is a single
  21687. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21688. cursor is at the beginning of a line or after the stars of a currently
  21689. empty headline, then the yank is handled specially. How exactly depends
  21690. on the value of the following variables, both set by default.
  21691. `org-yank-folded-subtrees'
  21692. When set, the subtree(s) will be folded after insertion, but only
  21693. if doing so would now swallow text after the yanked text.
  21694. `org-yank-adjusted-subtrees'
  21695. When set, the subtree will be promoted or demoted in order to
  21696. fit into the local outline tree structure, which means that the
  21697. level will be adjusted so that it becomes the smaller one of the
  21698. two *visible* surrounding headings.
  21699. Any prefix to this command will cause `yank' to be called directly with
  21700. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21701. will just
  21702. plainly yank the text as it is.
  21703. \[1] The test checks if the first non-white line is a heading
  21704. and if there are no other headings with fewer stars."
  21705. (interactive "P")
  21706. (org-yank-generic 'yank arg))
  21707. (defun org-yank-generic (command arg)
  21708. "Perform some yank-like command.
  21709. This function implements the behavior described in the `org-yank'
  21710. documentation. However, it has been generalized to work for any
  21711. interactive command with similar behavior."
  21712. ;; pretend to be command COMMAND
  21713. (setq this-command command)
  21714. (if arg
  21715. (call-interactively command)
  21716. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21717. (and (org-kill-is-subtree-p)
  21718. (or (bolp)
  21719. (and (looking-at "[ \t]*$")
  21720. (string-match
  21721. "\\`\\*+\\'"
  21722. (buffer-substring (point-at-bol) (point)))))))
  21723. swallowp)
  21724. (cond
  21725. ((and subtreep org-yank-folded-subtrees)
  21726. (let ((beg (point))
  21727. end)
  21728. (if (and subtreep org-yank-adjusted-subtrees)
  21729. (org-paste-subtree nil nil 'for-yank)
  21730. (call-interactively command))
  21731. (setq end (point))
  21732. (goto-char beg)
  21733. (when (and (bolp) subtreep
  21734. (not (setq swallowp
  21735. (org-yank-folding-would-swallow-text beg end))))
  21736. (org-with-limited-levels
  21737. (or (looking-at org-outline-regexp)
  21738. (re-search-forward org-outline-regexp-bol end t))
  21739. (while (and (< (point) end) (looking-at org-outline-regexp))
  21740. (hide-subtree)
  21741. (org-cycle-show-empty-lines 'folded)
  21742. (condition-case nil
  21743. (outline-forward-same-level 1)
  21744. (error (goto-char end))))))
  21745. (when swallowp
  21746. (message
  21747. "Inserted text not folded because that would swallow text"))
  21748. (goto-char end)
  21749. (skip-chars-forward " \t\n\r")
  21750. (beginning-of-line 1)
  21751. (push-mark beg 'nomsg)))
  21752. ((and subtreep org-yank-adjusted-subtrees)
  21753. (let ((beg (point-at-bol)))
  21754. (org-paste-subtree nil nil 'for-yank)
  21755. (push-mark beg 'nomsg)))
  21756. (t
  21757. (call-interactively command))))))
  21758. (defun org-yank-folding-would-swallow-text (beg end)
  21759. "Would hide-subtree at BEG swallow any text after END?"
  21760. (let (level)
  21761. (org-with-limited-levels
  21762. (save-excursion
  21763. (goto-char beg)
  21764. (when (or (looking-at org-outline-regexp)
  21765. (re-search-forward org-outline-regexp-bol end t))
  21766. (setq level (org-outline-level)))
  21767. (goto-char end)
  21768. (skip-chars-forward " \t\r\n\v\f")
  21769. (if (or (eobp)
  21770. (and (bolp) (looking-at org-outline-regexp)
  21771. (<= (org-outline-level) level)))
  21772. nil ; Nothing would be swallowed
  21773. t))))) ; something would swallow
  21774. (define-key org-mode-map "\C-y" 'org-yank)
  21775. (defun org-truely-invisible-p ()
  21776. "Check if point is at a character currently not visible.
  21777. This version does not only check the character property, but also
  21778. `visible-mode'."
  21779. ;; Early versions of noutline don't have `outline-invisible-p'.
  21780. (if (org-bound-and-true-p visible-mode)
  21781. nil
  21782. (outline-invisible-p)))
  21783. (defun org-invisible-p2 ()
  21784. "Check if point is at a character currently not visible."
  21785. (save-excursion
  21786. (if (and (eolp) (not (bobp))) (backward-char 1))
  21787. ;; Early versions of noutline don't have `outline-invisible-p'.
  21788. (outline-invisible-p)))
  21789. (defun org-back-to-heading (&optional invisible-ok)
  21790. "Call `outline-back-to-heading', but provide a better error message."
  21791. (condition-case nil
  21792. (outline-back-to-heading invisible-ok)
  21793. (error (error "Before first headline at position %d in buffer %s"
  21794. (point) (current-buffer)))))
  21795. (defun org-before-first-heading-p ()
  21796. "Before first heading?"
  21797. (save-excursion
  21798. (end-of-line)
  21799. (null (re-search-backward org-outline-regexp-bol nil t))))
  21800. (defun org-at-heading-p (&optional ignored)
  21801. (outline-on-heading-p t))
  21802. ;; Compatibility alias with Org versions < 7.8.03
  21803. (defalias 'org-on-heading-p 'org-at-heading-p)
  21804. (defun org-in-commented-heading-p (&optional no-inheritance)
  21805. "Non-nil if point is under a commented heading.
  21806. This function also checks ancestors of the current headline,
  21807. unless optional argument NO-INHERITANCE is non-nil."
  21808. (cond
  21809. ((org-before-first-heading-p) nil)
  21810. ((let ((headline (nth 4 (org-heading-components))))
  21811. (and headline
  21812. (let ((case-fold-search nil))
  21813. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  21814. headline)))))
  21815. (no-inheritance nil)
  21816. (t
  21817. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  21818. (defun org-at-comment-p nil
  21819. "Is cursor in a commented line?"
  21820. (save-excursion
  21821. (save-match-data
  21822. (beginning-of-line)
  21823. (looking-at "^[ \t]*# "))))
  21824. (defun org-at-drawer-p nil
  21825. "Is cursor at a drawer keyword?"
  21826. (save-excursion
  21827. (move-beginning-of-line 1)
  21828. (looking-at org-drawer-regexp)))
  21829. (defun org-at-block-p nil
  21830. "Is cursor at a block keyword?"
  21831. (save-excursion
  21832. (move-beginning-of-line 1)
  21833. (looking-at org-block-regexp)))
  21834. (defun org-point-at-end-of-empty-headline ()
  21835. "If point is at the end of an empty headline, return t, else nil.
  21836. If the heading only contains a TODO keyword, it is still still considered
  21837. empty."
  21838. (and (looking-at "[ \t]*$")
  21839. (when org-todo-line-regexp
  21840. (save-excursion
  21841. (beginning-of-line 1)
  21842. (let ((case-fold-search nil))
  21843. (looking-at org-todo-line-regexp)
  21844. (string= (match-string 3) ""))))))
  21845. (defun org-at-heading-or-item-p ()
  21846. (or (org-at-heading-p) (org-at-item-p)))
  21847. (defun org-at-target-p ()
  21848. (or (org-in-regexp org-radio-target-regexp)
  21849. (org-in-regexp org-target-regexp)))
  21850. ;; Compatibility alias with Org versions < 7.8.03
  21851. (defalias 'org-on-target-p 'org-at-target-p)
  21852. (defun org-up-heading-all (arg)
  21853. "Move to the heading line of which the present line is a subheading.
  21854. This function considers both visible and invisible heading lines.
  21855. With argument, move up ARG levels."
  21856. (if (fboundp 'outline-up-heading-all)
  21857. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21858. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21859. (defun org-up-heading-safe ()
  21860. "Move to the heading line of which the present line is a subheading.
  21861. This version will not throw an error. It will return the level of the
  21862. headline found, or nil if no higher level is found.
  21863. Also, this function will be a lot faster than `outline-up-heading',
  21864. because it relies on stars being the outline starters. This can really
  21865. make a significant difference in outlines with very many siblings."
  21866. (when (ignore-errors (org-back-to-heading t))
  21867. (let ((level-up (1- (funcall outline-level))))
  21868. (and (> level-up 0)
  21869. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  21870. (funcall outline-level)))))
  21871. (defun org-first-sibling-p ()
  21872. "Is this heading the first child of its parents?"
  21873. (interactive)
  21874. (let ((re org-outline-regexp-bol)
  21875. level l)
  21876. (unless (org-at-heading-p t)
  21877. (user-error "Not at a heading"))
  21878. (setq level (funcall outline-level))
  21879. (save-excursion
  21880. (if (not (re-search-backward re nil t))
  21881. t
  21882. (setq l (funcall outline-level))
  21883. (< l level)))))
  21884. (defun org-goto-sibling (&optional previous)
  21885. "Goto the next sibling, even if it is invisible.
  21886. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21887. when a sibling was found. When none is found, return nil and don't
  21888. move point."
  21889. (let ((fun (if previous 're-search-backward 're-search-forward))
  21890. (pos (point))
  21891. (re org-outline-regexp-bol)
  21892. level l)
  21893. (when (ignore-errors (org-back-to-heading t))
  21894. (setq level (funcall outline-level))
  21895. (catch 'exit
  21896. (or previous (forward-char 1))
  21897. (while (funcall fun re nil t)
  21898. (setq l (funcall outline-level))
  21899. (when (< l level) (goto-char pos) (throw 'exit nil))
  21900. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21901. (goto-char pos)
  21902. nil))))
  21903. (defun org-show-siblings ()
  21904. "Show all siblings of the current headline."
  21905. (save-excursion
  21906. (while (org-goto-sibling) (org-flag-heading nil)))
  21907. (save-excursion
  21908. (while (org-goto-sibling 'previous)
  21909. (org-flag-heading nil))))
  21910. (defun org-goto-first-child ()
  21911. "Goto the first child, even if it is invisible.
  21912. Return t when a child was found. Otherwise don't move point and
  21913. return nil."
  21914. (let (level (pos (point)) (re org-outline-regexp-bol))
  21915. (when (ignore-errors (org-back-to-heading t))
  21916. (setq level (outline-level))
  21917. (forward-char 1)
  21918. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21919. (progn (goto-char (match-beginning 0)) t)
  21920. (goto-char pos) nil))))
  21921. (defun org-show-hidden-entry ()
  21922. "Show an entry where even the heading is hidden."
  21923. (save-excursion
  21924. (org-show-entry)))
  21925. (defun org-flag-heading (flag &optional entry)
  21926. "Flag the current heading. FLAG non-nil means make invisible.
  21927. When ENTRY is non-nil, show the entire entry."
  21928. (save-excursion
  21929. (org-back-to-heading t)
  21930. ;; Check if we should show the entire entry
  21931. (if entry
  21932. (progn
  21933. (org-show-entry)
  21934. (save-excursion
  21935. (and (outline-next-heading)
  21936. (org-flag-heading nil))))
  21937. (outline-flag-region (max (point-min) (1- (point)))
  21938. (save-excursion (outline-end-of-heading) (point))
  21939. flag))))
  21940. (defun org-get-next-sibling ()
  21941. "Move to next heading of the same level, and return point.
  21942. If there is no such heading, return nil.
  21943. This is like outline-next-sibling, but invisible headings are ok."
  21944. (let ((level (funcall outline-level)))
  21945. (outline-next-heading)
  21946. (while (and (not (eobp)) (> (funcall outline-level) level))
  21947. (outline-next-heading))
  21948. (if (or (eobp) (< (funcall outline-level) level))
  21949. nil
  21950. (point))))
  21951. (defun org-get-last-sibling ()
  21952. "Move to previous heading of the same level, and return point.
  21953. If there is no such heading, return nil."
  21954. (let ((opoint (point))
  21955. (level (funcall outline-level)))
  21956. (outline-previous-heading)
  21957. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21958. (while (and (> (funcall outline-level) level)
  21959. (not (bobp)))
  21960. (outline-previous-heading))
  21961. (if (< (funcall outline-level) level)
  21962. nil
  21963. (point)))))
  21964. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21965. "Goto to the end of a subtree."
  21966. ;; This contains an exact copy of the original function, but it uses
  21967. ;; `org-back-to-heading', to make it work also in invisible
  21968. ;; trees. And is uses an invisible-ok argument.
  21969. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21970. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21971. ;; with many children and grandchildren etc, this can be much faster
  21972. ;; than the outline version.
  21973. (org-back-to-heading invisible-ok)
  21974. (let ((first t)
  21975. (level (funcall outline-level)))
  21976. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21977. ;; A true heading (not a plain list item), in Org-mode
  21978. ;; This means we can easily find the end by looking
  21979. ;; only for the right number of stars. Using a regexp to do
  21980. ;; this is so much faster than using a Lisp loop.
  21981. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21982. (forward-char 1)
  21983. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21984. ;; something else, do it the slow way
  21985. (while (and (not (eobp))
  21986. (or first (> (funcall outline-level) level)))
  21987. (setq first nil)
  21988. (outline-next-heading)))
  21989. (unless to-heading
  21990. (if (memq (preceding-char) '(?\n ?\^M))
  21991. (progn
  21992. ;; Go to end of line before heading
  21993. (forward-char -1)
  21994. (if (memq (preceding-char) '(?\n ?\^M))
  21995. ;; leave blank line before heading
  21996. (forward-char -1))))))
  21997. (point))
  21998. (defun org-end-of-meta-data-and-drawers ()
  21999. "Jump to the first text after meta data and drawers in the current entry.
  22000. This will move over empty lines, lines with planning time stamps,
  22001. clocking lines, and drawers."
  22002. (org-back-to-heading t)
  22003. (let ((end (save-excursion (outline-next-heading) (point)))
  22004. (re (concat "\\(" org-drawer-regexp "\\)"
  22005. "\\|" "[ \t]*" org-keyword-time-regexp)))
  22006. (forward-line 1)
  22007. (while (re-search-forward re end t)
  22008. (if (not (match-end 1))
  22009. ;; empty or planning line
  22010. (forward-line 1)
  22011. ;; a drawer, find the end
  22012. (re-search-forward "^[ \t]*:END:" end 'move)
  22013. (forward-line 1)))
  22014. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  22015. (point)))
  22016. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22017. "Move forward to the ARG'th subheading at same level as this one.
  22018. Stop at the first and last subheadings of a superior heading.
  22019. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22020. non-nil it will also look at invisible ones."
  22021. (interactive "p")
  22022. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22023. (if (and arg (< arg 0))
  22024. (goto-char (point-min))
  22025. (outline-next-heading))
  22026. (org-at-heading-p)
  22027. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22028. (f (if (and arg (< arg 0))
  22029. 're-search-backward
  22030. 're-search-forward))
  22031. (count (if arg (abs arg) 1))
  22032. (result (point)))
  22033. (while (and (prog1 (> count 0)
  22034. (forward-char (if (and arg (< arg 0)) -1 1)))
  22035. (funcall f org-outline-regexp-bol nil 'move))
  22036. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22037. (cond ((< l level) (setq count 0))
  22038. ((and (= l level)
  22039. (or invisible-ok
  22040. (progn
  22041. (goto-char (line-beginning-position))
  22042. (not (outline-invisible-p)))))
  22043. (setq count (1- count))
  22044. (when (eq l level)
  22045. (setq result (point)))))))
  22046. (goto-char result))
  22047. (beginning-of-line 1)))
  22048. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22049. "Move backward to the ARG'th subheading at same level as this one.
  22050. Stop at the first and last subheadings of a superior heading."
  22051. (interactive "p")
  22052. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22053. (defun org-next-block (arg &optional backward block-regexp)
  22054. "Jump to the next block.
  22055. With a prefix argument ARG, jump forward ARG many source blocks.
  22056. When BACKWARD is non-nil, jump to the previous block.
  22057. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22058. (interactive "p")
  22059. (let ((re (or block-regexp org-block-regexp))
  22060. (re-search-fn (or (and backward 're-search-backward)
  22061. 're-search-forward)))
  22062. (if (looking-at re) (forward-char 1))
  22063. (condition-case nil
  22064. (funcall re-search-fn re nil nil arg)
  22065. (error (user-error "No %s code blocks"
  22066. (if backward "previous" "further" ))))
  22067. (goto-char (match-beginning 0)) (org-show-context)))
  22068. (defun org-previous-block (arg &optional block-regexp)
  22069. "Jump to the previous block.
  22070. With a prefix argument ARG, jump backward ARG many source blocks.
  22071. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22072. (interactive "p")
  22073. (org-next-block arg t block-regexp))
  22074. (defun org-forward-paragraph ()
  22075. "Move forward to beginning of next paragraph or equivalent.
  22076. The function moves point to the beginning of the next visible
  22077. structural element, which can be a paragraph, a table, a list
  22078. item, etc. It also provides some special moves for convenience:
  22079. - On an affiliated keyword, jump to the beginning of the
  22080. relative element.
  22081. - On an item or a footnote definition, move to the second
  22082. element inside, if any.
  22083. - On a table or a property drawer, jump after it.
  22084. - On a verse or source block, stop after blank lines."
  22085. (interactive)
  22086. (when (eobp) (user-error "Cannot move further down"))
  22087. (let* ((deactivate-mark nil)
  22088. (element (org-element-at-point))
  22089. (type (org-element-type element))
  22090. (post-affiliated (org-element-property :post-affiliated element))
  22091. (contents-begin (org-element-property :contents-begin element))
  22092. (contents-end (org-element-property :contents-end element))
  22093. (end (let ((end (org-element-property :end element)) (parent element))
  22094. (while (and (setq parent (org-element-property :parent parent))
  22095. (= (org-element-property :contents-end parent) end))
  22096. (setq end (org-element-property :end parent)))
  22097. end)))
  22098. (cond ((not element)
  22099. (skip-chars-forward " \r\t\n")
  22100. (or (eobp) (beginning-of-line)))
  22101. ;; On affiliated keywords, move to element's beginning.
  22102. ((< (point) post-affiliated)
  22103. (goto-char post-affiliated))
  22104. ;; At a table row, move to the end of the table. Similarly,
  22105. ;; at a node property, move to the end of the property
  22106. ;; drawer.
  22107. ((memq type '(node-property table-row))
  22108. (goto-char (org-element-property
  22109. :end (org-element-property :parent element))))
  22110. ((memq type '(property-drawer table)) (goto-char end))
  22111. ;; Consider blank lines as separators in verse and source
  22112. ;; blocks to ease editing.
  22113. ((memq type '(src-block verse-block))
  22114. (when (eq type 'src-block)
  22115. (setq contents-end
  22116. (save-excursion (goto-char end)
  22117. (skip-chars-backward " \r\t\n")
  22118. (line-beginning-position))))
  22119. (beginning-of-line)
  22120. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22121. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22122. (goto-char end)
  22123. (skip-chars-forward " \r\t\n")
  22124. (if (= (point) contents-end) (goto-char end)
  22125. (beginning-of-line))))
  22126. ;; With no contents, just skip element.
  22127. ((not contents-begin) (goto-char end))
  22128. ;; If contents are invisible, skip the element altogether.
  22129. ((outline-invisible-p (line-end-position))
  22130. (case type
  22131. (headline
  22132. (org-with-limited-levels (outline-next-visible-heading 1)))
  22133. ;; At a plain list, make sure we move to the next item
  22134. ;; instead of skipping the whole list.
  22135. (plain-list (forward-char)
  22136. (org-forward-paragraph))
  22137. (otherwise (goto-char end))))
  22138. ((>= (point) contents-end) (goto-char end))
  22139. ((>= (point) contents-begin)
  22140. ;; This can only happen on paragraphs and plain lists.
  22141. (case type
  22142. (paragraph (goto-char end))
  22143. ;; At a plain list, try to move to second element in
  22144. ;; first item, if possible.
  22145. (plain-list (end-of-line)
  22146. (org-forward-paragraph))))
  22147. ;; When contents start on the middle of a line (e.g. in
  22148. ;; items and footnote definitions), try to reach first
  22149. ;; element starting after current line.
  22150. ((> (line-end-position) contents-begin)
  22151. (end-of-line)
  22152. (org-forward-paragraph))
  22153. (t (goto-char contents-begin)))))
  22154. (defun org-backward-paragraph ()
  22155. "Move backward to start of previous paragraph or equivalent.
  22156. The function moves point to the beginning of the current
  22157. structural element, which can be a paragraph, a table, a list
  22158. item, etc., or to the beginning of the previous visible one if
  22159. point is already there. It also provides some special moves for
  22160. convenience:
  22161. - On an affiliated keyword, jump to the first one.
  22162. - On a table or a property drawer, move to its beginning.
  22163. - On a verse or source block, stop before blank lines."
  22164. (interactive)
  22165. (when (bobp) (user-error "Cannot move further up"))
  22166. (let* ((deactivate-mark nil)
  22167. (element (org-element-at-point))
  22168. (type (org-element-type element))
  22169. (contents-begin (org-element-property :contents-begin element))
  22170. (contents-end (org-element-property :contents-end element))
  22171. (post-affiliated (org-element-property :post-affiliated element))
  22172. (begin (org-element-property :begin element)))
  22173. (cond
  22174. ((not element) (goto-char (point-min)))
  22175. ((= (point) begin)
  22176. (backward-char)
  22177. (org-backward-paragraph))
  22178. ((<= (point) post-affiliated) (goto-char begin))
  22179. ((memq type '(node-property table-row))
  22180. (goto-char (org-element-property
  22181. :post-affiliated (org-element-property :parent element))))
  22182. ((memq type '(property-drawer table)) (goto-char begin))
  22183. ((memq type '(src-block verse-block))
  22184. (when (eq type 'src-block)
  22185. (setq contents-begin
  22186. (save-excursion (goto-char begin) (forward-line) (point))))
  22187. (if (= (point) contents-begin) (goto-char post-affiliated)
  22188. ;; Inside a verse block, see blank lines as paragraph
  22189. ;; separators.
  22190. (let ((origin (point)))
  22191. (skip-chars-backward " \r\t\n" contents-begin)
  22192. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22193. (skip-chars-forward " \r\t\n" origin)
  22194. (if (= (point) origin) (goto-char contents-begin)
  22195. (beginning-of-line))))))
  22196. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22197. ((eq type 'paragraph)
  22198. (goto-char contents-begin)
  22199. ;; When at first paragraph in an item or a footnote definition,
  22200. ;; move directly to beginning of line.
  22201. (let ((parent-contents
  22202. (org-element-property
  22203. :contents-begin (org-element-property :parent element))))
  22204. (when (and parent-contents (= parent-contents contents-begin))
  22205. (beginning-of-line))))
  22206. ;; At the end of a greater element, move to the beginning of the
  22207. ;; last element within.
  22208. ((>= (point) contents-end)
  22209. (goto-char (1- contents-end))
  22210. (org-backward-paragraph))
  22211. (t (goto-char (or post-affiliated begin))))
  22212. ;; Ensure we never leave point invisible.
  22213. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22214. (defun org-forward-element ()
  22215. "Move forward by one element.
  22216. Move to the next element at the same level, when possible."
  22217. (interactive)
  22218. (cond ((eobp) (user-error "Cannot move further down"))
  22219. ((org-with-limited-levels (org-at-heading-p))
  22220. (let ((origin (point)))
  22221. (goto-char (org-end-of-subtree nil t))
  22222. (unless (org-with-limited-levels (org-at-heading-p))
  22223. (goto-char origin)
  22224. (user-error "Cannot move further down"))))
  22225. (t
  22226. (let* ((elem (org-element-at-point))
  22227. (end (org-element-property :end elem))
  22228. (parent (org-element-property :parent elem)))
  22229. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22230. (goto-char (org-element-property :end parent)))
  22231. ((integer-or-marker-p end) (goto-char end))
  22232. (t (message "No element at point")))))))
  22233. (defun org-backward-element ()
  22234. "Move backward by one element.
  22235. Move to the previous element at the same level, when possible."
  22236. (interactive)
  22237. (cond ((bobp) (user-error "Cannot move further up"))
  22238. ((org-with-limited-levels (org-at-heading-p))
  22239. ;; At a headline, move to the previous one, if any, or stay
  22240. ;; here.
  22241. (let ((origin (point)))
  22242. (org-with-limited-levels (org-backward-heading-same-level 1))
  22243. ;; When current headline has no sibling above, move to its
  22244. ;; parent.
  22245. (when (= (point) origin)
  22246. (or (org-with-limited-levels (org-up-heading-safe))
  22247. (progn (goto-char origin)
  22248. (user-error "Cannot move further up"))))))
  22249. (t
  22250. (let* ((elem (org-element-at-point))
  22251. (beg (org-element-property :begin elem)))
  22252. (cond
  22253. ;; Move to beginning of current element if point isn't
  22254. ;; there already.
  22255. ((null beg) (message "No element at point"))
  22256. ((/= (point) beg) (goto-char beg))
  22257. (t (goto-char beg)
  22258. (skip-chars-backward " \r\t\n")
  22259. (unless (bobp)
  22260. (let ((prev (org-element-at-point)))
  22261. (goto-char (org-element-property :begin prev))
  22262. (while (and (setq prev (org-element-property :parent prev))
  22263. (<= (org-element-property :end prev) beg))
  22264. (goto-char (org-element-property :begin prev)))))))))))
  22265. (defun org-up-element ()
  22266. "Move to upper element."
  22267. (interactive)
  22268. (if (org-with-limited-levels (org-at-heading-p))
  22269. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22270. (let* ((elem (org-element-at-point))
  22271. (parent (org-element-property :parent elem)))
  22272. (if parent (goto-char (org-element-property :begin parent))
  22273. (if (org-with-limited-levels (org-before-first-heading-p))
  22274. (user-error "No surrounding element")
  22275. (org-with-limited-levels (org-back-to-heading)))))))
  22276. (defvar org-element-greater-elements)
  22277. (defun org-down-element ()
  22278. "Move to inner element."
  22279. (interactive)
  22280. (let ((element (org-element-at-point)))
  22281. (cond
  22282. ((memq (org-element-type element) '(plain-list table))
  22283. (goto-char (org-element-property :contents-begin element))
  22284. (forward-char))
  22285. ((memq (org-element-type element) org-element-greater-elements)
  22286. ;; If contents are hidden, first disclose them.
  22287. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22288. (goto-char (or (org-element-property :contents-begin element)
  22289. (user-error "No content for this element"))))
  22290. (t (user-error "No inner element")))))
  22291. (defun org-drag-element-backward ()
  22292. "Move backward element at point."
  22293. (interactive)
  22294. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22295. (let* ((elem (org-element-at-point))
  22296. (prev-elem
  22297. (save-excursion
  22298. (goto-char (org-element-property :begin elem))
  22299. (skip-chars-backward " \r\t\n")
  22300. (unless (bobp)
  22301. (let* ((beg (org-element-property :begin elem))
  22302. (prev (org-element-at-point))
  22303. (up prev))
  22304. (while (and (setq up (org-element-property :parent up))
  22305. (<= (org-element-property :end up) beg))
  22306. (setq prev up))
  22307. prev)))))
  22308. ;; Error out if no previous element or previous element is
  22309. ;; a parent of the current one.
  22310. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22311. (user-error "Cannot drag element backward")
  22312. (let ((pos (point)))
  22313. (org-element-swap-A-B prev-elem elem)
  22314. (goto-char (+ (org-element-property :begin prev-elem)
  22315. (- pos (org-element-property :begin elem)))))))))
  22316. (defun org-drag-element-forward ()
  22317. "Move forward element at point."
  22318. (interactive)
  22319. (let* ((pos (point))
  22320. (elem (org-element-at-point)))
  22321. (when (= (point-max) (org-element-property :end elem))
  22322. (user-error "Cannot drag element forward"))
  22323. (goto-char (org-element-property :end elem))
  22324. (let ((next-elem (org-element-at-point)))
  22325. (when (or (org-element-nested-p elem next-elem)
  22326. (and (eq (org-element-type next-elem) 'headline)
  22327. (not (eq (org-element-type elem) 'headline))))
  22328. (goto-char pos)
  22329. (user-error "Cannot drag element forward"))
  22330. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22331. ;; body's length (without final blanks) and by the length of
  22332. ;; blanks between ELEM and NEXT-ELEM.
  22333. (let ((size-next (- (save-excursion
  22334. (goto-char (org-element-property :end next-elem))
  22335. (skip-chars-backward " \r\t\n")
  22336. (forward-line)
  22337. ;; Small correction if buffer doesn't end
  22338. ;; with a newline character.
  22339. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22340. (org-element-property :begin next-elem)))
  22341. (size-blank (- (org-element-property :end elem)
  22342. (save-excursion
  22343. (goto-char (org-element-property :end elem))
  22344. (skip-chars-backward " \r\t\n")
  22345. (forward-line)
  22346. (point)))))
  22347. (org-element-swap-A-B elem next-elem)
  22348. (goto-char (+ pos size-next size-blank))))))
  22349. (defun org-drag-line-forward (arg)
  22350. "Drag the line at point ARG lines forward."
  22351. (interactive "p")
  22352. (dotimes (n (abs arg))
  22353. (let ((c (current-column)))
  22354. (if (< 0 arg)
  22355. (progn
  22356. (beginning-of-line 2)
  22357. (transpose-lines 1)
  22358. (beginning-of-line 0))
  22359. (transpose-lines 1)
  22360. (beginning-of-line -1))
  22361. (org-move-to-column c))))
  22362. (defun org-drag-line-backward (arg)
  22363. "Drag the line at point ARG lines backward."
  22364. (interactive "p")
  22365. (org-drag-line-forward (- arg)))
  22366. (defun org-mark-element ()
  22367. "Put point at beginning of this element, mark at end.
  22368. Interactively, if this command is repeated or (in Transient Mark
  22369. mode) if the mark is active, it marks the next element after the
  22370. ones already marked."
  22371. (interactive)
  22372. (let (deactivate-mark)
  22373. (if (and (org-called-interactively-p 'any)
  22374. (or (and (eq last-command this-command) (mark t))
  22375. (and transient-mark-mode mark-active)))
  22376. (set-mark
  22377. (save-excursion
  22378. (goto-char (mark))
  22379. (goto-char (org-element-property :end (org-element-at-point)))))
  22380. (let ((element (org-element-at-point)))
  22381. (end-of-line)
  22382. (push-mark (org-element-property :end element) t t)
  22383. (goto-char (org-element-property :begin element))))))
  22384. (defun org-narrow-to-element ()
  22385. "Narrow buffer to current element."
  22386. (interactive)
  22387. (let ((elem (org-element-at-point)))
  22388. (cond
  22389. ((eq (car elem) 'headline)
  22390. (narrow-to-region
  22391. (org-element-property :begin elem)
  22392. (org-element-property :end elem)))
  22393. ((memq (car elem) org-element-greater-elements)
  22394. (narrow-to-region
  22395. (org-element-property :contents-begin elem)
  22396. (org-element-property :contents-end elem)))
  22397. (t
  22398. (narrow-to-region
  22399. (org-element-property :begin elem)
  22400. (org-element-property :end elem))))))
  22401. (defun org-transpose-element ()
  22402. "Transpose current and previous elements, keeping blank lines between.
  22403. Point is moved after both elements."
  22404. (interactive)
  22405. (org-skip-whitespace)
  22406. (let ((end (org-element-property :end (org-element-at-point))))
  22407. (org-drag-element-backward)
  22408. (goto-char end)))
  22409. (defun org-unindent-buffer ()
  22410. "Un-indent the visible part of the buffer.
  22411. Relative indentation (between items, inside blocks, etc.) isn't
  22412. modified."
  22413. (interactive)
  22414. (unless (eq major-mode 'org-mode)
  22415. (user-error "Cannot un-indent a buffer not in Org mode"))
  22416. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22417. unindent-tree ; For byte-compiler.
  22418. (unindent-tree
  22419. (function
  22420. (lambda (contents)
  22421. (mapc
  22422. (lambda (element)
  22423. (if (memq (org-element-type element) '(headline section))
  22424. (funcall unindent-tree (org-element-contents element))
  22425. (save-excursion
  22426. (save-restriction
  22427. (narrow-to-region
  22428. (org-element-property :begin element)
  22429. (org-element-property :end element))
  22430. (org-do-remove-indentation)))))
  22431. (reverse contents))))))
  22432. (funcall unindent-tree (org-element-contents parse-tree))))
  22433. (defun org-show-subtree ()
  22434. "Show everything after this heading at deeper levels."
  22435. (interactive)
  22436. (outline-flag-region
  22437. (point)
  22438. (save-excursion
  22439. (org-end-of-subtree t t))
  22440. nil))
  22441. (defun org-show-entry ()
  22442. "Show the body directly following this heading.
  22443. Show the heading too, if it is currently invisible."
  22444. (interactive)
  22445. (save-excursion
  22446. (ignore-errors
  22447. (org-back-to-heading t)
  22448. (outline-flag-region
  22449. (max (point-min) (1- (point)))
  22450. (save-excursion
  22451. (if (re-search-forward
  22452. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22453. (match-beginning 1)
  22454. (point-max)))
  22455. nil)
  22456. (org-cycle-hide-drawers 'children))))
  22457. (defun org-make-options-regexp (kwds &optional extra)
  22458. "Make a regular expression for keyword lines."
  22459. (concat
  22460. "^[ \t]*#\\+\\("
  22461. (mapconcat 'regexp-quote kwds "\\|")
  22462. (if extra (concat "\\|" extra))
  22463. "\\):[ \t]*\\(.*\\)"))
  22464. ;; Make isearch reveal the necessary context
  22465. (defun org-isearch-end ()
  22466. "Reveal context after isearch exits."
  22467. (when isearch-success ; only if search was successful
  22468. (if (featurep 'xemacs)
  22469. ;; Under XEmacs, the hook is run in the correct place,
  22470. ;; we directly show the context.
  22471. (org-show-context 'isearch)
  22472. ;; In Emacs the hook runs *before* restoring the overlays.
  22473. ;; So we have to use a one-time post-command-hook to do this.
  22474. ;; (Emacs 22 has a special variable, see function `org-mode')
  22475. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22476. isearch-mode-end-hook-quit)
  22477. ;; Only when the isearch was not quitted.
  22478. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22479. 'append 'local)))))
  22480. (defun org-isearch-post-command ()
  22481. "Remove self from hook, and show context."
  22482. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22483. (org-show-context 'isearch))
  22484. ;;;; Integration with and fixes for other packages
  22485. ;;; Imenu support
  22486. (defvar org-imenu-markers nil
  22487. "All markers currently used by Imenu.")
  22488. (make-variable-buffer-local 'org-imenu-markers)
  22489. (defun org-imenu-new-marker (&optional pos)
  22490. "Return a new marker for use by Imenu, and remember the marker."
  22491. (let ((m (make-marker)))
  22492. (move-marker m (or pos (point)))
  22493. (push m org-imenu-markers)
  22494. m))
  22495. (defun org-imenu-get-tree ()
  22496. "Produce the index for Imenu."
  22497. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22498. (setq org-imenu-markers nil)
  22499. (let* ((n org-imenu-depth)
  22500. (re (concat "^" (org-get-limited-outline-regexp)))
  22501. (subs (make-vector (1+ n) nil))
  22502. (last-level 0)
  22503. m level head0 head)
  22504. (save-excursion
  22505. (save-restriction
  22506. (widen)
  22507. (goto-char (point-max))
  22508. (while (re-search-backward re nil t)
  22509. (setq level (org-reduced-level (funcall outline-level)))
  22510. (when (and (<= level n)
  22511. (looking-at org-complex-heading-regexp)
  22512. (setq head0 (org-match-string-no-properties 4)))
  22513. (setq head (org-link-display-format head0)
  22514. m (org-imenu-new-marker))
  22515. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22516. (if (>= level last-level)
  22517. (push (cons head m) (aref subs level))
  22518. (push (cons head (aref subs (1+ level))) (aref subs level))
  22519. (loop for i from (1+ level) to n do (aset subs i nil)))
  22520. (setq last-level level)))))
  22521. (aref subs 1)))
  22522. (eval-after-load "imenu"
  22523. '(progn
  22524. (add-hook 'imenu-after-jump-hook
  22525. (lambda ()
  22526. (if (derived-mode-p 'org-mode)
  22527. (org-show-context 'org-goto))))))
  22528. (defun org-link-display-format (link)
  22529. "Replace a link with its the description.
  22530. If there is no description, use the link target."
  22531. (save-match-data
  22532. (if (string-match org-bracket-link-analytic-regexp link)
  22533. (replace-match (if (match-end 5)
  22534. (match-string 5 link)
  22535. (concat (match-string 1 link)
  22536. (match-string 3 link)))
  22537. nil t link)
  22538. link)))
  22539. (defun org-toggle-link-display ()
  22540. "Toggle the literal or descriptive display of links."
  22541. (interactive)
  22542. (if org-descriptive-links
  22543. (progn (org-remove-from-invisibility-spec '(org-link))
  22544. (org-restart-font-lock)
  22545. (setq org-descriptive-links nil))
  22546. (progn (add-to-invisibility-spec '(org-link))
  22547. (org-restart-font-lock)
  22548. (setq org-descriptive-links t))))
  22549. ;; Speedbar support
  22550. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22551. "Overlay marking the agenda restriction line in speedbar.")
  22552. (overlay-put org-speedbar-restriction-lock-overlay
  22553. 'face 'org-agenda-restriction-lock)
  22554. (overlay-put org-speedbar-restriction-lock-overlay
  22555. 'help-echo "Agendas are currently limited to this item.")
  22556. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22557. (defun org-speedbar-set-agenda-restriction ()
  22558. "Restrict future agenda commands to the location at point in speedbar.
  22559. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22560. (interactive)
  22561. (require 'org-agenda)
  22562. (let (p m tp np dir txt)
  22563. (cond
  22564. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22565. 'org-imenu t))
  22566. (setq m (get-text-property p 'org-imenu-marker))
  22567. (with-current-buffer (marker-buffer m)
  22568. (goto-char m)
  22569. (org-agenda-set-restriction-lock 'subtree)))
  22570. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22571. 'speedbar-function 'speedbar-find-file))
  22572. (setq tp (previous-single-property-change
  22573. (1+ p) 'speedbar-function)
  22574. np (next-single-property-change
  22575. tp 'speedbar-function)
  22576. dir (speedbar-line-directory)
  22577. txt (buffer-substring-no-properties (or tp (point-min))
  22578. (or np (point-max))))
  22579. (with-current-buffer (find-file-noselect
  22580. (let ((default-directory dir))
  22581. (expand-file-name txt)))
  22582. (unless (derived-mode-p 'org-mode)
  22583. (user-error "Cannot restrict to non-Org-mode file"))
  22584. (org-agenda-set-restriction-lock 'file)))
  22585. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  22586. (move-overlay org-speedbar-restriction-lock-overlay
  22587. (point-at-bol) (point-at-eol))
  22588. (setq current-prefix-arg nil)
  22589. (org-agenda-maybe-redo)))
  22590. (defvar speedbar-file-key-map)
  22591. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22592. (eval-after-load "speedbar"
  22593. '(progn
  22594. (speedbar-add-supported-extension ".org")
  22595. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22596. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22597. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22598. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22599. (add-hook 'speedbar-visiting-tag-hook
  22600. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22601. ;;; Fixes and Hacks for problems with other packages
  22602. (defun org--flyspell-object-check-p (element)
  22603. "Non-nil when Flyspell can check object at point.
  22604. ELEMENT is the element at point."
  22605. (let ((object (save-excursion
  22606. (when (org-looking-at-p "\\>") (backward-char))
  22607. (org-element-context element))))
  22608. (case (org-element-type object)
  22609. ;; Prevent checks in links due to keybinding conflict with
  22610. ;; Flyspell.
  22611. ((code entity export-snippet inline-babel-call
  22612. inline-src-block line-break latex-fragment link macro
  22613. statistics-cookie target timestamp verbatim)
  22614. nil)
  22615. (footnote-reference
  22616. ;; Only in inline footnotes, within the definition.
  22617. (and (eq (org-element-property :type object) 'inline)
  22618. (< (save-excursion
  22619. (goto-char (org-element-property :begin object))
  22620. (search-forward ":" nil t 2))
  22621. (point))))
  22622. (otherwise t))))
  22623. (defun org-mode-flyspell-verify ()
  22624. "Function used for `flyspell-generic-check-word-predicate'."
  22625. (if (org-at-heading-p)
  22626. ;; At a headline or an inlinetask, check title only. This is
  22627. ;; faster than relying on `org-element-at-point'.
  22628. (and (save-excursion (beginning-of-line)
  22629. (and (let ((case-fold-search t))
  22630. (not (looking-at "\\*+ END[ \t]*$")))
  22631. (looking-at org-complex-heading-regexp)))
  22632. (match-beginning 4)
  22633. (>= (point) (match-beginning 4))
  22634. (or (not (match-beginning 5))
  22635. (< (point) (match-beginning 5))))
  22636. (let* ((element (org-element-at-point))
  22637. (post-affiliated (org-element-property :post-affiliated element)))
  22638. (cond
  22639. ;; Ignore checks in all affiliated keywords but captions.
  22640. ((< (point) post-affiliated)
  22641. (and (save-excursion
  22642. (beginning-of-line)
  22643. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  22644. (> (point) (match-end 0))
  22645. (org--flyspell-object-check-p element)))
  22646. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  22647. ((and org-log-into-drawer
  22648. (let ((log (or (org-string-nw-p org-log-into-drawer) "LOGBOOK"))
  22649. (parent element))
  22650. (while (and parent (not (eq (org-element-type parent) 'drawer)))
  22651. (setq parent (org-element-property :parent parent)))
  22652. (and parent
  22653. (eq (compare-strings
  22654. log nil nil
  22655. (org-element-property :drawer-name parent) nil nil t)
  22656. t))))
  22657. nil)
  22658. (t
  22659. (case (org-element-type element)
  22660. ((comment quote-section) t)
  22661. (comment-block
  22662. ;; Allow checks between block markers, not on them.
  22663. (and (> (line-beginning-position) post-affiliated)
  22664. (save-excursion
  22665. (end-of-line)
  22666. (skip-chars-forward " \r\t\n")
  22667. (< (point) (org-element-property :end element)))))
  22668. ;; Arbitrary list of keywords where checks are meaningful.
  22669. ;; Make sure point is on the value part of the element.
  22670. (keyword
  22671. (and (member (org-element-property :key element)
  22672. '("DESCRIPTION" "TITLE"))
  22673. (< (save-excursion
  22674. (beginning-of-line) (search-forward ":") (point))
  22675. (point))))
  22676. ;; Check is globally allowed in paragraphs verse blocks and
  22677. ;; table rows (after affiliated keywords) but some objects
  22678. ;; must not be affected.
  22679. ((paragraph table-row verse-block)
  22680. (let ((cbeg (org-element-property :contents-begin element))
  22681. (cend (org-element-property :contents-end element)))
  22682. (and cbeg (>= (point) cbeg) (< (point) cend)
  22683. (org--flyspell-object-check-p element))))))))))
  22684. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  22685. (defun org-remove-flyspell-overlays-in (beg end)
  22686. "Remove flyspell overlays in region."
  22687. (and (org-bound-and-true-p flyspell-mode)
  22688. (fboundp 'flyspell-delete-region-overlays)
  22689. (flyspell-delete-region-overlays beg end)))
  22690. (defvar flyspell-delayed-commands)
  22691. (eval-after-load "flyspell"
  22692. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  22693. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  22694. (eval-after-load "bookmark"
  22695. '(if (boundp 'bookmark-after-jump-hook)
  22696. ;; We can use the hook
  22697. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22698. ;; Hook not available, use advice
  22699. (defadvice bookmark-jump (after org-make-visible activate)
  22700. "Make the position visible."
  22701. (org-bookmark-jump-unhide))))
  22702. ;; Make sure saveplace shows the location if it was hidden
  22703. (eval-after-load "saveplace"
  22704. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  22705. "Make the position visible."
  22706. (org-bookmark-jump-unhide)))
  22707. ;; Make sure ecb shows the location if it was hidden
  22708. (eval-after-load "ecb"
  22709. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  22710. "Make hierarchy visible when jumping into location from ECB tree buffer."
  22711. (if (derived-mode-p 'org-mode)
  22712. (org-show-context))))
  22713. (defun org-bookmark-jump-unhide ()
  22714. "Unhide the current position, to show the bookmark location."
  22715. (and (derived-mode-p 'org-mode)
  22716. (or (outline-invisible-p)
  22717. (save-excursion (goto-char (max (point-min) (1- (point))))
  22718. (outline-invisible-p)))
  22719. (org-show-context 'bookmark-jump)))
  22720. (defun org-mark-jump-unhide ()
  22721. "Make the point visible with `org-show-context' after jumping to the mark."
  22722. (when (and (derived-mode-p 'org-mode)
  22723. (outline-invisible-p))
  22724. (org-show-context 'mark-goto)))
  22725. (eval-after-load "simple"
  22726. '(defadvice pop-to-mark-command (after org-make-visible activate)
  22727. "Make the point visible with `org-show-context'."
  22728. (org-mark-jump-unhide)))
  22729. (eval-after-load "simple"
  22730. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  22731. "Make the point visible with `org-show-context'."
  22732. (org-mark-jump-unhide)))
  22733. (eval-after-load "simple"
  22734. '(defadvice pop-global-mark (after org-make-visible activate)
  22735. "Make the point visible with `org-show-context'."
  22736. (org-mark-jump-unhide)))
  22737. ;; Make session.el ignore our circular variable
  22738. (defvar session-globals-exclude)
  22739. (eval-after-load "session"
  22740. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22741. ;;;; Finish up
  22742. (provide 'org)
  22743. (run-hooks 'org-load-hook)
  22744. ;;; org.el ends here