org.el 843 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. (load "org-loaddefs.el" t t)
  74. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  75. ;; some places -- e.g. see the macro org-with-limited-levels.
  76. ;;
  77. ;; In Org buffers, the value of `outline-regexp' is that of
  78. ;; `org-outline-regexp'. The only function still directly relying on
  79. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  80. ;; job when `orgstruct-mode' is active.
  81. (defvar org-outline-regexp "\\*+ "
  82. "Regexp to match Org headlines.")
  83. (defvar org-outline-regexp-bol "^\\*+ "
  84. "Regexp to match Org headlines.
  85. This is similar to `org-outline-regexp' but additionally makes
  86. sure that we are at the beginning of the line.")
  87. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  88. "Matches an headline, putting stars and text into groups.
  89. Stars are put in group 1 and the trimmed body in group 2.")
  90. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  91. (when (fboundp 'defvaralias)
  92. (unless (boundp 'calendar-view-holidays-initially-flag)
  93. (defvaralias 'calendar-view-holidays-initially-flag
  94. 'view-calendar-holidays-initially))
  95. (unless (boundp 'calendar-view-diary-initially-flag)
  96. (defvaralias 'calendar-view-diary-initially-flag
  97. 'view-diary-entries-initially))
  98. (unless (boundp 'diary-fancy-buffer)
  99. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  100. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  101. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  102. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  103. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  104. (declare-function org-at-clock-log-p "org-clock" ())
  105. (declare-function org-clock-timestamps-up "org-clock" ())
  106. (declare-function org-clock-timestamps-down "org-clock" ())
  107. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  108. (declare-function orgtbl-mode "org-table" (&optional arg))
  109. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  110. (declare-function org-beamer-mode "org-beamer" ())
  111. (declare-function org-table-edit-field "org-table" (arg))
  112. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  113. (declare-function org-id-get-create "org-id" (&optional force))
  114. (declare-function org-id-find-id-file "org-id" (id))
  115. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  116. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  117. (declare-function org-table-align "org-table" ())
  118. (declare-function org-table-paste-rectangle "org-table" ())
  119. (declare-function org-table-maybe-eval-formula "org-table" ())
  120. (declare-function org-table-maybe-recalculate-line "org-table" ())
  121. ;; load languages based on value of `org-babel-load-languages'
  122. (defvar org-babel-load-languages)
  123. ;;;###autoload
  124. (defun org-babel-do-load-languages (sym value)
  125. "Load the languages defined in `org-babel-load-languages'."
  126. (set-default sym value)
  127. (mapc (lambda (pair)
  128. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  129. (if active
  130. (progn
  131. (require (intern (concat "ob-" lang))))
  132. (progn
  133. (funcall 'fmakunbound
  134. (intern (concat "org-babel-execute:" lang)))
  135. (funcall 'fmakunbound
  136. (intern (concat "org-babel-expand-body:" lang)))))))
  137. org-babel-load-languages))
  138. (defcustom org-babel-load-languages '((emacs-lisp . t))
  139. "Languages which can be evaluated in Org-mode buffers.
  140. This list can be used to load support for any of the languages
  141. below, note that each language will depend on a different set of
  142. system executables and/or Emacs modes. When a language is
  143. \"loaded\", then code blocks in that language can be evaluated
  144. with `org-babel-execute-src-block' bound by default to C-c
  145. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  146. be set to remove code block evaluation from the C-c C-c
  147. keybinding. By default only Emacs Lisp (which has no
  148. requirements) is loaded."
  149. :group 'org-babel
  150. :set 'org-babel-do-load-languages
  151. :version "24.1"
  152. :type '(alist :tag "Babel Languages"
  153. :key-type
  154. (choice
  155. (const :tag "Awk" awk)
  156. (const :tag "C" C)
  157. (const :tag "R" R)
  158. (const :tag "Asymptote" asymptote)
  159. (const :tag "Calc" calc)
  160. (const :tag "Clojure" clojure)
  161. (const :tag "CSS" css)
  162. (const :tag "Ditaa" ditaa)
  163. (const :tag "Dot" dot)
  164. (const :tag "Emacs Lisp" emacs-lisp)
  165. (const :tag "Fortran" fortran)
  166. (const :tag "Gnuplot" gnuplot)
  167. (const :tag "Haskell" haskell)
  168. (const :tag "IO" io)
  169. (const :tag "Java" java)
  170. (const :tag "Javascript" js)
  171. (const :tag "LaTeX" latex)
  172. (const :tag "Ledger" ledger)
  173. (const :tag "Lilypond" lilypond)
  174. (const :tag "Lisp" lisp)
  175. (const :tag "Maxima" maxima)
  176. (const :tag "Matlab" matlab)
  177. (const :tag "Mscgen" mscgen)
  178. (const :tag "Ocaml" ocaml)
  179. (const :tag "Octave" octave)
  180. (const :tag "Org" org)
  181. (const :tag "Perl" perl)
  182. (const :tag "Pico Lisp" picolisp)
  183. (const :tag "PlantUML" plantuml)
  184. (const :tag "Python" python)
  185. (const :tag "Ruby" ruby)
  186. (const :tag "Sass" sass)
  187. (const :tag "Scala" scala)
  188. (const :tag "Scheme" scheme)
  189. (const :tag "Screen" screen)
  190. (const :tag "Shell Script" sh)
  191. (const :tag "Shen" shen)
  192. (const :tag "Sql" sql)
  193. (const :tag "Sqlite" sqlite))
  194. :value-type (boolean :tag "Activate" :value t)))
  195. ;;;; Customization variables
  196. (defcustom org-clone-delete-id nil
  197. "Remove ID property of clones of a subtree.
  198. When non-nil, clones of a subtree don't inherit the ID property.
  199. Otherwise they inherit the ID property with a new unique
  200. identifier."
  201. :type 'boolean
  202. :version "24.1"
  203. :group 'org-id)
  204. ;;; Version
  205. (require 'org-compat)
  206. (org-check-version)
  207. ;;;###autoload
  208. (defun org-version (&optional here full message)
  209. "Show the org-mode version in the echo area.
  210. With prefix argument HERE, insert it at point.
  211. When FULL is non-nil, use a verbose version string.
  212. When MESSAGE is non-nil, display a message with the version."
  213. (interactive "P")
  214. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  215. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs.el")))
  216. (org-trash (or
  217. (and (fboundp 'org-release) (fboundp 'org-git-version))
  218. (load (concat org-dir "org-version.el")
  219. 'noerror 'nomessage 'nosuffix)))
  220. (org-version (org-release))
  221. (git-version (org-git-version))
  222. (version (format "Org-mode version %s (%s @ %s)"
  223. org-version
  224. git-version
  225. (if org-install-dir
  226. (if (string= org-dir org-install-dir)
  227. org-install-dir
  228. (concat "mixed installation! " org-install-dir " and " org-dir))
  229. "org-loaddefs.el can not be found!")))
  230. (_version (if full version org-version)))
  231. (if (org-called-interactively-p 'interactive)
  232. (if here
  233. (insert version)
  234. (message version))
  235. (if message (message _version))
  236. _version)))
  237. (defconst org-version (org-version))
  238. ;;; Compatibility constants
  239. ;;; The custom variables
  240. (defgroup org nil
  241. "Outline-based notes management and organizer."
  242. :tag "Org"
  243. :group 'outlines
  244. :group 'calendar)
  245. (defcustom org-mode-hook nil
  246. "Mode hook for Org-mode, run after the mode was turned on."
  247. :group 'org
  248. :type 'hook)
  249. (defcustom org-load-hook nil
  250. "Hook that is run after org.el has been loaded."
  251. :group 'org
  252. :type 'hook)
  253. (defcustom org-log-buffer-setup-hook nil
  254. "Hook that is run after an Org log buffer is created."
  255. :group 'org
  256. :version "24.1"
  257. :type 'hook)
  258. (defvar org-modules) ; defined below
  259. (defvar org-modules-loaded nil
  260. "Have the modules been loaded already?")
  261. (defun org-load-modules-maybe (&optional force)
  262. "Load all extensions listed in `org-modules'."
  263. (when (or force (not org-modules-loaded))
  264. (mapc (lambda (ext)
  265. (condition-case nil (require ext)
  266. (error (message "Problems while trying to load feature `%s'" ext))))
  267. org-modules)
  268. (setq org-modules-loaded t)))
  269. (defun org-set-modules (var value)
  270. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  271. (set var value)
  272. (when (featurep 'org)
  273. (org-load-modules-maybe 'force)))
  274. (when (org-bound-and-true-p org-modules)
  275. (let ((a (member 'org-infojs org-modules)))
  276. (and a (setcar a 'org-jsinfo))))
  277. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  278. "Modules that should always be loaded together with org.el.
  279. If a description starts with <C>, the file is not part of Emacs
  280. and loading it will require that you have downloaded and properly installed
  281. the org-mode distribution.
  282. You can also use this system to load external packages (i.e. neither Org
  283. core modules, nor modules from the CONTRIB directory). Just add symbols
  284. to the end of the list. If the package is called org-xyz.el, then you need
  285. to add the symbol `xyz', and the package must have a call to
  286. (provide 'org-xyz)"
  287. :group 'org
  288. :set 'org-set-modules
  289. :type
  290. '(set :greedy t
  291. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  292. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  293. (const :tag " crypt: Encryption of subtrees" org-crypt)
  294. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  295. (const :tag " docview: Links to doc-view buffers" org-docview)
  296. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  297. (const :tag " id: Global IDs for identifying entries" org-id)
  298. (const :tag " info: Links to Info nodes" org-info)
  299. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  300. (const :tag " habit: Track your consistency with habits" org-habit)
  301. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  302. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  303. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  304. (const :tag " mew Links to Mew folders/messages" org-mew)
  305. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  306. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  307. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  308. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  309. (const :tag " vm: Links to VM folders/messages" org-vm)
  310. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  311. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  312. (const :tag " mouse: Additional mouse support" org-mouse)
  313. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  314. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  315. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  316. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  317. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  318. (const :tag "C collector: Collect properties into tables" org-collector)
  319. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  320. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  321. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  322. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  323. (const :tag "C eval: Include command output as text" org-eval)
  324. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  325. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  326. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  327. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  328. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  329. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  330. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  331. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  332. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  333. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  334. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  335. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  336. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  337. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  338. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  339. (const :tag "C registry: A registry for Org-mode links" org-registry)
  340. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  341. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  342. (const :tag "C secretary: Team management with org-mode" org-secretary)
  343. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  344. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  345. (const :tag "C track: Keep up with Org-mode development" org-track)
  346. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  347. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  348. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  349. (defcustom org-support-shift-select nil
  350. "Non-nil means make shift-cursor commands select text when possible.
  351. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  352. start selecting a region, or enlarge regions started in this way.
  353. In Org-mode, in special contexts, these same keys are used for
  354. other purposes, important enough to compete with shift selection.
  355. Org tries to balance these needs by supporting `shift-select-mode'
  356. outside these special contexts, under control of this variable.
  357. The default of this variable is nil, to avoid confusing behavior. Shifted
  358. cursor keys will then execute Org commands in the following contexts:
  359. - on a headline, changing TODO state (left/right) and priority (up/down)
  360. - on a time stamp, changing the time
  361. - in a plain list item, changing the bullet type
  362. - in a property definition line, switching between allowed values
  363. - in the BEGIN line of a clock table (changing the time block).
  364. Outside these contexts, the commands will throw an error.
  365. When this variable is t and the cursor is not in a special
  366. context, Org-mode will support shift-selection for making and
  367. enlarging regions. To make this more effective, the bullet
  368. cycling will no longer happen anywhere in an item line, but only
  369. if the cursor is exactly on the bullet.
  370. If you set this variable to the symbol `always', then the keys
  371. will not be special in headlines, property lines, and item lines,
  372. to make shift selection work there as well. If this is what you
  373. want, you can use the following alternative commands: `C-c C-t'
  374. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  375. can be used to switch TODO sets, `C-c -' to cycle item bullet
  376. types, and properties can be edited by hand or in column view.
  377. However, when the cursor is on a timestamp, shift-cursor commands
  378. will still edit the time stamp - this is just too good to give up.
  379. XEmacs user should have this variable set to nil, because
  380. `shift-select-mode' is in Emacs 23 or later only."
  381. :group 'org
  382. :type '(choice
  383. (const :tag "Never" nil)
  384. (const :tag "When outside special context" t)
  385. (const :tag "Everywhere except timestamps" always)))
  386. (defcustom org-loop-over-headlines-in-active-region nil
  387. "Shall some commands act upon headlines in the active region?
  388. When set to `t', some commands will be performed in all headlines
  389. within the active region.
  390. When set to `start-level', some commands will be performed in all
  391. headlines within the active region, provided that these headlines
  392. are of the same level than the first one.
  393. When set to a string, those commands will be performed on the
  394. matching headlines within the active region. Such string must be
  395. a tags/property/todo match as it is used in the agenda tags view.
  396. The list of commands is: `org-schedule', `org-deadline',
  397. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  398. `org-archive-to-archive-sibling'. The archiving commands skip
  399. already archived entries."
  400. :type '(choice (const :tag "Don't loop" nil)
  401. (const :tag "All headlines in active region" t)
  402. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  403. (string :tag "Tags/Property/Todo matcher"))
  404. :version "24.1"
  405. :group 'org-todo
  406. :group 'org-archive)
  407. (defgroup org-startup nil
  408. "Options concerning startup of Org-mode."
  409. :tag "Org Startup"
  410. :group 'org)
  411. (defcustom org-startup-folded t
  412. "Non-nil means entering Org-mode will switch to OVERVIEW.
  413. This can also be configured on a per-file basis by adding one of
  414. the following lines anywhere in the buffer:
  415. #+STARTUP: fold (or `overview', this is equivalent)
  416. #+STARTUP: nofold (or `showall', this is equivalent)
  417. #+STARTUP: content
  418. #+STARTUP: showeverything"
  419. :group 'org-startup
  420. :type '(choice
  421. (const :tag "nofold: show all" nil)
  422. (const :tag "fold: overview" t)
  423. (const :tag "content: all headlines" content)
  424. (const :tag "show everything, even drawers" showeverything)))
  425. (defcustom org-startup-truncated t
  426. "Non-nil means entering Org-mode will set `truncate-lines'.
  427. This is useful since some lines containing links can be very long and
  428. uninteresting. Also tables look terrible when wrapped."
  429. :group 'org-startup
  430. :type 'boolean)
  431. (defcustom org-startup-indented nil
  432. "Non-nil means turn on `org-indent-mode' on startup.
  433. This can also be configured on a per-file basis by adding one of
  434. the following lines anywhere in the buffer:
  435. #+STARTUP: indent
  436. #+STARTUP: noindent"
  437. :group 'org-structure
  438. :type '(choice
  439. (const :tag "Not" nil)
  440. (const :tag "Globally (slow on startup in large files)" t)))
  441. (defcustom org-use-sub-superscripts t
  442. "Non-nil means interpret \"_\" and \"^\" for export.
  443. When this option is turned on, you can use TeX-like syntax for sub- and
  444. superscripts. Several characters after \"_\" or \"^\" will be
  445. considered as a single item - so grouping with {} is normally not
  446. needed. For example, the following things will be parsed as single
  447. sub- or superscripts.
  448. 10^24 or 10^tau several digits will be considered 1 item.
  449. 10^-12 or 10^-tau a leading sign with digits or a word
  450. x^2-y^3 will be read as x^2 - y^3, because items are
  451. terminated by almost any nonword/nondigit char.
  452. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  453. Still, ambiguity is possible - so when in doubt use {} to enclose the
  454. sub/superscript. If you set this variable to the symbol `{}',
  455. the braces are *required* in order to trigger interpretations as
  456. sub/superscript. This can be helpful in documents that need \"_\"
  457. frequently in plain text.
  458. Not all export backends support this, but HTML does.
  459. This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
  460. :group 'org-startup
  461. :group 'org-export-translation
  462. :version "24.1"
  463. :type '(choice
  464. (const :tag "Always interpret" t)
  465. (const :tag "Only with braces" {})
  466. (const :tag "Never interpret" nil)))
  467. (if (fboundp 'defvaralias)
  468. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  469. (defcustom org-startup-with-beamer-mode nil
  470. "Non-nil means turn on `org-beamer-mode' on startup.
  471. This can also be configured on a per-file basis by adding one of
  472. the following lines anywhere in the buffer:
  473. #+STARTUP: beamer"
  474. :group 'org-startup
  475. :version "24.1"
  476. :type 'boolean)
  477. (defcustom org-startup-align-all-tables nil
  478. "Non-nil means align all tables when visiting a file.
  479. This is useful when the column width in tables is forced with <N> cookies
  480. in table fields. Such tables will look correct only after the first re-align.
  481. This can also be configured on a per-file basis by adding one of
  482. the following lines anywhere in the buffer:
  483. #+STARTUP: align
  484. #+STARTUP: noalign"
  485. :group 'org-startup
  486. :type 'boolean)
  487. (defcustom org-startup-with-inline-images nil
  488. "Non-nil means show inline images when loading a new Org file.
  489. This can also be configured on a per-file basis by adding one of
  490. the following lines anywhere in the buffer:
  491. #+STARTUP: inlineimages
  492. #+STARTUP: noinlineimages"
  493. :group 'org-startup
  494. :version "24.1"
  495. :type 'boolean)
  496. (defcustom org-insert-mode-line-in-empty-file nil
  497. "Non-nil means insert the first line setting Org-mode in empty files.
  498. When the function `org-mode' is called interactively in an empty file, this
  499. normally means that the file name does not automatically trigger Org-mode.
  500. To ensure that the file will always be in Org-mode in the future, a
  501. line enforcing Org-mode will be inserted into the buffer, if this option
  502. has been set."
  503. :group 'org-startup
  504. :type 'boolean)
  505. (defcustom org-replace-disputed-keys nil
  506. "Non-nil means use alternative key bindings for some keys.
  507. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  508. These keys are also used by other packages like shift-selection-mode'
  509. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  510. If you want to use Org-mode together with one of these other modes,
  511. or more generally if you would like to move some Org-mode commands to
  512. other keys, set this variable and configure the keys with the variable
  513. `org-disputed-keys'.
  514. This option is only relevant at load-time of Org-mode, and must be set
  515. *before* org.el is loaded. Changing it requires a restart of Emacs to
  516. become effective."
  517. :group 'org-startup
  518. :type 'boolean)
  519. (defcustom org-use-extra-keys nil
  520. "Non-nil means use extra key sequence definitions for certain commands.
  521. This happens automatically if you run XEmacs or if `window-system'
  522. is nil. This variable lets you do the same manually. You must
  523. set it before loading org.
  524. Example: on Carbon Emacs 22 running graphically, with an external
  525. keyboard on a Powerbook, the default way of setting M-left might
  526. not work for either Alt or ESC. Setting this variable will make
  527. it work for ESC."
  528. :group 'org-startup
  529. :type 'boolean)
  530. (if (fboundp 'defvaralias)
  531. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  532. (defcustom org-disputed-keys
  533. '(([(shift up)] . [(meta p)])
  534. ([(shift down)] . [(meta n)])
  535. ([(shift left)] . [(meta -)])
  536. ([(shift right)] . [(meta +)])
  537. ([(control shift right)] . [(meta shift +)])
  538. ([(control shift left)] . [(meta shift -)]))
  539. "Keys for which Org-mode and other modes compete.
  540. This is an alist, cars are the default keys, second element specifies
  541. the alternative to use when `org-replace-disputed-keys' is t.
  542. Keys can be specified in any syntax supported by `define-key'.
  543. The value of this option takes effect only at Org-mode's startup,
  544. therefore you'll have to restart Emacs to apply it after changing."
  545. :group 'org-startup
  546. :type 'alist)
  547. (defun org-key (key)
  548. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  549. Or return the original if not disputed.
  550. Also apply the translations defined in `org-xemacs-key-equivalents'."
  551. (when org-replace-disputed-keys
  552. (let* ((nkey (key-description key))
  553. (x (org-find-if (lambda (x)
  554. (equal (key-description (car x)) nkey))
  555. org-disputed-keys)))
  556. (setq key (if x (cdr x) key))))
  557. (when (featurep 'xemacs)
  558. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  559. key)
  560. (defun org-find-if (predicate seq)
  561. (catch 'exit
  562. (while seq
  563. (if (funcall predicate (car seq))
  564. (throw 'exit (car seq))
  565. (pop seq)))))
  566. (defun org-defkey (keymap key def)
  567. "Define a key, possibly translated, as returned by `org-key'."
  568. (define-key keymap (org-key key) def))
  569. (defcustom org-ellipsis nil
  570. "The ellipsis to use in the Org-mode outline.
  571. When nil, just use the standard three dots. When a string, use that instead,
  572. When a face, use the standard 3 dots, but with the specified face.
  573. The change affects only Org-mode (which will then use its own display table).
  574. Changing this requires executing `M-x org-mode' in a buffer to become
  575. effective."
  576. :group 'org-startup
  577. :type '(choice (const :tag "Default" nil)
  578. (face :tag "Face" :value org-warning)
  579. (string :tag "String" :value "...#")))
  580. (defvar org-display-table nil
  581. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  582. (defgroup org-keywords nil
  583. "Keywords in Org-mode."
  584. :tag "Org Keywords"
  585. :group 'org)
  586. (defcustom org-deadline-string "DEADLINE:"
  587. "String to mark deadline entries.
  588. A deadline is this string, followed by a time stamp. Should be a word,
  589. terminated by a colon. You can insert a schedule keyword and
  590. a timestamp with \\[org-deadline].
  591. Changes become only effective after restarting Emacs."
  592. :group 'org-keywords
  593. :type 'string)
  594. (defcustom org-scheduled-string "SCHEDULED:"
  595. "String to mark scheduled TODO entries.
  596. A schedule is this string, followed by a time stamp. Should be a word,
  597. terminated by a colon. You can insert a schedule keyword and
  598. a timestamp with \\[org-schedule].
  599. Changes become only effective after restarting Emacs."
  600. :group 'org-keywords
  601. :type 'string)
  602. (defcustom org-closed-string "CLOSED:"
  603. "String used as the prefix for timestamps logging closing a TODO entry."
  604. :group 'org-keywords
  605. :type 'string)
  606. (defcustom org-clock-string "CLOCK:"
  607. "String used as prefix for timestamps clocking work hours on an item."
  608. :group 'org-keywords
  609. :type 'string)
  610. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  611. org-scheduled-string "\\|"
  612. org-deadline-string "\\|"
  613. org-closed-string "\\|"
  614. org-clock-string "\\)")
  615. "Matches a line with planning or clock info.")
  616. (defcustom org-comment-string "COMMENT"
  617. "Entries starting with this keyword will never be exported.
  618. An entry can be toggled between COMMENT and normal with
  619. \\[org-toggle-comment].
  620. Changes become only effective after restarting Emacs."
  621. :group 'org-keywords
  622. :type 'string)
  623. (defcustom org-quote-string "QUOTE"
  624. "Entries starting with this keyword will be exported in fixed-width font.
  625. Quoting applies only to the text in the entry following the headline, and does
  626. not extend beyond the next headline, even if that is lower level.
  627. An entry can be toggled between QUOTE and normal with
  628. \\[org-toggle-fixed-width-section]."
  629. :group 'org-keywords
  630. :type 'string)
  631. (defconst org-repeat-re
  632. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  633. "Regular expression for specifying repeated events.
  634. After a match, group 1 contains the repeat expression.")
  635. (defgroup org-structure nil
  636. "Options concerning the general structure of Org-mode files."
  637. :tag "Org Structure"
  638. :group 'org)
  639. (defgroup org-reveal-location nil
  640. "Options about how to make context of a location visible."
  641. :tag "Org Reveal Location"
  642. :group 'org-structure)
  643. (defconst org-context-choice
  644. '(choice
  645. (const :tag "Always" t)
  646. (const :tag "Never" nil)
  647. (repeat :greedy t :tag "Individual contexts"
  648. (cons
  649. (choice :tag "Context"
  650. (const agenda)
  651. (const org-goto)
  652. (const occur-tree)
  653. (const tags-tree)
  654. (const link-search)
  655. (const mark-goto)
  656. (const bookmark-jump)
  657. (const isearch)
  658. (const default))
  659. (boolean))))
  660. "Contexts for the reveal options.")
  661. (defcustom org-show-hierarchy-above '((default . t))
  662. "Non-nil means show full hierarchy when revealing a location.
  663. Org-mode often shows locations in an org-mode file which might have
  664. been invisible before. When this is set, the hierarchy of headings
  665. above the exposed location is shown.
  666. Turning this off for example for sparse trees makes them very compact.
  667. Instead of t, this can also be an alist specifying this option for different
  668. contexts. Valid contexts are
  669. agenda when exposing an entry from the agenda
  670. org-goto when using the command `org-goto' on key C-c C-j
  671. occur-tree when using the command `org-occur' on key C-c /
  672. tags-tree when constructing a sparse tree based on tags matches
  673. link-search when exposing search matches associated with a link
  674. mark-goto when exposing the jump goal of a mark
  675. bookmark-jump when exposing a bookmark location
  676. isearch when exiting from an incremental search
  677. default default for all contexts not set explicitly"
  678. :group 'org-reveal-location
  679. :type org-context-choice)
  680. (defcustom org-show-following-heading '((default . nil))
  681. "Non-nil means show following heading when revealing a location.
  682. Org-mode often shows locations in an org-mode file which might have
  683. been invisible before. When this is set, the heading following the
  684. match is shown.
  685. Turning this off for example for sparse trees makes them very compact,
  686. but makes it harder to edit the location of the match. In such a case,
  687. use the command \\[org-reveal] to show more context.
  688. Instead of t, this can also be an alist specifying this option for different
  689. contexts. See `org-show-hierarchy-above' for valid contexts."
  690. :group 'org-reveal-location
  691. :type org-context-choice)
  692. (defcustom org-show-siblings '((default . nil) (isearch t))
  693. "Non-nil means show all sibling heading when revealing a location.
  694. Org-mode often shows locations in an org-mode file which might have
  695. been invisible before. When this is set, the sibling of the current entry
  696. heading are all made visible. If `org-show-hierarchy-above' is t,
  697. the same happens on each level of the hierarchy above the current entry.
  698. By default this is on for the isearch context, off for all other contexts.
  699. Turning this off for example for sparse trees makes them very compact,
  700. but makes it harder to edit the location of the match. In such a case,
  701. use the command \\[org-reveal] to show more context.
  702. Instead of t, this can also be an alist specifying this option for different
  703. contexts. See `org-show-hierarchy-above' for valid contexts."
  704. :group 'org-reveal-location
  705. :type org-context-choice)
  706. (defcustom org-show-entry-below '((default . nil))
  707. "Non-nil means show the entry below a headline when revealing a location.
  708. Org-mode often shows locations in an org-mode file which might have
  709. been invisible before. When this is set, the text below the headline that is
  710. exposed is also shown.
  711. By default this is off for all contexts.
  712. Instead of t, this can also be an alist specifying this option for different
  713. contexts. See `org-show-hierarchy-above' for valid contexts."
  714. :group 'org-reveal-location
  715. :type org-context-choice)
  716. (defcustom org-indirect-buffer-display 'other-window
  717. "How should indirect tree buffers be displayed?
  718. This applies to indirect buffers created with the commands
  719. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  720. Valid values are:
  721. current-window Display in the current window
  722. other-window Just display in another window.
  723. dedicated-frame Create one new frame, and re-use it each time.
  724. new-frame Make a new frame each time. Note that in this case
  725. previously-made indirect buffers are kept, and you need to
  726. kill these buffers yourself."
  727. :group 'org-structure
  728. :group 'org-agenda-windows
  729. :type '(choice
  730. (const :tag "In current window" current-window)
  731. (const :tag "In current frame, other window" other-window)
  732. (const :tag "Each time a new frame" new-frame)
  733. (const :tag "One dedicated frame" dedicated-frame)))
  734. (defcustom org-use-speed-commands nil
  735. "Non-nil means activate single letter commands at beginning of a headline.
  736. This may also be a function to test for appropriate locations where speed
  737. commands should be active."
  738. :group 'org-structure
  739. :type '(choice
  740. (const :tag "Never" nil)
  741. (const :tag "At beginning of headline stars" t)
  742. (function)))
  743. (defcustom org-speed-commands-user nil
  744. "Alist of additional speed commands.
  745. This list will be checked before `org-speed-commands-default'
  746. when the variable `org-use-speed-commands' is non-nil
  747. and when the cursor is at the beginning of a headline.
  748. The car if each entry is a string with a single letter, which must
  749. be assigned to `self-insert-command' in the global map.
  750. The cdr is either a command to be called interactively, a function
  751. to be called, or a form to be evaluated.
  752. An entry that is just a list with a single string will be interpreted
  753. as a descriptive headline that will be added when listing the speed
  754. commands in the Help buffer using the `?' speed command."
  755. :group 'org-structure
  756. :type '(repeat :value ("k" . ignore)
  757. (choice :value ("k" . ignore)
  758. (list :tag "Descriptive Headline" (string :tag "Headline"))
  759. (cons :tag "Letter and Command"
  760. (string :tag "Command letter")
  761. (choice
  762. (function)
  763. (sexp))))))
  764. (defgroup org-cycle nil
  765. "Options concerning visibility cycling in Org-mode."
  766. :tag "Org Cycle"
  767. :group 'org-structure)
  768. (defcustom org-cycle-skip-children-state-if-no-children t
  769. "Non-nil means skip CHILDREN state in entries that don't have any."
  770. :group 'org-cycle
  771. :type 'boolean)
  772. (defcustom org-cycle-max-level nil
  773. "Maximum level which should still be subject to visibility cycling.
  774. Levels higher than this will, for cycling, be treated as text, not a headline.
  775. When `org-odd-levels-only' is set, a value of N in this variable actually
  776. means 2N-1 stars as the limiting headline.
  777. When nil, cycle all levels.
  778. Note that the limiting level of cycling is also influenced by
  779. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  780. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  781. than its value."
  782. :group 'org-cycle
  783. :type '(choice
  784. (const :tag "No limit" nil)
  785. (integer :tag "Maximum level")))
  786. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  787. "Names of drawers. Drawers are not opened by cycling on the headline above.
  788. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  789. this:
  790. :DRAWERNAME:
  791. .....
  792. :END:
  793. The drawer \"PROPERTIES\" is special for capturing properties through
  794. the property API.
  795. Drawers can be defined on the per-file basis with a line like:
  796. #+DRAWERS: HIDDEN STATE PROPERTIES"
  797. :group 'org-structure
  798. :group 'org-cycle
  799. :type '(repeat (string :tag "Drawer Name")))
  800. (defcustom org-hide-block-startup nil
  801. "Non-nil means entering Org-mode will fold all blocks.
  802. This can also be set in on a per-file basis with
  803. #+STARTUP: hideblocks
  804. #+STARTUP: showblocks"
  805. :group 'org-startup
  806. :group 'org-cycle
  807. :type 'boolean)
  808. (defcustom org-cycle-global-at-bob nil
  809. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  810. This makes it possible to do global cycling without having to use S-TAB or
  811. \\[universal-argument] TAB. For this special case to work, the first line
  812. of the buffer must not be a headline -- it may be empty or some other text.
  813. When used in this way, `org-cycle-hook' is disabled temporarily to make
  814. sure the cursor stays at the beginning of the buffer. When this option is
  815. nil, don't do anything special at the beginning of the buffer."
  816. :group 'org-cycle
  817. :type 'boolean)
  818. (defcustom org-cycle-level-after-item/entry-creation t
  819. "Non-nil means cycle entry level or item indentation in new empty entries.
  820. When the cursor is at the end of an empty headline, i.e with only stars
  821. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  822. and then all possible ancestor states, before returning to the original state.
  823. This makes data entry extremely fast: M-RET to create a new headline,
  824. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  825. When the cursor is at the end of an empty plain list item, one TAB will
  826. make it a subitem, two or more tabs will back up to make this an item
  827. higher up in the item hierarchy."
  828. :group 'org-cycle
  829. :type 'boolean)
  830. (defcustom org-cycle-emulate-tab t
  831. "Where should `org-cycle' emulate TAB.
  832. nil Never
  833. white Only in completely white lines
  834. whitestart Only at the beginning of lines, before the first non-white char
  835. t Everywhere except in headlines
  836. exc-hl-bol Everywhere except at the start of a headline
  837. If TAB is used in a place where it does not emulate TAB, the current subtree
  838. visibility is cycled."
  839. :group 'org-cycle
  840. :type '(choice (const :tag "Never" nil)
  841. (const :tag "Only in completely white lines" white)
  842. (const :tag "Before first char in a line" whitestart)
  843. (const :tag "Everywhere except in headlines" t)
  844. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  845. ))
  846. (defcustom org-cycle-separator-lines 2
  847. "Number of empty lines needed to keep an empty line between collapsed trees.
  848. If you leave an empty line between the end of a subtree and the following
  849. headline, this empty line is hidden when the subtree is folded.
  850. Org-mode will leave (exactly) one empty line visible if the number of
  851. empty lines is equal or larger to the number given in this variable.
  852. So the default 2 means at least 2 empty lines after the end of a subtree
  853. are needed to produce free space between a collapsed subtree and the
  854. following headline.
  855. If the number is negative, and the number of empty lines is at least -N,
  856. all empty lines are shown.
  857. Special case: when 0, never leave empty lines in collapsed view."
  858. :group 'org-cycle
  859. :type 'integer)
  860. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  861. (defcustom org-pre-cycle-hook nil
  862. "Hook that is run before visibility cycling is happening.
  863. The function(s) in this hook must accept a single argument which indicates
  864. the new state that will be set right after running this hook. The
  865. argument is a symbol. Before a global state change, it can have the values
  866. `overview', `content', or `all'. Before a local state change, it can have
  867. the values `folded', `children', or `subtree'."
  868. :group 'org-cycle
  869. :type 'hook)
  870. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  871. org-cycle-hide-drawers
  872. org-cycle-show-empty-lines
  873. org-optimize-window-after-visibility-change)
  874. "Hook that is run after `org-cycle' has changed the buffer visibility.
  875. The function(s) in this hook must accept a single argument which indicates
  876. the new state that was set by the most recent `org-cycle' command. The
  877. argument is a symbol. After a global state change, it can have the values
  878. `overview', `contents', or `all'. After a local state change, it can have
  879. the values `folded', `children', or `subtree'."
  880. :group 'org-cycle
  881. :type 'hook)
  882. (defgroup org-edit-structure nil
  883. "Options concerning structure editing in Org-mode."
  884. :tag "Org Edit Structure"
  885. :group 'org-structure)
  886. (defcustom org-odd-levels-only nil
  887. "Non-nil means skip even levels and only use odd levels for the outline.
  888. This has the effect that two stars are being added/taken away in
  889. promotion/demotion commands. It also influences how levels are
  890. handled by the exporters.
  891. Changing it requires restart of `font-lock-mode' to become effective
  892. for fontification also in regions already fontified.
  893. You may also set this on a per-file basis by adding one of the following
  894. lines to the buffer:
  895. #+STARTUP: odd
  896. #+STARTUP: oddeven"
  897. :group 'org-edit-structure
  898. :group 'org-appearance
  899. :type 'boolean)
  900. (defcustom org-adapt-indentation t
  901. "Non-nil means adapt indentation to outline node level.
  902. When this variable is set, Org assumes that you write outlines by
  903. indenting text in each node to align with the headline (after the stars).
  904. The following issues are influenced by this variable:
  905. - When this is set and the *entire* text in an entry is indented, the
  906. indentation is increased by one space in a demotion command, and
  907. decreased by one in a promotion command. If any line in the entry
  908. body starts with text at column 0, indentation is not changed at all.
  909. - Property drawers and planning information is inserted indented when
  910. this variable s set. When nil, they will not be indented.
  911. - TAB indents a line relative to context. The lines below a headline
  912. will be indented when this variable is set.
  913. Note that this is all about true indentation, by adding and removing
  914. space characters. See also `org-indent.el' which does level-dependent
  915. indentation in a virtual way, i.e. at display time in Emacs."
  916. :group 'org-edit-structure
  917. :type 'boolean)
  918. (defcustom org-special-ctrl-a/e nil
  919. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  920. When t, `C-a' will bring back the cursor to the beginning of the
  921. headline text, i.e. after the stars and after a possible TODO
  922. keyword. In an item, this will be the position after bullet and
  923. check-box, if any. When the cursor is already at that position,
  924. another `C-a' will bring it to the beginning of the line.
  925. `C-e' will jump to the end of the headline, ignoring the presence
  926. of tags in the headline. A second `C-e' will then jump to the
  927. true end of the line, after any tags. This also means that, when
  928. this variable is non-nil, `C-e' also will never jump beyond the
  929. end of the heading of a folded section, i.e. not after the
  930. ellipses.
  931. When set to the symbol `reversed', the first `C-a' or `C-e' works
  932. normally, going to the true line boundary first. Only a directly
  933. following, identical keypress will bring the cursor to the
  934. special positions.
  935. This may also be a cons cell where the behavior for `C-a' and
  936. `C-e' is set separately."
  937. :group 'org-edit-structure
  938. :type '(choice
  939. (const :tag "off" nil)
  940. (const :tag "on: after stars/bullet and before tags first" t)
  941. (const :tag "reversed: true line boundary first" reversed)
  942. (cons :tag "Set C-a and C-e separately"
  943. (choice :tag "Special C-a"
  944. (const :tag "off" nil)
  945. (const :tag "on: after stars/bullet first" t)
  946. (const :tag "reversed: before stars/bullet first" reversed))
  947. (choice :tag "Special C-e"
  948. (const :tag "off" nil)
  949. (const :tag "on: before tags first" t)
  950. (const :tag "reversed: after tags first" reversed)))))
  951. (if (fboundp 'defvaralias)
  952. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  953. (defcustom org-special-ctrl-k nil
  954. "Non-nil means `C-k' will behave specially in headlines.
  955. When nil, `C-k' will call the default `kill-line' command.
  956. When t, the following will happen while the cursor is in the headline:
  957. - When the cursor is at the beginning of a headline, kill the entire
  958. line and possible the folded subtree below the line.
  959. - When in the middle of the headline text, kill the headline up to the tags.
  960. - When after the headline text, kill the tags."
  961. :group 'org-edit-structure
  962. :type 'boolean)
  963. (defcustom org-ctrl-k-protect-subtree nil
  964. "Non-nil means, do not delete a hidden subtree with C-k.
  965. When set to the symbol `error', simply throw an error when C-k is
  966. used to kill (part-of) a headline that has hidden text behind it.
  967. Any other non-nil value will result in a query to the user, if it is
  968. OK to kill that hidden subtree. When nil, kill without remorse."
  969. :group 'org-edit-structure
  970. :version "24.1"
  971. :type '(choice
  972. (const :tag "Do not protect hidden subtrees" nil)
  973. (const :tag "Protect hidden subtrees with a security query" t)
  974. (const :tag "Never kill a hidden subtree with C-k" error)))
  975. (defcustom org-catch-invisible-edits nil
  976. "Check if in invisible region before inserting or deleting a character.
  977. Valid values are:
  978. nil Do not check, so just do invisible edits.
  979. error Throw an error and do nothing.
  980. show Make point visible, and do the requested edit.
  981. show-and-error Make point visible, then throw an error and abort the edit.
  982. smart Make point visible, and do insertion/deletion if it is
  983. adjacent to visible text and the change feels predictable.
  984. Never delete a previously invisible character or add in the
  985. middle or right after an invisible region. Basically, this
  986. allows insertion and backward-delete right before ellipses.
  987. FIXME: maybe in this case we should not even show?"
  988. :group 'org-edit-structure
  989. :version "24.1"
  990. :type '(choice
  991. (const :tag "Do not check" nil)
  992. (const :tag "Throw error when trying to edit" error)
  993. (const :tag "Unhide, but do not do the edit" show-and-error)
  994. (const :tag "Show invisible part and do the edit" show)
  995. (const :tag "Be smart and do the right thing" smart)))
  996. (defcustom org-yank-folded-subtrees t
  997. "Non-nil means when yanking subtrees, fold them.
  998. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  999. it starts with a heading and all other headings in it are either children
  1000. or siblings, then fold all the subtrees. However, do this only if no
  1001. text after the yank would be swallowed into a folded tree by this action."
  1002. :group 'org-edit-structure
  1003. :type 'boolean)
  1004. (defcustom org-yank-adjusted-subtrees nil
  1005. "Non-nil means when yanking subtrees, adjust the level.
  1006. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1007. this function for details."
  1008. :group 'org-edit-structure
  1009. :type 'boolean)
  1010. (defcustom org-M-RET-may-split-line '((default . t))
  1011. "Non-nil means M-RET will split the line at the cursor position.
  1012. When nil, it will go to the end of the line before making a
  1013. new line.
  1014. You may also set this option in a different way for different
  1015. contexts. Valid contexts are:
  1016. headline when creating a new headline
  1017. item when creating a new item
  1018. table in a table field
  1019. default the value to be used for all contexts not explicitly
  1020. customized"
  1021. :group 'org-structure
  1022. :group 'org-table
  1023. :type '(choice
  1024. (const :tag "Always" t)
  1025. (const :tag "Never" nil)
  1026. (repeat :greedy t :tag "Individual contexts"
  1027. (cons
  1028. (choice :tag "Context"
  1029. (const headline)
  1030. (const item)
  1031. (const table)
  1032. (const default))
  1033. (boolean)))))
  1034. (defcustom org-insert-heading-respect-content nil
  1035. "Non-nil means insert new headings after the current subtree.
  1036. When nil, the new heading is created directly after the current line.
  1037. The commands \\[org-insert-heading-respect-content] and
  1038. \\[org-insert-todo-heading-respect-content] turn this variable on
  1039. for the duration of the command."
  1040. :group 'org-structure
  1041. :type 'boolean)
  1042. (defcustom org-blank-before-new-entry '((heading . auto)
  1043. (plain-list-item . auto))
  1044. "Should `org-insert-heading' leave a blank line before new heading/item?
  1045. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1046. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1047. which case Org will look at the surrounding headings/items and try to
  1048. make an intelligent decision whether to insert a blank line or not.
  1049. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1050. set, the setting here is ignored and no empty line is inserted, to avoid
  1051. breaking the list structure."
  1052. :group 'org-edit-structure
  1053. :type '(list
  1054. (cons (const heading)
  1055. (choice (const :tag "Never" nil)
  1056. (const :tag "Always" t)
  1057. (const :tag "Auto" auto)))
  1058. (cons (const plain-list-item)
  1059. (choice (const :tag "Never" nil)
  1060. (const :tag "Always" t)
  1061. (const :tag "Auto" auto)))))
  1062. (defcustom org-insert-heading-hook nil
  1063. "Hook being run after inserting a new heading."
  1064. :group 'org-edit-structure
  1065. :type 'hook)
  1066. (defcustom org-enable-fixed-width-editor t
  1067. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1068. This currently only means they are never auto-wrapped.
  1069. When nil, such lines will be treated like ordinary lines.
  1070. See also the QUOTE keyword."
  1071. :group 'org-edit-structure
  1072. :type 'boolean)
  1073. (defcustom org-goto-auto-isearch t
  1074. "Non-nil means typing characters in `org-goto' starts incremental search.
  1075. When nil, you can use these keybindings to navigate the buffer:
  1076. q Quit the org-goto interface
  1077. n Go to the next visible heading
  1078. p Go to the previous visible heading
  1079. f Go one heading forward on same level
  1080. b Go one heading backward on same level
  1081. u Go one heading up"
  1082. :group 'org-edit-structure
  1083. :type 'boolean)
  1084. (defgroup org-sparse-trees nil
  1085. "Options concerning sparse trees in Org-mode."
  1086. :tag "Org Sparse Trees"
  1087. :group 'org-structure)
  1088. (defcustom org-highlight-sparse-tree-matches t
  1089. "Non-nil means highlight all matches that define a sparse tree.
  1090. The highlights will automatically disappear the next time the buffer is
  1091. changed by an edit command."
  1092. :group 'org-sparse-trees
  1093. :type 'boolean)
  1094. (defcustom org-remove-highlights-with-change t
  1095. "Non-nil means any change to the buffer will remove temporary highlights.
  1096. Such highlights are created by `org-occur' and `org-clock-display'.
  1097. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1098. The highlights created by `org-preview-latex-fragment' always need
  1099. `C-c C-c' to be removed."
  1100. :group 'org-sparse-trees
  1101. :group 'org-time
  1102. :type 'boolean)
  1103. (defcustom org-occur-hook '(org-first-headline-recenter)
  1104. "Hook that is run after `org-occur' has constructed a sparse tree.
  1105. This can be used to recenter the window to show as much of the structure
  1106. as possible."
  1107. :group 'org-sparse-trees
  1108. :type 'hook)
  1109. (defgroup org-imenu-and-speedbar nil
  1110. "Options concerning imenu and speedbar in Org-mode."
  1111. :tag "Org Imenu and Speedbar"
  1112. :group 'org-structure)
  1113. (defcustom org-imenu-depth 2
  1114. "The maximum level for Imenu access to Org-mode headlines.
  1115. This also applied for speedbar access."
  1116. :group 'org-imenu-and-speedbar
  1117. :type 'integer)
  1118. (defgroup org-table nil
  1119. "Options concerning tables in Org-mode."
  1120. :tag "Org Table"
  1121. :group 'org)
  1122. (defcustom org-enable-table-editor 'optimized
  1123. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1124. When nil, such lines will be treated like ordinary lines.
  1125. When equal to the symbol `optimized', the table editor will be optimized to
  1126. do the following:
  1127. - Automatic overwrite mode in front of whitespace in table fields.
  1128. This makes the structure of the table stay in tact as long as the edited
  1129. field does not exceed the column width.
  1130. - Minimize the number of realigns. Normally, the table is aligned each time
  1131. TAB or RET are pressed to move to another field. With optimization this
  1132. happens only if changes to a field might have changed the column width.
  1133. Optimization requires replacing the functions `self-insert-command',
  1134. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1135. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1136. very good at guessing when a re-align will be necessary, but you can always
  1137. force one with \\[org-ctrl-c-ctrl-c].
  1138. If you would like to use the optimized version in Org-mode, but the
  1139. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1140. This variable can be used to turn on and off the table editor during a session,
  1141. but in order to toggle optimization, a restart is required.
  1142. See also the variable `org-table-auto-blank-field'."
  1143. :group 'org-table
  1144. :type '(choice
  1145. (const :tag "off" nil)
  1146. (const :tag "on" t)
  1147. (const :tag "on, optimized" optimized)))
  1148. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1149. (version<= emacs-version "24.1"))
  1150. "Non-nil means cluster self-insert commands for undo when possible.
  1151. If this is set, then, like in the Emacs command loop, 20 consecutive
  1152. characters will be undone together.
  1153. This is configurable, because there is some impact on typing performance."
  1154. :group 'org-table
  1155. :type 'boolean)
  1156. (defcustom org-table-tab-recognizes-table.el t
  1157. "Non-nil means TAB will automatically notice a table.el table.
  1158. When it sees such a table, it moves point into it and - if necessary -
  1159. calls `table-recognize-table'."
  1160. :group 'org-table-editing
  1161. :type 'boolean)
  1162. (defgroup org-link nil
  1163. "Options concerning links in Org-mode."
  1164. :tag "Org Link"
  1165. :group 'org)
  1166. (defvar org-link-abbrev-alist-local nil
  1167. "Buffer-local version of `org-link-abbrev-alist', which see.
  1168. The value of this is taken from the #+LINK lines.")
  1169. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1170. (defcustom org-link-abbrev-alist nil
  1171. "Alist of link abbreviations.
  1172. The car of each element is a string, to be replaced at the start of a link.
  1173. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1174. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1175. [[linkkey:tag][description]]
  1176. The 'linkkey' must be a word word, starting with a letter, followed
  1177. by letters, numbers, '-' or '_'.
  1178. If REPLACE is a string, the tag will simply be appended to create the link.
  1179. If the string contains \"%s\", the tag will be inserted there. If the string
  1180. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1181. at that point (see the function `url-hexify-string'). If the string contains
  1182. the specifier \"%(my-function)\", then the custom function `my-function' will
  1183. be invoked: this function takes the tag as its only argument and must return
  1184. a string.
  1185. REPLACE may also be a function that will be called with the tag as the
  1186. only argument to create the link, which should be returned as a string.
  1187. See the manual for examples."
  1188. :group 'org-link
  1189. :type '(repeat
  1190. (cons
  1191. (string :tag "Protocol")
  1192. (choice
  1193. (string :tag "Format")
  1194. (function)))))
  1195. (defcustom org-descriptive-links t
  1196. "Non-nil means Org will display descriptive links.
  1197. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1198. \"Org Website\", hiding the link itself and just displaying its
  1199. description. When set to `nil', Org will display the full links
  1200. literally.
  1201. You can interactively set the value of this variable by calling
  1202. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1203. :group 'org-link
  1204. :type 'boolean)
  1205. (defcustom org-link-file-path-type 'adaptive
  1206. "How the path name in file links should be stored.
  1207. Valid values are:
  1208. relative Relative to the current directory, i.e. the directory of the file
  1209. into which the link is being inserted.
  1210. absolute Absolute path, if possible with ~ for home directory.
  1211. noabbrev Absolute path, no abbreviation of home directory.
  1212. adaptive Use relative path for files in the current directory and sub-
  1213. directories of it. For other files, use an absolute path."
  1214. :group 'org-link
  1215. :type '(choice
  1216. (const relative)
  1217. (const absolute)
  1218. (const noabbrev)
  1219. (const adaptive)))
  1220. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1221. "Types of links that should be activated in Org-mode files.
  1222. This is a list of symbols, each leading to the activation of a certain link
  1223. type. In principle, it does not hurt to turn on most link types - there may
  1224. be a small gain when turning off unused link types. The types are:
  1225. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1226. angle Links in angular brackets that may contain whitespace like
  1227. <bbdb:Carsten Dominik>.
  1228. plain Plain links in normal text, no whitespace, like http://google.com.
  1229. radio Text that is matched by a radio target, see manual for details.
  1230. tag Tag settings in a headline (link to tag search).
  1231. date Time stamps (link to calendar).
  1232. footnote Footnote labels.
  1233. Changing this variable requires a restart of Emacs to become effective."
  1234. :group 'org-link
  1235. :type '(set :greedy t
  1236. (const :tag "Double bracket links" bracket)
  1237. (const :tag "Angular bracket links" angle)
  1238. (const :tag "Plain text links" plain)
  1239. (const :tag "Radio target matches" radio)
  1240. (const :tag "Tags" tag)
  1241. (const :tag "Timestamps" date)
  1242. (const :tag "Footnotes" footnote)))
  1243. (defcustom org-make-link-description-function nil
  1244. "Function to use for generating link descriptions from links.
  1245. When nil, the link location will be used. This function must take
  1246. two parameters: the first one is the link, the second one is the
  1247. description generated by `org-insert-link'. The function should
  1248. return the description to use."
  1249. :group 'org-link
  1250. :type 'function)
  1251. (defgroup org-link-store nil
  1252. "Options concerning storing links in Org-mode."
  1253. :tag "Org Store Link"
  1254. :group 'org-link)
  1255. (defcustom org-url-hexify-p t
  1256. "When non-nil, hexify URL when creating a link."
  1257. :type 'boolean
  1258. :version "24.3"
  1259. :group 'org-link-store)
  1260. (defcustom org-email-link-description-format "Email %c: %.30s"
  1261. "Format of the description part of a link to an email or usenet message.
  1262. The following %-escapes will be replaced by corresponding information:
  1263. %F full \"From\" field
  1264. %f name, taken from \"From\" field, address if no name
  1265. %T full \"To\" field
  1266. %t first name in \"To\" field, address if no name
  1267. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1268. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1269. %s subject
  1270. %d date
  1271. %m message-id.
  1272. You may use normal field width specification between the % and the letter.
  1273. This is for example useful to limit the length of the subject.
  1274. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1275. :group 'org-link-store
  1276. :type 'string)
  1277. (defcustom org-from-is-user-regexp
  1278. (let (r1 r2)
  1279. (when (and user-mail-address (not (string= user-mail-address "")))
  1280. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1281. (when (and user-full-name (not (string= user-full-name "")))
  1282. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1283. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1284. "Regexp matched against the \"From:\" header of an email or usenet message.
  1285. It should match if the message is from the user him/herself."
  1286. :group 'org-link-store
  1287. :type 'regexp)
  1288. (defcustom org-context-in-file-links t
  1289. "Non-nil means file links from `org-store-link' contain context.
  1290. A search string will be added to the file name with :: as separator and
  1291. used to find the context when the link is activated by the command
  1292. `org-open-at-point'. When this option is t, the entire active region
  1293. will be placed in the search string of the file link. If set to a
  1294. positive integer, only the first n lines of context will be stored.
  1295. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1296. negates this setting for the duration of the command."
  1297. :group 'org-link-store
  1298. :type '(choice boolean integer))
  1299. (defcustom org-keep-stored-link-after-insertion nil
  1300. "Non-nil means keep link in list for entire session.
  1301. The command `org-store-link' adds a link pointing to the current
  1302. location to an internal list. These links accumulate during a session.
  1303. The command `org-insert-link' can be used to insert links into any
  1304. Org-mode file (offering completion for all stored links). When this
  1305. option is nil, every link which has been inserted once using \\[org-insert-link]
  1306. will be removed from the list, to make completing the unused links
  1307. more efficient."
  1308. :group 'org-link-store
  1309. :type 'boolean)
  1310. (defgroup org-link-follow nil
  1311. "Options concerning following links in Org-mode."
  1312. :tag "Org Follow Link"
  1313. :group 'org-link)
  1314. (defcustom org-link-translation-function nil
  1315. "Function to translate links with different syntax to Org syntax.
  1316. This can be used to translate links created for example by the Planner
  1317. or emacs-wiki packages to Org syntax.
  1318. The function must accept two parameters, a TYPE containing the link
  1319. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1320. which is everything after the link protocol. It should return a cons
  1321. with possibly modified values of type and path.
  1322. Org contains a function for this, so if you set this variable to
  1323. `org-translate-link-from-planner', you should be able follow many
  1324. links created by planner."
  1325. :group 'org-link-follow
  1326. :type 'function)
  1327. (defcustom org-follow-link-hook nil
  1328. "Hook that is run after a link has been followed."
  1329. :group 'org-link-follow
  1330. :type 'hook)
  1331. (defcustom org-tab-follows-link nil
  1332. "Non-nil means on links TAB will follow the link.
  1333. Needs to be set before org.el is loaded.
  1334. This really should not be used, it does not make sense, and the
  1335. implementation is bad."
  1336. :group 'org-link-follow
  1337. :type 'boolean)
  1338. (defcustom org-return-follows-link nil
  1339. "Non-nil means on links RET will follow the link."
  1340. :group 'org-link-follow
  1341. :type 'boolean)
  1342. (defcustom org-mouse-1-follows-link
  1343. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1344. "Non-nil means mouse-1 on a link will follow the link.
  1345. A longer mouse click will still set point. Does not work on XEmacs.
  1346. Needs to be set before org.el is loaded."
  1347. :group 'org-link-follow
  1348. :type 'boolean)
  1349. (defcustom org-mark-ring-length 4
  1350. "Number of different positions to be recorded in the ring.
  1351. Changing this requires a restart of Emacs to work correctly."
  1352. :group 'org-link-follow
  1353. :type 'integer)
  1354. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1355. "Non-nil means internal links in Org files must exactly match a headline.
  1356. When nil, the link search tries to match a phrase with all words
  1357. in the search text."
  1358. :group 'org-link-follow
  1359. :version "24.1"
  1360. :type '(choice
  1361. (const :tag "Use fuzzy text search" nil)
  1362. (const :tag "Match only exact headline" t)
  1363. (const :tag "Match exact headline or query to create it"
  1364. query-to-create)))
  1365. (defcustom org-link-frame-setup
  1366. '((vm . vm-visit-folder-other-frame)
  1367. (vm-imap . vm-visit-imap-folder-other-frame)
  1368. (gnus . org-gnus-no-new-news)
  1369. (file . find-file-other-window)
  1370. (wl . wl-other-frame))
  1371. "Setup the frame configuration for following links.
  1372. When following a link with Emacs, it may often be useful to display
  1373. this link in another window or frame. This variable can be used to
  1374. set this up for the different types of links.
  1375. For VM, use any of
  1376. `vm-visit-folder'
  1377. `vm-visit-folder-other-window'
  1378. `vm-visit-folder-other-frame'
  1379. For Gnus, use any of
  1380. `gnus'
  1381. `gnus-other-frame'
  1382. `org-gnus-no-new-news'
  1383. For FILE, use any of
  1384. `find-file'
  1385. `find-file-other-window'
  1386. `find-file-other-frame'
  1387. For Wanderlust use any of
  1388. `wl'
  1389. `wl-other-frame'
  1390. For the calendar, use the variable `calendar-setup'.
  1391. For BBDB, it is currently only possible to display the matches in
  1392. another window."
  1393. :group 'org-link-follow
  1394. :type '(list
  1395. (cons (const vm)
  1396. (choice
  1397. (const vm-visit-folder)
  1398. (const vm-visit-folder-other-window)
  1399. (const vm-visit-folder-other-frame)))
  1400. (cons (const gnus)
  1401. (choice
  1402. (const gnus)
  1403. (const gnus-other-frame)
  1404. (const org-gnus-no-new-news)))
  1405. (cons (const file)
  1406. (choice
  1407. (const find-file)
  1408. (const find-file-other-window)
  1409. (const find-file-other-frame)))
  1410. (cons (const wl)
  1411. (choice
  1412. (const wl)
  1413. (const wl-other-frame)))))
  1414. (defcustom org-display-internal-link-with-indirect-buffer nil
  1415. "Non-nil means use indirect buffer to display infile links.
  1416. Activating internal links (from one location in a file to another location
  1417. in the same file) normally just jumps to the location. When the link is
  1418. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1419. is displayed in
  1420. another window. When this option is set, the other window actually displays
  1421. an indirect buffer clone of the current buffer, to avoid any visibility
  1422. changes to the current buffer."
  1423. :group 'org-link-follow
  1424. :type 'boolean)
  1425. (defcustom org-open-non-existing-files nil
  1426. "Non-nil means `org-open-file' will open non-existing files.
  1427. When nil, an error will be generated.
  1428. This variable applies only to external applications because they
  1429. might choke on non-existing files. If the link is to a file that
  1430. will be opened in Emacs, the variable is ignored."
  1431. :group 'org-link-follow
  1432. :type 'boolean)
  1433. (defcustom org-open-directory-means-index-dot-org nil
  1434. "Non-nil means a link to a directory really means to index.org.
  1435. When nil, following a directory link will run dired or open a finder/explorer
  1436. window on that directory."
  1437. :group 'org-link-follow
  1438. :type 'boolean)
  1439. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1440. "Function and arguments to call for following mailto links.
  1441. This is a list with the first element being a Lisp function, and the
  1442. remaining elements being arguments to the function. In string arguments,
  1443. %a will be replaced by the address, and %s will be replaced by the subject
  1444. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1445. :group 'org-link-follow
  1446. :type '(choice
  1447. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1448. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1449. (const :tag "message-mail" (message-mail "%a" "%s"))
  1450. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1451. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1452. "Non-nil means ask for confirmation before executing shell links.
  1453. Shell links can be dangerous: just think about a link
  1454. [[shell:rm -rf ~/*][Google Search]]
  1455. This link would show up in your Org-mode document as \"Google Search\",
  1456. but really it would remove your entire home directory.
  1457. Therefore we advise against setting this variable to nil.
  1458. Just change it to `y-or-n-p' if you want to confirm with a
  1459. single keystroke rather than having to type \"yes\"."
  1460. :group 'org-link-follow
  1461. :type '(choice
  1462. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1463. (const :tag "with y-or-n (faster)" y-or-n-p)
  1464. (const :tag "no confirmation (dangerous)" nil)))
  1465. (put 'org-confirm-shell-link-function
  1466. 'safe-local-variable
  1467. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1468. (defcustom org-confirm-shell-link-not-regexp ""
  1469. "A regexp to skip confirmation for shell links."
  1470. :group 'org-link-follow
  1471. :version "24.1"
  1472. :type 'regexp)
  1473. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1474. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1475. Elisp links can be dangerous: just think about a link
  1476. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1477. This link would show up in your Org-mode document as \"Google Search\",
  1478. but really it would remove your entire home directory.
  1479. Therefore we advise against setting this variable to nil.
  1480. Just change it to `y-or-n-p' if you want to confirm with a
  1481. single keystroke rather than having to type \"yes\"."
  1482. :group 'org-link-follow
  1483. :type '(choice
  1484. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1485. (const :tag "with y-or-n (faster)" y-or-n-p)
  1486. (const :tag "no confirmation (dangerous)" nil)))
  1487. (put 'org-confirm-shell-link-function
  1488. 'safe-local-variable
  1489. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1490. (defcustom org-confirm-elisp-link-not-regexp ""
  1491. "A regexp to skip confirmation for Elisp links."
  1492. :group 'org-link-follow
  1493. :version "24.1"
  1494. :type 'regexp)
  1495. (defconst org-file-apps-defaults-gnu
  1496. '((remote . emacs)
  1497. (system . mailcap)
  1498. (t . mailcap))
  1499. "Default file applications on a UNIX or GNU/Linux system.
  1500. See `org-file-apps'.")
  1501. (defconst org-file-apps-defaults-macosx
  1502. '((remote . emacs)
  1503. (t . "open %s")
  1504. (system . "open %s")
  1505. ("ps.gz" . "gv %s")
  1506. ("eps.gz" . "gv %s")
  1507. ("dvi" . "xdvi %s")
  1508. ("fig" . "xfig %s"))
  1509. "Default file applications on a MacOS X system.
  1510. The system \"open\" is known as a default, but we use X11 applications
  1511. for some files for which the OS does not have a good default.
  1512. See `org-file-apps'.")
  1513. (defconst org-file-apps-defaults-windowsnt
  1514. (list
  1515. '(remote . emacs)
  1516. (cons t
  1517. (list (if (featurep 'xemacs)
  1518. 'mswindows-shell-execute
  1519. 'w32-shell-execute)
  1520. "open" 'file))
  1521. (cons 'system
  1522. (list (if (featurep 'xemacs)
  1523. 'mswindows-shell-execute
  1524. 'w32-shell-execute)
  1525. "open" 'file)))
  1526. "Default file applications on a Windows NT system.
  1527. The system \"open\" is used for most files.
  1528. See `org-file-apps'.")
  1529. (defcustom org-file-apps
  1530. '(
  1531. (auto-mode . emacs)
  1532. ("\\.mm\\'" . default)
  1533. ("\\.x?html?\\'" . default)
  1534. ("\\.pdf\\'" . default)
  1535. )
  1536. "External applications for opening `file:path' items in a document.
  1537. Org-mode uses system defaults for different file types, but
  1538. you can use this variable to set the application for a given file
  1539. extension. The entries in this list are cons cells where the car identifies
  1540. files and the cdr the corresponding command. Possible values for the
  1541. file identifier are
  1542. \"string\" A string as a file identifier can be interpreted in different
  1543. ways, depending on its contents:
  1544. - Alphanumeric characters only:
  1545. Match links with this file extension.
  1546. Example: (\"pdf\" . \"evince %s\")
  1547. to open PDFs with evince.
  1548. - Regular expression: Match links where the
  1549. filename matches the regexp. If you want to
  1550. use groups here, use shy groups.
  1551. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1552. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1553. to open *.html and *.xhtml with firefox.
  1554. - Regular expression which contains (non-shy) groups:
  1555. Match links where the whole link, including \"::\", and
  1556. anything after that, matches the regexp.
  1557. In a custom command string, %1, %2, etc. are replaced with
  1558. the parts of the link that were matched by the groups.
  1559. For backwards compatibility, if a command string is given
  1560. that does not use any of the group matches, this case is
  1561. handled identically to the second one (i.e. match against
  1562. file name only).
  1563. In a custom lisp form, you can access the group matches with
  1564. (match-string n link).
  1565. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1566. to open [[file:document.pdf::5]] with evince at page 5.
  1567. `directory' Matches a directory
  1568. `remote' Matches a remote file, accessible through tramp or efs.
  1569. Remote files most likely should be visited through Emacs
  1570. because external applications cannot handle such paths.
  1571. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1572. so all files Emacs knows how to handle. Using this with
  1573. command `emacs' will open most files in Emacs. Beware that this
  1574. will also open html files inside Emacs, unless you add
  1575. (\"html\" . default) to the list as well.
  1576. t Default for files not matched by any of the other options.
  1577. `system' The system command to open files, like `open' on Windows
  1578. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1579. that will be selected if you call `C-c C-o' with a double
  1580. \\[universal-argument] \\[universal-argument] prefix.
  1581. Possible values for the command are:
  1582. `emacs' The file will be visited by the current Emacs process.
  1583. `default' Use the default application for this file type, which is the
  1584. association for t in the list, most likely in the system-specific
  1585. part.
  1586. This can be used to overrule an unwanted setting in the
  1587. system-specific variable.
  1588. `system' Use the system command for opening files, like \"open\".
  1589. This command is specified by the entry whose car is `system'.
  1590. Most likely, the system-specific version of this variable
  1591. does define this command, but you can overrule/replace it
  1592. here.
  1593. string A command to be executed by a shell; %s will be replaced
  1594. by the path to the file.
  1595. sexp A Lisp form which will be evaluated. The file path will
  1596. be available in the Lisp variable `file'.
  1597. For more examples, see the system specific constants
  1598. `org-file-apps-defaults-macosx'
  1599. `org-file-apps-defaults-windowsnt'
  1600. `org-file-apps-defaults-gnu'."
  1601. :group 'org-link-follow
  1602. :type '(repeat
  1603. (cons (choice :value ""
  1604. (string :tag "Extension")
  1605. (const :tag "System command to open files" system)
  1606. (const :tag "Default for unrecognized files" t)
  1607. (const :tag "Remote file" remote)
  1608. (const :tag "Links to a directory" directory)
  1609. (const :tag "Any files that have Emacs modes"
  1610. auto-mode))
  1611. (choice :value ""
  1612. (const :tag "Visit with Emacs" emacs)
  1613. (const :tag "Use default" default)
  1614. (const :tag "Use the system command" system)
  1615. (string :tag "Command")
  1616. (sexp :tag "Lisp form")))))
  1617. (defcustom org-doi-server-url "http://dx.doi.org/"
  1618. "The URL of the DOI server."
  1619. :type 'string
  1620. :version "24.3"
  1621. :group 'org-link-follow)
  1622. (defgroup org-refile nil
  1623. "Options concerning refiling entries in Org-mode."
  1624. :tag "Org Refile"
  1625. :group 'org)
  1626. (defcustom org-directory "~/org"
  1627. "Directory with org files.
  1628. This is just a default location to look for Org files. There is no need
  1629. at all to put your files into this directory. It is only used in the
  1630. following situations:
  1631. 1. When a capture template specifies a target file that is not an
  1632. absolute path. The path will then be interpreted relative to
  1633. `org-directory'
  1634. 2. When a capture note is filed away in an interactive way (when exiting the
  1635. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1636. with `org-directory' as the default path."
  1637. :group 'org-refile
  1638. :group 'org-remember
  1639. :group 'org-capture
  1640. :type 'directory)
  1641. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1642. "Default target for storing notes.
  1643. Used as a fall back file for org-remember.el and org-capture.el, for
  1644. templates that do not specify a target file."
  1645. :group 'org-refile
  1646. :group 'org-remember
  1647. :group 'org-capture
  1648. :type '(choice
  1649. (const :tag "Default from remember-data-file" nil)
  1650. file))
  1651. (defcustom org-goto-interface 'outline
  1652. "The default interface to be used for `org-goto'.
  1653. Allowed values are:
  1654. outline The interface shows an outline of the relevant file
  1655. and the correct heading is found by moving through
  1656. the outline or by searching with incremental search.
  1657. outline-path-completion Headlines in the current buffer are offered via
  1658. completion. This is the interface also used by
  1659. the refile command."
  1660. :group 'org-refile
  1661. :type '(choice
  1662. (const :tag "Outline" outline)
  1663. (const :tag "Outline-path-completion" outline-path-completion)))
  1664. (defcustom org-goto-max-level 5
  1665. "Maximum target level when running `org-goto' with refile interface."
  1666. :group 'org-refile
  1667. :type 'integer)
  1668. (defcustom org-reverse-note-order nil
  1669. "Non-nil means store new notes at the beginning of a file or entry.
  1670. When nil, new notes will be filed to the end of a file or entry.
  1671. This can also be a list with cons cells of regular expressions that
  1672. are matched against file names, and values."
  1673. :group 'org-remember
  1674. :group 'org-capture
  1675. :group 'org-refile
  1676. :type '(choice
  1677. (const :tag "Reverse always" t)
  1678. (const :tag "Reverse never" nil)
  1679. (repeat :tag "By file name regexp"
  1680. (cons regexp boolean))))
  1681. (defcustom org-log-refile nil
  1682. "Information to record when a task is refiled.
  1683. Possible values are:
  1684. nil Don't add anything
  1685. time Add a time stamp to the task
  1686. note Prompt for a note and add it with template `org-log-note-headings'
  1687. This option can also be set with on a per-file-basis with
  1688. #+STARTUP: nologrefile
  1689. #+STARTUP: logrefile
  1690. #+STARTUP: lognoterefile
  1691. You can have local logging settings for a subtree by setting the LOGGING
  1692. property to one or more of these keywords.
  1693. When bulk-refiling from the agenda, the value `note' is forbidden and
  1694. will temporarily be changed to `time'."
  1695. :group 'org-refile
  1696. :group 'org-progress
  1697. :version "24.1"
  1698. :type '(choice
  1699. (const :tag "No logging" nil)
  1700. (const :tag "Record timestamp" time)
  1701. (const :tag "Record timestamp with note." note)))
  1702. (defcustom org-refile-targets nil
  1703. "Targets for refiling entries with \\[org-refile].
  1704. This is a list of cons cells. Each cell contains:
  1705. - a specification of the files to be considered, either a list of files,
  1706. or a symbol whose function or variable value will be used to retrieve
  1707. a file name or a list of file names. If you use `org-agenda-files' for
  1708. that, all agenda files will be scanned for targets. Nil means consider
  1709. headings in the current buffer.
  1710. - A specification of how to find candidate refile targets. This may be
  1711. any of:
  1712. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1713. This tag has to be present in all target headlines, inheritance will
  1714. not be considered.
  1715. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1716. todo keyword.
  1717. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1718. headlines that are refiling targets.
  1719. - a cons cell (:level . N). Any headline of level N is considered a target.
  1720. Note that, when `org-odd-levels-only' is set, level corresponds to
  1721. order in hierarchy, not to the number of stars.
  1722. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1723. Note that, when `org-odd-levels-only' is set, level corresponds to
  1724. order in hierarchy, not to the number of stars.
  1725. Each element of this list generates a set of possible targets.
  1726. The union of these sets is presented (with completion) to
  1727. the user by `org-refile'.
  1728. You can set the variable `org-refile-target-verify-function' to a function
  1729. to verify each headline found by the simple criteria above.
  1730. When this variable is nil, all top-level headlines in the current buffer
  1731. are used, equivalent to the value `((nil . (:level . 1))'."
  1732. :group 'org-refile
  1733. :type '(repeat
  1734. (cons
  1735. (choice :value org-agenda-files
  1736. (const :tag "All agenda files" org-agenda-files)
  1737. (const :tag "Current buffer" nil)
  1738. (function) (variable) (file))
  1739. (choice :tag "Identify target headline by"
  1740. (cons :tag "Specific tag" (const :value :tag) (string))
  1741. (cons :tag "TODO keyword" (const :value :todo) (string))
  1742. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1743. (cons :tag "Level number" (const :value :level) (integer))
  1744. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1745. (defcustom org-refile-target-verify-function nil
  1746. "Function to verify if the headline at point should be a refile target.
  1747. The function will be called without arguments, with point at the
  1748. beginning of the headline. It should return t and leave point
  1749. where it is if the headline is a valid target for refiling.
  1750. If the target should not be selected, the function must return nil.
  1751. In addition to this, it may move point to a place from where the search
  1752. should be continued. For example, the function may decide that the entire
  1753. subtree of the current entry should be excluded and move point to the end
  1754. of the subtree."
  1755. :group 'org-refile
  1756. :type 'function)
  1757. (defcustom org-refile-use-cache nil
  1758. "Non-nil means cache refile targets to speed up the process.
  1759. The cache for a particular file will be updated automatically when
  1760. the buffer has been killed, or when any of the marker used for flagging
  1761. refile targets no longer points at a live buffer.
  1762. If you have added new entries to a buffer that might themselves be targets,
  1763. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1764. find that easier, `C-u C-u C-u C-c C-w'."
  1765. :group 'org-refile
  1766. :version "24.1"
  1767. :type 'boolean)
  1768. (defcustom org-refile-use-outline-path nil
  1769. "Non-nil means provide refile targets as paths.
  1770. So a level 3 headline will be available as level1/level2/level3.
  1771. When the value is `file', also include the file name (without directory)
  1772. into the path. In this case, you can also stop the completion after
  1773. the file name, to get entries inserted as top level in the file.
  1774. When `full-file-path', include the full file path."
  1775. :group 'org-refile
  1776. :type '(choice
  1777. (const :tag "Not" nil)
  1778. (const :tag "Yes" t)
  1779. (const :tag "Start with file name" file)
  1780. (const :tag "Start with full file path" full-file-path)))
  1781. (defcustom org-outline-path-complete-in-steps t
  1782. "Non-nil means complete the outline path in hierarchical steps.
  1783. When Org-mode uses the refile interface to select an outline path
  1784. \(see variable `org-refile-use-outline-path'), the completion of
  1785. the path can be done is a single go, or if can be done in steps down
  1786. the headline hierarchy. Going in steps is probably the best if you
  1787. do not use a special completion package like `ido' or `icicles'.
  1788. However, when using these packages, going in one step can be very
  1789. fast, while still showing the whole path to the entry."
  1790. :group 'org-refile
  1791. :type 'boolean)
  1792. (defcustom org-refile-allow-creating-parent-nodes nil
  1793. "Non-nil means allow to create new nodes as refile targets.
  1794. New nodes are then created by adding \"/new node name\" to the completion
  1795. of an existing node. When the value of this variable is `confirm',
  1796. new node creation must be confirmed by the user (recommended)
  1797. When nil, the completion must match an existing entry.
  1798. Note that, if the new heading is not seen by the criteria
  1799. listed in `org-refile-targets', multiple instances of the same
  1800. heading would be created by trying again to file under the new
  1801. heading."
  1802. :group 'org-refile
  1803. :type '(choice
  1804. (const :tag "Never" nil)
  1805. (const :tag "Always" t)
  1806. (const :tag "Prompt for confirmation" confirm)))
  1807. (defcustom org-refile-active-region-within-subtree nil
  1808. "Non-nil means also refile active region within a subtree.
  1809. By default `org-refile' doesn't allow refiling regions if they
  1810. don't contain a set of subtrees, but it might be convenient to
  1811. do so sometimes: in that case, the first line of the region is
  1812. converted to a headline before refiling."
  1813. :group 'org-refile
  1814. :version "24.1"
  1815. :type 'boolean)
  1816. (defgroup org-todo nil
  1817. "Options concerning TODO items in Org-mode."
  1818. :tag "Org TODO"
  1819. :group 'org)
  1820. (defgroup org-progress nil
  1821. "Options concerning Progress logging in Org-mode."
  1822. :tag "Org Progress"
  1823. :group 'org-time)
  1824. (defvar org-todo-interpretation-widgets
  1825. '((:tag "Sequence (cycling hits every state)" sequence)
  1826. (:tag "Type (cycling directly to DONE)" type))
  1827. "The available interpretation symbols for customizing `org-todo-keywords'.
  1828. Interested libraries should add to this list.")
  1829. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1830. "List of TODO entry keyword sequences and their interpretation.
  1831. \\<org-mode-map>This is a list of sequences.
  1832. Each sequence starts with a symbol, either `sequence' or `type',
  1833. indicating if the keywords should be interpreted as a sequence of
  1834. action steps, or as different types of TODO items. The first
  1835. keywords are states requiring action - these states will select a headline
  1836. for inclusion into the global TODO list Org-mode produces. If one of
  1837. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1838. signify that no further action is necessary. If \"|\" is not found,
  1839. the last keyword is treated as the only DONE state of the sequence.
  1840. The command \\[org-todo] cycles an entry through these states, and one
  1841. additional state where no keyword is present. For details about this
  1842. cycling, see the manual.
  1843. TODO keywords and interpretation can also be set on a per-file basis with
  1844. the special #+SEQ_TODO and #+TYP_TODO lines.
  1845. Each keyword can optionally specify a character for fast state selection
  1846. \(in combination with the variable `org-use-fast-todo-selection')
  1847. and specifiers for state change logging, using the same syntax that
  1848. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1849. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1850. indicates to record a time stamp each time this state is selected.
  1851. Each keyword may also specify if a timestamp or a note should be
  1852. recorded when entering or leaving the state, by adding additional
  1853. characters in the parenthesis after the keyword. This looks like this:
  1854. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1855. record only the time of the state change. With X and Y being either
  1856. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1857. Y when leaving the state if and only if the *target* state does not
  1858. define X. You may omit any of the fast-selection key or X or /Y,
  1859. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1860. For backward compatibility, this variable may also be just a list
  1861. of keywords. In this case the interpretation (sequence or type) will be
  1862. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1863. :group 'org-todo
  1864. :group 'org-keywords
  1865. :type '(choice
  1866. (repeat :tag "Old syntax, just keywords"
  1867. (string :tag "Keyword"))
  1868. (repeat :tag "New syntax"
  1869. (cons
  1870. (choice
  1871. :tag "Interpretation"
  1872. ;;Quick and dirty way to see
  1873. ;;`org-todo-interpretations'. This takes the
  1874. ;;place of item arguments
  1875. :convert-widget
  1876. (lambda (widget)
  1877. (widget-put widget
  1878. :args (mapcar
  1879. #'(lambda (x)
  1880. (widget-convert
  1881. (cons 'const x)))
  1882. org-todo-interpretation-widgets))
  1883. widget))
  1884. (repeat
  1885. (string :tag "Keyword"))))))
  1886. (defvar org-todo-keywords-1 nil
  1887. "All TODO and DONE keywords active in a buffer.")
  1888. (make-variable-buffer-local 'org-todo-keywords-1)
  1889. (defvar org-todo-keywords-for-agenda nil)
  1890. (defvar org-done-keywords-for-agenda nil)
  1891. (defvar org-drawers-for-agenda nil)
  1892. (defvar org-todo-keyword-alist-for-agenda nil)
  1893. (defvar org-tag-alist-for-agenda nil)
  1894. (defvar org-agenda-contributing-files nil)
  1895. (defvar org-not-done-keywords nil)
  1896. (make-variable-buffer-local 'org-not-done-keywords)
  1897. (defvar org-done-keywords nil)
  1898. (make-variable-buffer-local 'org-done-keywords)
  1899. (defvar org-todo-heads nil)
  1900. (make-variable-buffer-local 'org-todo-heads)
  1901. (defvar org-todo-sets nil)
  1902. (make-variable-buffer-local 'org-todo-sets)
  1903. (defvar org-todo-log-states nil)
  1904. (make-variable-buffer-local 'org-todo-log-states)
  1905. (defvar org-todo-kwd-alist nil)
  1906. (make-variable-buffer-local 'org-todo-kwd-alist)
  1907. (defvar org-todo-key-alist nil)
  1908. (make-variable-buffer-local 'org-todo-key-alist)
  1909. (defvar org-todo-key-trigger nil)
  1910. (make-variable-buffer-local 'org-todo-key-trigger)
  1911. (defcustom org-todo-interpretation 'sequence
  1912. "Controls how TODO keywords are interpreted.
  1913. This variable is in principle obsolete and is only used for
  1914. backward compatibility, if the interpretation of todo keywords is
  1915. not given already in `org-todo-keywords'. See that variable for
  1916. more information."
  1917. :group 'org-todo
  1918. :group 'org-keywords
  1919. :type '(choice (const sequence)
  1920. (const type)))
  1921. (defcustom org-use-fast-todo-selection t
  1922. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1923. This variable describes if and under what circumstances the cycling
  1924. mechanism for TODO keywords will be replaced by a single-key, direct
  1925. selection scheme.
  1926. When nil, fast selection is never used.
  1927. When the symbol `prefix', it will be used when `org-todo' is called
  1928. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  1929. `C-u t' in an agenda buffer.
  1930. When t, fast selection is used by default. In this case, the prefix
  1931. argument forces cycling instead.
  1932. In all cases, the special interface is only used if access keys have
  1933. actually been assigned by the user, i.e. if keywords in the configuration
  1934. are followed by a letter in parenthesis, like TODO(t)."
  1935. :group 'org-todo
  1936. :type '(choice
  1937. (const :tag "Never" nil)
  1938. (const :tag "By default" t)
  1939. (const :tag "Only with C-u C-c C-t" prefix)))
  1940. (defcustom org-provide-todo-statistics t
  1941. "Non-nil means update todo statistics after insert and toggle.
  1942. ALL-HEADLINES means update todo statistics by including headlines
  1943. with no TODO keyword as well, counting them as not done.
  1944. A list of TODO keywords means the same, but skip keywords that are
  1945. not in this list.
  1946. When this is set, todo statistics is updated in the parent of the
  1947. current entry each time a todo state is changed."
  1948. :group 'org-todo
  1949. :type '(choice
  1950. (const :tag "Yes, only for TODO entries" t)
  1951. (const :tag "Yes, including all entries" 'all-headlines)
  1952. (repeat :tag "Yes, for TODOs in this list"
  1953. (string :tag "TODO keyword"))
  1954. (other :tag "No TODO statistics" nil)))
  1955. (defcustom org-hierarchical-todo-statistics t
  1956. "Non-nil means TODO statistics covers just direct children.
  1957. When nil, all entries in the subtree are considered.
  1958. This has only an effect if `org-provide-todo-statistics' is set.
  1959. To set this to nil for only a single subtree, use a COOKIE_DATA
  1960. property and include the word \"recursive\" into the value."
  1961. :group 'org-todo
  1962. :type 'boolean)
  1963. (defcustom org-after-todo-state-change-hook nil
  1964. "Hook which is run after the state of a TODO item was changed.
  1965. The new state (a string with a TODO keyword, or nil) is available in the
  1966. Lisp variable `org-state'."
  1967. :group 'org-todo
  1968. :type 'hook)
  1969. (defvar org-blocker-hook nil
  1970. "Hook for functions that are allowed to block a state change.
  1971. Functions in this hook should not modify the buffer.
  1972. Each function gets as its single argument a property list,
  1973. see `org-trigger-hook' for more information about this list.
  1974. If any of the functions in this hook returns nil, the state change
  1975. is blocked.")
  1976. (defvar org-trigger-hook nil
  1977. "Hook for functions that are triggered by a state change.
  1978. Each function gets as its single argument a property list with at
  1979. least the following elements:
  1980. (:type type-of-change :position pos-at-entry-start
  1981. :from old-state :to new-state)
  1982. Depending on the type, more properties may be present.
  1983. This mechanism is currently implemented for:
  1984. TODO state changes
  1985. ------------------
  1986. :type todo-state-change
  1987. :from previous state (keyword as a string), or nil, or a symbol
  1988. 'todo' or 'done', to indicate the general type of state.
  1989. :to new state, like in :from")
  1990. (defcustom org-enforce-todo-dependencies nil
  1991. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1992. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1993. be blocked if any prior sibling is not yet done.
  1994. Finally, if the parent is blocked because of ordered siblings of its own,
  1995. the child will also be blocked."
  1996. :set (lambda (var val)
  1997. (set var val)
  1998. (if val
  1999. (add-hook 'org-blocker-hook
  2000. 'org-block-todo-from-children-or-siblings-or-parent)
  2001. (remove-hook 'org-blocker-hook
  2002. 'org-block-todo-from-children-or-siblings-or-parent)))
  2003. :group 'org-todo
  2004. :type 'boolean)
  2005. (defcustom org-enforce-todo-checkbox-dependencies nil
  2006. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2007. When this is nil, checkboxes have no influence on switching TODO states.
  2008. When non-nil, you first need to check off all check boxes before the TODO
  2009. entry can be switched to DONE.
  2010. This variable needs to be set before org.el is loaded, and you need to
  2011. restart Emacs after a change to make the change effective. The only way
  2012. to change is while Emacs is running is through the customize interface."
  2013. :set (lambda (var val)
  2014. (set var val)
  2015. (if val
  2016. (add-hook 'org-blocker-hook
  2017. 'org-block-todo-from-checkboxes)
  2018. (remove-hook 'org-blocker-hook
  2019. 'org-block-todo-from-checkboxes)))
  2020. :group 'org-todo
  2021. :type 'boolean)
  2022. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2023. "Non-nil means inserting a TODO heading is treated as state change.
  2024. So when the command \\[org-insert-todo-heading] is used, state change
  2025. logging will apply if appropriate. When nil, the new TODO item will
  2026. be inserted directly, and no logging will take place."
  2027. :group 'org-todo
  2028. :type 'boolean)
  2029. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2030. "Non-nil means switching TODO states with S-cursor counts as state change.
  2031. This is the default behavior. However, setting this to nil allows a
  2032. convenient way to select a TODO state and bypass any logging associated
  2033. with that."
  2034. :group 'org-todo
  2035. :type 'boolean)
  2036. (defcustom org-todo-state-tags-triggers nil
  2037. "Tag changes that should be triggered by TODO state changes.
  2038. This is a list. Each entry is
  2039. (state-change (tag . flag) .......)
  2040. State-change can be a string with a state, and empty string to indicate the
  2041. state that has no TODO keyword, or it can be one of the symbols `todo'
  2042. or `done', meaning any not-done or done state, respectively."
  2043. :group 'org-todo
  2044. :group 'org-tags
  2045. :type '(repeat
  2046. (cons (choice :tag "When changing to"
  2047. (const :tag "Not-done state" todo)
  2048. (const :tag "Done state" done)
  2049. (string :tag "State"))
  2050. (repeat
  2051. (cons :tag "Tag action"
  2052. (string :tag "Tag")
  2053. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2054. (defcustom org-log-done nil
  2055. "Information to record when a task moves to the DONE state.
  2056. Possible values are:
  2057. nil Don't add anything, just change the keyword
  2058. time Add a time stamp to the task
  2059. note Prompt for a note and add it with template `org-log-note-headings'
  2060. This option can also be set with on a per-file-basis with
  2061. #+STARTUP: nologdone
  2062. #+STARTUP: logdone
  2063. #+STARTUP: lognotedone
  2064. You can have local logging settings for a subtree by setting the LOGGING
  2065. property to one or more of these keywords."
  2066. :group 'org-todo
  2067. :group 'org-progress
  2068. :type '(choice
  2069. (const :tag "No logging" nil)
  2070. (const :tag "Record CLOSED timestamp" time)
  2071. (const :tag "Record CLOSED timestamp with note." note)))
  2072. ;; Normalize old uses of org-log-done.
  2073. (cond
  2074. ((eq org-log-done t) (setq org-log-done 'time))
  2075. ((and (listp org-log-done) (memq 'done org-log-done))
  2076. (setq org-log-done 'note)))
  2077. (defcustom org-log-reschedule nil
  2078. "Information to record when the scheduling date of a tasks is modified.
  2079. Possible values are:
  2080. nil Don't add anything, just change the date
  2081. time Add a time stamp to the task
  2082. note Prompt for a note and add it with template `org-log-note-headings'
  2083. This option can also be set with on a per-file-basis with
  2084. #+STARTUP: nologreschedule
  2085. #+STARTUP: logreschedule
  2086. #+STARTUP: lognotereschedule"
  2087. :group 'org-todo
  2088. :group 'org-progress
  2089. :type '(choice
  2090. (const :tag "No logging" nil)
  2091. (const :tag "Record timestamp" time)
  2092. (const :tag "Record timestamp with note." note)))
  2093. (defcustom org-log-redeadline nil
  2094. "Information to record when the deadline date of a tasks is modified.
  2095. Possible values are:
  2096. nil Don't add anything, just change the date
  2097. time Add a time stamp to the task
  2098. note Prompt for a note and add it with template `org-log-note-headings'
  2099. This option can also be set with on a per-file-basis with
  2100. #+STARTUP: nologredeadline
  2101. #+STARTUP: logredeadline
  2102. #+STARTUP: lognoteredeadline
  2103. You can have local logging settings for a subtree by setting the LOGGING
  2104. property to one or more of these keywords."
  2105. :group 'org-todo
  2106. :group 'org-progress
  2107. :type '(choice
  2108. (const :tag "No logging" nil)
  2109. (const :tag "Record timestamp" time)
  2110. (const :tag "Record timestamp with note." note)))
  2111. (defcustom org-log-note-clock-out nil
  2112. "Non-nil means record a note when clocking out of an item.
  2113. This can also be configured on a per-file basis by adding one of
  2114. the following lines anywhere in the buffer:
  2115. #+STARTUP: lognoteclock-out
  2116. #+STARTUP: nolognoteclock-out"
  2117. :group 'org-todo
  2118. :group 'org-progress
  2119. :type 'boolean)
  2120. (defcustom org-log-done-with-time t
  2121. "Non-nil means the CLOSED time stamp will contain date and time.
  2122. When nil, only the date will be recorded."
  2123. :group 'org-progress
  2124. :type 'boolean)
  2125. (defcustom org-log-note-headings
  2126. '((done . "CLOSING NOTE %t")
  2127. (state . "State %-12s from %-12S %t")
  2128. (note . "Note taken on %t")
  2129. (reschedule . "Rescheduled from %S on %t")
  2130. (delschedule . "Not scheduled, was %S on %t")
  2131. (redeadline . "New deadline from %S on %t")
  2132. (deldeadline . "Removed deadline, was %S on %t")
  2133. (refile . "Refiled on %t")
  2134. (clock-out . ""))
  2135. "Headings for notes added to entries.
  2136. The value is an alist, with the car being a symbol indicating the note
  2137. context, and the cdr is the heading to be used. The heading may also be the
  2138. empty string.
  2139. %t in the heading will be replaced by a time stamp.
  2140. %T will be an active time stamp instead the default inactive one
  2141. %d will be replaced by a short-format time stamp.
  2142. %D will be replaced by an active short-format time stamp.
  2143. %s will be replaced by the new TODO state, in double quotes.
  2144. %S will be replaced by the old TODO state, in double quotes.
  2145. %u will be replaced by the user name.
  2146. %U will be replaced by the full user name.
  2147. In fact, it is not a good idea to change the `state' entry, because
  2148. agenda log mode depends on the format of these entries."
  2149. :group 'org-todo
  2150. :group 'org-progress
  2151. :type '(list :greedy t
  2152. (cons (const :tag "Heading when closing an item" done) string)
  2153. (cons (const :tag
  2154. "Heading when changing todo state (todo sequence only)"
  2155. state) string)
  2156. (cons (const :tag "Heading when just taking a note" note) string)
  2157. (cons (const :tag "Heading when clocking out" clock-out) string)
  2158. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2159. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2160. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2161. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2162. (cons (const :tag "Heading when refiling" refile) string)))
  2163. (unless (assq 'note org-log-note-headings)
  2164. (push '(note . "%t") org-log-note-headings))
  2165. (defcustom org-log-into-drawer nil
  2166. "Non-nil means insert state change notes and time stamps into a drawer.
  2167. When nil, state changes notes will be inserted after the headline and
  2168. any scheduling and clock lines, but not inside a drawer.
  2169. The value of this variable should be the name of the drawer to use.
  2170. LOGBOOK is proposed as the default drawer for this purpose, you can
  2171. also set this to a string to define the drawer of your choice.
  2172. A value of t is also allowed, representing \"LOGBOOK\".
  2173. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2174. will be ignored.
  2175. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2176. a subtree."
  2177. :group 'org-todo
  2178. :group 'org-progress
  2179. :type '(choice
  2180. (const :tag "Not into a drawer" nil)
  2181. (const :tag "LOGBOOK" t)
  2182. (string :tag "Other")))
  2183. (if (fboundp 'defvaralias)
  2184. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2185. (defun org-log-into-drawer ()
  2186. "Return the value of `org-log-into-drawer', but let properties overrule.
  2187. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2188. used instead of the default value."
  2189. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2190. (cond
  2191. ((not p) org-log-into-drawer)
  2192. ((equal p "nil") nil)
  2193. ((equal p "t") "LOGBOOK")
  2194. (t p))))
  2195. (defcustom org-log-state-notes-insert-after-drawers nil
  2196. "Non-nil means insert state change notes after any drawers in entry.
  2197. Only the drawers that *immediately* follow the headline and the
  2198. deadline/scheduled line are skipped.
  2199. When nil, insert notes right after the heading and perhaps the line
  2200. with deadline/scheduling if present.
  2201. This variable will have no effect if `org-log-into-drawer' is
  2202. set."
  2203. :group 'org-todo
  2204. :group 'org-progress
  2205. :type 'boolean)
  2206. (defcustom org-log-states-order-reversed t
  2207. "Non-nil means the latest state note will be directly after heading.
  2208. When nil, the state change notes will be ordered according to time."
  2209. :group 'org-todo
  2210. :group 'org-progress
  2211. :type 'boolean)
  2212. (defcustom org-todo-repeat-to-state nil
  2213. "The TODO state to which a repeater should return the repeating task.
  2214. By default this is the first task in a TODO sequence, or the previous state
  2215. in a TODO_TYP set. But you can specify another task here.
  2216. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2217. :group 'org-todo
  2218. :version "24.1"
  2219. :type '(choice (const :tag "Head of sequence" nil)
  2220. (string :tag "Specific state")))
  2221. (defcustom org-log-repeat 'time
  2222. "Non-nil means record moving through the DONE state when triggering repeat.
  2223. An auto-repeating task is immediately switched back to TODO when
  2224. marked DONE. If you are not logging state changes (by adding \"@\"
  2225. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2226. record a closing note, there will be no record of the task moving
  2227. through DONE. This variable forces taking a note anyway.
  2228. nil Don't force a record
  2229. time Record a time stamp
  2230. note Prompt for a note and add it with template `org-log-note-headings'
  2231. This option can also be set with on a per-file-basis with
  2232. #+STARTUP: nologrepeat
  2233. #+STARTUP: logrepeat
  2234. #+STARTUP: lognoterepeat
  2235. You can have local logging settings for a subtree by setting the LOGGING
  2236. property to one or more of these keywords."
  2237. :group 'org-todo
  2238. :group 'org-progress
  2239. :type '(choice
  2240. (const :tag "Don't force a record" nil)
  2241. (const :tag "Force recording the DONE state" time)
  2242. (const :tag "Force recording a note with the DONE state" note)))
  2243. (defgroup org-priorities nil
  2244. "Priorities in Org-mode."
  2245. :tag "Org Priorities"
  2246. :group 'org-todo)
  2247. (defcustom org-enable-priority-commands t
  2248. "Non-nil means priority commands are active.
  2249. When nil, these commands will be disabled, so that you never accidentally
  2250. set a priority."
  2251. :group 'org-priorities
  2252. :type 'boolean)
  2253. (defcustom org-highest-priority ?A
  2254. "The highest priority of TODO items. A character like ?A, ?B etc.
  2255. Must have a smaller ASCII number than `org-lowest-priority'."
  2256. :group 'org-priorities
  2257. :type 'character)
  2258. (defcustom org-lowest-priority ?C
  2259. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2260. Must have a larger ASCII number than `org-highest-priority'."
  2261. :group 'org-priorities
  2262. :type 'character)
  2263. (defcustom org-default-priority ?B
  2264. "The default priority of TODO items.
  2265. This is the priority an item gets if no explicit priority is given.
  2266. When starting to cycle on an empty priority the first step in the cycle
  2267. depends on `org-priority-start-cycle-with-default'. The resulting first
  2268. step priority must not exceed the range from `org-highest-priority' to
  2269. `org-lowest-priority' which means that `org-default-priority' has to be
  2270. in this range exclusive or inclusive the range boundaries. Else the
  2271. first step refuses to set the default and the second will fall back
  2272. to (depending on the command used) the highest or lowest priority."
  2273. :group 'org-priorities
  2274. :type 'character)
  2275. (defcustom org-priority-start-cycle-with-default t
  2276. "Non-nil means start with default priority when starting to cycle.
  2277. When this is nil, the first step in the cycle will be (depending on the
  2278. command used) one higher or lower than the default priority.
  2279. See also `org-default-priority'."
  2280. :group 'org-priorities
  2281. :type 'boolean)
  2282. (defcustom org-get-priority-function nil
  2283. "Function to extract the priority from a string.
  2284. The string is normally the headline. If this is nil Org computes the
  2285. priority from the priority cookie like [#A] in the headline. It returns
  2286. an integer, increasing by 1000 for each priority level.
  2287. The user can set a different function here, which should take a string
  2288. as an argument and return the numeric priority."
  2289. :group 'org-priorities
  2290. :version "24.1"
  2291. :type 'function)
  2292. (defgroup org-time nil
  2293. "Options concerning time stamps and deadlines in Org-mode."
  2294. :tag "Org Time"
  2295. :group 'org)
  2296. (defcustom org-insert-labeled-timestamps-at-point nil
  2297. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2298. When nil, these labeled time stamps are forces into the second line of an
  2299. entry, just after the headline. When scheduling from the global TODO list,
  2300. the time stamp will always be forced into the second line."
  2301. :group 'org-time
  2302. :type 'boolean)
  2303. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2304. "Formats for `format-time-string' which are used for time stamps.
  2305. It is not recommended to change this constant.")
  2306. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2307. "Number of minutes to round time stamps to.
  2308. These are two values, the first applies when first creating a time stamp.
  2309. The second applies when changing it with the commands `S-up' and `S-down'.
  2310. When changing the time stamp, this means that it will change in steps
  2311. of N minutes, as given by the second value.
  2312. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2313. numbers should be factors of 60, so for example 5, 10, 15.
  2314. When this is larger than 1, you can still force an exact time stamp by using
  2315. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2316. and by using a prefix arg to `S-up/down' to specify the exact number
  2317. of minutes to shift."
  2318. :group 'org-time
  2319. :get #'(lambda (var) ; Make sure both elements are there
  2320. (if (integerp (default-value var))
  2321. (list (default-value var) 5)
  2322. (default-value var)))
  2323. :type '(list
  2324. (integer :tag "when inserting times")
  2325. (integer :tag "when modifying times")))
  2326. ;; Normalize old customizations of this variable.
  2327. (when (integerp org-time-stamp-rounding-minutes)
  2328. (setq org-time-stamp-rounding-minutes
  2329. (list org-time-stamp-rounding-minutes
  2330. org-time-stamp-rounding-minutes)))
  2331. (defcustom org-display-custom-times nil
  2332. "Non-nil means overlay custom formats over all time stamps.
  2333. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2334. To turn this on on a per-file basis, insert anywhere in the file:
  2335. #+STARTUP: customtime"
  2336. :group 'org-time
  2337. :set 'set-default
  2338. :type 'sexp)
  2339. (make-variable-buffer-local 'org-display-custom-times)
  2340. (defcustom org-time-stamp-custom-formats
  2341. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2342. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2343. These are overlaid over the default ISO format if the variable
  2344. `org-display-custom-times' is set. Time like %H:%M should be at the
  2345. end of the second format. The custom formats are also honored by export
  2346. commands, if custom time display is turned on at the time of export."
  2347. :group 'org-time
  2348. :type 'sexp)
  2349. (defun org-time-stamp-format (&optional long inactive)
  2350. "Get the right format for a time string."
  2351. (let ((f (if long (cdr org-time-stamp-formats)
  2352. (car org-time-stamp-formats))))
  2353. (if inactive
  2354. (concat "[" (substring f 1 -1) "]")
  2355. f)))
  2356. (defcustom org-time-clocksum-format "%d:%02d"
  2357. "The format string used when creating CLOCKSUM lines.
  2358. This is also used when org-mode generates a time duration."
  2359. :group 'org-time
  2360. :type 'string)
  2361. (defcustom org-time-clocksum-use-fractional nil
  2362. "If non-nil, \\[org-clock-display] uses fractional times.
  2363. org-mode generates a time duration."
  2364. :group 'org-time
  2365. :type 'boolean)
  2366. (defcustom org-time-clocksum-fractional-format "%.2f"
  2367. "The format string used when creating CLOCKSUM lines, or when
  2368. org-mode generates a time duration."
  2369. :group 'org-time
  2370. :type 'string)
  2371. (defcustom org-deadline-warning-days 14
  2372. "No. of days before expiration during which a deadline becomes active.
  2373. This variable governs the display in sparse trees and in the agenda.
  2374. When 0 or negative, it means use this number (the absolute value of it)
  2375. even if a deadline has a different individual lead time specified.
  2376. Custom commands can set this variable in the options section."
  2377. :group 'org-time
  2378. :group 'org-agenda-daily/weekly
  2379. :type 'integer)
  2380. (defcustom org-read-date-prefer-future t
  2381. "Non-nil means assume future for incomplete date input from user.
  2382. This affects the following situations:
  2383. 1. The user gives a month but not a year.
  2384. For example, if it is April and you enter \"feb 2\", this will be read
  2385. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2386. 2. The user gives a day, but no month.
  2387. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2388. read this as the third of *next* month. However, if you enter \"17\",
  2389. it will be considered as *this* month.
  2390. If you set this variable to the symbol `time', then also the following
  2391. will work:
  2392. 3. If the user gives a time.
  2393. If the time is before now, it will be interpreted as tomorrow.
  2394. Currently none of this works for ISO week specifications.
  2395. When this option is nil, the current day, month and year will always be
  2396. used as defaults.
  2397. See also `org-agenda-jump-prefer-future'."
  2398. :group 'org-time
  2399. :type '(choice
  2400. (const :tag "Never" nil)
  2401. (const :tag "Check month and day" t)
  2402. (const :tag "Check month, day, and time" time)))
  2403. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2404. "Should the agenda jump command prefer the future for incomplete dates?
  2405. The default is to do the same as configured in `org-read-date-prefer-future'.
  2406. But you can also set a deviating value here.
  2407. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2408. :group 'org-agenda
  2409. :group 'org-time
  2410. :version "24.1"
  2411. :type '(choice
  2412. (const :tag "Use org-read-date-prefer-future"
  2413. org-read-date-prefer-future)
  2414. (const :tag "Never" nil)
  2415. (const :tag "Always" t)))
  2416. (defcustom org-read-date-force-compatible-dates t
  2417. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2418. Depending on the system Emacs is running on, certain dates cannot
  2419. be represented with the type used internally to represent time.
  2420. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2421. correctly. Some systems allow for earlier dates, some for later,
  2422. some for both. One way to find out it to insert any date into an
  2423. Org buffer, putting the cursor on the year and hitting S-up and
  2424. S-down to test the range.
  2425. When this variable is set to t, the date/time prompt will not let
  2426. you specify dates outside the 1970-2037 range, so it is certain that
  2427. these dates will work in whatever version of Emacs you are
  2428. running, and also that you can move a file from one Emacs implementation
  2429. to another. WHenever Org is forcing the year for you, it will display
  2430. a message and beep.
  2431. When this variable is nil, Org will check if the date is
  2432. representable in the specific Emacs implementation you are using.
  2433. If not, it will force a year, usually the current year, and beep
  2434. to remind you. Currently this setting is not recommended because
  2435. the likelihood that you will open your Org files in an Emacs that
  2436. has limited date range is not negligible.
  2437. A workaround for this problem is to use diary sexp dates for time
  2438. stamps outside of this range."
  2439. :group 'org-time
  2440. :version "24.1"
  2441. :type 'boolean)
  2442. (defcustom org-read-date-display-live t
  2443. "Non-nil means display current interpretation of date prompt live.
  2444. This display will be in an overlay, in the minibuffer."
  2445. :group 'org-time
  2446. :type 'boolean)
  2447. (defcustom org-read-date-popup-calendar t
  2448. "Non-nil means pop up a calendar when prompting for a date.
  2449. In the calendar, the date can be selected with mouse-1. However, the
  2450. minibuffer will also be active, and you can simply enter the date as well.
  2451. When nil, only the minibuffer will be available."
  2452. :group 'org-time
  2453. :type 'boolean)
  2454. (if (fboundp 'defvaralias)
  2455. (defvaralias 'org-popup-calendar-for-date-prompt
  2456. 'org-read-date-popup-calendar))
  2457. (defcustom org-read-date-minibuffer-setup-hook nil
  2458. "Hook to be used to set up keys for the date/time interface.
  2459. Add key definitions to `minibuffer-local-map', which will be a temporary
  2460. copy."
  2461. :group 'org-time
  2462. :type 'hook)
  2463. (defcustom org-extend-today-until 0
  2464. "The hour when your day really ends. Must be an integer.
  2465. This has influence for the following applications:
  2466. - When switching the agenda to \"today\". It it is still earlier than
  2467. the time given here, the day recognized as TODAY is actually yesterday.
  2468. - When a date is read from the user and it is still before the time given
  2469. here, the current date and time will be assumed to be yesterday, 23:59.
  2470. Also, timestamps inserted in capture templates follow this rule.
  2471. IMPORTANT: This is a feature whose implementation is and likely will
  2472. remain incomplete. Really, it is only here because past midnight seems to
  2473. be the favorite working time of John Wiegley :-)"
  2474. :group 'org-time
  2475. :type 'integer)
  2476. (defcustom org-use-effective-time nil
  2477. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2478. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2479. \"effective time\" of any timestamps between midnight and 8am will be
  2480. 23:59 of the previous day."
  2481. :group 'org-time
  2482. :version "24.1"
  2483. :type 'boolean)
  2484. (defcustom org-edit-timestamp-down-means-later nil
  2485. "Non-nil means S-down will increase the time in a time stamp.
  2486. When nil, S-up will increase."
  2487. :group 'org-time
  2488. :type 'boolean)
  2489. (defcustom org-calendar-follow-timestamp-change t
  2490. "Non-nil means make the calendar window follow timestamp changes.
  2491. When a timestamp is modified and the calendar window is visible, it will be
  2492. moved to the new date."
  2493. :group 'org-time
  2494. :type 'boolean)
  2495. (defgroup org-tags nil
  2496. "Options concerning tags in Org-mode."
  2497. :tag "Org Tags"
  2498. :group 'org)
  2499. (defcustom org-tag-alist nil
  2500. "List of tags allowed in Org-mode files.
  2501. When this list is nil, Org-mode will base TAG input on what is already in the
  2502. buffer.
  2503. The value of this variable is an alist, the car of each entry must be a
  2504. keyword as a string, the cdr may be a character that is used to select
  2505. that tag through the fast-tag-selection interface.
  2506. See the manual for details."
  2507. :group 'org-tags
  2508. :type '(repeat
  2509. (choice
  2510. (cons (string :tag "Tag name")
  2511. (character :tag "Access char"))
  2512. (list :tag "Start radio group"
  2513. (const :startgroup)
  2514. (option (string :tag "Group description")))
  2515. (list :tag "End radio group"
  2516. (const :endgroup)
  2517. (option (string :tag "Group description")))
  2518. (const :tag "New line" (:newline)))))
  2519. (defcustom org-tag-persistent-alist nil
  2520. "List of tags that will always appear in all Org-mode files.
  2521. This is in addition to any in buffer settings or customizations
  2522. of `org-tag-alist'.
  2523. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2524. The value of this variable is an alist, the car of each entry must be a
  2525. keyword as a string, the cdr may be a character that is used to select
  2526. that tag through the fast-tag-selection interface.
  2527. See the manual for details.
  2528. To disable these tags on a per-file basis, insert anywhere in the file:
  2529. #+STARTUP: noptag"
  2530. :group 'org-tags
  2531. :type '(repeat
  2532. (choice
  2533. (cons (string :tag "Tag name")
  2534. (character :tag "Access char"))
  2535. (const :tag "Start radio group" (:startgroup))
  2536. (const :tag "End radio group" (:endgroup))
  2537. (const :tag "New line" (:newline)))))
  2538. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2539. "If non-nil, always offer completion for all tags of all agenda files.
  2540. Instead of customizing this variable directly, you might want to
  2541. set it locally for capture buffers, because there no list of
  2542. tags in that file can be created dynamically (there are none).
  2543. (add-hook 'org-capture-mode-hook
  2544. (lambda ()
  2545. (set (make-local-variable
  2546. 'org-complete-tags-always-offer-all-agenda-tags)
  2547. t)))"
  2548. :group 'org-tags
  2549. :version "24.1"
  2550. :type 'boolean)
  2551. (defvar org-file-tags nil
  2552. "List of tags that can be inherited by all entries in the file.
  2553. The tags will be inherited if the variable `org-use-tag-inheritance'
  2554. says they should be.
  2555. This variable is populated from #+FILETAGS lines.")
  2556. (defcustom org-use-fast-tag-selection 'auto
  2557. "Non-nil means use fast tag selection scheme.
  2558. This is a special interface to select and deselect tags with single keys.
  2559. When nil, fast selection is never used.
  2560. When the symbol `auto', fast selection is used if and only if selection
  2561. characters for tags have been configured, either through the variable
  2562. `org-tag-alist' or through a #+TAGS line in the buffer.
  2563. When t, fast selection is always used and selection keys are assigned
  2564. automatically if necessary."
  2565. :group 'org-tags
  2566. :type '(choice
  2567. (const :tag "Always" t)
  2568. (const :tag "Never" nil)
  2569. (const :tag "When selection characters are configured" 'auto)))
  2570. (defcustom org-fast-tag-selection-single-key nil
  2571. "Non-nil means fast tag selection exits after first change.
  2572. When nil, you have to press RET to exit it.
  2573. During fast tag selection, you can toggle this flag with `C-c'.
  2574. This variable can also have the value `expert'. In this case, the window
  2575. displaying the tags menu is not even shown, until you press C-c again."
  2576. :group 'org-tags
  2577. :type '(choice
  2578. (const :tag "No" nil)
  2579. (const :tag "Yes" t)
  2580. (const :tag "Expert" expert)))
  2581. (defvar org-fast-tag-selection-include-todo nil
  2582. "Non-nil means fast tags selection interface will also offer TODO states.
  2583. This is an undocumented feature, you should not rely on it.")
  2584. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2585. "The column to which tags should be indented in a headline.
  2586. If this number is positive, it specifies the column. If it is negative,
  2587. it means that the tags should be flushright to that column. For example,
  2588. -80 works well for a normal 80 character screen.
  2589. When 0, place tags directly after headline text, with only one space in
  2590. between."
  2591. :group 'org-tags
  2592. :type 'integer)
  2593. (defcustom org-auto-align-tags t
  2594. "Non-nil keeps tags aligned when modifying headlines.
  2595. Some operations (i.e. demoting) change the length of a headline and
  2596. therefore shift the tags around. With this option turned on, after
  2597. each such operation the tags are again aligned to `org-tags-column'."
  2598. :group 'org-tags
  2599. :type 'boolean)
  2600. (defcustom org-use-tag-inheritance t
  2601. "Non-nil means tags in levels apply also for sublevels.
  2602. When nil, only the tags directly given in a specific line apply there.
  2603. This may also be a list of tags that should be inherited, or a regexp that
  2604. matches tags that should be inherited. Additional control is possible
  2605. with the variable `org-tags-exclude-from-inheritance' which gives an
  2606. explicit list of tags to be excluded from inheritance, even if the value of
  2607. `org-use-tag-inheritance' would select it for inheritance.
  2608. If this option is t, a match early-on in a tree can lead to a large
  2609. number of matches in the subtree when constructing the agenda or creating
  2610. a sparse tree. If you only want to see the first match in a tree during
  2611. a search, check out the variable `org-tags-match-list-sublevels'."
  2612. :group 'org-tags
  2613. :type '(choice
  2614. (const :tag "Not" nil)
  2615. (const :tag "Always" t)
  2616. (repeat :tag "Specific tags" (string :tag "Tag"))
  2617. (regexp :tag "Tags matched by regexp")))
  2618. (defcustom org-tags-exclude-from-inheritance nil
  2619. "List of tags that should never be inherited.
  2620. This is a way to exclude a few tags from inheritance. For way to do
  2621. the opposite, to actively allow inheritance for selected tags,
  2622. see the variable `org-use-tag-inheritance'."
  2623. :group 'org-tags
  2624. :type '(repeat (string :tag "Tag")))
  2625. (defun org-tag-inherit-p (tag)
  2626. "Check if TAG is one that should be inherited."
  2627. (cond
  2628. ((member tag org-tags-exclude-from-inheritance) nil)
  2629. ((eq org-use-tag-inheritance t) t)
  2630. ((not org-use-tag-inheritance) nil)
  2631. ((stringp org-use-tag-inheritance)
  2632. (string-match org-use-tag-inheritance tag))
  2633. ((listp org-use-tag-inheritance)
  2634. (member tag org-use-tag-inheritance))
  2635. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2636. (defcustom org-tags-match-list-sublevels t
  2637. "Non-nil means list also sublevels of headlines matching a search.
  2638. This variable applies to tags/property searches, and also to stuck
  2639. projects because this search is based on a tags match as well.
  2640. When set to the symbol `indented', sublevels are indented with
  2641. leading dots.
  2642. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2643. the sublevels of a headline matching a tag search often also match
  2644. the same search. Listing all of them can create very long lists.
  2645. Setting this variable to nil causes subtrees of a match to be skipped.
  2646. This variable is semi-obsolete and probably should always be true. It
  2647. is better to limit inheritance to certain tags using the variables
  2648. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2649. :group 'org-tags
  2650. :type '(choice
  2651. (const :tag "No, don't list them" nil)
  2652. (const :tag "Yes, do list them" t)
  2653. (const :tag "List them, indented with leading dots" indented)))
  2654. (defcustom org-tags-sort-function nil
  2655. "When set, tags are sorted using this function as a comparator."
  2656. :group 'org-tags
  2657. :type '(choice
  2658. (const :tag "No sorting" nil)
  2659. (const :tag "Alphabetical" string<)
  2660. (const :tag "Reverse alphabetical" string>)
  2661. (function :tag "Custom function" nil)))
  2662. (defvar org-tags-history nil
  2663. "History of minibuffer reads for tags.")
  2664. (defvar org-last-tags-completion-table nil
  2665. "The last used completion table for tags.")
  2666. (defvar org-after-tags-change-hook nil
  2667. "Hook that is run after the tags in a line have changed.")
  2668. (defgroup org-properties nil
  2669. "Options concerning properties in Org-mode."
  2670. :tag "Org Properties"
  2671. :group 'org)
  2672. (defcustom org-property-format "%-10s %s"
  2673. "How property key/value pairs should be formatted by `indent-line'.
  2674. When `indent-line' hits a property definition, it will format the line
  2675. according to this format, mainly to make sure that the values are
  2676. lined-up with respect to each other."
  2677. :group 'org-properties
  2678. :type 'string)
  2679. (defcustom org-properties-postprocess-alist nil
  2680. "Alist of properties and functions to adjust inserted values.
  2681. Elements of this alist must be of the form
  2682. ([string] [function])
  2683. where [string] must be a property name and [function] must be a
  2684. lambda expression: this lambda expression must take one argument,
  2685. the value to adjust, and return the new value as a string.
  2686. For example, this element will allow the property \"Remaining\"
  2687. to be updated wrt the relation between the \"Effort\" property
  2688. and the clock summary:
  2689. ((\"Remaining\" (lambda(value)
  2690. (let ((clocksum (org-clock-sum-current-item))
  2691. (effort (org-duration-string-to-minutes
  2692. (org-entry-get (point) \"Effort\"))))
  2693. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2694. :group 'org-properties
  2695. :version "24.1"
  2696. :type '(alist :key-type (string :tag "Property")
  2697. :value-type (function :tag "Function")))
  2698. (defcustom org-use-property-inheritance nil
  2699. "Non-nil means properties apply also for sublevels.
  2700. This setting is chiefly used during property searches. Turning it on can
  2701. cause significant overhead when doing a search, which is why it is not
  2702. on by default.
  2703. When nil, only the properties directly given in the current entry count.
  2704. When t, every property is inherited. The value may also be a list of
  2705. properties that should have inheritance, or a regular expression matching
  2706. properties that should be inherited.
  2707. However, note that some special properties use inheritance under special
  2708. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2709. and the properties ending in \"_ALL\" when they are used as descriptor
  2710. for valid values of a property.
  2711. Note for programmers:
  2712. When querying an entry with `org-entry-get', you can control if inheritance
  2713. should be used. By default, `org-entry-get' looks only at the local
  2714. properties. You can request inheritance by setting the inherit argument
  2715. to t (to force inheritance) or to `selective' (to respect the setting
  2716. in this variable)."
  2717. :group 'org-properties
  2718. :type '(choice
  2719. (const :tag "Not" nil)
  2720. (const :tag "Always" t)
  2721. (repeat :tag "Specific properties" (string :tag "Property"))
  2722. (regexp :tag "Properties matched by regexp")))
  2723. (defun org-property-inherit-p (property)
  2724. "Check if PROPERTY is one that should be inherited."
  2725. (cond
  2726. ((eq org-use-property-inheritance t) t)
  2727. ((not org-use-property-inheritance) nil)
  2728. ((stringp org-use-property-inheritance)
  2729. (string-match org-use-property-inheritance property))
  2730. ((listp org-use-property-inheritance)
  2731. (member property org-use-property-inheritance))
  2732. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2733. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2734. "The default column format, if no other format has been defined.
  2735. This variable can be set on the per-file basis by inserting a line
  2736. #+COLUMNS: %25ITEM ....."
  2737. :group 'org-properties
  2738. :type 'string)
  2739. (defcustom org-columns-ellipses ".."
  2740. "The ellipses to be used when a field in column view is truncated.
  2741. When this is the empty string, as many characters as possible are shown,
  2742. but then there will be no visual indication that the field has been truncated.
  2743. When this is a string of length N, the last N characters of a truncated
  2744. field are replaced by this string. If the column is narrower than the
  2745. ellipses string, only part of the ellipses string will be shown."
  2746. :group 'org-properties
  2747. :type 'string)
  2748. (defcustom org-columns-modify-value-for-display-function nil
  2749. "Function that modifies values for display in column view.
  2750. For example, it can be used to cut out a certain part from a time stamp.
  2751. The function must take 2 arguments:
  2752. column-title The title of the column (*not* the property name)
  2753. value The value that should be modified.
  2754. The function should return the value that should be displayed,
  2755. or nil if the normal value should be used."
  2756. :group 'org-properties
  2757. :type 'function)
  2758. (defcustom org-effort-property "Effort"
  2759. "The property that is being used to keep track of effort estimates.
  2760. Effort estimates given in this property need to have the format H:MM."
  2761. :group 'org-properties
  2762. :group 'org-progress
  2763. :type '(string :tag "Property"))
  2764. (defconst org-global-properties-fixed
  2765. '(("VISIBILITY_ALL" . "folded children content all")
  2766. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2767. "List of property/value pairs that can be inherited by any entry.
  2768. These are fixed values, for the preset properties. The user variable
  2769. that can be used to add to this list is `org-global-properties'.
  2770. The entries in this list are cons cells where the car is a property
  2771. name and cdr is a string with the value. If the value represents
  2772. multiple items like an \"_ALL\" property, separate the items by
  2773. spaces.")
  2774. (defcustom org-global-properties nil
  2775. "List of property/value pairs that can be inherited by any entry.
  2776. This list will be combined with the constant `org-global-properties-fixed'.
  2777. The entries in this list are cons cells where the car is a property
  2778. name and cdr is a string with the value.
  2779. You can set buffer-local values for the same purpose in the variable
  2780. `org-file-properties' this by adding lines like
  2781. #+PROPERTY: NAME VALUE"
  2782. :group 'org-properties
  2783. :type '(repeat
  2784. (cons (string :tag "Property")
  2785. (string :tag "Value"))))
  2786. (defvar org-file-properties nil
  2787. "List of property/value pairs that can be inherited by any entry.
  2788. Valid for the current buffer.
  2789. This variable is populated from #+PROPERTY lines.")
  2790. (make-variable-buffer-local 'org-file-properties)
  2791. (defgroup org-agenda nil
  2792. "Options concerning agenda views in Org-mode."
  2793. :tag "Org Agenda"
  2794. :group 'org)
  2795. (defvar org-category nil
  2796. "Variable used by org files to set a category for agenda display.
  2797. Such files should use a file variable to set it, for example
  2798. # -*- mode: org; org-category: \"ELisp\"
  2799. or contain a special line
  2800. #+CATEGORY: ELisp
  2801. If the file does not specify a category, then file's base name
  2802. is used instead.")
  2803. (make-variable-buffer-local 'org-category)
  2804. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2805. (defcustom org-agenda-files nil
  2806. "The files to be used for agenda display.
  2807. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2808. \\[org-remove-file]. You can also use customize to edit the list.
  2809. If an entry is a directory, all files in that directory that are matched by
  2810. `org-agenda-file-regexp' will be part of the file list.
  2811. If the value of the variable is not a list but a single file name, then
  2812. the list of agenda files is actually stored and maintained in that file, one
  2813. agenda file per line. In this file paths can be given relative to
  2814. `org-directory'. Tilde expansion and environment variable substitution
  2815. are also made."
  2816. :group 'org-agenda
  2817. :type '(choice
  2818. (repeat :tag "List of files and directories" file)
  2819. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2820. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2821. "Regular expression to match files for `org-agenda-files'.
  2822. If any element in the list in that variable contains a directory instead
  2823. of a normal file, all files in that directory that are matched by this
  2824. regular expression will be included."
  2825. :group 'org-agenda
  2826. :type 'regexp)
  2827. (defcustom org-agenda-text-search-extra-files nil
  2828. "List of extra files to be searched by text search commands.
  2829. These files will be search in addition to the agenda files by the
  2830. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2831. Note that these files will only be searched for text search commands,
  2832. not for the other agenda views like todo lists, tag searches or the weekly
  2833. agenda. This variable is intended to list notes and possibly archive files
  2834. that should also be searched by these two commands.
  2835. In fact, if the first element in the list is the symbol `agenda-archives',
  2836. than all archive files of all agenda files will be added to the search
  2837. scope."
  2838. :group 'org-agenda
  2839. :type '(set :greedy t
  2840. (const :tag "Agenda Archives" agenda-archives)
  2841. (repeat :inline t (file))))
  2842. (if (fboundp 'defvaralias)
  2843. (defvaralias 'org-agenda-multi-occur-extra-files
  2844. 'org-agenda-text-search-extra-files))
  2845. (defcustom org-agenda-skip-unavailable-files nil
  2846. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2847. A nil value means to remove them, after a query, from the list."
  2848. :group 'org-agenda
  2849. :type 'boolean)
  2850. (defcustom org-calendar-to-agenda-key [?c]
  2851. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2852. The command `org-calendar-goto-agenda' will be bound to this key. The
  2853. default is the character `c' because then `c' can be used to switch back and
  2854. forth between agenda and calendar."
  2855. :group 'org-agenda
  2856. :type 'sexp)
  2857. (defcustom org-calendar-insert-diary-entry-key [?i]
  2858. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2859. This option is irrelevant until `org-agenda-diary-file' has been configured
  2860. to point to an Org-mode file. When that is the case, the command
  2861. `org-agenda-diary-entry' will be bound to the key given here, by default
  2862. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2863. if you want to continue doing this, you need to change this to a different
  2864. key."
  2865. :group 'org-agenda
  2866. :type 'sexp)
  2867. (defcustom org-agenda-diary-file 'diary-file
  2868. "File to which to add new entries with the `i' key in agenda and calendar.
  2869. When this is the symbol `diary-file', the functionality in the Emacs
  2870. calendar will be used to add entries to the `diary-file'. But when this
  2871. points to a file, `org-agenda-diary-entry' will be used instead."
  2872. :group 'org-agenda
  2873. :type '(choice
  2874. (const :tag "The standard Emacs diary file" diary-file)
  2875. (file :tag "Special Org file diary entries")))
  2876. (eval-after-load "calendar"
  2877. '(progn
  2878. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2879. 'org-calendar-goto-agenda)
  2880. (add-hook 'calendar-mode-hook
  2881. (lambda ()
  2882. (unless (eq org-agenda-diary-file 'diary-file)
  2883. (define-key calendar-mode-map
  2884. org-calendar-insert-diary-entry-key
  2885. 'org-agenda-diary-entry))))))
  2886. (defgroup org-latex nil
  2887. "Options for embedding LaTeX code into Org-mode."
  2888. :tag "Org LaTeX"
  2889. :group 'org)
  2890. (defcustom org-format-latex-options
  2891. '(:foreground default :background default :scale 1.0
  2892. :html-foreground "Black" :html-background "Transparent"
  2893. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2894. "Options for creating images from LaTeX fragments.
  2895. This is a property list with the following properties:
  2896. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2897. `default' means use the foreground of the default face.
  2898. :background the background color, or \"Transparent\".
  2899. `default' means use the background of the default face.
  2900. :scale a scaling factor for the size of the images, to get more pixels
  2901. :html-foreground, :html-background, :html-scale
  2902. the same numbers for HTML export.
  2903. :matchers a list indicating which matchers should be used to
  2904. find LaTeX fragments. Valid members of this list are:
  2905. \"begin\" find environments
  2906. \"$1\" find single characters surrounded by $.$
  2907. \"$\" find math expressions surrounded by $...$
  2908. \"$$\" find math expressions surrounded by $$....$$
  2909. \"\\(\" find math expressions surrounded by \\(...\\)
  2910. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2911. :group 'org-latex
  2912. :type 'plist)
  2913. (defcustom org-format-latex-signal-error t
  2914. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2915. When nil, just push out a message."
  2916. :group 'org-latex
  2917. :version "24.1"
  2918. :type 'boolean)
  2919. (defcustom org-latex-to-mathml-jar-file nil
  2920. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2921. Use this to specify additional executable file say a jar file.
  2922. When using MathToWeb as the converter, specify the full-path to
  2923. your mathtoweb.jar file."
  2924. :group 'org-latex
  2925. :version "24.1"
  2926. :type '(choice
  2927. (const :tag "None" nil)
  2928. (file :tag "JAR file" :must-match t)))
  2929. (defcustom org-latex-to-mathml-convert-command nil
  2930. "Command to convert LaTeX fragments to MathML.
  2931. Replace format-specifiers in the command as noted below and use
  2932. `shell-command' to convert LaTeX to MathML.
  2933. %j: Executable file in fully expanded form as specified by
  2934. `org-latex-to-mathml-jar-file'.
  2935. %I: Input LaTeX file in fully expanded form
  2936. %o: Output MathML file
  2937. This command is used by `org-create-math-formula'.
  2938. When using MathToWeb as the converter, set this to
  2939. \"java -jar %j -unicode -force -df %o %I\"."
  2940. :group 'org-latex
  2941. :version "24.1"
  2942. :type '(choice
  2943. (const :tag "None" nil)
  2944. (string :tag "\nShell command")))
  2945. (defcustom org-latex-create-formula-image-program 'dvipng
  2946. "Program to convert LaTeX fragments with.
  2947. dvipng Process the LaTeX fragments to dvi file, then convert
  2948. dvi files to png files using dvipng.
  2949. This will also include processing of non-math environments.
  2950. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  2951. to convert pdf files to png files"
  2952. :group 'org-latex
  2953. :version "24.1"
  2954. :type '(choice
  2955. (const :tag "dvipng" dvipng)
  2956. (const :tag "imagemagick" imagemagick)))
  2957. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  2958. "Path to store latex preview images. A relative path here creates many
  2959. directories relative to the processed org files paths. An absolute path
  2960. puts all preview images at the same place."
  2961. :group 'org-latex
  2962. :version "24.3"
  2963. :type 'string)
  2964. (defun org-format-latex-mathml-available-p ()
  2965. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2966. (save-match-data
  2967. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2968. org-latex-to-mathml-convert-command)
  2969. (let ((executable (car (split-string
  2970. org-latex-to-mathml-convert-command))))
  2971. (when (executable-find executable)
  2972. (if (string-match
  2973. "%j" org-latex-to-mathml-convert-command)
  2974. (file-readable-p org-latex-to-mathml-jar-file)
  2975. t))))))
  2976. (defcustom org-format-latex-header "\\documentclass{article}
  2977. \\usepackage[usenames]{color}
  2978. \\usepackage{amsmath}
  2979. \\usepackage[mathscr]{eucal}
  2980. \\pagestyle{empty} % do not remove
  2981. \[PACKAGES]
  2982. \[DEFAULT-PACKAGES]
  2983. % The settings below are copied from fullpage.sty
  2984. \\setlength{\\textwidth}{\\paperwidth}
  2985. \\addtolength{\\textwidth}{-3cm}
  2986. \\setlength{\\oddsidemargin}{1.5cm}
  2987. \\addtolength{\\oddsidemargin}{-2.54cm}
  2988. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2989. \\setlength{\\textheight}{\\paperheight}
  2990. \\addtolength{\\textheight}{-\\headheight}
  2991. \\addtolength{\\textheight}{-\\headsep}
  2992. \\addtolength{\\textheight}{-\\footskip}
  2993. \\addtolength{\\textheight}{-3cm}
  2994. \\setlength{\\topmargin}{1.5cm}
  2995. \\addtolength{\\topmargin}{-2.54cm}"
  2996. "The document header used for processing LaTeX fragments.
  2997. It is imperative that this header make sure that no page number
  2998. appears on the page. The package defined in the variables
  2999. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  3000. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  3001. will be appended."
  3002. :group 'org-latex
  3003. :type 'string)
  3004. (defvar org-format-latex-header-extra nil)
  3005. (defun org-set-packages-alist (var val)
  3006. "Set the packages alist and make sure it has 3 elements per entry."
  3007. (set var (mapcar (lambda (x)
  3008. (if (and (consp x) (= (length x) 2))
  3009. (list (car x) (nth 1 x) t)
  3010. x))
  3011. val)))
  3012. (defun org-get-packages-alist (var)
  3013. "Get the packages alist and make sure it has 3 elements per entry."
  3014. (mapcar (lambda (x)
  3015. (if (and (consp x) (= (length x) 2))
  3016. (list (car x) (nth 1 x) t)
  3017. x))
  3018. (default-value var)))
  3019. ;; The following variables are defined here because is it also used
  3020. ;; when formatting latex fragments. Originally it was part of the
  3021. ;; LaTeX exporter, which is why the name includes "export".
  3022. (defcustom org-export-latex-default-packages-alist
  3023. '(("AUTO" "inputenc" t)
  3024. ("T1" "fontenc" t)
  3025. ("" "fixltx2e" nil)
  3026. ("" "graphicx" t)
  3027. ("" "longtable" nil)
  3028. ("" "float" nil)
  3029. ("" "wrapfig" nil)
  3030. ("" "soul" t)
  3031. ("" "textcomp" t)
  3032. ("" "marvosym" t)
  3033. ("" "wasysym" t)
  3034. ("" "latexsym" t)
  3035. ("" "amssymb" t)
  3036. ("" "hyperref" nil)
  3037. "\\tolerance=1000"
  3038. )
  3039. "Alist of default packages to be inserted in the header.
  3040. Change this only if one of the packages here causes an incompatibility
  3041. with another package you are using.
  3042. The packages in this list are needed by one part or another of Org-mode
  3043. to function properly.
  3044. - inputenc, fontenc: for basic font and character selection
  3045. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3046. for interpreting the entities in `org-entities'. You can skip some of these
  3047. packages if you don't use any of the symbols in it.
  3048. - graphicx: for including images
  3049. - float, wrapfig: for figure placement
  3050. - longtable: for long tables
  3051. - hyperref: for cross references
  3052. Therefore you should not modify this variable unless you know what you
  3053. are doing. The one reason to change it anyway is that you might be loading
  3054. some other package that conflicts with one of the default packages.
  3055. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3056. If SNIPPET-FLAG is t, the package also needs to be included when
  3057. compiling LaTeX snippets into images for inclusion into HTML."
  3058. :group 'org-export-latex
  3059. :set 'org-set-packages-alist
  3060. :get 'org-get-packages-alist
  3061. :version "24.1"
  3062. :type '(repeat
  3063. (choice
  3064. (list :tag "options/package pair"
  3065. (string :tag "options")
  3066. (string :tag "package")
  3067. (boolean :tag "Snippet"))
  3068. (string :tag "A line of LaTeX"))))
  3069. (defcustom org-export-latex-packages-alist nil
  3070. "Alist of packages to be inserted in every LaTeX header.
  3071. These will be inserted after `org-export-latex-default-packages-alist'.
  3072. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3073. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3074. turning LaTeX snippets into images for inclusion into HTML.
  3075. Make sure that you only list packages here which:
  3076. - you want in every file
  3077. - do not conflict with the default packages in
  3078. `org-export-latex-default-packages-alist'
  3079. - do not conflict with the setup in `org-format-latex-header'."
  3080. :group 'org-export-latex
  3081. :set 'org-set-packages-alist
  3082. :get 'org-get-packages-alist
  3083. :type '(repeat
  3084. (choice
  3085. (list :tag "options/package pair"
  3086. (string :tag "options")
  3087. (string :tag "package")
  3088. (boolean :tag "Snippet"))
  3089. (string :tag "A line of LaTeX"))))
  3090. (defgroup org-appearance nil
  3091. "Settings for Org-mode appearance."
  3092. :tag "Org Appearance"
  3093. :group 'org)
  3094. (defcustom org-level-color-stars-only nil
  3095. "Non-nil means fontify only the stars in each headline.
  3096. When nil, the entire headline is fontified.
  3097. Changing it requires restart of `font-lock-mode' to become effective
  3098. also in regions already fontified."
  3099. :group 'org-appearance
  3100. :type 'boolean)
  3101. (defcustom org-hide-leading-stars nil
  3102. "Non-nil means hide the first N-1 stars in a headline.
  3103. This works by using the face `org-hide' for these stars. This
  3104. face is white for a light background, and black for a dark
  3105. background. You may have to customize the face `org-hide' to
  3106. make this work.
  3107. Changing it requires restart of `font-lock-mode' to become effective
  3108. also in regions already fontified.
  3109. You may also set this on a per-file basis by adding one of the following
  3110. lines to the buffer:
  3111. #+STARTUP: hidestars
  3112. #+STARTUP: showstars"
  3113. :group 'org-appearance
  3114. :type 'boolean)
  3115. (defcustom org-hidden-keywords nil
  3116. "List of symbols corresponding to keywords to be hidden the org buffer.
  3117. For example, a value '(title) for this list will make the document's title
  3118. appear in the buffer without the initial #+TITLE: keyword."
  3119. :group 'org-appearance
  3120. :version "24.1"
  3121. :type '(set (const :tag "#+AUTHOR" author)
  3122. (const :tag "#+DATE" date)
  3123. (const :tag "#+EMAIL" email)
  3124. (const :tag "#+TITLE" title)))
  3125. (defcustom org-custom-properties nil
  3126. "List of properties (as strings) with a special meaning.
  3127. The default use of these custom properties is to let the user
  3128. hide them with `org-toggle-custom-properties-visibility'."
  3129. :group 'org-properties
  3130. :group 'org-appearance
  3131. :version "24.3"
  3132. :type '(repeat (string :tag "Property Name")))
  3133. (defcustom org-fontify-done-headline nil
  3134. "Non-nil means change the face of a headline if it is marked DONE.
  3135. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3136. When this is non-nil, the headline after the keyword is set to the
  3137. `org-headline-done' as an additional indication."
  3138. :group 'org-appearance
  3139. :type 'boolean)
  3140. (defcustom org-fontify-emphasized-text t
  3141. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3142. Changing this variable requires a restart of Emacs to take effect."
  3143. :group 'org-appearance
  3144. :type 'boolean)
  3145. (defcustom org-fontify-whole-heading-line nil
  3146. "Non-nil means fontify the whole line for headings.
  3147. This is useful when setting a background color for the
  3148. org-level-* faces."
  3149. :group 'org-appearance
  3150. :type 'boolean)
  3151. (defcustom org-highlight-latex-fragments-and-specials nil
  3152. "Non-nil means fontify what is treated specially by the exporters."
  3153. :group 'org-appearance
  3154. :type 'boolean)
  3155. (defcustom org-hide-emphasis-markers nil
  3156. "Non-nil mean font-lock should hide the emphasis marker characters."
  3157. :group 'org-appearance
  3158. :type 'boolean)
  3159. (defcustom org-pretty-entities nil
  3160. "Non-nil means show entities as UTF8 characters.
  3161. When nil, the \\name form remains in the buffer."
  3162. :group 'org-appearance
  3163. :version "24.1"
  3164. :type 'boolean)
  3165. (defcustom org-pretty-entities-include-sub-superscripts t
  3166. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3167. :group 'org-appearance
  3168. :version "24.1"
  3169. :type 'boolean)
  3170. (defvar org-emph-re nil
  3171. "Regular expression for matching emphasis.
  3172. After a match, the match groups contain these elements:
  3173. 0 The match of the full regular expression, including the characters
  3174. before and after the proper match
  3175. 1 The character before the proper match, or empty at beginning of line
  3176. 2 The proper match, including the leading and trailing markers
  3177. 3 The leading marker like * or /, indicating the type of highlighting
  3178. 4 The text between the emphasis markers, not including the markers
  3179. 5 The character after the match, empty at the end of a line")
  3180. (defvar org-verbatim-re nil
  3181. "Regular expression for matching verbatim text.")
  3182. (defvar org-emphasis-regexp-components) ; defined just below
  3183. (defvar org-emphasis-alist) ; defined just below
  3184. (defun org-set-emph-re (var val)
  3185. "Set variable and compute the emphasis regular expression."
  3186. (set var val)
  3187. (when (and (boundp 'org-emphasis-alist)
  3188. (boundp 'org-emphasis-regexp-components)
  3189. org-emphasis-alist org-emphasis-regexp-components)
  3190. (let* ((e org-emphasis-regexp-components)
  3191. (pre (car e))
  3192. (post (nth 1 e))
  3193. (border (nth 2 e))
  3194. (body (nth 3 e))
  3195. (nl (nth 4 e))
  3196. (body1 (concat body "*?"))
  3197. (markers (mapconcat 'car org-emphasis-alist ""))
  3198. (vmarkers (mapconcat
  3199. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3200. org-emphasis-alist "")))
  3201. ;; make sure special characters appear at the right position in the class
  3202. (if (string-match "\\^" markers)
  3203. (setq markers (concat (replace-match "" t t markers) "^")))
  3204. (if (string-match "-" markers)
  3205. (setq markers (concat (replace-match "" t t markers) "-")))
  3206. (if (string-match "\\^" vmarkers)
  3207. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3208. (if (string-match "-" vmarkers)
  3209. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3210. (if (> nl 0)
  3211. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3212. (int-to-string nl) "\\}")))
  3213. ;; Make the regexp
  3214. (setq org-emph-re
  3215. (concat "\\([" pre "]\\|^\\)"
  3216. "\\("
  3217. "\\([" markers "]\\)"
  3218. "\\("
  3219. "[^" border "]\\|"
  3220. "[^" border "]"
  3221. body1
  3222. "[^" border "]"
  3223. "\\)"
  3224. "\\3\\)"
  3225. "\\([" post "]\\|$\\)"))
  3226. (setq org-verbatim-re
  3227. (concat "\\([" pre "]\\|^\\)"
  3228. "\\("
  3229. "\\([" vmarkers "]\\)"
  3230. "\\("
  3231. "[^" border "]\\|"
  3232. "[^" border "]"
  3233. body1
  3234. "[^" border "]"
  3235. "\\)"
  3236. "\\3\\)"
  3237. "\\([" post "]\\|$\\)")))))
  3238. (defcustom org-emphasis-regexp-components
  3239. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3240. "Components used to build the regular expression for emphasis.
  3241. This is a list with five entries. Terminology: In an emphasis string
  3242. like \" *strong word* \", we call the initial space PREMATCH, the final
  3243. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3244. and \"trong wor\" is the body. The different components in this variable
  3245. specify what is allowed/forbidden in each part:
  3246. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3247. post Chars allowed as postmatch. End of line will be allowed too.
  3248. border The chars *forbidden* as border characters.
  3249. body-regexp A regexp like \".\" to match a body character. Don't use
  3250. non-shy groups here, and don't allow newline here.
  3251. newline The maximum number of newlines allowed in an emphasis exp.
  3252. Use customize to modify this, or restart Emacs after changing it."
  3253. :group 'org-appearance
  3254. :set 'org-set-emph-re
  3255. :type '(list
  3256. (sexp :tag "Allowed chars in pre ")
  3257. (sexp :tag "Allowed chars in post ")
  3258. (sexp :tag "Forbidden chars in border ")
  3259. (sexp :tag "Regexp for body ")
  3260. (integer :tag "number of newlines allowed")
  3261. (option (boolean :tag "Please ignore this button"))))
  3262. (defcustom org-emphasis-alist
  3263. `(("*" bold "<b>" "</b>")
  3264. ("/" italic "<i>" "</i>")
  3265. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3266. ("=" org-code "<code>" "</code>" verbatim)
  3267. ("~" org-verbatim "<code>" "</code>" verbatim)
  3268. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3269. "<del>" "</del>")
  3270. )
  3271. "Special syntax for emphasized text.
  3272. Text starting and ending with a special character will be emphasized, for
  3273. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3274. characters, the face to be used by font-lock for highlighting in Org-mode
  3275. Emacs buffers, and the HTML tags to be used for this.
  3276. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3277. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3278. Use customize to modify this, or restart Emacs after changing it."
  3279. :group 'org-appearance
  3280. :set 'org-set-emph-re
  3281. :type '(repeat
  3282. (list
  3283. (string :tag "Marker character")
  3284. (choice
  3285. (face :tag "Font-lock-face")
  3286. (plist :tag "Face property list"))
  3287. (string :tag "HTML start tag")
  3288. (string :tag "HTML end tag")
  3289. (option (const verbatim)))))
  3290. (defvar org-protecting-blocks
  3291. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3292. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3293. This is needed for font-lock setup.")
  3294. ;;; Miscellaneous options
  3295. (defgroup org-completion nil
  3296. "Completion in Org-mode."
  3297. :tag "Org Completion"
  3298. :group 'org)
  3299. (defcustom org-completion-use-ido nil
  3300. "Non-nil means use ido completion wherever possible.
  3301. Note that `ido-mode' must be active for this variable to be relevant.
  3302. If you decide to turn this variable on, you might well want to turn off
  3303. `org-outline-path-complete-in-steps'.
  3304. See also `org-completion-use-iswitchb'."
  3305. :group 'org-completion
  3306. :type 'boolean)
  3307. (defcustom org-completion-use-iswitchb nil
  3308. "Non-nil means use iswitchb completion wherever possible.
  3309. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3310. If you decide to turn this variable on, you might well want to turn off
  3311. `org-outline-path-complete-in-steps'.
  3312. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3313. :group 'org-completion
  3314. :type 'boolean)
  3315. (defcustom org-completion-fallback-command 'hippie-expand
  3316. "The expansion command called by \\[pcomplete] in normal context.
  3317. Normal means, no org-mode-specific context."
  3318. :group 'org-completion
  3319. :type 'function)
  3320. ;;; Functions and variables from their packages
  3321. ;; Declared here to avoid compiler warnings
  3322. ;; XEmacs only
  3323. (defvar outline-mode-menu-heading)
  3324. (defvar outline-mode-menu-show)
  3325. (defvar outline-mode-menu-hide)
  3326. (defvar zmacs-regions) ; XEmacs regions
  3327. ;; Emacs only
  3328. (defvar mark-active)
  3329. ;; Various packages
  3330. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3331. (declare-function calendar-forward-day "cal-move" (arg))
  3332. (declare-function calendar-goto-date "cal-move" (date))
  3333. (declare-function calendar-goto-today "cal-move" ())
  3334. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3335. (defvar calc-embedded-close-formula)
  3336. (defvar calc-embedded-open-formula)
  3337. (declare-function cdlatex-tab "ext:cdlatex" ())
  3338. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3339. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3340. (defvar font-lock-unfontify-region-function)
  3341. (declare-function iswitchb-read-buffer "iswitchb"
  3342. (prompt &optional default require-match start matches-set))
  3343. (defvar iswitchb-temp-buflist)
  3344. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3345. (defvar org-agenda-tags-todo-honor-ignore-options)
  3346. (declare-function org-agenda-skip "org-agenda" ())
  3347. (declare-function
  3348. org-agenda-format-item "org-agenda"
  3349. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3350. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3351. (declare-function org-agenda-change-all-lines "org-agenda"
  3352. (newhead hdmarker &optional fixface just-this))
  3353. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3354. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3355. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3356. (beg end))
  3357. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3358. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3359. "org-agenda" (&optional end))
  3360. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3361. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3362. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3363. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3364. (declare-function org-indent-mode "org-indent" (&optional arg))
  3365. (declare-function parse-time-string "parse-time" (string))
  3366. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3367. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3368. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3369. (defvar remember-data-file)
  3370. (defvar texmathp-why)
  3371. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3372. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3373. (defvar w3m-current-url)
  3374. (defvar w3m-current-title)
  3375. (defvar org-latex-regexps)
  3376. ;;; Autoload and prepare some org modules
  3377. ;; Some table stuff that needs to be defined here, because it is used
  3378. ;; by the functions setting up org-mode or checking for table context.
  3379. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3380. "Detect an org-type or table-type table.")
  3381. (defconst org-table-line-regexp "^[ \t]*|"
  3382. "Detect an org-type table line.")
  3383. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3384. "Detect an org-type table line.")
  3385. (defconst org-table-hline-regexp "^[ \t]*|-"
  3386. "Detect an org-type table hline.")
  3387. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3388. "Detect a table-type table hline.")
  3389. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3390. "Detect the first line outside a table when searching from within it.
  3391. This works for both table types.")
  3392. ;; Autoload the functions in org-table.el that are needed by functions here.
  3393. (eval-and-compile
  3394. (org-autoload "org-table"
  3395. '(org-table-begin org-table-blank-field org-table-end)))
  3396. ;;;###autoload
  3397. (defun turn-on-orgtbl ()
  3398. "Unconditionally turn on `orgtbl-mode'."
  3399. (require 'org-table)
  3400. (orgtbl-mode 1))
  3401. (defun org-at-table-p (&optional table-type)
  3402. "Return t if the cursor is inside an org-type table.
  3403. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3404. (if org-enable-table-editor
  3405. (save-excursion
  3406. (beginning-of-line 1)
  3407. (looking-at (if table-type org-table-any-line-regexp
  3408. org-table-line-regexp)))
  3409. nil))
  3410. (defsubst org-table-p () (org-at-table-p))
  3411. (defun org-at-table.el-p ()
  3412. "Return t if and only if we are at a table.el table."
  3413. (and (org-at-table-p 'any)
  3414. (save-excursion
  3415. (goto-char (org-table-begin 'any))
  3416. (looking-at org-table1-hline-regexp))))
  3417. (defun org-table-recognize-table.el ()
  3418. "If there is a table.el table nearby, recognize it and move into it."
  3419. (if org-table-tab-recognizes-table.el
  3420. (if (org-at-table.el-p)
  3421. (progn
  3422. (beginning-of-line 1)
  3423. (if (looking-at org-table-dataline-regexp)
  3424. nil
  3425. (if (looking-at org-table1-hline-regexp)
  3426. (progn
  3427. (beginning-of-line 2)
  3428. (if (looking-at org-table-any-border-regexp)
  3429. (beginning-of-line -1)))))
  3430. (if (re-search-forward "|" (org-table-end t) t)
  3431. (progn
  3432. (require 'table)
  3433. (if (table--at-cell-p (point))
  3434. t
  3435. (message "recognizing table.el table...")
  3436. (table-recognize-table)
  3437. (message "recognizing table.el table...done")))
  3438. (error "This should not happen"))
  3439. t)
  3440. nil)
  3441. nil))
  3442. (defun org-at-table-hline-p ()
  3443. "Return t if the cursor is inside a hline in a table."
  3444. (if org-enable-table-editor
  3445. (save-excursion
  3446. (beginning-of-line 1)
  3447. (looking-at org-table-hline-regexp))
  3448. nil))
  3449. (defvar org-table-clean-did-remove-column nil)
  3450. (defun org-table-map-tables (function &optional quietly)
  3451. "Apply FUNCTION to the start of all tables in the buffer."
  3452. (save-excursion
  3453. (save-restriction
  3454. (widen)
  3455. (goto-char (point-min))
  3456. (while (re-search-forward org-table-any-line-regexp nil t)
  3457. (unless quietly
  3458. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3459. (beginning-of-line 1)
  3460. (when (and (looking-at org-table-line-regexp)
  3461. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3462. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3463. (save-excursion (funcall function))
  3464. (or (looking-at org-table-line-regexp)
  3465. (forward-char 1)))
  3466. (re-search-forward org-table-any-border-regexp nil 1))))
  3467. (unless quietly (message "Mapping tables: done")))
  3468. ;; Declare and autoload functions from org-exp.el & Co
  3469. (declare-function org-default-export-plist "org-exp")
  3470. (declare-function org-infile-export-plist "org-exp")
  3471. (declare-function org-get-current-options "org-exp")
  3472. ;; Declare and autoload functions from org-agenda.el
  3473. (eval-and-compile
  3474. (org-autoload "org-agenda"
  3475. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3476. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3477. (declare-function org-clock-update-mode-line "org-clock" ())
  3478. (declare-function org-resolve-clocks "org-clock"
  3479. (&optional also-non-dangling-p prompt last-valid))
  3480. (defvar org-clock-start-time)
  3481. (defvar org-clock-marker (make-marker)
  3482. "Marker recording the last clock-in.")
  3483. (defvar org-clock-hd-marker (make-marker)
  3484. "Marker recording the last clock-in, but the headline position.")
  3485. (defvar org-clock-heading ""
  3486. "The heading of the current clock entry.")
  3487. (defun org-clock-is-active ()
  3488. "Return non-nil if clock is currently running.
  3489. The return value is actually the clock marker."
  3490. (marker-buffer org-clock-marker))
  3491. (eval-and-compile
  3492. (org-autoload "org-clock" '(org-clock-remove-overlays
  3493. org-clock-update-time-maybe
  3494. org-clocktable-shift)))
  3495. (defun org-check-running-clock ()
  3496. "Check if the current buffer contains the running clock.
  3497. If yes, offer to stop it and to save the buffer with the changes."
  3498. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3499. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3500. (buffer-name))))
  3501. (org-clock-out)
  3502. (when (y-or-n-p "Save changed buffer?")
  3503. (save-buffer))))
  3504. (defun org-clocktable-try-shift (dir n)
  3505. "Check if this line starts a clock table, if yes, shift the time block."
  3506. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3507. (org-clocktable-shift dir n)))
  3508. ;;;###autoload
  3509. (defun org-clock-persistence-insinuate ()
  3510. "Set up hooks for clock persistence."
  3511. (require 'org-clock)
  3512. (add-hook 'org-mode-hook 'org-clock-load)
  3513. (add-hook 'kill-emacs-hook 'org-clock-save))
  3514. ;; Define the variable already here, to make sure we have it.
  3515. (defvar org-indent-mode nil
  3516. "Non-nil if Org-Indent mode is enabled.
  3517. Use the command `org-indent-mode' to change this variable.")
  3518. ;; Autoload archiving code
  3519. ;; The stuff that is needed for cycling and tags has to be defined here.
  3520. (defgroup org-archive nil
  3521. "Options concerning archiving in Org-mode."
  3522. :tag "Org Archive"
  3523. :group 'org-structure)
  3524. (defcustom org-archive-location "%s_archive::"
  3525. "The location where subtrees should be archived.
  3526. The value of this variable is a string, consisting of two parts,
  3527. separated by a double-colon. The first part is a filename and
  3528. the second part is a headline.
  3529. When the filename is omitted, archiving happens in the same file.
  3530. %s in the filename will be replaced by the current file
  3531. name (without the directory part). Archiving to a different file
  3532. is useful to keep archived entries from contributing to the
  3533. Org-mode Agenda.
  3534. The archived entries will be filed as subtrees of the specified
  3535. headline. When the headline is omitted, the subtrees are simply
  3536. filed away at the end of the file, as top-level entries. Also in
  3537. the heading you can use %s to represent the file name, this can be
  3538. useful when using the same archive for a number of different files.
  3539. Here are a few examples:
  3540. \"%s_archive::\"
  3541. If the current file is Projects.org, archive in file
  3542. Projects.org_archive, as top-level trees. This is the default.
  3543. \"::* Archived Tasks\"
  3544. Archive in the current file, under the top-level headline
  3545. \"* Archived Tasks\".
  3546. \"~/org/archive.org::\"
  3547. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3548. \"~/org/archive.org::* From %s\"
  3549. Archive in file ~/org/archive.org (absolute path), under headlines
  3550. \"From FILENAME\" where file name is the current file name.
  3551. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3552. The \"datetree/\" string is special, signifying to archive
  3553. items to the datetree. Items are placed in either the CLOSED
  3554. date of the item, or the current date if there is no CLOSED date.
  3555. The heading will be a subentry to the current date. There doesn't
  3556. need to be a heading, but there always needs to be a slash after
  3557. datetree. For example, to store archived items directly in the
  3558. datetree, use \"~/org/datetree.org::datetree/\".
  3559. \"basement::** Finished Tasks\"
  3560. Archive in file ./basement (relative path), as level 3 trees
  3561. below the level 2 heading \"** Finished Tasks\".
  3562. You may set this option on a per-file basis by adding to the buffer a
  3563. line like
  3564. #+ARCHIVE: basement::** Finished Tasks
  3565. You may also define it locally for a subtree by setting an ARCHIVE property
  3566. in the entry. If such a property is found in an entry, or anywhere up
  3567. the hierarchy, it will be used."
  3568. :group 'org-archive
  3569. :type 'string)
  3570. (defcustom org-archive-tag "ARCHIVE"
  3571. "The tag that marks a subtree as archived.
  3572. An archived subtree does not open during visibility cycling, and does
  3573. not contribute to the agenda listings.
  3574. After changing this, font-lock must be restarted in the relevant buffers to
  3575. get the proper fontification."
  3576. :group 'org-archive
  3577. :group 'org-keywords
  3578. :type 'string)
  3579. (defcustom org-agenda-skip-archived-trees t
  3580. "Non-nil means the agenda will skip any items located in archived trees.
  3581. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3582. variable is no longer recommended, you should leave it at the value t.
  3583. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3584. :group 'org-archive
  3585. :group 'org-agenda-skip
  3586. :type 'boolean)
  3587. (defcustom org-columns-skip-archived-trees t
  3588. "Non-nil means ignore archived trees when creating column view."
  3589. :group 'org-archive
  3590. :group 'org-properties
  3591. :type 'boolean)
  3592. (defcustom org-cycle-open-archived-trees nil
  3593. "Non-nil means `org-cycle' will open archived trees.
  3594. An archived tree is a tree marked with the tag ARCHIVE.
  3595. When nil, archived trees will stay folded. You can still open them with
  3596. normal outline commands like `show-all', but not with the cycling commands."
  3597. :group 'org-archive
  3598. :group 'org-cycle
  3599. :type 'boolean)
  3600. (defcustom org-sparse-tree-open-archived-trees nil
  3601. "Non-nil means sparse tree construction shows matches in archived trees.
  3602. When nil, matches in these trees are highlighted, but the trees are kept in
  3603. collapsed state."
  3604. :group 'org-archive
  3605. :group 'org-sparse-trees
  3606. :type 'boolean)
  3607. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3608. "The default date type when building a sparse tree.
  3609. When this is nil, a date is a scheduled or a deadline timestamp.
  3610. Otherwise, these types are allowed:
  3611. all: all timestamps
  3612. active: only active timestamps (<...>)
  3613. inactive: only inactive timestamps (<...)
  3614. scheduled: only scheduled timestamps
  3615. deadline: only deadline timestamps"
  3616. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3617. (const :tag "All timestamps" all)
  3618. (const :tag "Only active timestamps" active)
  3619. (const :tag "Only inactive timestamps" inactive)
  3620. (const :tag "Only scheduled timestamps" scheduled)
  3621. (const :tag "Only deadline timestamps" deadline))
  3622. :version "24.3"
  3623. :group 'org-sparse-trees)
  3624. (defun org-cycle-hide-archived-subtrees (state)
  3625. "Re-hide all archived subtrees after a visibility state change."
  3626. (when (and (not org-cycle-open-archived-trees)
  3627. (not (memq state '(overview folded))))
  3628. (save-excursion
  3629. (let* ((globalp (memq state '(contents all)))
  3630. (beg (if globalp (point-min) (point)))
  3631. (end (if globalp (point-max) (org-end-of-subtree t))))
  3632. (org-hide-archived-subtrees beg end)
  3633. (goto-char beg)
  3634. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3635. (message "%s" (substitute-command-keys
  3636. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3637. (defun org-force-cycle-archived ()
  3638. "Cycle subtree even if it is archived."
  3639. (interactive)
  3640. (setq this-command 'org-cycle)
  3641. (let ((org-cycle-open-archived-trees t))
  3642. (call-interactively 'org-cycle)))
  3643. (defun org-hide-archived-subtrees (beg end)
  3644. "Re-hide all archived subtrees after a visibility state change."
  3645. (save-excursion
  3646. (let* ((re (concat ":" org-archive-tag ":")))
  3647. (goto-char beg)
  3648. (while (re-search-forward re end t)
  3649. (when (org-at-heading-p)
  3650. (org-flag-subtree t)
  3651. (org-end-of-subtree t))))))
  3652. (declare-function outline-end-of-heading "outline" ())
  3653. (declare-function outline-flag-region "outline" (from to flag))
  3654. (defun org-flag-subtree (flag)
  3655. (save-excursion
  3656. (org-back-to-heading t)
  3657. (outline-end-of-heading)
  3658. (outline-flag-region (point)
  3659. (progn (org-end-of-subtree t) (point))
  3660. flag)))
  3661. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3662. (eval-and-compile
  3663. (org-autoload "org-archive"
  3664. '(org-add-archive-files)))
  3665. ;; Autoload Column View Code
  3666. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3667. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3668. (declare-function org-columns-compute "org-colview" (property))
  3669. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3670. '(org-columns-number-to-string
  3671. org-columns-get-format-and-top-level
  3672. org-columns-compute
  3673. org-columns-remove-overlays))
  3674. ;; Autoload ID code
  3675. (declare-function org-id-store-link "org-id")
  3676. (declare-function org-id-locations-load "org-id")
  3677. (declare-function org-id-locations-save "org-id")
  3678. (defvar org-id-track-globally)
  3679. (org-autoload "org-id"
  3680. '(org-id-new
  3681. org-id-copy
  3682. org-id-get-with-outline-path-completion
  3683. org-id-get-with-outline-drilling))
  3684. ;;; Variables for pre-computed regular expressions, all buffer local
  3685. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3686. "Matches first line of a hidden block.")
  3687. (make-variable-buffer-local 'org-drawer-regexp)
  3688. (defvar org-todo-regexp nil
  3689. "Matches any of the TODO state keywords.")
  3690. (make-variable-buffer-local 'org-todo-regexp)
  3691. (defvar org-not-done-regexp nil
  3692. "Matches any of the TODO state keywords except the last one.")
  3693. (make-variable-buffer-local 'org-not-done-regexp)
  3694. (defvar org-not-done-heading-regexp nil
  3695. "Matches a TODO headline that is not done.")
  3696. (make-variable-buffer-local 'org-not-done-regexp)
  3697. (defvar org-todo-line-regexp nil
  3698. "Matches a headline and puts TODO state into group 2 if present.")
  3699. (make-variable-buffer-local 'org-todo-line-regexp)
  3700. (defvar org-complex-heading-regexp nil
  3701. "Matches a headline and puts everything into groups:
  3702. group 1: the stars
  3703. group 2: The todo keyword, maybe
  3704. group 3: Priority cookie
  3705. group 4: True headline
  3706. group 5: Tags")
  3707. (make-variable-buffer-local 'org-complex-heading-regexp)
  3708. (defvar org-complex-heading-regexp-format nil
  3709. "Printf format to make regexp to match an exact headline.
  3710. This regexp will match the headline of any node which has the
  3711. exact headline text that is put into the format, but may have any
  3712. TODO state, priority and tags.")
  3713. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3714. (defvar org-todo-line-tags-regexp nil
  3715. "Matches a headline and puts TODO state into group 2 if present.
  3716. Also put tags into group 4 if tags are present.")
  3717. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3718. (defvar org-ds-keyword-length 12
  3719. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3720. (make-variable-buffer-local 'org-ds-keyword-length)
  3721. (defvar org-deadline-regexp nil
  3722. "Matches the DEADLINE keyword.")
  3723. (make-variable-buffer-local 'org-deadline-regexp)
  3724. (defvar org-deadline-time-regexp nil
  3725. "Matches the DEADLINE keyword together with a time stamp.")
  3726. (make-variable-buffer-local 'org-deadline-time-regexp)
  3727. (defvar org-deadline-line-regexp nil
  3728. "Matches the DEADLINE keyword and the rest of the line.")
  3729. (make-variable-buffer-local 'org-deadline-line-regexp)
  3730. (defvar org-scheduled-regexp nil
  3731. "Matches the SCHEDULED keyword.")
  3732. (make-variable-buffer-local 'org-scheduled-regexp)
  3733. (defvar org-scheduled-time-regexp nil
  3734. "Matches the SCHEDULED keyword together with a time stamp.")
  3735. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3736. (defvar org-closed-time-regexp nil
  3737. "Matches the CLOSED keyword together with a time stamp.")
  3738. (make-variable-buffer-local 'org-closed-time-regexp)
  3739. (defvar org-keyword-time-regexp nil
  3740. "Matches any of the 4 keywords, together with the time stamp.")
  3741. (make-variable-buffer-local 'org-keyword-time-regexp)
  3742. (defvar org-keyword-time-not-clock-regexp nil
  3743. "Matches any of the 3 keywords, together with the time stamp.")
  3744. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3745. (defvar org-maybe-keyword-time-regexp nil
  3746. "Matches a timestamp, possibly preceded by a keyword.")
  3747. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3748. (defvar org-all-time-keywords nil
  3749. "List of time keywords.")
  3750. (make-variable-buffer-local 'org-all-time-keywords)
  3751. (defconst org-plain-time-of-day-regexp
  3752. (concat
  3753. "\\(\\<[012]?[0-9]"
  3754. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3755. "\\(--?"
  3756. "\\(\\<[012]?[0-9]"
  3757. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3758. "\\)?")
  3759. "Regular expression to match a plain time or time range.
  3760. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3761. groups carry important information:
  3762. 0 the full match
  3763. 1 the first time, range or not
  3764. 8 the second time, if it is a range.")
  3765. (defconst org-plain-time-extension-regexp
  3766. (concat
  3767. "\\(\\<[012]?[0-9]"
  3768. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3769. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3770. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3771. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3772. groups carry important information:
  3773. 0 the full match
  3774. 7 hours of duration
  3775. 9 minutes of duration")
  3776. (defconst org-stamp-time-of-day-regexp
  3777. (concat
  3778. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3779. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3780. "\\(--?"
  3781. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3782. "Regular expression to match a timestamp time or time range.
  3783. After a match, the following groups carry important information:
  3784. 0 the full match
  3785. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3786. 2 the first time, range or not
  3787. 4 the second time, if it is a range.")
  3788. (defconst org-startup-options
  3789. '(("fold" org-startup-folded t)
  3790. ("overview" org-startup-folded t)
  3791. ("nofold" org-startup-folded nil)
  3792. ("showall" org-startup-folded nil)
  3793. ("showeverything" org-startup-folded showeverything)
  3794. ("content" org-startup-folded content)
  3795. ("indent" org-startup-indented t)
  3796. ("noindent" org-startup-indented nil)
  3797. ("hidestars" org-hide-leading-stars t)
  3798. ("showstars" org-hide-leading-stars nil)
  3799. ("odd" org-odd-levels-only t)
  3800. ("oddeven" org-odd-levels-only nil)
  3801. ("align" org-startup-align-all-tables t)
  3802. ("noalign" org-startup-align-all-tables nil)
  3803. ("inlineimages" org-startup-with-inline-images t)
  3804. ("noinlineimages" org-startup-with-inline-images nil)
  3805. ("customtime" org-display-custom-times t)
  3806. ("logdone" org-log-done time)
  3807. ("lognotedone" org-log-done note)
  3808. ("nologdone" org-log-done nil)
  3809. ("lognoteclock-out" org-log-note-clock-out t)
  3810. ("nolognoteclock-out" org-log-note-clock-out nil)
  3811. ("logrepeat" org-log-repeat state)
  3812. ("lognoterepeat" org-log-repeat note)
  3813. ("nologrepeat" org-log-repeat nil)
  3814. ("logreschedule" org-log-reschedule time)
  3815. ("lognotereschedule" org-log-reschedule note)
  3816. ("nologreschedule" org-log-reschedule nil)
  3817. ("logredeadline" org-log-redeadline time)
  3818. ("lognoteredeadline" org-log-redeadline note)
  3819. ("nologredeadline" org-log-redeadline nil)
  3820. ("logrefile" org-log-refile time)
  3821. ("lognoterefile" org-log-refile note)
  3822. ("nologrefile" org-log-refile nil)
  3823. ("fninline" org-footnote-define-inline t)
  3824. ("nofninline" org-footnote-define-inline nil)
  3825. ("fnlocal" org-footnote-section nil)
  3826. ("fnauto" org-footnote-auto-label t)
  3827. ("fnprompt" org-footnote-auto-label nil)
  3828. ("fnconfirm" org-footnote-auto-label confirm)
  3829. ("fnplain" org-footnote-auto-label plain)
  3830. ("fnadjust" org-footnote-auto-adjust t)
  3831. ("nofnadjust" org-footnote-auto-adjust nil)
  3832. ("constcgs" constants-unit-system cgs)
  3833. ("constSI" constants-unit-system SI)
  3834. ("noptag" org-tag-persistent-alist nil)
  3835. ("hideblocks" org-hide-block-startup t)
  3836. ("nohideblocks" org-hide-block-startup nil)
  3837. ("beamer" org-startup-with-beamer-mode t)
  3838. ("entitiespretty" org-pretty-entities t)
  3839. ("entitiesplain" org-pretty-entities nil))
  3840. "Variable associated with STARTUP options for org-mode.
  3841. Each element is a list of three items: the startup options (as written
  3842. in the #+STARTUP line), the corresponding variable, and the value to set
  3843. this variable to if the option is found. An optional forth element PUSH
  3844. means to push this value onto the list in the variable.")
  3845. (defun org-update-property-plist (key val props)
  3846. "Update PROPS with KEY and VAL."
  3847. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3848. (key (if appending (substring key 0 (- (length key) 1)) key))
  3849. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3850. (previous (cdr (assoc key props))))
  3851. (if appending
  3852. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3853. (cons (cons key val) remainder))))
  3854. (defconst org-block-regexp
  3855. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3856. "Regular expression for hiding blocks.")
  3857. (defconst org-heading-keyword-regexp-format
  3858. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3859. "Printf format for a regexp matching an headline with some keyword.
  3860. This regexp will match the headline of any node which has the
  3861. exact keyword that is put into the format. The keyword isn't in
  3862. any group by default, but the stars and the body are.")
  3863. (defconst org-heading-keyword-maybe-regexp-format
  3864. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3865. "Printf format for a regexp matching an headline, possibly with some keyword.
  3866. This regexp can match any headline with the specified keyword, or
  3867. without a keyword. The keyword isn't in any group by default,
  3868. but the stars and the body are.")
  3869. (defun org-set-regexps-and-options ()
  3870. "Precompute regular expressions for current buffer."
  3871. (when (derived-mode-p 'org-mode)
  3872. (org-set-local 'org-todo-kwd-alist nil)
  3873. (org-set-local 'org-todo-key-alist nil)
  3874. (org-set-local 'org-todo-key-trigger nil)
  3875. (org-set-local 'org-todo-keywords-1 nil)
  3876. (org-set-local 'org-done-keywords nil)
  3877. (org-set-local 'org-todo-heads nil)
  3878. (org-set-local 'org-todo-sets nil)
  3879. (org-set-local 'org-todo-log-states nil)
  3880. (org-set-local 'org-file-properties nil)
  3881. (org-set-local 'org-file-tags nil)
  3882. (let ((re (org-make-options-regexp
  3883. '("CATEGORY" "TODO" "COLUMNS"
  3884. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3885. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3886. "OPTIONS")
  3887. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3888. (splitre "[ \t]+")
  3889. (scripts org-use-sub-superscripts)
  3890. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3891. tail sep kws1 prio props ftags drawers beamer-p
  3892. ext-setup-or-nil setup-contents (start 0))
  3893. (save-excursion
  3894. (save-restriction
  3895. (widen)
  3896. (goto-char (point-min))
  3897. (while (or (and ext-setup-or-nil
  3898. (string-match re ext-setup-or-nil start)
  3899. (setq start (match-end 0)))
  3900. (and (setq ext-setup-or-nil nil start 0)
  3901. (re-search-forward re nil t)))
  3902. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3903. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3904. (if (stringp value) (setq value (org-trim value)))
  3905. (cond
  3906. ((equal key "CATEGORY")
  3907. (setq cat value))
  3908. ((member key '("SEQ_TODO" "TODO"))
  3909. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3910. ((equal key "TYP_TODO")
  3911. (push (cons 'type (org-split-string value splitre)) kwds))
  3912. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3913. ;; general TODO-like setup
  3914. (push (cons (intern (downcase (match-string 1 key)))
  3915. (org-split-string value splitre)) kwds))
  3916. ((equal key "TAGS")
  3917. (setq tags (append tags (if tags '("\\n") nil)
  3918. (org-split-string value splitre))))
  3919. ((equal key "COLUMNS")
  3920. (org-set-local 'org-columns-default-format value))
  3921. ((equal key "LINK")
  3922. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3923. (push (cons (match-string 1 value)
  3924. (org-trim (match-string 2 value)))
  3925. links)))
  3926. ((equal key "PRIORITIES")
  3927. (setq prio (org-split-string value " +")))
  3928. ((equal key "PROPERTY")
  3929. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3930. (setq props (org-update-property-plist (match-string 1 value)
  3931. (match-string 2 value)
  3932. props))))
  3933. ((equal key "FILETAGS")
  3934. (when (string-match "\\S-" value)
  3935. (setq ftags
  3936. (append
  3937. ftags
  3938. (apply 'append
  3939. (mapcar (lambda (x) (org-split-string x ":"))
  3940. (org-split-string value)))))))
  3941. ((equal key "DRAWERS")
  3942. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  3943. ((equal key "CONSTANTS")
  3944. (setq const (append const (org-split-string value splitre))))
  3945. ((equal key "STARTUP")
  3946. (let ((opts (org-split-string value splitre))
  3947. l var val)
  3948. (while (setq l (pop opts))
  3949. (when (setq l (assoc l org-startup-options))
  3950. (setq var (nth 1 l) val (nth 2 l))
  3951. (if (not (nth 3 l))
  3952. (set (make-local-variable var) val)
  3953. (if (not (listp (symbol-value var)))
  3954. (set (make-local-variable var) nil))
  3955. (set (make-local-variable var) (symbol-value var))
  3956. (add-to-list var val))))))
  3957. ((equal key "ARCHIVE")
  3958. (setq arch value)
  3959. (remove-text-properties 0 (length arch)
  3960. '(face t fontified t) arch))
  3961. ((equal key "LATEX_CLASS")
  3962. (setq beamer-p (equal value "beamer")))
  3963. ((equal key "OPTIONS")
  3964. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3965. (setq scripts (read (match-string 2 value)))))
  3966. ((equal key "SETUPFILE")
  3967. (setq setup-contents (org-file-contents
  3968. (expand-file-name
  3969. (org-remove-double-quotes value))
  3970. 'noerror))
  3971. (if (not ext-setup-or-nil)
  3972. (setq ext-setup-or-nil setup-contents start 0)
  3973. (setq ext-setup-or-nil
  3974. (concat (substring ext-setup-or-nil 0 start)
  3975. "\n" setup-contents "\n"
  3976. (substring ext-setup-or-nil start)))))))
  3977. ;; search for property blocks
  3978. (goto-char (point-min))
  3979. (while (re-search-forward org-block-regexp nil t)
  3980. (when (equal "PROPERTY" (upcase (match-string 1)))
  3981. (setq value (replace-regexp-in-string
  3982. "[\n\r]" " " (match-string 4)))
  3983. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3984. (setq props (org-update-property-plist (match-string 1 value)
  3985. (match-string 2 value)
  3986. props)))))))
  3987. (org-set-local 'org-use-sub-superscripts scripts)
  3988. (when cat
  3989. (org-set-local 'org-category (intern cat))
  3990. (push (cons "CATEGORY" cat) props))
  3991. (when prio
  3992. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3993. (setq prio (mapcar 'string-to-char prio))
  3994. (org-set-local 'org-highest-priority (nth 0 prio))
  3995. (org-set-local 'org-lowest-priority (nth 1 prio))
  3996. (org-set-local 'org-default-priority (nth 2 prio)))
  3997. (and props (org-set-local 'org-file-properties (nreverse props)))
  3998. (and ftags (org-set-local 'org-file-tags
  3999. (mapcar 'org-add-prop-inherited ftags)))
  4000. (and drawers (org-set-local 'org-drawers drawers))
  4001. (and arch (org-set-local 'org-archive-location arch))
  4002. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4003. ;; Process the TODO keywords
  4004. (unless kwds
  4005. ;; Use the global values as if they had been given locally.
  4006. (setq kwds (default-value 'org-todo-keywords))
  4007. (if (stringp (car kwds))
  4008. (setq kwds (list (cons org-todo-interpretation
  4009. (default-value 'org-todo-keywords)))))
  4010. (setq kwds (reverse kwds)))
  4011. (setq kwds (nreverse kwds))
  4012. (let (inter kws kw)
  4013. (while (setq kws (pop kwds))
  4014. (let ((kws (or
  4015. (run-hook-with-args-until-success
  4016. 'org-todo-setup-filter-hook kws)
  4017. kws)))
  4018. (setq inter (pop kws) sep (member "|" kws)
  4019. kws0 (delete "|" (copy-sequence kws))
  4020. kwsa nil
  4021. kws1 (mapcar
  4022. (lambda (x)
  4023. ;; 1 2
  4024. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4025. (progn
  4026. (setq kw (match-string 1 x)
  4027. key (and (match-end 2) (match-string 2 x))
  4028. log (org-extract-log-state-settings x))
  4029. (push (cons kw (and key (string-to-char key))) kwsa)
  4030. (and log (push log org-todo-log-states))
  4031. kw)
  4032. (error "Invalid TODO keyword %s" x)))
  4033. kws0)
  4034. kwsa (if kwsa (append '((:startgroup))
  4035. (nreverse kwsa)
  4036. '((:endgroup))))
  4037. hw (car kws1)
  4038. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4039. tail (list inter hw (car dws) (org-last dws))))
  4040. (add-to-list 'org-todo-heads hw 'append)
  4041. (push kws1 org-todo-sets)
  4042. (setq org-done-keywords (append org-done-keywords dws nil))
  4043. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4044. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4045. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4046. (setq org-todo-sets (nreverse org-todo-sets)
  4047. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4048. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4049. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4050. ;; Process the constants
  4051. (when const
  4052. (let (e cst)
  4053. (while (setq e (pop const))
  4054. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4055. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4056. (setq org-table-formula-constants-local cst)))
  4057. ;; Process the tags.
  4058. (when tags
  4059. (let (e tgs)
  4060. (while (setq e (pop tags))
  4061. (cond
  4062. ((equal e "{") (push '(:startgroup) tgs))
  4063. ((equal e "}") (push '(:endgroup) tgs))
  4064. ((equal e "\\n") (push '(:newline) tgs))
  4065. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4066. (push (cons (match-string 1 e)
  4067. (string-to-char (match-string 2 e)))
  4068. tgs))
  4069. (t (push (list e) tgs))))
  4070. (org-set-local 'org-tag-alist nil)
  4071. (while (setq e (pop tgs))
  4072. (or (and (stringp (car e))
  4073. (assoc (car e) org-tag-alist))
  4074. (push e org-tag-alist)))))
  4075. ;; Compute the regular expressions and other local variables.
  4076. ;; Using `org-outline-regexp-bol' would complicate them much,
  4077. ;; because of the fixed white space at the end of that string.
  4078. (if (not org-done-keywords)
  4079. (setq org-done-keywords (and org-todo-keywords-1
  4080. (list (org-last org-todo-keywords-1)))))
  4081. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4082. (length org-scheduled-string)
  4083. (length org-clock-string)
  4084. (length org-closed-string)))
  4085. org-drawer-regexp
  4086. (concat "^[ \t]*:\\("
  4087. (mapconcat 'regexp-quote org-drawers "\\|")
  4088. "\\):[ \t]*$")
  4089. org-not-done-keywords
  4090. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4091. org-todo-regexp
  4092. (concat "\\("
  4093. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4094. "\\)")
  4095. org-not-done-regexp
  4096. (concat "\\("
  4097. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4098. "\\)")
  4099. org-not-done-heading-regexp
  4100. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4101. org-todo-line-regexp
  4102. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4103. org-complex-heading-regexp
  4104. (concat "^\\(\\*+\\)"
  4105. "\\(?: +" org-todo-regexp "\\)?"
  4106. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4107. "\\(?: +\\(.*?\\)\\)??"
  4108. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4109. "[ \t]*$")
  4110. org-complex-heading-regexp-format
  4111. (concat "^\\(\\*+\\)"
  4112. "\\(?: +" org-todo-regexp "\\)?"
  4113. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4114. "\\(?: +"
  4115. ;; Stats cookies can be stuck to body.
  4116. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4117. "\\(%s\\)"
  4118. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4119. "\\)"
  4120. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4121. "[ \t]*$")
  4122. org-todo-line-tags-regexp
  4123. (concat "^\\(\\*+\\)"
  4124. "\\(?: +" org-todo-regexp "\\)?"
  4125. "\\(?: +\\(.*?\\)\\)??"
  4126. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4127. "[ \t]*$")
  4128. org-deadline-regexp (concat "\\<" org-deadline-string)
  4129. org-deadline-time-regexp
  4130. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4131. org-deadline-line-regexp
  4132. (concat "\\<\\(" org-deadline-string "\\).*")
  4133. org-scheduled-regexp
  4134. (concat "\\<" org-scheduled-string)
  4135. org-scheduled-time-regexp
  4136. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4137. org-closed-time-regexp
  4138. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4139. org-keyword-time-regexp
  4140. (concat "\\<\\(" org-scheduled-string
  4141. "\\|" org-deadline-string
  4142. "\\|" org-closed-string
  4143. "\\|" org-clock-string "\\)"
  4144. " *[[<]\\([^]>]+\\)[]>]")
  4145. org-keyword-time-not-clock-regexp
  4146. (concat "\\<\\(" org-scheduled-string
  4147. "\\|" org-deadline-string
  4148. "\\|" org-closed-string
  4149. "\\)"
  4150. " *[[<]\\([^]>]+\\)[]>]")
  4151. org-maybe-keyword-time-regexp
  4152. (concat "\\(\\<\\(" org-scheduled-string
  4153. "\\|" org-deadline-string
  4154. "\\|" org-closed-string
  4155. "\\|" org-clock-string "\\)\\)?"
  4156. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4157. org-all-time-keywords
  4158. (mapcar (lambda (w) (substring w 0 -1))
  4159. (list org-scheduled-string org-deadline-string
  4160. org-clock-string org-closed-string))
  4161. )
  4162. (org-compute-latex-and-specials-regexp)
  4163. (org-set-font-lock-defaults))))
  4164. (defun org-file-contents (file &optional noerror)
  4165. "Return the contents of FILE, as a string."
  4166. (if (or (not file)
  4167. (not (file-readable-p file)))
  4168. (if noerror
  4169. (progn
  4170. (message "Cannot read file \"%s\"" file)
  4171. (ding) (sit-for 2)
  4172. "")
  4173. (error "Cannot read file \"%s\"" file))
  4174. (with-temp-buffer
  4175. (insert-file-contents file)
  4176. (buffer-string))))
  4177. (defun org-extract-log-state-settings (x)
  4178. "Extract the log state setting from a TODO keyword string.
  4179. This will extract info from a string like \"WAIT(w@/!)\"."
  4180. (let (kw key log1 log2)
  4181. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4182. (setq kw (match-string 1 x)
  4183. key (and (match-end 2) (match-string 2 x))
  4184. log1 (and (match-end 3) (match-string 3 x))
  4185. log2 (and (match-end 4) (match-string 4 x)))
  4186. (and (or log1 log2)
  4187. (list kw
  4188. (and log1 (if (equal log1 "!") 'time 'note))
  4189. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4190. (defun org-remove-keyword-keys (list)
  4191. "Remove a pair of parenthesis at the end of each string in LIST."
  4192. (mapcar (lambda (x)
  4193. (if (string-match "(.*)$" x)
  4194. (substring x 0 (match-beginning 0))
  4195. x))
  4196. list))
  4197. (defun org-assign-fast-keys (alist)
  4198. "Assign fast keys to a keyword-key alist.
  4199. Respect keys that are already there."
  4200. (let (new e (alt ?0))
  4201. (while (setq e (pop alist))
  4202. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4203. (cdr e)) ;; Key already assigned.
  4204. (push e new)
  4205. (let ((clist (string-to-list (downcase (car e))))
  4206. (used (append new alist)))
  4207. (when (= (car clist) ?@)
  4208. (pop clist))
  4209. (while (and clist (rassoc (car clist) used))
  4210. (pop clist))
  4211. (unless clist
  4212. (while (rassoc alt used)
  4213. (incf alt)))
  4214. (push (cons (car e) (or (car clist) alt)) new))))
  4215. (nreverse new)))
  4216. ;;; Some variables used in various places
  4217. (defvar org-window-configuration nil
  4218. "Used in various places to store a window configuration.")
  4219. (defvar org-selected-window nil
  4220. "Used in various places to store a window configuration.")
  4221. (defvar org-finish-function nil
  4222. "Function to be called when `C-c C-c' is used.
  4223. This is for getting out of special buffers like capture.")
  4224. ;; FIXME: Occasionally check by commenting these, to make sure
  4225. ;; no other functions uses these, forgetting to let-bind them.
  4226. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4227. (defvar org-last-state)
  4228. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4229. ;; Defined somewhere in this file, but used before definition.
  4230. (defvar org-entities) ;; defined in org-entities.el
  4231. (defvar org-struct-menu)
  4232. (defvar org-org-menu)
  4233. (defvar org-tbl-menu)
  4234. ;;;; Define the Org-mode
  4235. ;; We use a before-change function to check if a table might need
  4236. ;; an update.
  4237. (defvar org-table-may-need-update t
  4238. "Indicates that a table might need an update.
  4239. This variable is set by `org-before-change-function'.
  4240. `org-table-align' sets it back to nil.")
  4241. (defun org-before-change-function (beg end)
  4242. "Every change indicates that a table might need an update."
  4243. (setq org-table-may-need-update t))
  4244. (defvar org-mode-map)
  4245. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4246. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4247. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4248. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4249. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4250. (defvar org-table-buffer-is-an nil)
  4251. (defvar bidi-paragraph-direction)
  4252. (defvar buffer-face-mode-face)
  4253. (require 'outline)
  4254. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4255. (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"))
  4256. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4257. ;; Other stuff we need.
  4258. (require 'time-date)
  4259. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4260. (require 'easymenu)
  4261. (require 'overlay)
  4262. (require 'org-macs)
  4263. (require 'org-entities)
  4264. ;; (require 'org-compat) moved higher up in the file before it is first used
  4265. (require 'org-faces)
  4266. (require 'org-list)
  4267. (require 'org-pcomplete)
  4268. (require 'org-src)
  4269. (require 'org-footnote)
  4270. ;; babel
  4271. (require 'ob)
  4272. (require 'ob-table)
  4273. (require 'ob-lob)
  4274. (require 'ob-ref)
  4275. (require 'ob-tangle)
  4276. (require 'ob-comint)
  4277. (require 'ob-keys)
  4278. ;;;###autoload
  4279. (define-derived-mode org-mode outline-mode "Org"
  4280. "Outline-based notes management and organizer, alias
  4281. \"Carsten's outline-mode for keeping track of everything.\"
  4282. Org-mode develops organizational tasks around a NOTES file which
  4283. contains information about projects as plain text. Org-mode is
  4284. implemented on top of outline-mode, which is ideal to keep the content
  4285. of large files well structured. It supports ToDo items, deadlines and
  4286. time stamps, which magically appear in the diary listing of the Emacs
  4287. calendar. Tables are easily created with a built-in table editor.
  4288. Plain text URL-like links connect to websites, emails (VM), Usenet
  4289. messages (Gnus), BBDB entries, and any files related to the project.
  4290. For printing and sharing of notes, an Org-mode file (or a part of it)
  4291. can be exported as a structured ASCII or HTML file.
  4292. The following commands are available:
  4293. \\{org-mode-map}"
  4294. ;; Get rid of Outline menus, they are not needed
  4295. ;; Need to do this here because define-derived-mode sets up
  4296. ;; the keymap so late. Still, it is a waste to call this each time
  4297. ;; we switch another buffer into org-mode.
  4298. (if (featurep 'xemacs)
  4299. (when (boundp 'outline-mode-menu-heading)
  4300. ;; Assume this is Greg's port, it uses easymenu
  4301. (easy-menu-remove outline-mode-menu-heading)
  4302. (easy-menu-remove outline-mode-menu-show)
  4303. (easy-menu-remove outline-mode-menu-hide))
  4304. (define-key org-mode-map [menu-bar headings] 'undefined)
  4305. (define-key org-mode-map [menu-bar hide] 'undefined)
  4306. (define-key org-mode-map [menu-bar show] 'undefined))
  4307. (org-load-modules-maybe)
  4308. (easy-menu-add org-org-menu)
  4309. (easy-menu-add org-tbl-menu)
  4310. (org-install-agenda-files-menu)
  4311. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4312. (add-to-invisibility-spec '(org-cwidth))
  4313. (add-to-invisibility-spec '(org-hide-block . t))
  4314. (when (featurep 'xemacs)
  4315. (org-set-local 'line-move-ignore-invisible t))
  4316. (org-set-local 'outline-regexp org-outline-regexp)
  4317. (org-set-local 'outline-level 'org-outline-level)
  4318. (setq bidi-paragraph-direction 'left-to-right)
  4319. (when (and org-ellipsis
  4320. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4321. (fboundp 'make-glyph-code))
  4322. (unless org-display-table
  4323. (setq org-display-table (make-display-table)))
  4324. (set-display-table-slot
  4325. org-display-table 4
  4326. (vconcat (mapcar
  4327. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4328. org-ellipsis)))
  4329. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4330. (setq buffer-display-table org-display-table))
  4331. (org-set-regexps-and-options)
  4332. (when (and org-tag-faces (not org-tags-special-faces-re))
  4333. ;; tag faces set outside customize.... force initialization.
  4334. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4335. ;; Calc embedded
  4336. (org-set-local 'calc-embedded-open-mode "# ")
  4337. (modify-syntax-entry ?@ "w")
  4338. (if org-startup-truncated (setq truncate-lines t))
  4339. (org-set-local 'font-lock-unfontify-region-function
  4340. 'org-unfontify-region)
  4341. ;; Activate before-change-function
  4342. (org-set-local 'org-table-may-need-update t)
  4343. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4344. 'local)
  4345. ;; Check for running clock before killing a buffer
  4346. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4347. ;; Indentation.
  4348. (org-set-local 'indent-line-function 'org-indent-line)
  4349. (org-set-local 'indent-region-function 'org-indent-region)
  4350. ;; Initialize radio targets.
  4351. (org-update-radio-target-regexp)
  4352. ;; Filling and auto-filling.
  4353. (org-setup-filling)
  4354. ;; Comments.
  4355. (org-setup-comments-handling)
  4356. ;; Beginning/end of defun
  4357. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4358. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4359. ;; Next error for sparse trees
  4360. (org-set-local 'next-error-function 'org-occur-next-match)
  4361. ;; Make sure dependence stuff works reliably, even for users who set it
  4362. ;; too late :-(
  4363. (if org-enforce-todo-dependencies
  4364. (add-hook 'org-blocker-hook
  4365. 'org-block-todo-from-children-or-siblings-or-parent)
  4366. (remove-hook 'org-blocker-hook
  4367. 'org-block-todo-from-children-or-siblings-or-parent))
  4368. (if org-enforce-todo-checkbox-dependencies
  4369. (add-hook 'org-blocker-hook
  4370. 'org-block-todo-from-checkboxes)
  4371. (remove-hook 'org-blocker-hook
  4372. 'org-block-todo-from-checkboxes))
  4373. ;; Align options lines
  4374. (org-set-local
  4375. 'align-mode-rules-list
  4376. '((org-in-buffer-settings
  4377. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4378. (modes . '(org-mode)))))
  4379. ;; Imenu
  4380. (org-set-local 'imenu-create-index-function
  4381. 'org-imenu-get-tree)
  4382. ;; Make isearch reveal context
  4383. (if (or (featurep 'xemacs)
  4384. (not (boundp 'outline-isearch-open-invisible-function)))
  4385. ;; Emacs 21 and XEmacs make use of the hook
  4386. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4387. ;; Emacs 22 deals with this through a special variable
  4388. (org-set-local 'outline-isearch-open-invisible-function
  4389. (lambda (&rest ignore) (org-show-context 'isearch))))
  4390. ;; Turn on org-beamer-mode?
  4391. (and org-startup-with-beamer-mode (org-beamer-mode))
  4392. ;; Setup the pcomplete hooks
  4393. (set (make-local-variable 'pcomplete-command-completion-function)
  4394. 'org-pcomplete-initial)
  4395. (set (make-local-variable 'pcomplete-command-name-function)
  4396. 'org-command-at-point)
  4397. (set (make-local-variable 'pcomplete-default-completion-function)
  4398. 'ignore)
  4399. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4400. 'org-parse-arguments)
  4401. (set (make-local-variable 'pcomplete-termination-string) "")
  4402. (when (>= emacs-major-version 23)
  4403. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4404. ;; If empty file that did not turn on org-mode automatically, make it to.
  4405. (if (and org-insert-mode-line-in-empty-file
  4406. (org-called-interactively-p 'any)
  4407. (= (point-min) (point-max)))
  4408. (insert "# -*- mode: org -*-\n\n"))
  4409. (unless org-inhibit-startup
  4410. (when org-startup-align-all-tables
  4411. (let ((bmp (buffer-modified-p)))
  4412. (org-table-map-tables 'org-table-align 'quietly)
  4413. (set-buffer-modified-p bmp)))
  4414. (when org-startup-with-inline-images
  4415. (org-display-inline-images))
  4416. (when org-startup-indented
  4417. (require 'org-indent)
  4418. (org-indent-mode 1))
  4419. (unless org-inhibit-startup-visibility-stuff
  4420. (org-set-startup-visibility)))
  4421. ;; Try to set org-hide correctly
  4422. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4423. (when (fboundp 'abbrev-table-put)
  4424. (abbrev-table-put org-mode-abbrev-table
  4425. :parents (list text-mode-abbrev-table)))
  4426. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4427. (defun org-find-invisible-foreground ()
  4428. (let ((candidates (remove
  4429. "unspecified-bg"
  4430. (nconc
  4431. (list (face-background 'default)
  4432. (face-background 'org-default))
  4433. (mapcar
  4434. (lambda (alist)
  4435. (when (boundp alist)
  4436. (cdr (assoc 'background-color (symbol-value alist)))))
  4437. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4438. (list (face-foreground 'org-hide))))))
  4439. (car (remove nil candidates))))
  4440. (defun org-current-time ()
  4441. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4442. (if (> (car org-time-stamp-rounding-minutes) 1)
  4443. (let ((r (car org-time-stamp-rounding-minutes))
  4444. (time (decode-time)))
  4445. (apply 'encode-time
  4446. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4447. (nthcdr 2 time))))
  4448. (current-time)))
  4449. (defun org-today ()
  4450. "Return today date, considering `org-extend-today-until'."
  4451. (time-to-days
  4452. (time-subtract (current-time)
  4453. (list 0 (* 3600 org-extend-today-until) 0))))
  4454. ;;;; Font-Lock stuff, including the activators
  4455. (defvar org-mouse-map (make-sparse-keymap))
  4456. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4457. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4458. (when org-mouse-1-follows-link
  4459. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4460. (when org-tab-follows-link
  4461. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4462. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4463. (require 'font-lock)
  4464. (defconst org-non-link-chars "]\t\n\r<>")
  4465. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4466. "shell" "elisp" "doi" "message"))
  4467. (defvar org-link-types-re nil
  4468. "Matches a link that has a url-like prefix like \"http:\"")
  4469. (defvar org-link-re-with-space nil
  4470. "Matches a link with spaces, optional angular brackets around it.")
  4471. (defvar org-link-re-with-space2 nil
  4472. "Matches a link with spaces, optional angular brackets around it.")
  4473. (defvar org-link-re-with-space3 nil
  4474. "Matches a link with spaces, only for internal part in bracket links.")
  4475. (defvar org-angle-link-re nil
  4476. "Matches link with angular brackets, spaces are allowed.")
  4477. (defvar org-plain-link-re nil
  4478. "Matches plain link, without spaces.")
  4479. (defvar org-bracket-link-regexp nil
  4480. "Matches a link in double brackets.")
  4481. (defvar org-bracket-link-analytic-regexp nil
  4482. "Regular expression used to analyze links.
  4483. Here is what the match groups contain after a match:
  4484. 1: http:
  4485. 2: http
  4486. 3: path
  4487. 4: [desc]
  4488. 5: desc")
  4489. (defvar org-bracket-link-analytic-regexp++ nil
  4490. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4491. (defvar org-any-link-re nil
  4492. "Regular expression matching any link.")
  4493. (defcustom org-match-sexp-depth 3
  4494. "Number of stacked braces for sub/superscript matching.
  4495. This has to be set before loading org.el to be effective."
  4496. :group 'org-export-translation ; ??????????????????????????/
  4497. :type 'integer)
  4498. (defun org-create-multibrace-regexp (left right n)
  4499. "Create a regular expression which will match a balanced sexp.
  4500. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4501. as single character strings.
  4502. The regexp returned will match the entire expression including the
  4503. delimiters. It will also define a single group which contains the
  4504. match except for the outermost delimiters. The maximum depth of
  4505. stacked delimiters is N. Escaping delimiters is not possible."
  4506. (let* ((nothing (concat "[^" left right "]*?"))
  4507. (or "\\|")
  4508. (re nothing)
  4509. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4510. (while (> n 1)
  4511. (setq n (1- n)
  4512. re (concat re or next)
  4513. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4514. (concat left "\\(" re "\\)" right)))
  4515. (defvar org-match-substring-regexp
  4516. (concat
  4517. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4518. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4519. "\\|"
  4520. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4521. "\\|"
  4522. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4523. "The regular expression matching a sub- or superscript.")
  4524. (defvar org-match-substring-with-braces-regexp
  4525. (concat
  4526. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4527. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4528. "\\)")
  4529. "The regular expression matching a sub- or superscript, forcing braces.")
  4530. (defun org-make-link-regexps ()
  4531. "Update the link regular expressions.
  4532. This should be called after the variable `org-link-types' has changed."
  4533. (setq org-link-types-re
  4534. (concat
  4535. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4536. org-link-re-with-space
  4537. (concat
  4538. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4539. "\\([^" org-non-link-chars " ]"
  4540. "[^" org-non-link-chars "]*"
  4541. "[^" org-non-link-chars " ]\\)>?")
  4542. org-link-re-with-space2
  4543. (concat
  4544. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4545. "\\([^" org-non-link-chars " ]"
  4546. "[^\t\n\r]*"
  4547. "[^" org-non-link-chars " ]\\)>?")
  4548. org-link-re-with-space3
  4549. (concat
  4550. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4551. "\\([^" org-non-link-chars " ]"
  4552. "[^\t\n\r]*\\)")
  4553. org-angle-link-re
  4554. (concat
  4555. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4556. "\\([^" org-non-link-chars " ]"
  4557. "[^" org-non-link-chars "]*"
  4558. "\\)>")
  4559. org-plain-link-re
  4560. (concat
  4561. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4562. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4563. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4564. org-bracket-link-regexp
  4565. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4566. org-bracket-link-analytic-regexp
  4567. (concat
  4568. "\\[\\["
  4569. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4570. "\\([^]]+\\)"
  4571. "\\]"
  4572. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4573. "\\]")
  4574. org-bracket-link-analytic-regexp++
  4575. (concat
  4576. "\\[\\["
  4577. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4578. "\\([^]]+\\)"
  4579. "\\]"
  4580. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4581. "\\]")
  4582. org-any-link-re
  4583. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4584. org-angle-link-re "\\)\\|\\("
  4585. org-plain-link-re "\\)")))
  4586. (org-make-link-regexps)
  4587. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4588. "Regular expression for fast time stamp matching.")
  4589. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4590. "Regular expression for fast time stamp matching.")
  4591. (defconst org-ts-regexp0
  4592. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4593. "Regular expression matching time strings for analysis.
  4594. This one does not require the space after the date, so it can be used
  4595. on a string that terminates immediately after the date.")
  4596. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4597. "Regular expression matching time strings for analysis.")
  4598. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4599. "Regular expression matching time stamps, with groups.")
  4600. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4601. "Regular expression matching time stamps (also [..]), with groups.")
  4602. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4603. "Regular expression matching a time stamp range.")
  4604. (defconst org-tr-regexp-both
  4605. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4606. "Regular expression matching a time stamp range.")
  4607. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4608. org-ts-regexp "\\)?")
  4609. "Regular expression matching a time stamp or time stamp range.")
  4610. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4611. org-ts-regexp-both "\\)?")
  4612. "Regular expression matching a time stamp or time stamp range.
  4613. The time stamps may be either active or inactive.")
  4614. (defvar org-emph-face nil)
  4615. (defun org-do-emphasis-faces (limit)
  4616. "Run through the buffer and add overlays to emphasized strings."
  4617. (let (rtn a)
  4618. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4619. (if (not (= (char-after (match-beginning 3))
  4620. (char-after (match-beginning 4))))
  4621. (progn
  4622. (setq rtn t)
  4623. (setq a (assoc (match-string 3) org-emphasis-alist))
  4624. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4625. 'face
  4626. (nth 1 a))
  4627. (and (nth 4 a)
  4628. (org-remove-flyspell-overlays-in
  4629. (match-beginning 0) (match-end 0)))
  4630. (add-text-properties (match-beginning 2) (match-end 2)
  4631. '(font-lock-multiline t org-emphasis t))
  4632. (when org-hide-emphasis-markers
  4633. (add-text-properties (match-end 4) (match-beginning 5)
  4634. '(invisible org-link))
  4635. (add-text-properties (match-beginning 3) (match-end 3)
  4636. '(invisible org-link)))))
  4637. (backward-char 1))
  4638. rtn))
  4639. (defun org-emphasize (&optional char)
  4640. "Insert or change an emphasis, i.e. a font like bold or italic.
  4641. If there is an active region, change that region to a new emphasis.
  4642. If there is no region, just insert the marker characters and position
  4643. the cursor between them.
  4644. CHAR should be either the marker character, or the first character of the
  4645. HTML tag associated with that emphasis. If CHAR is a space, the means
  4646. to remove the emphasis of the selected region.
  4647. If char is not given (for example in an interactive call) it
  4648. will be prompted for."
  4649. (interactive)
  4650. (let ((eal org-emphasis-alist) e det
  4651. (erc org-emphasis-regexp-components)
  4652. (prompt "")
  4653. (string "") beg end move tag c s)
  4654. (if (org-region-active-p)
  4655. (setq beg (region-beginning) end (region-end)
  4656. string (buffer-substring beg end))
  4657. (setq move t))
  4658. (while (setq e (pop eal))
  4659. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4660. c (aref tag 0))
  4661. (push (cons c (string-to-char (car e))) det)
  4662. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4663. (substring tag 1)))))
  4664. (setq det (nreverse det))
  4665. (unless char
  4666. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4667. (setq char (read-char-exclusive)))
  4668. (setq char (or (cdr (assoc char det)) char))
  4669. (if (equal char ?\ )
  4670. (setq s "" move nil)
  4671. (unless (assoc (char-to-string char) org-emphasis-alist)
  4672. (error "No such emphasis marker: \"%c\"" char))
  4673. (setq s (char-to-string char)))
  4674. (while (and (> (length string) 1)
  4675. (equal (substring string 0 1) (substring string -1))
  4676. (assoc (substring string 0 1) org-emphasis-alist))
  4677. (setq string (substring string 1 -1)))
  4678. (setq string (concat s string s))
  4679. (if beg (delete-region beg end))
  4680. (unless (or (bolp)
  4681. (string-match (concat "[" (nth 0 erc) "\n]")
  4682. (char-to-string (char-before (point)))))
  4683. (insert " "))
  4684. (unless (or (eobp)
  4685. (string-match (concat "[" (nth 1 erc) "\n]")
  4686. (char-to-string (char-after (point)))))
  4687. (insert " ") (backward-char 1))
  4688. (insert string)
  4689. (and move (backward-char 1))))
  4690. (defconst org-nonsticky-props
  4691. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4692. (defsubst org-rear-nonsticky-at (pos)
  4693. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4694. (defun org-activate-plain-links (limit)
  4695. "Run through the buffer and add overlays to links."
  4696. (catch 'exit
  4697. (let (f)
  4698. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4699. (not (org-in-src-block-p)))
  4700. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4701. (setq f (get-text-property (match-beginning 0) 'face))
  4702. (unless (or (org-in-src-block-p)
  4703. (eq f 'org-tag)
  4704. (and (listp f) (memq 'org-tag f)))
  4705. (add-text-properties (match-beginning 0) (match-end 0)
  4706. (list 'mouse-face 'highlight
  4707. 'face 'org-link
  4708. 'keymap org-mouse-map))
  4709. (org-rear-nonsticky-at (match-end 0)))
  4710. t))))
  4711. (defun org-activate-code (limit)
  4712. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4713. (progn
  4714. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4715. (remove-text-properties (match-beginning 0) (match-end 0)
  4716. '(display t invisible t intangible t))
  4717. t)))
  4718. (defcustom org-src-fontify-natively nil
  4719. "When non-nil, fontify code in code blocks."
  4720. :type 'boolean
  4721. :version "24.1"
  4722. :group 'org-appearance
  4723. :group 'org-babel)
  4724. (defcustom org-allow-promoting-top-level-subtree nil
  4725. "When non-nil, allow promoting a top level subtree.
  4726. The leading star of the top level headline will be replaced
  4727. by a #."
  4728. :type 'boolean
  4729. :version "24.1"
  4730. :group 'org-appearance)
  4731. (defun org-fontify-meta-lines-and-blocks (limit)
  4732. (condition-case nil
  4733. (org-fontify-meta-lines-and-blocks-1 limit)
  4734. (error (message "org-mode fontification error"))))
  4735. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4736. "Fontify #+ lines and blocks, in the correct ways."
  4737. (let ((case-fold-search t))
  4738. (if (re-search-forward
  4739. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4740. limit t)
  4741. (let ((beg (match-beginning 0))
  4742. (block-start (match-end 0))
  4743. (block-end nil)
  4744. (lang (match-string 7))
  4745. (beg1 (line-beginning-position 2))
  4746. (dc1 (downcase (match-string 2)))
  4747. (dc3 (downcase (match-string 3)))
  4748. end end1 quoting block-type ovl)
  4749. (cond
  4750. ((member dc1 '("+html:" "+ascii:" "+latex:" "+docbook:"))
  4751. ;; a single line of backend-specific content
  4752. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4753. (remove-text-properties (match-beginning 0) (match-end 0)
  4754. '(display t invisible t intangible t))
  4755. (add-text-properties (match-beginning 1) (match-end 3)
  4756. '(font-lock-fontified t face org-meta-line))
  4757. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4758. '(font-lock-fontified t face org-block))
  4759. ; for backend-specific code
  4760. t)
  4761. ((and (match-end 4) (equal dc3 "+begin"))
  4762. ;; Truly a block
  4763. (setq block-type (downcase (match-string 5))
  4764. quoting (member block-type org-protecting-blocks))
  4765. (when (re-search-forward
  4766. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4767. nil t) ;; on purpose, we look further than LIMIT
  4768. (setq end (min (point-max) (match-end 0))
  4769. end1 (min (point-max) (1- (match-beginning 0))))
  4770. (setq block-end (match-beginning 0))
  4771. (when quoting
  4772. (remove-text-properties beg end
  4773. '(display t invisible t intangible t)))
  4774. (add-text-properties
  4775. beg end
  4776. '(font-lock-fontified t font-lock-multiline t))
  4777. (add-text-properties beg beg1 '(face org-meta-line))
  4778. (add-text-properties end1 (min (point-max) (1+ end))
  4779. '(face org-meta-line)) ; for end_src
  4780. (cond
  4781. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4782. (org-src-font-lock-fontify-block lang block-start block-end)
  4783. ;; remove old background overlays
  4784. (mapc (lambda (ov)
  4785. (if (eq (overlay-get ov 'face) 'org-block-background)
  4786. (delete-overlay ov)))
  4787. (overlays-at (/ (+ beg1 block-end) 2)))
  4788. ;; add a background overlay
  4789. (setq ovl (make-overlay beg1 block-end))
  4790. (overlay-put ovl 'face 'org-block-background)
  4791. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4792. (quoting
  4793. (add-text-properties beg1 (min (point-max) (1+ end1))
  4794. '(face org-block))) ; end of source block
  4795. ((not org-fontify-quote-and-verse-blocks))
  4796. ((string= block-type "quote")
  4797. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4798. ((string= block-type "verse")
  4799. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4800. (add-text-properties beg beg1 '(face org-block-begin-line))
  4801. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4802. '(face org-block-end-line))
  4803. t))
  4804. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  4805. (add-text-properties
  4806. beg (match-end 3)
  4807. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4808. '(font-lock-fontified t invisible t)
  4809. '(font-lock-fontified t face org-document-info-keyword)))
  4810. (add-text-properties
  4811. (match-beginning 6) (1+ (match-end 6))
  4812. (if (string-equal dc1 "+title:")
  4813. '(font-lock-fontified t face org-document-title)
  4814. '(font-lock-fontified t face org-document-info))))
  4815. ((or (equal dc1 "+results")
  4816. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  4817. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  4818. "+call:" "+header:" "+headers:" "+name:"))
  4819. (and (match-end 4) (equal dc3 "+attr")))
  4820. (add-text-properties
  4821. beg (match-end 0)
  4822. '(font-lock-fontified t face org-meta-line))
  4823. t)
  4824. ((member dc3 '(" " ""))
  4825. (add-text-properties
  4826. beg (match-end 0)
  4827. '(font-lock-fontified t face font-lock-comment-face)))
  4828. ((not (member (char-after beg) '(?\ ?\t)))
  4829. ;; just any other in-buffer setting, but not indented
  4830. (add-text-properties
  4831. beg (match-end 0)
  4832. '(font-lock-fontified t face org-meta-line))
  4833. t)
  4834. (t nil))))))
  4835. (defun org-activate-angle-links (limit)
  4836. "Run through the buffer and add overlays to links."
  4837. (if (and (re-search-forward org-angle-link-re limit t)
  4838. (not (org-in-src-block-p)))
  4839. (progn
  4840. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4841. (add-text-properties (match-beginning 0) (match-end 0)
  4842. (list 'mouse-face 'highlight
  4843. 'keymap org-mouse-map))
  4844. (org-rear-nonsticky-at (match-end 0))
  4845. t)))
  4846. (defun org-activate-footnote-links (limit)
  4847. "Run through the buffer and add overlays to footnotes."
  4848. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4849. (when fn
  4850. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4851. (org-remove-flyspell-overlays-in beg end)
  4852. (add-text-properties beg end
  4853. (list 'mouse-face 'highlight
  4854. 'keymap org-mouse-map
  4855. 'help-echo
  4856. (if (= (point-at-bol) beg)
  4857. "Footnote definition"
  4858. "Footnote reference")
  4859. 'font-lock-fontified t
  4860. 'font-lock-multiline t
  4861. 'face 'org-footnote))))))
  4862. (defun org-activate-bracket-links (limit)
  4863. "Run through the buffer and add overlays to bracketed links."
  4864. (if (and (re-search-forward org-bracket-link-regexp limit t)
  4865. (not (org-in-src-block-p)))
  4866. (let* ((help (concat "LINK: "
  4867. (org-match-string-no-properties 1)))
  4868. ;; FIXME: above we should remove the escapes.
  4869. ;; but that requires another match, protecting match data,
  4870. ;; a lot of overhead for font-lock.
  4871. (ip (org-maybe-intangible
  4872. (list 'invisible 'org-link
  4873. 'keymap org-mouse-map 'mouse-face 'highlight
  4874. 'font-lock-multiline t 'help-echo help)))
  4875. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4876. 'font-lock-multiline t 'help-echo help)))
  4877. ;; We need to remove the invisible property here. Table narrowing
  4878. ;; may have made some of this invisible.
  4879. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4880. (remove-text-properties (match-beginning 0) (match-end 0)
  4881. '(invisible nil))
  4882. (if (match-end 3)
  4883. (progn
  4884. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4885. (org-rear-nonsticky-at (match-beginning 3))
  4886. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4887. (org-rear-nonsticky-at (match-end 3))
  4888. (add-text-properties (match-end 3) (match-end 0) ip)
  4889. (org-rear-nonsticky-at (match-end 0)))
  4890. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4891. (org-rear-nonsticky-at (match-beginning 1))
  4892. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4893. (org-rear-nonsticky-at (match-end 1))
  4894. (add-text-properties (match-end 1) (match-end 0) ip)
  4895. (org-rear-nonsticky-at (match-end 0)))
  4896. t)))
  4897. (defun org-activate-dates (limit)
  4898. "Run through the buffer and add overlays to dates."
  4899. (if (re-search-forward org-tsr-regexp-both limit t)
  4900. (progn
  4901. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4902. (add-text-properties (match-beginning 0) (match-end 0)
  4903. (list 'mouse-face 'highlight
  4904. 'keymap org-mouse-map))
  4905. (org-rear-nonsticky-at (match-end 0))
  4906. (when org-display-custom-times
  4907. (if (match-end 3)
  4908. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4909. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4910. t)))
  4911. (defvar org-target-link-regexp nil
  4912. "Regular expression matching radio targets in plain text.")
  4913. (make-variable-buffer-local 'org-target-link-regexp)
  4914. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4915. "Regular expression matching a link target.")
  4916. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4917. "Regular expression matching a radio target.")
  4918. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4919. "Regular expression matching any target.")
  4920. (defun org-activate-target-links (limit)
  4921. "Run through the buffer and add overlays to target matches."
  4922. (when org-target-link-regexp
  4923. (let ((case-fold-search t))
  4924. (if (re-search-forward org-target-link-regexp limit t)
  4925. (progn
  4926. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4927. (add-text-properties (match-beginning 0) (match-end 0)
  4928. (list 'mouse-face 'highlight
  4929. 'keymap org-mouse-map
  4930. 'help-echo "Radio target link"
  4931. 'org-linked-text t))
  4932. (org-rear-nonsticky-at (match-end 0))
  4933. t)))))
  4934. (defun org-update-radio-target-regexp ()
  4935. "Find all radio targets in this file and update the regular expression."
  4936. (interactive)
  4937. (when (memq 'radio org-activate-links)
  4938. (setq org-target-link-regexp
  4939. (org-make-target-link-regexp (org-all-targets 'radio)))
  4940. (org-restart-font-lock)))
  4941. (defun org-hide-wide-columns (limit)
  4942. (let (s e)
  4943. (setq s (text-property-any (point) (or limit (point-max))
  4944. 'org-cwidth t))
  4945. (when s
  4946. (setq e (next-single-property-change s 'org-cwidth))
  4947. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4948. (goto-char e)
  4949. t)))
  4950. (defvar org-latex-and-specials-regexp nil
  4951. "Regular expression for highlighting export special stuff.")
  4952. (defvar org-match-substring-regexp)
  4953. (defvar org-match-substring-with-braces-regexp)
  4954. ;; This should be with the exporter code, but we also use if for font-locking
  4955. (defconst org-export-html-special-string-regexps
  4956. '(("\\\\-" . "&shy;")
  4957. ("---\\([^-]\\)" . "&mdash;\\1")
  4958. ("--\\([^-]\\)" . "&ndash;\\1")
  4959. ("\\.\\.\\." . "&hellip;"))
  4960. "Regular expressions for special string conversion.")
  4961. (defun org-compute-latex-and-specials-regexp ()
  4962. "Compute regular expression for stuff treated specially by exporters."
  4963. (if (not org-highlight-latex-fragments-and-specials)
  4964. (org-set-local 'org-latex-and-specials-regexp nil)
  4965. (require 'org-exp)
  4966. (let*
  4967. ((matchers (plist-get org-format-latex-options :matchers))
  4968. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4969. org-latex-regexps)))
  4970. (org-export-allow-BIND nil)
  4971. (options (org-combine-plists (org-default-export-plist)
  4972. (org-infile-export-plist)))
  4973. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4974. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4975. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4976. (org-export-html-expand (plist-get options :expand-quoted-html))
  4977. (org-export-with-special-strings (plist-get options :special-strings))
  4978. (re-sub
  4979. (cond
  4980. ((equal org-export-with-sub-superscripts '{})
  4981. (list org-match-substring-with-braces-regexp))
  4982. (org-export-with-sub-superscripts
  4983. (list org-match-substring-regexp))))
  4984. (re-latex
  4985. (if org-export-with-LaTeX-fragments
  4986. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4987. (re-macros
  4988. (if org-export-with-TeX-macros
  4989. (list (concat "\\\\"
  4990. (regexp-opt
  4991. (append
  4992. (delq nil
  4993. (mapcar 'car-safe
  4994. (append org-entities-user
  4995. org-entities)))
  4996. (if (boundp 'org-latex-entities)
  4997. (mapcar (lambda (x)
  4998. (or (car-safe x) x))
  4999. org-latex-entities)
  5000. nil))
  5001. 'words))) ; FIXME
  5002. ))
  5003. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5004. (re-special (if org-export-with-special-strings
  5005. (mapcar (lambda (x) (car x))
  5006. org-export-html-special-string-regexps)))
  5007. (re-rest
  5008. (delq nil
  5009. (list
  5010. (if org-export-html-expand "@<[^>\n]+>")
  5011. ))))
  5012. (org-set-local
  5013. 'org-latex-and-specials-regexp
  5014. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5015. re-rest) "\\|")))))
  5016. (defun org-do-latex-and-special-faces (limit)
  5017. "Run through the buffer and add overlays to links."
  5018. (when org-latex-and-specials-regexp
  5019. (let (rtn d)
  5020. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5021. limit t))
  5022. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5023. 'face))
  5024. '(org-code org-verbatim underline)))
  5025. (progn
  5026. (setq rtn t
  5027. d (cond ((member (char-after (1+ (match-beginning 0)))
  5028. '(?_ ?^)) 1)
  5029. (t 0)))
  5030. (font-lock-prepend-text-property
  5031. (+ d (match-beginning 0)) (match-end 0)
  5032. 'face 'org-latex-and-export-specials)
  5033. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5034. '(font-lock-multiline t)))))
  5035. rtn)))
  5036. (defun org-restart-font-lock ()
  5037. "Restart `font-lock-mode', to force refontification."
  5038. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5039. (font-lock-mode -1)
  5040. (font-lock-mode 1)))
  5041. (defun org-all-targets (&optional radio)
  5042. "Return a list of all targets in this file.
  5043. With optional argument RADIO, only find radio targets."
  5044. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5045. rtn)
  5046. (save-excursion
  5047. (goto-char (point-min))
  5048. (while (re-search-forward re nil t)
  5049. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5050. rtn)))
  5051. (defun org-make-target-link-regexp (targets)
  5052. "Make regular expression matching all strings in TARGETS.
  5053. The regular expression finds the targets also if there is a line break
  5054. between words."
  5055. (and targets
  5056. (concat
  5057. "\\<\\("
  5058. (mapconcat
  5059. (lambda (x)
  5060. (setq x (regexp-quote x))
  5061. (while (string-match " +" x)
  5062. (setq x (replace-match "\\s-+" t t x)))
  5063. x)
  5064. targets
  5065. "\\|")
  5066. "\\)\\>")))
  5067. (defun org-activate-tags (limit)
  5068. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5069. (progn
  5070. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5071. (add-text-properties (match-beginning 1) (match-end 1)
  5072. (list 'mouse-face 'highlight
  5073. 'keymap org-mouse-map))
  5074. (org-rear-nonsticky-at (match-end 1))
  5075. t)))
  5076. (defun org-outline-level ()
  5077. "Compute the outline level of the heading at point.
  5078. This function assumes that the cursor is at the beginning of a line matched
  5079. by `outline-regexp'. Otherwise it returns garbage.
  5080. If this is called at a normal headline, the level is the number of stars.
  5081. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5082. (save-excursion
  5083. (looking-at org-outline-regexp)
  5084. (1- (- (match-end 0) (match-beginning 0)))))
  5085. (defvar org-font-lock-keywords nil)
  5086. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5087. "Regular expression matching a property line.")
  5088. (defvar org-font-lock-hook nil
  5089. "Functions to be called for special font lock stuff.")
  5090. (defvar org-font-lock-set-keywords-hook nil
  5091. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5092. This is called after `org-font-lock-extra-keywords' is defined, but before
  5093. it is installed to be used by font lock. This can be useful if something
  5094. needs to be inserted at a specific position in the font-lock sequence.")
  5095. (defun org-font-lock-hook (limit)
  5096. "Run `org-font-lock-hook' within LIMIT."
  5097. (run-hook-with-args 'org-font-lock-hook limit))
  5098. (defun org-set-font-lock-defaults ()
  5099. "Set font lock defaults for the current buffer."
  5100. (let* ((em org-fontify-emphasized-text)
  5101. (lk org-activate-links)
  5102. (org-font-lock-extra-keywords
  5103. (list
  5104. ;; Call the hook
  5105. '(org-font-lock-hook)
  5106. ;; Headlines
  5107. `(,(if org-fontify-whole-heading-line
  5108. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5109. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5110. (1 (org-get-level-face 1))
  5111. (2 (org-get-level-face 2))
  5112. (3 (org-get-level-face 3)))
  5113. ;; Table lines
  5114. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5115. (1 'org-table t))
  5116. ;; Table internals
  5117. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5118. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5119. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5120. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5121. ;; Drawers
  5122. (list org-drawer-regexp '(0 'org-special-keyword t))
  5123. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5124. ;; Properties
  5125. (list org-property-re
  5126. '(1 'org-special-keyword t)
  5127. '(3 'org-property-value t))
  5128. ;; Links
  5129. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5130. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5131. (if (memq 'plain lk) '(org-activate-plain-links))
  5132. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5133. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5134. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5135. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5136. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5137. '(org-hide-wide-columns (0 nil append))
  5138. ;; TODO keyword
  5139. (list (format org-heading-keyword-regexp-format
  5140. org-todo-regexp)
  5141. '(2 (org-get-todo-face 2) t))
  5142. ;; DONE
  5143. (if org-fontify-done-headline
  5144. (list (format org-heading-keyword-regexp-format
  5145. (concat
  5146. "\\(?:"
  5147. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5148. "\\)"))
  5149. '(2 'org-headline-done t))
  5150. nil)
  5151. ;; Priorities
  5152. '(org-font-lock-add-priority-faces)
  5153. ;; Tags
  5154. '(org-font-lock-add-tag-faces)
  5155. ;; Special keywords
  5156. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5157. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5158. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5159. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5160. ;; Emphasis
  5161. (if em
  5162. (if (featurep 'xemacs)
  5163. '(org-do-emphasis-faces (0 nil append))
  5164. '(org-do-emphasis-faces)))
  5165. ;; Checkboxes
  5166. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5167. 1 'org-checkbox prepend)
  5168. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5169. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5170. (0 (org-get-checkbox-statistics-face) t)))
  5171. ;; Description list items
  5172. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5173. 1 'org-list-dt prepend)
  5174. ;; ARCHIVEd headings
  5175. (list (concat
  5176. org-outline-regexp-bol
  5177. "\\(.*:" org-archive-tag ":.*\\)")
  5178. '(1 'org-archived prepend))
  5179. ;; Specials
  5180. '(org-do-latex-and-special-faces)
  5181. '(org-fontify-entities)
  5182. '(org-raise-scripts)
  5183. ;; Code
  5184. '(org-activate-code (1 'org-code t))
  5185. ;; COMMENT
  5186. (list (format org-heading-keyword-regexp-format
  5187. (concat "\\("
  5188. org-comment-string "\\|" org-quote-string
  5189. "\\)"))
  5190. '(2 'org-special-keyword t))
  5191. ;; Blocks and meta lines
  5192. '(org-fontify-meta-lines-and-blocks)
  5193. )))
  5194. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5195. (run-hooks 'org-font-lock-set-keywords-hook)
  5196. ;; Now set the full font-lock-keywords
  5197. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5198. (org-set-local 'font-lock-defaults
  5199. '(org-font-lock-keywords t nil nil backward-paragraph))
  5200. (kill-local-variable 'font-lock-keywords) nil))
  5201. (defun org-toggle-pretty-entities ()
  5202. "Toggle the composition display of entities as UTF8 characters."
  5203. (interactive)
  5204. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5205. (org-restart-font-lock)
  5206. (if org-pretty-entities
  5207. (message "Entities are displayed as UTF8 characters")
  5208. (save-restriction
  5209. (widen)
  5210. (org-decompose-region (point-min) (point-max))
  5211. (message "Entities are displayed plain"))))
  5212. (defvar org-custom-properties-overlays nil
  5213. "List of overlays used for custom properties.")
  5214. (make-variable-buffer-local 'org-custom-properties-overlays)
  5215. (defun org-toggle-custom-properties-visibility ()
  5216. "Display or hide properties in `org-custom-properties'."
  5217. (interactive)
  5218. (if org-custom-properties-overlays
  5219. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5220. (setq org-custom-properties-overlays nil))
  5221. (unless (not org-custom-properties)
  5222. (save-excursion
  5223. (save-restriction
  5224. (widen)
  5225. (goto-char (point-min))
  5226. (while (re-search-forward org-property-re nil t)
  5227. (mapc (lambda(p)
  5228. (when (equal p (substring (match-string 1) 1 -1))
  5229. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5230. (overlay-put o 'invisible t)
  5231. (overlay-put o 'org-custom-property t)
  5232. (push o org-custom-properties-overlays))))
  5233. org-custom-properties)))))))
  5234. (defun org-fontify-entities (limit)
  5235. "Find an entity to fontify."
  5236. (let (ee)
  5237. (when org-pretty-entities
  5238. (catch 'match
  5239. (while (re-search-forward
  5240. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5241. limit t)
  5242. (if (and (not (org-in-indented-comment-line))
  5243. (setq ee (org-entity-get (match-string 1)))
  5244. (= (length (nth 6 ee)) 1))
  5245. (let*
  5246. ((end (if (equal (match-string 2) "{}")
  5247. (match-end 2)
  5248. (match-end 1))))
  5249. (add-text-properties
  5250. (match-beginning 0) end
  5251. (list 'font-lock-fontified t))
  5252. (compose-region (match-beginning 0) end
  5253. (nth 6 ee) nil)
  5254. (backward-char 1)
  5255. (throw 'match t))))
  5256. nil))))
  5257. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5258. "Fontify string S like in Org-mode."
  5259. (with-temp-buffer
  5260. (insert s)
  5261. (let ((org-odd-levels-only odd-levels))
  5262. (org-mode)
  5263. (font-lock-fontify-buffer)
  5264. (buffer-string))))
  5265. (defvar org-m nil)
  5266. (defvar org-l nil)
  5267. (defvar org-f nil)
  5268. (defun org-get-level-face (n)
  5269. "Get the right face for match N in font-lock matching of headlines."
  5270. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5271. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5272. (if org-cycle-level-faces
  5273. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5274. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5275. (cond
  5276. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5277. ((eq n 2) org-f)
  5278. (t (if org-level-color-stars-only nil org-f))))
  5279. (defun org-get-todo-face (kwd)
  5280. "Get the right face for a TODO keyword KWD.
  5281. If KWD is a number, get the corresponding match group."
  5282. (if (numberp kwd) (setq kwd (match-string kwd)))
  5283. (or (org-face-from-face-or-color
  5284. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5285. (and (member kwd org-done-keywords) 'org-done)
  5286. 'org-todo))
  5287. (defun org-face-from-face-or-color (context inherit face-or-color)
  5288. "Create a face list that inherits INHERIT, but sets the foreground color.
  5289. When FACE-OR-COLOR is not a string, just return it."
  5290. (if (stringp face-or-color)
  5291. (list :inherit inherit
  5292. (cdr (assoc context org-faces-easy-properties))
  5293. face-or-color)
  5294. face-or-color))
  5295. (defun org-font-lock-add-tag-faces (limit)
  5296. "Add the special tag faces."
  5297. (when (and org-tag-faces org-tags-special-faces-re)
  5298. (while (re-search-forward org-tags-special-faces-re limit t)
  5299. (add-text-properties (match-beginning 1) (match-end 1)
  5300. (list 'face (org-get-tag-face 1)
  5301. 'font-lock-fontified t))
  5302. (backward-char 1))))
  5303. (defun org-font-lock-add-priority-faces (limit)
  5304. "Add the special priority faces."
  5305. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5306. (when (save-match-data (org-at-heading-p))
  5307. (add-text-properties
  5308. (match-beginning 0) (match-end 0)
  5309. (list 'face (or (org-face-from-face-or-color
  5310. 'priority 'org-special-keyword
  5311. (cdr (assoc (char-after (match-beginning 1))
  5312. org-priority-faces)))
  5313. 'org-special-keyword)
  5314. 'font-lock-fontified t)))))
  5315. (defun org-get-tag-face (kwd)
  5316. "Get the right face for a TODO keyword KWD.
  5317. If KWD is a number, get the corresponding match group."
  5318. (if (numberp kwd) (setq kwd (match-string kwd)))
  5319. (or (org-face-from-face-or-color
  5320. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5321. 'org-tag))
  5322. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5323. "Remove fontification and activation overlays from links."
  5324. (font-lock-default-unfontify-region beg end)
  5325. (let* ((buffer-undo-list t)
  5326. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5327. (inhibit-modification-hooks t)
  5328. deactivate-mark buffer-file-name buffer-file-truename)
  5329. (org-decompose-region beg end)
  5330. (remove-text-properties beg end
  5331. '(mouse-face t keymap t org-linked-text t
  5332. invisible t intangible t
  5333. org-no-flyspell t org-emphasis t))
  5334. (org-remove-font-lock-display-properties beg end)))
  5335. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5336. ((raise 0.3) (height 0.7))
  5337. ((raise -0.5))
  5338. ((raise 0.5)))
  5339. "Display properties for showing superscripts and subscripts.")
  5340. (defun org-remove-font-lock-display-properties (beg end)
  5341. "Remove specific display properties that have been added by font lock.
  5342. The will remove the raise properties that are used to show superscripts
  5343. and subscripts."
  5344. (let (next prop)
  5345. (while (< beg end)
  5346. (setq next (next-single-property-change beg 'display nil end)
  5347. prop (get-text-property beg 'display))
  5348. (if (member prop org-script-display)
  5349. (put-text-property beg next 'display nil))
  5350. (setq beg next))))
  5351. (defun org-raise-scripts (limit)
  5352. "Add raise properties to sub/superscripts."
  5353. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5354. (if (re-search-forward
  5355. (if (eq org-use-sub-superscripts t)
  5356. org-match-substring-regexp
  5357. org-match-substring-with-braces-regexp)
  5358. limit t)
  5359. (let* ((pos (point)) table-p comment-p
  5360. (mpos (match-beginning 3))
  5361. (emph-p (get-text-property mpos 'org-emphasis))
  5362. (link-p (get-text-property mpos 'mouse-face))
  5363. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5364. (goto-char (point-at-bol))
  5365. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5366. comment-p (org-looking-at-p "[ \t]*#"))
  5367. (goto-char pos)
  5368. ;; FIXME: Should we go back one character here, for a_b^c
  5369. ;; (goto-char (1- pos)) ;????????????????????
  5370. (if (or comment-p emph-p link-p keyw-p)
  5371. t
  5372. (put-text-property (match-beginning 3) (match-end 0)
  5373. 'display
  5374. (if (equal (char-after (match-beginning 2)) ?^)
  5375. (nth (if table-p 3 1) org-script-display)
  5376. (nth (if table-p 2 0) org-script-display)))
  5377. (add-text-properties (match-beginning 2) (match-end 2)
  5378. (list 'invisible t
  5379. 'org-dwidth t 'org-dwidth-n 1))
  5380. (if (and (eq (char-after (match-beginning 3)) ?{)
  5381. (eq (char-before (match-end 3)) ?}))
  5382. (progn
  5383. (add-text-properties
  5384. (match-beginning 3) (1+ (match-beginning 3))
  5385. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5386. (add-text-properties
  5387. (1- (match-end 3)) (match-end 3)
  5388. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5389. t)))))
  5390. ;;;; Visibility cycling, including org-goto and indirect buffer
  5391. ;;; Cycling
  5392. (defvar org-cycle-global-status nil)
  5393. (make-variable-buffer-local 'org-cycle-global-status)
  5394. (defvar org-cycle-subtree-status nil)
  5395. (make-variable-buffer-local 'org-cycle-subtree-status)
  5396. (defvar org-inlinetask-min-level)
  5397. ;;;###autoload
  5398. (defun org-cycle (&optional arg)
  5399. "TAB-action and visibility cycling for Org-mode.
  5400. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5401. is outline visibility cycling, but it also invokes other actions
  5402. in special contexts.
  5403. - When this function is called with a prefix argument, rotate the entire
  5404. buffer through 3 states (global cycling)
  5405. 1. OVERVIEW: Show only top-level headlines.
  5406. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5407. 3. SHOW ALL: Show everything.
  5408. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5409. determined by the variable `org-startup-folded', and by any VISIBILITY
  5410. properties in the buffer.
  5411. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5412. including any drawers.
  5413. - When inside a table, re-align the table and move to the next field.
  5414. - When point is at the beginning of a headline, rotate the subtree started
  5415. by this line through 3 different states (local cycling)
  5416. 1. FOLDED: Only the main headline is shown.
  5417. 2. CHILDREN: The main headline and the direct children are shown.
  5418. From this state, you can move to one of the children
  5419. and zoom in further.
  5420. 3. SUBTREE: Show the entire subtree, including body text.
  5421. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5422. - When point is at the beginning of an empty headline and the variable
  5423. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5424. of the headline by demoting and promoting it to likely levels. This
  5425. speeds up creation document structure by pressing TAB once or several
  5426. times right after creating a new headline.
  5427. - When there is a numeric prefix, go up to a heading with level ARG, do
  5428. a `show-subtree' and return to the previous cursor position. If ARG
  5429. is negative, go up that many levels.
  5430. - When point is not at the beginning of a headline, execute the global
  5431. binding for TAB, which is re-indenting the line. See the option
  5432. `org-cycle-emulate-tab' for details.
  5433. - Special case: if point is at the beginning of the buffer and there is
  5434. no headline in line 1, this function will act as if called with prefix arg
  5435. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5436. But only if also the variable `org-cycle-global-at-bob' is t."
  5437. (interactive "P")
  5438. (org-load-modules-maybe)
  5439. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5440. (and org-cycle-level-after-item/entry-creation
  5441. (or (org-cycle-level)
  5442. (org-cycle-item-indentation))))
  5443. (let* ((limit-level
  5444. (or org-cycle-max-level
  5445. (and (boundp 'org-inlinetask-min-level)
  5446. org-inlinetask-min-level
  5447. (1- org-inlinetask-min-level))))
  5448. (nstars (and limit-level
  5449. (if org-odd-levels-only
  5450. (and limit-level (1- (* limit-level 2)))
  5451. limit-level)))
  5452. (org-outline-regexp
  5453. (if (not (derived-mode-p 'org-mode))
  5454. outline-regexp
  5455. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5456. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5457. (not (looking-at org-outline-regexp))))
  5458. (org-cycle-hook
  5459. (if bob-special
  5460. (delq 'org-optimize-window-after-visibility-change
  5461. (copy-sequence org-cycle-hook))
  5462. org-cycle-hook))
  5463. (pos (point)))
  5464. (if (or bob-special (equal arg '(4)))
  5465. ;; special case: use global cycling
  5466. (setq arg t))
  5467. (cond
  5468. ((equal arg '(16))
  5469. (setq last-command 'dummy)
  5470. (org-set-startup-visibility)
  5471. (message "Startup visibility, plus VISIBILITY properties"))
  5472. ((equal arg '(64))
  5473. (show-all)
  5474. (message "Entire buffer visible, including drawers"))
  5475. ;; Table: enter it or move to the next field.
  5476. ((org-at-table-p 'any)
  5477. (if (org-at-table.el-p)
  5478. (message "Use C-c ' to edit table.el tables")
  5479. (if arg (org-table-edit-field t)
  5480. (org-table-justify-field-maybe)
  5481. (call-interactively 'org-table-next-field))))
  5482. ((run-hook-with-args-until-success
  5483. 'org-tab-after-check-for-table-hook))
  5484. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5485. ((eq arg t) (org-cycle-internal-global))
  5486. ;; Drawers: delegate to `org-flag-drawer'.
  5487. ((and org-drawers org-drawer-regexp
  5488. (save-excursion
  5489. (beginning-of-line 1)
  5490. (looking-at org-drawer-regexp)))
  5491. (org-flag-drawer ; toggle block visibility
  5492. (not (get-char-property (match-end 0) 'invisible))))
  5493. ;; Show-subtree, ARG levels up from here.
  5494. ((integerp arg)
  5495. (save-excursion
  5496. (org-back-to-heading)
  5497. (outline-up-heading (if (< arg 0) (- arg)
  5498. (- (funcall outline-level) arg)))
  5499. (org-show-subtree)))
  5500. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5501. ((and (featurep 'org-inlinetask)
  5502. (org-inlinetask-at-task-p)
  5503. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5504. (org-inlinetask-toggle-visibility))
  5505. ((org-try-cdlatex-tab))
  5506. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5507. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5508. (save-excursion (beginning-of-line 1)
  5509. (looking-at org-outline-regexp)))
  5510. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5511. (org-cycle-internal-local))
  5512. ;; From there: TAB emulation and template completion.
  5513. (buffer-read-only (org-back-to-heading))
  5514. ((run-hook-with-args-until-success
  5515. 'org-tab-after-check-for-cycling-hook))
  5516. ((org-try-structure-completion))
  5517. ((run-hook-with-args-until-success
  5518. 'org-tab-before-tab-emulation-hook))
  5519. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5520. (or (not (bolp))
  5521. (not (looking-at org-outline-regexp))))
  5522. (call-interactively (global-key-binding "\t")))
  5523. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5524. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5525. (or (and (eq org-cycle-emulate-tab 'white)
  5526. (= (match-end 0) (point-at-eol)))
  5527. (and (eq org-cycle-emulate-tab 'whitestart)
  5528. (>= (match-end 0) pos))))
  5529. t
  5530. (eq org-cycle-emulate-tab t))
  5531. (call-interactively (global-key-binding "\t")))
  5532. (t (save-excursion
  5533. (org-back-to-heading)
  5534. (org-cycle)))))))
  5535. (defun org-cycle-internal-global ()
  5536. "Do the global cycling action."
  5537. ;; Hack to avoid display of messages for .org attachments in Gnus
  5538. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5539. (cond
  5540. ((and (eq last-command this-command)
  5541. (eq org-cycle-global-status 'overview))
  5542. ;; We just created the overview - now do table of contents
  5543. ;; This can be slow in very large buffers, so indicate action
  5544. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5545. (unless ga (message "CONTENTS..."))
  5546. (org-content)
  5547. (unless ga (message "CONTENTS...done"))
  5548. (setq org-cycle-global-status 'contents)
  5549. (run-hook-with-args 'org-cycle-hook 'contents))
  5550. ((and (eq last-command this-command)
  5551. (eq org-cycle-global-status 'contents))
  5552. ;; We just showed the table of contents - now show everything
  5553. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5554. (show-all)
  5555. (unless ga (message "SHOW ALL"))
  5556. (setq org-cycle-global-status 'all)
  5557. (run-hook-with-args 'org-cycle-hook 'all))
  5558. (t
  5559. ;; Default action: go to overview
  5560. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5561. (org-overview)
  5562. (unless ga (message "OVERVIEW"))
  5563. (setq org-cycle-global-status 'overview)
  5564. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5565. (defun org-cycle-internal-local ()
  5566. "Do the local cycling action."
  5567. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5568. ;; First, determine end of headline (EOH), end of subtree or item
  5569. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5570. (save-excursion
  5571. (if (org-at-item-p)
  5572. (progn
  5573. (beginning-of-line)
  5574. (setq struct (org-list-struct))
  5575. (setq eoh (point-at-eol))
  5576. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5577. (setq has-children (org-list-has-child-p (point) struct)))
  5578. (org-back-to-heading)
  5579. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5580. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5581. (setq has-children
  5582. (or (save-excursion
  5583. (let ((level (funcall outline-level)))
  5584. (outline-next-heading)
  5585. (and (org-at-heading-p t)
  5586. (> (funcall outline-level) level))))
  5587. (save-excursion
  5588. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5589. ;; Determine end invisible part of buffer (EOL)
  5590. (beginning-of-line 2)
  5591. ;; XEmacs doesn't have `next-single-char-property-change'
  5592. (if (featurep 'xemacs)
  5593. (while (and (not (eobp)) ;; this is like `next-line'
  5594. (get-char-property (1- (point)) 'invisible))
  5595. (beginning-of-line 2))
  5596. (while (and (not (eobp)) ;; this is like `next-line'
  5597. (get-char-property (1- (point)) 'invisible))
  5598. (goto-char (next-single-char-property-change (point) 'invisible))
  5599. (and (eolp) (beginning-of-line 2))))
  5600. (setq eol (point)))
  5601. ;; Find out what to do next and set `this-command'
  5602. (cond
  5603. ((= eos eoh)
  5604. ;; Nothing is hidden behind this heading
  5605. (unless (org-before-first-heading-p)
  5606. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5607. (message "EMPTY ENTRY")
  5608. (setq org-cycle-subtree-status nil)
  5609. (save-excursion
  5610. (goto-char eos)
  5611. (outline-next-heading)
  5612. (if (outline-invisible-p) (org-flag-heading nil))))
  5613. ((and (or (>= eol eos)
  5614. (not (string-match "\\S-" (buffer-substring eol eos))))
  5615. (or has-children
  5616. (not (setq children-skipped
  5617. org-cycle-skip-children-state-if-no-children))))
  5618. ;; Entire subtree is hidden in one line: children view
  5619. (unless (org-before-first-heading-p)
  5620. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5621. (if (org-at-item-p)
  5622. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5623. (org-show-entry)
  5624. (org-with-limited-levels (show-children))
  5625. ;; FIXME: This slows down the func way too much.
  5626. ;; How keep drawers hidden in subtree anyway?
  5627. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5628. ;; (org-cycle-hide-drawers 'subtree))
  5629. ;; Fold every list in subtree to top-level items.
  5630. (when (eq org-cycle-include-plain-lists 'integrate)
  5631. (save-excursion
  5632. (org-back-to-heading)
  5633. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5634. (beginning-of-line 1)
  5635. (let* ((struct (org-list-struct))
  5636. (prevs (org-list-prevs-alist struct))
  5637. (end (org-list-get-bottom-point struct)))
  5638. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5639. (org-list-get-all-items (point) struct prevs))
  5640. (goto-char end))))))
  5641. (message "CHILDREN")
  5642. (save-excursion
  5643. (goto-char eos)
  5644. (outline-next-heading)
  5645. (if (outline-invisible-p) (org-flag-heading nil)))
  5646. (setq org-cycle-subtree-status 'children)
  5647. (unless (org-before-first-heading-p)
  5648. (run-hook-with-args 'org-cycle-hook 'children)))
  5649. ((or children-skipped
  5650. (and (eq last-command this-command)
  5651. (eq org-cycle-subtree-status 'children)))
  5652. ;; We just showed the children, or no children are there,
  5653. ;; now show everything.
  5654. (unless (org-before-first-heading-p)
  5655. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5656. (outline-flag-region eoh eos nil)
  5657. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5658. (setq org-cycle-subtree-status 'subtree)
  5659. (unless (org-before-first-heading-p)
  5660. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5661. (t
  5662. ;; Default action: hide the subtree.
  5663. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5664. (outline-flag-region eoh eos t)
  5665. (message "FOLDED")
  5666. (setq org-cycle-subtree-status 'folded)
  5667. (unless (org-before-first-heading-p)
  5668. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5669. ;;;###autoload
  5670. (defun org-global-cycle (&optional arg)
  5671. "Cycle the global visibility. For details see `org-cycle'.
  5672. With \\[universal-argument] prefix arg, switch to startup visibility.
  5673. With a numeric prefix, show all headlines up to that level."
  5674. (interactive "P")
  5675. (let ((org-cycle-include-plain-lists
  5676. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5677. (cond
  5678. ((integerp arg)
  5679. (show-all)
  5680. (hide-sublevels arg)
  5681. (setq org-cycle-global-status 'contents))
  5682. ((equal arg '(4))
  5683. (org-set-startup-visibility)
  5684. (message "Startup visibility, plus VISIBILITY properties."))
  5685. (t
  5686. (org-cycle '(4))))))
  5687. (defun org-set-startup-visibility ()
  5688. "Set the visibility required by startup options and properties."
  5689. (cond
  5690. ((eq org-startup-folded t)
  5691. (org-cycle '(4)))
  5692. ((eq org-startup-folded 'content)
  5693. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5694. (org-cycle '(4)) (org-cycle '(4)))))
  5695. (unless (eq org-startup-folded 'showeverything)
  5696. (if org-hide-block-startup (org-hide-block-all))
  5697. (org-set-visibility-according-to-property 'no-cleanup)
  5698. (org-cycle-hide-archived-subtrees 'all)
  5699. (org-cycle-hide-drawers 'all)
  5700. (org-cycle-show-empty-lines t)))
  5701. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5702. "Switch subtree visibilities according to :VISIBILITY: property."
  5703. (interactive)
  5704. (let (org-show-entry-below state)
  5705. (save-excursion
  5706. (goto-char (point-min))
  5707. (while (re-search-forward
  5708. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5709. nil t)
  5710. (setq state (match-string 1))
  5711. (save-excursion
  5712. (org-back-to-heading t)
  5713. (hide-subtree)
  5714. (org-reveal)
  5715. (cond
  5716. ((equal state '("fold" "folded"))
  5717. (hide-subtree))
  5718. ((equal state "children")
  5719. (org-show-hidden-entry)
  5720. (show-children))
  5721. ((equal state "content")
  5722. (save-excursion
  5723. (save-restriction
  5724. (org-narrow-to-subtree)
  5725. (org-content))))
  5726. ((member state '("all" "showall"))
  5727. (show-subtree)))))
  5728. (unless no-cleanup
  5729. (org-cycle-hide-archived-subtrees 'all)
  5730. (org-cycle-hide-drawers 'all)
  5731. (org-cycle-show-empty-lines 'all)))))
  5732. ;; This function uses outline-regexp instead of the more fundamental
  5733. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5734. ;; buffers, where outline-regexp is needed.
  5735. (defun org-overview ()
  5736. "Switch to overview mode, showing only top-level headlines.
  5737. Really, this shows all headlines with level equal or greater than the level
  5738. of the first headline in the buffer. This is important, because if the
  5739. first headline is not level one, then (hide-sublevels 1) gives confusing
  5740. results."
  5741. (interactive)
  5742. (let ((level (save-excursion
  5743. (goto-char (point-min))
  5744. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5745. (progn
  5746. (goto-char (match-beginning 0))
  5747. (funcall outline-level))))))
  5748. (and level (hide-sublevels level))))
  5749. (defun org-content (&optional arg)
  5750. "Show all headlines in the buffer, like a table of contents.
  5751. With numerical argument N, show content up to level N."
  5752. (interactive "P")
  5753. (save-excursion
  5754. ;; Visit all headings and show their offspring
  5755. (and (integerp arg) (org-overview))
  5756. (goto-char (point-max))
  5757. (catch 'exit
  5758. (while (and (progn (condition-case nil
  5759. (outline-previous-visible-heading 1)
  5760. (error (goto-char (point-min))))
  5761. t)
  5762. (looking-at org-outline-regexp))
  5763. (if (integerp arg)
  5764. (show-children (1- arg))
  5765. (show-branches))
  5766. (if (bobp) (throw 'exit nil))))))
  5767. (defun org-optimize-window-after-visibility-change (state)
  5768. "Adjust the window after a change in outline visibility.
  5769. This function is the default value of the hook `org-cycle-hook'."
  5770. (when (get-buffer-window (current-buffer))
  5771. (cond
  5772. ((eq state 'content) nil)
  5773. ((eq state 'all) nil)
  5774. ((eq state 'folded) nil)
  5775. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5776. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5777. (defun org-remove-empty-overlays-at (pos)
  5778. "Remove outline overlays that do not contain non-white stuff."
  5779. (mapc
  5780. (lambda (o)
  5781. (and (eq 'outline (overlay-get o 'invisible))
  5782. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5783. (overlay-end o))))
  5784. (delete-overlay o)))
  5785. (overlays-at pos)))
  5786. (defun org-clean-visibility-after-subtree-move ()
  5787. "Fix visibility issues after moving a subtree."
  5788. ;; First, find a reasonable region to look at:
  5789. ;; Start two siblings above, end three below
  5790. (let* ((beg (save-excursion
  5791. (and (org-get-last-sibling)
  5792. (org-get-last-sibling))
  5793. (point)))
  5794. (end (save-excursion
  5795. (and (org-get-next-sibling)
  5796. (org-get-next-sibling)
  5797. (org-get-next-sibling))
  5798. (if (org-at-heading-p)
  5799. (point-at-eol)
  5800. (point))))
  5801. (level (looking-at "\\*+"))
  5802. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5803. (save-excursion
  5804. (save-restriction
  5805. (narrow-to-region beg end)
  5806. (when re
  5807. ;; Properly fold already folded siblings
  5808. (goto-char (point-min))
  5809. (while (re-search-forward re nil t)
  5810. (if (and (not (outline-invisible-p))
  5811. (save-excursion
  5812. (goto-char (point-at-eol)) (outline-invisible-p)))
  5813. (hide-entry))))
  5814. (org-cycle-show-empty-lines 'overview)
  5815. (org-cycle-hide-drawers 'overview)))))
  5816. (defun org-cycle-show-empty-lines (state)
  5817. "Show empty lines above all visible headlines.
  5818. The region to be covered depends on STATE when called through
  5819. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5820. entire buffer covered. Note that an empty line is only shown if there
  5821. are at least `org-cycle-separator-lines' empty lines before the headline."
  5822. (when (not (= org-cycle-separator-lines 0))
  5823. (save-excursion
  5824. (let* ((n (abs org-cycle-separator-lines))
  5825. (re (cond
  5826. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5827. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5828. (t (let ((ns (number-to-string (- n 2))))
  5829. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5830. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5831. beg end b e)
  5832. (cond
  5833. ((memq state '(overview contents t))
  5834. (setq beg (point-min) end (point-max)))
  5835. ((memq state '(children folded))
  5836. (setq beg (point) end (progn (org-end-of-subtree t t)
  5837. (beginning-of-line 2)
  5838. (point)))))
  5839. (when beg
  5840. (goto-char beg)
  5841. (while (re-search-forward re end t)
  5842. (unless (get-char-property (match-end 1) 'invisible)
  5843. (setq e (match-end 1))
  5844. (if (< org-cycle-separator-lines 0)
  5845. (setq b (save-excursion
  5846. (goto-char (match-beginning 0))
  5847. (org-back-over-empty-lines)
  5848. (if (save-excursion
  5849. (goto-char (max (point-min) (1- (point))))
  5850. (org-at-heading-p))
  5851. (1- (point))
  5852. (point))))
  5853. (setq b (match-beginning 1)))
  5854. (outline-flag-region b e nil)))))))
  5855. ;; Never hide empty lines at the end of the file.
  5856. (save-excursion
  5857. (goto-char (point-max))
  5858. (outline-previous-heading)
  5859. (outline-end-of-heading)
  5860. (if (and (looking-at "[ \t\n]+")
  5861. (= (match-end 0) (point-max)))
  5862. (outline-flag-region (point) (match-end 0) nil))))
  5863. (defun org-show-empty-lines-in-parent ()
  5864. "Move to the parent and re-show empty lines before visible headlines."
  5865. (save-excursion
  5866. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5867. (org-cycle-show-empty-lines context))))
  5868. (defun org-files-list ()
  5869. "Return `org-agenda-files' list, plus all open org-mode files.
  5870. This is useful for operations that need to scan all of a user's
  5871. open and agenda-wise Org files."
  5872. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5873. (dolist (buf (buffer-list))
  5874. (with-current-buffer buf
  5875. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  5876. (let ((file (expand-file-name (buffer-file-name))))
  5877. (unless (member file files)
  5878. (push file files))))))
  5879. files))
  5880. (defsubst org-entry-beginning-position ()
  5881. "Return the beginning position of the current entry."
  5882. (save-excursion (outline-back-to-heading t) (point)))
  5883. (defsubst org-entry-end-position ()
  5884. "Return the end position of the current entry."
  5885. (save-excursion (outline-next-heading) (point)))
  5886. (defun org-cycle-hide-drawers (state)
  5887. "Re-hide all drawers after a visibility state change."
  5888. (when (and (derived-mode-p 'org-mode)
  5889. (not (memq state '(overview folded contents))))
  5890. (save-excursion
  5891. (let* ((globalp (memq state '(contents all)))
  5892. (beg (if globalp (point-min) (point)))
  5893. (end (if globalp (point-max)
  5894. (if (eq state 'children)
  5895. (save-excursion (outline-next-heading) (point))
  5896. (org-end-of-subtree t)))))
  5897. (goto-char beg)
  5898. (while (re-search-forward org-drawer-regexp end t)
  5899. (org-flag-drawer t))))))
  5900. (defun org-flag-drawer (flag)
  5901. "When FLAG is non-nil, hide the drawer we are within.
  5902. Otherwise make it visible."
  5903. (save-excursion
  5904. (beginning-of-line 1)
  5905. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5906. (let ((b (match-end 0)))
  5907. (if (re-search-forward
  5908. "^[ \t]*:END:"
  5909. (save-excursion (outline-next-heading) (point)) t)
  5910. (outline-flag-region b (point-at-eol) flag)
  5911. (error ":END: line missing at position %s" b))))))
  5912. (defun org-subtree-end-visible-p ()
  5913. "Is the end of the current subtree visible?"
  5914. (pos-visible-in-window-p
  5915. (save-excursion (org-end-of-subtree t) (point))))
  5916. (defun org-first-headline-recenter (&optional N)
  5917. "Move cursor to the first headline and recenter the headline.
  5918. Optional argument N means put the headline into the Nth line of the window."
  5919. (goto-char (point-min))
  5920. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5921. (beginning-of-line)
  5922. (recenter (prefix-numeric-value N))))
  5923. ;;; Saving and restoring visibility
  5924. (defun org-outline-overlay-data (&optional use-markers)
  5925. "Return a list of the locations of all outline overlays.
  5926. These are overlays with the `invisible' property value `outline'.
  5927. The return value is a list of cons cells, with start and stop
  5928. positions for each overlay.
  5929. If USE-MARKERS is set, return the positions as markers."
  5930. (let (beg end)
  5931. (save-excursion
  5932. (save-restriction
  5933. (widen)
  5934. (delq nil
  5935. (mapcar (lambda (o)
  5936. (when (eq (overlay-get o 'invisible) 'outline)
  5937. (setq beg (overlay-start o)
  5938. end (overlay-end o))
  5939. (and beg end (> end beg)
  5940. (if use-markers
  5941. (cons (move-marker (make-marker) beg)
  5942. (move-marker (make-marker) end))
  5943. (cons beg end)))))
  5944. (overlays-in (point-min) (point-max))))))))
  5945. (defun org-set-outline-overlay-data (data)
  5946. "Create visibility overlays for all positions in DATA.
  5947. DATA should have been made by `org-outline-overlay-data'."
  5948. (let (o)
  5949. (save-excursion
  5950. (save-restriction
  5951. (widen)
  5952. (show-all)
  5953. (mapc (lambda (c)
  5954. (outline-flag-region (car c) (cdr c) t))
  5955. data)))))
  5956. ;;; Folding of blocks
  5957. (defvar org-hide-block-overlays nil
  5958. "Overlays hiding blocks.")
  5959. (make-variable-buffer-local 'org-hide-block-overlays)
  5960. (defun org-block-map (function &optional start end)
  5961. "Call FUNCTION at the head of all source blocks in the current buffer.
  5962. Optional arguments START and END can be used to limit the range."
  5963. (let ((start (or start (point-min)))
  5964. (end (or end (point-max))))
  5965. (save-excursion
  5966. (goto-char start)
  5967. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5968. (save-excursion
  5969. (save-match-data
  5970. (goto-char (match-beginning 0))
  5971. (funcall function)))))))
  5972. (defun org-hide-block-toggle-all ()
  5973. "Toggle the visibility of all blocks in the current buffer."
  5974. (org-block-map #'org-hide-block-toggle))
  5975. (defun org-hide-block-all ()
  5976. "Fold all blocks in the current buffer."
  5977. (interactive)
  5978. (org-show-block-all)
  5979. (org-block-map #'org-hide-block-toggle-maybe))
  5980. (defun org-show-block-all ()
  5981. "Unfold all blocks in the current buffer."
  5982. (interactive)
  5983. (mapc 'delete-overlay org-hide-block-overlays)
  5984. (setq org-hide-block-overlays nil))
  5985. (defun org-hide-block-toggle-maybe ()
  5986. "Toggle visibility of block at point."
  5987. (interactive)
  5988. (let ((case-fold-search t))
  5989. (if (save-excursion
  5990. (beginning-of-line 1)
  5991. (looking-at org-block-regexp))
  5992. (progn (org-hide-block-toggle)
  5993. t) ;; to signal that we took action
  5994. nil))) ;; to signal that we did not
  5995. (defun org-hide-block-toggle (&optional force)
  5996. "Toggle the visibility of the current block."
  5997. (interactive)
  5998. (save-excursion
  5999. (beginning-of-line)
  6000. (if (re-search-forward org-block-regexp nil t)
  6001. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6002. (end (match-end 0)) ;; end of entire body
  6003. ov)
  6004. (if (memq t (mapcar (lambda (overlay)
  6005. (eq (overlay-get overlay 'invisible)
  6006. 'org-hide-block))
  6007. (overlays-at start)))
  6008. (if (or (not force) (eq force 'off))
  6009. (mapc (lambda (ov)
  6010. (when (member ov org-hide-block-overlays)
  6011. (setq org-hide-block-overlays
  6012. (delq ov org-hide-block-overlays)))
  6013. (when (eq (overlay-get ov 'invisible)
  6014. 'org-hide-block)
  6015. (delete-overlay ov)))
  6016. (overlays-at start)))
  6017. (setq ov (make-overlay start end))
  6018. (overlay-put ov 'invisible 'org-hide-block)
  6019. ;; make the block accessible to isearch
  6020. (overlay-put
  6021. ov 'isearch-open-invisible
  6022. (lambda (ov)
  6023. (when (member ov org-hide-block-overlays)
  6024. (setq org-hide-block-overlays
  6025. (delq ov org-hide-block-overlays)))
  6026. (when (eq (overlay-get ov 'invisible)
  6027. 'org-hide-block)
  6028. (delete-overlay ov))))
  6029. (push ov org-hide-block-overlays)))
  6030. (error "Not looking at a source block"))))
  6031. ;; org-tab-after-check-for-cycling-hook
  6032. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6033. ;; Remove overlays when changing major mode
  6034. (add-hook 'org-mode-hook
  6035. (lambda () (org-add-hook 'change-major-mode-hook
  6036. 'org-show-block-all 'append 'local)))
  6037. ;;; Org-goto
  6038. (defvar org-goto-window-configuration nil)
  6039. (defvar org-goto-marker nil)
  6040. (defvar org-goto-map)
  6041. (defun org-goto-map ()
  6042. "Set the keymap `org-goto'."
  6043. (setq org-goto-map
  6044. (let ((map (make-sparse-keymap)))
  6045. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6046. mouse-drag-region universal-argument org-occur))
  6047. cmd)
  6048. (while (setq cmd (pop cmds))
  6049. (substitute-key-definition cmd cmd map global-map)))
  6050. (suppress-keymap map)
  6051. (org-defkey map "\C-m" 'org-goto-ret)
  6052. (org-defkey map [(return)] 'org-goto-ret)
  6053. (org-defkey map [(left)] 'org-goto-left)
  6054. (org-defkey map [(right)] 'org-goto-right)
  6055. (org-defkey map [(control ?g)] 'org-goto-quit)
  6056. (org-defkey map "\C-i" 'org-cycle)
  6057. (org-defkey map [(tab)] 'org-cycle)
  6058. (org-defkey map [(down)] 'outline-next-visible-heading)
  6059. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6060. (if org-goto-auto-isearch
  6061. (if (fboundp 'define-key-after)
  6062. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6063. nil)
  6064. (org-defkey map "q" 'org-goto-quit)
  6065. (org-defkey map "n" 'outline-next-visible-heading)
  6066. (org-defkey map "p" 'outline-previous-visible-heading)
  6067. (org-defkey map "f" 'outline-forward-same-level)
  6068. (org-defkey map "b" 'outline-backward-same-level)
  6069. (org-defkey map "u" 'outline-up-heading))
  6070. (org-defkey map "/" 'org-occur)
  6071. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6072. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6073. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6074. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6075. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6076. map)))
  6077. (defconst org-goto-help
  6078. "Browse buffer copy, to find location or copy text.%s
  6079. RET=jump to location C-g=quit and return to previous location
  6080. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6081. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6082. ;; FIXME: Docstring does not mention both interfaces
  6083. (defun org-goto (&optional alternative-interface)
  6084. "Look up a different location in the current file, keeping current visibility.
  6085. When you want look-up or go to a different location in a
  6086. document, the fastest way is often to fold the entire buffer and
  6087. then dive into the tree. This method has the disadvantage, that
  6088. the previous location will be folded, which may not be what you
  6089. want.
  6090. This command works around this by showing a copy of the current
  6091. buffer in an indirect buffer, in overview mode. You can dive
  6092. into the tree in that copy, use org-occur and incremental search
  6093. to find a location. When pressing RET or `Q', the command
  6094. returns to the original buffer in which the visibility is still
  6095. unchanged. After RET it will also jump to the location selected
  6096. in the indirect buffer and expose the headline hierarchy above.
  6097. With a prefix argument, use the alternative interface: e.g. if
  6098. `org-goto-interface' is 'outline use 'outline-path-completion."
  6099. (interactive "P")
  6100. (org-goto-map)
  6101. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6102. (org-refile-use-outline-path t)
  6103. (org-refile-target-verify-function nil)
  6104. (interface
  6105. (if (not alternative-interface)
  6106. org-goto-interface
  6107. (if (eq org-goto-interface 'outline)
  6108. 'outline-path-completion
  6109. 'outline)))
  6110. (org-goto-start-pos (point))
  6111. (selected-point
  6112. (if (eq interface 'outline)
  6113. (car (org-get-location (current-buffer) org-goto-help))
  6114. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6115. (org-refile-check-position pa)
  6116. (nth 3 pa)))))
  6117. (if selected-point
  6118. (progn
  6119. (org-mark-ring-push org-goto-start-pos)
  6120. (goto-char selected-point)
  6121. (if (or (outline-invisible-p) (org-invisible-p2))
  6122. (org-show-context 'org-goto)))
  6123. (message "Quit"))))
  6124. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6125. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6126. (defvar org-goto-local-auto-isearch-map) ; defined below
  6127. (defun org-get-location (buf help)
  6128. "Let the user select a location in the Org-mode buffer BUF.
  6129. This function uses a recursive edit. It returns the selected position
  6130. or nil."
  6131. (org-no-popups
  6132. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6133. (isearch-hide-immediately nil)
  6134. (isearch-search-fun-function
  6135. (lambda () 'org-goto-local-search-headings))
  6136. (org-goto-selected-point org-goto-exit-command))
  6137. (save-excursion
  6138. (save-window-excursion
  6139. (delete-other-windows)
  6140. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6141. (org-pop-to-buffer-same-window
  6142. (condition-case nil
  6143. (make-indirect-buffer (current-buffer) "*org-goto*")
  6144. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6145. (with-output-to-temp-buffer "*Org Help*"
  6146. (princ (format help (if org-goto-auto-isearch
  6147. " Just type for auto-isearch."
  6148. " n/p/f/b/u to navigate, q to quit."))))
  6149. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6150. (setq buffer-read-only nil)
  6151. (let ((org-startup-truncated t)
  6152. (org-startup-folded nil)
  6153. (org-startup-align-all-tables nil))
  6154. (org-mode)
  6155. (org-overview))
  6156. (setq buffer-read-only t)
  6157. (if (and (boundp 'org-goto-start-pos)
  6158. (integer-or-marker-p org-goto-start-pos))
  6159. (let ((org-show-hierarchy-above t)
  6160. (org-show-siblings t)
  6161. (org-show-following-heading t))
  6162. (goto-char org-goto-start-pos)
  6163. (and (outline-invisible-p) (org-show-context)))
  6164. (goto-char (point-min)))
  6165. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6166. (message "Select location and press RET")
  6167. (use-local-map org-goto-map)
  6168. (recursive-edit)))
  6169. (kill-buffer "*org-goto*")
  6170. (cons org-goto-selected-point org-goto-exit-command))))
  6171. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6172. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6173. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6174. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6175. (defun org-goto-local-search-headings (string bound noerror)
  6176. "Search and make sure that any matches are in headlines."
  6177. (catch 'return
  6178. (while (if isearch-forward
  6179. (search-forward string bound noerror)
  6180. (search-backward string bound noerror))
  6181. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6182. (and (member :headline context)
  6183. (not (member :tags context))))
  6184. (throw 'return (point))))))
  6185. (defun org-goto-local-auto-isearch ()
  6186. "Start isearch."
  6187. (interactive)
  6188. (goto-char (point-min))
  6189. (let ((keys (this-command-keys)))
  6190. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6191. (isearch-mode t)
  6192. (isearch-process-search-char (string-to-char keys)))))
  6193. (defun org-goto-ret (&optional arg)
  6194. "Finish `org-goto' by going to the new location."
  6195. (interactive "P")
  6196. (setq org-goto-selected-point (point)
  6197. org-goto-exit-command 'return)
  6198. (throw 'exit nil))
  6199. (defun org-goto-left ()
  6200. "Finish `org-goto' by going to the new location."
  6201. (interactive)
  6202. (if (org-at-heading-p)
  6203. (progn
  6204. (beginning-of-line 1)
  6205. (setq org-goto-selected-point (point)
  6206. org-goto-exit-command 'left)
  6207. (throw 'exit nil))
  6208. (error "Not on a heading")))
  6209. (defun org-goto-right ()
  6210. "Finish `org-goto' by going to the new location."
  6211. (interactive)
  6212. (if (org-at-heading-p)
  6213. (progn
  6214. (setq org-goto-selected-point (point)
  6215. org-goto-exit-command 'right)
  6216. (throw 'exit nil))
  6217. (error "Not on a heading")))
  6218. (defun org-goto-quit ()
  6219. "Finish `org-goto' without cursor motion."
  6220. (interactive)
  6221. (setq org-goto-selected-point nil)
  6222. (setq org-goto-exit-command 'quit)
  6223. (throw 'exit nil))
  6224. ;;; Indirect buffer display of subtrees
  6225. (defvar org-indirect-dedicated-frame nil
  6226. "This is the frame being used for indirect tree display.")
  6227. (defvar org-last-indirect-buffer nil)
  6228. (defun org-tree-to-indirect-buffer (&optional arg)
  6229. "Create indirect buffer and narrow it to current subtree.
  6230. With a numerical prefix ARG, go up to this level and then take that tree.
  6231. If ARG is negative, go up that many levels.
  6232. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6233. indirect buffer previously made with this command, to avoid proliferation of
  6234. indirect buffers. However, when you call the command with a \
  6235. \\[universal-argument] prefix, or
  6236. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6237. is kept so that you can work with several indirect buffers at the same time.
  6238. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6239. \\[universal-argument] prefix also
  6240. requests that a new frame be made for the new buffer, so that the dedicated
  6241. frame is not changed."
  6242. (interactive "P")
  6243. (let ((cbuf (current-buffer))
  6244. (cwin (selected-window))
  6245. (pos (point))
  6246. beg end level heading ibuf)
  6247. (save-excursion
  6248. (org-back-to-heading t)
  6249. (when (numberp arg)
  6250. (setq level (org-outline-level))
  6251. (if (< arg 0) (setq arg (+ level arg)))
  6252. (while (> (setq level (org-outline-level)) arg)
  6253. (org-up-heading-safe)))
  6254. (setq beg (point)
  6255. heading (org-get-heading))
  6256. (org-end-of-subtree t t)
  6257. (if (org-at-heading-p) (backward-char 1))
  6258. (setq end (point)))
  6259. (if (and (buffer-live-p org-last-indirect-buffer)
  6260. (not (eq org-indirect-buffer-display 'new-frame))
  6261. (not arg))
  6262. (kill-buffer org-last-indirect-buffer))
  6263. (setq ibuf (org-get-indirect-buffer cbuf)
  6264. org-last-indirect-buffer ibuf)
  6265. (cond
  6266. ((or (eq org-indirect-buffer-display 'new-frame)
  6267. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6268. (select-frame (make-frame))
  6269. (delete-other-windows)
  6270. (org-pop-to-buffer-same-window ibuf)
  6271. (org-set-frame-title heading))
  6272. ((eq org-indirect-buffer-display 'dedicated-frame)
  6273. (raise-frame
  6274. (select-frame (or (and org-indirect-dedicated-frame
  6275. (frame-live-p org-indirect-dedicated-frame)
  6276. org-indirect-dedicated-frame)
  6277. (setq org-indirect-dedicated-frame (make-frame)))))
  6278. (delete-other-windows)
  6279. (org-pop-to-buffer-same-window ibuf)
  6280. (org-set-frame-title (concat "Indirect: " heading)))
  6281. ((eq org-indirect-buffer-display 'current-window)
  6282. (org-pop-to-buffer-same-window ibuf))
  6283. ((eq org-indirect-buffer-display 'other-window)
  6284. (pop-to-buffer ibuf))
  6285. (t (error "Invalid value")))
  6286. (if (featurep 'xemacs)
  6287. (save-excursion (org-mode) (turn-on-font-lock)))
  6288. (narrow-to-region beg end)
  6289. (show-all)
  6290. (goto-char pos)
  6291. (run-hook-with-args 'org-cycle-hook 'all)
  6292. (and (window-live-p cwin) (select-window cwin))))
  6293. (defun org-get-indirect-buffer (&optional buffer)
  6294. (setq buffer (or buffer (current-buffer)))
  6295. (let ((n 1) (base (buffer-name buffer)) bname)
  6296. (while (buffer-live-p
  6297. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6298. (setq n (1+ n)))
  6299. (condition-case nil
  6300. (make-indirect-buffer buffer bname 'clone)
  6301. (error (make-indirect-buffer buffer bname)))))
  6302. (defun org-set-frame-title (title)
  6303. "Set the title of the current frame to the string TITLE."
  6304. ;; FIXME: how to name a single frame in XEmacs???
  6305. (unless (featurep 'xemacs)
  6306. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6307. ;;;; Structure editing
  6308. ;;; Inserting headlines
  6309. (defun org-previous-line-empty-p ()
  6310. (save-excursion
  6311. (and (not (bobp))
  6312. (or (beginning-of-line 0) t)
  6313. (save-match-data
  6314. (looking-at "[ \t]*$")))))
  6315. (defun org-insert-heading (&optional force-heading invisible-ok)
  6316. "Insert a new heading or item with same depth at point.
  6317. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6318. If point is at the beginning of a headline, insert a sibling before the
  6319. current headline. If point is not at the beginning, split the line,
  6320. create the new headline with the text in the current line after point
  6321. \(but see also the variable `org-M-RET-may-split-line').
  6322. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6323. This is important for non-interactive uses of the command."
  6324. (interactive "P")
  6325. (if (or (= (buffer-size) 0)
  6326. (and (not (save-excursion
  6327. (and (ignore-errors (org-back-to-heading invisible-ok))
  6328. (org-at-heading-p))))
  6329. (or force-heading (not (org-in-item-p)))))
  6330. (progn
  6331. (insert "\n* ")
  6332. (run-hooks 'org-insert-heading-hook))
  6333. (when (or force-heading (not (org-insert-item)))
  6334. (let* ((empty-line-p nil)
  6335. (level nil)
  6336. (on-heading (org-at-heading-p))
  6337. (head (save-excursion
  6338. (condition-case nil
  6339. (progn
  6340. (org-back-to-heading invisible-ok)
  6341. (when (and (not on-heading)
  6342. (featurep 'org-inlinetask)
  6343. (integerp org-inlinetask-min-level)
  6344. (>= (length (match-string 0))
  6345. org-inlinetask-min-level))
  6346. ;; Find a heading level before the inline task
  6347. (while (and (setq level (org-up-heading-safe))
  6348. (>= level org-inlinetask-min-level)))
  6349. (if (org-at-heading-p)
  6350. (org-back-to-heading invisible-ok)
  6351. (error "This should not happen")))
  6352. (setq empty-line-p (org-previous-line-empty-p))
  6353. (match-string 0))
  6354. (error "*"))))
  6355. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6356. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6357. pos hide-previous previous-pos)
  6358. (cond
  6359. ((and (org-at-heading-p) (bolp)
  6360. (or (bobp)
  6361. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6362. ;; insert before the current line
  6363. (open-line (if blank 2 1)))
  6364. ((and (bolp)
  6365. (not org-insert-heading-respect-content)
  6366. (or (bobp)
  6367. (save-excursion
  6368. (backward-char 1) (not (outline-invisible-p)))))
  6369. ;; insert right here
  6370. nil)
  6371. (t
  6372. ;; somewhere in the line
  6373. (save-excursion
  6374. (setq previous-pos (point-at-bol))
  6375. (end-of-line)
  6376. (setq hide-previous (outline-invisible-p)))
  6377. (and org-insert-heading-respect-content (org-show-subtree))
  6378. (let ((split
  6379. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6380. (save-excursion
  6381. (let ((p (point)))
  6382. (goto-char (point-at-bol))
  6383. (and (looking-at org-complex-heading-regexp)
  6384. (match-beginning 4)
  6385. (> p (match-beginning 4)))))))
  6386. tags pos)
  6387. (cond
  6388. (org-insert-heading-respect-content
  6389. (org-end-of-subtree nil t)
  6390. (when (featurep 'org-inlinetask)
  6391. (while (and (not (eobp))
  6392. (looking-at "\\(\\*+\\)[ \t]+")
  6393. (>= (length (match-string 1))
  6394. org-inlinetask-min-level))
  6395. (org-end-of-subtree nil t)))
  6396. (or (bolp) (newline))
  6397. (or (org-previous-line-empty-p)
  6398. (and blank (newline)))
  6399. (open-line 1))
  6400. ((org-at-heading-p)
  6401. (when hide-previous
  6402. (show-children)
  6403. (org-show-entry))
  6404. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6405. (setq tags (and (match-end 2) (match-string 2)))
  6406. (and (match-end 1)
  6407. (delete-region (match-beginning 1) (match-end 1)))
  6408. (setq pos (point-at-bol))
  6409. (or split (end-of-line 1))
  6410. (delete-horizontal-space)
  6411. (if (string-match "\\`\\*+\\'"
  6412. (buffer-substring (point-at-bol) (point)))
  6413. (insert " "))
  6414. (newline (if blank 2 1))
  6415. (when tags
  6416. (save-excursion
  6417. (goto-char pos)
  6418. (end-of-line 1)
  6419. (insert " " tags)
  6420. (org-set-tags nil 'align))))
  6421. (t
  6422. (or split (end-of-line 1))
  6423. (newline (if blank 2 1)))))))
  6424. (insert head) (just-one-space)
  6425. (setq pos (point))
  6426. (end-of-line 1)
  6427. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6428. (when (and org-insert-heading-respect-content hide-previous)
  6429. (save-excursion
  6430. (goto-char previous-pos)
  6431. (hide-subtree)))
  6432. (run-hooks 'org-insert-heading-hook)))))
  6433. (defun org-get-heading (&optional no-tags no-todo)
  6434. "Return the heading of the current entry, without the stars.
  6435. When NO-TAGS is non-nil, don't include tags.
  6436. When NO-TODO is non-nil, don't include TODO keywords."
  6437. (save-excursion
  6438. (org-back-to-heading t)
  6439. (cond
  6440. ((and no-tags no-todo)
  6441. (looking-at org-complex-heading-regexp)
  6442. (match-string 4))
  6443. (no-tags
  6444. (looking-at (concat org-outline-regexp
  6445. "\\(.*?\\)"
  6446. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6447. (match-string 1))
  6448. (no-todo
  6449. (looking-at org-todo-line-regexp)
  6450. (match-string 3))
  6451. (t (looking-at org-heading-regexp)
  6452. (match-string 2)))))
  6453. (defun org-heading-components ()
  6454. "Return the components of the current heading.
  6455. This is a list with the following elements:
  6456. - the level as an integer
  6457. - the reduced level, different if `org-odd-levels-only' is set.
  6458. - the TODO keyword, or nil
  6459. - the priority character, like ?A, or nil if no priority is given
  6460. - the headline text itself, or the tags string if no headline text
  6461. - the tags string, or nil."
  6462. (save-excursion
  6463. (org-back-to-heading t)
  6464. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6465. (list (length (match-string 1))
  6466. (org-reduced-level (length (match-string 1)))
  6467. (org-match-string-no-properties 2)
  6468. (and (match-end 3) (aref (match-string 3) 2))
  6469. (org-match-string-no-properties 4)
  6470. (org-match-string-no-properties 5)))))
  6471. (defun org-get-entry ()
  6472. "Get the entry text, after heading, entire subtree."
  6473. (save-excursion
  6474. (org-back-to-heading t)
  6475. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6476. (defun org-insert-heading-after-current ()
  6477. "Insert a new heading with same level as current, after current subtree."
  6478. (interactive)
  6479. (org-back-to-heading)
  6480. (org-insert-heading)
  6481. (org-move-subtree-down)
  6482. (end-of-line 1))
  6483. (defun org-insert-heading-respect-content ()
  6484. (interactive)
  6485. (let ((org-insert-heading-respect-content t))
  6486. (org-insert-heading t)))
  6487. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6488. (interactive "P")
  6489. (let ((org-insert-heading-respect-content t))
  6490. (org-insert-todo-heading force-state t)))
  6491. (defun org-insert-todo-heading (arg &optional force-heading)
  6492. "Insert a new heading with the same level and TODO state as current heading.
  6493. If the heading has no TODO state, or if the state is DONE, use the first
  6494. state (TODO by default). Also with prefix arg, force first state."
  6495. (interactive "P")
  6496. (when (or force-heading (not (org-insert-item 'checkbox)))
  6497. (org-insert-heading force-heading)
  6498. (save-excursion
  6499. (org-back-to-heading)
  6500. (outline-previous-heading)
  6501. (looking-at org-todo-line-regexp))
  6502. (let*
  6503. ((new-mark-x
  6504. (if (or arg
  6505. (not (match-beginning 2))
  6506. (member (match-string 2) org-done-keywords))
  6507. (car org-todo-keywords-1)
  6508. (match-string 2)))
  6509. (new-mark
  6510. (or
  6511. (run-hook-with-args-until-success
  6512. 'org-todo-get-default-hook new-mark-x nil)
  6513. new-mark-x)))
  6514. (beginning-of-line 1)
  6515. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6516. (if org-treat-insert-todo-heading-as-state-change
  6517. (org-todo new-mark)
  6518. (insert new-mark " "))))
  6519. (when org-provide-todo-statistics
  6520. (org-update-parent-todo-statistics))))
  6521. (defun org-insert-subheading (arg)
  6522. "Insert a new subheading and demote it.
  6523. Works for outline headings and for plain lists alike."
  6524. (interactive "P")
  6525. (org-insert-heading arg)
  6526. (cond
  6527. ((org-at-heading-p) (org-do-demote))
  6528. ((org-at-item-p) (org-indent-item))))
  6529. (defun org-insert-todo-subheading (arg)
  6530. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6531. Works for outline headings and for plain lists alike."
  6532. (interactive "P")
  6533. (org-insert-todo-heading arg)
  6534. (cond
  6535. ((org-at-heading-p) (org-do-demote))
  6536. ((org-at-item-p) (org-indent-item))))
  6537. ;;; Promotion and Demotion
  6538. (defvar org-after-demote-entry-hook nil
  6539. "Hook run after an entry has been demoted.
  6540. The cursor will be at the beginning of the entry.
  6541. When a subtree is being demoted, the hook will be called for each node.")
  6542. (defvar org-after-promote-entry-hook nil
  6543. "Hook run after an entry has been promoted.
  6544. The cursor will be at the beginning of the entry.
  6545. When a subtree is being promoted, the hook will be called for each node.")
  6546. (defun org-promote-subtree ()
  6547. "Promote the entire subtree.
  6548. See also `org-promote'."
  6549. (interactive)
  6550. (save-excursion
  6551. (org-with-limited-levels (org-map-tree 'org-promote)))
  6552. (org-fix-position-after-promote))
  6553. (defun org-demote-subtree ()
  6554. "Demote the entire subtree. See `org-demote'.
  6555. See also `org-promote'."
  6556. (interactive)
  6557. (save-excursion
  6558. (org-with-limited-levels (org-map-tree 'org-demote)))
  6559. (org-fix-position-after-promote))
  6560. (defun org-do-promote ()
  6561. "Promote the current heading higher up the tree.
  6562. If the region is active in `transient-mark-mode', promote all headings
  6563. in the region."
  6564. (interactive)
  6565. (save-excursion
  6566. (if (org-region-active-p)
  6567. (org-map-region 'org-promote (region-beginning) (region-end))
  6568. (org-promote)))
  6569. (org-fix-position-after-promote))
  6570. (defun org-do-demote ()
  6571. "Demote the current heading lower down the tree.
  6572. If the region is active in `transient-mark-mode', demote all headings
  6573. in the region."
  6574. (interactive)
  6575. (save-excursion
  6576. (if (org-region-active-p)
  6577. (org-map-region 'org-demote (region-beginning) (region-end))
  6578. (org-demote)))
  6579. (org-fix-position-after-promote))
  6580. (defun org-fix-position-after-promote ()
  6581. "Make sure that after pro/demotion cursor position is right."
  6582. (let ((pos (point)))
  6583. (when (save-excursion
  6584. (beginning-of-line 1)
  6585. (looking-at org-todo-line-regexp)
  6586. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6587. (cond ((eobp) (insert " "))
  6588. ((eolp) (insert " "))
  6589. ((equal (char-after) ?\ ) (forward-char 1))))))
  6590. (defun org-current-level ()
  6591. "Return the level of the current entry, or nil if before the first headline.
  6592. The level is the number of stars at the beginning of the headline."
  6593. (save-excursion
  6594. (org-with-limited-levels
  6595. (if (ignore-errors (org-back-to-heading t))
  6596. (funcall outline-level)))))
  6597. (defun org-get-previous-line-level ()
  6598. "Return the outline depth of the last headline before the current line.
  6599. Returns 0 for the first headline in the buffer, and nil if before the
  6600. first headline."
  6601. (let ((current-level (org-current-level))
  6602. (prev-level (when (> (line-number-at-pos) 1)
  6603. (save-excursion
  6604. (beginning-of-line 0)
  6605. (org-current-level)))))
  6606. (cond ((null current-level) nil) ; Before first headline
  6607. ((null prev-level) 0) ; At first headline
  6608. (prev-level))))
  6609. (defun org-reduced-level (l)
  6610. "Compute the effective level of a heading.
  6611. This takes into account the setting of `org-odd-levels-only'."
  6612. (cond
  6613. ((zerop l) 0)
  6614. (org-odd-levels-only (1+ (floor (/ l 2))))
  6615. (t l)))
  6616. (defun org-level-increment ()
  6617. "Return the number of stars that will be added or removed at a
  6618. time to headlines when structure editing, based on the value of
  6619. `org-odd-levels-only'."
  6620. (if org-odd-levels-only 2 1))
  6621. (defun org-get-valid-level (level &optional change)
  6622. "Rectify a level change under the influence of `org-odd-levels-only'
  6623. LEVEL is a current level, CHANGE is by how much the level should be
  6624. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6625. even level numbers will become the next higher odd number."
  6626. (if org-odd-levels-only
  6627. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6628. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6629. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6630. (max 1 (+ level (or change 0)))))
  6631. (org-define-obsolete-function-alias 'org-get-legal-level 'org-get-valid-level "23.1")
  6632. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6633. ;; ̀org-with-limited-levels'
  6634. (defun org-promote ()
  6635. "Promote the current heading higher up the tree.
  6636. If the region is active in `transient-mark-mode', promote all headings
  6637. in the region."
  6638. (org-back-to-heading t)
  6639. (let* ((level (save-match-data (funcall outline-level)))
  6640. (after-change-functions (remove 'flyspell-after-change-function
  6641. after-change-functions))
  6642. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6643. (diff (abs (- level (length up-head) -1))))
  6644. (cond ((and (= level 1) org-called-with-limited-levels
  6645. org-allow-promoting-top-level-subtree)
  6646. (replace-match "# " nil t))
  6647. ((= level 1)
  6648. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6649. (t (replace-match up-head nil t)))
  6650. ;; Fixup tag positioning
  6651. (unless (= level 1)
  6652. (and org-auto-align-tags (org-set-tags nil t))
  6653. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6654. (run-hooks 'org-after-promote-entry-hook)))
  6655. (defun org-demote ()
  6656. "Demote the current heading lower down the tree.
  6657. If the region is active in `transient-mark-mode', demote all headings
  6658. in the region."
  6659. (org-back-to-heading t)
  6660. (let* ((level (save-match-data (funcall outline-level)))
  6661. (after-change-functions (remove 'flyspell-after-change-function
  6662. after-change-functions))
  6663. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6664. (diff (abs (- level (length down-head) -1))))
  6665. (replace-match down-head nil t)
  6666. ;; Fixup tag positioning
  6667. (and org-auto-align-tags (org-set-tags nil t))
  6668. (if org-adapt-indentation (org-fixup-indentation diff))
  6669. (run-hooks 'org-after-demote-entry-hook)))
  6670. (defun org-cycle-level ()
  6671. "Cycle the level of an empty headline through possible states.
  6672. This goes first to child, then to parent, level, then up the hierarchy.
  6673. After top level, it switches back to sibling level."
  6674. (interactive)
  6675. (let ((org-adapt-indentation nil))
  6676. (when (org-point-at-end-of-empty-headline)
  6677. (setq this-command 'org-cycle-level) ; Only needed for caching
  6678. (let ((cur-level (org-current-level))
  6679. (prev-level (org-get-previous-line-level)))
  6680. (cond
  6681. ;; If first headline in file, promote to top-level.
  6682. ((= prev-level 0)
  6683. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6684. do (org-do-promote)))
  6685. ;; If same level as prev, demote one.
  6686. ((= prev-level cur-level)
  6687. (org-do-demote))
  6688. ;; If parent is top-level, promote to top level if not already.
  6689. ((= prev-level 1)
  6690. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6691. do (org-do-promote)))
  6692. ;; If top-level, return to prev-level.
  6693. ((= cur-level 1)
  6694. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6695. do (org-do-demote)))
  6696. ;; If less than prev-level, promote one.
  6697. ((< cur-level prev-level)
  6698. (org-do-promote))
  6699. ;; If deeper than prev-level, promote until higher than
  6700. ;; prev-level.
  6701. ((> cur-level prev-level)
  6702. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6703. do (org-do-promote))))
  6704. t))))
  6705. (defun org-map-tree (fun)
  6706. "Call FUN for every heading underneath the current one."
  6707. (org-back-to-heading)
  6708. (let ((level (funcall outline-level)))
  6709. (save-excursion
  6710. (funcall fun)
  6711. (while (and (progn
  6712. (outline-next-heading)
  6713. (> (funcall outline-level) level))
  6714. (not (eobp)))
  6715. (funcall fun)))))
  6716. (defun org-map-region (fun beg end)
  6717. "Call FUN for every heading between BEG and END."
  6718. (let ((org-ignore-region t))
  6719. (save-excursion
  6720. (setq end (copy-marker end))
  6721. (goto-char beg)
  6722. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6723. (< (point) end))
  6724. (funcall fun))
  6725. (while (and (progn
  6726. (outline-next-heading)
  6727. (< (point) end))
  6728. (not (eobp)))
  6729. (funcall fun)))))
  6730. (defvar org-property-end-re) ; silence byte-compiler
  6731. (defun org-fixup-indentation (diff)
  6732. "Change the indentation in the current entry by DIFF.
  6733. However, if any line in the current entry has no indentation, or if it
  6734. would end up with no indentation after the change, nothing at all is done."
  6735. (save-excursion
  6736. (let ((end (save-excursion (outline-next-heading)
  6737. (point-marker)))
  6738. (prohibit (if (> diff 0)
  6739. "^\\S-"
  6740. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6741. col)
  6742. (unless (save-excursion (end-of-line 1)
  6743. (re-search-forward prohibit end t))
  6744. (while (and (< (point) end)
  6745. (re-search-forward "^[ \t]+" end t))
  6746. (goto-char (match-end 0))
  6747. (setq col (current-column))
  6748. (if (< diff 0) (replace-match ""))
  6749. (org-indent-to-column (+ diff col))))
  6750. (move-marker end nil))))
  6751. (defun org-convert-to-odd-levels ()
  6752. "Convert an org-mode file with all levels allowed to one with odd levels.
  6753. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6754. level 5 etc."
  6755. (interactive)
  6756. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6757. (let ((outline-level 'org-outline-level)
  6758. (org-odd-levels-only nil) n)
  6759. (save-excursion
  6760. (goto-char (point-min))
  6761. (while (re-search-forward "^\\*\\*+ " nil t)
  6762. (setq n (- (length (match-string 0)) 2))
  6763. (while (>= (setq n (1- n)) 0)
  6764. (org-demote))
  6765. (end-of-line 1))))))
  6766. (defun org-convert-to-oddeven-levels ()
  6767. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6768. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6769. file contains a section with an even level, conversion would
  6770. destroy the structure of the file. An error is signaled in this
  6771. case."
  6772. (interactive)
  6773. (goto-char (point-min))
  6774. ;; First check if there are no even levels
  6775. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6776. (org-show-context t)
  6777. (error "Not all levels are odd in this file. Conversion not possible"))
  6778. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6779. (let ((outline-regexp org-outline-regexp)
  6780. (outline-level 'org-outline-level)
  6781. (org-odd-levels-only nil) n)
  6782. (save-excursion
  6783. (goto-char (point-min))
  6784. (while (re-search-forward "^\\*\\*+ " nil t)
  6785. (setq n (/ (1- (length (match-string 0))) 2))
  6786. (while (>= (setq n (1- n)) 0)
  6787. (org-promote))
  6788. (end-of-line 1))))))
  6789. (defun org-tr-level (n)
  6790. "Make N odd if required."
  6791. (if org-odd-levels-only (1+ (/ n 2)) n))
  6792. ;;; Vertical tree motion, cutting and pasting of subtrees
  6793. (defun org-move-subtree-up (&optional arg)
  6794. "Move the current subtree up past ARG headlines of the same level."
  6795. (interactive "p")
  6796. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6797. (defun org-move-subtree-down (&optional arg)
  6798. "Move the current subtree down past ARG headlines of the same level."
  6799. (interactive "p")
  6800. (setq arg (prefix-numeric-value arg))
  6801. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6802. 'org-get-last-sibling))
  6803. (ins-point (make-marker))
  6804. (cnt (abs arg))
  6805. (col (current-column))
  6806. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6807. ;; Select the tree
  6808. (org-back-to-heading)
  6809. (setq beg0 (point))
  6810. (save-excursion
  6811. (setq ne-beg (org-back-over-empty-lines))
  6812. (setq beg (point)))
  6813. (save-match-data
  6814. (save-excursion (outline-end-of-heading)
  6815. (setq folded (outline-invisible-p)))
  6816. (outline-end-of-subtree))
  6817. (outline-next-heading)
  6818. (setq ne-end (org-back-over-empty-lines))
  6819. (setq end (point))
  6820. (goto-char beg0)
  6821. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6822. ;; include less whitespace
  6823. (save-excursion
  6824. (goto-char beg)
  6825. (forward-line (- ne-beg ne-end))
  6826. (setq beg (point))))
  6827. ;; Find insertion point, with error handling
  6828. (while (> cnt 0)
  6829. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6830. (progn (goto-char beg0)
  6831. (error "Cannot move past superior level or buffer limit")))
  6832. (setq cnt (1- cnt)))
  6833. (if (> arg 0)
  6834. ;; Moving forward - still need to move over subtree
  6835. (progn (org-end-of-subtree t t)
  6836. (save-excursion
  6837. (org-back-over-empty-lines)
  6838. (or (bolp) (newline)))))
  6839. (setq ne-ins (org-back-over-empty-lines))
  6840. (move-marker ins-point (point))
  6841. (setq txt (buffer-substring beg end))
  6842. (org-save-markers-in-region beg end)
  6843. (delete-region beg end)
  6844. (org-remove-empty-overlays-at beg)
  6845. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6846. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6847. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6848. (let ((bbb (point)))
  6849. (insert-before-markers txt)
  6850. (org-reinstall-markers-in-region bbb)
  6851. (move-marker ins-point bbb))
  6852. (or (bolp) (insert "\n"))
  6853. (setq ins-end (point))
  6854. (goto-char ins-point)
  6855. (org-skip-whitespace)
  6856. (when (and (< arg 0)
  6857. (org-first-sibling-p)
  6858. (> ne-ins ne-beg))
  6859. ;; Move whitespace back to beginning
  6860. (save-excursion
  6861. (goto-char ins-end)
  6862. (let ((kill-whole-line t))
  6863. (kill-line (- ne-ins ne-beg)) (point)))
  6864. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6865. (move-marker ins-point nil)
  6866. (if folded
  6867. (hide-subtree)
  6868. (org-show-entry)
  6869. (show-children)
  6870. (org-cycle-hide-drawers 'children))
  6871. (org-clean-visibility-after-subtree-move)
  6872. ;; move back to the initial column we were at
  6873. (move-to-column col)))
  6874. (defvar org-subtree-clip ""
  6875. "Clipboard for cut and paste of subtrees.
  6876. This is actually only a copy of the kill, because we use the normal kill
  6877. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6878. (defvar org-subtree-clip-folded nil
  6879. "Was the last copied subtree folded?
  6880. This is used to fold the tree back after pasting.")
  6881. (defun org-cut-subtree (&optional n)
  6882. "Cut the current subtree into the clipboard.
  6883. With prefix arg N, cut this many sequential subtrees.
  6884. This is a short-hand for marking the subtree and then cutting it."
  6885. (interactive "p")
  6886. (org-copy-subtree n 'cut))
  6887. (defun org-copy-subtree (&optional n cut force-store-markers)
  6888. "Cut the current subtree into the clipboard.
  6889. With prefix arg N, cut this many sequential subtrees.
  6890. This is a short-hand for marking the subtree and then copying it.
  6891. If CUT is non-nil, actually cut the subtree.
  6892. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6893. of some markers in the region, even if CUT is non-nil. This is
  6894. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6895. (interactive "p")
  6896. (let (beg end folded (beg0 (point)))
  6897. (if (org-called-interactively-p 'any)
  6898. (org-back-to-heading nil) ; take what looks like a subtree
  6899. (org-back-to-heading t)) ; take what is really there
  6900. (setq beg (point))
  6901. (skip-chars-forward " \t\r\n")
  6902. (save-match-data
  6903. (save-excursion (outline-end-of-heading)
  6904. (setq folded (outline-invisible-p)))
  6905. (condition-case nil
  6906. (org-forward-heading-same-level (1- n) t)
  6907. (error nil))
  6908. (org-end-of-subtree t t))
  6909. (setq end (point))
  6910. (goto-char beg0)
  6911. (when (> end beg)
  6912. (setq org-subtree-clip-folded folded)
  6913. (when (or cut force-store-markers)
  6914. (org-save-markers-in-region beg end))
  6915. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6916. (setq org-subtree-clip (current-kill 0))
  6917. (message "%s: Subtree(s) with %d characters"
  6918. (if cut "Cut" "Copied")
  6919. (length org-subtree-clip)))))
  6920. (defun org-paste-subtree (&optional level tree for-yank)
  6921. "Paste the clipboard as a subtree, with modification of headline level.
  6922. The entire subtree is promoted or demoted in order to match a new headline
  6923. level.
  6924. If the cursor is at the beginning of a headline, the same level as
  6925. that headline is used to paste the tree
  6926. If not, the new level is derived from the *visible* headings
  6927. before and after the insertion point, and taken to be the inferior headline
  6928. level of the two. So if the previous visible heading is level 3 and the
  6929. next is level 4 (or vice versa), level 4 will be used for insertion.
  6930. This makes sure that the subtree remains an independent subtree and does
  6931. not swallow low level entries.
  6932. You can also force a different level, either by using a numeric prefix
  6933. argument, or by inserting the heading marker by hand. For example, if the
  6934. cursor is after \"*****\", then the tree will be shifted to level 5.
  6935. If optional TREE is given, use this text instead of the kill ring.
  6936. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6937. move back over whitespace before inserting, and move point to the end of
  6938. the inserted text when done."
  6939. (interactive "P")
  6940. (setq tree (or tree (and kill-ring (current-kill 0))))
  6941. (unless (org-kill-is-subtree-p tree)
  6942. (error "%s"
  6943. (substitute-command-keys
  6944. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6945. (org-with-limited-levels
  6946. (let* ((visp (not (outline-invisible-p)))
  6947. (txt tree)
  6948. (^re_ "\\(\\*+\\)[ \t]*")
  6949. (old-level (if (string-match org-outline-regexp-bol txt)
  6950. (- (match-end 0) (match-beginning 0) 1)
  6951. -1))
  6952. (force-level (cond (level (prefix-numeric-value level))
  6953. ((and (looking-at "[ \t]*$")
  6954. (string-match
  6955. "^\\*+$" (buffer-substring
  6956. (point-at-bol) (point))))
  6957. (- (match-end 1) (match-beginning 1)))
  6958. ((and (bolp)
  6959. (looking-at org-outline-regexp))
  6960. (- (match-end 0) (point) 1))))
  6961. (previous-level (save-excursion
  6962. (condition-case nil
  6963. (progn
  6964. (outline-previous-visible-heading 1)
  6965. (if (looking-at ^re_)
  6966. (- (match-end 0) (match-beginning 0) 1)
  6967. 1))
  6968. (error 1))))
  6969. (next-level (save-excursion
  6970. (condition-case nil
  6971. (progn
  6972. (or (looking-at org-outline-regexp)
  6973. (outline-next-visible-heading 1))
  6974. (if (looking-at ^re_)
  6975. (- (match-end 0) (match-beginning 0) 1)
  6976. 1))
  6977. (error 1))))
  6978. (new-level (or force-level (max previous-level next-level)))
  6979. (shift (if (or (= old-level -1)
  6980. (= new-level -1)
  6981. (= old-level new-level))
  6982. 0
  6983. (- new-level old-level)))
  6984. (delta (if (> shift 0) -1 1))
  6985. (func (if (> shift 0) 'org-demote 'org-promote))
  6986. (org-odd-levels-only nil)
  6987. beg end newend)
  6988. ;; Remove the forced level indicator
  6989. (if force-level
  6990. (delete-region (point-at-bol) (point)))
  6991. ;; Paste
  6992. (beginning-of-line (if (bolp) 1 2))
  6993. (setq beg (point))
  6994. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6995. (insert-before-markers txt)
  6996. (unless (string-match "\n\\'" txt) (insert "\n"))
  6997. (setq newend (point))
  6998. (org-reinstall-markers-in-region beg)
  6999. (setq end (point))
  7000. (goto-char beg)
  7001. (skip-chars-forward " \t\n\r")
  7002. (setq beg (point))
  7003. (if (and (outline-invisible-p) visp)
  7004. (save-excursion (outline-show-heading)))
  7005. ;; Shift if necessary
  7006. (unless (= shift 0)
  7007. (save-restriction
  7008. (narrow-to-region beg end)
  7009. (while (not (= shift 0))
  7010. (org-map-region func (point-min) (point-max))
  7011. (setq shift (+ delta shift)))
  7012. (goto-char (point-min))
  7013. (setq newend (point-max))))
  7014. (when (or (org-called-interactively-p 'interactive) for-yank)
  7015. (message "Clipboard pasted as level %d subtree" new-level))
  7016. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7017. kill-ring
  7018. (eq org-subtree-clip (current-kill 0))
  7019. org-subtree-clip-folded)
  7020. ;; The tree was folded before it was killed/copied
  7021. (hide-subtree))
  7022. (and for-yank (goto-char newend)))))
  7023. (defun org-kill-is-subtree-p (&optional txt)
  7024. "Check if the current kill is an outline subtree, or a set of trees.
  7025. Returns nil if kill does not start with a headline, or if the first
  7026. headline level is not the largest headline level in the tree.
  7027. So this will actually accept several entries of equal levels as well,
  7028. which is OK for `org-paste-subtree'.
  7029. If optional TXT is given, check this string instead of the current kill."
  7030. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7031. (re (org-get-limited-outline-regexp))
  7032. (^re (concat "^" re))
  7033. (start-level (and kill
  7034. (string-match
  7035. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7036. kill)
  7037. (- (match-end 2) (match-beginning 2) 1)))
  7038. (start (1+ (or (match-beginning 2) -1))))
  7039. (if (not start-level)
  7040. (progn
  7041. nil) ;; does not even start with a heading
  7042. (catch 'exit
  7043. (while (setq start (string-match ^re kill (1+ start)))
  7044. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7045. (throw 'exit nil)))
  7046. t))))
  7047. (defvar org-markers-to-move nil
  7048. "Markers that should be moved with a cut-and-paste operation.
  7049. Those markers are stored together with their positions relative to
  7050. the start of the region.")
  7051. (defun org-save-markers-in-region (beg end)
  7052. "Check markers in region.
  7053. If these markers are between BEG and END, record their position relative
  7054. to BEG, so that after moving the block of text, we can put the markers back
  7055. into place.
  7056. This function gets called just before an entry or tree gets cut from the
  7057. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7058. called immediately, to move the markers with the entries."
  7059. (setq org-markers-to-move nil)
  7060. (when (featurep 'org-clock)
  7061. (org-clock-save-markers-for-cut-and-paste beg end))
  7062. (when (featurep 'org-agenda)
  7063. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7064. (defun org-check-and-save-marker (marker beg end)
  7065. "Check if MARKER is between BEG and END.
  7066. If yes, remember the marker and the distance to BEG."
  7067. (when (and (marker-buffer marker)
  7068. (equal (marker-buffer marker) (current-buffer)))
  7069. (if (and (>= marker beg) (< marker end))
  7070. (push (cons marker (- marker beg)) org-markers-to-move))))
  7071. (defun org-reinstall-markers-in-region (beg)
  7072. "Move all remembered markers to their position relative to BEG."
  7073. (mapc (lambda (x)
  7074. (move-marker (car x) (+ beg (cdr x))))
  7075. org-markers-to-move)
  7076. (setq org-markers-to-move nil))
  7077. (defun org-narrow-to-subtree ()
  7078. "Narrow buffer to the current subtree."
  7079. (interactive)
  7080. (save-excursion
  7081. (save-match-data
  7082. (org-with-limited-levels
  7083. (narrow-to-region
  7084. (progn (org-back-to-heading t) (point))
  7085. (progn (org-end-of-subtree t t)
  7086. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7087. (point)))))))
  7088. (defun org-narrow-to-block ()
  7089. "Narrow buffer to the current block."
  7090. (interactive)
  7091. (let* ((case-fold-search t)
  7092. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7093. "^[ \t]*#\\+end_.*")))
  7094. (if blockp
  7095. (narrow-to-region (car blockp) (cdr blockp))
  7096. (error "Not in a block"))))
  7097. (eval-when-compile
  7098. (defvar org-property-drawer-re))
  7099. (defvar org-property-start-re) ;; defined below
  7100. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7101. "Clone the task (subtree) at point N times.
  7102. The clones will be inserted as siblings.
  7103. In interactive use, the user will be prompted for the number of
  7104. clones to be produced, and for a time SHIFT, which may be a
  7105. repeater as used in time stamps, for example `+3d'.
  7106. When a valid repeater is given and the entry contains any time
  7107. stamps, the clones will become a sequence in time, with time
  7108. stamps in the subtree shifted for each clone produced. If SHIFT
  7109. is nil or the empty string, time stamps will be left alone. The
  7110. ID property of the original subtree is removed.
  7111. If the original subtree did contain time stamps with a repeater,
  7112. the following will happen:
  7113. - the repeater will be removed in each clone
  7114. - an additional clone will be produced, with the current, unshifted
  7115. date(s) in the entry.
  7116. - the original entry will be placed *after* all the clones, with
  7117. repeater intact.
  7118. - the start days in the repeater in the original entry will be shifted
  7119. to past the last clone.
  7120. In this way you can spell out a number of instances of a repeating task,
  7121. and still retain the repeater to cover future instances of the task."
  7122. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7123. (let (beg end template task idprop
  7124. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7125. (drawer-re org-drawer-regexp))
  7126. (if (not (and (integerp n) (> n 0)))
  7127. (error "Invalid number of replications %s" n))
  7128. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7129. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7130. shift)))
  7131. (error "Invalid shift specification %s" shift))
  7132. (when doshift
  7133. (setq shift-n (string-to-number (match-string 1 shift))
  7134. shift-what (cdr (assoc (match-string 2 shift)
  7135. '(("d" . day) ("w" . week)
  7136. ("m" . month) ("y" . year))))))
  7137. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7138. (setq nmin 1 nmax n)
  7139. (org-back-to-heading t)
  7140. (setq beg (point))
  7141. (setq idprop (org-entry-get nil "ID"))
  7142. (org-end-of-subtree t t)
  7143. (or (bolp) (insert "\n"))
  7144. (setq end (point))
  7145. (setq template (buffer-substring beg end))
  7146. (when (and doshift
  7147. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7148. (delete-region beg end)
  7149. (setq end beg)
  7150. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7151. (goto-char end)
  7152. (loop for n from nmin to nmax do
  7153. ;; prepare clone
  7154. (with-temp-buffer
  7155. (insert template)
  7156. (org-mode)
  7157. (goto-char (point-min))
  7158. (org-show-subtree)
  7159. (and idprop (if org-clone-delete-id
  7160. (org-entry-delete nil "ID")
  7161. (org-id-get-create t)))
  7162. (unless (= n 0)
  7163. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7164. (kill-whole-line))
  7165. (goto-char (point-min))
  7166. (while (re-search-forward drawer-re nil t)
  7167. (mapc (lambda (d)
  7168. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7169. (goto-char (point-min))
  7170. (when doshift
  7171. (while (re-search-forward org-ts-regexp-both nil t)
  7172. (org-timestamp-change (* n shift-n) shift-what))
  7173. (unless (= n n-no-remove)
  7174. (goto-char (point-min))
  7175. (while (re-search-forward org-ts-regexp nil t)
  7176. (save-excursion
  7177. (goto-char (match-beginning 0))
  7178. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7179. (delete-region (match-beginning 1) (match-end 1)))))))
  7180. (setq task (buffer-string)))
  7181. (insert task))
  7182. (goto-char beg)))
  7183. ;;; Outline Sorting
  7184. (defun org-sort (with-case)
  7185. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7186. Optional argument WITH-CASE means sort case-sensitively."
  7187. (interactive "P")
  7188. (cond
  7189. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7190. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7191. (t
  7192. (org-call-with-arg 'org-sort-entries with-case))))
  7193. (defun org-sort-remove-invisible (s)
  7194. (remove-text-properties 0 (length s) org-rm-props s)
  7195. (while (string-match org-bracket-link-regexp s)
  7196. (setq s (replace-match (if (match-end 2)
  7197. (match-string 3 s)
  7198. (match-string 1 s)) t t s)))
  7199. s)
  7200. (defvar org-priority-regexp) ; defined later in the file
  7201. (defvar org-after-sorting-entries-or-items-hook nil
  7202. "Hook that is run after a bunch of entries or items have been sorted.
  7203. When children are sorted, the cursor is in the parent line when this
  7204. hook gets called. When a region or a plain list is sorted, the cursor
  7205. will be in the first entry of the sorted region/list.")
  7206. (defun org-sort-entries
  7207. (&optional with-case sorting-type getkey-func compare-func property)
  7208. "Sort entries on a certain level of an outline tree.
  7209. If there is an active region, the entries in the region are sorted.
  7210. Else, if the cursor is before the first entry, sort the top-level items.
  7211. Else, the children of the entry at point are sorted.
  7212. Sorting can be alphabetically, numerically, by date/time as given by
  7213. a time stamp, by a property or by priority.
  7214. The command prompts for the sorting type unless it has been given to the
  7215. function through the SORTING-TYPE argument, which needs to be a character,
  7216. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7217. precise meaning of each character:
  7218. n Numerically, by converting the beginning of the entry/item to a number.
  7219. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7220. o By order of TODO keywords.
  7221. t By date/time, either the first active time stamp in the entry, or, if
  7222. none exist, by the first inactive one.
  7223. s By the scheduled date/time.
  7224. d By deadline date/time.
  7225. c By creation time, which is assumed to be the first inactive time stamp
  7226. at the beginning of a line.
  7227. p By priority according to the cookie.
  7228. r By the value of a property.
  7229. Capital letters will reverse the sort order.
  7230. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7231. called with point at the beginning of the record. It must return either
  7232. a string or a number that should serve as the sorting key for that record.
  7233. Comparing entries ignores case by default. However, with an optional argument
  7234. WITH-CASE, the sorting considers case as well."
  7235. (interactive "P")
  7236. (let ((case-func (if with-case 'identity 'downcase))
  7237. (cmstr
  7238. ;; The clock marker is lost when using `sort-subr', let's
  7239. ;; store the clocking string.
  7240. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7241. (save-excursion
  7242. (goto-char org-clock-marker)
  7243. (looking-back "^.*") (match-string-no-properties 0))))
  7244. start beg end stars re re2
  7245. txt what tmp)
  7246. ;; Find beginning and end of region to sort
  7247. (cond
  7248. ((org-region-active-p)
  7249. ;; we will sort the region
  7250. (setq end (region-end)
  7251. what "region")
  7252. (goto-char (region-beginning))
  7253. (if (not (org-at-heading-p)) (outline-next-heading))
  7254. (setq start (point)))
  7255. ((or (org-at-heading-p)
  7256. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7257. ;; we will sort the children of the current headline
  7258. (org-back-to-heading)
  7259. (setq start (point)
  7260. end (progn (org-end-of-subtree t t)
  7261. (or (bolp) (insert "\n"))
  7262. (org-back-over-empty-lines)
  7263. (point))
  7264. what "children")
  7265. (goto-char start)
  7266. (show-subtree)
  7267. (outline-next-heading))
  7268. (t
  7269. ;; we will sort the top-level entries in this file
  7270. (goto-char (point-min))
  7271. (or (org-at-heading-p) (outline-next-heading))
  7272. (setq start (point))
  7273. (goto-char (point-max))
  7274. (beginning-of-line 1)
  7275. (when (looking-at ".*?\\S-")
  7276. ;; File ends in a non-white line
  7277. (end-of-line 1)
  7278. (insert "\n"))
  7279. (setq end (point-max))
  7280. (setq what "top-level")
  7281. (goto-char start)
  7282. (show-all)))
  7283. (setq beg (point))
  7284. (if (>= beg end) (error "Nothing to sort"))
  7285. (looking-at "\\(\\*+\\)")
  7286. (setq stars (match-string 1)
  7287. re (concat "^" (regexp-quote stars) " +")
  7288. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7289. txt (buffer-substring beg end))
  7290. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7291. (if (and (not (equal stars "*")) (string-match re2 txt))
  7292. (error "Region to sort contains a level above the first entry"))
  7293. (unless sorting-type
  7294. (message
  7295. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7296. [t]ime [s]cheduled [d]eadline [c]reated
  7297. A/N/P/R/O/F/T/S/D/C means reversed:"
  7298. what)
  7299. (setq sorting-type (read-char-exclusive))
  7300. (and (= (downcase sorting-type) ?f)
  7301. (setq getkey-func
  7302. (org-icompleting-read "Sort using function: "
  7303. obarray 'fboundp t nil nil))
  7304. (setq getkey-func (intern getkey-func)))
  7305. (and (= (downcase sorting-type) ?r)
  7306. (setq property
  7307. (org-icompleting-read "Property: "
  7308. (mapcar 'list (org-buffer-property-keys t))
  7309. nil t))))
  7310. (message "Sorting entries...")
  7311. (save-restriction
  7312. (narrow-to-region start end)
  7313. (let ((dcst (downcase sorting-type))
  7314. (case-fold-search nil)
  7315. (now (current-time)))
  7316. (sort-subr
  7317. (/= dcst sorting-type)
  7318. ;; This function moves to the beginning character of the "record" to
  7319. ;; be sorted.
  7320. (lambda nil
  7321. (if (re-search-forward re nil t)
  7322. (goto-char (match-beginning 0))
  7323. (goto-char (point-max))))
  7324. ;; This function moves to the last character of the "record" being
  7325. ;; sorted.
  7326. (lambda nil
  7327. (save-match-data
  7328. (condition-case nil
  7329. (outline-forward-same-level 1)
  7330. (error
  7331. (goto-char (point-max))))))
  7332. ;; This function returns the value that gets sorted against.
  7333. (lambda nil
  7334. (cond
  7335. ((= dcst ?n)
  7336. (if (looking-at org-complex-heading-regexp)
  7337. (string-to-number (match-string 4))
  7338. nil))
  7339. ((= dcst ?a)
  7340. (if (looking-at org-complex-heading-regexp)
  7341. (funcall case-func (match-string 4))
  7342. nil))
  7343. ((= dcst ?t)
  7344. (let ((end (save-excursion (outline-next-heading) (point))))
  7345. (if (or (re-search-forward org-ts-regexp end t)
  7346. (re-search-forward org-ts-regexp-both end t))
  7347. (org-time-string-to-seconds (match-string 0))
  7348. (org-float-time now))))
  7349. ((= dcst ?c)
  7350. (let ((end (save-excursion (outline-next-heading) (point))))
  7351. (if (re-search-forward
  7352. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7353. end t)
  7354. (org-time-string-to-seconds (match-string 0))
  7355. (org-float-time now))))
  7356. ((= dcst ?s)
  7357. (let ((end (save-excursion (outline-next-heading) (point))))
  7358. (if (re-search-forward org-scheduled-time-regexp end t)
  7359. (org-time-string-to-seconds (match-string 1))
  7360. (org-float-time now))))
  7361. ((= dcst ?d)
  7362. (let ((end (save-excursion (outline-next-heading) (point))))
  7363. (if (re-search-forward org-deadline-time-regexp end t)
  7364. (org-time-string-to-seconds (match-string 1))
  7365. (org-float-time now))))
  7366. ((= dcst ?p)
  7367. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7368. (string-to-char (match-string 2))
  7369. org-default-priority))
  7370. ((= dcst ?r)
  7371. (or (org-entry-get nil property) ""))
  7372. ((= dcst ?o)
  7373. (if (looking-at org-complex-heading-regexp)
  7374. (- 9999 (length (member (match-string 2)
  7375. org-todo-keywords-1)))))
  7376. ((= dcst ?f)
  7377. (if getkey-func
  7378. (progn
  7379. (setq tmp (funcall getkey-func))
  7380. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7381. tmp)
  7382. (error "Invalid key function `%s'" getkey-func)))
  7383. (t (error "Invalid sorting type `%c'" sorting-type))))
  7384. nil
  7385. (cond
  7386. ((= dcst ?a) 'string<)
  7387. ((= dcst ?f) compare-func)
  7388. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7389. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7390. ;; Reset the clock marker if needed
  7391. (when cmstr
  7392. (save-excursion
  7393. (goto-char start)
  7394. (search-forward cmstr nil t)
  7395. (move-marker org-clock-marker (point))))
  7396. (message "Sorting entries...done")))
  7397. (defun org-do-sort (table what &optional with-case sorting-type)
  7398. "Sort TABLE of WHAT according to SORTING-TYPE.
  7399. The user will be prompted for the SORTING-TYPE if the call to this
  7400. function does not specify it. WHAT is only for the prompt, to indicate
  7401. what is being sorted. The sorting key will be extracted from
  7402. the car of the elements of the table.
  7403. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7404. (unless sorting-type
  7405. (message
  7406. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7407. what)
  7408. (setq sorting-type (read-char-exclusive)))
  7409. (let ((dcst (downcase sorting-type))
  7410. extractfun comparefun)
  7411. ;; Define the appropriate functions
  7412. (cond
  7413. ((= dcst ?n)
  7414. (setq extractfun 'string-to-number
  7415. comparefun (if (= dcst sorting-type) '< '>)))
  7416. ((= dcst ?a)
  7417. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7418. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7419. comparefun (if (= dcst sorting-type)
  7420. 'string<
  7421. (lambda (a b) (and (not (string< a b))
  7422. (not (string= a b)))))))
  7423. ((= dcst ?t)
  7424. (setq extractfun
  7425. (lambda (x)
  7426. (if (or (string-match org-ts-regexp x)
  7427. (string-match org-ts-regexp-both x))
  7428. (org-float-time
  7429. (org-time-string-to-time (match-string 0 x)))
  7430. 0))
  7431. comparefun (if (= dcst sorting-type) '< '>)))
  7432. (t (error "Invalid sorting type `%c'" sorting-type)))
  7433. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7434. table)
  7435. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7436. ;;; The orgstruct minor mode
  7437. ;; Define a minor mode which can be used in other modes in order to
  7438. ;; integrate the org-mode structure editing commands.
  7439. ;; This is really a hack, because the org-mode structure commands use
  7440. ;; keys which normally belong to the major mode. Here is how it
  7441. ;; works: The minor mode defines all the keys necessary to operate the
  7442. ;; structure commands, but wraps the commands into a function which
  7443. ;; tests if the cursor is currently at a headline or a plain list
  7444. ;; item. If that is the case, the structure command is used,
  7445. ;; temporarily setting many Org-mode variables like regular
  7446. ;; expressions for filling etc. However, when any of those keys is
  7447. ;; used at a different location, function uses `key-binding' to look
  7448. ;; up if the key has an associated command in another currently active
  7449. ;; keymap (minor modes, major mode, global), and executes that
  7450. ;; command. There might be problems if any of the keys is otherwise
  7451. ;; used as a prefix key.
  7452. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7453. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7454. ;; addresses this by checking explicitly for both bindings.
  7455. (defvar orgstruct-mode-map (make-sparse-keymap)
  7456. "Keymap for the minor `orgstruct-mode'.")
  7457. (defvar org-local-vars nil
  7458. "List of local variables, for use by `orgstruct-mode'.")
  7459. ;;;###autoload
  7460. (define-minor-mode orgstruct-mode
  7461. "Toggle the minor mode `orgstruct-mode'.
  7462. This mode is for using Org-mode structure commands in other
  7463. modes. The following keys behave as if Org-mode were active, if
  7464. the cursor is on a headline, or on a plain list item (both as
  7465. defined by Org-mode).
  7466. M-up Move entry/item up
  7467. M-down Move entry/item down
  7468. M-left Promote
  7469. M-right Demote
  7470. M-S-up Move entry/item up
  7471. M-S-down Move entry/item down
  7472. M-S-left Promote subtree
  7473. M-S-right Demote subtree
  7474. M-q Fill paragraph and items like in Org-mode
  7475. C-c ^ Sort entries
  7476. C-c - Cycle list bullet
  7477. TAB Cycle item visibility
  7478. M-RET Insert new heading/item
  7479. S-M-RET Insert new TODO heading / Checkbox item
  7480. C-c C-c Set tags / toggle checkbox"
  7481. nil " OrgStruct" nil
  7482. (org-load-modules-maybe)
  7483. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7484. ;;;###autoload
  7485. (defun turn-on-orgstruct ()
  7486. "Unconditionally turn on `orgstruct-mode'."
  7487. (orgstruct-mode 1))
  7488. (defvar org-fb-vars nil)
  7489. (make-variable-buffer-local 'org-fb-vars)
  7490. (defun orgstruct++-mode (&optional arg)
  7491. "Toggle `orgstruct-mode', the enhanced version of it.
  7492. In addition to setting orgstruct-mode, this also exports all
  7493. indentation and autofilling variables from org-mode into the
  7494. buffer. It will also recognize item context in multiline items."
  7495. (interactive "P")
  7496. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7497. (if (< arg 1)
  7498. (progn (orgstruct-mode -1)
  7499. (mapc (lambda(v)
  7500. (org-set-local (car v)
  7501. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7502. org-fb-vars))
  7503. (orgstruct-mode 1)
  7504. (setq org-fb-vars nil)
  7505. (let (var val)
  7506. (mapc
  7507. (lambda (x)
  7508. (when (string-match
  7509. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7510. (symbol-name (car x)))
  7511. (setq var (car x) val (nth 1 x))
  7512. (push (list var `(quote ,(eval var))) org-fb-vars)
  7513. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7514. org-local-vars)
  7515. (org-set-local 'orgstruct-is-++ t))))
  7516. (defvar orgstruct-is-++ nil
  7517. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7518. (make-variable-buffer-local 'orgstruct-is-++)
  7519. ;;;###autoload
  7520. (defun turn-on-orgstruct++ ()
  7521. "Unconditionally turn on `orgstruct++-mode'."
  7522. (orgstruct++-mode 1))
  7523. (defun orgstruct-error ()
  7524. "Error when there is no default binding for a structure key."
  7525. (interactive)
  7526. (error "This key has no function outside structure elements"))
  7527. (defun orgstruct-setup ()
  7528. "Setup orgstruct keymaps."
  7529. (let ((nfunc 0)
  7530. (bindings
  7531. (list
  7532. '([(meta up)] org-metaup)
  7533. '([(meta down)] org-metadown)
  7534. '([(meta left)] org-metaleft)
  7535. '([(meta right)] org-metaright)
  7536. '([(meta shift up)] org-shiftmetaup)
  7537. '([(meta shift down)] org-shiftmetadown)
  7538. '([(meta shift left)] org-shiftmetaleft)
  7539. '([(meta shift right)] org-shiftmetaright)
  7540. '([?\e (up)] org-metaup)
  7541. '([?\e (down)] org-metadown)
  7542. '([?\e (left)] org-metaleft)
  7543. '([?\e (right)] org-metaright)
  7544. '([?\e (shift up)] org-shiftmetaup)
  7545. '([?\e (shift down)] org-shiftmetadown)
  7546. '([?\e (shift left)] org-shiftmetaleft)
  7547. '([?\e (shift right)] org-shiftmetaright)
  7548. '([(shift up)] org-shiftup)
  7549. '([(shift down)] org-shiftdown)
  7550. '([(shift left)] org-shiftleft)
  7551. '([(shift right)] org-shiftright)
  7552. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7553. '("\M-q" fill-paragraph)
  7554. '("\C-c^" org-sort)
  7555. '("\C-c-" org-cycle-list-bullet)))
  7556. elt key fun cmd)
  7557. (while (setq elt (pop bindings))
  7558. (setq nfunc (1+ nfunc))
  7559. (setq key (org-key (car elt))
  7560. fun (nth 1 elt)
  7561. cmd (orgstruct-make-binding fun nfunc key))
  7562. (org-defkey orgstruct-mode-map key cmd))
  7563. ;; Prevent an error for users who forgot to make autoloads
  7564. (require 'org-element)
  7565. ;; Special treatment needed for TAB and RET
  7566. (org-defkey orgstruct-mode-map [(tab)]
  7567. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7568. (org-defkey orgstruct-mode-map "\C-i"
  7569. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7570. (org-defkey orgstruct-mode-map "\M-\C-m"
  7571. (orgstruct-make-binding 'org-insert-heading 105
  7572. "\M-\C-m" [(meta return)]))
  7573. (org-defkey orgstruct-mode-map [(meta return)]
  7574. (orgstruct-make-binding 'org-insert-heading 106
  7575. [(meta return)] "\M-\C-m"))
  7576. (org-defkey orgstruct-mode-map [(shift meta return)]
  7577. (orgstruct-make-binding 'org-insert-todo-heading 107
  7578. [(meta return)] "\M-\C-m"))
  7579. (org-defkey orgstruct-mode-map "\e\C-m"
  7580. (orgstruct-make-binding 'org-insert-heading 108
  7581. "\e\C-m" [?\e (return)]))
  7582. (org-defkey orgstruct-mode-map [?\e (return)]
  7583. (orgstruct-make-binding 'org-insert-heading 109
  7584. [?\e (return)] "\e\C-m"))
  7585. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7586. (orgstruct-make-binding 'org-insert-todo-heading 110
  7587. [?\e (return)] "\e\C-m"))
  7588. (unless org-local-vars
  7589. (setq org-local-vars (org-get-local-variables)))
  7590. t))
  7591. (defun orgstruct-make-binding (fun n &rest keys)
  7592. "Create a function for binding in the structure minor mode.
  7593. FUN is the command to call inside a table. N is used to create a unique
  7594. command name. KEYS are keys that should be checked in for a command
  7595. to execute outside of tables."
  7596. (eval
  7597. (list 'defun
  7598. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7599. '(arg)
  7600. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7601. "Outside of structure, run the binding of `"
  7602. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7603. "'.")
  7604. '(interactive "p")
  7605. (list 'if
  7606. `(org-context-p 'headline 'item
  7607. (and orgstruct-is-++
  7608. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7609. 'item-body))
  7610. (list 'org-run-like-in-org-mode (list 'quote fun))
  7611. (list 'let '(orgstruct-mode)
  7612. (list 'call-interactively
  7613. (append '(or)
  7614. (mapcar (lambda (k)
  7615. (list 'key-binding k))
  7616. keys)
  7617. '('orgstruct-error))))))))
  7618. (defun org-contextualize-keys (alist contexts)
  7619. "Return valid elements in ALIST depending on CONTEXTS.
  7620. `org-agenda-custom-commands' or `org-capture-templates' are the
  7621. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7622. or `org-capture-templates-contexts' are the associated contexts
  7623. definitions."
  7624. (let ((contexts
  7625. ;; normalize contexts
  7626. (mapcar
  7627. (lambda(c) (cond ((listp (cadr c))
  7628. (list (car c) (car c) (cadr c)))
  7629. ((string= "" (cadr c))
  7630. (list (car c) (car c) (caddr c)))
  7631. (t c))) contexts))
  7632. (a alist) c r s)
  7633. ;; loop over all commands or templates
  7634. (while (setq c (pop a))
  7635. (let (vrules repl)
  7636. (cond
  7637. ((not (assoc (car c) contexts))
  7638. (push c r))
  7639. ((and (assoc (car c) contexts)
  7640. (setq vrules (org-contextualize-validate-key
  7641. (car c) contexts)))
  7642. (mapc (lambda (vr)
  7643. (when (not (equal (car vr) (cadr vr)))
  7644. (setq repl vr))) vrules)
  7645. (if (not repl) (push c r)
  7646. (push (cadr repl) s)
  7647. (push
  7648. (cons (car c)
  7649. (cdr (or (assoc (cadr repl) alist)
  7650. (error "Undefined key `%s' as contextual replacement for `%s'"
  7651. (cadr repl) (car c)))))
  7652. r))))))
  7653. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7654. (delq
  7655. nil
  7656. (delete-dups
  7657. (mapcar (lambda (x)
  7658. (let ((tpl (car x)))
  7659. (when (not (delq
  7660. nil
  7661. (mapcar (lambda(y)
  7662. (equal y tpl)) s))) x)))
  7663. (reverse r))))))
  7664. (defun org-contextualize-validate-key (key contexts)
  7665. "Check CONTEXTS for agenda or capture KEY."
  7666. (let (r rr res)
  7667. (while (setq r (pop contexts))
  7668. (mapc
  7669. (lambda (rr)
  7670. (when
  7671. (and (equal key (car r))
  7672. (if (functionp rr) (funcall rr)
  7673. (or (and (eq (car rr) 'in-file)
  7674. (buffer-file-name)
  7675. (string-match (cdr rr) (buffer-file-name)))
  7676. (and (eq (car rr) 'in-mode)
  7677. (string-match (cdr rr) (symbol-name major-mode)))
  7678. (when (and (eq (car rr) 'not-in-file)
  7679. (buffer-file-name))
  7680. (not (string-match (cdr rr) (buffer-file-name))))
  7681. (when (eq (car rr) 'not-in-mode)
  7682. (not (string-match (cdr rr) (symbol-name major-mode)))))))
  7683. (push r res)))
  7684. (car (last r))))
  7685. (delete-dups (delq nil res))))
  7686. (defun org-context-p (&rest contexts)
  7687. "Check if local context is any of CONTEXTS.
  7688. Possible values in the list of contexts are `table', `headline', and `item'."
  7689. (let ((pos (point)))
  7690. (goto-char (point-at-bol))
  7691. (prog1 (or (and (memq 'table contexts)
  7692. (looking-at "[ \t]*|"))
  7693. (and (memq 'headline contexts)
  7694. (looking-at org-outline-regexp))
  7695. (and (memq 'item contexts)
  7696. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7697. (and (memq 'item-body contexts)
  7698. (org-in-item-p)))
  7699. (goto-char pos))))
  7700. (defun org-get-local-variables ()
  7701. "Return a list of all local variables in an Org mode buffer."
  7702. (let (varlist)
  7703. (with-current-buffer (get-buffer-create "*Org tmp*")
  7704. (erase-buffer)
  7705. (org-mode)
  7706. (setq varlist (buffer-local-variables)))
  7707. (kill-buffer "*Org tmp*")
  7708. (delq nil
  7709. (mapcar
  7710. (lambda (x)
  7711. (setq x
  7712. (if (symbolp x)
  7713. (list x)
  7714. (list (car x) (list 'quote (cdr x)))))
  7715. (if (string-match
  7716. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7717. (symbol-name (car x)))
  7718. x nil))
  7719. varlist))))
  7720. (defun org-clone-local-variables (from-buffer &optional regexp)
  7721. "Clone local variables from FROM-BUFFER.
  7722. Optional argument REGEXP selects variables to clone."
  7723. (mapc
  7724. (lambda (pair)
  7725. (and (symbolp (car pair))
  7726. (or (null regexp)
  7727. (string-match regexp (symbol-name (car pair))))
  7728. (set (make-local-variable (car pair))
  7729. (cdr pair))))
  7730. (buffer-local-variables from-buffer)))
  7731. ;;;###autoload
  7732. (defun org-run-like-in-org-mode (cmd)
  7733. "Run a command, pretending that the current buffer is in Org-mode.
  7734. This will temporarily bind local variables that are typically bound in
  7735. Org-mode to the values they have in Org-mode, and then interactively
  7736. call CMD."
  7737. (org-load-modules-maybe)
  7738. (unless org-local-vars
  7739. (setq org-local-vars (org-get-local-variables)))
  7740. (eval (list 'let org-local-vars
  7741. (list 'call-interactively (list 'quote cmd)))))
  7742. ;;;; Archiving
  7743. (defun org-get-category (&optional pos force-refresh)
  7744. "Get the category applying to position POS."
  7745. (save-match-data
  7746. (if force-refresh (org-refresh-category-properties))
  7747. (let ((pos (or pos (point))))
  7748. (or (get-text-property pos 'org-category)
  7749. (progn (org-refresh-category-properties)
  7750. (get-text-property pos 'org-category))))))
  7751. (defun org-refresh-category-properties ()
  7752. "Refresh category text properties in the buffer."
  7753. (let ((case-fold-search t)
  7754. (inhibit-read-only t)
  7755. (def-cat (cond
  7756. ((null org-category)
  7757. (if buffer-file-name
  7758. (file-name-sans-extension
  7759. (file-name-nondirectory buffer-file-name))
  7760. "???"))
  7761. ((symbolp org-category) (symbol-name org-category))
  7762. (t org-category)))
  7763. beg end cat pos optionp)
  7764. (org-unmodified
  7765. (save-excursion
  7766. (save-restriction
  7767. (widen)
  7768. (goto-char (point-min))
  7769. (put-text-property (point) (point-max) 'org-category def-cat)
  7770. (while (re-search-forward
  7771. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7772. (setq pos (match-end 0)
  7773. optionp (equal (char-after (match-beginning 0)) ?#)
  7774. cat (org-trim (match-string 2)))
  7775. (if optionp
  7776. (setq beg (point-at-bol) end (point-max))
  7777. (org-back-to-heading t)
  7778. (setq beg (point) end (org-end-of-subtree t t)))
  7779. (put-text-property beg end 'org-category cat)
  7780. (put-text-property beg end 'org-category-position beg)
  7781. (goto-char pos)))))))
  7782. (defun org-refresh-properties (dprop tprop)
  7783. "Refresh buffer text properties.
  7784. DPROP is the drawer property and TPROP is the corresponding text
  7785. property to set."
  7786. (let ((case-fold-search t)
  7787. (inhibit-read-only t) p)
  7788. (org-unmodified
  7789. (save-excursion
  7790. (save-restriction
  7791. (widen)
  7792. (goto-char (point-min))
  7793. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  7794. (setq p (org-match-string-no-properties 1))
  7795. (save-excursion
  7796. (org-back-to-heading t)
  7797. (put-text-property
  7798. (point-at-bol) (point-at-eol) tprop p))))))))
  7799. ;;;; Link Stuff
  7800. ;;; Link abbreviations
  7801. (defun org-link-expand-abbrev (link)
  7802. "Apply replacements as defined in `org-link-abbrev-alist'."
  7803. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7804. (let* ((key (match-string 1 link))
  7805. (as (or (assoc key org-link-abbrev-alist-local)
  7806. (assoc key org-link-abbrev-alist)))
  7807. (tag (and (match-end 2) (match-string 3 link)))
  7808. rpl)
  7809. (if (not as)
  7810. link
  7811. (setq rpl (cdr as))
  7812. (cond
  7813. ((symbolp rpl) (funcall rpl tag))
  7814. ((string-match "%(\\([^)]+\\))" rpl)
  7815. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  7816. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7817. ((string-match "%h" rpl)
  7818. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7819. (t (concat rpl tag)))))
  7820. link))
  7821. ;;; Storing and inserting links
  7822. (defvar org-insert-link-history nil
  7823. "Minibuffer history for links inserted with `org-insert-link'.")
  7824. (defvar org-stored-links nil
  7825. "Contains the links stored with `org-store-link'.")
  7826. (defvar org-store-link-plist nil
  7827. "Plist with info about the most recently link created with `org-store-link'.")
  7828. (defvar org-link-protocols nil
  7829. "Link protocols added to Org-mode using `org-add-link-type'.")
  7830. (defvar org-store-link-functions nil
  7831. "List of functions that are called to create and store a link.
  7832. Each function will be called in turn until one returns a non-nil
  7833. value. Each function should check if it is responsible for creating
  7834. this link (for example by looking at the major mode).
  7835. If not, it must exit and return nil.
  7836. If yes, it should return a non-nil value after a calling
  7837. `org-store-link-props' with a list of properties and values.
  7838. Special properties are:
  7839. :type The link prefix, like \"http\". This must be given.
  7840. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7841. This is obligatory as well.
  7842. :description Optional default description for the second pair
  7843. of brackets in an Org-mode link. The user can still change
  7844. this when inserting this link into an Org-mode buffer.
  7845. In addition to these, any additional properties can be specified
  7846. and then used in capture templates.")
  7847. (defun org-add-link-type (type &optional follow export)
  7848. "Add TYPE to the list of `org-link-types'.
  7849. Re-compute all regular expressions depending on `org-link-types'
  7850. FOLLOW and EXPORT are two functions.
  7851. FOLLOW should take the link path as the single argument and do whatever
  7852. is necessary to follow the link, for example find a file or display
  7853. a mail message.
  7854. EXPORT should format the link path for export to one of the export formats.
  7855. It should be a function accepting three arguments:
  7856. path the path of the link, the text after the prefix (like \"http:\")
  7857. desc the description of the link, if any, or a description added by
  7858. org-export-normalize-links if there is none
  7859. format the export format, a symbol like `html' or `latex' or `ascii'..
  7860. The function may use the FORMAT information to return different values
  7861. depending on the format. The return value will be put literally into
  7862. the exported file. If the return value is nil, this means Org should
  7863. do what it normally does with links which do not have EXPORT defined.
  7864. Org-mode has a built-in default for exporting links. If you are happy with
  7865. this default, there is no need to define an export function for the link
  7866. type. For a simple example of an export function, see `org-bbdb.el'."
  7867. (add-to-list 'org-link-types type t)
  7868. (org-make-link-regexps)
  7869. (if (assoc type org-link-protocols)
  7870. (setcdr (assoc type org-link-protocols) (list follow export))
  7871. (push (list type follow export) org-link-protocols)))
  7872. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  7873. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  7874. ;;;###autoload
  7875. (defun org-store-link (arg)
  7876. "\\<org-mode-map>Store an org-link to the current location.
  7877. This link is added to `org-stored-links' and can later be inserted
  7878. into an org-buffer with \\[org-insert-link].
  7879. For some link types, a prefix arg is interpreted:
  7880. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7881. For file links, arg negates `org-context-in-file-links'."
  7882. (interactive "P")
  7883. (org-load-modules-maybe)
  7884. (setq org-store-link-plist nil) ; reset
  7885. (org-with-limited-levels
  7886. (let (link cpltxt desc description search txt custom-id agenda-link)
  7887. (cond
  7888. ((run-hook-with-args-until-success 'org-store-link-functions)
  7889. (setq link (plist-get org-store-link-plist :link)
  7890. desc (or (plist-get org-store-link-plist :description) link)))
  7891. ((org-src-edit-buffer-p)
  7892. (let (label gc)
  7893. (while (or (not label)
  7894. (save-excursion
  7895. (save-restriction
  7896. (widen)
  7897. (goto-char (point-min))
  7898. (re-search-forward
  7899. (regexp-quote (format org-coderef-label-format label))
  7900. nil t))))
  7901. (when label (message "Label exists already") (sit-for 2))
  7902. (setq label (read-string "Code line label: " label)))
  7903. (end-of-line 1)
  7904. (setq link (format org-coderef-label-format label))
  7905. (setq gc (- 79 (length link)))
  7906. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7907. (insert link)
  7908. (setq link (concat "(" label ")") desc nil)))
  7909. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7910. ;; We are in the agenda, link to referenced location
  7911. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7912. (get-text-property (point) 'org-marker))))
  7913. (when m
  7914. (org-with-point-at m
  7915. (setq agenda-link
  7916. (if (org-called-interactively-p 'any)
  7917. (call-interactively 'org-store-link)
  7918. (org-store-link nil)))))))
  7919. ((eq major-mode 'calendar-mode)
  7920. (let ((cd (calendar-cursor-to-date)))
  7921. (setq link
  7922. (format-time-string
  7923. (car org-time-stamp-formats)
  7924. (apply 'encode-time
  7925. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7926. nil nil nil))))
  7927. (org-store-link-props :type "calendar" :date cd)))
  7928. ((eq major-mode 'help-mode)
  7929. (setq link (concat "help:" (save-excursion
  7930. (goto-char (point-min))
  7931. (looking-at "^[^ ]+")
  7932. (match-string 0))))
  7933. (org-store-link-props :type "help"))
  7934. ((eq major-mode 'w3-mode)
  7935. (setq cpltxt (if (and (buffer-name)
  7936. (not (string-match "Untitled" (buffer-name))))
  7937. (buffer-name)
  7938. (url-view-url t))
  7939. link (url-view-url t))
  7940. (org-store-link-props :type "w3" :url (url-view-url t)))
  7941. ((eq major-mode 'w3m-mode)
  7942. (setq cpltxt (or w3m-current-title w3m-current-url)
  7943. link w3m-current-url)
  7944. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7945. ((setq search (run-hook-with-args-until-success
  7946. 'org-create-file-search-functions))
  7947. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7948. "::" search))
  7949. (setq cpltxt (or description link)))
  7950. ((eq major-mode 'image-mode)
  7951. (setq cpltxt (concat "file:"
  7952. (abbreviate-file-name buffer-file-name))
  7953. link cpltxt)
  7954. (org-store-link-props :type "image" :file buffer-file-name))
  7955. ((eq major-mode 'dired-mode)
  7956. ;; link to the file in the current line
  7957. (let ((file (dired-get-filename nil t)))
  7958. (setq file (if file
  7959. (abbreviate-file-name
  7960. (expand-file-name (dired-get-filename nil t)))
  7961. ;; otherwise, no file so use current directory.
  7962. default-directory))
  7963. (setq cpltxt (concat "file:" file)
  7964. link cpltxt)))
  7965. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  7966. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7967. (cond
  7968. ((org-in-regexp "<<\\(.*?\\)>>")
  7969. (setq cpltxt
  7970. (concat "file:"
  7971. (abbreviate-file-name
  7972. (buffer-file-name (buffer-base-buffer)))
  7973. "::" (match-string 1))
  7974. link cpltxt))
  7975. ((and (featurep 'org-id)
  7976. (or (eq org-id-link-to-org-use-id t)
  7977. (and (org-called-interactively-p 'any)
  7978. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  7979. (and (eq org-id-link-to-org-use-id
  7980. 'create-if-interactive-and-no-custom-id)
  7981. (not custom-id))))
  7982. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  7983. ;; We can make a link using the ID.
  7984. (setq link (condition-case nil
  7985. (prog1 (org-id-store-link)
  7986. (setq desc (plist-get org-store-link-plist :description)))
  7987. (error
  7988. ;; probably before first headline, link to file only
  7989. (concat "file:"
  7990. (abbreviate-file-name
  7991. (buffer-file-name (buffer-base-buffer))))))))
  7992. (t
  7993. ;; Just link to current headline
  7994. (setq cpltxt (concat "file:"
  7995. (abbreviate-file-name
  7996. (buffer-file-name (buffer-base-buffer)))))
  7997. ;; Add a context search string
  7998. (when (org-xor org-context-in-file-links arg)
  7999. (setq txt (cond
  8000. ((org-at-heading-p) nil)
  8001. ((org-region-active-p)
  8002. (buffer-substring (region-beginning) (region-end)))))
  8003. (when (or (null txt) (string-match "\\S-" txt))
  8004. (setq cpltxt
  8005. (concat cpltxt "::"
  8006. (condition-case nil
  8007. (org-make-org-heading-search-string txt)
  8008. (error "")))
  8009. desc (or (nth 4 (ignore-errors
  8010. (org-heading-components))) "NONE"))))
  8011. (if (string-match "::\\'" cpltxt)
  8012. (setq cpltxt (substring cpltxt 0 -2)))
  8013. (setq link cpltxt))))
  8014. ((buffer-file-name (buffer-base-buffer))
  8015. ;; Just link to this file here.
  8016. (setq cpltxt (concat "file:"
  8017. (abbreviate-file-name
  8018. (buffer-file-name (buffer-base-buffer)))))
  8019. ;; Add a context string
  8020. (when (org-xor org-context-in-file-links arg)
  8021. (setq txt (if (org-region-active-p)
  8022. (buffer-substring (region-beginning) (region-end))
  8023. (buffer-substring (point-at-bol) (point-at-eol))))
  8024. ;; Only use search option if there is some text.
  8025. (when (string-match "\\S-" txt)
  8026. (setq cpltxt
  8027. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8028. desc "NONE")))
  8029. (setq link cpltxt))
  8030. ((org-called-interactively-p 'interactive)
  8031. (error "Cannot link to a buffer which is not visiting a file"))
  8032. (t (setq link nil)))
  8033. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8034. (setq link (or link cpltxt)
  8035. desc (or desc cpltxt))
  8036. (if (equal desc "NONE") (setq desc nil))
  8037. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8038. (progn
  8039. (setq org-stored-links
  8040. (cons (list link desc) org-stored-links))
  8041. (message "Stored: %s" (or desc link))
  8042. (when custom-id
  8043. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8044. "::#" custom-id))
  8045. (setq org-stored-links
  8046. (cons (list link desc) org-stored-links))))
  8047. (or agenda-link (and link (org-make-link-string link desc)))))))
  8048. (defun org-store-link-props (&rest plist)
  8049. "Store link properties, extract names and addresses."
  8050. (let (x adr)
  8051. (when (setq x (plist-get plist :from))
  8052. (setq adr (mail-extract-address-components x))
  8053. (setq plist (plist-put plist :fromname (car adr)))
  8054. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8055. (when (setq x (plist-get plist :to))
  8056. (setq adr (mail-extract-address-components x))
  8057. (setq plist (plist-put plist :toname (car adr)))
  8058. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8059. (let ((from (plist-get plist :from))
  8060. (to (plist-get plist :to)))
  8061. (when (and from to org-from-is-user-regexp)
  8062. (setq plist
  8063. (plist-put plist :fromto
  8064. (if (string-match org-from-is-user-regexp from)
  8065. (concat "to %t")
  8066. (concat "from %f"))))))
  8067. (setq org-store-link-plist plist))
  8068. (defun org-add-link-props (&rest plist)
  8069. "Add these properties to the link property list."
  8070. (let (key value)
  8071. (while plist
  8072. (setq key (pop plist) value (pop plist))
  8073. (setq org-store-link-plist
  8074. (plist-put org-store-link-plist key value)))))
  8075. (defun org-email-link-description (&optional fmt)
  8076. "Return the description part of an email link.
  8077. This takes information from `org-store-link-plist' and formats it
  8078. according to FMT (default from `org-email-link-description-format')."
  8079. (setq fmt (or fmt org-email-link-description-format))
  8080. (let* ((p org-store-link-plist)
  8081. (to (plist-get p :toaddress))
  8082. (from (plist-get p :fromaddress))
  8083. (table
  8084. (list
  8085. (cons "%c" (plist-get p :fromto))
  8086. (cons "%F" (plist-get p :from))
  8087. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8088. (cons "%T" (plist-get p :to))
  8089. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8090. (cons "%s" (plist-get p :subject))
  8091. (cons "%d" (plist-get p :date))
  8092. (cons "%m" (plist-get p :message-id)))))
  8093. (when (string-match "%c" fmt)
  8094. ;; Check if the user wrote this message
  8095. (if (and org-from-is-user-regexp from to
  8096. (save-match-data (string-match org-from-is-user-regexp from)))
  8097. (setq fmt (replace-match "to %t" t t fmt))
  8098. (setq fmt (replace-match "from %f" t t fmt))))
  8099. (org-replace-escapes fmt table)))
  8100. (defun org-make-org-heading-search-string (&optional string heading)
  8101. "Make search string for STRING or current headline."
  8102. (interactive)
  8103. (let ((s (or string (org-get-heading)))
  8104. (lines org-context-in-file-links))
  8105. (unless (and string (not heading))
  8106. ;; We are using a headline, clean up garbage in there.
  8107. (if (string-match org-todo-regexp s)
  8108. (setq s (replace-match "" t t s)))
  8109. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8110. (setq s (replace-match "" t t s)))
  8111. (setq s (org-trim s))
  8112. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8113. org-comment-string "\\)") s)
  8114. (setq s (replace-match "" t t s)))
  8115. (while (string-match org-ts-regexp s)
  8116. (setq s (replace-match "" t t s))))
  8117. (or string (setq s (concat "*" s))) ; Add * for headlines
  8118. (when (and string (integerp lines) (> lines 0))
  8119. (let ((slines (org-split-string s "\n")))
  8120. (when (< lines (length slines))
  8121. (setq s (mapconcat
  8122. 'identity
  8123. (reverse (nthcdr (- (length slines) lines)
  8124. (reverse slines))) "\n")))))
  8125. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8126. (defun org-make-link-string (link &optional description)
  8127. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8128. (unless (string-match "\\S-" link)
  8129. (error "Empty link"))
  8130. (when (and description
  8131. (stringp description)
  8132. (not (string-match "\\S-" description)))
  8133. (setq description nil))
  8134. (when (stringp description)
  8135. ;; Remove brackets from the description, they are fatal.
  8136. (while (string-match "\\[" description)
  8137. (setq description (replace-match "{" t t description)))
  8138. (while (string-match "\\]" description)
  8139. (setq description (replace-match "}" t t description))))
  8140. (when (equal link description)
  8141. ;; No description needed, it is identical
  8142. (setq description nil))
  8143. (when (and (not description)
  8144. (not (string-match (org-image-file-name-regexp) link))
  8145. (not (equal link (org-link-escape link))))
  8146. (setq description (org-extract-attributes link)))
  8147. (setq link
  8148. (cond ((string-match (org-image-file-name-regexp) link) link)
  8149. ((string-match org-link-types-re link)
  8150. (concat (match-string 1 link)
  8151. (org-link-escape (substring link (match-end 1)))))
  8152. (t (org-link-escape link))))
  8153. (concat "[[" link "]"
  8154. (if description (concat "[" description "]") "")
  8155. "]"))
  8156. (defconst org-link-escape-chars
  8157. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8158. "List of characters that should be escaped in link.
  8159. This is the list that is used for internal purposes.")
  8160. (defconst org-link-escape-chars-browser
  8161. '(?\ )
  8162. "List of escapes for characters that are problematic in links.
  8163. This is the list that is used before handing over to the browser.")
  8164. (defun org-link-escape (text &optional table merge)
  8165. "Return percent escaped representation of TEXT.
  8166. TEXT is a string with the text to escape.
  8167. Optional argument TABLE is a list with characters that should be
  8168. escaped. When nil, `org-link-escape-chars' is used.
  8169. If optional argument MERGE is set, merge TABLE into
  8170. `org-link-escape-chars'."
  8171. (cond
  8172. ((and table merge)
  8173. (mapc (lambda (defchr)
  8174. (unless (member defchr table)
  8175. (setq table (cons defchr table)))) org-link-escape-chars))
  8176. ((null table)
  8177. (setq table org-link-escape-chars)))
  8178. (mapconcat
  8179. (lambda (char)
  8180. (if (or (member char table)
  8181. (and (or (< char 32) (= char 37) (> char 126))
  8182. org-url-hexify-p))
  8183. (mapconcat (lambda (sequence-element)
  8184. (format "%%%.2X" sequence-element))
  8185. (or (encode-coding-char char 'utf-8)
  8186. (error "Unable to percent escape character: %s"
  8187. (char-to-string char))) "")
  8188. (char-to-string char))) text ""))
  8189. (defun org-link-unescape (str)
  8190. "Unhex hexified Unicode strings as returned from the JavaScript function
  8191. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8192. (unless (and (null str) (string= "" str))
  8193. (let ((pos 0) (case-fold-search t) unhexed)
  8194. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8195. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8196. (setq str (replace-match unhexed t t str))
  8197. (setq pos (+ pos (length unhexed))))))
  8198. str)
  8199. (defun org-link-unescape-compound (hex)
  8200. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8201. Note: this function also decodes single byte encodings like
  8202. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8203. (save-match-data
  8204. (let* ((bytes (cdr (split-string hex "%")))
  8205. (ret "")
  8206. (eat 0)
  8207. (sum 0))
  8208. (while bytes
  8209. (let* ((val (string-to-number (pop bytes) 16))
  8210. (shift-xor
  8211. (if (= 0 eat)
  8212. (cond
  8213. ((>= val 252) (cons 6 252))
  8214. ((>= val 248) (cons 5 248))
  8215. ((>= val 240) (cons 4 240))
  8216. ((>= val 224) (cons 3 224))
  8217. ((>= val 192) (cons 2 192))
  8218. (t (cons 0 0)))
  8219. (cons 6 128))))
  8220. (if (>= val 192) (setq eat (car shift-xor)))
  8221. (setq val (logxor val (cdr shift-xor)))
  8222. (setq sum (+ (lsh sum (car shift-xor)) val))
  8223. (if (> eat 0) (setq eat (- eat 1)))
  8224. (cond
  8225. ((= 0 eat) ;multi byte
  8226. (setq ret (concat ret (org-char-to-string sum)))
  8227. (setq sum 0))
  8228. ((not bytes) ; single byte(s)
  8229. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8230. )) ;; end (while bytes
  8231. ret )))
  8232. (defun org-link-unescape-single-byte-sequence (hex)
  8233. "Unhexify hex-encoded single byte character sequences."
  8234. (mapconcat (lambda (byte)
  8235. (char-to-string (string-to-number byte 16)))
  8236. (cdr (split-string hex "%")) ""))
  8237. (defun org-xor (a b)
  8238. "Exclusive or."
  8239. (if a (not b) b))
  8240. (defun org-fixup-message-id-for-http (s)
  8241. "Replace special characters in a message id, so it can be used in an http query."
  8242. (when (string-match "%" s)
  8243. (setq s (mapconcat (lambda (c)
  8244. (if (eq c ?%)
  8245. "%25"
  8246. (char-to-string c)))
  8247. s "")))
  8248. (while (string-match "<" s)
  8249. (setq s (replace-match "%3C" t t s)))
  8250. (while (string-match ">" s)
  8251. (setq s (replace-match "%3E" t t s)))
  8252. (while (string-match "@" s)
  8253. (setq s (replace-match "%40" t t s)))
  8254. s)
  8255. (defun org-link-prettify (link)
  8256. "Return a human-readable representation of LINK.
  8257. The car of LINK must be a raw link the cdr of LINK must be either
  8258. a link description or nil."
  8259. (let ((desc (or (cadr link) "<no description>")))
  8260. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8261. "<" (car link) ">")))
  8262. ;;;###autoload
  8263. (defun org-insert-link-global ()
  8264. "Insert a link like Org-mode does.
  8265. This command can be called in any mode to insert a link in Org-mode syntax."
  8266. (interactive)
  8267. (org-load-modules-maybe)
  8268. (org-run-like-in-org-mode 'org-insert-link))
  8269. (defun org-insert-all-links (&optional keep)
  8270. "Insert all links in `org-stored-links'."
  8271. (interactive "P")
  8272. (let ((links (copy-sequence org-stored-links)) l)
  8273. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8274. (insert "- ")
  8275. (org-insert-link nil (car l) (cadr l))
  8276. (insert "\n"))))
  8277. (defun org-link-fontify-links-to-this-file ()
  8278. "Fontify links to the current file in `org-stored-links'."
  8279. (let ((f (buffer-file-name)) a b)
  8280. (setq a (mapcar (lambda(l)
  8281. (let ((ll (car l)))
  8282. (when (and (string-match "^file:\\(.+\\)::" ll)
  8283. (equal f (expand-file-name (match-string 1 ll))))
  8284. ll)))
  8285. org-stored-links))
  8286. (when (featurep 'org-id)
  8287. (setq b (mapcar (lambda(l)
  8288. (let ((ll (car l)))
  8289. (when (and (string-match "^id:\\(.+\\)$" ll)
  8290. (equal f (expand-file-name
  8291. (or (org-id-find-id-file
  8292. (match-string 1 ll)) ""))))
  8293. ll)))
  8294. org-stored-links)))
  8295. (mapcar (lambda(l)
  8296. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8297. (delq nil (append a b)))))
  8298. (defvar org-link-links-in-this-file nil)
  8299. (defun org-insert-link (&optional complete-file link-location default-description)
  8300. "Insert a link. At the prompt, enter the link.
  8301. Completion can be used to insert any of the link protocol prefixes like
  8302. http or ftp in use.
  8303. The history can be used to select a link previously stored with
  8304. `org-store-link'. When the empty string is entered (i.e. if you just
  8305. press RET at the prompt), the link defaults to the most recently
  8306. stored link. As SPC triggers completion in the minibuffer, you need to
  8307. use M-SPC or C-q SPC to force the insertion of a space character.
  8308. You will also be prompted for a description, and if one is given, it will
  8309. be displayed in the buffer instead of the link.
  8310. If there is already a link at point, this command will allow you to edit link
  8311. and description parts.
  8312. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8313. be selected using completion. The path to the file will be relative to the
  8314. current directory if the file is in the current directory or a subdirectory.
  8315. Otherwise, the link will be the absolute path as completed in the minibuffer
  8316. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8317. option `org-link-file-path-type'.
  8318. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8319. the current directory or below.
  8320. With three \\[universal-argument] prefixes, negate the meaning of
  8321. `org-keep-stored-link-after-insertion'.
  8322. If `org-make-link-description-function' is non-nil, this function will be
  8323. called with the link target, and the result will be the default
  8324. link description.
  8325. If the LINK-LOCATION parameter is non-nil, this value will be
  8326. used as the link location instead of reading one interactively.
  8327. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8328. be used as the default description."
  8329. (interactive "P")
  8330. (let* ((wcf (current-window-configuration))
  8331. (region (if (org-region-active-p)
  8332. (buffer-substring (region-beginning) (region-end))))
  8333. (remove (and region (list (region-beginning) (region-end))))
  8334. (desc region)
  8335. tmphist ; byte-compile incorrectly complains about this
  8336. (link link-location)
  8337. (abbrevs org-link-abbrev-alist-local)
  8338. entry file all-prefixes auto-desc)
  8339. (cond
  8340. (link-location) ; specified by arg, just use it.
  8341. ((org-in-regexp org-bracket-link-regexp 1)
  8342. ;; We do have a link at point, and we are going to edit it.
  8343. (setq remove (list (match-beginning 0) (match-end 0)))
  8344. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8345. (setq link (read-string "Link: "
  8346. (org-link-unescape
  8347. (org-match-string-no-properties 1)))))
  8348. ((or (org-in-regexp org-angle-link-re)
  8349. (org-in-regexp org-plain-link-re))
  8350. ;; Convert to bracket link
  8351. (setq remove (list (match-beginning 0) (match-end 0))
  8352. link (read-string "Link: "
  8353. (org-remove-angle-brackets (match-string 0)))))
  8354. ((member complete-file '((4) (16)))
  8355. ;; Completing read for file names.
  8356. (setq link (org-file-complete-link complete-file)))
  8357. (t
  8358. ;; Read link, with completion for stored links.
  8359. (org-link-fontify-links-to-this-file)
  8360. (org-switch-to-buffer-other-window "*Org Links*")
  8361. (with-current-buffer "*Org Links*"
  8362. (erase-buffer)
  8363. (insert "Insert a link.
  8364. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8365. (when org-stored-links
  8366. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8367. (insert (mapconcat 'org-link-prettify
  8368. (reverse org-stored-links) "\n")))
  8369. (goto-char (point-min)))
  8370. (let ((cw (selected-window)))
  8371. (select-window (get-buffer-window "*Org Links*" 'visible))
  8372. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8373. (unless (pos-visible-in-window-p (point-max))
  8374. (org-fit-window-to-buffer))
  8375. (and (window-live-p cw) (select-window cw)))
  8376. ;; Fake a link history, containing the stored links.
  8377. (setq tmphist (append (mapcar 'car org-stored-links)
  8378. org-insert-link-history))
  8379. (setq all-prefixes (append (mapcar 'car abbrevs)
  8380. (mapcar 'car org-link-abbrev-alist)
  8381. org-link-types))
  8382. (unwind-protect
  8383. (progn
  8384. (setq link
  8385. (let ((org-completion-use-ido nil)
  8386. (org-completion-use-iswitchb nil))
  8387. (org-completing-read
  8388. "Link: "
  8389. (append
  8390. (mapcar (lambda (x) (list (concat x ":")))
  8391. all-prefixes)
  8392. (mapcar 'car org-stored-links)
  8393. (mapcar 'cadr org-stored-links))
  8394. nil nil nil
  8395. 'tmphist
  8396. (caar org-stored-links))))
  8397. (if (not (string-match "\\S-" link))
  8398. (error "No link selected"))
  8399. (mapc (lambda(l)
  8400. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8401. org-stored-links)
  8402. (if (or (member link all-prefixes)
  8403. (and (equal ":" (substring link -1))
  8404. (member (substring link 0 -1) all-prefixes)
  8405. (setq link (substring link 0 -1))))
  8406. (setq link (org-link-try-special-completion link))))
  8407. (set-window-configuration wcf)
  8408. (kill-buffer "*Org Links*"))
  8409. (setq entry (assoc link org-stored-links))
  8410. (or entry (push link org-insert-link-history))
  8411. (setq desc (or desc (nth 1 entry)))))
  8412. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8413. (not org-keep-stored-link-after-insertion))
  8414. (setq org-stored-links (delq (assoc link org-stored-links)
  8415. org-stored-links)))
  8416. (if (string-match org-plain-link-re link)
  8417. ;; URL-like link, normalize the use of angular brackets.
  8418. (setq link (org-remove-angle-brackets link)))
  8419. ;; Check if we are linking to the current file with a search
  8420. ;; option If yes, simplify the link by using only the search
  8421. ;; option.
  8422. (when (and buffer-file-name
  8423. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8424. (let* ((path (match-string 1 link))
  8425. (case-fold-search nil)
  8426. (search (match-string 2 link)))
  8427. (save-match-data
  8428. (if (equal (file-truename buffer-file-name) (file-truename path))
  8429. ;; We are linking to this same file, with a search option
  8430. (setq link search)))))
  8431. ;; Check if we can/should use a relative path. If yes, simplify the link
  8432. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8433. (let* ((type (match-string 1 link))
  8434. (path (match-string 2 link))
  8435. (origpath path)
  8436. (case-fold-search nil))
  8437. (cond
  8438. ((or (eq org-link-file-path-type 'absolute)
  8439. (equal complete-file '(16)))
  8440. (setq path (abbreviate-file-name (expand-file-name path))))
  8441. ((eq org-link-file-path-type 'noabbrev)
  8442. (setq path (expand-file-name path)))
  8443. ((eq org-link-file-path-type 'relative)
  8444. (setq path (file-relative-name path)))
  8445. (t
  8446. (save-match-data
  8447. (if (string-match (concat "^" (regexp-quote
  8448. (expand-file-name
  8449. (file-name-as-directory
  8450. default-directory))))
  8451. (expand-file-name path))
  8452. ;; We are linking a file with relative path name.
  8453. (setq path (substring (expand-file-name path)
  8454. (match-end 0)))
  8455. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8456. (setq link (concat type path))
  8457. (if (equal desc origpath)
  8458. (setq desc path))))
  8459. (if org-make-link-description-function
  8460. (setq desc
  8461. (or (condition-case nil
  8462. (funcall org-make-link-description-function link desc)
  8463. (error (progn (message "Can't get link description from `%s'"
  8464. (symbol-name org-make-link-description-function))
  8465. (sit-for 2) nil)))
  8466. (read-string "Description: " default-description)))
  8467. (if default-description (setq desc default-description)
  8468. (setq desc (or (and auto-desc desc)
  8469. (read-string "Description: " desc)))))
  8470. (unless (string-match "\\S-" desc) (setq desc nil))
  8471. (if remove (apply 'delete-region remove))
  8472. (insert (org-make-link-string link desc))))
  8473. (defun org-link-try-special-completion (type)
  8474. "If there is completion support for link type TYPE, offer it."
  8475. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8476. (if (functionp fun)
  8477. (funcall fun)
  8478. (read-string "Link (no completion support): " (concat type ":")))))
  8479. (defun org-file-complete-link (&optional arg)
  8480. "Create a file link using completion."
  8481. (let (file link)
  8482. (setq file (read-file-name "File: "))
  8483. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8484. (pwd1 (file-name-as-directory (abbreviate-file-name
  8485. (expand-file-name ".")))))
  8486. (cond
  8487. ((equal arg '(16))
  8488. (setq link (concat
  8489. "file:"
  8490. (abbreviate-file-name (expand-file-name file)))))
  8491. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8492. (setq link (concat "file:" (match-string 1 file))))
  8493. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8494. (expand-file-name file))
  8495. (setq link (concat
  8496. "file:" (match-string 1 (expand-file-name file)))))
  8497. (t (setq link (concat "file:" file)))))
  8498. link))
  8499. (defun org-completing-read (&rest args)
  8500. "Completing-read with SPACE being a normal character."
  8501. (let ((enable-recursive-minibuffers t)
  8502. (minibuffer-local-completion-map
  8503. (copy-keymap minibuffer-local-completion-map)))
  8504. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8505. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8506. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8507. (apply 'org-icompleting-read args)))
  8508. (defun org-completing-read-no-i (&rest args)
  8509. (let (org-completion-use-ido org-completion-use-iswitchb)
  8510. (apply 'org-completing-read args)))
  8511. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8512. "Use iswitch as a completing-read replacement to choose from choices.
  8513. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8514. from."
  8515. (let* ((iswitchb-use-virtual-buffers nil)
  8516. (iswitchb-make-buflist-hook
  8517. (lambda ()
  8518. (setq iswitchb-temp-buflist choices))))
  8519. (iswitchb-read-buffer prompt)))
  8520. (defun org-icompleting-read (&rest args)
  8521. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8522. (org-without-partial-completion
  8523. (if (and org-completion-use-ido
  8524. (fboundp 'ido-completing-read)
  8525. (boundp 'ido-mode) ido-mode
  8526. (listp (second args)))
  8527. (let ((ido-enter-matching-directory nil))
  8528. (apply 'ido-completing-read (concat (car args))
  8529. (if (consp (car (nth 1 args)))
  8530. (mapcar 'car (nth 1 args))
  8531. (nth 1 args))
  8532. (cddr args)))
  8533. (if (and org-completion-use-iswitchb
  8534. (boundp 'iswitchb-mode) iswitchb-mode
  8535. (listp (second args)))
  8536. (apply 'org-iswitchb-completing-read (concat (car args))
  8537. (if (consp (car (nth 1 args)))
  8538. (mapcar 'car (nth 1 args))
  8539. (nth 1 args))
  8540. (cddr args))
  8541. (apply 'completing-read args)))))
  8542. (defun org-extract-attributes (s)
  8543. "Extract the attributes cookie from a string and set as text property."
  8544. (let (a attr (start 0) key value)
  8545. (save-match-data
  8546. (when (string-match "{{\\([^}]+\\)}}$" s)
  8547. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8548. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8549. (setq key (match-string 1 a) value (match-string 2 a)
  8550. start (match-end 0)
  8551. attr (plist-put attr (intern key) value))))
  8552. (org-add-props s nil 'org-attr attr))
  8553. s))
  8554. (defun org-extract-attributes-from-string (tag)
  8555. (let (key value attr)
  8556. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8557. (setq key (match-string 1 tag) value (match-string 2 tag)
  8558. tag (replace-match "" t t tag)
  8559. attr (plist-put attr (intern key) value)))
  8560. (cons tag attr)))
  8561. (defun org-attributes-to-string (plist)
  8562. "Format a property list into an HTML attribute list."
  8563. (let ((s "") key value)
  8564. (while plist
  8565. (setq key (pop plist) value (pop plist))
  8566. (and value
  8567. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8568. s))
  8569. ;;; Opening/following a link
  8570. (defvar org-link-search-failed nil)
  8571. (defvar org-open-link-functions nil
  8572. "Hook for functions finding a plain text link.
  8573. These functions must take a single argument, the link content.
  8574. They will be called for links that look like [[link text][description]]
  8575. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8576. like a filename (e.g. \"./blue.png\").
  8577. These functions will be called *before* Org attempts to resolve the
  8578. link by doing text searches in the current buffer - so if you want a
  8579. link \"[[target]]\" to still find \"<<target>>\", your function should
  8580. handle this as a special case.
  8581. When the function does handle the link, it must return a non-nil value.
  8582. If it decides that it is not responsible for this link, it must return
  8583. nil to indicate that that Org-mode can continue with other options
  8584. like exact and fuzzy text search.")
  8585. (defun org-next-link ()
  8586. "Move forward to the next link.
  8587. If the link is in hidden text, expose it."
  8588. (interactive)
  8589. (when (and org-link-search-failed (eq this-command last-command))
  8590. (goto-char (point-min))
  8591. (message "Link search wrapped back to beginning of buffer"))
  8592. (setq org-link-search-failed nil)
  8593. (let* ((pos (point))
  8594. (ct (org-context))
  8595. (a (assoc :link ct)))
  8596. (if a (goto-char (nth 2 a)))
  8597. (if (re-search-forward org-any-link-re nil t)
  8598. (progn
  8599. (goto-char (match-beginning 0))
  8600. (if (outline-invisible-p) (org-show-context)))
  8601. (goto-char pos)
  8602. (setq org-link-search-failed t)
  8603. (error "No further link found"))))
  8604. (defun org-previous-link ()
  8605. "Move backward to the previous link.
  8606. If the link is in hidden text, expose it."
  8607. (interactive)
  8608. (when (and org-link-search-failed (eq this-command last-command))
  8609. (goto-char (point-max))
  8610. (message "Link search wrapped back to end of buffer"))
  8611. (setq org-link-search-failed nil)
  8612. (let* ((pos (point))
  8613. (ct (org-context))
  8614. (a (assoc :link ct)))
  8615. (if a (goto-char (nth 1 a)))
  8616. (if (re-search-backward org-any-link-re nil t)
  8617. (progn
  8618. (goto-char (match-beginning 0))
  8619. (if (outline-invisible-p) (org-show-context)))
  8620. (goto-char pos)
  8621. (setq org-link-search-failed t)
  8622. (error "No further link found"))))
  8623. (defun org-translate-link (s)
  8624. "Translate a link string if a translation function has been defined."
  8625. (if (and org-link-translation-function
  8626. (fboundp org-link-translation-function)
  8627. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8628. (progn
  8629. (setq s (funcall org-link-translation-function
  8630. (match-string 1 s) (match-string 2 s)))
  8631. (concat (car s) ":" (cdr s)))
  8632. s))
  8633. (defun org-translate-link-from-planner (type path)
  8634. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8635. This is still an experimental function, your mileage may vary."
  8636. (cond
  8637. ((member type '("http" "https" "news" "ftp"))
  8638. ;; standard Internet links are the same.
  8639. nil)
  8640. ((and (equal type "irc") (string-match "^//" path))
  8641. ;; Planner has two / at the beginning of an irc link, we have 1.
  8642. ;; We should have zero, actually....
  8643. (setq path (substring path 1)))
  8644. ((and (equal type "lisp") (string-match "^/" path))
  8645. ;; Planner has a slash, we do not.
  8646. (setq type "elisp" path (substring path 1)))
  8647. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8648. ;; A typical message link. Planner has the id after the final slash,
  8649. ;; we separate it with a hash mark
  8650. (setq path (concat (match-string 1 path) "#"
  8651. (org-remove-angle-brackets (match-string 2 path)))))
  8652. )
  8653. (cons type path))
  8654. (defun org-find-file-at-mouse (ev)
  8655. "Open file link or URL at mouse."
  8656. (interactive "e")
  8657. (mouse-set-point ev)
  8658. (org-open-at-point 'in-emacs))
  8659. (defun org-open-at-mouse (ev)
  8660. "Open file link or URL at mouse.
  8661. See the docstring of `org-open-file' for details."
  8662. (interactive "e")
  8663. (mouse-set-point ev)
  8664. (if (eq major-mode 'org-agenda-mode)
  8665. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8666. (org-open-at-point))
  8667. (defvar org-window-config-before-follow-link nil
  8668. "The window configuration before following a link.
  8669. This is saved in case the need arises to restore it.")
  8670. (defvar org-open-link-marker (make-marker)
  8671. "Marker pointing to the location where `org-open-at-point; was called.")
  8672. ;;;###autoload
  8673. (defun org-open-at-point-global ()
  8674. "Follow a link like Org-mode does.
  8675. This command can be called in any mode to follow a link that has
  8676. Org-mode syntax."
  8677. (interactive)
  8678. (org-run-like-in-org-mode 'org-open-at-point))
  8679. ;;;###autoload
  8680. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8681. "Open a link in the string S, as if it was in Org-mode."
  8682. (interactive "sLink: \nP")
  8683. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8684. (with-temp-buffer
  8685. (let ((org-inhibit-startup (not reference-buffer)))
  8686. (org-mode)
  8687. (insert s)
  8688. (goto-char (point-min))
  8689. (when reference-buffer
  8690. (setq org-link-abbrev-alist-local
  8691. (with-current-buffer reference-buffer
  8692. org-link-abbrev-alist-local)))
  8693. (org-open-at-point arg reference-buffer)))))
  8694. (defvar org-open-at-point-functions nil
  8695. "Hook that is run when following a link at point.
  8696. Functions in this hook must return t if they identify and follow
  8697. a link at point. If they don't find anything interesting at point,
  8698. they must return nil.")
  8699. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  8700. (defun org-open-at-point (&optional arg reference-buffer)
  8701. "Open link at or after point.
  8702. If there is no link at point, this function will search forward up to
  8703. the end of the current line.
  8704. Normally, files will be opened by an appropriate application. If the
  8705. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8706. With a double prefix argument, try to open outside of Emacs, in the
  8707. application the system uses for this file type."
  8708. (interactive "P")
  8709. ;; if in a code block, then open the block's results
  8710. (unless (call-interactively #'org-babel-open-src-block-result)
  8711. (org-load-modules-maybe)
  8712. (move-marker org-open-link-marker (point))
  8713. (setq org-window-config-before-follow-link (current-window-configuration))
  8714. (org-remove-occur-highlights nil nil t)
  8715. (cond
  8716. ((and (org-at-heading-p)
  8717. (not (org-at-timestamp-p t))
  8718. (not (org-in-regexp
  8719. (concat org-plain-link-re "\\|"
  8720. org-bracket-link-regexp "\\|"
  8721. org-angle-link-re "\\|"
  8722. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8723. (not (get-text-property (point) 'org-linked-text)))
  8724. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  8725. (lk (car lkall))
  8726. (lkend (cdr lkall)))
  8727. (when lk
  8728. (prog1 (search-forward lk nil lkend)
  8729. (goto-char (match-beginning 0))
  8730. (org-open-at-point))))
  8731. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8732. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8733. ((and (org-at-timestamp-p t)
  8734. (not (org-in-regexp org-bracket-link-regexp)))
  8735. (org-follow-timestamp-link))
  8736. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8737. (not (org-in-regexp org-any-link-re)))
  8738. (org-footnote-action))
  8739. (t
  8740. (let (type path link line search (pos (point)))
  8741. (catch 'match
  8742. (save-excursion
  8743. (skip-chars-forward "^]\n\r")
  8744. (when (org-in-regexp org-bracket-link-regexp 1)
  8745. (setq link (org-extract-attributes
  8746. (org-link-unescape (org-match-string-no-properties 1))))
  8747. (while (string-match " *\n *" link)
  8748. (setq link (replace-match " " t t link)))
  8749. (setq link (org-link-expand-abbrev link))
  8750. (cond
  8751. ((or (file-name-absolute-p link)
  8752. (string-match "^\\.\\.?/" link))
  8753. (setq type "file" path link))
  8754. ((string-match org-link-re-with-space3 link)
  8755. (setq type (match-string 1 link) path (match-string 2 link)))
  8756. ((string-match "^help:+\\(.+\\)" link)
  8757. (setq type "help" path (match-string 1 link)))
  8758. (t (setq type "thisfile" path link)))
  8759. (throw 'match t)))
  8760. (when (get-text-property (point) 'org-linked-text)
  8761. (setq type "thisfile"
  8762. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8763. (1+ (point)) (point))
  8764. path (buffer-substring
  8765. (or (previous-single-property-change pos 'org-linked-text)
  8766. (point-min))
  8767. (or (next-single-property-change pos 'org-linked-text)
  8768. (point-max))))
  8769. (throw 'match t))
  8770. (save-excursion
  8771. (let ((plinkpos (org-in-regexp org-plain-link-re)))
  8772. (when (or (org-in-regexp org-angle-link-re)
  8773. (and plinkpos (goto-char (car plinkpos))
  8774. (save-match-data (not (looking-back "\\[\\[")))))
  8775. (setq type (match-string 1)
  8776. path (org-link-unescape (match-string 2)))
  8777. (throw 'match t))))
  8778. (save-excursion
  8779. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8780. (setq type "tags"
  8781. path (match-string 1))
  8782. (while (string-match ":" path)
  8783. (setq path (replace-match "+" t t path)))
  8784. (throw 'match t)))
  8785. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8786. (setq type "tree-match"
  8787. path (match-string 1))
  8788. (throw 'match t)))
  8789. (unless path
  8790. (user-error "No link found"))
  8791. ;; switch back to reference buffer
  8792. ;; needed when if called in a temporary buffer through
  8793. ;; org-open-link-from-string
  8794. (with-current-buffer (or reference-buffer (current-buffer))
  8795. ;; Remove any trailing spaces in path
  8796. (if (string-match " +\\'" path)
  8797. (setq path (replace-match "" t t path)))
  8798. (if (and org-link-translation-function
  8799. (fboundp org-link-translation-function))
  8800. ;; Check if we need to translate the link
  8801. (let ((tmp (funcall org-link-translation-function type path)))
  8802. (setq type (car tmp) path (cdr tmp))))
  8803. (cond
  8804. ((assoc type org-link-protocols)
  8805. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8806. ((equal type "help")
  8807. (let ((f-or-v (intern path)))
  8808. (cond ((fboundp f-or-v)
  8809. (describe-function f-or-v))
  8810. ((boundp f-or-v)
  8811. (describe-variable f-or-v))
  8812. (t (error "Not a known function or variable")))))
  8813. ((equal type "mailto")
  8814. (let ((cmd (car org-link-mailto-program))
  8815. (args (cdr org-link-mailto-program)) args1
  8816. (address path) (subject "") a)
  8817. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8818. (setq address (match-string 1 path)
  8819. subject (org-link-escape (match-string 2 path))))
  8820. (while args
  8821. (cond
  8822. ((not (stringp (car args))) (push (pop args) args1))
  8823. (t (setq a (pop args))
  8824. (if (string-match "%a" a)
  8825. (setq a (replace-match address t t a)))
  8826. (if (string-match "%s" a)
  8827. (setq a (replace-match subject t t a)))
  8828. (push a args1))))
  8829. (apply cmd (nreverse args1))))
  8830. ((member type '("http" "https" "ftp" "news"))
  8831. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8832. (org-link-escape
  8833. path org-link-escape-chars-browser)
  8834. path))))
  8835. ((string= type "doi")
  8836. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  8837. (org-link-escape
  8838. path org-link-escape-chars-browser)
  8839. path))))
  8840. ((member type '("message"))
  8841. (browse-url (concat type ":" path)))
  8842. ((string= type "tags")
  8843. (org-tags-view arg path))
  8844. ((string= type "tree-match")
  8845. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8846. ((string= type "file")
  8847. (if (string-match "::\\([0-9]+\\)\\'" path)
  8848. (setq line (string-to-number (match-string 1 path))
  8849. path (substring path 0 (match-beginning 0)))
  8850. (if (string-match "::\\(.+\\)\\'" path)
  8851. (setq search (match-string 1 path)
  8852. path (substring path 0 (match-beginning 0)))))
  8853. (if (string-match "[*?{]" (file-name-nondirectory path))
  8854. (dired path)
  8855. (org-open-file path arg line search)))
  8856. ((string= type "shell")
  8857. (let ((buf (generate-new-buffer "*Org Shell Output"))
  8858. (cmd path))
  8859. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8860. (string-match org-confirm-shell-link-not-regexp cmd))
  8861. (not org-confirm-shell-link-function)
  8862. (funcall org-confirm-shell-link-function
  8863. (format "Execute \"%s\" in shell? "
  8864. (org-add-props cmd nil
  8865. 'face 'org-warning))))
  8866. (progn
  8867. (message "Executing %s" cmd)
  8868. (shell-command cmd buf)
  8869. (if (featurep 'midnight)
  8870. (setq clean-buffer-list-kill-buffer-names
  8871. (cons buf clean-buffer-list-kill-buffer-names))))
  8872. (error "Abort"))))
  8873. ((string= type "elisp")
  8874. (let ((cmd path))
  8875. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8876. (string-match org-confirm-elisp-link-not-regexp cmd))
  8877. (not org-confirm-elisp-link-function)
  8878. (funcall org-confirm-elisp-link-function
  8879. (format "Execute \"%s\" as elisp? "
  8880. (org-add-props cmd nil
  8881. 'face 'org-warning))))
  8882. (message "%s => %s" cmd
  8883. (if (equal (string-to-char cmd) ?\()
  8884. (eval (read cmd))
  8885. (call-interactively (read cmd))))
  8886. (error "Abort"))))
  8887. ((and (string= type "thisfile")
  8888. (run-hook-with-args-until-success
  8889. 'org-open-link-functions path)))
  8890. ((string= type "thisfile")
  8891. (if arg
  8892. (switch-to-buffer-other-window
  8893. (org-get-buffer-for-internal-link (current-buffer)))
  8894. (org-mark-ring-push))
  8895. (let ((cmd `(org-link-search
  8896. ,path
  8897. ,(cond ((equal arg '(4)) ''occur)
  8898. ((equal arg '(16)) ''org-occur))
  8899. ,pos)))
  8900. (condition-case nil (let ((org-link-search-inhibit-query t))
  8901. (eval cmd))
  8902. (error (progn (widen) (eval cmd))))))
  8903. (t (browse-url-at-point)))))))
  8904. (move-marker org-open-link-marker nil)
  8905. (run-hook-with-args 'org-follow-link-hook)))
  8906. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  8907. "Offer links in the current entry and return the selected link.
  8908. If there is only one link, return it.
  8909. If NTH is an integer, return the NTH link found.
  8910. If ZERO is a string, check also this string for a link, and if
  8911. there is one, return it."
  8912. (with-current-buffer buffer
  8913. (save-excursion
  8914. (save-restriction
  8915. (widen)
  8916. (goto-char marker)
  8917. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8918. "\\(" org-angle-link-re "\\)\\|"
  8919. "\\(" org-plain-link-re "\\)"))
  8920. (cnt ?0)
  8921. (in-emacs (if (integerp nth) nil nth))
  8922. have-zero end links link c)
  8923. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8924. (push (match-string 0 zero) links)
  8925. (setq cnt (1- cnt) have-zero t))
  8926. (save-excursion
  8927. (org-back-to-heading t)
  8928. (setq end (save-excursion (outline-next-heading) (point)))
  8929. (while (re-search-forward re end t)
  8930. (push (match-string 0) links))
  8931. (setq links (org-uniquify (reverse links))))
  8932. (cond
  8933. ((null links)
  8934. (message "No links"))
  8935. ((equal (length links) 1)
  8936. (setq link (car links)))
  8937. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8938. (setq link (nth (if have-zero nth (1- nth)) links)))
  8939. (t ; we have to select a link
  8940. (save-excursion
  8941. (save-window-excursion
  8942. (delete-other-windows)
  8943. (with-output-to-temp-buffer "*Select Link*"
  8944. (mapc (lambda (l)
  8945. (if (not (string-match org-bracket-link-regexp l))
  8946. (princ (format "[%c] %s\n" (incf cnt)
  8947. (org-remove-angle-brackets l)))
  8948. (if (match-end 3)
  8949. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8950. (match-string 3 l) (match-string 1 l)))
  8951. (princ (format "[%c] %s\n" (incf cnt)
  8952. (match-string 1 l))))))
  8953. links))
  8954. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8955. (message "Select link to open, RET to open all:")
  8956. (setq c (read-char-exclusive))
  8957. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8958. (when (equal c ?q) (error "Abort"))
  8959. (if (equal c ?\C-m)
  8960. (setq link links)
  8961. (setq nth (- c ?0))
  8962. (if have-zero (setq nth (1+ nth)))
  8963. (unless (and (integerp nth) (>= (length links) nth))
  8964. (error "Invalid link selection"))
  8965. (setq link (nth (1- nth) links)))))
  8966. (cons link end))))))
  8967. ;; Add special file links that specify the way of opening
  8968. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8969. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8970. (defun org-open-file-with-system (path)
  8971. "Open file at PATH using the system way of opening it."
  8972. (org-open-file path 'system))
  8973. (defun org-open-file-with-emacs (path)
  8974. "Open file at PATH in Emacs."
  8975. (org-open-file path 'emacs))
  8976. (defun org-remove-file-link-modifiers ()
  8977. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8978. (goto-char (point-min))
  8979. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8980. (org-if-unprotected
  8981. (replace-match "file:" t t))))
  8982. (eval-after-load "org-exp"
  8983. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8984. 'org-remove-file-link-modifiers))
  8985. ;;; File search
  8986. (defvar org-create-file-search-functions nil
  8987. "List of functions to construct the right search string for a file link.
  8988. These functions are called in turn with point at the location to
  8989. which the link should point.
  8990. A function in the hook should first test if it would like to
  8991. handle this file type, for example by checking the `major-mode'
  8992. or the file extension. If it decides not to handle this file, it
  8993. should just return nil to give other functions a chance. If it
  8994. does handle the file, it must return the search string to be used
  8995. when following the link. The search string will be part of the
  8996. file link, given after a double colon, and `org-open-at-point'
  8997. will automatically search for it. If special measures must be
  8998. taken to make the search successful, another function should be
  8999. added to the companion hook `org-execute-file-search-functions',
  9000. which see.
  9001. A function in this hook may also use `setq' to set the variable
  9002. `description' to provide a suggestion for the descriptive text to
  9003. be used for this link when it gets inserted into an Org-mode
  9004. buffer with \\[org-insert-link].")
  9005. (defvar org-execute-file-search-functions nil
  9006. "List of functions to execute a file search triggered by a link.
  9007. Functions added to this hook must accept a single argument, the
  9008. search string that was part of the file link, the part after the
  9009. double colon. The function must first check if it would like to
  9010. handle this search, for example by checking the `major-mode' or
  9011. the file extension. If it decides not to handle this search, it
  9012. should just return nil to give other functions a chance. If it
  9013. does handle the search, it must return a non-nil value to keep
  9014. other functions from trying.
  9015. Each function can access the current prefix argument through the
  9016. variable `current-prefix-argument'. Note that a single prefix is
  9017. used to force opening a link in Emacs, so it may be good to only
  9018. use a numeric or double prefix to guide the search function.
  9019. In case this is needed, a function in this hook can also restore
  9020. the window configuration before `org-open-at-point' was called using:
  9021. (set-window-configuration org-window-config-before-follow-link)")
  9022. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9023. (defun org-link-search (s &optional type avoid-pos stealth)
  9024. "Search for a link search option.
  9025. If S is surrounded by forward slashes, it is interpreted as a
  9026. regular expression. In org-mode files, this will create an `org-occur'
  9027. sparse tree. In ordinary files, `occur' will be used to list matches.
  9028. If the current buffer is in `dired-mode', grep will be used to search
  9029. in all files. If AVOID-POS is given, ignore matches near that position.
  9030. When optional argument STEALTH is non-nil, do not modify
  9031. visibility around point, thus ignoring
  9032. `org-show-hierarchy-above', `org-show-following-heading' and
  9033. `org-show-siblings' variables."
  9034. (let ((case-fold-search t)
  9035. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9036. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9037. (append '(("") (" ") ("\t") ("\n"))
  9038. org-emphasis-alist)
  9039. "\\|") "\\)"))
  9040. (pos (point))
  9041. (pre nil) (post nil)
  9042. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9043. (cond
  9044. ;; First check if there are any special search functions
  9045. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9046. ;; Now try the builtin stuff
  9047. ((and (equal (string-to-char s0) ?#)
  9048. (> (length s0) 1)
  9049. (save-excursion
  9050. (goto-char (point-min))
  9051. (and
  9052. (re-search-forward
  9053. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9054. (setq type 'dedicated
  9055. pos (match-beginning 0))))
  9056. ;; There is an exact target for this
  9057. (goto-char pos)
  9058. (org-back-to-heading t)))
  9059. ((save-excursion
  9060. (goto-char (point-min))
  9061. (and
  9062. (re-search-forward
  9063. (concat "<<" (regexp-quote s0) ">>") nil t)
  9064. (setq type 'dedicated
  9065. pos (match-beginning 0))))
  9066. ;; There is an exact target for this
  9067. (goto-char pos))
  9068. ((save-excursion
  9069. (goto-char (point-min))
  9070. (and
  9071. (re-search-forward
  9072. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9073. (setq type 'dedicated pos (match-beginning 0))))
  9074. ;; Found an invisible target.
  9075. (goto-char pos))
  9076. ((save-excursion
  9077. (goto-char (point-min))
  9078. (and
  9079. (re-search-forward
  9080. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9081. (setq type 'dedicated pos (match-beginning 0))))
  9082. ;; Found an element with a matching #+name affiliated keyword.
  9083. (goto-char pos))
  9084. ((and (string-match "^(\\(.*\\))$" s0)
  9085. (save-excursion
  9086. (goto-char (point-min))
  9087. (and
  9088. (re-search-forward
  9089. (concat "[^[]" (regexp-quote
  9090. (format org-coderef-label-format
  9091. (match-string 1 s0))))
  9092. nil t)
  9093. (setq type 'dedicated
  9094. pos (1+ (match-beginning 0))))))
  9095. ;; There is a coderef target for this
  9096. (goto-char pos))
  9097. ((string-match "^/\\(.*\\)/$" s)
  9098. ;; A regular expression
  9099. (cond
  9100. ((derived-mode-p 'org-mode)
  9101. (org-occur (match-string 1 s)))
  9102. ;;((eq major-mode 'dired-mode)
  9103. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9104. (t (org-do-occur (match-string 1 s)))))
  9105. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9106. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9107. (goto-char (point-min))
  9108. (cond
  9109. ((let (case-fold-search)
  9110. (re-search-forward (format org-complex-heading-regexp-format
  9111. (regexp-quote s))
  9112. nil t))
  9113. ;; OK, found a match
  9114. (setq type 'dedicated)
  9115. (goto-char (match-beginning 0)))
  9116. ((and (not org-link-search-inhibit-query)
  9117. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9118. (y-or-n-p "No match - create this as a new heading? "))
  9119. (goto-char (point-max))
  9120. (or (bolp) (newline))
  9121. (insert "* " s "\n")
  9122. (beginning-of-line 0))
  9123. (t
  9124. (goto-char pos)
  9125. (error "No match"))))
  9126. (t
  9127. ;; A normal search string
  9128. (when (equal (string-to-char s) ?*)
  9129. ;; Anchor on headlines, post may include tags.
  9130. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9131. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9132. s (substring s 1)))
  9133. (remove-text-properties
  9134. 0 (length s)
  9135. '(face nil mouse-face nil keymap nil fontified nil) s)
  9136. ;; Make a series of regular expressions to find a match
  9137. (setq words (org-split-string s "[ \n\r\t]+")
  9138. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9139. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9140. "\\)" markers)
  9141. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9142. re2a (concat "[ \t\r\n]" re2a_)
  9143. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9144. re4 (concat "[^a-zA-Z_]" re4_)
  9145. re1 (concat pre re2 post)
  9146. re3 (concat pre (if pre re4_ re4) post)
  9147. re5 (concat pre ".*" re4)
  9148. re2 (concat pre re2)
  9149. re2a (concat pre (if pre re2a_ re2a))
  9150. re4 (concat pre (if pre re4_ re4))
  9151. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9152. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9153. re5 "\\)"
  9154. ))
  9155. (cond
  9156. ((eq type 'org-occur) (org-occur reall))
  9157. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9158. (t (goto-char (point-min))
  9159. (setq type 'fuzzy)
  9160. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9161. (org-search-not-self 1 re1 nil t)
  9162. (org-search-not-self 1 re2 nil t)
  9163. (org-search-not-self 1 re2a nil t)
  9164. (org-search-not-self 1 re3 nil t)
  9165. (org-search-not-self 1 re4 nil t)
  9166. (org-search-not-self 1 re5 nil t)
  9167. )
  9168. (goto-char (match-beginning 1))
  9169. (goto-char pos)
  9170. (error "No match"))))))
  9171. (and (derived-mode-p 'org-mode)
  9172. (not stealth)
  9173. (org-show-context 'link-search))
  9174. type))
  9175. (defun org-search-not-self (group &rest args)
  9176. "Execute `re-search-forward', but only accept matches that do not
  9177. enclose the position of `org-open-link-marker'."
  9178. (let ((m org-open-link-marker))
  9179. (catch 'exit
  9180. (while (apply 're-search-forward args)
  9181. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9182. (goto-char (match-end group))
  9183. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9184. (> (match-beginning 0) (marker-position m))
  9185. (< (match-end 0) (marker-position m)))
  9186. (save-match-data
  9187. (or (not (org-in-regexp
  9188. org-bracket-link-analytic-regexp 1))
  9189. (not (match-end 4)) ; no description
  9190. (and (<= (match-beginning 4) (point))
  9191. (>= (match-end 4) (point))))))
  9192. (throw 'exit (point))))))))
  9193. (defun org-get-buffer-for-internal-link (buffer)
  9194. "Return a buffer to be used for displaying the link target of internal links."
  9195. (cond
  9196. ((not org-display-internal-link-with-indirect-buffer)
  9197. buffer)
  9198. ((string-match "(Clone)$" (buffer-name buffer))
  9199. (message "Buffer is already a clone, not making another one")
  9200. ;; we also do not modify visibility in this case
  9201. buffer)
  9202. (t ; make a new indirect buffer for displaying the link
  9203. (let* ((bn (buffer-name buffer))
  9204. (ibn (concat bn "(Clone)"))
  9205. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9206. (with-current-buffer ib (org-overview))
  9207. ib))))
  9208. (defun org-do-occur (regexp &optional cleanup)
  9209. "Call the Emacs command `occur'.
  9210. If CLEANUP is non-nil, remove the printout of the regular expression
  9211. in the *Occur* buffer. This is useful if the regex is long and not useful
  9212. to read."
  9213. (occur regexp)
  9214. (when cleanup
  9215. (let ((cwin (selected-window)) win beg end)
  9216. (when (setq win (get-buffer-window "*Occur*"))
  9217. (select-window win))
  9218. (goto-char (point-min))
  9219. (when (re-search-forward "match[a-z]+" nil t)
  9220. (setq beg (match-end 0))
  9221. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9222. (setq end (1- (match-beginning 0)))))
  9223. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9224. (goto-char (point-min))
  9225. (select-window cwin))))
  9226. ;;; The mark ring for links jumps
  9227. (defvar org-mark-ring nil
  9228. "Mark ring for positions before jumps in Org-mode.")
  9229. (defvar org-mark-ring-last-goto nil
  9230. "Last position in the mark ring used to go back.")
  9231. ;; Fill and close the ring
  9232. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9233. (loop for i from 1 to org-mark-ring-length do
  9234. (push (make-marker) org-mark-ring))
  9235. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9236. org-mark-ring)
  9237. (defun org-mark-ring-push (&optional pos buffer)
  9238. "Put the current position or POS into the mark ring and rotate it."
  9239. (interactive)
  9240. (setq pos (or pos (point)))
  9241. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9242. (move-marker (car org-mark-ring)
  9243. (or pos (point))
  9244. (or buffer (current-buffer)))
  9245. (message "%s"
  9246. (substitute-command-keys
  9247. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9248. (defun org-mark-ring-goto (&optional n)
  9249. "Jump to the previous position in the mark ring.
  9250. With prefix arg N, jump back that many stored positions. When
  9251. called several times in succession, walk through the entire ring.
  9252. Org-mode commands jumping to a different position in the current file,
  9253. or to another Org-mode file, automatically push the old position
  9254. onto the ring."
  9255. (interactive "p")
  9256. (let (p m)
  9257. (if (eq last-command this-command)
  9258. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9259. (setq p org-mark-ring))
  9260. (setq org-mark-ring-last-goto p)
  9261. (setq m (car p))
  9262. (org-pop-to-buffer-same-window (marker-buffer m))
  9263. (goto-char m)
  9264. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9265. (defun org-remove-angle-brackets (s)
  9266. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9267. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9268. s)
  9269. (defun org-add-angle-brackets (s)
  9270. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9271. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9272. s)
  9273. (defun org-remove-double-quotes (s)
  9274. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9275. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9276. s)
  9277. ;;; Following specific links
  9278. (defun org-follow-timestamp-link ()
  9279. "Open an agenda view for the time-stamp date/range at point."
  9280. (cond
  9281. ((org-at-date-range-p t)
  9282. (let ((org-agenda-start-on-weekday)
  9283. (t1 (match-string 1))
  9284. (t2 (match-string 2)) tt1 tt2)
  9285. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9286. tt2 (time-to-days (org-time-string-to-time t2)))
  9287. (let ((org-agenda-buffer-tmp-name
  9288. (format "*Org Agenda(a:%s)"
  9289. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9290. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9291. ((org-at-timestamp-p t)
  9292. (let ((org-agenda-buffer-tmp-name
  9293. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9294. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9295. (substring (match-string 1) 0 10)))
  9296. 1)))
  9297. (t (error "This should not happen"))))
  9298. ;;; Following file links
  9299. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9300. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9301. (declare-function mailcap-mime-info
  9302. "mailcap" (string &optional request no-decode))
  9303. (defvar org-wait nil)
  9304. (defun org-open-file (path &optional in-emacs line search)
  9305. "Open the file at PATH.
  9306. First, this expands any special file name abbreviations. Then the
  9307. configuration variable `org-file-apps' is checked if it contains an
  9308. entry for this file type, and if yes, the corresponding command is launched.
  9309. If no application is found, Emacs simply visits the file.
  9310. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9311. With a double \\[universal-argument] \\[universal-argument] \
  9312. prefix arg, Org tries to avoid opening in Emacs
  9313. and to use an external application to visit the file.
  9314. Optional LINE specifies a line to go to, optional SEARCH a string
  9315. to search for. If LINE or SEARCH is given, the file will be
  9316. opened in Emacs, unless an entry from org-file-apps that makes
  9317. use of groups in a regexp matches.
  9318. If you want to change the way frames are used when following a
  9319. link, please customize `org-link-frame-setup'.
  9320. If the file does not exist, an error is thrown."
  9321. (let* ((file (if (equal path "")
  9322. buffer-file-name
  9323. (substitute-in-file-name (expand-file-name path))))
  9324. (file-apps (append org-file-apps (org-default-apps)))
  9325. (apps (org-remove-if
  9326. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9327. (apps-dlink (org-remove-if-not
  9328. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9329. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9330. (dirp (if remp nil (file-directory-p file)))
  9331. (file (if (and dirp org-open-directory-means-index-dot-org)
  9332. (concat (file-name-as-directory file) "index.org")
  9333. file))
  9334. (a-m-a-p (assq 'auto-mode apps))
  9335. (dfile (downcase file))
  9336. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9337. (link (cond ((and (eq line nil)
  9338. (eq search nil))
  9339. file)
  9340. (line
  9341. (concat file "::" (number-to-string line)))
  9342. (search
  9343. (concat file "::" search))))
  9344. (dlink (downcase link))
  9345. (old-buffer (current-buffer))
  9346. (old-pos (point))
  9347. (old-mode major-mode)
  9348. ext cmd link-match-data)
  9349. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9350. (setq ext (match-string 1 dfile))
  9351. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9352. (setq ext (match-string 1 dfile))))
  9353. (cond
  9354. ((member in-emacs '((16) system))
  9355. (setq cmd (cdr (assoc 'system apps))))
  9356. (in-emacs (setq cmd 'emacs))
  9357. (t
  9358. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9359. (and dirp (cdr (assoc 'directory apps)))
  9360. ; first, try matching against apps-dlink
  9361. ; if we get a match here, store the match data for later
  9362. (let ((match (assoc-default dlink apps-dlink
  9363. 'string-match)))
  9364. (if match
  9365. (progn (setq link-match-data (match-data))
  9366. match)
  9367. (progn (setq in-emacs (or in-emacs line search))
  9368. nil))) ; if we have no match in apps-dlink,
  9369. ; always open the file in emacs if line or search
  9370. ; is given (for backwards compatibility)
  9371. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9372. 'string-match)
  9373. (cdr (assoc ext apps))
  9374. (cdr (assoc t apps))))))
  9375. (when (eq cmd 'system)
  9376. (setq cmd (cdr (assoc 'system apps))))
  9377. (when (eq cmd 'default)
  9378. (setq cmd (cdr (assoc t apps))))
  9379. (when (eq cmd 'mailcap)
  9380. (require 'mailcap)
  9381. (mailcap-parse-mailcaps)
  9382. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9383. (command (mailcap-mime-info mime-type)))
  9384. (if (stringp command)
  9385. (setq cmd command)
  9386. (setq cmd 'emacs))))
  9387. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9388. (not (file-exists-p file))
  9389. (not org-open-non-existing-files))
  9390. (error "No such file: %s" file))
  9391. (cond
  9392. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9393. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9394. (while (string-match "['\"]%s['\"]" cmd)
  9395. (setq cmd (replace-match "%s" t t cmd)))
  9396. (while (string-match "%s" cmd)
  9397. (setq cmd (replace-match
  9398. (save-match-data
  9399. (shell-quote-argument
  9400. (convert-standard-filename file)))
  9401. t t cmd)))
  9402. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9403. (save-match-data
  9404. (let ((match-index 1)
  9405. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9406. (set-match-data link-match-data)
  9407. (while (<= match-index number-of-groups)
  9408. (let ((regex (concat "%" (number-to-string match-index)))
  9409. (replace-with (match-string match-index dlink)))
  9410. (while (string-match regex cmd)
  9411. (setq cmd (replace-match replace-with t t cmd))))
  9412. (setq match-index (+ match-index 1)))))
  9413. (save-window-excursion
  9414. (start-process-shell-command cmd nil cmd)
  9415. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9416. ))
  9417. ((or (stringp cmd)
  9418. (eq cmd 'emacs))
  9419. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9420. (widen)
  9421. (if line (org-goto-line line)
  9422. (if search (org-link-search search))))
  9423. ((consp cmd)
  9424. (let ((file (convert-standard-filename file)))
  9425. (save-match-data
  9426. (set-match-data link-match-data)
  9427. (eval cmd))))
  9428. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9429. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9430. (or (not (equal old-buffer (current-buffer)))
  9431. (not (equal old-pos (point))))
  9432. (org-mark-ring-push old-pos old-buffer))))
  9433. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9434. "This function returns non-nil if `entry' uses a regular
  9435. expression which should be matched against the whole link by
  9436. org-open-file.
  9437. It assumes that is the case when the entry uses a regular
  9438. expression which has at least one grouping construct and the
  9439. action is either a lisp form or a command string containing
  9440. '%1', i.e. using at least one subexpression match as a
  9441. parameter."
  9442. (let ((selector (car entry))
  9443. (action (cdr entry)))
  9444. (if (stringp selector)
  9445. (and (> (regexp-opt-depth selector) 0)
  9446. (or (and (stringp action)
  9447. (string-match "%[0-9]" action))
  9448. (consp action)))
  9449. nil)))
  9450. (defun org-default-apps ()
  9451. "Return the default applications for this operating system."
  9452. (cond
  9453. ((eq system-type 'darwin)
  9454. org-file-apps-defaults-macosx)
  9455. ((eq system-type 'windows-nt)
  9456. org-file-apps-defaults-windowsnt)
  9457. (t org-file-apps-defaults-gnu)))
  9458. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9459. "Convert extensions to regular expressions in the cars of LIST.
  9460. Also, weed out any non-string entries, because the return value is used
  9461. only for regexp matching.
  9462. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9463. point to the symbol `emacs', indicating that the file should
  9464. be opened in Emacs."
  9465. (append
  9466. (delq nil
  9467. (mapcar (lambda (x)
  9468. (if (not (stringp (car x)))
  9469. nil
  9470. (if (string-match "\\W" (car x))
  9471. x
  9472. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9473. list))
  9474. (if add-auto-mode
  9475. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9476. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9477. (defun org-file-remote-p (file)
  9478. "Test whether FILE specifies a location on a remote system.
  9479. Return non-nil if the location is indeed remote.
  9480. For example, the filename \"/user@host:/foo\" specifies a location
  9481. on the system \"/user@host:\"."
  9482. (cond ((fboundp 'file-remote-p)
  9483. (file-remote-p file))
  9484. ((fboundp 'tramp-handle-file-remote-p)
  9485. (tramp-handle-file-remote-p file))
  9486. ((and (boundp 'ange-ftp-name-format)
  9487. (string-match (car ange-ftp-name-format) file))
  9488. t)))
  9489. ;;;; Refiling
  9490. (defun org-get-org-file ()
  9491. "Read a filename, with default directory `org-directory'."
  9492. (let ((default (or org-default-notes-file remember-data-file)))
  9493. (read-file-name (format "File name [%s]: " default)
  9494. (file-name-as-directory org-directory)
  9495. default)))
  9496. (defun org-notes-order-reversed-p ()
  9497. "Check if the current file should receive notes in reversed order."
  9498. (cond
  9499. ((not org-reverse-note-order) nil)
  9500. ((eq t org-reverse-note-order) t)
  9501. ((not (listp org-reverse-note-order)) nil)
  9502. (t (catch 'exit
  9503. (let ((all org-reverse-note-order)
  9504. entry)
  9505. (while (setq entry (pop all))
  9506. (if (string-match (car entry) buffer-file-name)
  9507. (throw 'exit (cdr entry))))
  9508. nil)))))
  9509. (defvar org-refile-target-table nil
  9510. "The list of refile targets, created by `org-refile'.")
  9511. (defvar org-agenda-new-buffers nil
  9512. "Buffers created to visit agenda files.")
  9513. (defvar org-refile-cache nil
  9514. "Cache for refile targets.")
  9515. (defvar org-refile-markers nil
  9516. "All the markers used for caching refile locations.")
  9517. (defun org-refile-marker (pos)
  9518. "Get a new refile marker, but only if caching is in use."
  9519. (if (not org-refile-use-cache)
  9520. pos
  9521. (let ((m (make-marker)))
  9522. (move-marker m pos)
  9523. (push m org-refile-markers)
  9524. m)))
  9525. (defun org-refile-cache-clear ()
  9526. "Clear the refile cache and disable all the markers."
  9527. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9528. (setq org-refile-markers nil)
  9529. (setq org-refile-cache nil)
  9530. (message "Refile cache has been cleared"))
  9531. (defun org-refile-cache-check-set (set)
  9532. "Check if all the markers in the cache still have live buffers."
  9533. (let (marker)
  9534. (catch 'exit
  9535. (while (and set (setq marker (nth 3 (pop set))))
  9536. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9537. (when (and marker (null (marker-buffer marker)))
  9538. (message "not found") (sit-for 3)
  9539. (throw 'exit nil)))
  9540. t)))
  9541. (defun org-refile-cache-put (set &rest identifiers)
  9542. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9543. (let* ((key (sha1 (prin1-to-string identifiers)))
  9544. (entry (assoc key org-refile-cache)))
  9545. (if entry
  9546. (setcdr entry set)
  9547. (push (cons key set) org-refile-cache))))
  9548. (defun org-refile-cache-get (&rest identifiers)
  9549. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9550. (cond
  9551. ((not org-refile-cache) nil)
  9552. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9553. (t
  9554. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9555. org-refile-cache))))
  9556. (and set (org-refile-cache-check-set set) set)))))
  9557. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9558. "Produce a table with refile targets."
  9559. (let ((case-fold-search nil)
  9560. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9561. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9562. targets tgs txt re files f desc descre fast-path-p level pos0)
  9563. (message "Getting targets...")
  9564. (with-current-buffer (or default-buffer (current-buffer))
  9565. (while (setq entry (pop entries))
  9566. (setq files (car entry) desc (cdr entry))
  9567. (setq fast-path-p nil)
  9568. (cond
  9569. ((null files) (setq files (list (current-buffer))))
  9570. ((eq files 'org-agenda-files)
  9571. (setq files (org-agenda-files 'unrestricted)))
  9572. ((and (symbolp files) (fboundp files))
  9573. (setq files (funcall files)))
  9574. ((and (symbolp files) (boundp files))
  9575. (setq files (symbol-value files))))
  9576. (if (stringp files) (setq files (list files)))
  9577. (cond
  9578. ((eq (car desc) :tag)
  9579. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9580. ((eq (car desc) :todo)
  9581. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9582. ((eq (car desc) :regexp)
  9583. (setq descre (cdr desc)))
  9584. ((eq (car desc) :level)
  9585. (setq descre (concat "^\\*\\{" (number-to-string
  9586. (if org-odd-levels-only
  9587. (1- (* 2 (cdr desc)))
  9588. (cdr desc)))
  9589. "\\}[ \t]")))
  9590. ((eq (car desc) :maxlevel)
  9591. (setq fast-path-p t)
  9592. (setq descre (concat "^\\*\\{1," (number-to-string
  9593. (if org-odd-levels-only
  9594. (1- (* 2 (cdr desc)))
  9595. (cdr desc)))
  9596. "\\}[ \t]")))
  9597. (t (error "Bad refiling target description %s" desc)))
  9598. (while (setq f (pop files))
  9599. (with-current-buffer
  9600. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9601. (or
  9602. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9603. (progn
  9604. (if (bufferp f) (setq f (buffer-file-name
  9605. (buffer-base-buffer f))))
  9606. (setq f (and f (expand-file-name f)))
  9607. (if (eq org-refile-use-outline-path 'file)
  9608. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9609. (save-excursion
  9610. (save-restriction
  9611. (widen)
  9612. (goto-char (point-min))
  9613. (while (re-search-forward descre nil t)
  9614. (goto-char (setq pos0 (point-at-bol)))
  9615. (catch 'next
  9616. (when org-refile-target-verify-function
  9617. (save-match-data
  9618. (or (funcall org-refile-target-verify-function)
  9619. (throw 'next t))))
  9620. (when (and (looking-at org-complex-heading-regexp)
  9621. (not (member (match-string 4) excluded-entries))
  9622. (match-string 4))
  9623. (setq level (org-reduced-level
  9624. (- (match-end 1) (match-beginning 1)))
  9625. txt (org-link-display-format (match-string 4))
  9626. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9627. re (format org-complex-heading-regexp-format
  9628. (regexp-quote (match-string 4))))
  9629. (when org-refile-use-outline-path
  9630. (setq txt (mapconcat
  9631. 'org-protect-slash
  9632. (append
  9633. (if (eq org-refile-use-outline-path
  9634. 'file)
  9635. (list (file-name-nondirectory
  9636. (buffer-file-name
  9637. (buffer-base-buffer))))
  9638. (if (eq org-refile-use-outline-path
  9639. 'full-file-path)
  9640. (list (buffer-file-name
  9641. (buffer-base-buffer)))))
  9642. (org-get-outline-path fast-path-p
  9643. level txt)
  9644. (list txt))
  9645. "/")))
  9646. (push (list txt f re (org-refile-marker (point)))
  9647. tgs)))
  9648. (when (= (point) pos0)
  9649. ;; verification function has not moved point
  9650. (goto-char (point-at-eol))))))))
  9651. (when org-refile-use-cache
  9652. (org-refile-cache-put tgs (buffer-file-name) descre))
  9653. (setq targets (append tgs targets))
  9654. ))))
  9655. (message "Getting targets...done")
  9656. (nreverse targets)))
  9657. (defun org-protect-slash (s)
  9658. (while (string-match "/" s)
  9659. (setq s (replace-match "\\" t t s)))
  9660. s)
  9661. (defvar org-olpa (make-vector 20 nil))
  9662. (defun org-get-outline-path (&optional fastp level heading)
  9663. "Return the outline path to the current entry, as a list.
  9664. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9665. routine which makes outline path derivations for an entire file,
  9666. avoiding backtracing. Refile target collection makes use of that."
  9667. (if fastp
  9668. (progn
  9669. (if (> level 19)
  9670. (error "Outline path failure, more than 19 levels"))
  9671. (loop for i from level upto 19 do
  9672. (aset org-olpa i nil))
  9673. (prog1
  9674. (delq nil (append org-olpa nil))
  9675. (aset org-olpa level heading)))
  9676. (let (rtn case-fold-search)
  9677. (save-excursion
  9678. (save-restriction
  9679. (widen)
  9680. (while (org-up-heading-safe)
  9681. (when (looking-at org-complex-heading-regexp)
  9682. (push (org-match-string-no-properties 4) rtn)))
  9683. rtn)))))
  9684. (defun org-format-outline-path (path &optional width prefix)
  9685. "Format the outline path PATH for display.
  9686. Width is the maximum number of characters that is available.
  9687. Prefix is a prefix to be included in the returned string,
  9688. such as the file name."
  9689. (setq width (or width 79))
  9690. (if prefix (setq width (- width (length prefix))))
  9691. (if (not path)
  9692. (or prefix "")
  9693. (let* ((nsteps (length path))
  9694. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9695. (maxwidth (if (<= total-width width)
  9696. 10000 ;; everything fits
  9697. ;; we need to shorten the level headings
  9698. (/ (- width nsteps) nsteps)))
  9699. (org-odd-levels-only nil)
  9700. (n 0)
  9701. (total (1+ (length prefix))))
  9702. (setq maxwidth (max maxwidth 10))
  9703. (concat prefix
  9704. (mapconcat
  9705. (lambda (h)
  9706. (setq n (1+ n))
  9707. (if (and (= n nsteps) (< maxwidth 10000))
  9708. (setq maxwidth (- total-width total)))
  9709. (if (< (length h) maxwidth)
  9710. (progn (setq total (+ total (length h) 1)) h)
  9711. (setq h (substring h 0 (- maxwidth 2))
  9712. total (+ total maxwidth 1))
  9713. (if (string-match "[ \t]+\\'" h)
  9714. (setq h (substring h 0 (match-beginning 0))))
  9715. (setq h (concat h "..")))
  9716. (org-add-props h nil 'face
  9717. (nth (% (1- n) org-n-level-faces)
  9718. org-level-faces))
  9719. h)
  9720. path "/")))))
  9721. (defun org-display-outline-path (&optional file current)
  9722. "Display the current outline path in the echo area."
  9723. (interactive "P")
  9724. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9725. (case-fold-search nil)
  9726. (path (and (derived-mode-p 'org-mode) (org-get-outline-path))))
  9727. (if current (setq path (append path
  9728. (save-excursion
  9729. (org-back-to-heading t)
  9730. (if (looking-at org-complex-heading-regexp)
  9731. (list (match-string 4)))))))
  9732. (message "%s"
  9733. (org-format-outline-path
  9734. path
  9735. (1- (frame-width))
  9736. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9737. (defvar org-refile-history nil
  9738. "History for refiling operations.")
  9739. (defvar org-after-refile-insert-hook nil
  9740. "Hook run after `org-refile' has inserted its stuff at the new location.
  9741. Note that this is still *before* the stuff will be removed from
  9742. the *old* location.")
  9743. (defvar org-capture-last-stored-marker)
  9744. (defun org-refile (&optional goto default-buffer rfloc)
  9745. "Move the entry or entries at point to another heading.
  9746. The list of target headings is compiled using the information in
  9747. `org-refile-targets', which see.
  9748. At the target location, the entry is filed as a subitem of the target
  9749. heading. Depending on `org-reverse-note-order', the new subitem will
  9750. either be the first or the last subitem.
  9751. If there is an active region, all entries in that region will be moved.
  9752. However, the region must fulfill the requirement that the first heading
  9753. is the first one sets the top-level of the moved text - at most siblings
  9754. below it are allowed.
  9755. With prefix arg GOTO, the command will only visit the target location
  9756. and not actually move anything.
  9757. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9758. go to the location where the last refiling operation has put the subtree.
  9759. With a prefix argument of `2', refile to the running clock.
  9760. RFLOC can be a refile location obtained in a different way.
  9761. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9762. If you are using target caching (see `org-refile-use-cache'),
  9763. you have to clear the target cache in order to find new targets.
  9764. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9765. prefix argument (`C-u C-u C-u C-c C-w')."
  9766. (interactive "P")
  9767. (if (member goto '(0 (64)))
  9768. (org-refile-cache-clear)
  9769. (let* ((cbuf (current-buffer))
  9770. (regionp (org-region-active-p))
  9771. (region-start (and regionp (region-beginning)))
  9772. (region-end (and regionp (region-end)))
  9773. (region-length (and regionp (- region-end region-start)))
  9774. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9775. pos it nbuf file re level reversed)
  9776. (setq last-command nil)
  9777. (when regionp
  9778. (goto-char region-start)
  9779. (or (bolp) (goto-char (point-at-bol)))
  9780. (setq region-start (point))
  9781. (unless (or (org-kill-is-subtree-p
  9782. (buffer-substring region-start region-end))
  9783. (prog1 org-refile-active-region-within-subtree
  9784. (org-toggle-heading)))
  9785. (error "The region is not a (sequence of) subtree(s)")))
  9786. (if (equal goto '(16))
  9787. (org-refile-goto-last-stored)
  9788. (when (or
  9789. (and (equal goto 2)
  9790. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9791. (prog1
  9792. (setq it (list (or org-clock-heading "running clock")
  9793. (buffer-file-name
  9794. (marker-buffer org-clock-hd-marker))
  9795. ""
  9796. (marker-position org-clock-hd-marker)))
  9797. (setq goto nil)))
  9798. (setq it (or rfloc
  9799. (let (heading-text)
  9800. (save-excursion
  9801. (unless goto
  9802. (org-back-to-heading t)
  9803. (setq heading-text
  9804. (nth 4 (org-heading-components))))
  9805. (org-refile-get-location
  9806. (cond (goto "Goto")
  9807. (regionp "Refile region to")
  9808. (t (concat "Refile subtree \""
  9809. heading-text "\" to")))
  9810. default-buffer
  9811. (and (not (equal '(4) goto))
  9812. org-refile-allow-creating-parent-nodes)
  9813. goto))))))
  9814. (setq file (nth 1 it)
  9815. re (nth 2 it)
  9816. pos (nth 3 it))
  9817. (if (and (not goto)
  9818. pos
  9819. (equal (buffer-file-name) file)
  9820. (if regionp
  9821. (and (>= pos region-start)
  9822. (<= pos region-end))
  9823. (and (>= pos (point))
  9824. (< pos (save-excursion
  9825. (org-end-of-subtree t t))))))
  9826. (error "Cannot refile to position inside the tree or region"))
  9827. (setq nbuf (or (find-buffer-visiting file)
  9828. (find-file-noselect file)))
  9829. (if goto
  9830. (progn
  9831. (org-pop-to-buffer-same-window nbuf)
  9832. (goto-char pos)
  9833. (org-show-context 'org-goto))
  9834. (if regionp
  9835. (progn
  9836. (org-kill-new (buffer-substring region-start region-end))
  9837. (org-save-markers-in-region region-start region-end))
  9838. (org-copy-subtree 1 nil t))
  9839. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9840. (find-file-noselect file)))
  9841. (setq reversed (org-notes-order-reversed-p))
  9842. (save-excursion
  9843. (save-restriction
  9844. (widen)
  9845. (if pos
  9846. (progn
  9847. (goto-char pos)
  9848. (looking-at org-outline-regexp)
  9849. (setq level (org-get-valid-level (funcall outline-level) 1))
  9850. (goto-char
  9851. (if reversed
  9852. (or (outline-next-heading) (point-max))
  9853. (or (save-excursion (org-get-next-sibling))
  9854. (org-end-of-subtree t t)
  9855. (point-max)))))
  9856. (setq level 1)
  9857. (if (not reversed)
  9858. (goto-char (point-max))
  9859. (goto-char (point-min))
  9860. (or (outline-next-heading) (goto-char (point-max)))))
  9861. (if (not (bolp)) (newline))
  9862. (org-paste-subtree level)
  9863. (when org-log-refile
  9864. (org-add-log-setup 'refile nil nil 'findpos
  9865. org-log-refile)
  9866. (unless (eq org-log-refile 'note)
  9867. (save-excursion (org-add-log-note))))
  9868. (and org-auto-align-tags
  9869. (let ((org-loop-over-headlines-in-active-region nil))
  9870. (org-set-tags nil t)))
  9871. (with-demoted-errors
  9872. (bookmark-set "org-refile-last-stored"))
  9873. ;; If we are refiling for capture, make sure that the
  9874. ;; last-capture pointers point here
  9875. (when (org-bound-and-true-p org-refile-for-capture)
  9876. (with-demoted-errors
  9877. (bookmark-set "org-capture-last-stored-marker"))
  9878. (move-marker org-capture-last-stored-marker (point)))
  9879. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9880. (run-hooks 'org-after-refile-insert-hook))))
  9881. (if regionp
  9882. (delete-region (point) (+ (point) region-length))
  9883. (org-cut-subtree))
  9884. (when (featurep 'org-inlinetask)
  9885. (org-inlinetask-remove-END-maybe))
  9886. (setq org-markers-to-move nil)
  9887. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9888. (defun org-refile-goto-last-stored ()
  9889. "Go to the location where the last refile was stored."
  9890. (interactive)
  9891. (bookmark-jump "org-refile-last-stored")
  9892. (message "This is the location of the last refile"))
  9893. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9894. no-exclude)
  9895. "Prompt the user for a refile location, using PROMPT.
  9896. PROMPT should not be suffixed with a colon and a space, because
  9897. this function appends the default value from
  9898. `org-refile-history' automatically, if that is not empty.
  9899. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9900. this is used for the GOTO interface."
  9901. (let ((org-refile-targets org-refile-targets)
  9902. (org-refile-use-outline-path org-refile-use-outline-path)
  9903. excluded-entries)
  9904. (when (and (derived-mode-p 'org-mode)
  9905. (not org-refile-use-cache)
  9906. (not no-exclude))
  9907. (org-map-tree
  9908. (lambda()
  9909. (setq excluded-entries
  9910. (append excluded-entries (list (org-get-heading t t)))))))
  9911. (setq org-refile-target-table
  9912. (org-refile-get-targets default-buffer excluded-entries)))
  9913. (unless org-refile-target-table
  9914. (error "No refile targets"))
  9915. (let* ((prompt (concat prompt
  9916. (and (car org-refile-history)
  9917. (concat " (default " (car org-refile-history) ")"))
  9918. ": "))
  9919. (cbuf (current-buffer))
  9920. (partial-completion-mode nil)
  9921. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9922. (cfunc (if (and org-refile-use-outline-path
  9923. org-outline-path-complete-in-steps)
  9924. 'org-olpath-completing-read
  9925. 'org-icompleting-read))
  9926. (extra (if org-refile-use-outline-path "/" ""))
  9927. (filename (and cfn (expand-file-name cfn)))
  9928. (tbl (mapcar
  9929. (lambda (x)
  9930. (if (and (not (member org-refile-use-outline-path
  9931. '(file full-file-path)))
  9932. (not (equal filename (nth 1 x))))
  9933. (cons (concat (car x) extra " ("
  9934. (file-name-nondirectory (nth 1 x)) ")")
  9935. (cdr x))
  9936. (cons (concat (car x) extra) (cdr x))))
  9937. org-refile-target-table))
  9938. (completion-ignore-case t)
  9939. pa answ parent-target child parent old-hist)
  9940. (setq old-hist org-refile-history)
  9941. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9942. nil 'org-refile-history (car org-refile-history)))
  9943. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9944. (org-refile-check-position pa)
  9945. (if pa
  9946. (progn
  9947. (when (or (not org-refile-history)
  9948. (not (eq old-hist org-refile-history))
  9949. (not (equal (car pa) (car org-refile-history))))
  9950. (setq org-refile-history
  9951. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9952. org-refile-history
  9953. (cdr org-refile-history))))
  9954. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9955. (pop org-refile-history)))
  9956. pa)
  9957. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9958. (progn
  9959. (setq parent (match-string 1 answ)
  9960. child (match-string 2 answ))
  9961. (setq parent-target (or (assoc parent tbl)
  9962. (assoc (concat parent "/") tbl)))
  9963. (when (and parent-target
  9964. (or (eq new-nodes t)
  9965. (and (eq new-nodes 'confirm)
  9966. (y-or-n-p (format "Create new node \"%s\"? "
  9967. child)))))
  9968. (org-refile-new-child parent-target child)))
  9969. (error "Invalid target location")))))
  9970. (declare-function org-string-nw-p "org-macs" (s))
  9971. (defun org-refile-check-position (refile-pointer)
  9972. "Check if the refile pointer matches the headline to which it points."
  9973. (let* ((file (nth 1 refile-pointer))
  9974. (re (nth 2 refile-pointer))
  9975. (pos (nth 3 refile-pointer))
  9976. buffer)
  9977. (if (and (not (markerp pos)) (not file))
  9978. (error "Please save the buffer to a file before refiling")
  9979. (when (org-string-nw-p re)
  9980. (setq buffer (if (markerp pos)
  9981. (marker-buffer pos)
  9982. (or (find-buffer-visiting file)
  9983. (find-file-noselect file))))
  9984. (with-current-buffer buffer
  9985. (save-excursion
  9986. (save-restriction
  9987. (widen)
  9988. (goto-char pos)
  9989. (beginning-of-line 1)
  9990. (unless (org-looking-at-p re)
  9991. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  9992. (defun org-refile-new-child (parent-target child)
  9993. "Use refile target PARENT-TARGET to add new CHILD below it."
  9994. (unless parent-target
  9995. (error "Cannot find parent for new node"))
  9996. (let ((file (nth 1 parent-target))
  9997. (pos (nth 3 parent-target))
  9998. level)
  9999. (with-current-buffer (or (find-buffer-visiting file)
  10000. (find-file-noselect file))
  10001. (save-excursion
  10002. (save-restriction
  10003. (widen)
  10004. (if pos
  10005. (goto-char pos)
  10006. (goto-char (point-max))
  10007. (if (not (bolp)) (newline)))
  10008. (when (looking-at org-outline-regexp)
  10009. (setq level (funcall outline-level))
  10010. (org-end-of-subtree t t))
  10011. (org-back-over-empty-lines)
  10012. (insert "\n" (make-string
  10013. (if pos (org-get-valid-level level 1) 1) ?*)
  10014. " " child "\n")
  10015. (beginning-of-line 0)
  10016. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10017. (defun org-olpath-completing-read (prompt collection &rest args)
  10018. "Read an outline path like a file name."
  10019. (let ((thetable collection)
  10020. (org-completion-use-ido nil) ; does not work with ido.
  10021. (org-completion-use-iswitchb nil)) ; or iswitchb
  10022. (apply
  10023. 'org-icompleting-read prompt
  10024. (lambda (string predicate &optional flag)
  10025. (let (rtn r f (l (length string)))
  10026. (cond
  10027. ((eq flag nil)
  10028. ;; try completion
  10029. (try-completion string thetable))
  10030. ((eq flag t)
  10031. ;; all-completions
  10032. (setq rtn (all-completions string thetable predicate))
  10033. (mapcar
  10034. (lambda (x)
  10035. (setq r (substring x l))
  10036. (if (string-match " ([^)]*)$" x)
  10037. (setq f (match-string 0 x))
  10038. (setq f ""))
  10039. (if (string-match "/" r)
  10040. (concat string (substring r 0 (match-end 0)) f)
  10041. x))
  10042. rtn))
  10043. ((eq flag 'lambda)
  10044. ;; exact match?
  10045. (assoc string thetable)))))
  10046. args)))
  10047. ;;;; Dynamic blocks
  10048. (defun org-find-dblock (name)
  10049. "Find the first dynamic block with name NAME in the buffer.
  10050. If not found, stay at current position and return nil."
  10051. (let ((case-fold-search t) pos)
  10052. (save-excursion
  10053. (goto-char (point-min))
  10054. (setq pos (and (re-search-forward
  10055. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10056. (match-beginning 0))))
  10057. (if pos (goto-char pos))
  10058. pos))
  10059. (defconst org-dblock-start-re
  10060. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10061. "Matches the start line of a dynamic block, with parameters.")
  10062. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10063. "Matches the end of a dynamic block.")
  10064. (defun org-create-dblock (plist)
  10065. "Create a dynamic block section, with parameters taken from PLIST.
  10066. PLIST must contain a :name entry which is used as name of the block."
  10067. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10068. (end-of-line 1)
  10069. (newline))
  10070. (let ((col (current-column))
  10071. (name (plist-get plist :name)))
  10072. (insert "#+BEGIN: " name)
  10073. (while plist
  10074. (if (eq (car plist) :name)
  10075. (setq plist (cddr plist))
  10076. (insert " " (prin1-to-string (pop plist)))))
  10077. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10078. (beginning-of-line -2)))
  10079. (defun org-prepare-dblock ()
  10080. "Prepare dynamic block for refresh.
  10081. This empties the block, puts the cursor at the insert position and returns
  10082. the property list including an extra property :name with the block name."
  10083. (unless (looking-at org-dblock-start-re)
  10084. (error "Not at a dynamic block"))
  10085. (let* ((begdel (1+ (match-end 0)))
  10086. (name (org-no-properties (match-string 1)))
  10087. (params (append (list :name name)
  10088. (read (concat "(" (match-string 3) ")")))))
  10089. (save-excursion
  10090. (beginning-of-line 1)
  10091. (skip-chars-forward " \t")
  10092. (setq params (plist-put params :indentation-column (current-column))))
  10093. (unless (re-search-forward org-dblock-end-re nil t)
  10094. (error "Dynamic block not terminated"))
  10095. (setq params
  10096. (append params
  10097. (list :content (buffer-substring
  10098. begdel (match-beginning 0)))))
  10099. (delete-region begdel (match-beginning 0))
  10100. (goto-char begdel)
  10101. (open-line 1)
  10102. params))
  10103. (defun org-map-dblocks (&optional command)
  10104. "Apply COMMAND to all dynamic blocks in the current buffer.
  10105. If COMMAND is not given, use `org-update-dblock'."
  10106. (let ((cmd (or command 'org-update-dblock)))
  10107. (save-excursion
  10108. (goto-char (point-min))
  10109. (while (re-search-forward org-dblock-start-re nil t)
  10110. (goto-char (match-beginning 0))
  10111. (save-excursion
  10112. (condition-case nil
  10113. (funcall cmd)
  10114. (error (message "Error during update of dynamic block"))))
  10115. (unless (re-search-forward org-dblock-end-re nil t)
  10116. (error "Dynamic block not terminated"))))))
  10117. (defun org-dblock-update (&optional arg)
  10118. "User command for updating dynamic blocks.
  10119. Update the dynamic block at point. With prefix ARG, update all dynamic
  10120. blocks in the buffer."
  10121. (interactive "P")
  10122. (if arg
  10123. (org-update-all-dblocks)
  10124. (or (looking-at org-dblock-start-re)
  10125. (org-beginning-of-dblock))
  10126. (org-update-dblock)))
  10127. (defun org-update-dblock ()
  10128. "Update the dynamic block at point.
  10129. This means to empty the block, parse for parameters and then call
  10130. the correct writing function."
  10131. (interactive)
  10132. (save-window-excursion
  10133. (let* ((pos (point))
  10134. (line (org-current-line))
  10135. (params (org-prepare-dblock))
  10136. (name (plist-get params :name))
  10137. (indent (plist-get params :indentation-column))
  10138. (cmd (intern (concat "org-dblock-write:" name))))
  10139. (message "Updating dynamic block `%s' at line %d..." name line)
  10140. (funcall cmd params)
  10141. (message "Updating dynamic block `%s' at line %d...done" name line)
  10142. (goto-char pos)
  10143. (when (and indent (> indent 0))
  10144. (setq indent (make-string indent ?\ ))
  10145. (save-excursion
  10146. (org-beginning-of-dblock)
  10147. (forward-line 1)
  10148. (while (not (looking-at org-dblock-end-re))
  10149. (insert indent)
  10150. (beginning-of-line 2))
  10151. (when (looking-at org-dblock-end-re)
  10152. (and (looking-at "[ \t]+")
  10153. (replace-match ""))
  10154. (insert indent)))))))
  10155. (defun org-beginning-of-dblock ()
  10156. "Find the beginning of the dynamic block at point.
  10157. Error if there is no such block at point."
  10158. (let ((pos (point))
  10159. beg)
  10160. (end-of-line 1)
  10161. (if (and (re-search-backward org-dblock-start-re nil t)
  10162. (setq beg (match-beginning 0))
  10163. (re-search-forward org-dblock-end-re nil t)
  10164. (> (match-end 0) pos))
  10165. (goto-char beg)
  10166. (goto-char pos)
  10167. (error "Not in a dynamic block"))))
  10168. (defun org-update-all-dblocks ()
  10169. "Update all dynamic blocks in the buffer.
  10170. This function can be used in a hook."
  10171. (interactive)
  10172. (when (derived-mode-p 'org-mode)
  10173. (org-map-dblocks 'org-update-dblock)))
  10174. ;;;; Completion
  10175. (defconst org-additional-option-like-keywords
  10176. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10177. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10178. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10179. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10180. "BEGIN:" "END:"
  10181. "ORGTBL" "TBLFM:" "TBLNAME:"
  10182. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10183. "BEGIN_VERBATIM" "END_VERBATIM"
  10184. "BEGIN_QUOTE" "END_QUOTE"
  10185. "BEGIN_VERSE" "END_VERSE"
  10186. "BEGIN_CENTER" "END_CENTER"
  10187. "BEGIN_SRC" "END_SRC"
  10188. "BEGIN_RESULT" "END_RESULT"
  10189. "BEGIN_lstlisting" "END_lstlisting"
  10190. "NAME:" "RESULTS:"
  10191. "HEADER:" "HEADERS:"
  10192. "COLUMNS:" "PROPERTY:"
  10193. "CAPTION:" "LABEL:"
  10194. "SETUPFILE:"
  10195. "INCLUDE:" "INDEX:"
  10196. "BIND:"
  10197. "MACRO:"))
  10198. (defconst org-options-keywords
  10199. '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
  10200. "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
  10201. "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
  10202. "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
  10203. "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
  10204. "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:" "STYLE:"
  10205. "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
  10206. (defconst org-additional-option-like-keywords-for-flyspell
  10207. (delete-dups
  10208. (split-string
  10209. (mapconcat (lambda(k)
  10210. (replace-regexp-in-string
  10211. "_\\|:" " "
  10212. (concat k " " (downcase k) " " (upcase k))))
  10213. (append org-options-keywords org-additional-option-like-keywords)
  10214. " ")
  10215. " +" t)))
  10216. (defcustom org-structure-template-alist
  10217. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10218. "<src lang=\"?\">\n\n</src>")
  10219. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10220. "<example>\n?\n</example>")
  10221. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10222. "<quote>\n?\n</quote>")
  10223. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10224. "<verse>\n?\n</verse>")
  10225. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10226. "<verbatim>\n?\n</verbatim>")
  10227. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10228. "<center>\n?\n</center>")
  10229. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10230. "<literal style=\"latex\">\n?\n</literal>")
  10231. ("L" "#+LaTeX: "
  10232. "<literal style=\"latex\">?</literal>")
  10233. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10234. "<literal style=\"html\">\n?\n</literal>")
  10235. ("H" "#+HTML: "
  10236. "<literal style=\"html\">?</literal>")
  10237. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10238. ("A" "#+ASCII: ")
  10239. ("i" "#+INDEX: ?"
  10240. "#+INDEX: ?")
  10241. ("I" "#+INCLUDE: %file ?"
  10242. "<include file=%file markup=\"?\">"))
  10243. "Structure completion elements.
  10244. This is a list of abbreviation keys and values. The value gets inserted
  10245. if you type `<' followed by the key and then press the completion key,
  10246. usually `M-TAB'. %file will be replaced by a file name after prompting
  10247. for the file using completion. The cursor will be placed at the position
  10248. of the `?` in the template.
  10249. There are two templates for each key, the first uses the original Org syntax,
  10250. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10251. the default when the /org-mtags.el/ module has been loaded. See also the
  10252. variable `org-mtags-prefer-muse-templates'."
  10253. :group 'org-completion
  10254. :type '(repeat
  10255. (string :tag "Key")
  10256. (string :tag "Template")
  10257. (string :tag "Muse Template")))
  10258. (defun org-try-structure-completion ()
  10259. "Try to complete a structure template before point.
  10260. This looks for strings like \"<e\" on an otherwise empty line and
  10261. expands them."
  10262. (let ((l (buffer-substring (point-at-bol) (point)))
  10263. a)
  10264. (when (and (looking-at "[ \t]*$")
  10265. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10266. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10267. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10268. (match-beginning 1)) a)
  10269. t)))
  10270. (defun org-complete-expand-structure-template (start cell)
  10271. "Expand a structure template."
  10272. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10273. (rpl (nth (if musep 2 1) cell))
  10274. (ind ""))
  10275. (delete-region start (point))
  10276. (when (string-match "\\`#\\+" rpl)
  10277. (cond
  10278. ((bolp))
  10279. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10280. (setq ind (buffer-substring (point-at-bol) (point))))
  10281. (t (newline))))
  10282. (setq start (point))
  10283. (if (string-match "%file" rpl)
  10284. (setq rpl (replace-match
  10285. (concat
  10286. "\""
  10287. (save-match-data
  10288. (abbreviate-file-name (read-file-name "Include file: ")))
  10289. "\"")
  10290. t t rpl)))
  10291. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10292. (concat "\n" ind)))
  10293. (insert rpl)
  10294. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10295. ;;;; TODO, DEADLINE, Comments
  10296. (defun org-toggle-comment ()
  10297. "Change the COMMENT state of an entry."
  10298. (interactive)
  10299. (save-excursion
  10300. (org-back-to-heading)
  10301. (let (case-fold-search)
  10302. (cond
  10303. ((looking-at (format org-heading-keyword-regexp-format
  10304. org-comment-string))
  10305. (goto-char (match-end 1))
  10306. (looking-at (concat " +" org-comment-string))
  10307. (replace-match "" t t)
  10308. (when (eolp) (insert " ")))
  10309. ((looking-at org-outline-regexp)
  10310. (goto-char (match-end 0))
  10311. (insert org-comment-string " "))))))
  10312. (defvar org-last-todo-state-is-todo nil
  10313. "This is non-nil when the last TODO state change led to a TODO state.
  10314. If the last change removed the TODO tag or switched to DONE, then
  10315. this is nil.")
  10316. (defvar org-setting-tags nil) ; dynamically skipped
  10317. (defvar org-todo-setup-filter-hook nil
  10318. "Hook for functions that pre-filter todo specs.
  10319. Each function takes a todo spec and returns either nil or the spec
  10320. transformed into canonical form." )
  10321. (defvar org-todo-get-default-hook nil
  10322. "Hook for functions that get a default item for todo.
  10323. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10324. nil or a string to be used for the todo mark." )
  10325. (defvar org-agenda-headline-snapshot-before-repeat)
  10326. (defun org-current-effective-time ()
  10327. "Return current time adjusted for `org-extend-today-until' variable."
  10328. (let* ((ct (org-current-time))
  10329. (dct (decode-time ct))
  10330. (ct1
  10331. (if (and org-use-effective-time
  10332. (< (nth 2 dct) org-extend-today-until))
  10333. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10334. ct)))
  10335. ct1))
  10336. (defun org-todo-yesterday (&optional arg)
  10337. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10338. (interactive "P")
  10339. (if (eq major-mode 'org-agenda-mode)
  10340. (apply 'org-agenda-todo-yesterday arg)
  10341. (let* ((hour (third (decode-time
  10342. (org-current-time))))
  10343. (org-extend-today-until (1+ hour)))
  10344. (org-todo arg))))
  10345. (defun org-todo (&optional arg)
  10346. "Change the TODO state of an item.
  10347. The state of an item is given by a keyword at the start of the heading,
  10348. like
  10349. *** TODO Write paper
  10350. *** DONE Call mom
  10351. The different keywords are specified in the variable `org-todo-keywords'.
  10352. By default the available states are \"TODO\" and \"DONE\".
  10353. So for this example: when the item starts with TODO, it is changed to DONE.
  10354. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10355. DONE are present, add TODO at the beginning of the heading.
  10356. With \\[universal-argument] prefix arg, use completion to determine the new \
  10357. state.
  10358. With numeric prefix arg, switch to that state.
  10359. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10360. keywords (nextset).
  10361. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10362. With a numeric prefix arg of 0, inhibit note taking for the change.
  10363. For calling through lisp, arg is also interpreted in the following way:
  10364. 'none -> empty state
  10365. \"\"(empty string) -> switch to empty state
  10366. 'done -> switch to DONE
  10367. 'nextset -> switch to the next set of keywords
  10368. 'previousset -> switch to the previous set of keywords
  10369. \"WAITING\" -> switch to the specified keyword, but only if it
  10370. really is a member of `org-todo-keywords'."
  10371. (interactive "P")
  10372. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10373. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10374. 'region-start-level 'region))
  10375. org-loop-over-headlines-in-active-region)
  10376. (org-map-entries
  10377. `(org-todo ,arg)
  10378. org-loop-over-headlines-in-active-region
  10379. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10380. (if (equal arg '(16)) (setq arg 'nextset))
  10381. (let ((org-blocker-hook org-blocker-hook)
  10382. commentp
  10383. case-fold-search)
  10384. (when (equal arg '(64))
  10385. (setq arg nil org-blocker-hook nil))
  10386. (when (and org-blocker-hook
  10387. (or org-inhibit-blocking
  10388. (org-entry-get nil "NOBLOCKING")))
  10389. (setq org-blocker-hook nil))
  10390. (save-excursion
  10391. (catch 'exit
  10392. (org-back-to-heading t)
  10393. (when (looking-at (concat "^\\*+ " org-comment-string))
  10394. (org-toggle-comment)
  10395. (setq commentp t))
  10396. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10397. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10398. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10399. (let* ((match-data (match-data))
  10400. (startpos (point-at-bol))
  10401. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10402. (org-log-done org-log-done)
  10403. (org-log-repeat org-log-repeat)
  10404. (org-todo-log-states org-todo-log-states)
  10405. (org-inhibit-logging
  10406. (if (equal arg 0)
  10407. (progn (setq arg nil) 'note) org-inhibit-logging))
  10408. (this (match-string 1))
  10409. (hl-pos (match-beginning 0))
  10410. (head (org-get-todo-sequence-head this))
  10411. (ass (assoc head org-todo-kwd-alist))
  10412. (interpret (nth 1 ass))
  10413. (done-word (nth 3 ass))
  10414. (final-done-word (nth 4 ass))
  10415. (org-last-state (or this ""))
  10416. (completion-ignore-case t)
  10417. (member (member this org-todo-keywords-1))
  10418. (tail (cdr member))
  10419. (org-state (cond
  10420. ((and org-todo-key-trigger
  10421. (or (and (equal arg '(4))
  10422. (eq org-use-fast-todo-selection 'prefix))
  10423. (and (not arg) org-use-fast-todo-selection
  10424. (not (eq org-use-fast-todo-selection
  10425. 'prefix)))))
  10426. ;; Use fast selection
  10427. (org-fast-todo-selection))
  10428. ((and (equal arg '(4))
  10429. (or (not org-use-fast-todo-selection)
  10430. (not org-todo-key-trigger)))
  10431. ;; Read a state with completion
  10432. (org-icompleting-read
  10433. "State: " (mapcar (lambda(x) (list x))
  10434. org-todo-keywords-1)
  10435. nil t))
  10436. ((eq arg 'right)
  10437. (if this
  10438. (if tail (car tail) nil)
  10439. (car org-todo-keywords-1)))
  10440. ((eq arg 'left)
  10441. (if (equal member org-todo-keywords-1)
  10442. nil
  10443. (if this
  10444. (nth (- (length org-todo-keywords-1)
  10445. (length tail) 2)
  10446. org-todo-keywords-1)
  10447. (org-last org-todo-keywords-1))))
  10448. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10449. (setq arg nil))) ; hack to fall back to cycling
  10450. (arg
  10451. ;; user or caller requests a specific state
  10452. (cond
  10453. ((equal arg "") nil)
  10454. ((eq arg 'none) nil)
  10455. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10456. ((eq arg 'nextset)
  10457. (or (car (cdr (member head org-todo-heads)))
  10458. (car org-todo-heads)))
  10459. ((eq arg 'previousset)
  10460. (let ((org-todo-heads (reverse org-todo-heads)))
  10461. (or (car (cdr (member head org-todo-heads)))
  10462. (car org-todo-heads))))
  10463. ((car (member arg org-todo-keywords-1)))
  10464. ((stringp arg)
  10465. (error "State `%s' not valid in this file" arg))
  10466. ((nth (1- (prefix-numeric-value arg))
  10467. org-todo-keywords-1))))
  10468. ((null member) (or head (car org-todo-keywords-1)))
  10469. ((equal this final-done-word) nil) ;; -> make empty
  10470. ((null tail) nil) ;; -> first entry
  10471. ((memq interpret '(type priority))
  10472. (if (eq this-command last-command)
  10473. (car tail)
  10474. (if (> (length tail) 0)
  10475. (or done-word (car org-done-keywords))
  10476. nil)))
  10477. (t
  10478. (car tail))))
  10479. (org-state (or
  10480. (run-hook-with-args-until-success
  10481. 'org-todo-get-default-hook org-state org-last-state)
  10482. org-state))
  10483. (next (if org-state (concat " " org-state " ") " "))
  10484. (change-plist (list :type 'todo-state-change :from this :to org-state
  10485. :position startpos))
  10486. dolog now-done-p)
  10487. (when org-blocker-hook
  10488. (setq org-last-todo-state-is-todo
  10489. (not (member this org-done-keywords)))
  10490. (unless (save-excursion
  10491. (save-match-data
  10492. (org-with-wide-buffer
  10493. (run-hook-with-args-until-failure
  10494. 'org-blocker-hook change-plist))))
  10495. (if (org-called-interactively-p 'interactive)
  10496. (error "TODO state change from %s to %s blocked" this org-state)
  10497. ;; fail silently
  10498. (message "TODO state change from %s to %s blocked" this org-state)
  10499. (throw 'exit nil))))
  10500. (store-match-data match-data)
  10501. (replace-match next t t)
  10502. (unless (pos-visible-in-window-p hl-pos)
  10503. (message "TODO state changed to %s" (org-trim next)))
  10504. (unless head
  10505. (setq head (org-get-todo-sequence-head org-state)
  10506. ass (assoc head org-todo-kwd-alist)
  10507. interpret (nth 1 ass)
  10508. done-word (nth 3 ass)
  10509. final-done-word (nth 4 ass)))
  10510. (when (memq arg '(nextset previousset))
  10511. (message "Keyword-Set %d/%d: %s"
  10512. (- (length org-todo-sets) -1
  10513. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10514. (length org-todo-sets)
  10515. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10516. (setq org-last-todo-state-is-todo
  10517. (not (member org-state org-done-keywords)))
  10518. (setq now-done-p (and (member org-state org-done-keywords)
  10519. (not (member this org-done-keywords))))
  10520. (and logging (org-local-logging logging))
  10521. (when (and (or org-todo-log-states org-log-done)
  10522. (not (eq org-inhibit-logging t))
  10523. (not (memq arg '(nextset previousset))))
  10524. ;; we need to look at recording a time and note
  10525. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10526. (nth 2 (assoc this org-todo-log-states))))
  10527. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10528. (setq dolog 'time))
  10529. (when (and org-state
  10530. (member org-state org-not-done-keywords)
  10531. (not (member this org-not-done-keywords)))
  10532. ;; This is now a todo state and was not one before
  10533. ;; If there was a CLOSED time stamp, get rid of it.
  10534. (org-add-planning-info nil nil 'closed))
  10535. (when (and now-done-p org-log-done)
  10536. ;; It is now done, and it was not done before
  10537. (org-add-planning-info 'closed (org-current-effective-time))
  10538. (if (and (not dolog) (eq 'note org-log-done))
  10539. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10540. (when (and org-state dolog)
  10541. ;; This is a non-nil state, and we need to log it
  10542. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10543. ;; Fixup tag positioning
  10544. (org-todo-trigger-tag-changes org-state)
  10545. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10546. (when org-provide-todo-statistics
  10547. (org-update-parent-todo-statistics))
  10548. (run-hooks 'org-after-todo-state-change-hook)
  10549. (if (and arg (not (member org-state org-done-keywords)))
  10550. (setq head (org-get-todo-sequence-head org-state)))
  10551. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10552. ;; Do we need to trigger a repeat?
  10553. (when now-done-p
  10554. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10555. ;; This is for the agenda, take a snapshot of the headline.
  10556. (save-match-data
  10557. (setq org-agenda-headline-snapshot-before-repeat
  10558. (org-get-heading))))
  10559. (org-auto-repeat-maybe org-state))
  10560. ;; Fixup cursor location if close to the keyword
  10561. (if (and (outline-on-heading-p)
  10562. (not (bolp))
  10563. (save-excursion (beginning-of-line 1)
  10564. (looking-at org-todo-line-regexp))
  10565. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10566. (progn
  10567. (goto-char (or (match-end 2) (match-end 1)))
  10568. (and (looking-at " ") (just-one-space))))
  10569. (when org-trigger-hook
  10570. (save-excursion
  10571. (run-hook-with-args 'org-trigger-hook change-plist)))
  10572. (when commentp (org-toggle-comment))))))))
  10573. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10574. "Block turning an entry into a TODO, using the hierarchy.
  10575. This checks whether the current task should be blocked from state
  10576. changes. Such blocking occurs when:
  10577. 1. The task has children which are not all in a completed state.
  10578. 2. A task has a parent with the property :ORDERED:, and there
  10579. are siblings prior to the current task with incomplete
  10580. status.
  10581. 3. The parent of the task is blocked because it has siblings that should
  10582. be done first, or is child of a block grandparent TODO entry."
  10583. (if (not org-enforce-todo-dependencies)
  10584. t ; if locally turned off don't block
  10585. (catch 'dont-block
  10586. ;; If this is not a todo state change, or if this entry is already DONE,
  10587. ;; do not block
  10588. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10589. (member (plist-get change-plist :from)
  10590. (cons 'done org-done-keywords))
  10591. (member (plist-get change-plist :to)
  10592. (cons 'todo org-not-done-keywords))
  10593. (not (plist-get change-plist :to)))
  10594. (throw 'dont-block t))
  10595. ;; If this task has children, and any are undone, it's blocked
  10596. (save-excursion
  10597. (org-back-to-heading t)
  10598. (let ((this-level (funcall outline-level)))
  10599. (outline-next-heading)
  10600. (let ((child-level (funcall outline-level)))
  10601. (while (and (not (eobp))
  10602. (> child-level this-level))
  10603. ;; this todo has children, check whether they are all
  10604. ;; completed
  10605. (if (and (not (org-entry-is-done-p))
  10606. (org-entry-is-todo-p))
  10607. (throw 'dont-block nil))
  10608. (outline-next-heading)
  10609. (setq child-level (funcall outline-level))))))
  10610. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10611. ;; any previous siblings are undone, it's blocked
  10612. (save-excursion
  10613. (org-back-to-heading t)
  10614. (let* ((pos (point))
  10615. (parent-pos (and (org-up-heading-safe) (point))))
  10616. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10617. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10618. (forward-line 1)
  10619. (re-search-forward org-not-done-heading-regexp pos t))
  10620. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10621. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10622. (while t
  10623. (goto-char parent-pos)
  10624. (if (not (looking-at org-not-done-heading-regexp))
  10625. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10626. (setq pos (point))
  10627. (setq parent-pos (and (org-up-heading-safe) (point)))
  10628. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10629. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10630. (forward-line 1)
  10631. (re-search-forward org-not-done-heading-regexp pos t))
  10632. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10633. (defcustom org-track-ordered-property-with-tag nil
  10634. "Should the ORDERED property also be shown as a tag?
  10635. The ORDERED property decides if an entry should require subtasks to be
  10636. completed in sequence. Since a property is not very visible, setting
  10637. this option means that toggling the ORDERED property with the command
  10638. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10639. not relevant for the behavior, but it makes things more visible.
  10640. Note that toggling the tag with tags commands will not change the property
  10641. and therefore not influence behavior!
  10642. This can be t, meaning the tag ORDERED should be used, It can also be a
  10643. string to select a different tag for this task."
  10644. :group 'org-todo
  10645. :type '(choice
  10646. (const :tag "No tracking" nil)
  10647. (const :tag "Track with ORDERED tag" t)
  10648. (string :tag "Use other tag")))
  10649. (defun org-toggle-ordered-property ()
  10650. "Toggle the ORDERED property of the current entry.
  10651. For better visibility, you can track the value of this property with a tag.
  10652. See variable `org-track-ordered-property-with-tag'."
  10653. (interactive)
  10654. (let* ((t1 org-track-ordered-property-with-tag)
  10655. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10656. (save-excursion
  10657. (org-back-to-heading)
  10658. (if (org-entry-get nil "ORDERED")
  10659. (progn
  10660. (org-delete-property "ORDERED")
  10661. (and tag (org-toggle-tag tag 'off))
  10662. (message "Subtasks can be completed in arbitrary order"))
  10663. (org-entry-put nil "ORDERED" "t")
  10664. (and tag (org-toggle-tag tag 'on))
  10665. (message "Subtasks must be completed in sequence")))))
  10666. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10667. (defun org-block-todo-from-checkboxes (change-plist)
  10668. "Block turning an entry into a TODO, using checkboxes.
  10669. This checks whether the current task should be blocked from state
  10670. changes because there are unchecked boxes in this entry."
  10671. (if (not org-enforce-todo-checkbox-dependencies)
  10672. t ; if locally turned off don't block
  10673. (catch 'dont-block
  10674. ;; If this is not a todo state change, or if this entry is already DONE,
  10675. ;; do not block
  10676. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10677. (member (plist-get change-plist :from)
  10678. (cons 'done org-done-keywords))
  10679. (member (plist-get change-plist :to)
  10680. (cons 'todo org-not-done-keywords))
  10681. (not (plist-get change-plist :to)))
  10682. (throw 'dont-block t))
  10683. ;; If this task has checkboxes that are not checked, it's blocked
  10684. (save-excursion
  10685. (org-back-to-heading t)
  10686. (let ((beg (point)) end)
  10687. (outline-next-heading)
  10688. (setq end (point))
  10689. (goto-char beg)
  10690. (if (org-list-search-forward
  10691. (concat (org-item-beginning-re)
  10692. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10693. "\\[[- ]\\]")
  10694. end t)
  10695. (progn
  10696. (if (boundp 'org-blocked-by-checkboxes)
  10697. (setq org-blocked-by-checkboxes t))
  10698. (throw 'dont-block nil)))))
  10699. t))) ; do not block
  10700. (defun org-entry-blocked-p ()
  10701. "Is the current entry blocked?"
  10702. (org-with-buffer-modified-unmodified
  10703. (if (org-entry-get nil "NOBLOCKING")
  10704. nil ;; Never block this entry
  10705. (not
  10706. (run-hook-with-args-until-failure
  10707. 'org-blocker-hook
  10708. (list :type 'todo-state-change
  10709. :position (point)
  10710. :from 'todo
  10711. :to 'done))))))
  10712. (defun org-update-statistics-cookies (all)
  10713. "Update the statistics cookie, either from TODO or from checkboxes.
  10714. This should be called with the cursor in a line with a statistics cookie."
  10715. (interactive "P")
  10716. (if all
  10717. (progn
  10718. (org-update-checkbox-count 'all)
  10719. (org-map-entries 'org-update-parent-todo-statistics))
  10720. (if (not (org-at-heading-p))
  10721. (org-update-checkbox-count)
  10722. (let ((pos (point-marker))
  10723. end l1 l2)
  10724. (ignore-errors (org-back-to-heading t))
  10725. (if (not (org-at-heading-p))
  10726. (org-update-checkbox-count)
  10727. (setq l1 (org-outline-level))
  10728. (setq end (save-excursion
  10729. (outline-next-heading)
  10730. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10731. (point)))
  10732. (if (and (save-excursion
  10733. (re-search-forward
  10734. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10735. (not (save-excursion (re-search-forward
  10736. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10737. (org-update-checkbox-count)
  10738. (if (and l2 (> l2 l1))
  10739. (progn
  10740. (goto-char end)
  10741. (org-update-parent-todo-statistics))
  10742. (goto-char pos)
  10743. (beginning-of-line 1)
  10744. (while (re-search-forward
  10745. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10746. (point-at-eol) t)
  10747. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10748. (goto-char pos)
  10749. (move-marker pos nil)))))
  10750. (defvar org-entry-property-inherited-from) ;; defined below
  10751. (defun org-update-parent-todo-statistics ()
  10752. "Update any statistics cookie in the parent of the current headline.
  10753. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10754. the entire subtree and this will travel up the hierarchy and update
  10755. statistics everywhere."
  10756. (let* ((prop (save-excursion (org-up-heading-safe)
  10757. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10758. (recursive (or (not org-hierarchical-todo-statistics)
  10759. (and prop (string-match "\\<recursive\\>" prop))))
  10760. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10761. 0))
  10762. (first t)
  10763. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10764. level ltoggle l1 new ndel
  10765. (cnt-all 0) (cnt-done 0) is-percent kwd
  10766. checkbox-beg ov ovs ove cookie-present)
  10767. (catch 'exit
  10768. (save-excursion
  10769. (beginning-of-line 1)
  10770. (setq ltoggle (funcall outline-level))
  10771. ;; Three situations are to consider:
  10772. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10773. ;; to the top-level ancestor on the headline;
  10774. ;; 2. If parent has "recursive" property, repeat up to the
  10775. ;; headline setting that property, taking inheritance into
  10776. ;; account;
  10777. ;; 3. Else, move up to direct parent and proceed only once.
  10778. (while (and (setq level (org-up-heading-safe))
  10779. (or recursive first)
  10780. (>= (point) lim))
  10781. (setq first nil cookie-present nil)
  10782. (unless (and level
  10783. (not (string-match
  10784. "\\<checkbox\\>"
  10785. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10786. "")))))
  10787. (throw 'exit nil))
  10788. (while (re-search-forward box-re (point-at-eol) t)
  10789. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10790. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10791. (save-match-data
  10792. (unless (outline-next-heading) (throw 'exit nil))
  10793. (while (and (looking-at org-complex-heading-regexp)
  10794. (> (setq l1 (length (match-string 1))) level))
  10795. (setq kwd (and (or recursive (= l1 ltoggle))
  10796. (match-string 2)))
  10797. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10798. (and (listp org-provide-todo-statistics)
  10799. (or (member kwd org-provide-todo-statistics)
  10800. (member kwd org-done-keywords))))
  10801. (setq cnt-all (1+ cnt-all))
  10802. (if (eq org-provide-todo-statistics t)
  10803. (and kwd (setq cnt-all (1+ cnt-all)))))
  10804. (and (member kwd org-done-keywords)
  10805. (setq cnt-done (1+ cnt-done)))
  10806. (outline-next-heading)))
  10807. (setq new
  10808. (if is-percent
  10809. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10810. (format "[%d/%d]" cnt-done cnt-all))
  10811. ndel (- (match-end 0) checkbox-beg))
  10812. ;; handle overlays when updating cookie from column view
  10813. (when (setq ov (car (overlays-at checkbox-beg)))
  10814. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10815. (delete-overlay ov))
  10816. (goto-char checkbox-beg)
  10817. (insert new)
  10818. (delete-region (point) (+ (point) ndel))
  10819. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10820. (when ov (move-overlay ov ovs ove)))
  10821. (when cookie-present
  10822. (run-hook-with-args 'org-after-todo-statistics-hook
  10823. cnt-done (- cnt-all cnt-done))))))
  10824. (run-hooks 'org-todo-statistics-hook)))
  10825. (defvar org-after-todo-statistics-hook nil
  10826. "Hook that is called after a TODO statistics cookie has been updated.
  10827. Each function is called with two arguments: the number of not-done entries
  10828. and the number of done entries.
  10829. For example, the following function, when added to this hook, will switch
  10830. an entry to DONE when all children are done, and back to TODO when new
  10831. entries are set to a TODO status. Note that this hook is only called
  10832. when there is a statistics cookie in the headline!
  10833. (defun org-summary-todo (n-done n-not-done)
  10834. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10835. (let (org-log-done org-log-states) ; turn off logging
  10836. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10837. ")
  10838. (defvar org-todo-statistics-hook nil
  10839. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10840. This hook runs even if there is no statistics cookie present, in which case
  10841. `org-after-todo-statistics-hook' would not run.")
  10842. (defun org-todo-trigger-tag-changes (state)
  10843. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10844. (let ((l org-todo-state-tags-triggers)
  10845. changes)
  10846. (when (or (not state) (equal state ""))
  10847. (setq changes (append changes (cdr (assoc "" l)))))
  10848. (when (and (stringp state) (> (length state) 0))
  10849. (setq changes (append changes (cdr (assoc state l)))))
  10850. (when (member state org-not-done-keywords)
  10851. (setq changes (append changes (cdr (assoc 'todo l)))))
  10852. (when (member state org-done-keywords)
  10853. (setq changes (append changes (cdr (assoc 'done l)))))
  10854. (dolist (c changes)
  10855. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10856. (defun org-local-logging (value)
  10857. "Get logging settings from a property VALUE."
  10858. (let* (words w a)
  10859. ;; directly set the variables, they are already local.
  10860. (setq org-log-done nil
  10861. org-log-repeat nil
  10862. org-todo-log-states nil)
  10863. (setq words (org-split-string value))
  10864. (while (setq w (pop words))
  10865. (cond
  10866. ((setq a (assoc w org-startup-options))
  10867. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10868. (set (nth 1 a) (nth 2 a))))
  10869. ((setq a (org-extract-log-state-settings w))
  10870. (and (member (car a) org-todo-keywords-1)
  10871. (push a org-todo-log-states)))))))
  10872. (defun org-get-todo-sequence-head (kwd)
  10873. "Return the head of the TODO sequence to which KWD belongs.
  10874. If KWD is not set, check if there is a text property remembering the
  10875. right sequence."
  10876. (let (p)
  10877. (cond
  10878. ((not kwd)
  10879. (or (get-text-property (point-at-bol) 'org-todo-head)
  10880. (progn
  10881. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10882. nil (point-at-eol)))
  10883. (get-text-property p 'org-todo-head))))
  10884. ((not (member kwd org-todo-keywords-1))
  10885. (car org-todo-keywords-1))
  10886. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10887. (defun org-fast-todo-selection ()
  10888. "Fast TODO keyword selection with single keys.
  10889. Returns the new TODO keyword, or nil if no state change should occur."
  10890. (let* ((fulltable org-todo-key-alist)
  10891. (done-keywords org-done-keywords) ;; needed for the faces.
  10892. (maxlen (apply 'max (mapcar
  10893. (lambda (x)
  10894. (if (stringp (car x)) (string-width (car x)) 0))
  10895. fulltable)))
  10896. (expert nil)
  10897. (fwidth (+ maxlen 3 1 3))
  10898. (ncol (/ (- (window-width) 4) fwidth))
  10899. tg cnt e c tbl
  10900. groups ingroup)
  10901. (save-excursion
  10902. (save-window-excursion
  10903. (if expert
  10904. (set-buffer (get-buffer-create " *Org todo*"))
  10905. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10906. (erase-buffer)
  10907. (org-set-local 'org-done-keywords done-keywords)
  10908. (setq tbl fulltable cnt 0)
  10909. (while (setq e (pop tbl))
  10910. (cond
  10911. ((equal e '(:startgroup))
  10912. (push '() groups) (setq ingroup t)
  10913. (when (not (= cnt 0))
  10914. (setq cnt 0)
  10915. (insert "\n"))
  10916. (insert "{ "))
  10917. ((equal e '(:endgroup))
  10918. (setq ingroup nil cnt 0)
  10919. (insert "}\n"))
  10920. ((equal e '(:newline))
  10921. (when (not (= cnt 0))
  10922. (setq cnt 0)
  10923. (insert "\n")
  10924. (setq e (car tbl))
  10925. (while (equal (car tbl) '(:newline))
  10926. (insert "\n")
  10927. (setq tbl (cdr tbl)))))
  10928. (t
  10929. (setq tg (car e) c (cdr e))
  10930. (if ingroup (push tg (car groups)))
  10931. (setq tg (org-add-props tg nil 'face
  10932. (org-get-todo-face tg)))
  10933. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10934. (insert "[" c "] " tg (make-string
  10935. (- fwidth 4 (length tg)) ?\ ))
  10936. (when (= (setq cnt (1+ cnt)) ncol)
  10937. (insert "\n")
  10938. (if ingroup (insert " "))
  10939. (setq cnt 0)))))
  10940. (insert "\n")
  10941. (goto-char (point-min))
  10942. (if (not expert) (org-fit-window-to-buffer))
  10943. (message "[a-z..]:Set [SPC]:clear")
  10944. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10945. (cond
  10946. ((or (= c ?\C-g)
  10947. (and (= c ?q) (not (rassoc c fulltable))))
  10948. (setq quit-flag t))
  10949. ((= c ?\ ) nil)
  10950. ((setq e (rassoc c fulltable) tg (car e))
  10951. tg)
  10952. (t (setq quit-flag t)))))))
  10953. (defun org-entry-is-todo-p ()
  10954. (member (org-get-todo-state) org-not-done-keywords))
  10955. (defun org-entry-is-done-p ()
  10956. (member (org-get-todo-state) org-done-keywords))
  10957. (defun org-get-todo-state ()
  10958. (save-excursion
  10959. (org-back-to-heading t)
  10960. (and (looking-at org-todo-line-regexp)
  10961. (match-end 2)
  10962. (match-string 2))))
  10963. (defun org-at-date-range-p (&optional inactive-ok)
  10964. "Is the cursor inside a date range?"
  10965. (interactive)
  10966. (save-excursion
  10967. (catch 'exit
  10968. (let ((pos (point)))
  10969. (skip-chars-backward "^[<\r\n")
  10970. (skip-chars-backward "<[")
  10971. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10972. (>= (match-end 0) pos)
  10973. (throw 'exit t))
  10974. (skip-chars-backward "^<[\r\n")
  10975. (skip-chars-backward "<[")
  10976. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10977. (>= (match-end 0) pos)
  10978. (throw 'exit t)))
  10979. nil)))
  10980. (defun org-get-repeat (&optional tagline)
  10981. "Check if there is a deadline/schedule with repeater in this entry."
  10982. (save-match-data
  10983. (save-excursion
  10984. (org-back-to-heading t)
  10985. (and (re-search-forward (if tagline
  10986. (concat tagline "\\s-*" org-repeat-re)
  10987. org-repeat-re)
  10988. (org-entry-end-position) t)
  10989. (match-string-no-properties 1)))))
  10990. (defvar org-last-changed-timestamp)
  10991. (defvar org-last-inserted-timestamp)
  10992. (defvar org-log-post-message)
  10993. (defvar org-log-note-purpose)
  10994. (defvar org-log-note-how)
  10995. (defvar org-log-note-extra)
  10996. (defun org-auto-repeat-maybe (done-word)
  10997. "Check if the current headline contains a repeated deadline/schedule.
  10998. If yes, set TODO state back to what it was and change the base date
  10999. of repeating deadline/scheduled time stamps to new date.
  11000. This function is run automatically after each state change to a DONE state."
  11001. ;; last-state is dynamically scoped into this function
  11002. (let* ((repeat (org-get-repeat))
  11003. (aa (assoc org-last-state org-todo-kwd-alist))
  11004. (interpret (nth 1 aa))
  11005. (head (nth 2 aa))
  11006. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11007. (msg "Entry repeats: ")
  11008. (org-log-done nil)
  11009. (org-todo-log-states nil)
  11010. re type n what ts time to-state)
  11011. (when repeat
  11012. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11013. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11014. org-todo-repeat-to-state))
  11015. (unless (and to-state (member to-state org-todo-keywords-1))
  11016. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11017. (org-todo to-state)
  11018. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11019. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11020. (org-time-stamp-format t t))))
  11021. (when org-log-repeat
  11022. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11023. (memq 'org-add-log-note post-command-hook))
  11024. ;; OK, we are already setup for some record
  11025. (if (eq org-log-repeat 'note)
  11026. ;; make sure we take a note, not only a time stamp
  11027. (setq org-log-note-how 'note))
  11028. ;; Set up for taking a record
  11029. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11030. org-last-state
  11031. 'findpos org-log-repeat)))
  11032. (org-back-to-heading t)
  11033. (org-add-planning-info nil nil 'closed)
  11034. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11035. org-deadline-time-regexp "\\)\\|\\("
  11036. org-ts-regexp "\\)"))
  11037. (while (re-search-forward
  11038. re (save-excursion (outline-next-heading) (point)) t)
  11039. (setq type (if (match-end 1) org-scheduled-string
  11040. (if (match-end 3) org-deadline-string "Plain:"))
  11041. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11042. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11043. (setq n (string-to-number (match-string 2 ts))
  11044. what (match-string 3 ts))
  11045. (if (equal what "w") (setq n (* n 7) what "d"))
  11046. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11047. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11048. ;; Preparation, see if we need to modify the start date for the change
  11049. (when (match-end 1)
  11050. (setq time (save-match-data (org-time-string-to-time ts)))
  11051. (cond
  11052. ((equal (match-string 1 ts) ".")
  11053. ;; Shift starting date to today
  11054. (org-timestamp-change
  11055. (- (org-today) (time-to-days time))
  11056. 'day))
  11057. ((equal (match-string 1 ts) "+")
  11058. (let ((nshiftmax 10) (nshift 0))
  11059. (while (or (= nshift 0)
  11060. (<= (time-to-days time)
  11061. (time-to-days (current-time))))
  11062. (when (= (incf nshift) nshiftmax)
  11063. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11064. (error "Abort")))
  11065. (org-timestamp-change n (cdr (assoc what whata)))
  11066. (org-at-timestamp-p t)
  11067. (setq ts (match-string 1))
  11068. (setq time (save-match-data (org-time-string-to-time ts)))))
  11069. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11070. ;; rematch, so that we have everything in place for the real shift
  11071. (org-at-timestamp-p t)
  11072. (setq ts (match-string 1))
  11073. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11074. (org-timestamp-change n (cdr (assoc what whata)))
  11075. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11076. (setq org-log-post-message msg)
  11077. (message "%s" msg))))
  11078. (defun org-show-todo-tree (arg)
  11079. "Make a compact tree which shows all headlines marked with TODO.
  11080. The tree will show the lines where the regexp matches, and all higher
  11081. headlines above the match.
  11082. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11083. With a numeric prefix N, construct a sparse tree for the Nth element
  11084. of `org-todo-keywords-1'."
  11085. (interactive "P")
  11086. (let ((case-fold-search nil)
  11087. (kwd-re
  11088. (cond ((null arg) org-not-done-regexp)
  11089. ((equal arg '(4))
  11090. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11091. (mapcar 'list org-todo-keywords-1))))
  11092. (concat "\\("
  11093. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11094. "\\)\\>")))
  11095. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11096. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11097. org-todo-keywords-1)))
  11098. (t (error "Invalid prefix argument: %s" arg)))))
  11099. (message "%d TODO entries found"
  11100. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11101. (defun org-deadline (&optional remove time)
  11102. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11103. With argument REMOVE, remove any deadline from the item.
  11104. With argument TIME, set the deadline at the corresponding date. TIME
  11105. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11106. (interactive "P")
  11107. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11108. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11109. 'region-start-level 'region))
  11110. org-loop-over-headlines-in-active-region)
  11111. (org-map-entries
  11112. `(org-deadline ',remove ,time)
  11113. org-loop-over-headlines-in-active-region
  11114. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11115. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11116. (repeater (and old-date
  11117. (string-match
  11118. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11119. old-date)
  11120. (match-string 1 old-date))))
  11121. (if remove
  11122. (progn
  11123. (when (and old-date org-log-redeadline)
  11124. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11125. org-log-redeadline))
  11126. (org-remove-timestamp-with-keyword org-deadline-string)
  11127. (message "Item no longer has a deadline."))
  11128. (org-add-planning-info 'deadline time 'closed)
  11129. (when (and old-date org-log-redeadline
  11130. (not (equal old-date
  11131. (substring org-last-inserted-timestamp 1 -1))))
  11132. (org-add-log-setup 'redeadline nil old-date 'findpos
  11133. org-log-redeadline))
  11134. (when repeater
  11135. (save-excursion
  11136. (org-back-to-heading t)
  11137. (when (re-search-forward (concat org-deadline-string " "
  11138. org-last-inserted-timestamp)
  11139. (save-excursion
  11140. (outline-next-heading) (point)) t)
  11141. (goto-char (1- (match-end 0)))
  11142. (insert " " repeater)
  11143. (setq org-last-inserted-timestamp
  11144. (concat (substring org-last-inserted-timestamp 0 -1)
  11145. " " repeater
  11146. (substring org-last-inserted-timestamp -1))))))
  11147. (message "Deadline on %s" org-last-inserted-timestamp)))))
  11148. (defun org-schedule (&optional remove time)
  11149. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11150. With argument REMOVE, remove any scheduling date from the item.
  11151. With argument TIME, scheduled at the corresponding date. TIME can
  11152. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11153. (interactive "P")
  11154. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11155. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11156. 'region-start-level 'region))
  11157. org-loop-over-headlines-in-active-region)
  11158. (org-map-entries
  11159. `(org-schedule ',remove ,time)
  11160. org-loop-over-headlines-in-active-region
  11161. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11162. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11163. (repeater (and old-date
  11164. (string-match
  11165. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11166. old-date)
  11167. (match-string 1 old-date))))
  11168. (if remove
  11169. (progn
  11170. (when (and old-date org-log-reschedule)
  11171. (org-add-log-setup 'delschedule nil old-date 'findpos
  11172. org-log-reschedule))
  11173. (org-remove-timestamp-with-keyword org-scheduled-string)
  11174. (message "Item is no longer scheduled."))
  11175. (org-add-planning-info 'scheduled time 'closed)
  11176. (when (and old-date org-log-reschedule
  11177. (not (equal old-date
  11178. (substring org-last-inserted-timestamp 1 -1))))
  11179. (org-add-log-setup 'reschedule nil old-date 'findpos
  11180. org-log-reschedule))
  11181. (when repeater
  11182. (save-excursion
  11183. (org-back-to-heading t)
  11184. (when (re-search-forward (concat org-scheduled-string " "
  11185. org-last-inserted-timestamp)
  11186. (save-excursion
  11187. (outline-next-heading) (point)) t)
  11188. (goto-char (1- (match-end 0)))
  11189. (insert " " repeater)
  11190. (setq org-last-inserted-timestamp
  11191. (concat (substring org-last-inserted-timestamp 0 -1)
  11192. " " repeater
  11193. (substring org-last-inserted-timestamp -1))))))
  11194. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11195. (defun org-get-scheduled-time (pom &optional inherit)
  11196. "Get the scheduled time as a time tuple, of a format suitable
  11197. for calling org-schedule with, or if there is no scheduling,
  11198. returns nil."
  11199. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11200. (when time
  11201. (apply 'encode-time (org-parse-time-string time)))))
  11202. (defun org-get-deadline-time (pom &optional inherit)
  11203. "Get the deadline as a time tuple, of a format suitable for
  11204. calling org-deadline with, or if there is no scheduling, returns
  11205. nil."
  11206. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11207. (when time
  11208. (apply 'encode-time (org-parse-time-string time)))))
  11209. (defun org-remove-timestamp-with-keyword (keyword)
  11210. "Remove all time stamps with KEYWORD in the current entry."
  11211. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11212. beg)
  11213. (save-excursion
  11214. (org-back-to-heading t)
  11215. (setq beg (point))
  11216. (outline-next-heading)
  11217. (while (re-search-backward re beg t)
  11218. (replace-match "")
  11219. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11220. (equal (char-before) ?\ ))
  11221. (backward-delete-char 1)
  11222. (if (string-match "^[ \t]*$" (buffer-substring
  11223. (point-at-bol) (point-at-eol)))
  11224. (delete-region (point-at-bol)
  11225. (min (point-max) (1+ (point-at-eol))))))))))
  11226. (defun org-add-planning-info (what &optional time &rest remove)
  11227. "Insert new timestamp with keyword in the line directly after the headline.
  11228. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11229. If non is given, the user is prompted for a date.
  11230. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11231. be removed."
  11232. (interactive)
  11233. (let (org-time-was-given org-end-time-was-given ts
  11234. end default-time default-input)
  11235. (catch 'exit
  11236. (when (and (memq what '(scheduled deadline))
  11237. (or (not time)
  11238. (and (stringp time)
  11239. (string-match "^[-+]+[0-9]" time))))
  11240. ;; Try to get a default date/time from existing timestamp
  11241. (save-excursion
  11242. (org-back-to-heading t)
  11243. (setq end (save-excursion (outline-next-heading) (point)))
  11244. (when (re-search-forward (if (eq what 'scheduled)
  11245. org-scheduled-time-regexp
  11246. org-deadline-time-regexp)
  11247. end t)
  11248. (setq ts (match-string 1)
  11249. default-time
  11250. (apply 'encode-time (org-parse-time-string ts))
  11251. default-input (and ts (org-get-compact-tod ts))))))
  11252. (when what
  11253. (setq time
  11254. (if (stringp time)
  11255. ;; This is a string (relative or absolute), set proper date
  11256. (apply 'encode-time
  11257. (org-read-date-analyze
  11258. time default-time (decode-time default-time)))
  11259. ;; If necessary, get the time from the user
  11260. (or time (org-read-date nil 'to-time nil nil
  11261. default-time default-input)))))
  11262. (when (and org-insert-labeled-timestamps-at-point
  11263. (member what '(scheduled deadline)))
  11264. (insert
  11265. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11266. (org-insert-time-stamp time org-time-was-given
  11267. nil nil nil (list org-end-time-was-given))
  11268. (setq what nil))
  11269. (save-excursion
  11270. (save-restriction
  11271. (let (col list elt ts buffer-invisibility-spec)
  11272. (org-back-to-heading t)
  11273. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11274. (goto-char (match-end 1))
  11275. (setq col (current-column))
  11276. (goto-char (match-end 0))
  11277. (if (eobp) (insert "\n") (forward-char 1))
  11278. (when (and (not what)
  11279. (not (looking-at
  11280. (concat "[ \t]*"
  11281. org-keyword-time-not-clock-regexp))))
  11282. ;; Nothing to add, nothing to remove...... :-)
  11283. (throw 'exit nil))
  11284. (if (and (not (looking-at org-outline-regexp))
  11285. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11286. "[^\r\n]*"))
  11287. (not (equal (match-string 1) org-clock-string)))
  11288. (narrow-to-region (match-beginning 0) (match-end 0))
  11289. (insert-before-markers "\n")
  11290. (backward-char 1)
  11291. (narrow-to-region (point) (point))
  11292. (and org-adapt-indentation (org-indent-to-column col)))
  11293. ;; Check if we have to remove something.
  11294. (setq list (cons what remove))
  11295. (while list
  11296. (setq elt (pop list))
  11297. (when (or (and (eq elt 'scheduled)
  11298. (re-search-forward org-scheduled-time-regexp nil t))
  11299. (and (eq elt 'deadline)
  11300. (re-search-forward org-deadline-time-regexp nil t))
  11301. (and (eq elt 'closed)
  11302. (re-search-forward org-closed-time-regexp nil t)))
  11303. (replace-match "")
  11304. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11305. (and (looking-at "[ \t]+") (replace-match ""))
  11306. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11307. (when what
  11308. (insert
  11309. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11310. (cond ((eq what 'scheduled) org-scheduled-string)
  11311. ((eq what 'deadline) org-deadline-string)
  11312. ((eq what 'closed) org-closed-string))
  11313. " ")
  11314. (setq ts (org-insert-time-stamp
  11315. time
  11316. (or org-time-was-given
  11317. (and (eq what 'closed) org-log-done-with-time))
  11318. (eq what 'closed)
  11319. nil nil (list org-end-time-was-given)))
  11320. (insert
  11321. (if (not (or (bolp) (eq (char-before) ?\ )
  11322. (memq (char-after) '(32 10))
  11323. (eobp))) " " ""))
  11324. (end-of-line 1))
  11325. (goto-char (point-min))
  11326. (widen)
  11327. (if (and (looking-at "[ \t]*\n")
  11328. (equal (char-before) ?\n))
  11329. (delete-region (1- (point)) (point-at-eol)))
  11330. ts))))))
  11331. (defvar org-log-note-marker (make-marker))
  11332. (defvar org-log-note-purpose nil)
  11333. (defvar org-log-note-state nil)
  11334. (defvar org-log-note-previous-state nil)
  11335. (defvar org-log-note-how nil)
  11336. (defvar org-log-note-extra nil)
  11337. (defvar org-log-note-window-configuration nil)
  11338. (defvar org-log-note-return-to (make-marker))
  11339. (defvar org-log-note-effective-time nil
  11340. "Remembered current time so that dynamically scoped
  11341. `org-extend-today-until' affects tha timestamps in state change
  11342. log")
  11343. (defvar org-log-post-message nil
  11344. "Message to be displayed after a log note has been stored.
  11345. The auto-repeater uses this.")
  11346. (defun org-add-note ()
  11347. "Add a note to the current entry.
  11348. This is done in the same way as adding a state change note."
  11349. (interactive)
  11350. (org-add-log-setup 'note nil nil 'findpos nil))
  11351. (defvar org-property-end-re)
  11352. (defun org-add-log-setup (&optional purpose state prev-state
  11353. findpos how extra)
  11354. "Set up the post command hook to take a note.
  11355. If this is about to TODO state change, the new state is expected in STATE.
  11356. When FINDPOS is non-nil, find the correct position for the note in
  11357. the current entry. If not, assume that it can be inserted at point.
  11358. HOW is an indicator what kind of note should be created.
  11359. EXTRA is additional text that will be inserted into the notes buffer."
  11360. (let* ((org-log-into-drawer (org-log-into-drawer))
  11361. (drawer (cond ((stringp org-log-into-drawer)
  11362. org-log-into-drawer)
  11363. (org-log-into-drawer "LOGBOOK"))))
  11364. (save-restriction
  11365. (save-excursion
  11366. (when findpos
  11367. (org-back-to-heading t)
  11368. (narrow-to-region (point) (save-excursion
  11369. (outline-next-heading) (point)))
  11370. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11371. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11372. "[^\r\n]*\\)?"))
  11373. (goto-char (match-end 0))
  11374. (cond
  11375. (drawer
  11376. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11377. nil t)
  11378. (progn
  11379. (goto-char (match-end 0))
  11380. (or org-log-states-order-reversed
  11381. (and (re-search-forward org-property-end-re nil t)
  11382. (goto-char (1- (match-beginning 0))))))
  11383. (insert "\n:" drawer ":\n:END:")
  11384. (beginning-of-line 0)
  11385. (org-indent-line)
  11386. (beginning-of-line 2)
  11387. (org-indent-line)
  11388. (end-of-line 0)))
  11389. ((and org-log-state-notes-insert-after-drawers
  11390. (save-excursion
  11391. (forward-line) (looking-at org-drawer-regexp)))
  11392. (forward-line)
  11393. (while (looking-at org-drawer-regexp)
  11394. (goto-char (match-end 0))
  11395. (re-search-forward org-property-end-re (point-max) t)
  11396. (forward-line))
  11397. (forward-line -1)))
  11398. (unless org-log-states-order-reversed
  11399. (and (= (char-after) ?\n) (forward-char 1))
  11400. (org-skip-over-state-notes)
  11401. (skip-chars-backward " \t\n\r")))
  11402. (move-marker org-log-note-marker (point))
  11403. (setq org-log-note-purpose purpose
  11404. org-log-note-state state
  11405. org-log-note-previous-state prev-state
  11406. org-log-note-how how
  11407. org-log-note-extra extra
  11408. org-log-note-effective-time (org-current-effective-time))
  11409. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11410. (defun org-skip-over-state-notes ()
  11411. "Skip past the list of State notes in an entry."
  11412. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11413. (when (ignore-errors (goto-char (org-in-item-p)))
  11414. (let* ((struct (org-list-struct))
  11415. (prevs (org-list-prevs-alist struct)))
  11416. (while (looking-at "[ \t]*- State")
  11417. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11418. (org-list-get-item-end (point) struct)))))))
  11419. (defun org-add-log-note (&optional purpose)
  11420. "Pop up a window for taking a note, and add this note later at point."
  11421. (remove-hook 'post-command-hook 'org-add-log-note)
  11422. (setq org-log-note-window-configuration (current-window-configuration))
  11423. (delete-other-windows)
  11424. (move-marker org-log-note-return-to (point))
  11425. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11426. (goto-char org-log-note-marker)
  11427. (org-switch-to-buffer-other-window "*Org Note*")
  11428. (erase-buffer)
  11429. (if (memq org-log-note-how '(time state))
  11430. (let (current-prefix-arg) (org-store-log-note))
  11431. (let ((org-inhibit-startup t)) (org-mode))
  11432. (insert (format "# Insert note for %s.
  11433. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11434. (cond
  11435. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11436. ((eq org-log-note-purpose 'done) "closed todo item")
  11437. ((eq org-log-note-purpose 'state)
  11438. (format "state change from \"%s\" to \"%s\""
  11439. (or org-log-note-previous-state "")
  11440. (or org-log-note-state "")))
  11441. ((eq org-log-note-purpose 'reschedule)
  11442. "rescheduling")
  11443. ((eq org-log-note-purpose 'delschedule)
  11444. "no longer scheduled")
  11445. ((eq org-log-note-purpose 'redeadline)
  11446. "changing deadline")
  11447. ((eq org-log-note-purpose 'deldeadline)
  11448. "removing deadline")
  11449. ((eq org-log-note-purpose 'refile)
  11450. "refiling")
  11451. ((eq org-log-note-purpose 'note)
  11452. "this entry")
  11453. (t (error "This should not happen")))))
  11454. (if org-log-note-extra (insert org-log-note-extra))
  11455. (org-set-local 'org-finish-function 'org-store-log-note)
  11456. (run-hooks 'org-log-buffer-setup-hook)))
  11457. (defvar org-note-abort nil) ; dynamically scoped
  11458. (defun org-store-log-note ()
  11459. "Finish taking a log note, and insert it to where it belongs."
  11460. (let ((txt (buffer-string))
  11461. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11462. lines ind bul)
  11463. (kill-buffer (current-buffer))
  11464. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11465. (setq txt (replace-match "" t t txt)))
  11466. (if (string-match "\\s-+\\'" txt)
  11467. (setq txt (replace-match "" t t txt)))
  11468. (setq lines (org-split-string txt "\n"))
  11469. (when (and note (string-match "\\S-" note))
  11470. (setq note
  11471. (org-replace-escapes
  11472. note
  11473. (list (cons "%u" (user-login-name))
  11474. (cons "%U" user-full-name)
  11475. (cons "%t" (format-time-string
  11476. (org-time-stamp-format 'long 'inactive)
  11477. org-log-note-effective-time))
  11478. (cons "%T" (format-time-string
  11479. (org-time-stamp-format 'long nil)
  11480. org-log-note-effective-time))
  11481. (cons "%d" (format-time-string
  11482. (org-time-stamp-format nil 'inactive)
  11483. org-log-note-effective-time))
  11484. (cons "%D" (format-time-string
  11485. (org-time-stamp-format nil nil)
  11486. org-log-note-effective-time))
  11487. (cons "%s" (if org-log-note-state
  11488. (concat "\"" org-log-note-state "\"")
  11489. ""))
  11490. (cons "%S" (if org-log-note-previous-state
  11491. (concat "\"" org-log-note-previous-state "\"")
  11492. "\"\"")))))
  11493. (if lines (setq note (concat note " \\\\")))
  11494. (push note lines))
  11495. (when (or current-prefix-arg org-note-abort)
  11496. (when org-log-into-drawer
  11497. (org-remove-empty-drawer-at
  11498. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11499. org-log-note-marker))
  11500. (setq lines nil))
  11501. (when lines
  11502. (with-current-buffer (marker-buffer org-log-note-marker)
  11503. (save-excursion
  11504. (goto-char org-log-note-marker)
  11505. (move-marker org-log-note-marker nil)
  11506. (end-of-line 1)
  11507. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11508. (setq ind (save-excursion
  11509. (if (ignore-errors (goto-char (org-in-item-p)))
  11510. (let ((struct (org-list-struct)))
  11511. (org-list-get-ind
  11512. (org-list-get-top-point struct) struct))
  11513. (skip-chars-backward " \r\t\n")
  11514. (cond
  11515. ((and (org-at-heading-p)
  11516. org-adapt-indentation)
  11517. (1+ (org-current-level)))
  11518. ((org-at-heading-p) 0)
  11519. (t (org-get-indentation))))))
  11520. (setq bul (org-list-bullet-string "-"))
  11521. (org-indent-line-to ind)
  11522. (insert bul (pop lines))
  11523. (let ((ind-body (+ (length bul) ind)))
  11524. (while lines
  11525. (insert "\n")
  11526. (org-indent-line-to ind-body)
  11527. (insert (pop lines))))
  11528. (message "Note stored")
  11529. (org-back-to-heading t)
  11530. (org-cycle-hide-drawers 'children)))))
  11531. (set-window-configuration org-log-note-window-configuration)
  11532. (with-current-buffer (marker-buffer org-log-note-return-to)
  11533. (goto-char org-log-note-return-to))
  11534. (move-marker org-log-note-return-to nil)
  11535. (and org-log-post-message (message "%s" org-log-post-message)))
  11536. (defun org-remove-empty-drawer-at (drawer pos)
  11537. "Remove an empty drawer DRAWER at position POS.
  11538. POS may also be a marker."
  11539. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11540. (save-excursion
  11541. (save-restriction
  11542. (widen)
  11543. (goto-char pos)
  11544. (if (org-in-regexp
  11545. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11546. (replace-match ""))))))
  11547. (defvar org-ts-type nil)
  11548. (defun org-sparse-tree (&optional arg type)
  11549. "Create a sparse tree, prompt for the details.
  11550. This command can create sparse trees. You first need to select the type
  11551. of match used to create the tree:
  11552. t Show all TODO entries.
  11553. T Show entries with a specific TODO keyword.
  11554. m Show entries selected by a tags/property match.
  11555. p Enter a property name and its value (both with completion on existing
  11556. names/values) and show entries with that property.
  11557. r Show entries matching a regular expression (`/' can be used as well).
  11558. b Show deadlines and scheduled items before a date.
  11559. a Show deadlines and scheduled items after a date.
  11560. d Show deadlines due within `org-deadline-warning-days'.
  11561. D Show deadlines and scheduled items between a date range."
  11562. (interactive "P")
  11563. (let (ans kwd value ts-type)
  11564. (setq type (or type org-sparse-tree-default-date-type))
  11565. (setq org-ts-type type)
  11566. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range\n [c]ycle through date types: %s"
  11567. (cond ((eq type 'all) "all timestamps")
  11568. ((eq type 'scheduled) "only scheduled")
  11569. ((eq type 'deadline) "only deadline")
  11570. ((eq type 'active) "only active timestamps")
  11571. ((eq type 'inactive) "only inactive timestamps")
  11572. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11573. (t "scheduled/deadline")))
  11574. (setq ans (read-char-exclusive))
  11575. (cond
  11576. ((equal ans ?c)
  11577. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11578. ((equal ans ?d)
  11579. (call-interactively 'org-check-deadlines))
  11580. ((equal ans ?b)
  11581. (call-interactively 'org-check-before-date))
  11582. ((equal ans ?a)
  11583. (call-interactively 'org-check-after-date))
  11584. ((equal ans ?D)
  11585. (call-interactively 'org-check-dates-range))
  11586. ((equal ans ?t)
  11587. (call-interactively 'org-show-todo-tree))
  11588. ((equal ans ?T)
  11589. (org-show-todo-tree '(4)))
  11590. ((member ans '(?T ?m))
  11591. (call-interactively 'org-match-sparse-tree))
  11592. ((member ans '(?p ?P))
  11593. (setq kwd (org-icompleting-read "Property: "
  11594. (mapcar 'list (org-buffer-property-keys))))
  11595. (setq value (org-icompleting-read "Value: "
  11596. (mapcar 'list (org-property-values kwd))))
  11597. (unless (string-match "\\`{.*}\\'" value)
  11598. (setq value (concat "\"" value "\"")))
  11599. (org-match-sparse-tree arg (concat kwd "=" value)))
  11600. ((member ans '(?r ?R ?/))
  11601. (call-interactively 'org-occur))
  11602. (t (error "No such sparse tree command \"%c\"" ans)))))
  11603. (defvar org-occur-highlights nil
  11604. "List of overlays used for occur matches.")
  11605. (make-variable-buffer-local 'org-occur-highlights)
  11606. (defvar org-occur-parameters nil
  11607. "Parameters of the active org-occur calls.
  11608. This is a list, each call to org-occur pushes as cons cell,
  11609. containing the regular expression and the callback, onto the list.
  11610. The list can contain several entries if `org-occur' has been called
  11611. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11612. will only contain one set of parameters. When the highlights are
  11613. removed (for example with `C-c C-c', or with the next edit (depending
  11614. on `org-remove-highlights-with-change'), this variable is emptied
  11615. as well.")
  11616. (make-variable-buffer-local 'org-occur-parameters)
  11617. (defun org-occur (regexp &optional keep-previous callback)
  11618. "Make a compact tree which shows all matches of REGEXP.
  11619. The tree will show the lines where the regexp matches, and all higher
  11620. headlines above the match. It will also show the heading after the match,
  11621. to make sure editing the matching entry is easy.
  11622. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11623. call to `org-occur' will be kept, to allow stacking of calls to this
  11624. command.
  11625. If CALLBACK is non-nil, it is a function which is called to confirm
  11626. that the match should indeed be shown."
  11627. (interactive "sRegexp: \nP")
  11628. (when (equal regexp "")
  11629. (error "Regexp cannot be empty"))
  11630. (unless keep-previous
  11631. (org-remove-occur-highlights nil nil t))
  11632. (push (cons regexp callback) org-occur-parameters)
  11633. (let ((cnt 0))
  11634. (save-excursion
  11635. (goto-char (point-min))
  11636. (if (or (not keep-previous) ; do not want to keep
  11637. (not org-occur-highlights)) ; no previous matches
  11638. ;; hide everything
  11639. (org-overview))
  11640. (while (re-search-forward regexp nil t)
  11641. (when (or (not callback)
  11642. (save-match-data (funcall callback)))
  11643. (setq cnt (1+ cnt))
  11644. (when org-highlight-sparse-tree-matches
  11645. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11646. (org-show-context 'occur-tree))))
  11647. (when org-remove-highlights-with-change
  11648. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11649. nil 'local))
  11650. (unless org-sparse-tree-open-archived-trees
  11651. (org-hide-archived-subtrees (point-min) (point-max)))
  11652. (run-hooks 'org-occur-hook)
  11653. (if (org-called-interactively-p 'interactive)
  11654. (message "%d match(es) for regexp %s" cnt regexp))
  11655. cnt))
  11656. (defun org-occur-next-match (&optional n reset)
  11657. "Function for `next-error-function' to find sparse tree matches.
  11658. N is the number of matches to move, when negative move backwards.
  11659. RESET is entirely ignored - this function always goes back to the
  11660. starting point when no match is found."
  11661. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11662. (search-func (if (< n 0)
  11663. 'previous-single-char-property-change
  11664. 'next-single-char-property-change))
  11665. (n (abs n))
  11666. (pos (point))
  11667. p1)
  11668. (catch 'exit
  11669. (while (setq p1 (funcall search-func (point) 'org-type))
  11670. (when (equal p1 limit)
  11671. (goto-char pos)
  11672. (error "No more matches"))
  11673. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11674. (setq n (1- n))
  11675. (when (= n 0)
  11676. (goto-char p1)
  11677. (throw 'exit (point))))
  11678. (goto-char p1))
  11679. (goto-char p1)
  11680. (error "No more matches"))))
  11681. (defun org-show-context (&optional key)
  11682. "Make sure point and context are visible.
  11683. How much context is shown depends upon the variables
  11684. `org-show-hierarchy-above', `org-show-following-heading',
  11685. `org-show-entry-below' and `org-show-siblings'."
  11686. (let ((heading-p (org-at-heading-p t))
  11687. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11688. (following-p (org-get-alist-option org-show-following-heading key))
  11689. (entry-p (org-get-alist-option org-show-entry-below key))
  11690. (siblings-p (org-get-alist-option org-show-siblings key)))
  11691. (catch 'exit
  11692. ;; Show heading or entry text
  11693. (if (and heading-p (not entry-p))
  11694. (org-flag-heading nil) ; only show the heading
  11695. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11696. (org-show-hidden-entry))) ; show entire entry
  11697. (when following-p
  11698. ;; Show next sibling, or heading below text
  11699. (save-excursion
  11700. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11701. (org-flag-heading nil))))
  11702. (when siblings-p (org-show-siblings))
  11703. (when hierarchy-p
  11704. ;; show all higher headings, possibly with siblings
  11705. (save-excursion
  11706. (while (and (condition-case nil
  11707. (progn (org-up-heading-all 1) t)
  11708. (error nil))
  11709. (not (bobp)))
  11710. (org-flag-heading nil)
  11711. (when siblings-p (org-show-siblings))))))))
  11712. (defvar org-reveal-start-hook nil
  11713. "Hook run before revealing a location.")
  11714. (defun org-reveal (&optional siblings)
  11715. "Show current entry, hierarchy above it, and the following headline.
  11716. This can be used to show a consistent set of context around locations
  11717. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11718. not t for the search context.
  11719. With optional argument SIBLINGS, on each level of the hierarchy all
  11720. siblings are shown. This repairs the tree structure to what it would
  11721. look like when opened with hierarchical calls to `org-cycle'.
  11722. With double optional argument \\[universal-argument] \\[universal-argument], \
  11723. go to the parent and show the
  11724. entire tree."
  11725. (interactive "P")
  11726. (run-hooks 'org-reveal-start-hook)
  11727. (let ((org-show-hierarchy-above t)
  11728. (org-show-following-heading t)
  11729. (org-show-siblings (if siblings t org-show-siblings)))
  11730. (org-show-context nil))
  11731. (when (equal siblings '(16))
  11732. (save-excursion
  11733. (when (org-up-heading-safe)
  11734. (org-show-subtree)
  11735. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11736. (defun org-highlight-new-match (beg end)
  11737. "Highlight from BEG to END and mark the highlight is an occur headline."
  11738. (let ((ov (make-overlay beg end)))
  11739. (overlay-put ov 'face 'secondary-selection)
  11740. (overlay-put ov 'org-type 'org-occur)
  11741. (push ov org-occur-highlights)))
  11742. (defun org-remove-occur-highlights (&optional beg end noremove)
  11743. "Remove the occur highlights from the buffer.
  11744. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11745. from the `before-change-functions' in the current buffer."
  11746. (interactive)
  11747. (unless org-inhibit-highlight-removal
  11748. (mapc 'delete-overlay org-occur-highlights)
  11749. (setq org-occur-highlights nil)
  11750. (setq org-occur-parameters nil)
  11751. (unless noremove
  11752. (remove-hook 'before-change-functions
  11753. 'org-remove-occur-highlights 'local))))
  11754. ;;;; Priorities
  11755. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11756. "Regular expression matching the priority indicator.")
  11757. (defvar org-remove-priority-next-time nil)
  11758. (defun org-priority-up ()
  11759. "Increase the priority of the current item."
  11760. (interactive)
  11761. (org-priority 'up))
  11762. (defun org-priority-down ()
  11763. "Decrease the priority of the current item."
  11764. (interactive)
  11765. (org-priority 'down))
  11766. (defun org-priority (&optional action show)
  11767. "Change the priority of an item.
  11768. ACTION can be `set', `up', `down', or a character."
  11769. (interactive "P")
  11770. (if (equal action '(4))
  11771. (org-show-priority)
  11772. (unless org-enable-priority-commands
  11773. (error "Priority commands are disabled"))
  11774. (setq action (or action 'set))
  11775. (let (current new news have remove)
  11776. (save-excursion
  11777. (org-back-to-heading t)
  11778. (if (looking-at org-priority-regexp)
  11779. (setq current (string-to-char (match-string 2))
  11780. have t))
  11781. (cond
  11782. ((eq action 'remove)
  11783. (setq remove t new ?\ ))
  11784. ((or (eq action 'set)
  11785. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11786. (if (not (eq action 'set))
  11787. (setq new action)
  11788. (message "Priority %c-%c, SPC to remove: "
  11789. org-highest-priority org-lowest-priority)
  11790. (save-match-data
  11791. (setq new (read-char-exclusive))))
  11792. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11793. (= (upcase org-lowest-priority) org-lowest-priority))
  11794. (setq new (upcase new)))
  11795. (cond ((equal new ?\ ) (setq remove t))
  11796. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11797. (error "Priority must be between `%c' and `%c'"
  11798. org-highest-priority org-lowest-priority))))
  11799. ((eq action 'up)
  11800. (setq new (if have
  11801. (1- current) ; normal cycling
  11802. ;; last priority was empty
  11803. (if (eq last-command this-command)
  11804. org-lowest-priority ; wrap around empty to lowest
  11805. ;; default
  11806. (if org-priority-start-cycle-with-default
  11807. org-default-priority
  11808. (1- org-default-priority))))))
  11809. ((eq action 'down)
  11810. (setq new (if have
  11811. (1+ current) ; normal cycling
  11812. ;; last priority was empty
  11813. (if (eq last-command this-command)
  11814. org-highest-priority ; wrap around empty to highest
  11815. ;; default
  11816. (if org-priority-start-cycle-with-default
  11817. org-default-priority
  11818. (1+ org-default-priority))))))
  11819. (t (error "Invalid action")))
  11820. (if (or (< (upcase new) org-highest-priority)
  11821. (> (upcase new) org-lowest-priority))
  11822. (if (and (memq action '(up down))
  11823. (not have) (not (eq last-command this-command)))
  11824. ;; `new' is from default priority
  11825. (error
  11826. "The default can not be set, see `org-default-priority' why")
  11827. ;; normal cycling: `new' is beyond highest/lowest priority
  11828. ;; and is wrapped around to the empty priority
  11829. (setq remove t)))
  11830. (setq news (format "%c" new))
  11831. (if have
  11832. (if remove
  11833. (replace-match "" t t nil 1)
  11834. (replace-match news t t nil 2))
  11835. (if remove
  11836. (error "No priority cookie found in line")
  11837. (let ((case-fold-search nil))
  11838. (looking-at org-todo-line-regexp))
  11839. (if (match-end 2)
  11840. (progn
  11841. (goto-char (match-end 2))
  11842. (insert " [#" news "]"))
  11843. (goto-char (match-beginning 3))
  11844. (insert "[#" news "] "))))
  11845. (org-preserve-lc (org-set-tags nil 'align)))
  11846. (if remove
  11847. (message "Priority removed")
  11848. (message "Priority of current item set to %s" news)))))
  11849. (defun org-show-priority ()
  11850. "Show the priority of the current item.
  11851. This priority is composed of the main priority given with the [#A] cookies,
  11852. and by additional input from the age of a schedules or deadline entry."
  11853. (interactive)
  11854. (let ((pri (if (eq major-mode 'org-agenda-mode)
  11855. (org-get-at-bol 'priority)
  11856. (save-excursion
  11857. (save-match-data
  11858. (beginning-of-line)
  11859. (and (looking-at org-heading-regexp)
  11860. (org-get-priority (match-string 0))))))))
  11861. (message "Priority is %d" (if pri pri -1000))))
  11862. (defun org-get-priority (s)
  11863. "Find priority cookie and return priority."
  11864. (save-match-data
  11865. (if (functionp org-get-priority-function)
  11866. (funcall org-get-priority-function)
  11867. (if (not (string-match org-priority-regexp s))
  11868. (* 1000 (- org-lowest-priority org-default-priority))
  11869. (* 1000 (- org-lowest-priority
  11870. (string-to-char (match-string 2 s))))))))
  11871. ;;;; Tags
  11872. (defvar org-agenda-archives-mode)
  11873. (defvar org-map-continue-from nil
  11874. "Position from where mapping should continue.
  11875. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11876. (defvar org-scanner-tags nil
  11877. "The current tag list while the tags scanner is running.")
  11878. (defvar org-trust-scanner-tags nil
  11879. "Should `org-get-tags-at' use the tags for the scanner.
  11880. This is for internal dynamical scoping only.
  11881. When this is non-nil, the function `org-get-tags-at' will return the value
  11882. of `org-scanner-tags' instead of building the list by itself. This
  11883. can lead to large speed-ups when the tags scanner is used in a file with
  11884. many entries, and when the list of tags is retrieved, for example to
  11885. obtain a list of properties. Building the tags list for each entry in such
  11886. a file becomes an N^2 operation - but with this variable set, it scales
  11887. as N.")
  11888. (defun org-scan-tags (action matcher todo-only &optional start-level)
  11889. "Scan headline tags with inheritance and produce output ACTION.
  11890. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11891. or `agenda' to produce an entry list for an agenda view. It can also be
  11892. a Lisp form or a function that should be called at each matched headline, in
  11893. this case the return value is a list of all return values from these calls.
  11894. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11895. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11896. only lines with a not-done TODO keyword are included in the output.
  11897. This should be the same variable that was scoped into
  11898. and set by `org-make-tags-matcher' when it constructed MATCHER.
  11899. START-LEVEL can be a string with asterisks, reducing the scope to
  11900. headlines matching this string."
  11901. (require 'org-agenda)
  11902. (let* ((re (concat "^"
  11903. (if start-level
  11904. ;; Get the correct level to match
  11905. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11906. org-outline-regexp)
  11907. " *\\(\\<\\("
  11908. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11909. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11910. (props (list 'face 'default
  11911. 'done-face 'org-agenda-done
  11912. 'undone-face 'default
  11913. 'mouse-face 'highlight
  11914. 'org-not-done-regexp org-not-done-regexp
  11915. 'org-todo-regexp org-todo-regexp
  11916. 'org-complex-heading-regexp org-complex-heading-regexp
  11917. 'help-echo
  11918. (format "mouse-2 or RET jump to org file %s"
  11919. (abbreviate-file-name
  11920. (or (buffer-file-name (buffer-base-buffer))
  11921. (buffer-name (buffer-base-buffer)))))))
  11922. (case-fold-search nil)
  11923. (org-map-continue-from nil)
  11924. lspos tags tags-list
  11925. (tags-alist (list (cons 0 org-file-tags)))
  11926. (llast 0) rtn rtn1 level category i txt
  11927. todo marker entry priority)
  11928. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11929. (setq action (list 'lambda nil action)))
  11930. (save-excursion
  11931. (goto-char (point-min))
  11932. (when (eq action 'sparse-tree)
  11933. (org-overview)
  11934. (org-remove-occur-highlights))
  11935. (while (re-search-forward re nil t)
  11936. (setq org-map-continue-from nil)
  11937. (catch :skip
  11938. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11939. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11940. (goto-char (setq lspos (match-beginning 0)))
  11941. (setq level (org-reduced-level (funcall outline-level))
  11942. category (org-get-category))
  11943. (setq i llast llast level)
  11944. ;; remove tag lists from same and sublevels
  11945. (while (>= i level)
  11946. (when (setq entry (assoc i tags-alist))
  11947. (setq tags-alist (delete entry tags-alist)))
  11948. (setq i (1- i)))
  11949. ;; add the next tags
  11950. (when tags
  11951. (setq tags (org-split-string tags ":")
  11952. tags-alist
  11953. (cons (cons level tags) tags-alist)))
  11954. ;; compile tags for current headline
  11955. (setq tags-list
  11956. (if org-use-tag-inheritance
  11957. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11958. tags)
  11959. org-scanner-tags tags-list)
  11960. (when org-use-tag-inheritance
  11961. (setcdr (car tags-alist)
  11962. (mapcar (lambda (x)
  11963. (setq x (copy-sequence x))
  11964. (org-add-prop-inherited x))
  11965. (cdar tags-alist))))
  11966. (when (and tags org-use-tag-inheritance
  11967. (or (not (eq t org-use-tag-inheritance))
  11968. org-tags-exclude-from-inheritance))
  11969. ;; selective inheritance, remove uninherited ones
  11970. (setcdr (car tags-alist)
  11971. (org-remove-uninherited-tags (cdar tags-alist))))
  11972. (when (and
  11973. ;; eval matcher only when the todo condition is OK
  11974. (and (or (not todo-only) (member todo org-not-done-keywords))
  11975. (let ((case-fold-search t) (org-trust-scanner-tags t))
  11976. (eval matcher)))
  11977. ;; Call the skipper, but return t if it does not skip,
  11978. ;; so that the `and' form continues evaluating
  11979. (progn
  11980. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11981. t)
  11982. ;; Check if timestamps are deselecting this entry
  11983. (or (not todo-only)
  11984. (and (member todo org-not-done-keywords)
  11985. (or (not org-agenda-tags-todo-honor-ignore-options)
  11986. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  11987. ;; select this headline
  11988. (cond
  11989. ((eq action 'sparse-tree)
  11990. (and org-highlight-sparse-tree-matches
  11991. (org-get-heading) (match-end 0)
  11992. (org-highlight-new-match
  11993. (match-beginning 1) (match-end 1)))
  11994. (org-show-context 'tags-tree))
  11995. ((eq action 'agenda)
  11996. (setq txt (org-agenda-format-item
  11997. ""
  11998. (concat
  11999. (if (eq org-tags-match-list-sublevels 'indented)
  12000. (make-string (1- level) ?.) "")
  12001. (org-get-heading))
  12002. category
  12003. tags-list)
  12004. priority (org-get-priority txt))
  12005. (goto-char lspos)
  12006. (setq marker (org-agenda-new-marker))
  12007. (org-add-props txt props
  12008. 'org-marker marker 'org-hd-marker marker 'org-category category
  12009. 'todo-state todo
  12010. 'priority priority 'type "tagsmatch")
  12011. (push txt rtn))
  12012. ((functionp action)
  12013. (setq org-map-continue-from nil)
  12014. (save-excursion
  12015. (setq rtn1 (funcall action))
  12016. (push rtn1 rtn)))
  12017. (t (error "Invalid action")))
  12018. ;; if we are to skip sublevels, jump to end of subtree
  12019. (unless org-tags-match-list-sublevels
  12020. (org-end-of-subtree t)
  12021. (backward-char 1))))
  12022. ;; Get the correct position from where to continue
  12023. (if org-map-continue-from
  12024. (goto-char org-map-continue-from)
  12025. (and (= (point) lspos) (end-of-line 1)))))
  12026. (when (and (eq action 'sparse-tree)
  12027. (not org-sparse-tree-open-archived-trees))
  12028. (org-hide-archived-subtrees (point-min) (point-max)))
  12029. (nreverse rtn)))
  12030. (defun org-remove-uninherited-tags (tags)
  12031. "Remove all tags that are not inherited from the list TAGS."
  12032. (cond
  12033. ((eq org-use-tag-inheritance t)
  12034. (if org-tags-exclude-from-inheritance
  12035. (org-delete-all org-tags-exclude-from-inheritance tags)
  12036. tags))
  12037. ((not org-use-tag-inheritance) nil)
  12038. ((stringp org-use-tag-inheritance)
  12039. (delq nil (mapcar
  12040. (lambda (x)
  12041. (if (and (string-match org-use-tag-inheritance x)
  12042. (not (member x org-tags-exclude-from-inheritance)))
  12043. x nil))
  12044. tags)))
  12045. ((listp org-use-tag-inheritance)
  12046. (delq nil (mapcar
  12047. (lambda (x)
  12048. (if (member x org-use-tag-inheritance) x nil))
  12049. tags)))))
  12050. (defun org-match-sparse-tree (&optional todo-only match)
  12051. "Create a sparse tree according to tags string MATCH.
  12052. MATCH can contain positive and negative selection of tags, like
  12053. \"+WORK+URGENT-WITHBOSS\".
  12054. If optional argument TODO-ONLY is non-nil, only select lines that are
  12055. also TODO lines."
  12056. (interactive "P")
  12057. (org-agenda-prepare-buffers (list (current-buffer)))
  12058. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12059. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12060. (defvar org-cached-props nil)
  12061. (defun org-cached-entry-get (pom property)
  12062. (if (or (eq t org-use-property-inheritance)
  12063. (and (stringp org-use-property-inheritance)
  12064. (string-match org-use-property-inheritance property))
  12065. (and (listp org-use-property-inheritance)
  12066. (member property org-use-property-inheritance)))
  12067. ;; Caching is not possible, check it directly
  12068. (org-entry-get pom property 'inherit)
  12069. ;; Get all properties, so that we can do complicated checks easily
  12070. (cdr (assoc property (or org-cached-props
  12071. (setq org-cached-props
  12072. (org-entry-properties pom)))))))
  12073. (defun org-global-tags-completion-table (&optional files)
  12074. "Return the list of all tags in all agenda buffer/files.
  12075. Optional FILES argument is a list of files which can be used
  12076. instead of the agenda files."
  12077. (save-excursion
  12078. (org-uniquify
  12079. (delq nil
  12080. (apply 'append
  12081. (mapcar
  12082. (lambda (file)
  12083. (set-buffer (find-file-noselect file))
  12084. (append (org-get-buffer-tags)
  12085. (mapcar (lambda (x) (if (stringp (car-safe x))
  12086. (list (car-safe x)) nil))
  12087. org-tag-alist)))
  12088. (if (and files (car files))
  12089. files
  12090. (org-agenda-files))))))))
  12091. (defun org-make-tags-matcher (match)
  12092. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12093. The variable `todo-only' is scoped dynamically into this function.
  12094. It will be set to t if the matcher restricts matching to TODO entries,
  12095. otherwise will not be touched.
  12096. Returns a cons of the selection string MATCH and the constructed
  12097. lisp form implementing the matcher. The matcher is to be evaluated
  12098. at an Org entry, with point on the headline, and returns t if the
  12099. entry matches the selection string MATCH. The returned lisp form
  12100. references two variables with information about the entry, which
  12101. must be bound around the form's evaluation: todo, the TODO keyword
  12102. at the entry (or nil of none); and tags-list, the list of all tags
  12103. at the entry including inherited ones. Additionally, the category
  12104. of the entry (if any) must be specified as the text property
  12105. 'org-category on the headline.
  12106. See also `org-scan-tags'.
  12107. "
  12108. (declare (special todo-only))
  12109. (unless (boundp 'todo-only)
  12110. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12111. (unless match
  12112. ;; Get a new match request, with completion
  12113. (let ((org-last-tags-completion-table
  12114. (org-global-tags-completion-table)))
  12115. (setq match (org-completing-read-no-i
  12116. "Match: " 'org-tags-completion-function nil nil nil
  12117. 'org-tags-history))))
  12118. ;; Parse the string and create a lisp form
  12119. (let ((match0 match)
  12120. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12121. minus tag mm
  12122. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12123. orterms term orlist re-p str-p level-p level-op time-p
  12124. prop-p pn pv po gv rest)
  12125. (if (string-match "/+" match)
  12126. ;; match contains also a todo-matching request
  12127. (progn
  12128. (setq tagsmatch (substring match 0 (match-beginning 0))
  12129. todomatch (substring match (match-end 0)))
  12130. (if (string-match "^!" todomatch)
  12131. (setq todo-only t todomatch (substring todomatch 1)))
  12132. (if (string-match "^\\s-*$" todomatch)
  12133. (setq todomatch nil)))
  12134. ;; only matching tags
  12135. (setq tagsmatch match todomatch nil))
  12136. ;; Make the tags matcher
  12137. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12138. (setq tagsmatcher t)
  12139. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12140. (while (setq term (pop orterms))
  12141. (while (and (equal (substring term -1) "\\") orterms)
  12142. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12143. (while (string-match re term)
  12144. (setq rest (substring term (match-end 0))
  12145. minus (and (match-end 1)
  12146. (equal (match-string 1 term) "-"))
  12147. tag (save-match-data (replace-regexp-in-string
  12148. "\\\\-" "-"
  12149. (match-string 2 term)))
  12150. re-p (equal (string-to-char tag) ?{)
  12151. level-p (match-end 4)
  12152. prop-p (match-end 5)
  12153. mm (cond
  12154. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12155. (level-p
  12156. (setq level-op (org-op-to-function (match-string 3 term)))
  12157. `(,level-op level ,(string-to-number
  12158. (match-string 4 term))))
  12159. (prop-p
  12160. (setq pn (match-string 5 term)
  12161. po (match-string 6 term)
  12162. pv (match-string 7 term)
  12163. re-p (equal (string-to-char pv) ?{)
  12164. str-p (equal (string-to-char pv) ?\")
  12165. time-p (save-match-data
  12166. (string-match "^\"[[<].*[]>]\"$" pv))
  12167. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12168. (if time-p (setq pv (org-matcher-time pv)))
  12169. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12170. (cond
  12171. ((equal pn "CATEGORY")
  12172. (setq gv '(get-text-property (point) 'org-category)))
  12173. ((equal pn "TODO")
  12174. (setq gv 'todo))
  12175. (t
  12176. (setq gv `(org-cached-entry-get nil ,pn))))
  12177. (if re-p
  12178. (if (eq po 'org<>)
  12179. `(not (string-match ,pv (or ,gv "")))
  12180. `(string-match ,pv (or ,gv "")))
  12181. (if str-p
  12182. `(,po (or ,gv "") ,pv)
  12183. `(,po (string-to-number (or ,gv ""))
  12184. ,(string-to-number pv) ))))
  12185. (t `(member ,tag tags-list)))
  12186. mm (if minus (list 'not mm) mm)
  12187. term rest)
  12188. (push mm tagsmatcher))
  12189. (push (if (> (length tagsmatcher) 1)
  12190. (cons 'and tagsmatcher)
  12191. (car tagsmatcher))
  12192. orlist)
  12193. (setq tagsmatcher nil))
  12194. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12195. (setq tagsmatcher
  12196. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12197. ;; Make the todo matcher
  12198. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12199. (setq todomatcher t)
  12200. (setq orterms (org-split-string todomatch "|") orlist nil)
  12201. (while (setq term (pop orterms))
  12202. (while (string-match re term)
  12203. (setq minus (and (match-end 1)
  12204. (equal (match-string 1 term) "-"))
  12205. kwd (match-string 2 term)
  12206. re-p (equal (string-to-char kwd) ?{)
  12207. term (substring term (match-end 0))
  12208. mm (if re-p
  12209. `(string-match ,(substring kwd 1 -1) todo)
  12210. (list 'equal 'todo kwd))
  12211. mm (if minus (list 'not mm) mm))
  12212. (push mm todomatcher))
  12213. (push (if (> (length todomatcher) 1)
  12214. (cons 'and todomatcher)
  12215. (car todomatcher))
  12216. orlist)
  12217. (setq todomatcher nil))
  12218. (setq todomatcher (if (> (length orlist) 1)
  12219. (cons 'or orlist) (car orlist))))
  12220. ;; Return the string and lisp forms of the matcher
  12221. (setq matcher (if todomatcher
  12222. (list 'and tagsmatcher todomatcher)
  12223. tagsmatcher))
  12224. (when todo-only
  12225. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12226. matcher)))
  12227. (cons match0 matcher)))
  12228. (defun org-op-to-function (op &optional stringp)
  12229. "Turn an operator into the appropriate function."
  12230. (setq op
  12231. (cond
  12232. ((equal op "<" ) '(< string< org-time<))
  12233. ((equal op ">" ) '(> org-string> org-time>))
  12234. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12235. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12236. ((member op '("=" "==")) '(= string= org-time=))
  12237. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12238. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12239. (defun org<> (a b) (not (= a b)))
  12240. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12241. (defun org-string>= (a b) (not (string< a b)))
  12242. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12243. (defun org-string<> (a b) (not (string= a b)))
  12244. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12245. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12246. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12247. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12248. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12249. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12250. (defun org-2ft (s)
  12251. "Convert S to a floating point time.
  12252. If S is already a number, just return it. If it is a string, parse
  12253. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12254. (cond
  12255. ((numberp s) s)
  12256. ((stringp s)
  12257. (condition-case nil
  12258. (float-time (apply 'encode-time (org-parse-time-string s)))
  12259. (error 0.)))
  12260. (t 0.)))
  12261. (defun org-time-today ()
  12262. "Time in seconds today at 0:00.
  12263. Returns the float number of seconds since the beginning of the
  12264. epoch to the beginning of today (00:00)."
  12265. (float-time (apply 'encode-time
  12266. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12267. (defun org-matcher-time (s)
  12268. "Interpret a time comparison value."
  12269. (save-match-data
  12270. (cond
  12271. ((string= s "<now>") (float-time))
  12272. ((string= s "<today>") (org-time-today))
  12273. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12274. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12275. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12276. (+ (org-time-today)
  12277. (* (string-to-number (match-string 1 s))
  12278. (cdr (assoc (match-string 2 s)
  12279. '(("d" . 86400.0) ("w" . 604800.0)
  12280. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12281. (t (org-2ft s)))))
  12282. (defun org-match-any-p (re list)
  12283. "Does re match any element of list?"
  12284. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12285. (delq nil list))
  12286. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12287. (defvar org-tags-overlay (make-overlay 1 1))
  12288. (org-detach-overlay org-tags-overlay)
  12289. (defun org-get-local-tags-at (&optional pos)
  12290. "Get a list of tags defined in the current headline."
  12291. (org-get-tags-at pos 'local))
  12292. (defun org-get-local-tags ()
  12293. "Get a list of tags defined in the current headline."
  12294. (org-get-tags-at nil 'local))
  12295. (defun org-get-tags-at (&optional pos local)
  12296. "Get a list of all headline tags applicable at POS.
  12297. POS defaults to point. If tags are inherited, the list contains
  12298. the targets in the same sequence as the headlines appear, i.e.
  12299. the tags of the current headline come last.
  12300. When LOCAL is non-nil, only return tags from the current headline,
  12301. ignore inherited ones."
  12302. (interactive)
  12303. (if (and org-trust-scanner-tags
  12304. (or (not pos) (equal pos (point)))
  12305. (not local))
  12306. org-scanner-tags
  12307. (let (tags ltags lastpos parent)
  12308. (save-excursion
  12309. (save-restriction
  12310. (widen)
  12311. (goto-char (or pos (point)))
  12312. (save-match-data
  12313. (catch 'done
  12314. (condition-case nil
  12315. (progn
  12316. (org-back-to-heading t)
  12317. (while (not (equal lastpos (point)))
  12318. (setq lastpos (point))
  12319. (when (looking-at
  12320. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12321. (setq ltags (org-split-string
  12322. (org-match-string-no-properties 1) ":"))
  12323. (when parent
  12324. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12325. (setq tags (append
  12326. (if parent
  12327. (org-remove-uninherited-tags ltags)
  12328. ltags)
  12329. tags)))
  12330. (or org-use-tag-inheritance (throw 'done t))
  12331. (if local (throw 'done t))
  12332. (or (org-up-heading-safe) (error nil))
  12333. (setq parent t)))
  12334. (error nil)))))
  12335. (if local
  12336. tags
  12337. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12338. (defun org-add-prop-inherited (s)
  12339. (add-text-properties 0 (length s) '(inherited t) s)
  12340. s)
  12341. (defun org-toggle-tag (tag &optional onoff)
  12342. "Toggle the tag TAG for the current line.
  12343. If ONOFF is `on' or `off', don't toggle but set to this state."
  12344. (let (res current)
  12345. (save-excursion
  12346. (org-back-to-heading t)
  12347. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12348. (point-at-eol) t)
  12349. (progn
  12350. (setq current (match-string 1))
  12351. (replace-match ""))
  12352. (setq current ""))
  12353. (setq current (nreverse (org-split-string current ":")))
  12354. (cond
  12355. ((eq onoff 'on)
  12356. (setq res t)
  12357. (or (member tag current) (push tag current)))
  12358. ((eq onoff 'off)
  12359. (or (not (member tag current)) (setq current (delete tag current))))
  12360. (t (if (member tag current)
  12361. (setq current (delete tag current))
  12362. (setq res t)
  12363. (push tag current))))
  12364. (end-of-line 1)
  12365. (if current
  12366. (progn
  12367. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12368. (org-set-tags nil t))
  12369. (delete-horizontal-space))
  12370. (run-hooks 'org-after-tags-change-hook))
  12371. res))
  12372. (defun org-align-tags-here (to-col)
  12373. ;; Assumes that this is a headline
  12374. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12375. (beginning-of-line 1)
  12376. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12377. (< pos (match-beginning 2)))
  12378. (progn
  12379. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12380. (goto-char (match-beginning 1))
  12381. (insert " ")
  12382. (delete-region (point) (1+ (match-beginning 2)))
  12383. (setq ncol (max (current-column)
  12384. (1+ col)
  12385. (if (> to-col 0)
  12386. to-col
  12387. (- (abs to-col) tags-l))))
  12388. (setq p (point))
  12389. (insert (make-string (- ncol (current-column)) ?\ ))
  12390. (setq ncol (current-column))
  12391. (when indent-tabs-mode (tabify p (point-at-eol)))
  12392. (org-move-to-column (min ncol col) t))
  12393. (goto-char pos))))
  12394. (defun org-set-tags-command (&optional arg just-align)
  12395. "Call the set-tags command for the current entry."
  12396. (interactive "P")
  12397. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12398. (org-set-tags arg just-align)
  12399. (save-excursion
  12400. (org-back-to-heading t)
  12401. (org-set-tags arg just-align))))
  12402. (defun org-set-tags-to (data)
  12403. "Set the tags of the current entry to DATA, replacing the current tags.
  12404. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12405. If DATA is nil or the empty string, any tags will be removed."
  12406. (interactive "sTags: ")
  12407. (setq data
  12408. (cond
  12409. ((eq data nil) "")
  12410. ((equal data "") "")
  12411. ((stringp data)
  12412. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12413. ":"))
  12414. ((listp data)
  12415. (concat ":" (mapconcat 'identity data ":") ":"))))
  12416. (when data
  12417. (save-excursion
  12418. (org-back-to-heading t)
  12419. (when (looking-at org-complex-heading-regexp)
  12420. (if (match-end 5)
  12421. (progn
  12422. (goto-char (match-beginning 5))
  12423. (insert data)
  12424. (delete-region (point) (point-at-eol))
  12425. (org-set-tags nil 'align))
  12426. (goto-char (point-at-eol))
  12427. (insert " " data)
  12428. (org-set-tags nil 'align)))
  12429. (beginning-of-line 1)
  12430. (if (looking-at ".*?\\([ \t]+\\)$")
  12431. (delete-region (match-beginning 1) (match-end 1))))))
  12432. (defun org-align-all-tags ()
  12433. "Align the tags i all headings."
  12434. (interactive)
  12435. (save-excursion
  12436. (or (ignore-errors (org-back-to-heading t))
  12437. (outline-next-heading))
  12438. (if (org-at-heading-p)
  12439. (org-set-tags t)
  12440. (message "No headings"))))
  12441. (defvar org-indent-indentation-per-level)
  12442. (defun org-set-tags (&optional arg just-align)
  12443. "Set the tags for the current headline.
  12444. With prefix ARG, realign all tags in headings in the current buffer."
  12445. (interactive "P")
  12446. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12447. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12448. 'region-start-level 'region))
  12449. org-loop-over-headlines-in-active-region)
  12450. (org-map-entries
  12451. ;; We don't use ARG and JUST-ALIGN here these args are not
  12452. ;; useful when looping over headlines
  12453. `(org-set-tags)
  12454. org-loop-over-headlines-in-active-region
  12455. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12456. (let* ((re org-outline-regexp-bol)
  12457. (current (unless arg (org-get-tags-string)))
  12458. (col (current-column))
  12459. (org-setting-tags t)
  12460. table current-tags inherited-tags ; computed below when needed
  12461. tags p0 c0 c1 rpl di tc level)
  12462. (if arg
  12463. (save-excursion
  12464. (goto-char (point-min))
  12465. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12466. (while (re-search-forward re nil t)
  12467. (org-set-tags nil t)
  12468. (end-of-line 1)))
  12469. (message "All tags realigned to column %d" org-tags-column))
  12470. (if just-align
  12471. (setq tags current)
  12472. ;; Get a new set of tags from the user
  12473. (save-excursion
  12474. (setq table (append org-tag-persistent-alist
  12475. (or org-tag-alist (org-get-buffer-tags))
  12476. (and
  12477. org-complete-tags-always-offer-all-agenda-tags
  12478. (org-global-tags-completion-table
  12479. (org-agenda-files))))
  12480. org-last-tags-completion-table table
  12481. current-tags (org-split-string current ":")
  12482. inherited-tags (nreverse
  12483. (nthcdr (length current-tags)
  12484. (nreverse (org-get-tags-at))))
  12485. tags
  12486. (if (or (eq t org-use-fast-tag-selection)
  12487. (and org-use-fast-tag-selection
  12488. (delq nil (mapcar 'cdr table))))
  12489. (org-fast-tag-selection
  12490. current-tags inherited-tags table
  12491. (if org-fast-tag-selection-include-todo
  12492. org-todo-key-alist))
  12493. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12494. (org-trim
  12495. (org-icompleting-read "Tags: "
  12496. 'org-tags-completion-function
  12497. nil nil current 'org-tags-history))))))
  12498. (while (string-match "[-+&]+" tags)
  12499. ;; No boolean logic, just a list
  12500. (setq tags (replace-match ":" t t tags))))
  12501. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12502. (if org-tags-sort-function
  12503. (setq tags (mapconcat 'identity
  12504. (sort (org-split-string
  12505. tags (org-re "[^[:alnum:]_@#%]+"))
  12506. org-tags-sort-function) ":")))
  12507. (if (string-match "\\`[\t ]*\\'" tags)
  12508. (setq tags "")
  12509. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12510. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12511. ;; Insert new tags at the correct column
  12512. (beginning-of-line 1)
  12513. (setq level (or (and (looking-at org-outline-regexp)
  12514. (- (match-end 0) (point) 1))
  12515. 1))
  12516. (cond
  12517. ((and (equal current "") (equal tags "")))
  12518. ((re-search-forward
  12519. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12520. (point-at-eol) t)
  12521. (if (equal tags "")
  12522. (setq rpl "")
  12523. (goto-char (match-beginning 0))
  12524. (setq c0 (current-column)
  12525. ;; compute offset for the case of org-indent-mode active
  12526. di (if org-indent-mode
  12527. (* (1- org-indent-indentation-per-level) (1- level))
  12528. 0)
  12529. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12530. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12531. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12532. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12533. (replace-match rpl t t)
  12534. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12535. tags)
  12536. (t (error "Tags alignment failed")))
  12537. (org-move-to-column col)
  12538. (unless just-align
  12539. (run-hooks 'org-after-tags-change-hook))))))
  12540. (defun org-change-tag-in-region (beg end tag off)
  12541. "Add or remove TAG for each entry in the region.
  12542. This works in the agenda, and also in an org-mode buffer."
  12543. (interactive
  12544. (list (region-beginning) (region-end)
  12545. (let ((org-last-tags-completion-table
  12546. (if (derived-mode-p 'org-mode)
  12547. (org-get-buffer-tags)
  12548. (org-global-tags-completion-table))))
  12549. (org-icompleting-read
  12550. "Tag: " 'org-tags-completion-function nil nil nil
  12551. 'org-tags-history))
  12552. (progn
  12553. (message "[s]et or [r]emove? ")
  12554. (equal (read-char-exclusive) ?r))))
  12555. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12556. (let ((agendap (equal major-mode 'org-agenda-mode))
  12557. l1 l2 m buf pos newhead (cnt 0))
  12558. (goto-char end)
  12559. (setq l2 (1- (org-current-line)))
  12560. (goto-char beg)
  12561. (setq l1 (org-current-line))
  12562. (loop for l from l1 to l2 do
  12563. (org-goto-line l)
  12564. (setq m (get-text-property (point) 'org-hd-marker))
  12565. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12566. (and agendap m))
  12567. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12568. pos (if agendap m (point)))
  12569. (with-current-buffer buf
  12570. (save-excursion
  12571. (save-restriction
  12572. (goto-char pos)
  12573. (setq cnt (1+ cnt))
  12574. (org-toggle-tag tag (if off 'off 'on))
  12575. (setq newhead (org-get-heading)))))
  12576. (and agendap (org-agenda-change-all-lines newhead m))))
  12577. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12578. (defun org-tags-completion-function (string predicate &optional flag)
  12579. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12580. (confirm (lambda (x) (stringp (car x)))))
  12581. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12582. (setq s1 (match-string 1 string)
  12583. s2 (match-string 2 string))
  12584. (setq s1 "" s2 string))
  12585. (cond
  12586. ((eq flag nil)
  12587. ;; try completion
  12588. (setq rtn (try-completion s2 ctable confirm))
  12589. (if (stringp rtn)
  12590. (setq rtn
  12591. (concat s1 s2 (substring rtn (length s2))
  12592. (if (and org-add-colon-after-tag-completion
  12593. (assoc rtn ctable))
  12594. ":" ""))))
  12595. rtn)
  12596. ((eq flag t)
  12597. ;; all-completions
  12598. (all-completions s2 ctable confirm)
  12599. )
  12600. ((eq flag 'lambda)
  12601. ;; exact match?
  12602. (assoc s2 ctable)))
  12603. ))
  12604. (defun org-fast-tag-insert (kwd tags face &optional end)
  12605. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12606. (insert (format "%-12s" (concat kwd ":"))
  12607. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12608. (or end "")))
  12609. (defun org-fast-tag-show-exit (flag)
  12610. (save-excursion
  12611. (org-goto-line 3)
  12612. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12613. (replace-match ""))
  12614. (when flag
  12615. (end-of-line 1)
  12616. (org-move-to-column (- (window-width) 19) t)
  12617. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12618. (defun org-set-current-tags-overlay (current prefix)
  12619. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12620. (if (featurep 'xemacs)
  12621. (org-overlay-display org-tags-overlay (concat prefix s)
  12622. 'secondary-selection)
  12623. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12624. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12625. (defvar org-last-tag-selection-key nil)
  12626. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12627. "Fast tag selection with single keys.
  12628. CURRENT is the current list of tags in the headline, INHERITED is the
  12629. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12630. possibly with grouping information. TODO-TABLE is a similar table with
  12631. TODO keywords, should these have keys assigned to them.
  12632. If the keys are nil, a-z are automatically assigned.
  12633. Returns the new tags string, or nil to not change the current settings."
  12634. (let* ((fulltable (append table todo-table))
  12635. (maxlen (apply 'max (mapcar
  12636. (lambda (x)
  12637. (if (stringp (car x)) (string-width (car x)) 0))
  12638. fulltable)))
  12639. (buf (current-buffer))
  12640. (expert (eq org-fast-tag-selection-single-key 'expert))
  12641. (buffer-tags nil)
  12642. (fwidth (+ maxlen 3 1 3))
  12643. (ncol (/ (- (window-width) 4) fwidth))
  12644. (i-face 'org-done)
  12645. (c-face 'org-todo)
  12646. tg cnt e c char c1 c2 ntable tbl rtn
  12647. ov-start ov-end ov-prefix
  12648. (exit-after-next org-fast-tag-selection-single-key)
  12649. (done-keywords org-done-keywords)
  12650. groups ingroup)
  12651. (save-excursion
  12652. (beginning-of-line 1)
  12653. (if (looking-at
  12654. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12655. (setq ov-start (match-beginning 1)
  12656. ov-end (match-end 1)
  12657. ov-prefix "")
  12658. (setq ov-start (1- (point-at-eol))
  12659. ov-end (1+ ov-start))
  12660. (skip-chars-forward "^\n\r")
  12661. (setq ov-prefix
  12662. (concat
  12663. (buffer-substring (1- (point)) (point))
  12664. (if (> (current-column) org-tags-column)
  12665. " "
  12666. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12667. (move-overlay org-tags-overlay ov-start ov-end)
  12668. (save-window-excursion
  12669. (if expert
  12670. (set-buffer (get-buffer-create " *Org tags*"))
  12671. (delete-other-windows)
  12672. (split-window-vertically)
  12673. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12674. (erase-buffer)
  12675. (org-set-local 'org-done-keywords done-keywords)
  12676. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12677. (org-fast-tag-insert "Current" current c-face "\n\n")
  12678. (org-fast-tag-show-exit exit-after-next)
  12679. (org-set-current-tags-overlay current ov-prefix)
  12680. (setq tbl fulltable char ?a cnt 0)
  12681. (while (setq e (pop tbl))
  12682. (cond
  12683. ((equal (car e) :startgroup)
  12684. (push '() groups) (setq ingroup t)
  12685. (when (not (= cnt 0))
  12686. (setq cnt 0)
  12687. (insert "\n"))
  12688. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12689. ((equal (car e) :endgroup)
  12690. (setq ingroup nil cnt 0)
  12691. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12692. ((equal e '(:newline))
  12693. (when (not (= cnt 0))
  12694. (setq cnt 0)
  12695. (insert "\n")
  12696. (setq e (car tbl))
  12697. (while (equal (car tbl) '(:newline))
  12698. (insert "\n")
  12699. (setq tbl (cdr tbl)))))
  12700. (t
  12701. (setq tg (copy-sequence (car e)) c2 nil)
  12702. (if (cdr e)
  12703. (setq c (cdr e))
  12704. ;; automatically assign a character.
  12705. (setq c1 (string-to-char
  12706. (downcase (substring
  12707. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12708. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12709. (while (or (rassoc char ntable) (rassoc char table))
  12710. (setq char (1+ char)))
  12711. (setq c2 c1))
  12712. (setq c (or c2 char)))
  12713. (if ingroup (push tg (car groups)))
  12714. (setq tg (org-add-props tg nil 'face
  12715. (cond
  12716. ((not (assoc tg table))
  12717. (org-get-todo-face tg))
  12718. ((member tg current) c-face)
  12719. ((member tg inherited) i-face))))
  12720. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12721. (insert "[" c "] " tg (make-string
  12722. (- fwidth 4 (length tg)) ?\ ))
  12723. (push (cons tg c) ntable)
  12724. (when (= (setq cnt (1+ cnt)) ncol)
  12725. (insert "\n")
  12726. (if ingroup (insert " "))
  12727. (setq cnt 0)))))
  12728. (setq ntable (nreverse ntable))
  12729. (insert "\n")
  12730. (goto-char (point-min))
  12731. (if (not expert) (org-fit-window-to-buffer))
  12732. (setq rtn
  12733. (catch 'exit
  12734. (while t
  12735. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12736. (if (not groups) "no " "")
  12737. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12738. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12739. (setq org-last-tag-selection-key c)
  12740. (cond
  12741. ((= c ?\r) (throw 'exit t))
  12742. ((= c ?!)
  12743. (setq groups (not groups))
  12744. (goto-char (point-min))
  12745. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12746. ((= c ?\C-c)
  12747. (if (not expert)
  12748. (org-fast-tag-show-exit
  12749. (setq exit-after-next (not exit-after-next)))
  12750. (setq expert nil)
  12751. (delete-other-windows)
  12752. (set-window-buffer (split-window-vertically) " *Org tags*")
  12753. (org-switch-to-buffer-other-window " *Org tags*")
  12754. (org-fit-window-to-buffer)))
  12755. ((or (= c ?\C-g)
  12756. (and (= c ?q) (not (rassoc c ntable))))
  12757. (org-detach-overlay org-tags-overlay)
  12758. (setq quit-flag t))
  12759. ((= c ?\ )
  12760. (setq current nil)
  12761. (if exit-after-next (setq exit-after-next 'now)))
  12762. ((= c ?\t)
  12763. (condition-case nil
  12764. (setq tg (org-icompleting-read
  12765. "Tag: "
  12766. (or buffer-tags
  12767. (with-current-buffer buf
  12768. (org-get-buffer-tags)))))
  12769. (quit (setq tg "")))
  12770. (when (string-match "\\S-" tg)
  12771. (add-to-list 'buffer-tags (list tg))
  12772. (if (member tg current)
  12773. (setq current (delete tg current))
  12774. (push tg current)))
  12775. (if exit-after-next (setq exit-after-next 'now)))
  12776. ((setq e (rassoc c todo-table) tg (car e))
  12777. (with-current-buffer buf
  12778. (save-excursion (org-todo tg)))
  12779. (if exit-after-next (setq exit-after-next 'now)))
  12780. ((setq e (rassoc c ntable) tg (car e))
  12781. (if (member tg current)
  12782. (setq current (delete tg current))
  12783. (loop for g in groups do
  12784. (if (member tg g)
  12785. (mapc (lambda (x)
  12786. (setq current (delete x current)))
  12787. g)))
  12788. (push tg current))
  12789. (if exit-after-next (setq exit-after-next 'now))))
  12790. ;; Create a sorted list
  12791. (setq current
  12792. (sort current
  12793. (lambda (a b)
  12794. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12795. (if (eq exit-after-next 'now) (throw 'exit t))
  12796. (goto-char (point-min))
  12797. (beginning-of-line 2)
  12798. (delete-region (point) (point-at-eol))
  12799. (org-fast-tag-insert "Current" current c-face)
  12800. (org-set-current-tags-overlay current ov-prefix)
  12801. (while (re-search-forward
  12802. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12803. (setq tg (match-string 1))
  12804. (add-text-properties
  12805. (match-beginning 1) (match-end 1)
  12806. (list 'face
  12807. (cond
  12808. ((member tg current) c-face)
  12809. ((member tg inherited) i-face)
  12810. (t (get-text-property (match-beginning 1) 'face))))))
  12811. (goto-char (point-min)))))
  12812. (org-detach-overlay org-tags-overlay)
  12813. (if rtn
  12814. (mapconcat 'identity current ":")
  12815. nil))))
  12816. (defun org-get-tags-string ()
  12817. "Get the TAGS string in the current headline."
  12818. (unless (org-at-heading-p t)
  12819. (error "Not on a heading"))
  12820. (save-excursion
  12821. (beginning-of-line 1)
  12822. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12823. (org-match-string-no-properties 1)
  12824. "")))
  12825. (defun org-get-tags ()
  12826. "Get the list of tags specified in the current headline."
  12827. (org-split-string (org-get-tags-string) ":"))
  12828. (defun org-get-buffer-tags ()
  12829. "Get a table of all tags used in the buffer, for completion."
  12830. (let (tags)
  12831. (save-excursion
  12832. (goto-char (point-min))
  12833. (while (re-search-forward
  12834. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12835. (when (equal (char-after (point-at-bol 0)) ?*)
  12836. (mapc (lambda (x) (add-to-list 'tags x))
  12837. (org-split-string (org-match-string-no-properties 1) ":")))))
  12838. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12839. (mapcar 'list tags)))
  12840. ;;;; The mapping API
  12841. (defun org-map-entries (func &optional match scope &rest skip)
  12842. "Call FUNC at each headline selected by MATCH in SCOPE.
  12843. FUNC is a function or a lisp form. The function will be called without
  12844. arguments, with the cursor positioned at the beginning of the headline.
  12845. The return values of all calls to the function will be collected and
  12846. returned as a list.
  12847. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12848. does not need to preserve point. After evaluation, the cursor will be
  12849. moved to the end of the line (presumably of the headline of the
  12850. processed entry) and search continues from there. Under some
  12851. circumstances, this may not produce the wanted results. For example,
  12852. if you have removed (e.g. archived) the current (sub)tree it could
  12853. mean that the next entry will be skipped entirely. In such cases, you
  12854. can specify the position from where search should continue by making
  12855. FUNC set the variable `org-map-continue-from' to the desired buffer
  12856. position.
  12857. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12858. Only headlines that are matched by this query will be considered during
  12859. the iteration. When MATCH is nil or t, all headlines will be
  12860. visited by the iteration.
  12861. SCOPE determines the scope of this command. It can be any of:
  12862. nil The current buffer, respecting the restriction if any
  12863. tree The subtree started with the entry at point
  12864. region The entries within the active region, if any
  12865. region-start-level
  12866. The entries within the active region, but only those at
  12867. the same level than the first one.
  12868. file The current buffer, without restriction
  12869. file-with-archives
  12870. The current buffer, and any archives associated with it
  12871. agenda All agenda files
  12872. agenda-with-archives
  12873. All agenda files with any archive files associated with them
  12874. \(file1 file2 ...)
  12875. If this is a list, all files in the list will be scanned
  12876. The remaining args are treated as settings for the skipping facilities of
  12877. the scanner. The following items can be given here:
  12878. archive skip trees with the archive tag.
  12879. comment skip trees with the COMMENT keyword
  12880. function or Emacs Lisp form:
  12881. will be used as value for `org-agenda-skip-function', so whenever
  12882. the function returns t, FUNC will not be called for that
  12883. entry and search will continue from the point where the
  12884. function leaves it.
  12885. If your function needs to retrieve the tags including inherited tags
  12886. at the *current* entry, you can use the value of the variable
  12887. `org-scanner-tags' which will be much faster than getting the value
  12888. with `org-get-tags-at'. If your function gets properties with
  12889. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12890. to t around the call to `org-entry-properties' to get the same speedup.
  12891. Note that if your function moves around to retrieve tags and properties at
  12892. a *different* entry, you cannot use these techniques."
  12893. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12894. (not (org-region-active-p)))
  12895. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12896. (org-agenda-skip-archived-trees (memq 'archive skip))
  12897. (org-agenda-skip-comment-trees (memq 'comment skip))
  12898. (org-agenda-skip-function
  12899. (car (org-delete-all '(comment archive) skip)))
  12900. (org-tags-match-list-sublevels t)
  12901. (start-level (eq scope 'region-start-level))
  12902. matcher file res
  12903. org-todo-keywords-for-agenda
  12904. org-done-keywords-for-agenda
  12905. org-todo-keyword-alist-for-agenda
  12906. org-drawers-for-agenda
  12907. org-tag-alist-for-agenda
  12908. todo-only)
  12909. (cond
  12910. ((eq match t) (setq matcher t))
  12911. ((eq match nil) (setq matcher t))
  12912. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12913. (save-excursion
  12914. (save-restriction
  12915. (cond ((eq scope 'tree)
  12916. (org-back-to-heading t)
  12917. (org-narrow-to-subtree)
  12918. (setq scope nil))
  12919. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12920. (org-region-active-p))
  12921. ;; If needed, set start-level to a string like "2"
  12922. (when start-level
  12923. (save-excursion
  12924. (goto-char (region-beginning))
  12925. (unless (org-at-heading-p) (outline-next-heading))
  12926. (setq start-level (org-current-level))))
  12927. (narrow-to-region (region-beginning)
  12928. (save-excursion
  12929. (goto-char (region-end))
  12930. (unless (and (bolp) (org-at-heading-p))
  12931. (outline-next-heading))
  12932. (point)))
  12933. (setq scope nil)))
  12934. (if (not scope)
  12935. (progn
  12936. (org-agenda-prepare-buffers
  12937. (list (buffer-file-name (current-buffer))))
  12938. (setq res (org-scan-tags func matcher todo-only start-level)))
  12939. ;; Get the right scope
  12940. (cond
  12941. ((and scope (listp scope) (symbolp (car scope)))
  12942. (setq scope (eval scope)))
  12943. ((eq scope 'agenda)
  12944. (setq scope (org-agenda-files t)))
  12945. ((eq scope 'agenda-with-archives)
  12946. (setq scope (org-agenda-files t))
  12947. (setq scope (org-add-archive-files scope)))
  12948. ((eq scope 'file)
  12949. (setq scope (list (buffer-file-name))))
  12950. ((eq scope 'file-with-archives)
  12951. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12952. (org-agenda-prepare-buffers scope)
  12953. (while (setq file (pop scope))
  12954. (with-current-buffer (org-find-base-buffer-visiting file)
  12955. (save-excursion
  12956. (save-restriction
  12957. (widen)
  12958. (goto-char (point-min))
  12959. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  12960. res)))
  12961. ;;;; Properties
  12962. ;;; Setting and retrieving properties
  12963. (defconst org-special-properties
  12964. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12965. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  12966. "The special properties valid in Org-mode.
  12967. These are properties that are not defined in the property drawer,
  12968. but in some other way.")
  12969. (defconst org-default-properties
  12970. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12971. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12972. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12973. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12974. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12975. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12976. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12977. "Some properties that are used by Org-mode for various purposes.
  12978. Being in this list makes sure that they are offered for completion.")
  12979. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12980. "Regular expression matching the first line of a property drawer.")
  12981. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12982. "Regular expression matching the last line of a property drawer.")
  12983. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12984. "Regular expression matching the first line of a property drawer.")
  12985. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12986. "Regular expression matching the first line of a property drawer.")
  12987. (defconst org-property-drawer-re
  12988. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12989. org-property-end-re "\\)\n?")
  12990. "Matches an entire property drawer.")
  12991. (defconst org-clock-drawer-re
  12992. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12993. org-property-end-re "\\)\n?")
  12994. "Matches an entire clock drawer.")
  12995. (defsubst org-re-property (property)
  12996. "Return a regexp matching a PROPERTY line.
  12997. Match group 1 will be set to the value."
  12998. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12999. (defsubst org-re-property-keyword (property)
  13000. "Return a regexp matching a PROPERTY line, possibly with no
  13001. value for the property."
  13002. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13003. (defun org-property-action ()
  13004. "Do an action on properties."
  13005. (interactive)
  13006. (let (c)
  13007. (org-at-property-p)
  13008. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13009. (setq c (read-char-exclusive))
  13010. (cond
  13011. ((equal c ?s)
  13012. (call-interactively 'org-set-property))
  13013. ((equal c ?d)
  13014. (call-interactively 'org-delete-property))
  13015. ((equal c ?D)
  13016. (call-interactively 'org-delete-property-globally))
  13017. ((equal c ?c)
  13018. (call-interactively 'org-compute-property-at-point))
  13019. (t (error "No such property action %c" c)))))
  13020. (defun org-inc-effort ()
  13021. "Increment the value of the effort property in the current entry."
  13022. (interactive)
  13023. (org-set-effort nil t))
  13024. (defun org-set-effort (&optional value increment)
  13025. "Set the effort property of the current entry.
  13026. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13027. 10th allowed value.
  13028. When INCREMENT is non-nil, set the property to the next allowed value."
  13029. (interactive "P")
  13030. (if (equal value 0) (setq value 10))
  13031. (let* ((completion-ignore-case t)
  13032. (prop org-effort-property)
  13033. (cur (org-entry-get nil prop))
  13034. (allowed (org-property-get-allowed-values nil prop 'table))
  13035. (existing (mapcar 'list (org-property-values prop)))
  13036. rpl
  13037. (val (cond
  13038. ((stringp value) value)
  13039. ((and allowed (integerp value))
  13040. (or (car (nth (1- value) allowed))
  13041. (car (org-last allowed))))
  13042. ((and allowed increment)
  13043. (or (caadr (member (list cur) allowed))
  13044. (error "Allowed effort values are not set")))
  13045. (allowed
  13046. (message "Select 1-9,0, [RET%s]: %s"
  13047. (if cur (concat "=" cur) "")
  13048. (mapconcat 'car allowed " "))
  13049. (setq rpl (read-char-exclusive))
  13050. (if (equal rpl ?\r)
  13051. cur
  13052. (setq rpl (- rpl ?0))
  13053. (if (equal rpl 0) (setq rpl 10))
  13054. (if (and (> rpl 0) (<= rpl (length allowed)))
  13055. (car (nth (1- rpl) allowed))
  13056. (org-completing-read "Effort: " allowed nil))))
  13057. (t
  13058. (let (org-completion-use-ido org-completion-use-iswitchb)
  13059. (org-completing-read
  13060. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13061. (concat "[" cur "]") "")
  13062. ": ")
  13063. existing nil nil "" nil cur))))))
  13064. (unless (equal (org-entry-get nil prop) val)
  13065. (org-entry-put nil prop val))
  13066. (save-excursion
  13067. (org-back-to-heading t)
  13068. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13069. (message "%s is now %s" prop val)))
  13070. (defun org-at-property-p ()
  13071. "Is cursor inside a property drawer?"
  13072. (save-excursion
  13073. (beginning-of-line 1)
  13074. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13075. (save-match-data ;; Used by calling procedures
  13076. (let ((p (point))
  13077. (range (unless (org-before-first-heading-p)
  13078. (org-get-property-block))))
  13079. (and range (<= (car range) p) (< p (cdr range))))))))
  13080. (defun org-get-property-block (&optional beg end force)
  13081. "Return the (beg . end) range of the body of the property drawer.
  13082. BEG and END are the beginning and end of the current subtree, or of
  13083. the part before the first headline. If they are not given, they will
  13084. be found. If the drawer does not exist and FORCE is non-nil, create
  13085. the drawer."
  13086. (catch 'exit
  13087. (save-excursion
  13088. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13089. (progn (org-back-to-heading t) (point))))
  13090. (end (or end (and (not (outline-next-heading)) (point-max))
  13091. (point))))
  13092. (goto-char beg)
  13093. (if (re-search-forward org-property-start-re end t)
  13094. (setq beg (1+ (match-end 0)))
  13095. (if force
  13096. (save-excursion
  13097. (org-insert-property-drawer)
  13098. (setq end (progn (outline-next-heading) (point))))
  13099. (throw 'exit nil))
  13100. (goto-char beg)
  13101. (if (re-search-forward org-property-start-re end t)
  13102. (setq beg (1+ (match-end 0)))))
  13103. (if (re-search-forward org-property-end-re end t)
  13104. (setq end (match-beginning 0))
  13105. (or force (throw 'exit nil))
  13106. (goto-char beg)
  13107. (setq end beg)
  13108. (org-indent-line)
  13109. (insert ":END:\n"))
  13110. (cons beg end)))))
  13111. (defun org-entry-properties (&optional pom which specific)
  13112. "Get all properties of the entry at point-or-marker POM.
  13113. This includes the TODO keyword, the tags, time strings for deadline,
  13114. scheduled, and clocking, and any additional properties defined in the
  13115. entry. The return value is an alist, keys may occur multiple times
  13116. if the property key was used several times.
  13117. POM may also be nil, in which case the current entry is used.
  13118. If WHICH is nil or `all', get all properties. If WHICH is
  13119. `special' or `standard', only get that subclass. If WHICH
  13120. is a string only get exactly this property. SPECIFIC can be a string, the
  13121. specific property we are interested in. Specifying it can speed
  13122. things up because then unnecessary parsing is avoided."
  13123. (setq which (or which 'all))
  13124. (org-with-point-at pom
  13125. (let ((clockstr (substring org-clock-string 0 -1))
  13126. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13127. (case-fold-search nil)
  13128. beg end range props sum-props key key1 value string clocksum clocksumt)
  13129. (save-excursion
  13130. (when (condition-case nil
  13131. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13132. (error nil))
  13133. (setq beg (point))
  13134. (setq sum-props (get-text-property (point) 'org-summaries))
  13135. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13136. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13137. (outline-next-heading)
  13138. (setq end (point))
  13139. (when (memq which '(all special))
  13140. ;; Get the special properties, like TODO and tags
  13141. (goto-char beg)
  13142. (when (and (or (not specific) (string= specific "TODO"))
  13143. (looking-at org-todo-line-regexp) (match-end 2))
  13144. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13145. (when (and (or (not specific) (string= specific "PRIORITY"))
  13146. (looking-at org-priority-regexp))
  13147. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13148. (when (or (not specific) (string= specific "FILE"))
  13149. (push (cons "FILE" buffer-file-name) props))
  13150. (when (and (or (not specific) (string= specific "TAGS"))
  13151. (setq value (org-get-tags-string))
  13152. (string-match "\\S-" value))
  13153. (push (cons "TAGS" value) props))
  13154. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13155. (setq value (org-get-tags-at)))
  13156. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13157. ":"))
  13158. props))
  13159. (when (or (not specific) (string= specific "BLOCKED"))
  13160. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13161. (when (or (not specific)
  13162. (member specific
  13163. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13164. "TIMESTAMP" "TIMESTAMP_IA")))
  13165. (catch 'match
  13166. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13167. (setq key (if (match-end 1)
  13168. (substring (org-match-string-no-properties 1)
  13169. 0 -1))
  13170. string (if (equal key clockstr)
  13171. (org-trim
  13172. (buffer-substring-no-properties
  13173. (match-beginning 3) (goto-char
  13174. (point-at-eol))))
  13175. (substring (org-match-string-no-properties 3)
  13176. 1 -1)))
  13177. ;; Get the correct property name from the key. This is
  13178. ;; necessary if the user has configured time keywords.
  13179. (setq key1 (concat key ":"))
  13180. (cond
  13181. ((not key)
  13182. (setq key
  13183. (if (= (char-after (match-beginning 3)) ?\[)
  13184. "TIMESTAMP_IA" "TIMESTAMP")))
  13185. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13186. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13187. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13188. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13189. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13190. (progn
  13191. (push (cons key string) props)
  13192. ;; no need to search further if match is found
  13193. (throw 'match t))
  13194. (when (or (equal key "CLOCK") (not (assoc key props)))
  13195. (push (cons key string) props)))))))
  13196. (when (memq which '(all standard))
  13197. ;; Get the standard properties, like :PROP: ...
  13198. (setq range (org-get-property-block beg end))
  13199. (when range
  13200. (goto-char (car range))
  13201. (while (re-search-forward
  13202. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13203. (cdr range) t)
  13204. (setq key (org-match-string-no-properties 1)
  13205. value (org-trim (or (org-match-string-no-properties 2) "")))
  13206. (unless (member key excluded)
  13207. (push (cons key (or value "")) props)))))
  13208. (if clocksum
  13209. (push (cons "CLOCKSUM"
  13210. (org-columns-number-to-string (/ (float clocksum) 60.)
  13211. 'add_times))
  13212. props))
  13213. (if clocksumt
  13214. (push (cons "CLOCKSUM_T"
  13215. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13216. 'add_times))
  13217. props))
  13218. (unless (assoc "CATEGORY" props)
  13219. (push (cons "CATEGORY" (org-get-category)) props))
  13220. (append sum-props (nreverse props)))))))
  13221. (defun org-entry-get (pom property &optional inherit literal-nil)
  13222. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13223. If INHERIT is non-nil and the entry does not have the property,
  13224. then also check higher levels of the hierarchy.
  13225. If INHERIT is the symbol `selective', use inheritance only if the setting
  13226. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13227. If the property is present but empty, the return value is the empty string.
  13228. If the property is not present at all, nil is returned.
  13229. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13230. do not interpret it as the list atom nil. This is used for inheritance
  13231. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13232. (org-with-point-at pom
  13233. (if (and inherit (if (eq inherit 'selective)
  13234. (org-property-inherit-p property)
  13235. t))
  13236. (org-entry-get-with-inheritance property literal-nil)
  13237. (if (member property org-special-properties)
  13238. ;; We need a special property. Use `org-entry-properties' to
  13239. ;; retrieve it, but specify the wanted property
  13240. (cdr (assoc property (org-entry-properties nil 'special property)))
  13241. (let ((range (org-get-property-block)))
  13242. (when (and range (not (eq (car range) (cdr range))))
  13243. (let* ((props (list (or (assoc property org-file-properties)
  13244. (assoc property org-global-properties)
  13245. (assoc property org-global-properties-fixed))))
  13246. (ap (lambda (key)
  13247. (when (re-search-forward
  13248. (org-re-property key) (cdr range) t)
  13249. (setq props
  13250. (org-update-property-plist
  13251. key
  13252. (if (match-end 1)
  13253. (org-match-string-no-properties 1) "")
  13254. props)))))
  13255. val)
  13256. (goto-char (car range))
  13257. (funcall ap property)
  13258. (goto-char (car range))
  13259. (while (funcall ap (concat property "+")))
  13260. (setq val (cdr (assoc property props)))
  13261. (when val (if literal-nil val (org-not-nil val))))))))))
  13262. (defun org-property-or-variable-value (var &optional inherit)
  13263. "Check if there is a property fixing the value of VAR.
  13264. If yes, return this value. If not, return the current value of the variable."
  13265. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13266. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13267. (read prop)
  13268. (symbol-value var))))
  13269. (defun org-entry-delete (pom property)
  13270. "Delete the property PROPERTY from entry at point-or-marker POM."
  13271. (org-with-point-at pom
  13272. (if (member property org-special-properties)
  13273. nil ; cannot delete these properties.
  13274. (let ((range (org-get-property-block)))
  13275. (if (and range
  13276. (goto-char (car range))
  13277. (re-search-forward
  13278. (org-re-property property)
  13279. (cdr range) t))
  13280. (progn
  13281. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13282. t)
  13283. nil)))))
  13284. ;; Multi-values properties are properties that contain multiple values
  13285. ;; These values are assumed to be single words, separated by whitespace.
  13286. (defun org-entry-add-to-multivalued-property (pom property value)
  13287. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13288. (let* ((old (org-entry-get pom property))
  13289. (values (and old (org-split-string old "[ \t]"))))
  13290. (setq value (org-entry-protect-space value))
  13291. (unless (member value values)
  13292. (setq values (cons value values))
  13293. (org-entry-put pom property
  13294. (mapconcat 'identity values " ")))))
  13295. (defun org-entry-remove-from-multivalued-property (pom property value)
  13296. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13297. (let* ((old (org-entry-get pom property))
  13298. (values (and old (org-split-string old "[ \t]"))))
  13299. (setq value (org-entry-protect-space value))
  13300. (when (member value values)
  13301. (setq values (delete value values))
  13302. (org-entry-put pom property
  13303. (mapconcat 'identity values " ")))))
  13304. (defun org-entry-member-in-multivalued-property (pom property value)
  13305. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13306. (let* ((old (org-entry-get pom property))
  13307. (values (and old (org-split-string old "[ \t]"))))
  13308. (setq value (org-entry-protect-space value))
  13309. (member value values)))
  13310. (defun org-entry-get-multivalued-property (pom property)
  13311. "Return a list of values in a multivalued property."
  13312. (let* ((value (org-entry-get pom property))
  13313. (values (and value (org-split-string value "[ \t]"))))
  13314. (mapcar 'org-entry-restore-space values)))
  13315. (defun org-entry-put-multivalued-property (pom property &rest values)
  13316. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13317. VALUES should be a list of strings. Spaces will be protected."
  13318. (org-entry-put pom property
  13319. (mapconcat 'org-entry-protect-space values " "))
  13320. (let* ((value (org-entry-get pom property))
  13321. (values (and value (org-split-string value "[ \t]"))))
  13322. (mapcar 'org-entry-restore-space values)))
  13323. (defun org-entry-protect-space (s)
  13324. "Protect spaces and newline in string S."
  13325. (while (string-match " " s)
  13326. (setq s (replace-match "%20" t t s)))
  13327. (while (string-match "\n" s)
  13328. (setq s (replace-match "%0A" t t s)))
  13329. s)
  13330. (defun org-entry-restore-space (s)
  13331. "Restore spaces and newline in string S."
  13332. (while (string-match "%20" s)
  13333. (setq s (replace-match " " t t s)))
  13334. (while (string-match "%0A" s)
  13335. (setq s (replace-match "\n" t t s)))
  13336. s)
  13337. (defvar org-entry-property-inherited-from (make-marker)
  13338. "Marker pointing to the entry from where a property was inherited.
  13339. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13340. location of the entry where the inheritance search matched. If there was
  13341. no match, the marker will point nowhere.
  13342. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13343. is set.")
  13344. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13345. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13346. The search will stop at the first ancestor which has the property defined.
  13347. If the value found is \"nil\", return nil to show that the property
  13348. should be considered as undefined (this is the meaning of nil here).
  13349. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13350. (move-marker org-entry-property-inherited-from nil)
  13351. (let (tmp)
  13352. (save-excursion
  13353. (save-restriction
  13354. (widen)
  13355. (catch 'ex
  13356. (while t
  13357. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13358. (or (ignore-errors (org-back-to-heading t))
  13359. (goto-char (point-min)))
  13360. (move-marker org-entry-property-inherited-from (point))
  13361. (throw 'ex tmp))
  13362. (or (ignore-errors (org-up-heading-safe))
  13363. (throw 'ex nil))))))
  13364. (setq tmp (or tmp
  13365. (cdr (assoc property org-file-properties))
  13366. (cdr (assoc property org-global-properties))
  13367. (cdr (assoc property org-global-properties-fixed))))
  13368. (if literal-nil tmp (org-not-nil tmp))))
  13369. (defvar org-property-changed-functions nil
  13370. "Hook called when the value of a property has changed.
  13371. Each hook function should accept two arguments, the name of the property
  13372. and the new value.")
  13373. (defun org-entry-put (pom property value)
  13374. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13375. (org-with-point-at pom
  13376. (org-back-to-heading t)
  13377. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13378. range)
  13379. (cond
  13380. ((equal property "TODO")
  13381. (when (and (stringp value) (string-match "\\S-" value)
  13382. (not (member value org-todo-keywords-1)))
  13383. (error "\"%s\" is not a valid TODO state" value))
  13384. (if (or (not value)
  13385. (not (string-match "\\S-" value)))
  13386. (setq value 'none))
  13387. (org-todo value)
  13388. (org-set-tags nil 'align))
  13389. ((equal property "PRIORITY")
  13390. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13391. (string-to-char value) ?\ ))
  13392. (org-set-tags nil 'align))
  13393. ((equal property "SCHEDULED")
  13394. (if (re-search-forward org-scheduled-time-regexp end t)
  13395. (cond
  13396. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13397. ((eq value 'later) (org-timestamp-change 1 'day))
  13398. (t (call-interactively 'org-schedule)))
  13399. (call-interactively 'org-schedule)))
  13400. ((equal property "DEADLINE")
  13401. (if (re-search-forward org-deadline-time-regexp end t)
  13402. (cond
  13403. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13404. ((eq value 'later) (org-timestamp-change 1 'day))
  13405. (t (call-interactively 'org-deadline)))
  13406. (call-interactively 'org-deadline)))
  13407. ((member property org-special-properties)
  13408. (error "The %s property can not yet be set with `org-entry-put'"
  13409. property))
  13410. (t ; a non-special property
  13411. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13412. (setq range (org-get-property-block beg end 'force))
  13413. (goto-char (car range))
  13414. (if (re-search-forward
  13415. (org-re-property-keyword property) (cdr range) t)
  13416. (progn
  13417. (delete-region (match-beginning 0) (match-end 0))
  13418. (goto-char (match-beginning 0)))
  13419. (goto-char (cdr range))
  13420. (insert "\n")
  13421. (backward-char 1)
  13422. (org-indent-line))
  13423. (insert ":" property ":")
  13424. (and value (insert " " value))
  13425. (org-indent-line)))))
  13426. (run-hook-with-args 'org-property-changed-functions property value)))
  13427. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13428. "Get all property keys in the current buffer.
  13429. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13430. like tags and TODO state.
  13431. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13432. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13433. and others.
  13434. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13435. formats in the current buffer."
  13436. (let (rtn range cfmt s p)
  13437. (save-excursion
  13438. (save-restriction
  13439. (widen)
  13440. (goto-char (point-min))
  13441. (while (re-search-forward org-property-start-re nil t)
  13442. (setq range (org-get-property-block))
  13443. (goto-char (car range))
  13444. (while (re-search-forward
  13445. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13446. (cdr range) t)
  13447. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13448. (outline-next-heading))))
  13449. (when include-specials
  13450. (setq rtn (append org-special-properties rtn)))
  13451. (when include-defaults
  13452. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13453. (add-to-list 'rtn org-effort-property))
  13454. (when include-columns
  13455. (save-excursion
  13456. (save-restriction
  13457. (widen)
  13458. (goto-char (point-min))
  13459. (while (re-search-forward
  13460. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13461. nil t)
  13462. (setq cfmt (match-string 2) s 0)
  13463. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13464. cfmt s)
  13465. (setq s (match-end 0)
  13466. p (match-string 1 cfmt))
  13467. (unless (or (equal p "ITEM")
  13468. (member p org-special-properties))
  13469. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13470. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13471. (defun org-property-values (key)
  13472. "Return a list of all values of property KEY in the current buffer."
  13473. (save-excursion
  13474. (save-restriction
  13475. (widen)
  13476. (goto-char (point-min))
  13477. (let ((re (org-re-property key))
  13478. values)
  13479. (while (re-search-forward re nil t)
  13480. (add-to-list 'values (org-trim (match-string 1))))
  13481. (delete "" values)))))
  13482. (defun org-insert-property-drawer ()
  13483. "Insert a property drawer into the current entry."
  13484. (org-back-to-heading t)
  13485. (looking-at org-outline-regexp)
  13486. (let ((indent (if org-adapt-indentation
  13487. (- (match-end 0) (match-beginning 0))
  13488. 0))
  13489. (beg (point))
  13490. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13491. end hiddenp)
  13492. (outline-next-heading)
  13493. (setq end (point))
  13494. (goto-char beg)
  13495. (while (re-search-forward re end t))
  13496. (setq hiddenp (outline-invisible-p))
  13497. (end-of-line 1)
  13498. (and (equal (char-after) ?\n) (forward-char 1))
  13499. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13500. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13501. ;; just skip this line
  13502. (beginning-of-line 2)
  13503. ;; Drawer start, find the end
  13504. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13505. (beginning-of-line 1)))
  13506. (org-skip-over-state-notes)
  13507. (skip-chars-backward " \t\n\r")
  13508. (if (eq (char-before) ?*) (forward-char 1))
  13509. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13510. (beginning-of-line 0)
  13511. (org-indent-to-column indent)
  13512. (beginning-of-line 2)
  13513. (org-indent-to-column indent)
  13514. (beginning-of-line 0)
  13515. (if hiddenp
  13516. (save-excursion
  13517. (org-back-to-heading t)
  13518. (hide-entry))
  13519. (org-flag-drawer t))))
  13520. (defun org-insert-drawer (&optional arg drawer)
  13521. "Insert a drawer at point.
  13522. Optional argument DRAWER, when non-nil, is a string representing
  13523. drawer's name. Otherwise, the user is prompted for a name.
  13524. If a region is active, insert the drawer around that region
  13525. instead.
  13526. Point is left between drawer's boundaries."
  13527. (interactive "P")
  13528. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13529. "LOGBOOK"))
  13530. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13531. ;; internally by Org. They are meant to be inserted
  13532. ;; automatically.
  13533. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13534. ;; Remove system drawers from list. Note: For some reason,
  13535. ;; `org-completing-read' ignores the predicate while
  13536. ;; `completing-read' handles it fine.
  13537. (drawer (if arg "PROPERTIES"
  13538. (or drawer
  13539. (completing-read
  13540. "Drawer: " org-drawers
  13541. (lambda (d) (not (member d system-drawers))))))))
  13542. (cond
  13543. ;; With C-u, fall back on `org-insert-property-drawer'
  13544. (arg (org-insert-property-drawer))
  13545. ;; With an active region, insert a drawer at point.
  13546. ((not (org-region-active-p))
  13547. (progn
  13548. (unless (bolp) (insert "\n"))
  13549. (insert (format ":%s:\n\n:END:\n" drawer))
  13550. (forward-line -2)))
  13551. ;; Otherwise, insert the drawer at point
  13552. (t
  13553. (let ((rbeg (region-beginning))
  13554. (rend (copy-marker (region-end))))
  13555. (unwind-protect
  13556. (progn
  13557. (goto-char rbeg)
  13558. (beginning-of-line)
  13559. (when (save-excursion
  13560. (re-search-forward org-outline-regexp-bol rend t))
  13561. (error "Drawers cannot contain headlines"))
  13562. ;; Position point at the beginning of the first
  13563. ;; non-blank line in region. Insert drawer's opening
  13564. ;; there, then indent it.
  13565. (org-skip-whitespace)
  13566. (beginning-of-line)
  13567. (insert ":" drawer ":\n")
  13568. (forward-line -1)
  13569. (indent-for-tab-command)
  13570. ;; Move point to the beginning of the first blank line
  13571. ;; after the last non-blank line in region. Insert
  13572. ;; drawer's closing, then indent it.
  13573. (goto-char rend)
  13574. (skip-chars-backward " \r\t\n")
  13575. (insert "\n:END:")
  13576. (deactivate-mark t)
  13577. (indent-for-tab-command)
  13578. (unless (eolp) (insert "\n")))
  13579. ;; Clear marker, whatever the outcome of insertion is.
  13580. (set-marker rend nil)))))))
  13581. (defvar org-property-set-functions-alist nil
  13582. "Property set function alist.
  13583. Each entry should have the following format:
  13584. (PROPERTY . READ-FUNCTION)
  13585. The read function will be called with the same argument as
  13586. `org-completing-read'.")
  13587. (defun org-set-property-function (property)
  13588. "Get the function that should be used to set PROPERTY.
  13589. This is computed according to `org-property-set-functions-alist'."
  13590. (or (cdr (assoc property org-property-set-functions-alist))
  13591. 'org-completing-read))
  13592. (defun org-read-property-value (property)
  13593. "Read PROPERTY value from user."
  13594. (let* ((completion-ignore-case t)
  13595. (allowed (org-property-get-allowed-values nil property 'table))
  13596. (cur (org-entry-get nil property))
  13597. (prompt (concat property " value"
  13598. (if (and cur (string-match "\\S-" cur))
  13599. (concat " [" cur "]") "") ": "))
  13600. (set-function (org-set-property-function property))
  13601. (val (if allowed
  13602. (funcall set-function prompt allowed nil
  13603. (not (get-text-property 0 'org-unrestricted
  13604. (caar allowed))))
  13605. (let (org-completion-use-ido org-completion-use-iswitchb)
  13606. (funcall set-function prompt
  13607. (mapcar 'list (org-property-values property))
  13608. nil nil "" nil cur)))))
  13609. (if (equal val "")
  13610. cur
  13611. val)))
  13612. (defvar org-last-set-property nil)
  13613. (defun org-read-property-name ()
  13614. "Read a property name."
  13615. (let* ((completion-ignore-case t)
  13616. (keys (org-buffer-property-keys nil t t))
  13617. (default-prop (or (save-excursion
  13618. (save-match-data
  13619. (beginning-of-line)
  13620. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13621. (null (string= (match-string 1) "END"))
  13622. (match-string 1))))
  13623. org-last-set-property))
  13624. (property (org-icompleting-read
  13625. (concat "Property"
  13626. (if default-prop (concat " [" default-prop "]") "")
  13627. ": ")
  13628. (mapcar 'list keys)
  13629. nil nil nil nil
  13630. default-prop
  13631. )))
  13632. (if (member property keys)
  13633. property
  13634. (or (cdr (assoc (downcase property)
  13635. (mapcar (lambda (x) (cons (downcase x) x))
  13636. keys)))
  13637. property))))
  13638. (defun org-set-property (property value)
  13639. "In the current entry, set PROPERTY to VALUE.
  13640. When called interactively, this will prompt for a property name, offering
  13641. completion on existing and default properties. And then it will prompt
  13642. for a value, offering completion either on allowed values (via an inherited
  13643. xxx_ALL property) or on existing values in other instances of this property
  13644. in the current file."
  13645. (interactive (list nil nil))
  13646. (let* ((property (or property (org-read-property-name)))
  13647. (value (or value (org-read-property-value property)))
  13648. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13649. (setq org-last-set-property property)
  13650. ;; Possibly postprocess the inserted value:
  13651. (when fn (setq value (funcall fn value)))
  13652. (unless (equal (org-entry-get nil property) value)
  13653. (org-entry-put nil property value))))
  13654. (defun org-delete-property (property)
  13655. "In the current entry, delete PROPERTY."
  13656. (interactive
  13657. (let* ((completion-ignore-case t)
  13658. (prop (org-icompleting-read "Property: "
  13659. (org-entry-properties nil 'standard))))
  13660. (list prop)))
  13661. (message "Property %s %s" property
  13662. (if (org-entry-delete nil property)
  13663. "deleted"
  13664. "was not present in the entry")))
  13665. (defun org-delete-property-globally (property)
  13666. "Remove PROPERTY globally, from all entries."
  13667. (interactive
  13668. (let* ((completion-ignore-case t)
  13669. (prop (org-icompleting-read
  13670. "Globally remove property: "
  13671. (mapcar 'list (org-buffer-property-keys)))))
  13672. (list prop)))
  13673. (save-excursion
  13674. (save-restriction
  13675. (widen)
  13676. (goto-char (point-min))
  13677. (let ((cnt 0))
  13678. (while (re-search-forward
  13679. (org-re-property property)
  13680. nil t)
  13681. (setq cnt (1+ cnt))
  13682. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13683. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13684. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13685. (defun org-compute-property-at-point ()
  13686. "Compute the property at point.
  13687. This looks for an enclosing column format, extracts the operator and
  13688. then applies it to the property in the column format's scope."
  13689. (interactive)
  13690. (unless (org-at-property-p)
  13691. (error "Not at a property"))
  13692. (let ((prop (org-match-string-no-properties 2)))
  13693. (org-columns-get-format-and-top-level)
  13694. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13695. (error "No operator defined for property %s" prop))
  13696. (org-columns-compute prop)))
  13697. (defvar org-property-allowed-value-functions nil
  13698. "Hook for functions supplying allowed values for a specific property.
  13699. The functions must take a single argument, the name of the property, and
  13700. return a flat list of allowed values. If \":ETC\" is one of
  13701. the values, this means that these values are intended as defaults for
  13702. completion, but that other values should be allowed too.
  13703. The functions must return nil if they are not responsible for this
  13704. property.")
  13705. (defun org-property-get-allowed-values (pom property &optional table)
  13706. "Get allowed values for the property PROPERTY.
  13707. When TABLE is non-nil, return an alist that can directly be used for
  13708. completion."
  13709. (let (vals)
  13710. (cond
  13711. ((equal property "TODO")
  13712. (setq vals (org-with-point-at pom
  13713. (append org-todo-keywords-1 '("")))))
  13714. ((equal property "PRIORITY")
  13715. (let ((n org-lowest-priority))
  13716. (while (>= n org-highest-priority)
  13717. (push (char-to-string n) vals)
  13718. (setq n (1- n)))))
  13719. ((member property org-special-properties))
  13720. ((setq vals (run-hook-with-args-until-success
  13721. 'org-property-allowed-value-functions property)))
  13722. (t
  13723. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13724. (when (and vals (string-match "\\S-" vals))
  13725. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13726. (setq vals (mapcar (lambda (x)
  13727. (cond ((stringp x) x)
  13728. ((numberp x) (number-to-string x))
  13729. ((symbolp x) (symbol-name x))
  13730. (t "???")))
  13731. vals)))))
  13732. (when (member ":ETC" vals)
  13733. (setq vals (remove ":ETC" vals))
  13734. (org-add-props (car vals) '(org-unrestricted t)))
  13735. (if table (mapcar 'list vals) vals)))
  13736. (defun org-property-previous-allowed-value (&optional previous)
  13737. "Switch to the next allowed value for this property."
  13738. (interactive)
  13739. (org-property-next-allowed-value t))
  13740. (defun org-property-next-allowed-value (&optional previous)
  13741. "Switch to the next allowed value for this property."
  13742. (interactive)
  13743. (unless (org-at-property-p)
  13744. (error "Not at a property"))
  13745. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  13746. (key (match-string 2))
  13747. (value (match-string 3))
  13748. (allowed (or (org-property-get-allowed-values (point) key)
  13749. (and (member value '("[ ]" "[-]" "[X]"))
  13750. '("[ ]" "[X]"))))
  13751. nval)
  13752. (unless allowed
  13753. (error "Allowed values for this property have not been defined"))
  13754. (if previous (setq allowed (reverse allowed)))
  13755. (if (member value allowed)
  13756. (setq nval (car (cdr (member value allowed)))))
  13757. (setq nval (or nval (car allowed)))
  13758. (if (equal nval value)
  13759. (error "Only one allowed value for this property"))
  13760. (org-at-property-p)
  13761. (replace-match (concat " :" key ": " nval) t t)
  13762. (org-indent-line)
  13763. (beginning-of-line 1)
  13764. (skip-chars-forward " \t")
  13765. (when (equal prop org-effort-property)
  13766. (save-excursion
  13767. (org-back-to-heading t)
  13768. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval)))
  13769. (run-hook-with-args 'org-property-changed-functions key nval)))
  13770. (defun org-find-olp (path &optional this-buffer)
  13771. "Return a marker pointing to the entry at outline path OLP.
  13772. If anything goes wrong, throw an error.
  13773. You can wrap this call to catch the error like this:
  13774. (condition-case msg
  13775. (org-mobile-locate-entry (match-string 4))
  13776. (error (nth 1 msg)))
  13777. The return value will then be either a string with the error message,
  13778. or a marker if everything is OK.
  13779. If THIS-BUFFER is set, the outline path does not contain a file,
  13780. only headings."
  13781. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13782. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13783. (level 1)
  13784. (lmin 1)
  13785. (lmax 1)
  13786. limit re end found pos heading cnt flevel)
  13787. (unless buffer (error "File not found :%s" file))
  13788. (with-current-buffer buffer
  13789. (save-excursion
  13790. (save-restriction
  13791. (widen)
  13792. (setq limit (point-max))
  13793. (goto-char (point-min))
  13794. (while (setq heading (pop path))
  13795. (setq re (format org-complex-heading-regexp-format
  13796. (regexp-quote heading)))
  13797. (setq cnt 0 pos (point))
  13798. (while (re-search-forward re end t)
  13799. (setq level (- (match-end 1) (match-beginning 1)))
  13800. (if (and (>= level lmin) (<= level lmax))
  13801. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13802. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13803. lmax heading))
  13804. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13805. lmax heading))
  13806. (goto-char found)
  13807. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13808. (setq end (save-excursion (org-end-of-subtree t t))))
  13809. (when (org-at-heading-p)
  13810. (point-marker)))))))
  13811. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13812. "Find node HEADING in BUFFER.
  13813. Return a marker to the heading if it was found, or nil if not.
  13814. If POS-ONLY is set, return just the position instead of a marker.
  13815. The heading text must match exact, but it may have a TODO keyword,
  13816. a priority cookie and tags in the standard locations."
  13817. (with-current-buffer (or buffer (current-buffer))
  13818. (save-excursion
  13819. (save-restriction
  13820. (widen)
  13821. (goto-char (point-min))
  13822. (let (case-fold-search)
  13823. (if (re-search-forward
  13824. (format org-complex-heading-regexp-format
  13825. (regexp-quote heading)) nil t)
  13826. (if pos-only
  13827. (match-beginning 0)
  13828. (move-marker (make-marker) (match-beginning 0)))))))))
  13829. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13830. "Find Org node headline HEADING in all .org files in directory DIR.
  13831. When the target headline is found, return a marker to this location."
  13832. (let ((files (directory-files (or dir default-directory)
  13833. nil "\\`[^.#].*\\.org\\'"))
  13834. file visiting m buffer)
  13835. (catch 'found
  13836. (while (setq file (pop files))
  13837. (message "trying %s" file)
  13838. (setq visiting (org-find-base-buffer-visiting file))
  13839. (setq buffer (or visiting (find-file-noselect file)))
  13840. (setq m (org-find-exact-headline-in-buffer
  13841. heading buffer))
  13842. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13843. (and m (throw 'found m))))))
  13844. (defun org-find-entry-with-id (ident)
  13845. "Locate the entry that contains the ID property with exact value IDENT.
  13846. IDENT can be a string, a symbol or a number, this function will search for
  13847. the string representation of it.
  13848. Return the position where this entry starts, or nil if there is no such entry."
  13849. (interactive "sID: ")
  13850. (let ((id (cond
  13851. ((stringp ident) ident)
  13852. ((symbol-name ident) (symbol-name ident))
  13853. ((numberp ident) (number-to-string ident))
  13854. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13855. (case-fold-search nil))
  13856. (save-excursion
  13857. (save-restriction
  13858. (widen)
  13859. (goto-char (point-min))
  13860. (when (re-search-forward
  13861. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13862. nil t)
  13863. (org-back-to-heading t)
  13864. (point))))))
  13865. ;;;; Timestamps
  13866. (defvar org-last-changed-timestamp nil)
  13867. (defvar org-last-inserted-timestamp nil
  13868. "The last time stamp inserted with `org-insert-time-stamp'.")
  13869. (defvar org-time-was-given) ; dynamically scoped parameter
  13870. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13871. (defvar org-ts-what) ; dynamically scoped parameter
  13872. (defun org-time-stamp (arg &optional inactive)
  13873. "Prompt for a date/time and insert a time stamp.
  13874. If the user specifies a time like HH:MM or if this command is
  13875. called with at least one prefix argument, the time stamp contains
  13876. the date and the time. Otherwise, only the date is be included.
  13877. All parts of a date not specified by the user is filled in from
  13878. the current date/time. So if you just press return without
  13879. typing anything, the time stamp will represent the current
  13880. date/time.
  13881. If there is already a timestamp at the cursor, it will be
  13882. modified.
  13883. With two universal prefix arguments, insert an active timestamp
  13884. with the current time without prompting the user."
  13885. (interactive "P")
  13886. (let* ((ts nil)
  13887. (default-time
  13888. ;; Default time is either today, or, when entering a range,
  13889. ;; the range start.
  13890. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13891. (save-excursion
  13892. (re-search-backward
  13893. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13894. (- (point) 20) t)))
  13895. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13896. (current-time)))
  13897. (default-input (and ts (org-get-compact-tod ts)))
  13898. (repeater (save-excursion
  13899. (save-match-data
  13900. (beginning-of-line)
  13901. (when (re-search-forward
  13902. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  13903. (save-excursion (progn (end-of-line) (point))) t)
  13904. (match-string 0)))))
  13905. org-time-was-given org-end-time-was-given time)
  13906. (cond
  13907. ((and (org-at-timestamp-p t)
  13908. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13909. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13910. (insert "--")
  13911. (setq time (let ((this-command this-command))
  13912. (org-read-date arg 'totime nil nil
  13913. default-time default-input inactive)))
  13914. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13915. ((org-at-timestamp-p t)
  13916. (setq time (let ((this-command this-command))
  13917. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13918. (when (org-at-timestamp-p t) ; just to get the match data
  13919. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13920. (replace-match "")
  13921. (setq org-last-changed-timestamp
  13922. (org-insert-time-stamp
  13923. time (or org-time-was-given arg)
  13924. inactive nil nil (list org-end-time-was-given)))
  13925. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13926. (setq org-last-changed-timestamp
  13927. (concat (substring org-last-inserted-timestamp 0 -1)
  13928. " " repeater ">"))))
  13929. (message "Timestamp updated"))
  13930. ((equal arg '(16))
  13931. (org-insert-time-stamp (current-time) t))
  13932. (t
  13933. (setq time (let ((this-command this-command))
  13934. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13935. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13936. nil nil (list org-end-time-was-given))))))
  13937. ;; FIXME: can we use this for something else, like computing time differences?
  13938. (defun org-get-compact-tod (s)
  13939. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13940. (let* ((t1 (match-string 1 s))
  13941. (h1 (string-to-number (match-string 2 s)))
  13942. (m1 (string-to-number (match-string 3 s)))
  13943. (t2 (and (match-end 4) (match-string 5 s)))
  13944. (h2 (and t2 (string-to-number (match-string 6 s))))
  13945. (m2 (and t2 (string-to-number (match-string 7 s))))
  13946. dh dm)
  13947. (if (not t2)
  13948. t1
  13949. (setq dh (- h2 h1) dm (- m2 m1))
  13950. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13951. (concat t1 "+" (number-to-string dh)
  13952. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13953. (defun org-time-stamp-inactive (&optional arg)
  13954. "Insert an inactive time stamp.
  13955. An inactive time stamp is enclosed in square brackets instead of angle
  13956. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13957. does not link to the calendar and cannot be changed with the S-cursor keys.
  13958. So these are more for recording a certain time/date."
  13959. (interactive "P")
  13960. (org-time-stamp arg 'inactive))
  13961. (defvar org-date-ovl (make-overlay 1 1))
  13962. (overlay-put org-date-ovl 'face 'org-date-selected)
  13963. (org-detach-overlay org-date-ovl)
  13964. (defvar org-ans1) ; dynamically scoped parameter
  13965. (defvar org-ans2) ; dynamically scoped parameter
  13966. (defvar org-plain-time-of-day-regexp) ; defined below
  13967. (defvar org-overriding-default-time nil) ; dynamically scoped
  13968. (defvar org-read-date-overlay nil)
  13969. (defvar org-dcst nil) ; dynamically scoped
  13970. (defvar org-read-date-history nil)
  13971. (defvar org-read-date-final-answer nil)
  13972. (defvar org-read-date-analyze-futurep nil)
  13973. (defvar org-read-date-analyze-forced-year nil)
  13974. (defvar org-read-date-inactive)
  13975. (defun org-read-date (&optional org-with-time to-time from-string prompt
  13976. default-time default-input inactive)
  13977. "Read a date, possibly a time, and make things smooth for the user.
  13978. The prompt will suggest to enter an ISO date, but you can also enter anything
  13979. which will at least partially be understood by `parse-time-string'.
  13980. Unrecognized parts of the date will default to the current day, month, year,
  13981. hour and minute. If this command is called to replace a timestamp at point,
  13982. or to enter the second timestamp of a range, the default time is taken
  13983. from the existing stamp. Furthermore, the command prefers the future,
  13984. so if you are giving a date where the year is not given, and the day-month
  13985. combination is already past in the current year, it will assume you
  13986. mean next year. For details, see the manual. A few examples:
  13987. 3-2-5 --> 2003-02-05
  13988. feb 15 --> currentyear-02-15
  13989. 2/15 --> currentyear-02-15
  13990. sep 12 9 --> 2009-09-12
  13991. 12:45 --> today 12:45
  13992. 22 sept 0:34 --> currentyear-09-22 0:34
  13993. 12 --> currentyear-currentmonth-12
  13994. Fri --> nearest Friday (today or later)
  13995. etc.
  13996. Furthermore you can specify a relative date by giving, as the *first* thing
  13997. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  13998. change in days weeks, months, years.
  13999. With a single plus or minus, the date is relative to today. With a double
  14000. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14001. +4d --> four days from today
  14002. +4 --> same as above
  14003. +2w --> two weeks from today
  14004. ++5 --> five days from default date
  14005. The function understands only English month and weekday abbreviations.
  14006. While prompting, a calendar is popped up - you can also select the
  14007. date with the mouse (button 1). The calendar shows a period of three
  14008. months. To scroll it to other months, use the keys `>' and `<'.
  14009. If you don't like the calendar, turn it off with
  14010. \(setq org-read-date-popup-calendar nil)
  14011. With optional argument TO-TIME, the date will immediately be converted
  14012. to an internal time.
  14013. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14014. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14015. still enter a time, and this function will inform the calling routine
  14016. about this change. The calling routine may then choose to change the
  14017. format used to insert the time stamp into the buffer to include the time.
  14018. With optional argument FROM-STRING, read from this string instead from
  14019. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14020. the time/date that is used for everything that is not specified by the
  14021. user."
  14022. (require 'parse-time)
  14023. (let* ((org-time-stamp-rounding-minutes
  14024. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14025. (org-dcst org-display-custom-times)
  14026. (ct (org-current-time))
  14027. (org-def (or org-overriding-default-time default-time ct))
  14028. (org-defdecode (decode-time org-def))
  14029. (dummy (progn
  14030. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14031. (setcar (nthcdr 2 org-defdecode) -1)
  14032. (setcar (nthcdr 1 org-defdecode) 59)
  14033. (setq org-def (apply 'encode-time org-defdecode)
  14034. org-defdecode (decode-time org-def)))))
  14035. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14036. (calendar-frame-setup nil)
  14037. (calendar-setup nil)
  14038. (calendar-move-hook nil)
  14039. (calendar-view-diary-initially-flag nil)
  14040. (calendar-view-holidays-initially-flag nil)
  14041. (timestr (format-time-string
  14042. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14043. (prompt (concat (if prompt (concat prompt " ") "")
  14044. (format "Date+time [%s]: " timestr)))
  14045. ans (org-ans0 "") org-ans1 org-ans2 final)
  14046. (cond
  14047. (from-string (setq ans from-string))
  14048. (org-read-date-popup-calendar
  14049. (save-excursion
  14050. (save-window-excursion
  14051. (calendar)
  14052. (org-eval-in-calendar '(setq cursor-type nil) t)
  14053. (unwind-protect
  14054. (progn
  14055. (calendar-forward-day (- (time-to-days org-def)
  14056. (calendar-absolute-from-gregorian
  14057. (calendar-current-date))))
  14058. (org-eval-in-calendar nil t)
  14059. (let* ((old-map (current-local-map))
  14060. (map (copy-keymap calendar-mode-map))
  14061. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  14062. (org-defkey map (kbd "RET") 'org-calendar-select)
  14063. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14064. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14065. (org-defkey minibuffer-local-map [(meta shift left)]
  14066. (lambda () (interactive)
  14067. (org-eval-in-calendar '(calendar-backward-month 1))))
  14068. (org-defkey minibuffer-local-map [(meta shift right)]
  14069. (lambda () (interactive)
  14070. (org-eval-in-calendar '(calendar-forward-month 1))))
  14071. (org-defkey minibuffer-local-map [(meta shift up)]
  14072. (lambda () (interactive)
  14073. (org-eval-in-calendar '(calendar-backward-year 1))))
  14074. (org-defkey minibuffer-local-map [(meta shift down)]
  14075. (lambda () (interactive)
  14076. (org-eval-in-calendar '(calendar-forward-year 1))))
  14077. (org-defkey minibuffer-local-map [?\e (shift left)]
  14078. (lambda () (interactive)
  14079. (org-eval-in-calendar '(calendar-backward-month 1))))
  14080. (org-defkey minibuffer-local-map [?\e (shift right)]
  14081. (lambda () (interactive)
  14082. (org-eval-in-calendar '(calendar-forward-month 1))))
  14083. (org-defkey minibuffer-local-map [?\e (shift up)]
  14084. (lambda () (interactive)
  14085. (org-eval-in-calendar '(calendar-backward-year 1))))
  14086. (org-defkey minibuffer-local-map [?\e (shift down)]
  14087. (lambda () (interactive)
  14088. (org-eval-in-calendar '(calendar-forward-year 1))))
  14089. (org-defkey minibuffer-local-map [(shift up)]
  14090. (lambda () (interactive)
  14091. (org-eval-in-calendar '(calendar-backward-week 1))))
  14092. (org-defkey minibuffer-local-map [(shift down)]
  14093. (lambda () (interactive)
  14094. (org-eval-in-calendar '(calendar-forward-week 1))))
  14095. (org-defkey minibuffer-local-map [(shift left)]
  14096. (lambda () (interactive)
  14097. (org-eval-in-calendar '(calendar-backward-day 1))))
  14098. (org-defkey minibuffer-local-map [(shift right)]
  14099. (lambda () (interactive)
  14100. (org-eval-in-calendar '(calendar-forward-day 1))))
  14101. (org-defkey minibuffer-local-map ">"
  14102. (lambda () (interactive)
  14103. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14104. (org-defkey minibuffer-local-map "<"
  14105. (lambda () (interactive)
  14106. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14107. (org-defkey minibuffer-local-map "\C-v"
  14108. (lambda () (interactive)
  14109. (org-eval-in-calendar
  14110. '(calendar-scroll-left-three-months 1))))
  14111. (org-defkey minibuffer-local-map "\M-v"
  14112. (lambda () (interactive)
  14113. (org-eval-in-calendar
  14114. '(calendar-scroll-right-three-months 1))))
  14115. (run-hooks 'org-read-date-minibuffer-setup-hook)
  14116. (unwind-protect
  14117. (progn
  14118. (use-local-map map)
  14119. (setq org-read-date-inactive inactive)
  14120. (add-hook 'post-command-hook 'org-read-date-display)
  14121. (setq org-ans0 (read-string prompt default-input
  14122. 'org-read-date-history nil))
  14123. ;; org-ans0: from prompt
  14124. ;; org-ans1: from mouse click
  14125. ;; org-ans2: from calendar motion
  14126. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14127. (remove-hook 'post-command-hook 'org-read-date-display)
  14128. (use-local-map old-map)
  14129. (when org-read-date-overlay
  14130. (delete-overlay org-read-date-overlay)
  14131. (setq org-read-date-overlay nil)))))
  14132. (bury-buffer "*Calendar*")))))
  14133. (t ; Naked prompt only
  14134. (unwind-protect
  14135. (setq ans (read-string prompt default-input
  14136. 'org-read-date-history timestr))
  14137. (when org-read-date-overlay
  14138. (delete-overlay org-read-date-overlay)
  14139. (setq org-read-date-overlay nil)))))
  14140. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14141. (when org-read-date-analyze-forced-year
  14142. (message "Year was forced into %s"
  14143. (if org-read-date-force-compatible-dates
  14144. "compatible range (1970-2037)"
  14145. "range representable on this machine"))
  14146. (ding))
  14147. ;; One round trip to get rid of 34th of August and stuff like that....
  14148. (setq final (decode-time (apply 'encode-time final)))
  14149. (setq org-read-date-final-answer ans)
  14150. (if to-time
  14151. (apply 'encode-time final)
  14152. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14153. (format "%04d-%02d-%02d %02d:%02d"
  14154. (nth 5 final) (nth 4 final) (nth 3 final)
  14155. (nth 2 final) (nth 1 final))
  14156. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14157. (defvar org-def)
  14158. (defvar org-defdecode)
  14159. (defvar org-with-time)
  14160. (defun org-read-date-display ()
  14161. "Display the current date prompt interpretation in the minibuffer."
  14162. (when org-read-date-display-live
  14163. (when org-read-date-overlay
  14164. (delete-overlay org-read-date-overlay))
  14165. (when (minibufferp (current-buffer))
  14166. (save-excursion
  14167. (end-of-line 1)
  14168. (while (not (equal (buffer-substring
  14169. (max (point-min) (- (point) 4)) (point))
  14170. " "))
  14171. (insert " ")))
  14172. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14173. " " (or org-ans1 org-ans2)))
  14174. (org-end-time-was-given nil)
  14175. (f (org-read-date-analyze ans org-def org-defdecode))
  14176. (fmts (if org-dcst
  14177. org-time-stamp-custom-formats
  14178. org-time-stamp-formats))
  14179. (fmt (if (or org-with-time
  14180. (and (boundp 'org-time-was-given) org-time-was-given))
  14181. (cdr fmts)
  14182. (car fmts)))
  14183. (txt (format-time-string fmt (apply 'encode-time f)))
  14184. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14185. (txt (concat "=> " txt)))
  14186. (when (and org-end-time-was-given
  14187. (string-match org-plain-time-of-day-regexp txt))
  14188. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14189. org-end-time-was-given
  14190. (substring txt (match-end 0)))))
  14191. (when org-read-date-analyze-futurep
  14192. (setq txt (concat txt " (=>F)")))
  14193. (setq org-read-date-overlay
  14194. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14195. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14196. (defun org-read-date-analyze (ans org-def org-defdecode)
  14197. "Analyze the combined answer of the date prompt."
  14198. ;; FIXME: cleanup and comment
  14199. (let ((nowdecode (decode-time (current-time)))
  14200. delta deltan deltaw deltadef year month day
  14201. hour minute second wday pm h2 m2 tl wday1
  14202. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14203. (setq org-read-date-analyze-futurep nil
  14204. org-read-date-analyze-forced-year nil)
  14205. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14206. (setq ans "+0"))
  14207. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14208. (setq ans (replace-match "" t t ans)
  14209. deltan (car delta)
  14210. deltaw (nth 1 delta)
  14211. deltadef (nth 2 delta)))
  14212. ;; Check if there is an iso week date in there. If yes, store the
  14213. ;; info and postpone interpreting it until the rest of the parsing
  14214. ;; is done.
  14215. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14216. (setq iso-year (if (match-end 1)
  14217. (org-small-year-to-year
  14218. (string-to-number (match-string 1 ans))))
  14219. iso-weekday (if (match-end 3)
  14220. (string-to-number (match-string 3 ans)))
  14221. iso-week (string-to-number (match-string 2 ans)))
  14222. (setq ans (replace-match "" t t ans)))
  14223. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14224. (when (string-match
  14225. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14226. (setq year (if (match-end 2)
  14227. (string-to-number (match-string 2 ans))
  14228. (progn (setq kill-year t)
  14229. (string-to-number (format-time-string "%Y"))))
  14230. month (string-to-number (match-string 3 ans))
  14231. day (string-to-number (match-string 4 ans)))
  14232. (if (< year 100) (setq year (+ 2000 year)))
  14233. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14234. t nil ans)))
  14235. ;; Help matching dotted european dates
  14236. (when (string-match
  14237. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14238. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14239. (setq kill-year t)
  14240. (string-to-number (format-time-string "%Y")))
  14241. day (string-to-number (match-string 1 ans))
  14242. month (string-to-number (match-string 2 ans))
  14243. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14244. t nil ans)))
  14245. ;; Help matching american dates, like 5/30 or 5/30/7
  14246. (when (string-match
  14247. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14248. (setq year (if (match-end 4)
  14249. (string-to-number (match-string 4 ans))
  14250. (progn (setq kill-year t)
  14251. (string-to-number (format-time-string "%Y"))))
  14252. month (string-to-number (match-string 1 ans))
  14253. day (string-to-number (match-string 2 ans)))
  14254. (if (< year 100) (setq year (+ 2000 year)))
  14255. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14256. t nil ans)))
  14257. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14258. ;; If there is a time with am/pm, and *no* time without it, we convert
  14259. ;; so that matching will be successful.
  14260. (loop for i from 1 to 2 do ; twice, for end time as well
  14261. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14262. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14263. (setq hour (string-to-number (match-string 1 ans))
  14264. minute (if (match-end 3)
  14265. (string-to-number (match-string 3 ans))
  14266. 0)
  14267. pm (equal ?p
  14268. (string-to-char (downcase (match-string 4 ans)))))
  14269. (if (and (= hour 12) (not pm))
  14270. (setq hour 0)
  14271. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14272. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14273. t t ans))))
  14274. ;; Check if a time range is given as a duration
  14275. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14276. (setq hour (string-to-number (match-string 1 ans))
  14277. h2 (+ hour (string-to-number (match-string 3 ans)))
  14278. minute (string-to-number (match-string 2 ans))
  14279. m2 (+ minute (if (match-end 5) (string-to-number
  14280. (match-string 5 ans))0)))
  14281. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14282. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14283. t t ans)))
  14284. ;; Check if there is a time range
  14285. (when (boundp 'org-end-time-was-given)
  14286. (setq org-time-was-given nil)
  14287. (when (and (string-match org-plain-time-of-day-regexp ans)
  14288. (match-end 8))
  14289. (setq org-end-time-was-given (match-string 8 ans))
  14290. (setq ans (concat (substring ans 0 (match-beginning 7))
  14291. (substring ans (match-end 7))))))
  14292. (setq tl (parse-time-string ans)
  14293. day (or (nth 3 tl) (nth 3 org-defdecode))
  14294. month (or (nth 4 tl)
  14295. (if (and org-read-date-prefer-future
  14296. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14297. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14298. (nth 4 org-defdecode)))
  14299. year (or (and (not kill-year) (nth 5 tl))
  14300. (if (and org-read-date-prefer-future
  14301. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14302. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14303. (nth 5 org-defdecode)))
  14304. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14305. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14306. second (or (nth 0 tl) 0)
  14307. wday (nth 6 tl))
  14308. (when (and (eq org-read-date-prefer-future 'time)
  14309. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14310. (equal day (nth 3 nowdecode))
  14311. (equal month (nth 4 nowdecode))
  14312. (equal year (nth 5 nowdecode))
  14313. (nth 2 tl)
  14314. (or (< (nth 2 tl) (nth 2 nowdecode))
  14315. (and (= (nth 2 tl) (nth 2 nowdecode))
  14316. (nth 1 tl)
  14317. (< (nth 1 tl) (nth 1 nowdecode)))))
  14318. (setq day (1+ day)
  14319. futurep t))
  14320. ;; Special date definitions below
  14321. (cond
  14322. (iso-week
  14323. ;; There was an iso week
  14324. (require 'cal-iso)
  14325. (setq futurep nil)
  14326. (setq year (or iso-year year)
  14327. day (or iso-weekday wday 1)
  14328. wday nil ; to make sure that the trigger below does not match
  14329. iso-date (calendar-gregorian-from-absolute
  14330. (calendar-absolute-from-iso
  14331. (list iso-week day year))))
  14332. ; FIXME: Should we also push ISO weeks into the future?
  14333. ; (when (and org-read-date-prefer-future
  14334. ; (not iso-year)
  14335. ; (< (calendar-absolute-from-gregorian iso-date)
  14336. ; (time-to-days (current-time))))
  14337. ; (setq year (1+ year)
  14338. ; iso-date (calendar-gregorian-from-absolute
  14339. ; (calendar-absolute-from-iso
  14340. ; (list iso-week day year)))))
  14341. (setq month (car iso-date)
  14342. year (nth 2 iso-date)
  14343. day (nth 1 iso-date)))
  14344. (deltan
  14345. (setq futurep nil)
  14346. (unless deltadef
  14347. (let ((now (decode-time (current-time))))
  14348. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14349. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14350. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14351. ((equal deltaw "m") (setq month (+ month deltan)))
  14352. ((equal deltaw "y") (setq year (+ year deltan)))))
  14353. ((and wday (not (nth 3 tl)))
  14354. ;; Weekday was given, but no day, so pick that day in the week
  14355. ;; on or after the derived date.
  14356. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14357. (unless (equal wday wday1)
  14358. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14359. (if (and (boundp 'org-time-was-given)
  14360. (nth 2 tl))
  14361. (setq org-time-was-given t))
  14362. (if (< year 100) (setq year (+ 2000 year)))
  14363. ;; Check of the date is representable
  14364. (if org-read-date-force-compatible-dates
  14365. (progn
  14366. (if (< year 1970)
  14367. (setq year 1970 org-read-date-analyze-forced-year t))
  14368. (if (> year 2037)
  14369. (setq year 2037 org-read-date-analyze-forced-year t)))
  14370. (condition-case nil
  14371. (ignore (encode-time second minute hour day month year))
  14372. (error
  14373. (setq year (nth 5 org-defdecode))
  14374. (setq org-read-date-analyze-forced-year t))))
  14375. (setq org-read-date-analyze-futurep futurep)
  14376. (list second minute hour day month year)))
  14377. (defvar parse-time-weekdays)
  14378. (defun org-read-date-get-relative (s today default)
  14379. "Check string S for special relative date string.
  14380. TODAY and DEFAULT are internal times, for today and for a default.
  14381. Return shift list (N what def-flag)
  14382. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14383. N is the number of WHATs to shift.
  14384. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14385. the DEFAULT date rather than TODAY."
  14386. (require 'parse-time)
  14387. (when (and
  14388. (string-match
  14389. (concat
  14390. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14391. "\\([0-9]+\\)?"
  14392. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14393. "\\([ \t]\\|$\\)") s)
  14394. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14395. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14396. (string-to-char (substring (match-string 1 s) -1))
  14397. ?+))
  14398. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14399. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14400. (what (if (match-end 3) (match-string 3 s) "d"))
  14401. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14402. (date (if rel default today))
  14403. (wday (nth 6 (decode-time date)))
  14404. delta)
  14405. (if wday1
  14406. (progn
  14407. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14408. (if (= dir ?-) (setq delta (- delta 7)))
  14409. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14410. (list delta "d" rel))
  14411. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14412. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14413. "Turn a user-specified date into the internal representation.
  14414. The internal representation needed by the calendar is (month day year).
  14415. This is a wrapper to handle the brain-dead convention in calendar that
  14416. user function argument order change dependent on argument order."
  14417. (if (boundp 'calendar-date-style)
  14418. (cond
  14419. ((eq calendar-date-style 'american)
  14420. (list arg1 arg2 arg3))
  14421. ((eq calendar-date-style 'european)
  14422. (list arg2 arg1 arg3))
  14423. ((eq calendar-date-style 'iso)
  14424. (list arg2 arg3 arg1)))
  14425. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14426. (if (org-bound-and-true-p european-calendar-style)
  14427. (list arg2 arg1 arg3)
  14428. (list arg1 arg2 arg3)))))
  14429. (defun org-eval-in-calendar (form &optional keepdate)
  14430. "Eval FORM in the calendar window and return to current window.
  14431. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14432. otherwise stick to the current value of `org-ans2'."
  14433. (let ((sf (selected-frame))
  14434. (sw (selected-window)))
  14435. (select-window (get-buffer-window "*Calendar*" t))
  14436. (eval form)
  14437. (when (and (not keepdate) (calendar-cursor-to-date))
  14438. (let* ((date (calendar-cursor-to-date))
  14439. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14440. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14441. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14442. (select-window sw)
  14443. (org-select-frame-set-input-focus sf)))
  14444. (defun org-calendar-select ()
  14445. "Return to `org-read-date' with the date currently selected.
  14446. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14447. (interactive)
  14448. (when (calendar-cursor-to-date)
  14449. (let* ((date (calendar-cursor-to-date))
  14450. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14451. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14452. (if (active-minibuffer-window) (exit-minibuffer))))
  14453. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14454. "Insert a date stamp for the date given by the internal TIME.
  14455. WITH-HM means use the stamp format that includes the time of the day.
  14456. INACTIVE means use square brackets instead of angular ones, so that the
  14457. stamp will not contribute to the agenda.
  14458. PRE and POST are optional strings to be inserted before and after the
  14459. stamp.
  14460. The command returns the inserted time stamp."
  14461. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14462. stamp)
  14463. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14464. (insert-before-markers (or pre ""))
  14465. (when (listp extra)
  14466. (setq extra (car extra))
  14467. (if (and (stringp extra)
  14468. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14469. (setq extra (format "-%02d:%02d"
  14470. (string-to-number (match-string 1 extra))
  14471. (string-to-number (match-string 2 extra))))
  14472. (setq extra nil)))
  14473. (when extra
  14474. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14475. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14476. (insert-before-markers (or post ""))
  14477. (setq org-last-inserted-timestamp stamp)))
  14478. (defun org-toggle-time-stamp-overlays ()
  14479. "Toggle the use of custom time stamp formats."
  14480. (interactive)
  14481. (setq org-display-custom-times (not org-display-custom-times))
  14482. (unless org-display-custom-times
  14483. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14484. (while (setq p (next-single-property-change p 'display))
  14485. (if (and (get-text-property p 'display)
  14486. (eq (get-text-property p 'face) 'org-date))
  14487. (remove-text-properties
  14488. p (setq p (next-single-property-change p 'display))
  14489. '(display t))))
  14490. (set-buffer-modified-p bmp)))
  14491. (if (featurep 'xemacs)
  14492. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14493. (org-restart-font-lock)
  14494. (setq org-table-may-need-update t)
  14495. (if org-display-custom-times
  14496. (message "Time stamps are overlaid with custom format")
  14497. (message "Time stamp overlays removed")))
  14498. (defun org-display-custom-time (beg end)
  14499. "Overlay modified time stamp format over timestamp between BEG and END."
  14500. (let* ((ts (buffer-substring beg end))
  14501. t1 w1 with-hm tf time str w2 (off 0))
  14502. (save-match-data
  14503. (setq t1 (org-parse-time-string ts t))
  14504. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14505. (setq off (- (match-end 0) (match-beginning 0)))))
  14506. (setq end (- end off))
  14507. (setq w1 (- end beg)
  14508. with-hm (and (nth 1 t1) (nth 2 t1))
  14509. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14510. time (org-fix-decoded-time t1)
  14511. str (org-add-props
  14512. (format-time-string
  14513. (substring tf 1 -1) (apply 'encode-time time))
  14514. nil 'mouse-face 'highlight)
  14515. w2 (length str))
  14516. (if (not (= w2 w1))
  14517. (add-text-properties (1+ beg) (+ 2 beg)
  14518. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14519. (if (featurep 'xemacs)
  14520. (progn
  14521. (put-text-property beg end 'invisible t)
  14522. (put-text-property beg end 'end-glyph (make-glyph str)))
  14523. (put-text-property beg end 'display str))))
  14524. (defun org-translate-time (string)
  14525. "Translate all timestamps in STRING to custom format.
  14526. But do this only if the variable `org-display-custom-times' is set."
  14527. (when org-display-custom-times
  14528. (save-match-data
  14529. (let* ((start 0)
  14530. (re org-ts-regexp-both)
  14531. t1 with-hm inactive tf time str beg end)
  14532. (while (setq start (string-match re string start))
  14533. (setq beg (match-beginning 0)
  14534. end (match-end 0)
  14535. t1 (save-match-data
  14536. (org-parse-time-string (substring string beg end) t))
  14537. with-hm (and (nth 1 t1) (nth 2 t1))
  14538. inactive (equal (substring string beg (1+ beg)) "[")
  14539. tf (funcall (if with-hm 'cdr 'car)
  14540. org-time-stamp-custom-formats)
  14541. time (org-fix-decoded-time t1)
  14542. str (format-time-string
  14543. (concat
  14544. (if inactive "[" "<") (substring tf 1 -1)
  14545. (if inactive "]" ">"))
  14546. (apply 'encode-time time))
  14547. string (replace-match str t t string)
  14548. start (+ start (length str)))))))
  14549. string)
  14550. (defun org-fix-decoded-time (time)
  14551. "Set 0 instead of nil for the first 6 elements of time.
  14552. Don't touch the rest."
  14553. (let ((n 0))
  14554. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14555. (defun org-days-to-time (timestamp-string)
  14556. "Difference between TIMESTAMP-STRING and now in days."
  14557. (- (time-to-days (org-time-string-to-time timestamp-string))
  14558. (time-to-days (current-time))))
  14559. (defun org-deadline-close (timestamp-string &optional ndays)
  14560. "Is the time in TIMESTAMP-STRING close to the current date?"
  14561. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14562. (and (< (org-days-to-time timestamp-string) ndays)
  14563. (not (org-entry-is-done-p))))
  14564. (defun org-get-wdays (ts)
  14565. "Get the deadline lead time appropriate for timestring TS."
  14566. (cond
  14567. ((<= org-deadline-warning-days 0)
  14568. ;; 0 or negative, enforce this value no matter what
  14569. (- org-deadline-warning-days))
  14570. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14571. ;; lead time is specified.
  14572. (floor (* (string-to-number (match-string 1 ts))
  14573. (cdr (assoc (match-string 2 ts)
  14574. '(("d" . 1) ("w" . 7)
  14575. ("m" . 30.4) ("y" . 365.25)))))))
  14576. ;; go for the default.
  14577. (t org-deadline-warning-days)))
  14578. (defun org-calendar-select-mouse (ev)
  14579. "Return to `org-read-date' with the date currently selected.
  14580. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14581. (interactive "e")
  14582. (mouse-set-point ev)
  14583. (when (calendar-cursor-to-date)
  14584. (let* ((date (calendar-cursor-to-date))
  14585. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14586. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14587. (if (active-minibuffer-window) (exit-minibuffer))))
  14588. (defun org-check-deadlines (ndays)
  14589. "Check if there are any deadlines due or past due.
  14590. A deadline is considered due if it happens within `org-deadline-warning-days'
  14591. days from today's date. If the deadline appears in an entry marked DONE,
  14592. it is not shown. The prefix arg NDAYS can be used to test that many
  14593. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14594. (interactive "P")
  14595. (let* ((org-warn-days
  14596. (cond
  14597. ((equal ndays '(4)) 100000)
  14598. (ndays (prefix-numeric-value ndays))
  14599. (t (abs org-deadline-warning-days))))
  14600. (case-fold-search nil)
  14601. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14602. (callback
  14603. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14604. (message "%d deadlines past-due or due within %d days"
  14605. (org-occur regexp nil callback)
  14606. org-warn-days)))
  14607. (defsubst org-re-timestamp (type)
  14608. "Return a regexp for timestamp TYPE.
  14609. Allowed values for TYPE are:
  14610. all: all timestamps
  14611. active: only active timestamps (<...>)
  14612. inactive: only inactive timestamps ([...])
  14613. scheduled: only scheduled timestamps
  14614. deadline: only deadline timestamps
  14615. When TYPE is nil, fall back on returning a regexp that matches
  14616. both scheduled and deadline timestamps."
  14617. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  14618. ((eq type 'active) org-ts-regexp)
  14619. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  14620. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  14621. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14622. ((eq type 'scheduled-or-deadline)
  14623. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  14624. (defun org-check-before-date (date)
  14625. "Check if there are deadlines or scheduled entries before DATE."
  14626. (interactive (list (org-read-date)))
  14627. (let ((case-fold-search nil)
  14628. (regexp (org-re-timestamp org-ts-type))
  14629. (callback
  14630. (lambda () (time-less-p
  14631. (org-time-string-to-time (match-string 1))
  14632. (org-time-string-to-time date)))))
  14633. (message "%d entries before %s"
  14634. (org-occur regexp nil callback) date)))
  14635. (defun org-check-after-date (date)
  14636. "Check if there are deadlines or scheduled entries after DATE."
  14637. (interactive (list (org-read-date)))
  14638. (let ((case-fold-search nil)
  14639. (regexp (org-re-timestamp org-ts-type))
  14640. (callback
  14641. (lambda () (not
  14642. (time-less-p
  14643. (org-time-string-to-time (match-string 1))
  14644. (org-time-string-to-time date))))))
  14645. (message "%d entries after %s"
  14646. (org-occur regexp nil callback) date)))
  14647. (defun org-check-dates-range (start-date end-date)
  14648. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14649. (interactive (list (org-read-date nil nil nil "Range starts")
  14650. (org-read-date nil nil nil "Range end")))
  14651. (let ((case-fold-search nil)
  14652. (regexp (org-re-timestamp org-ts-type))
  14653. (callback
  14654. (lambda ()
  14655. (let ((match (match-string 1)))
  14656. (and
  14657. (not (time-less-p
  14658. (org-time-string-to-time match)
  14659. (org-time-string-to-time start-date)))
  14660. (time-less-p
  14661. (org-time-string-to-time match)
  14662. (org-time-string-to-time end-date)))))))
  14663. (message "%d entries between %s and %s"
  14664. (org-occur regexp nil callback) start-date end-date)))
  14665. (defun org-evaluate-time-range (&optional to-buffer)
  14666. "Evaluate a time range by computing the difference between start and end.
  14667. Normally the result is just printed in the echo area, but with prefix arg
  14668. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14669. If the time range is actually in a table, the result is inserted into the
  14670. next column.
  14671. For time difference computation, a year is assumed to be exactly 365
  14672. days in order to avoid rounding problems."
  14673. (interactive "P")
  14674. (or
  14675. (org-clock-update-time-maybe)
  14676. (save-excursion
  14677. (unless (org-at-date-range-p t)
  14678. (goto-char (point-at-bol))
  14679. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14680. (if (not (org-at-date-range-p t))
  14681. (error "Not at a time-stamp range, and none found in current line")))
  14682. (let* ((ts1 (match-string 1))
  14683. (ts2 (match-string 2))
  14684. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14685. (match-end (match-end 0))
  14686. (time1 (org-time-string-to-time ts1))
  14687. (time2 (org-time-string-to-time ts2))
  14688. (t1 (org-float-time time1))
  14689. (t2 (org-float-time time2))
  14690. (diff (abs (- t2 t1)))
  14691. (negative (< (- t2 t1) 0))
  14692. ;; (ys (floor (* 365 24 60 60)))
  14693. (ds (* 24 60 60))
  14694. (hs (* 60 60))
  14695. (fy "%dy %dd %02d:%02d")
  14696. (fy1 "%dy %dd")
  14697. (fd "%dd %02d:%02d")
  14698. (fd1 "%dd")
  14699. (fh "%02d:%02d")
  14700. y d h m align)
  14701. (if havetime
  14702. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14703. y 0
  14704. d (floor (/ diff ds)) diff (mod diff ds)
  14705. h (floor (/ diff hs)) diff (mod diff hs)
  14706. m (floor (/ diff 60)))
  14707. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14708. y 0
  14709. d (floor (+ (/ diff ds) 0.5))
  14710. h 0 m 0))
  14711. (if (not to-buffer)
  14712. (message "%s" (org-make-tdiff-string y d h m))
  14713. (if (org-at-table-p)
  14714. (progn
  14715. (goto-char match-end)
  14716. (setq align t)
  14717. (and (looking-at " *|") (goto-char (match-end 0))))
  14718. (goto-char match-end))
  14719. (if (looking-at
  14720. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14721. (replace-match ""))
  14722. (if negative (insert " -"))
  14723. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14724. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14725. (insert " " (format fh h m))))
  14726. (if align (org-table-align))
  14727. (message "Time difference inserted")))))
  14728. (defun org-make-tdiff-string (y d h m)
  14729. (let ((fmt "")
  14730. (l nil))
  14731. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14732. l (push y l)))
  14733. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14734. l (push d l)))
  14735. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14736. l (push h l)))
  14737. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14738. l (push m l)))
  14739. (apply 'format fmt (nreverse l))))
  14740. (defun org-time-string-to-time (s &optional buffer pos)
  14741. "Convert a timestamp string into internal time."
  14742. (condition-case errdata
  14743. (apply 'encode-time (org-parse-time-string s))
  14744. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14745. s (if (not (and buffer pos))
  14746. ""
  14747. (format " at %d in buffer `%s'" pos buffer))
  14748. (cdr errdata)))))
  14749. (defun org-time-string-to-seconds (s)
  14750. "Convert a timestamp string to a number of seconds."
  14751. (org-float-time (org-time-string-to-time s)))
  14752. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14753. "Convert a time stamp to an absolute day number.
  14754. If there is a specifier for a cyclic time stamp, get the closest date to
  14755. DAYNR.
  14756. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14757. The variable date is bound by the calendar when this is called."
  14758. (cond
  14759. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14760. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14761. daynr
  14762. (+ daynr 1000)))
  14763. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  14764. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14765. (time-to-days (current-time))) (match-string 0 s)
  14766. prefer show-all))
  14767. (t (time-to-days
  14768. (condition-case errdata
  14769. (apply 'encode-time (org-parse-time-string s))
  14770. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14771. s (if (not (and buffer pos))
  14772. ""
  14773. (format " at %d in buffer `%s'" pos buffer))
  14774. (cdr errdata))))))))
  14775. (defun org-days-to-iso-week (days)
  14776. "Return the iso week number."
  14777. (require 'cal-iso)
  14778. (car (calendar-iso-from-absolute days)))
  14779. (defun org-small-year-to-year (year)
  14780. "Convert 2-digit years into 4-digit years.
  14781. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14782. The year 2000 cannot be abbreviated. Any year larger than 99
  14783. is returned unchanged."
  14784. (if (< year 38)
  14785. (setq year (+ 2000 year))
  14786. (if (< year 100)
  14787. (setq year (+ 1900 year))))
  14788. year)
  14789. (defun org-time-from-absolute (d)
  14790. "Return the time corresponding to date D.
  14791. D may be an absolute day number, or a calendar-type list (month day year)."
  14792. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14793. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14794. (defun org-calendar-holiday ()
  14795. "List of holidays, for Diary display in Org-mode."
  14796. (require 'holidays)
  14797. (let ((hl (funcall
  14798. (if (fboundp 'calendar-check-holidays)
  14799. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14800. (if hl (mapconcat 'identity hl "; "))))
  14801. (defun org-diary-sexp-entry (sexp entry date)
  14802. "Process a SEXP diary ENTRY for DATE."
  14803. (require 'diary-lib)
  14804. (let ((result (if calendar-debug-sexp
  14805. (let ((stack-trace-on-error t))
  14806. (eval (car (read-from-string sexp))))
  14807. (condition-case nil
  14808. (eval (car (read-from-string sexp)))
  14809. (error
  14810. (beep)
  14811. (message "Bad sexp at line %d in %s: %s"
  14812. (org-current-line)
  14813. (buffer-file-name) sexp)
  14814. (sleep-for 2))))))
  14815. (cond ((stringp result) (split-string result "; "))
  14816. ((and (consp result)
  14817. (not (consp (cdr result)))
  14818. (stringp (cdr result))) (cdr result))
  14819. ((and (consp result)
  14820. (stringp (car result))) result)
  14821. (result entry))))
  14822. (defun org-diary-to-ical-string (frombuf)
  14823. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14824. This uses the icalendar.el library."
  14825. (let* ((tmpdir (if (featurep 'xemacs)
  14826. (temp-directory)
  14827. temporary-file-directory))
  14828. (tmpfile (make-temp-name
  14829. (expand-file-name "orgics" tmpdir)))
  14830. buf rtn b e)
  14831. (with-current-buffer frombuf
  14832. (icalendar-export-region (point-min) (point-max) tmpfile)
  14833. (setq buf (find-buffer-visiting tmpfile))
  14834. (set-buffer buf)
  14835. (goto-char (point-min))
  14836. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14837. (setq b (match-beginning 0)))
  14838. (goto-char (point-max))
  14839. (if (re-search-backward "^END:VEVENT" nil t)
  14840. (setq e (match-end 0)))
  14841. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14842. (kill-buffer buf)
  14843. (delete-file tmpfile)
  14844. rtn))
  14845. (defun org-closest-date (start current change prefer show-all)
  14846. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14847. When PREFER is `past', return a date that is either CURRENT or past.
  14848. When PREFER is `future', return a date that is either CURRENT or future.
  14849. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14850. ;; Make the proper lists from the dates
  14851. (catch 'exit
  14852. (let ((a1 '(("h" . hour)
  14853. ("d" . day)
  14854. ("w" . week)
  14855. ("m" . month)
  14856. ("y" . year)))
  14857. (shour (nth 2 (org-parse-time-string start)))
  14858. dn dw sday cday n1 n2 n0
  14859. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14860. (setq start (org-date-to-gregorian start)
  14861. current (org-date-to-gregorian
  14862. (if show-all
  14863. current
  14864. (time-to-days (current-time))))
  14865. sday (calendar-absolute-from-gregorian start)
  14866. cday (calendar-absolute-from-gregorian current))
  14867. (if (<= cday sday) (throw 'exit sday))
  14868. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  14869. (setq dn (string-to-number (match-string 1 change))
  14870. dw (cdr (assoc (match-string 2 change) a1)))
  14871. (error "Invalid change specifier: %s" change))
  14872. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14873. (cond
  14874. ((eq dw 'hour)
  14875. (let ((missing-hours
  14876. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  14877. dn)))
  14878. (setq n1 (if (zerop missing-hours) cday
  14879. (- cday (1+ (floor (/ missing-hours 24)))))
  14880. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  14881. ((eq dw 'day)
  14882. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14883. n2 (+ n1 dn)))
  14884. ((eq dw 'year)
  14885. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14886. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14887. (setq date1 (list m d y1)
  14888. n1 (calendar-absolute-from-gregorian date1)
  14889. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14890. n2 (calendar-absolute-from-gregorian date2)))
  14891. ((eq dw 'month)
  14892. ;; approx number of month between the two dates
  14893. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14894. ;; How often does dn fit in there?
  14895. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14896. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14897. m (+ m nm)
  14898. ny (floor (/ m 12))
  14899. y (+ y ny)
  14900. m (- m (* ny 12)))
  14901. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14902. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14903. (setq m2 (+ m dn) y2 y)
  14904. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14905. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14906. (while (<= n2 cday)
  14907. (setq n1 n2 m m2 y y2)
  14908. (setq m2 (+ m dn) y2 y)
  14909. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14910. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14911. ;; Make sure n1 is the earlier date
  14912. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14913. (if show-all
  14914. (cond
  14915. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14916. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14917. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14918. (cond
  14919. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14920. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14921. (t (if (= cday n1) n1 n2)))))))
  14922. (defun org-date-to-gregorian (date)
  14923. "Turn any specification of DATE into a Gregorian date for the calendar."
  14924. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14925. ((and (listp date) (= (length date) 3)) date)
  14926. ((stringp date)
  14927. (setq date (org-parse-time-string date))
  14928. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14929. ((listp date)
  14930. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14931. (defun org-parse-time-string (s &optional nodefault)
  14932. "Parse the standard Org-mode time string.
  14933. This should be a lot faster than the normal `parse-time-string'.
  14934. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14935. hour and minute fields will be nil if not given."
  14936. (if (string-match org-ts-regexp0 s)
  14937. (list 0
  14938. (if (or (match-beginning 8) (not nodefault))
  14939. (string-to-number (or (match-string 8 s) "0")))
  14940. (if (or (match-beginning 7) (not nodefault))
  14941. (string-to-number (or (match-string 7 s) "0")))
  14942. (string-to-number (match-string 4 s))
  14943. (string-to-number (match-string 3 s))
  14944. (string-to-number (match-string 2 s))
  14945. nil nil nil)
  14946. (error "Not a standard Org-mode time string: %s" s)))
  14947. (defun org-timestamp-up (&optional arg)
  14948. "Increase the date item at the cursor by one.
  14949. If the cursor is on the year, change the year. If it is on the month,
  14950. the day or the time, change that.
  14951. With prefix ARG, change by that many units."
  14952. (interactive "p")
  14953. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14954. (defun org-timestamp-down (&optional arg)
  14955. "Decrease the date item at the cursor by one.
  14956. If the cursor is on the year, change the year. If it is on the month,
  14957. the day or the time, change that.
  14958. With prefix ARG, change by that many units."
  14959. (interactive "p")
  14960. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14961. (defun org-timestamp-up-day (&optional arg)
  14962. "Increase the date in the time stamp by one day.
  14963. With prefix ARG, change that many days."
  14964. (interactive "p")
  14965. (if (and (not (org-at-timestamp-p t))
  14966. (org-at-heading-p))
  14967. (org-todo 'up)
  14968. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14969. (defun org-timestamp-down-day (&optional arg)
  14970. "Decrease the date in the time stamp by one day.
  14971. With prefix ARG, change that many days."
  14972. (interactive "p")
  14973. (if (and (not (org-at-timestamp-p t))
  14974. (org-at-heading-p))
  14975. (org-todo 'down)
  14976. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14977. (defun org-at-timestamp-p (&optional inactive-ok)
  14978. "Determine if the cursor is in or at a timestamp."
  14979. (interactive)
  14980. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14981. (pos (point))
  14982. (ans (or (looking-at tsr)
  14983. (save-excursion
  14984. (skip-chars-backward "^[<\n\r\t")
  14985. (if (> (point) (point-min)) (backward-char 1))
  14986. (and (looking-at tsr)
  14987. (> (- (match-end 0) pos) -1))))))
  14988. (and ans
  14989. (boundp 'org-ts-what)
  14990. (setq org-ts-what
  14991. (cond
  14992. ((= pos (match-beginning 0)) 'bracket)
  14993. ;; Point is considered to be "on the bracket" whether
  14994. ;; it's really on it or right after it.
  14995. ((= pos (1- (match-end 0))) 'bracket)
  14996. ((= pos (match-end 0)) 'after)
  14997. ((org-pos-in-match-range pos 2) 'year)
  14998. ((org-pos-in-match-range pos 3) 'month)
  14999. ((org-pos-in-match-range pos 7) 'hour)
  15000. ((org-pos-in-match-range pos 8) 'minute)
  15001. ((or (org-pos-in-match-range pos 4)
  15002. (org-pos-in-match-range pos 5)) 'day)
  15003. ((and (> pos (or (match-end 8) (match-end 5)))
  15004. (< pos (match-end 0)))
  15005. (- pos (or (match-end 8) (match-end 5))))
  15006. (t 'day))))
  15007. ans))
  15008. (defun org-toggle-timestamp-type ()
  15009. "Toggle the type (<active> or [inactive]) of a time stamp."
  15010. (interactive)
  15011. (when (org-at-timestamp-p t)
  15012. (let ((beg (match-beginning 0)) (end (match-end 0))
  15013. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15014. (save-excursion
  15015. (goto-char beg)
  15016. (while (re-search-forward "[][<>]" end t)
  15017. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15018. t t)))
  15019. (message "Timestamp is now %sactive"
  15020. (if (equal (char-after beg) ?<) "" "in")))))
  15021. (defvar org-clock-history) ; defined in org-clock.el
  15022. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15023. (defun org-timestamp-change (n &optional what updown)
  15024. "Change the date in the time stamp at point.
  15025. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15026. `year', `minute', `second'. If WHAT is not given, the cursor position
  15027. in the timestamp determines what will be changed."
  15028. (let ((origin (point)) origin-cat
  15029. with-hm inactive
  15030. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15031. org-ts-what
  15032. extra rem
  15033. ts time time0 fixnext clrgx)
  15034. (if (not (org-at-timestamp-p t))
  15035. (error "Not at a timestamp"))
  15036. (if (and (not what) (eq org-ts-what 'bracket))
  15037. (org-toggle-timestamp-type)
  15038. ;; Point isn't on brackets. Remember the part of the time-stamp
  15039. ;; the point was in. Indeed, size of time-stamps may change,
  15040. ;; but point must be kept in the same category nonetheless.
  15041. (setq origin-cat org-ts-what)
  15042. (if (and (not what) (not (eq org-ts-what 'day))
  15043. org-display-custom-times
  15044. (get-text-property (point) 'display)
  15045. (not (get-text-property (1- (point)) 'display)))
  15046. (setq org-ts-what 'day))
  15047. (setq org-ts-what (or what org-ts-what)
  15048. inactive (= (char-after (match-beginning 0)) ?\[)
  15049. ts (match-string 0))
  15050. (replace-match "")
  15051. (if (string-match
  15052. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15053. ts)
  15054. (setq extra (match-string 1 ts)))
  15055. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15056. (setq with-hm t))
  15057. (setq time0 (org-parse-time-string ts))
  15058. (when (and updown
  15059. (eq org-ts-what 'minute)
  15060. (not current-prefix-arg))
  15061. ;; This looks like s-up and s-down. Change by one rounding step.
  15062. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15063. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15064. (setcar (cdr time0) (+ (nth 1 time0)
  15065. (if (> n 0) (- rem) (- dm rem))))))
  15066. (setq time
  15067. (encode-time (or (car time0) 0)
  15068. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15069. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15070. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15071. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15072. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15073. (nthcdr 6 time0)))
  15074. (when (and (member org-ts-what '(hour minute))
  15075. extra
  15076. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15077. (setq extra (org-modify-ts-extra
  15078. extra
  15079. (if (eq org-ts-what 'hour) 2 5)
  15080. n dm)))
  15081. (when (integerp org-ts-what)
  15082. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15083. (if (eq what 'calendar)
  15084. (let ((cal-date (org-get-date-from-calendar)))
  15085. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15086. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15087. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15088. (setcar time0 (or (car time0) 0))
  15089. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15090. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15091. (setq time (apply 'encode-time time0))))
  15092. ;; Insert the new time-stamp, and ensure point stays in the same
  15093. ;; category as before (i.e. not after the last position in that
  15094. ;; category).
  15095. (let ((pos (point)))
  15096. ;; Stay before inserted string. `save-excursion' is of no use.
  15097. (setq org-last-changed-timestamp
  15098. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15099. (goto-char pos))
  15100. (save-match-data
  15101. (looking-at org-ts-regexp3)
  15102. (goto-char (cond
  15103. ;; `day' category ends before `hour' if any, or at
  15104. ;; the end of the day name.
  15105. ((eq origin-cat 'day)
  15106. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15107. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15108. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15109. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15110. ;; `year' and `month' have both fixed size: point
  15111. ;; couldn't have moved into another part.
  15112. (t origin))))
  15113. ;; Update clock if on a CLOCK line.
  15114. (org-clock-update-time-maybe)
  15115. ;; Maybe adjust the closest clock in `org-clock-history'
  15116. (when org-clock-adjust-closest
  15117. (if (not (and (org-at-clock-log-p)
  15118. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15119. org-clock-history))))))
  15120. (message "No clock to adjust")
  15121. (cond ((save-excursion ; fix previous clock?
  15122. (re-search-backward org-ts-regexp0 nil t)
  15123. (org-looking-back (concat org-clock-string " \\[")))
  15124. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15125. ((save-excursion ; fix next clock?
  15126. (re-search-backward org-ts-regexp0 nil t)
  15127. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15128. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15129. (save-window-excursion
  15130. ;; Find closest clock to point, adjust the previous/next one in history
  15131. (let* ((p (save-excursion (org-back-to-heading t)))
  15132. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15133. (clfixnth
  15134. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15135. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15136. (if (not clfixpos)
  15137. (message "No clock to adjust")
  15138. (save-excursion
  15139. (org-goto-marker-or-bmk clfixpos)
  15140. (org-show-subtree)
  15141. (when (re-search-forward clrgx nil t)
  15142. (goto-char (match-beginning 1))
  15143. (let (org-clock-adjust-closest)
  15144. (org-timestamp-change n org-ts-what updown))
  15145. (message "Clock adjusted in %s for heading: %s"
  15146. (file-name-nondirectory (buffer-file-name))
  15147. (org-get-heading t t)))))))))
  15148. ;; Try to recenter the calendar window, if any.
  15149. (if (and org-calendar-follow-timestamp-change
  15150. (get-buffer-window "*Calendar*" t)
  15151. (memq org-ts-what '(day month year)))
  15152. (org-recenter-calendar (time-to-days time))))))
  15153. (defun org-modify-ts-extra (s pos n dm)
  15154. "Change the different parts of the lead-time and repeat fields in timestamp."
  15155. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15156. ng h m new rem)
  15157. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15158. (cond
  15159. ((or (org-pos-in-match-range pos 2)
  15160. (org-pos-in-match-range pos 3))
  15161. (setq m (string-to-number (match-string 3 s))
  15162. h (string-to-number (match-string 2 s)))
  15163. (if (org-pos-in-match-range pos 2)
  15164. (setq h (+ h n))
  15165. (setq n (* dm (org-no-warnings (signum n))))
  15166. (when (not (= 0 (setq rem (% m dm))))
  15167. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15168. (setq m (+ m n)))
  15169. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15170. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15171. (setq h (min 24 (max 0 h)))
  15172. (setq ng 1 new (format "-%02d:%02d" h m)))
  15173. ((org-pos-in-match-range pos 6)
  15174. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15175. ((org-pos-in-match-range pos 5)
  15176. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15177. ((org-pos-in-match-range pos 9)
  15178. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15179. ((org-pos-in-match-range pos 8)
  15180. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15181. (when ng
  15182. (setq s (concat
  15183. (substring s 0 (match-beginning ng))
  15184. new
  15185. (substring s (match-end ng))))))
  15186. s))
  15187. (defun org-recenter-calendar (date)
  15188. "If the calendar is visible, recenter it to DATE."
  15189. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15190. (when cwin
  15191. (let ((calendar-move-hook nil))
  15192. (with-selected-window cwin
  15193. (calendar-goto-date (if (listp date) date
  15194. (calendar-gregorian-from-absolute date))))))))
  15195. (defun org-goto-calendar (&optional arg)
  15196. "Go to the Emacs calendar at the current date.
  15197. If there is a time stamp in the current line, go to that date.
  15198. A prefix ARG can be used to force the current date."
  15199. (interactive "P")
  15200. (let ((tsr org-ts-regexp) diff
  15201. (calendar-move-hook nil)
  15202. (calendar-view-holidays-initially-flag nil)
  15203. (calendar-view-diary-initially-flag nil))
  15204. (if (or (org-at-timestamp-p)
  15205. (save-excursion
  15206. (beginning-of-line 1)
  15207. (looking-at (concat ".*" tsr))))
  15208. (let ((d1 (time-to-days (current-time)))
  15209. (d2 (time-to-days
  15210. (org-time-string-to-time (match-string 1)))))
  15211. (setq diff (- d2 d1))))
  15212. (calendar)
  15213. (calendar-goto-today)
  15214. (if (and diff (not arg)) (calendar-forward-day diff))))
  15215. (defun org-get-date-from-calendar ()
  15216. "Return a list (month day year) of date at point in calendar."
  15217. (with-current-buffer "*Calendar*"
  15218. (save-match-data
  15219. (calendar-cursor-to-date))))
  15220. (defun org-date-from-calendar ()
  15221. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15222. If there is already a time stamp at the cursor position, update it."
  15223. (interactive)
  15224. (if (org-at-timestamp-p t)
  15225. (org-timestamp-change 0 'calendar)
  15226. (let ((cal-date (org-get-date-from-calendar)))
  15227. (org-insert-time-stamp
  15228. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15229. (defun org-minutes-to-hh:mm-string (m)
  15230. "Compute H:MM from a number of minutes."
  15231. (let ((h (/ m 60)))
  15232. (setq m (- m (* 60 h)))
  15233. (format org-time-clocksum-format h m)))
  15234. (defun org-hh:mm-string-to-minutes (s)
  15235. "Convert a string H:MM to a number of minutes.
  15236. If the string is just a number, interpret it as minutes.
  15237. In fact, the first hh:mm or number in the string will be taken,
  15238. there can be extra stuff in the string.
  15239. If no number is found, the return value is 0."
  15240. (cond
  15241. ((integerp s) s)
  15242. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15243. (+ (* (string-to-number (match-string 1 s)) 60)
  15244. (string-to-number (match-string 2 s))))
  15245. ((string-match "\\([0-9]+\\)" s)
  15246. (string-to-number (match-string 1 s)))
  15247. (t 0)))
  15248. (defcustom org-effort-durations
  15249. `(("h" . 60)
  15250. ("d" . ,(* 60 8))
  15251. ("w" . ,(* 60 8 5))
  15252. ("m" . ,(* 60 8 5 4))
  15253. ("y" . ,(* 60 8 5 40)))
  15254. "Conversion factor to minutes for an effort modifier.
  15255. Each entry has the form (MODIFIER . MINUTES).
  15256. In an effort string, a number followed by MODIFIER is multiplied
  15257. by the specified number of MINUTES to obtain an effort in
  15258. minutes.
  15259. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15260. effort string \"2hours\" is equivalent to 120 minutes."
  15261. :group 'org-agenda
  15262. :version "24.1"
  15263. :type '(alist :key-type (string :tag "Modifier")
  15264. :value-type (number :tag "Minutes")))
  15265. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15266. "Convert a duration string S to minutes.
  15267. A bare number is interpreted as minutes, modifiers can be set by
  15268. customizing `org-effort-durations' (which see).
  15269. Entries containing a colon are interpreted as H:MM by
  15270. `org-hh:mm-string-to-minutes'."
  15271. (let ((result 0)
  15272. (re (concat "\\([0-9.]+\\) *\\("
  15273. (regexp-opt (mapcar 'car org-effort-durations))
  15274. "\\)")))
  15275. (while (string-match re s)
  15276. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15277. (string-to-number (match-string 1 s))))
  15278. (setq s (replace-match "" nil t s)))
  15279. (setq result (floor result))
  15280. (incf result (org-hh:mm-string-to-minutes s))
  15281. (if output-to-string (number-to-string result) result)))
  15282. ;;;; Files
  15283. (defun org-save-all-org-buffers ()
  15284. "Save all Org-mode buffers without user confirmation."
  15285. (interactive)
  15286. (message "Saving all Org-mode buffers...")
  15287. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15288. (when (featurep 'org-id) (org-id-locations-save))
  15289. (message "Saving all Org-mode buffers... done"))
  15290. (defun org-revert-all-org-buffers ()
  15291. "Revert all Org-mode buffers.
  15292. Prompt for confirmation when there are unsaved changes.
  15293. Be sure you know what you are doing before letting this function
  15294. overwrite your changes.
  15295. This function is useful in a setup where one tracks org files
  15296. with a version control system, to revert on one machine after pulling
  15297. changes from another. I believe the procedure must be like this:
  15298. 1. M-x org-save-all-org-buffers
  15299. 2. Pull changes from the other machine, resolve conflicts
  15300. 3. M-x org-revert-all-org-buffers"
  15301. (interactive)
  15302. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15303. (error "Abort"))
  15304. (save-excursion
  15305. (save-window-excursion
  15306. (mapc
  15307. (lambda (b)
  15308. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15309. (with-current-buffer b buffer-file-name))
  15310. (org-pop-to-buffer-same-window b)
  15311. (revert-buffer t 'no-confirm)))
  15312. (buffer-list))
  15313. (when (and (featurep 'org-id) org-id-track-globally)
  15314. (org-id-locations-load)))))
  15315. ;;;; Agenda files
  15316. ;;;###autoload
  15317. (defun org-switchb (&optional arg)
  15318. "Switch between Org buffers.
  15319. With one prefix argument, restrict available buffers to files.
  15320. With two prefix arguments, restrict available buffers to agenda files.
  15321. Defaults to `iswitchb' for buffer name completion.
  15322. Set `org-completion-use-ido' to make it use ido instead."
  15323. (interactive "P")
  15324. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15325. ((equal arg '(16)) (org-buffer-list 'agenda))
  15326. (t (org-buffer-list))))
  15327. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15328. (org-completion-use-ido org-completion-use-ido))
  15329. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15330. (setq org-completion-use-iswitchb t))
  15331. (org-pop-to-buffer-same-window
  15332. (org-icompleting-read "Org buffer: "
  15333. (mapcar 'list (mapcar 'buffer-name blist))
  15334. nil t))))
  15335. ;;; Define some older names previously used for this functionality
  15336. ;;;###autoload
  15337. (defalias 'org-ido-switchb 'org-switchb)
  15338. ;;;###autoload
  15339. (defalias 'org-iswitchb 'org-switchb)
  15340. (defun org-buffer-list (&optional predicate exclude-tmp)
  15341. "Return a list of Org buffers.
  15342. PREDICATE can be `export', `files' or `agenda'.
  15343. export restrict the list to Export buffers.
  15344. files restrict the list to buffers visiting Org files.
  15345. agenda restrict the list to buffers visiting agenda files.
  15346. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15347. (let* ((bfn nil)
  15348. (agenda-files (and (eq predicate 'agenda)
  15349. (mapcar 'file-truename (org-agenda-files t))))
  15350. (filter
  15351. (cond
  15352. ((eq predicate 'files)
  15353. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15354. ((eq predicate 'export)
  15355. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15356. ((eq predicate 'agenda)
  15357. (lambda (b)
  15358. (with-current-buffer b
  15359. (and (derived-mode-p 'org-mode)
  15360. (setq bfn (buffer-file-name b))
  15361. (member (file-truename bfn) agenda-files)))))
  15362. (t (lambda (b) (with-current-buffer b
  15363. (or (derived-mode-p 'org-mode)
  15364. (string-match "\*Org .*Export"
  15365. (buffer-name b)))))))))
  15366. (delq nil
  15367. (mapcar
  15368. (lambda(b)
  15369. (if (and (funcall filter b)
  15370. (or (not exclude-tmp)
  15371. (not (string-match "tmp" (buffer-name b)))))
  15372. b
  15373. nil))
  15374. (buffer-list)))))
  15375. (defun org-agenda-files (&optional unrestricted archives)
  15376. "Get the list of agenda files.
  15377. Optional UNRESTRICTED means return the full list even if a restriction
  15378. is currently in place.
  15379. When ARCHIVES is t, include all archive files that are really being
  15380. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15381. `org-agenda-archives-mode' is t."
  15382. (let ((files
  15383. (cond
  15384. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15385. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15386. ((listp org-agenda-files) org-agenda-files)
  15387. (t (error "Invalid value of `org-agenda-files'")))))
  15388. (setq files (apply 'append
  15389. (mapcar (lambda (f)
  15390. (if (file-directory-p f)
  15391. (directory-files
  15392. f t org-agenda-file-regexp)
  15393. (list f)))
  15394. files)))
  15395. (when org-agenda-skip-unavailable-files
  15396. (setq files (delq nil
  15397. (mapcar (function
  15398. (lambda (file)
  15399. (and (file-readable-p file) file)))
  15400. files))))
  15401. (when (or (eq archives t)
  15402. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15403. (setq files (org-add-archive-files files)))
  15404. files))
  15405. (defun org-agenda-file-p (&optional file)
  15406. "Return non-nil, if FILE is an agenda file.
  15407. If FILE is omitted, use the file associated with the current
  15408. buffer."
  15409. (member (or file (buffer-file-name))
  15410. (org-agenda-files t)))
  15411. (defun org-edit-agenda-file-list ()
  15412. "Edit the list of agenda files.
  15413. Depending on setup, this either uses customize to edit the variable
  15414. `org-agenda-files', or it visits the file that is holding the list. In the
  15415. latter case, the buffer is set up in a way that saving it automatically kills
  15416. the buffer and restores the previous window configuration."
  15417. (interactive)
  15418. (if (stringp org-agenda-files)
  15419. (let ((cw (current-window-configuration)))
  15420. (find-file org-agenda-files)
  15421. (org-set-local 'org-window-configuration cw)
  15422. (org-add-hook 'after-save-hook
  15423. (lambda ()
  15424. (set-window-configuration
  15425. (prog1 org-window-configuration
  15426. (kill-buffer (current-buffer))))
  15427. (org-install-agenda-files-menu)
  15428. (message "New agenda file list installed"))
  15429. nil 'local)
  15430. (message "%s" (substitute-command-keys
  15431. "Edit list and finish with \\[save-buffer]")))
  15432. (customize-variable 'org-agenda-files)))
  15433. (defun org-store-new-agenda-file-list (list)
  15434. "Set new value for the agenda file list and save it correctly."
  15435. (if (stringp org-agenda-files)
  15436. (let ((fe (org-read-agenda-file-list t)) b u)
  15437. (while (setq b (find-buffer-visiting org-agenda-files))
  15438. (kill-buffer b))
  15439. (with-temp-file org-agenda-files
  15440. (insert
  15441. (mapconcat
  15442. (lambda (f) ;; Keep un-expanded entries.
  15443. (if (setq u (assoc f fe))
  15444. (cdr u)
  15445. f))
  15446. list "\n")
  15447. "\n")))
  15448. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15449. (org-insert-mode-line-in-empty-file nil))
  15450. (setq org-agenda-files list)
  15451. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15452. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15453. "Read the list of agenda files from a file.
  15454. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15455. filenames, used by `org-store-new-agenda-file-list' to write back
  15456. un-expanded file names."
  15457. (when (file-directory-p org-agenda-files)
  15458. (error "`org-agenda-files' cannot be a single directory"))
  15459. (when (stringp org-agenda-files)
  15460. (with-temp-buffer
  15461. (insert-file-contents org-agenda-files)
  15462. (mapcar
  15463. (lambda (f)
  15464. (let ((e (expand-file-name (substitute-in-file-name f)
  15465. org-directory)))
  15466. (if pair-with-expansion
  15467. (cons e f)
  15468. e)))
  15469. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15470. ;;;###autoload
  15471. (defun org-cycle-agenda-files ()
  15472. "Cycle through the files in `org-agenda-files'.
  15473. If the current buffer visits an agenda file, find the next one in the list.
  15474. If the current buffer does not, find the first agenda file."
  15475. (interactive)
  15476. (let* ((fs (org-agenda-files t))
  15477. (files (append fs (list (car fs))))
  15478. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15479. file)
  15480. (unless files (error "No agenda files"))
  15481. (catch 'exit
  15482. (while (setq file (pop files))
  15483. (if (equal (file-truename file) tcf)
  15484. (when (car files)
  15485. (find-file (car files))
  15486. (throw 'exit t))))
  15487. (find-file (car fs)))
  15488. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15489. (defun org-agenda-file-to-front (&optional to-end)
  15490. "Move/add the current file to the top of the agenda file list.
  15491. If the file is not present in the list, it is added to the front. If it is
  15492. present, it is moved there. With optional argument TO-END, add/move to the
  15493. end of the list."
  15494. (interactive "P")
  15495. (let ((org-agenda-skip-unavailable-files nil)
  15496. (file-alist (mapcar (lambda (x)
  15497. (cons (file-truename x) x))
  15498. (org-agenda-files t)))
  15499. (ctf (file-truename
  15500. (or buffer-file-name
  15501. (error "Please save the current buffer to a file"))))
  15502. x had)
  15503. (setq x (assoc ctf file-alist) had x)
  15504. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15505. (if to-end
  15506. (setq file-alist (append (delq x file-alist) (list x)))
  15507. (setq file-alist (cons x (delq x file-alist))))
  15508. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15509. (org-install-agenda-files-menu)
  15510. (message "File %s to %s of agenda file list"
  15511. (if had "moved" "added") (if to-end "end" "front"))))
  15512. (defun org-remove-file (&optional file)
  15513. "Remove current file from the list of files in variable `org-agenda-files'.
  15514. These are the files which are being checked for agenda entries.
  15515. Optional argument FILE means use this file instead of the current."
  15516. (interactive)
  15517. (let* ((org-agenda-skip-unavailable-files nil)
  15518. (file (or file buffer-file-name
  15519. (error "Current buffer does not visit a file")))
  15520. (true-file (file-truename file))
  15521. (afile (abbreviate-file-name file))
  15522. (files (delq nil (mapcar
  15523. (lambda (x)
  15524. (if (equal true-file
  15525. (file-truename x))
  15526. nil x))
  15527. (org-agenda-files t)))))
  15528. (if (not (= (length files) (length (org-agenda-files t))))
  15529. (progn
  15530. (org-store-new-agenda-file-list files)
  15531. (org-install-agenda-files-menu)
  15532. (message "Removed file: %s" afile))
  15533. (message "File was not in list: %s (not removed)" afile))))
  15534. (defun org-file-menu-entry (file)
  15535. (vector file (list 'find-file file) t))
  15536. (defun org-check-agenda-file (file)
  15537. "Make sure FILE exists. If not, ask user what to do."
  15538. (when (not (file-exists-p file))
  15539. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  15540. (abbreviate-file-name file))
  15541. (let ((r (downcase (read-char-exclusive))))
  15542. (cond
  15543. ((equal r ?r)
  15544. (org-remove-file file)
  15545. (throw 'nextfile t))
  15546. (t (error "Abort"))))))
  15547. (defun org-get-agenda-file-buffer (file)
  15548. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15549. it to the list of buffers which might be released later."
  15550. (let ((buf (org-find-base-buffer-visiting file)))
  15551. (if buf
  15552. buf ; just return it
  15553. ;; Make a new buffer and remember it
  15554. (setq buf (find-file-noselect file))
  15555. (if buf (push buf org-agenda-new-buffers))
  15556. buf)))
  15557. (defun org-release-buffers (blist)
  15558. "Release all buffers in list, asking the user for confirmation when needed.
  15559. When a buffer is unmodified, it is just killed. When modified, it is saved
  15560. \(if the user agrees) and then killed."
  15561. (let (buf file)
  15562. (while (setq buf (pop blist))
  15563. (setq file (buffer-file-name buf))
  15564. (when (and (buffer-modified-p buf)
  15565. file
  15566. (y-or-n-p (format "Save file %s? " file)))
  15567. (with-current-buffer buf (save-buffer)))
  15568. (kill-buffer buf))))
  15569. (defun org-agenda-prepare-buffers (files)
  15570. "Create buffers for all agenda files, protect archived trees and comments."
  15571. (interactive)
  15572. (let ((pa '(:org-archived t))
  15573. (pc '(:org-comment t))
  15574. (pall '(:org-archived t :org-comment t))
  15575. (inhibit-read-only t)
  15576. (rea (concat ":" org-archive-tag ":"))
  15577. bmp file re)
  15578. (save-excursion
  15579. (save-restriction
  15580. (while (setq file (pop files))
  15581. (catch 'nextfile
  15582. (if (bufferp file)
  15583. (set-buffer file)
  15584. (org-check-agenda-file file)
  15585. (set-buffer (org-get-agenda-file-buffer file)))
  15586. (widen)
  15587. (setq bmp (buffer-modified-p))
  15588. (org-refresh-category-properties)
  15589. (org-refresh-properties org-effort-property 'org-effort)
  15590. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  15591. (setq org-todo-keywords-for-agenda
  15592. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15593. (setq org-done-keywords-for-agenda
  15594. (append org-done-keywords-for-agenda org-done-keywords))
  15595. (setq org-todo-keyword-alist-for-agenda
  15596. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15597. (setq org-drawers-for-agenda
  15598. (append org-drawers-for-agenda org-drawers))
  15599. (setq org-tag-alist-for-agenda
  15600. (append org-tag-alist-for-agenda org-tag-alist))
  15601. (save-excursion
  15602. (remove-text-properties (point-min) (point-max) pall)
  15603. (when org-agenda-skip-archived-trees
  15604. (goto-char (point-min))
  15605. (while (re-search-forward rea nil t)
  15606. (if (org-at-heading-p t)
  15607. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15608. (goto-char (point-min))
  15609. (setq re (format org-heading-keyword-regexp-format
  15610. org-comment-string))
  15611. (while (re-search-forward re nil t)
  15612. (add-text-properties
  15613. (match-beginning 0) (org-end-of-subtree t) pc)))
  15614. (set-buffer-modified-p bmp)))))
  15615. (setq org-todo-keywords-for-agenda
  15616. (org-uniquify org-todo-keywords-for-agenda))
  15617. (setq org-todo-keyword-alist-for-agenda
  15618. (org-uniquify org-todo-keyword-alist-for-agenda)
  15619. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15620. ;;;; Embedded LaTeX
  15621. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15622. "Keymap for the minor `org-cdlatex-mode'.")
  15623. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15624. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15625. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15626. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15627. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15628. (defvar org-cdlatex-texmathp-advice-is-done nil
  15629. "Flag remembering if we have applied the advice to texmathp already.")
  15630. (define-minor-mode org-cdlatex-mode
  15631. "Toggle the minor `org-cdlatex-mode'.
  15632. This mode supports entering LaTeX environment and math in LaTeX fragments
  15633. in Org-mode.
  15634. \\{org-cdlatex-mode-map}"
  15635. nil " OCDL" nil
  15636. (when org-cdlatex-mode
  15637. (require 'cdlatex)
  15638. (run-hooks 'cdlatex-mode-hook)
  15639. (cdlatex-compute-tables))
  15640. (unless org-cdlatex-texmathp-advice-is-done
  15641. (setq org-cdlatex-texmathp-advice-is-done t)
  15642. (defadvice texmathp (around org-math-always-on activate)
  15643. "Always return t in org-mode buffers.
  15644. This is because we want to insert math symbols without dollars even outside
  15645. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15646. an embedded LaTeX fragment, let texmathp do its job.
  15647. \\[org-cdlatex-mode-map]"
  15648. (interactive)
  15649. (let (p)
  15650. (cond
  15651. ((not (derived-mode-p 'org-mode)) ad-do-it)
  15652. ((eq this-command 'cdlatex-math-symbol)
  15653. (setq ad-return-value t
  15654. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15655. (t
  15656. (let ((p (org-inside-LaTeX-fragment-p)))
  15657. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15658. (setq ad-return-value t
  15659. texmathp-why '("Org-mode embedded math" . 0))
  15660. (if p ad-do-it)))))))))
  15661. (defun turn-on-org-cdlatex ()
  15662. "Unconditionally turn on `org-cdlatex-mode'."
  15663. (org-cdlatex-mode 1))
  15664. (defun org-inside-LaTeX-fragment-p ()
  15665. "Test if point is inside a LaTeX fragment.
  15666. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15667. sequence appearing also before point.
  15668. Even though the matchers for math are configurable, this function assumes
  15669. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15670. delimiters are skipped when they have been removed by customization.
  15671. The return value is nil, or a cons cell with the delimiter and the
  15672. position of this delimiter.
  15673. This function does a reasonably good job, but can locally be fooled by
  15674. for example currency specifications. For example it will assume being in
  15675. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15676. fragments that are properly closed, but during editing, we have to live
  15677. with the uncertainty caused by missing closing delimiters. This function
  15678. looks only before point, not after."
  15679. (catch 'exit
  15680. (let ((pos (point))
  15681. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15682. (lim (progn
  15683. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15684. (point)))
  15685. dd-on str (start 0) m re)
  15686. (goto-char pos)
  15687. (when dodollar
  15688. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15689. re (nth 1 (assoc "$" org-latex-regexps)))
  15690. (while (string-match re str start)
  15691. (cond
  15692. ((= (match-end 0) (length str))
  15693. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15694. ((= (match-end 0) (- (length str) 5))
  15695. (throw 'exit nil))
  15696. (t (setq start (match-end 0))))))
  15697. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15698. (goto-char pos)
  15699. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15700. (and (match-beginning 2) (throw 'exit nil))
  15701. ;; count $$
  15702. (while (re-search-backward "\\$\\$" lim t)
  15703. (setq dd-on (not dd-on)))
  15704. (goto-char pos)
  15705. (if dd-on (cons "$$" m))))))
  15706. (defun org-inside-latex-macro-p ()
  15707. "Is point inside a LaTeX macro or its arguments?"
  15708. (save-match-data
  15709. (org-in-regexp
  15710. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15711. (defun org-try-cdlatex-tab ()
  15712. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15713. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15714. - inside a LaTeX fragment, or
  15715. - after the first word in a line, where an abbreviation expansion could
  15716. insert a LaTeX environment."
  15717. (when org-cdlatex-mode
  15718. (cond
  15719. ;; Before any word on the line: No expansion possible.
  15720. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15721. ;; Just after first word on the line: Expand it. Make sure it
  15722. ;; cannot happen on headlines, though.
  15723. ((save-excursion
  15724. (skip-chars-backward "a-zA-Z0-9*")
  15725. (skip-chars-backward " \t")
  15726. (and (bolp) (not (org-at-heading-p))))
  15727. (cdlatex-tab) t)
  15728. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15729. (defun org-cdlatex-underscore-caret (&optional arg)
  15730. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15731. Revert to the normal definition outside of these fragments."
  15732. (interactive "P")
  15733. (if (org-inside-LaTeX-fragment-p)
  15734. (call-interactively 'cdlatex-sub-superscript)
  15735. (let (org-cdlatex-mode)
  15736. (call-interactively (key-binding (vector last-input-event))))))
  15737. (defun org-cdlatex-math-modify (&optional arg)
  15738. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15739. Revert to the normal definition outside of these fragments."
  15740. (interactive "P")
  15741. (if (org-inside-LaTeX-fragment-p)
  15742. (call-interactively 'cdlatex-math-modify)
  15743. (let (org-cdlatex-mode)
  15744. (call-interactively (key-binding (vector last-input-event))))))
  15745. (defvar org-latex-fragment-image-overlays nil
  15746. "List of overlays carrying the images of latex fragments.")
  15747. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15748. (defun org-remove-latex-fragment-image-overlays ()
  15749. "Remove all overlays with LaTeX fragment images in current buffer."
  15750. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15751. (setq org-latex-fragment-image-overlays nil))
  15752. (defun org-preview-latex-fragment (&optional subtree)
  15753. "Preview the LaTeX fragment at point, or all locally or globally.
  15754. If the cursor is in a LaTeX fragment, create the image and overlay
  15755. it over the source code. If there is no fragment at point, display
  15756. all fragments in the current text, from one headline to the next. With
  15757. prefix SUBTREE, display all fragments in the current subtree. With a
  15758. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15759. the cursor is before the first headline,
  15760. display all fragments in the buffer.
  15761. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15762. (interactive "P")
  15763. (unless buffer-file-name
  15764. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15765. (org-remove-latex-fragment-image-overlays)
  15766. (save-excursion
  15767. (save-restriction
  15768. (let (beg end at msg)
  15769. (cond
  15770. ((or (equal subtree '(16))
  15771. (not (save-excursion
  15772. (re-search-backward org-outline-regexp-bol nil t))))
  15773. (setq beg (point-min) end (point-max)
  15774. msg "Creating images for buffer...%s"))
  15775. ((equal subtree '(4))
  15776. (org-back-to-heading)
  15777. (setq beg (point) end (org-end-of-subtree t)
  15778. msg "Creating images for subtree...%s"))
  15779. (t
  15780. (if (setq at (org-inside-LaTeX-fragment-p))
  15781. (goto-char (max (point-min) (- (cdr at) 2)))
  15782. (org-back-to-heading))
  15783. (setq beg (point) end (progn (outline-next-heading) (point))
  15784. msg (if at "Creating image...%s"
  15785. "Creating images for entry...%s"))))
  15786. (message msg "")
  15787. (narrow-to-region beg end)
  15788. (goto-char beg)
  15789. (org-format-latex
  15790. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  15791. (file-name-nondirectory
  15792. buffer-file-name)))
  15793. default-directory 'overlays msg at 'forbuffer
  15794. org-latex-create-formula-image-program)
  15795. (message msg "done. Use `C-c C-c' to remove images.")))))
  15796. (defvar org-latex-regexps
  15797. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15798. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15799. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15800. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15801. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15802. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15803. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15804. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15805. "Regular expressions for matching embedded LaTeX.")
  15806. (defvar org-export-have-math nil) ;; dynamic scoping
  15807. (defun org-format-latex (prefix &optional dir overlays msg at
  15808. forbuffer processing-type)
  15809. "Replace LaTeX fragments with links to an image, and produce images.
  15810. Some of the options can be changed using the variable
  15811. `org-format-latex-options'."
  15812. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15813. (let* ((prefixnodir (file-name-nondirectory prefix))
  15814. (absprefix (expand-file-name prefix dir))
  15815. (todir (file-name-directory absprefix))
  15816. (opt org-format-latex-options)
  15817. (matchers (plist-get opt :matchers))
  15818. (re-list org-latex-regexps)
  15819. (org-format-latex-header-extra
  15820. (plist-get (org-infile-export-plist) :latex-header-extra))
  15821. (cnt 0) txt hash link beg end re e checkdir
  15822. executables-checked string
  15823. m n block-type block linkfile movefile ov)
  15824. ;; Check the different regular expressions
  15825. (while (setq e (pop re-list))
  15826. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15827. block (if block-type "\n\n" ""))
  15828. (when (member m matchers)
  15829. (goto-char (point-min))
  15830. (while (re-search-forward re nil t)
  15831. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15832. (not (get-text-property (match-beginning n)
  15833. 'org-protected))
  15834. (or (not overlays)
  15835. (not (eq (get-char-property (match-beginning n)
  15836. 'org-overlay-type)
  15837. 'org-latex-overlay))))
  15838. (setq org-export-have-math t)
  15839. (cond
  15840. ((eq processing-type 'verbatim)
  15841. ;; Leave the text verbatim, just protect it
  15842. (add-text-properties (match-beginning n) (match-end n)
  15843. '(org-protected t)))
  15844. ((eq processing-type 'mathjax)
  15845. ;; Prepare for MathJax processing
  15846. (setq string (match-string n))
  15847. (if (member m '("$" "$1"))
  15848. (save-excursion
  15849. (delete-region (match-beginning n) (match-end n))
  15850. (goto-char (match-beginning n))
  15851. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15852. "\\)")
  15853. '(org-protected t))))
  15854. (add-text-properties (match-beginning n) (match-end n)
  15855. '(org-protected t))))
  15856. ((or (eq processing-type 'dvipng)
  15857. (eq processing-type 'imagemagick))
  15858. ;; Process to an image
  15859. (setq txt (match-string n)
  15860. beg (match-beginning n) end (match-end n)
  15861. cnt (1+ cnt))
  15862. (let (print-length print-level) ; make sure full list is printed
  15863. (setq hash (sha1 (prin1-to-string
  15864. (list org-format-latex-header
  15865. org-format-latex-header-extra
  15866. org-export-latex-default-packages-alist
  15867. org-export-latex-packages-alist
  15868. org-format-latex-options
  15869. forbuffer txt)))
  15870. linkfile (format "%s_%s.png" prefix hash)
  15871. movefile (format "%s_%s.png" absprefix hash)))
  15872. (setq link (concat block "[[file:" linkfile "]]" block))
  15873. (if msg (message msg cnt))
  15874. (goto-char beg)
  15875. (unless checkdir ; make sure the directory exists
  15876. (setq checkdir t)
  15877. (or (file-directory-p todir) (make-directory todir t)))
  15878. (cond
  15879. ((eq processing-type 'dvipng)
  15880. (unless executables-checked
  15881. (org-check-external-command
  15882. "latex" "needed to convert LaTeX fragments to images")
  15883. (org-check-external-command
  15884. "dvipng" "needed to convert LaTeX fragments to images")
  15885. (setq executables-checked t))
  15886. (unless (file-exists-p movefile)
  15887. (org-create-formula-image-with-dvipng
  15888. txt movefile opt forbuffer)))
  15889. ((eq processing-type 'imagemagick)
  15890. (unless executables-checked
  15891. (org-check-external-command
  15892. "convert" "you need to install imagemagick")
  15893. (setq executables-checked t))
  15894. (unless (file-exists-p movefile)
  15895. (org-create-formula-image-with-imagemagick
  15896. txt movefile opt forbuffer))))
  15897. (if overlays
  15898. (progn
  15899. (mapc (lambda (o)
  15900. (if (eq (overlay-get o 'org-overlay-type)
  15901. 'org-latex-overlay)
  15902. (delete-overlay o)))
  15903. (overlays-in beg end))
  15904. (setq ov (make-overlay beg end))
  15905. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15906. (if (featurep 'xemacs)
  15907. (progn
  15908. (overlay-put ov 'invisible t)
  15909. (overlay-put
  15910. ov 'end-glyph
  15911. (make-glyph (vector 'png :file movefile))))
  15912. (overlay-put
  15913. ov 'display
  15914. (list 'image :type 'png :file movefile :ascent 'center)))
  15915. (push ov org-latex-fragment-image-overlays)
  15916. (goto-char end))
  15917. (delete-region beg end)
  15918. (insert (org-add-props link
  15919. (list 'org-latex-src
  15920. (replace-regexp-in-string
  15921. "\"" "" txt)
  15922. 'org-latex-src-embed-type
  15923. (if block-type 'paragraph 'character))))))
  15924. ((eq processing-type 'mathml)
  15925. ;; Process to MathML
  15926. (unless executables-checked
  15927. (unless (save-match-data (org-format-latex-mathml-available-p))
  15928. (error "LaTeX to MathML converter not configured"))
  15929. (setq executables-checked t))
  15930. (setq txt (match-string n)
  15931. beg (match-beginning n) end (match-end n)
  15932. cnt (1+ cnt))
  15933. (if msg (message msg cnt))
  15934. (goto-char beg)
  15935. (delete-region beg end)
  15936. (insert (org-format-latex-as-mathml
  15937. txt block-type prefix dir)))
  15938. (t
  15939. (error "Unknown conversion type %s for latex fragments"
  15940. processing-type)))))))))
  15941. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15942. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15943. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15944. sucessful, return the portion between \"<math...> </math>\"
  15945. elements otherwise return nil. When MATHML-FILE is specified,
  15946. write the results in to that file. When invoked as an
  15947. interactive command, prompt for LATEX-FRAG, with initial value
  15948. set to the current active region and echo the results for user
  15949. inspection."
  15950. (interactive (list (let ((frag (when (org-region-active-p)
  15951. (buffer-substring-no-properties
  15952. (region-beginning) (region-end)))))
  15953. (read-string "LaTeX Fragment: " frag nil frag))))
  15954. (unless latex-frag (error "Invalid latex-frag"))
  15955. (let* ((tmp-in-file (file-relative-name
  15956. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15957. (ignore (write-region latex-frag nil tmp-in-file))
  15958. (tmp-out-file (file-relative-name
  15959. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15960. (cmd (format-spec
  15961. org-latex-to-mathml-convert-command
  15962. `((?j . ,(shell-quote-argument
  15963. (expand-file-name org-latex-to-mathml-jar-file)))
  15964. (?I . ,(shell-quote-argument tmp-in-file))
  15965. (?o . ,(shell-quote-argument tmp-out-file)))))
  15966. mathml shell-command-output)
  15967. (when (org-called-interactively-p 'any)
  15968. (unless (org-format-latex-mathml-available-p)
  15969. (error "LaTeX to MathML converter not configured")))
  15970. (message "Running %s" cmd)
  15971. (setq shell-command-output (shell-command-to-string cmd))
  15972. (setq mathml
  15973. (when (file-readable-p tmp-out-file)
  15974. (with-current-buffer (find-file-noselect tmp-out-file t)
  15975. (goto-char (point-min))
  15976. (when (re-search-forward
  15977. (concat
  15978. (regexp-quote
  15979. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15980. "\\(.\\|\n\\)*"
  15981. (regexp-quote "</math>")) nil t)
  15982. (prog1 (match-string 0) (kill-buffer))))))
  15983. (cond
  15984. (mathml
  15985. (setq mathml
  15986. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15987. (when mathml-file
  15988. (write-region mathml nil mathml-file))
  15989. (when (org-called-interactively-p 'any)
  15990. (message mathml)))
  15991. ((message "LaTeX to MathML conversion failed")
  15992. (message shell-command-output)))
  15993. (delete-file tmp-in-file)
  15994. (when (file-exists-p tmp-out-file)
  15995. (delete-file tmp-out-file))
  15996. mathml))
  15997. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15998. prefix &optional dir)
  15999. "Use `org-create-math-formula' but check local cache first."
  16000. (let* ((absprefix (expand-file-name prefix dir))
  16001. (print-length nil) (print-level nil)
  16002. (formula-id (concat
  16003. "formula-"
  16004. (sha1
  16005. (prin1-to-string
  16006. (list latex-frag
  16007. org-latex-to-mathml-convert-command)))))
  16008. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16009. (formula-cache-dir (file-name-directory formula-cache)))
  16010. (unless (file-directory-p formula-cache-dir)
  16011. (make-directory formula-cache-dir t))
  16012. (unless (file-exists-p formula-cache)
  16013. (org-create-math-formula latex-frag formula-cache))
  16014. (if (file-exists-p formula-cache)
  16015. ;; Successful conversion. Return the link to MathML file.
  16016. (org-add-props
  16017. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16018. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16019. 'org-latex-src-embed-type (if latex-frag-type
  16020. 'paragraph 'character)))
  16021. ;; Failed conversion. Return the LaTeX fragment verbatim
  16022. (add-text-properties
  16023. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  16024. latex-frag)))
  16025. ;; This function borrows from Ganesh Swami's latex2png.el
  16026. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16027. "This calls dvipng."
  16028. (require 'org-latex)
  16029. (let* ((tmpdir (if (featurep 'xemacs)
  16030. (temp-directory)
  16031. temporary-file-directory))
  16032. (texfilebase (make-temp-name
  16033. (expand-file-name "orgtex" tmpdir)))
  16034. (texfile (concat texfilebase ".tex"))
  16035. (dvifile (concat texfilebase ".dvi"))
  16036. (pngfile (concat texfilebase ".png"))
  16037. (fnh (if (featurep 'xemacs)
  16038. (font-height (face-font 'default))
  16039. (face-attribute 'default :height nil)))
  16040. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16041. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16042. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16043. "Black"))
  16044. (bg (or (plist-get options (if buffer :background :html-background))
  16045. "Transparent")))
  16046. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  16047. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  16048. (with-temp-file texfile
  16049. (insert (org-splice-latex-header
  16050. org-format-latex-header
  16051. org-export-latex-default-packages-alist
  16052. org-export-latex-packages-alist t
  16053. org-format-latex-header-extra))
  16054. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  16055. (require 'org-latex)
  16056. (org-export-latex-fix-inputenc))
  16057. (let ((dir default-directory))
  16058. (condition-case nil
  16059. (progn
  16060. (cd tmpdir)
  16061. (call-process "latex" nil nil nil texfile))
  16062. (error nil))
  16063. (cd dir))
  16064. (if (not (file-exists-p dvifile))
  16065. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16066. (condition-case nil
  16067. (if (featurep 'xemacs)
  16068. (call-process "dvipng" nil nil nil
  16069. "-fg" fg "-bg" bg
  16070. "-T" "tight"
  16071. "-o" pngfile
  16072. dvifile)
  16073. (call-process "dvipng" nil nil nil
  16074. "-fg" fg "-bg" bg
  16075. "-D" dpi
  16076. ;;"-x" scale "-y" scale
  16077. "-T" "tight"
  16078. "-o" pngfile
  16079. dvifile))
  16080. (error nil))
  16081. (if (not (file-exists-p pngfile))
  16082. (if org-format-latex-signal-error
  16083. (error "Failed to create png file from %s" texfile)
  16084. (message "Failed to create png file from %s" texfile)
  16085. nil)
  16086. ;; Use the requested file name and clean up
  16087. (copy-file pngfile tofile 'replace)
  16088. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16089. (if (file-exists-p (concat texfilebase e))
  16090. (delete-file (concat texfilebase e))))
  16091. pngfile))))
  16092. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  16093. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16094. "This calls convert, which is included into imagemagick."
  16095. (require 'org-latex)
  16096. (let* ((tmpdir (if (featurep 'xemacs)
  16097. (temp-directory)
  16098. temporary-file-directory))
  16099. (texfilebase (make-temp-name
  16100. (expand-file-name "orgtex" tmpdir)))
  16101. (texfile (concat texfilebase ".tex"))
  16102. (pdffile (concat texfilebase ".pdf"))
  16103. (pngfile (concat texfilebase ".png"))
  16104. (fnh (if (featurep 'xemacs)
  16105. (font-height (face-font 'default))
  16106. (face-attribute 'default :height nil)))
  16107. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16108. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16109. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16110. "black"))
  16111. (bg (or (plist-get options (if buffer :background :html-background))
  16112. "white")))
  16113. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16114. (setq fg (org-latex-color-format fg)))
  16115. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16116. (setq bg (org-latex-color-format
  16117. (if (string= bg "Transparent")(setq bg "white")))))
  16118. (with-temp-file texfile
  16119. (insert (org-splice-latex-header
  16120. org-format-latex-header
  16121. org-export-latex-default-packages-alist
  16122. org-export-latex-packages-alist t
  16123. org-format-latex-header-extra))
  16124. (insert "\n\\begin{document}\n"
  16125. "\\definecolor{fg}{rgb}{" fg "}\n"
  16126. "\\definecolor{bg}{rgb}{" bg "}\n"
  16127. "\n\\pagecolor{bg}\n"
  16128. "\n{\\color{fg}\n"
  16129. string
  16130. "\n}\n"
  16131. "\n\\end{document}\n" )
  16132. (require 'org-latex)
  16133. (org-export-latex-fix-inputenc))
  16134. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16135. (condition-case nil
  16136. (progn
  16137. (cd tmpdir)
  16138. (setq cmds org-latex-to-pdf-process)
  16139. (while cmds
  16140. (setq latex-frags-cmds (pop cmds))
  16141. (if (listp latex-frags-cmds)
  16142. (setq cmds nil)
  16143. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  16144. (while latex-frags-cmds
  16145. (setq cmd (pop latex-frags-cmds))
  16146. (while (string-match "%b" cmd)
  16147. (setq cmd (replace-match
  16148. (save-match-data
  16149. (shell-quote-argument texfile))
  16150. t t cmd)))
  16151. (while (string-match "%f" cmd)
  16152. (setq cmd (replace-match
  16153. (save-match-data
  16154. (shell-quote-argument (file-name-nondirectory texfile)))
  16155. t t cmd)))
  16156. (while (string-match "%o" cmd)
  16157. (setq cmd (replace-match
  16158. (save-match-data
  16159. (shell-quote-argument (file-name-directory texfile)))
  16160. t t cmd)))
  16161. (setq cmd (split-string cmd))
  16162. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16163. (error nil))
  16164. (cd dir))
  16165. (if (not (file-exists-p pdffile))
  16166. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16167. (condition-case nil
  16168. (if (featurep 'xemacs)
  16169. (call-process "convert" nil nil nil
  16170. "-density" "96"
  16171. "-trim"
  16172. "-antialias"
  16173. pdffile
  16174. "-quality" "100"
  16175. ;; "-sharpen" "0x1.0"
  16176. pngfile)
  16177. (call-process "convert" nil nil nil
  16178. "-density" dpi
  16179. "-trim"
  16180. "-antialias"
  16181. pdffile
  16182. "-quality" "100"
  16183. ; "-sharpen" "0x1.0"
  16184. pngfile))
  16185. (error nil))
  16186. (if (not (file-exists-p pngfile))
  16187. (if org-format-latex-signal-error
  16188. (error "Failed to create png file from %s" texfile)
  16189. (message "Failed to create png file from %s" texfile)
  16190. nil)
  16191. ;; Use the requested file name and clean up
  16192. (copy-file pngfile tofile 'replace)
  16193. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16194. (if (file-exists-p (concat texfilebase e))
  16195. (delete-file (concat texfilebase e))))
  16196. pngfile))))
  16197. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16198. "Fill a LaTeX header template TPL.
  16199. In the template, the following place holders will be recognized:
  16200. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16201. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16202. [PACKAGES] \\usepackage statements for PKG
  16203. [NO-PACKAGES] do not include PKG
  16204. [EXTRA] the string EXTRA
  16205. [NO-EXTRA] do not include EXTRA
  16206. For backward compatibility, if both the positive and the negative place
  16207. holder is missing, the positive one (without the \"NO-\") will be
  16208. assumed to be present at the end of the template.
  16209. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16210. EXTRA is a string.
  16211. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16212. (let (rpl (end ""))
  16213. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16214. (setq rpl (if (or (match-end 1) (not def-pkg))
  16215. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16216. tpl (replace-match rpl t t tpl))
  16217. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16218. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16219. (setq rpl (if (or (match-end 1) (not pkg))
  16220. "" (org-latex-packages-to-string pkg snippets-p t))
  16221. tpl (replace-match rpl t t tpl))
  16222. (if pkg (setq end
  16223. (concat end "\n"
  16224. (org-latex-packages-to-string pkg snippets-p)))))
  16225. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16226. (setq rpl (if (or (match-end 1) (not extra))
  16227. "" (concat extra "\n"))
  16228. tpl (replace-match rpl t t tpl))
  16229. (if (and extra (string-match "\\S-" extra))
  16230. (setq end (concat end "\n" extra))))
  16231. (if (string-match "\\S-" end)
  16232. (concat tpl "\n" end)
  16233. tpl)))
  16234. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16235. "Turn an alist of packages into a string with the \\usepackage macros."
  16236. (setq pkg (mapconcat (lambda(p)
  16237. (cond
  16238. ((stringp p) p)
  16239. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16240. (format "%% Package %s omitted" (cadr p)))
  16241. ((equal "" (car p))
  16242. (format "\\usepackage{%s}" (cadr p)))
  16243. (t
  16244. (format "\\usepackage[%s]{%s}"
  16245. (car p) (cadr p)))))
  16246. pkg
  16247. "\n"))
  16248. (if newline (concat pkg "\n") pkg))
  16249. (defun org-dvipng-color (attr)
  16250. "Return a RGB color specification for dvipng."
  16251. (apply 'format "rgb %s %s %s"
  16252. (mapcar 'org-normalize-color
  16253. (if (featurep 'xemacs)
  16254. (color-rgb-components
  16255. (face-property 'default
  16256. (cond ((eq attr :foreground) 'foreground)
  16257. ((eq attr :background) 'background))))
  16258. (color-values (face-attribute 'default attr nil))))))
  16259. (defun org-latex-color (attr)
  16260. "Return a RGB color for the LaTeX color package."
  16261. (apply 'format "%s,%s,%s"
  16262. (mapcar 'org-normalize-color
  16263. (if (featurep 'xemacs)
  16264. (color-rgb-components
  16265. (face-property 'default
  16266. (cond ((eq attr :foreground) 'foreground)
  16267. ((eq attr :background) 'background))))
  16268. (color-values (face-attribute 'default attr nil))))))
  16269. (defun org-latex-color-format (color-name)
  16270. "Convert COLOR-NAME to a RGB color value."
  16271. (apply 'format "%s,%s,%s"
  16272. (mapcar 'org-normalize-color
  16273. (color-values color-name))))
  16274. (defun org-normalize-color (value)
  16275. "Return string to be used as color value for an RGB component."
  16276. (format "%g" (/ value 65535.0)))
  16277. ;; Image display
  16278. (defvar org-inline-image-overlays nil)
  16279. (make-variable-buffer-local 'org-inline-image-overlays)
  16280. (defun org-toggle-inline-images (&optional include-linked)
  16281. "Toggle the display of inline images.
  16282. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16283. (interactive "P")
  16284. (if org-inline-image-overlays
  16285. (progn
  16286. (org-remove-inline-images)
  16287. (message "Inline image display turned off"))
  16288. (org-display-inline-images include-linked)
  16289. (if org-inline-image-overlays
  16290. (message "%d images displayed inline"
  16291. (length org-inline-image-overlays))
  16292. (message "No images to display inline"))))
  16293. (defun org-redisplay-inline-images ()
  16294. "Refresh the display of inline images."
  16295. (interactive)
  16296. (if (not org-inline-image-overlays)
  16297. (org-toggle-inline-images)
  16298. (org-toggle-inline-images)
  16299. (org-toggle-inline-images)))
  16300. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16301. "Display inline images.
  16302. Normally only links without a description part are inlined, because this
  16303. is how it will work for export. When INCLUDE-LINKED is set, also links
  16304. with a description part will be inlined. This can be nice for a quick
  16305. look at those images, but it does not reflect what exported files will look
  16306. like.
  16307. When REFRESH is set, refresh existing images between BEG and END.
  16308. This will create new image displays only if necessary.
  16309. BEG and END default to the buffer boundaries."
  16310. (interactive "P")
  16311. (unless refresh
  16312. (org-remove-inline-images)
  16313. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16314. (save-excursion
  16315. (save-restriction
  16316. (widen)
  16317. (setq beg (or beg (point-min)) end (or end (point-max)))
  16318. (goto-char beg)
  16319. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16320. (substring (org-image-file-name-regexp) 0 -2)
  16321. "\\)\\]" (if include-linked "" "\\]")))
  16322. old file ov img)
  16323. (while (re-search-forward re end t)
  16324. (setq old (get-char-property-and-overlay (match-beginning 1)
  16325. 'org-image-overlay))
  16326. (setq file (expand-file-name
  16327. (concat (or (match-string 3) "") (match-string 4))))
  16328. (when (file-exists-p file)
  16329. (if (and (car-safe old) refresh)
  16330. (image-refresh (overlay-get (cdr old) 'display))
  16331. (setq img (save-match-data (create-image file)))
  16332. (when img
  16333. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16334. (overlay-put ov 'display img)
  16335. (overlay-put ov 'face 'default)
  16336. (overlay-put ov 'org-image-overlay t)
  16337. (overlay-put ov 'modification-hooks
  16338. (list 'org-display-inline-remove-overlay))
  16339. (push ov org-inline-image-overlays)))))))))
  16340. (org-define-obsolete-function-alias
  16341. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16342. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16343. "Remove inline-display overlay if a corresponding region is modified."
  16344. (let ((inhibit-modification-hooks t))
  16345. (when (and ov after)
  16346. (delete ov org-inline-image-overlays)
  16347. (delete-overlay ov))))
  16348. (defun org-remove-inline-images ()
  16349. "Remove inline display of images."
  16350. (interactive)
  16351. (mapc 'delete-overlay org-inline-image-overlays)
  16352. (setq org-inline-image-overlays nil))
  16353. ;;;; Key bindings
  16354. ;; Outline functions from `outline-mode-prefix-map'
  16355. ;; that can be remapped in Org:
  16356. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16357. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16358. (define-key org-mode-map [remap outline-forward-same-level]
  16359. 'org-forward-heading-same-level)
  16360. (define-key org-mode-map [remap outline-backward-same-level]
  16361. 'org-backward-heading-same-level)
  16362. (define-key org-mode-map [remap show-branches]
  16363. 'org-kill-note-or-show-branches)
  16364. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16365. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16366. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16367. ;; Outline functions from `outline-mode-prefix-map' that can not
  16368. ;; be remapped in Org:
  16369. ;;
  16370. ;; - the column "key binding" shows whether the Outline function is still
  16371. ;; available in Org mode on the same key that it has been bound to in
  16372. ;; Outline mode:
  16373. ;; - "overridden": key used for a different functionality in Org mode
  16374. ;; - else: key still bound to the same Outline function in Org mode
  16375. ;;
  16376. ;; | Outline function | key binding | Org replacement |
  16377. ;; |------------------------------------+-------------+-----------------------|
  16378. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16379. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16380. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16381. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16382. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16383. ;; | `show-entry' | overridden | no replacement |
  16384. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16385. ;; | `show-branches' | `C-c C-k' | still same function |
  16386. ;; | `show-subtree' | overridden | visibility cycling |
  16387. ;; | `show-all' | overridden | no replacement |
  16388. ;; | `hide-subtree' | overridden | visibility cycling |
  16389. ;; | `hide-body' | overridden | no replacement |
  16390. ;; | `hide-entry' | overridden | visibility cycling |
  16391. ;; | `hide-leaves' | overridden | no replacement |
  16392. ;; | `hide-sublevels' | overridden | no replacement |
  16393. ;; | `hide-other' | overridden | no replacement |
  16394. ;; Make `C-c C-x' a prefix key
  16395. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16396. ;; TAB key with modifiers
  16397. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16398. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16399. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16400. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16401. ;; The following line is necessary under Suse GNU/Linux
  16402. (unless (featurep 'xemacs)
  16403. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16404. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16405. (define-key org-mode-map [backtab] 'org-shifttab)
  16406. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16407. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16408. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16409. ;; Cursor keys with modifiers
  16410. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16411. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16412. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16413. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16414. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16415. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16416. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16417. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16418. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16419. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16420. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16421. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16422. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16423. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16424. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16425. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16426. ;; Babel keys
  16427. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16428. (mapc (lambda (pair)
  16429. (define-key org-babel-map (car pair) (cdr pair)))
  16430. org-babel-key-bindings)
  16431. ;;; Extra keys for tty access.
  16432. ;; We only set them when really needed because otherwise the
  16433. ;; menus don't show the simple keys
  16434. (when (or org-use-extra-keys
  16435. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16436. (not window-system))
  16437. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16438. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16439. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16440. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16441. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16442. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16443. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16444. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16445. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16446. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16447. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16448. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16449. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16450. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16451. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16452. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16453. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16454. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16455. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16456. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16457. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16458. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16459. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16460. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16461. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16462. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16463. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16464. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16465. ;; All the other keys
  16466. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16467. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16468. (if (boundp 'narrow-map)
  16469. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16470. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16471. (if (boundp 'narrow-map)
  16472. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16473. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16474. (if (boundp 'narrow-map)
  16475. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16476. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16477. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  16478. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  16479. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  16480. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  16481. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  16482. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  16483. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  16484. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16485. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16486. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16487. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16488. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16489. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16490. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16491. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16492. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16493. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16494. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16495. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16496. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16497. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16498. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16499. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16500. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16501. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16502. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16503. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16504. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16505. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16506. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16507. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16508. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16509. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  16510. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16511. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16512. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16513. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16514. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16515. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16516. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16517. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16518. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16519. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16520. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16521. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16522. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16523. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16524. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16525. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16526. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16527. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16528. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16529. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16530. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16531. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16532. (org-defkey org-mode-map "\C-m" 'org-return)
  16533. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16534. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16535. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16536. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16537. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16538. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16539. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16540. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16541. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16542. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16543. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16544. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16545. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16546. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16547. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16548. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16549. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16550. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16551. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16552. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16553. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  16554. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16555. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16556. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16557. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16558. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16559. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16560. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16561. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  16562. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  16563. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16564. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16565. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  16566. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16567. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16568. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16569. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16570. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16571. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  16572. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16573. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16574. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16575. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16576. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  16577. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16578. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16579. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16580. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16581. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16582. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16583. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16584. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16585. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16586. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16587. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16588. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16589. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16590. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16591. (when (featurep 'xemacs)
  16592. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16593. (defconst org-speed-commands-default
  16594. '(
  16595. ("Outline Navigation")
  16596. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16597. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16598. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  16599. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  16600. ("u" . (org-speed-move-safe 'outline-up-heading))
  16601. ("j" . org-goto)
  16602. ("g" . (org-refile t))
  16603. ("Outline Visibility")
  16604. ("c" . org-cycle)
  16605. ("C" . org-shifttab)
  16606. (" " . org-display-outline-path)
  16607. ("=" . org-columns)
  16608. ("Outline Structure Editing")
  16609. ("U" . org-shiftmetaup)
  16610. ("D" . org-shiftmetadown)
  16611. ("r" . org-metaright)
  16612. ("l" . org-metaleft)
  16613. ("R" . org-shiftmetaright)
  16614. ("L" . org-shiftmetaleft)
  16615. ("i" . (progn (forward-char 1) (call-interactively
  16616. 'org-insert-heading-respect-content)))
  16617. ("^" . org-sort)
  16618. ("w" . org-refile)
  16619. ("a" . org-archive-subtree-default-with-confirmation)
  16620. ("." . org-mark-subtree)
  16621. ("#" . org-toggle-comment)
  16622. ("Clock Commands")
  16623. ("I" . org-clock-in)
  16624. ("O" . org-clock-out)
  16625. ("Meta Data Editing")
  16626. ("t" . org-todo)
  16627. ("," . (org-priority))
  16628. ("0" . (org-priority ?\ ))
  16629. ("1" . (org-priority ?A))
  16630. ("2" . (org-priority ?B))
  16631. ("3" . (org-priority ?C))
  16632. (":" . org-set-tags-command)
  16633. ("e" . org-set-effort)
  16634. ("E" . org-inc-effort)
  16635. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  16636. (org-entry-put (point) "APPT_WARNTIME" m)))
  16637. ("Agenda Views etc")
  16638. ("v" . org-agenda)
  16639. ("/" . org-sparse-tree)
  16640. ("Misc")
  16641. ("o" . org-open-at-point)
  16642. ("?" . org-speed-command-help)
  16643. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16644. (">" . (org-agenda-remove-restriction-lock))
  16645. )
  16646. "The default speed commands.")
  16647. (defun org-print-speed-command (e)
  16648. (if (> (length (car e)) 1)
  16649. (progn
  16650. (princ "\n")
  16651. (princ (car e))
  16652. (princ "\n")
  16653. (princ (make-string (length (car e)) ?-))
  16654. (princ "\n"))
  16655. (princ (car e))
  16656. (princ " ")
  16657. (if (symbolp (cdr e))
  16658. (princ (symbol-name (cdr e)))
  16659. (prin1 (cdr e)))
  16660. (princ "\n")))
  16661. (defun org-speed-command-help ()
  16662. "Show the available speed commands."
  16663. (interactive)
  16664. (if (not org-use-speed-commands)
  16665. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16666. (with-output-to-temp-buffer "*Help*"
  16667. (princ "User-defined Speed commands\n===========================\n")
  16668. (mapc 'org-print-speed-command org-speed-commands-user)
  16669. (princ "\n")
  16670. (princ "Built-in Speed commands\n=======================\n")
  16671. (mapc 'org-print-speed-command org-speed-commands-default))
  16672. (with-current-buffer "*Help*"
  16673. (setq truncate-lines t))))
  16674. (defun org-speed-move-safe (cmd)
  16675. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16676. If not, return to the original position and throw an error."
  16677. (interactive)
  16678. (let ((pos (point)))
  16679. (call-interactively cmd)
  16680. (unless (and (bolp) (org-at-heading-p))
  16681. (goto-char pos)
  16682. (error "Boundary reached while executing %s" cmd))))
  16683. (defvar org-self-insert-command-undo-counter 0)
  16684. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16685. (defvar org-speed-command nil)
  16686. (org-define-obsolete-function-alias
  16687. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  16688. (defun org-speed-command-activate (keys)
  16689. "Hook for activating single-letter speed commands.
  16690. `org-speed-commands-default' specifies a minimal command set.
  16691. Use `org-speed-commands-user' for further customization."
  16692. (when (or (and (bolp) (looking-at org-outline-regexp))
  16693. (and (functionp org-use-speed-commands)
  16694. (funcall org-use-speed-commands)))
  16695. (cdr (assoc keys (append org-speed-commands-user
  16696. org-speed-commands-default)))))
  16697. (org-define-obsolete-function-alias
  16698. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  16699. (defun org-babel-speed-command-activate (keys)
  16700. "Hook for activating single-letter code block commands."
  16701. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16702. (cdr (assoc keys org-babel-key-bindings))))
  16703. (defcustom org-speed-command-hook
  16704. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16705. "Hook for activating speed commands at strategic locations.
  16706. Hook functions are called in sequence until a valid handler is
  16707. found.
  16708. Each hook takes a single argument, a user-pressed command key
  16709. which is also a `self-insert-command' from the global map.
  16710. Within the hook, examine the cursor position and the command key
  16711. and return nil or a valid handler as appropriate. Handler could
  16712. be one of an interactive command, a function, or a form.
  16713. Set `org-use-speed-commands' to non-nil value to enable this
  16714. hook. The default setting is `org-speed-command-activate'."
  16715. :group 'org-structure
  16716. :version "24.1"
  16717. :type 'hook)
  16718. (defun org-self-insert-command (N)
  16719. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16720. If the cursor is in a table looking at whitespace, the whitespace is
  16721. overwritten, and the table is not marked as requiring realignment."
  16722. (interactive "p")
  16723. (org-check-before-invisible-edit 'insert)
  16724. (cond
  16725. ((and org-use-speed-commands
  16726. (setq org-speed-command
  16727. (run-hook-with-args-until-success
  16728. 'org-speed-command-hook (this-command-keys))))
  16729. (cond
  16730. ((commandp org-speed-command)
  16731. (setq this-command org-speed-command)
  16732. (call-interactively org-speed-command))
  16733. ((functionp org-speed-command)
  16734. (funcall org-speed-command))
  16735. ((and org-speed-command (listp org-speed-command))
  16736. (eval org-speed-command))
  16737. (t (let (org-use-speed-commands)
  16738. (call-interactively 'org-self-insert-command)))))
  16739. ((and
  16740. (org-table-p)
  16741. (progn
  16742. ;; check if we blank the field, and if that triggers align
  16743. (and (featurep 'org-table) org-table-auto-blank-field
  16744. (member last-command
  16745. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16746. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16747. ;; got extra space, this field does not determine column width
  16748. (let (org-table-may-need-update) (org-table-blank-field))
  16749. ;; no extra space, this field may determine column width
  16750. (org-table-blank-field)))
  16751. t)
  16752. (eq N 1)
  16753. (looking-at "[^|\n]* |"))
  16754. (let (org-table-may-need-update)
  16755. (goto-char (1- (match-end 0)))
  16756. (backward-delete-char 1)
  16757. (goto-char (match-beginning 0))
  16758. (self-insert-command N)))
  16759. (t
  16760. (setq org-table-may-need-update t)
  16761. (self-insert-command N)
  16762. (org-fix-tags-on-the-fly)
  16763. (if org-self-insert-cluster-for-undo
  16764. (if (not (eq last-command 'org-self-insert-command))
  16765. (setq org-self-insert-command-undo-counter 1)
  16766. (if (>= org-self-insert-command-undo-counter 20)
  16767. (setq org-self-insert-command-undo-counter 1)
  16768. (and (> org-self-insert-command-undo-counter 0)
  16769. buffer-undo-list (listp buffer-undo-list)
  16770. (not (cadr buffer-undo-list)) ; remove nil entry
  16771. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16772. (setq org-self-insert-command-undo-counter
  16773. (1+ org-self-insert-command-undo-counter))))))))
  16774. (defun org-check-before-invisible-edit (kind)
  16775. "Check is editing if kind KIND would be dangerous with invisible text around.
  16776. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16777. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16778. (if (and org-catch-invisible-edits
  16779. (or (not (boundp 'visible-mode)) (not visible-mode))
  16780. (or (get-char-property (point) 'invisible)
  16781. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16782. ;; OK, we need to take a closer look
  16783. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16784. (invisible-before-point (if (bobp) nil (get-char-property
  16785. (1- (point)) 'invisible)))
  16786. (border-and-ok-direction
  16787. (or
  16788. ;; Check if we are acting predictably before invisible text
  16789. (and invisible-at-point (not invisible-before-point)
  16790. (memq kind '(insert delete-backward)))
  16791. ;; Check if we are acting predictably after invisible text
  16792. ;; This works not well, and I have turned it off. It seems
  16793. ;; better to always show and stop after invisible text.
  16794. ;; (and (not invisible-at-point) invisible-before-point
  16795. ;; (memq kind '(insert delete)))
  16796. )))
  16797. (when (or (memq invisible-at-point '(outline org-hide-block t))
  16798. (memq invisible-before-point '(outline org-hide-block t)))
  16799. (if (eq org-catch-invisible-edits 'error)
  16800. (error "Editing in invisible areas is prohibited - make visible first"))
  16801. (if (and org-custom-properties-overlays
  16802. (y-or-n-p "Display invisible properties in this buffer? "))
  16803. (org-toggle-custom-properties-visibility)
  16804. ;; Make the area visible
  16805. (save-excursion
  16806. (if invisible-before-point
  16807. (goto-char (previous-single-char-property-change
  16808. (point) 'invisible)))
  16809. (org-cycle))
  16810. (cond
  16811. ((eq org-catch-invisible-edits 'show)
  16812. ;; That's it, we do the edit after showing
  16813. (message
  16814. "Unfolding invisible region around point before editing")
  16815. (sit-for 1))
  16816. ((and (eq org-catch-invisible-edits 'smart)
  16817. border-and-ok-direction)
  16818. (message "Unfolding invisible region around point before editing"))
  16819. (t
  16820. ;; Don't do the edit, make the user repeat it in full visibility
  16821. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  16822. (defun org-fix-tags-on-the-fly ()
  16823. (when (and (equal (char-after (point-at-bol)) ?*)
  16824. (org-at-heading-p))
  16825. (org-align-tags-here org-tags-column)))
  16826. (defun org-delete-backward-char (N)
  16827. "Like `delete-backward-char', insert whitespace at field end in tables.
  16828. When deleting backwards, in tables this function will insert whitespace in
  16829. front of the next \"|\" separator, to keep the table aligned. The table will
  16830. still be marked for re-alignment if the field did fill the entire column,
  16831. because, in this case the deletion might narrow the column."
  16832. (interactive "p")
  16833. (save-match-data
  16834. (org-check-before-invisible-edit 'delete-backward)
  16835. (if (and (org-table-p)
  16836. (eq N 1)
  16837. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16838. (looking-at ".*?|"))
  16839. (let ((pos (point))
  16840. (noalign (looking-at "[^|\n\r]* |"))
  16841. (c org-table-may-need-update))
  16842. (backward-delete-char N)
  16843. (if (not overwrite-mode)
  16844. (progn
  16845. (skip-chars-forward "^|")
  16846. (insert " ")
  16847. (goto-char (1- pos))))
  16848. ;; noalign: if there were two spaces at the end, this field
  16849. ;; does not determine the width of the column.
  16850. (if noalign (setq org-table-may-need-update c)))
  16851. (backward-delete-char N)
  16852. (org-fix-tags-on-the-fly))))
  16853. (defun org-delete-char (N)
  16854. "Like `delete-char', but insert whitespace at field end in tables.
  16855. When deleting characters, in tables this function will insert whitespace in
  16856. front of the next \"|\" separator, to keep the table aligned. The table will
  16857. still be marked for re-alignment if the field did fill the entire column,
  16858. because, in this case the deletion might narrow the column."
  16859. (interactive "p")
  16860. (save-match-data
  16861. (org-check-before-invisible-edit 'delete)
  16862. (if (and (org-table-p)
  16863. (not (bolp))
  16864. (not (= (char-after) ?|))
  16865. (eq N 1))
  16866. (if (looking-at ".*?|")
  16867. (let ((pos (point))
  16868. (noalign (looking-at "[^|\n\r]* |"))
  16869. (c org-table-may-need-update))
  16870. (replace-match (concat
  16871. (substring (match-string 0) 1 -1)
  16872. " |"))
  16873. (goto-char pos)
  16874. ;; noalign: if there were two spaces at the end, this field
  16875. ;; does not determine the width of the column.
  16876. (if noalign (setq org-table-may-need-update c)))
  16877. (delete-char N))
  16878. (delete-char N)
  16879. (org-fix-tags-on-the-fly))))
  16880. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16881. (put 'org-self-insert-command 'delete-selection t)
  16882. (put 'orgtbl-self-insert-command 'delete-selection t)
  16883. (put 'org-delete-char 'delete-selection 'supersede)
  16884. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16885. (put 'org-yank 'delete-selection 'yank)
  16886. ;; Make `flyspell-mode' delay after some commands
  16887. (put 'org-self-insert-command 'flyspell-delayed t)
  16888. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16889. (put 'org-delete-char 'flyspell-delayed t)
  16890. (put 'org-delete-backward-char 'flyspell-delayed t)
  16891. ;; Make pabbrev-mode expand after org-mode commands
  16892. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16893. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16894. ;; How to do this: Measure non-white length of current string
  16895. ;; If equal to column width, we should realign.
  16896. (defun org-remap (map &rest commands)
  16897. "In MAP, remap the functions given in COMMANDS.
  16898. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16899. (let (new old)
  16900. (while commands
  16901. (setq old (pop commands) new (pop commands))
  16902. (if (fboundp 'command-remapping)
  16903. (org-defkey map (vector 'remap old) new)
  16904. (substitute-key-definition old new map global-map)))))
  16905. (when (eq org-enable-table-editor 'optimized)
  16906. ;; If the user wants maximum table support, we need to hijack
  16907. ;; some standard editing functions
  16908. (org-remap org-mode-map
  16909. 'self-insert-command 'org-self-insert-command
  16910. 'delete-char 'org-delete-char
  16911. 'delete-backward-char 'org-delete-backward-char)
  16912. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16913. (defvar org-ctrl-c-ctrl-c-hook nil
  16914. "Hook for functions attaching themselves to `C-c C-c'.
  16915. This can be used to add additional functionality to the C-c C-c
  16916. key which executes context-dependent commands. This hook is run
  16917. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16918. run after the last test.
  16919. Each function will be called with no arguments. The function
  16920. must check if the context is appropriate for it to act. If yes,
  16921. it should do its thing and then return a non-nil value. If the
  16922. context is wrong, just do nothing and return nil.")
  16923. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16924. "Hook for functions attaching themselves to `C-c C-c'.
  16925. This can be used to add additional functionality to the C-c C-c
  16926. key which executes context-dependent commands. This hook is run
  16927. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16928. before the first test.
  16929. Each function will be called with no arguments. The function
  16930. must check if the context is appropriate for it to act. If yes,
  16931. it should do its thing and then return a non-nil value. If the
  16932. context is wrong, just do nothing and return nil.")
  16933. (defvar org-tab-first-hook nil
  16934. "Hook for functions to attach themselves to TAB.
  16935. See `org-ctrl-c-ctrl-c-hook' for more information.
  16936. This hook runs as the first action when TAB is pressed, even before
  16937. `org-cycle' messes around with the `outline-regexp' to cater for
  16938. inline tasks and plain list item folding.
  16939. If any function in this hook returns t, any other actions that
  16940. would have been caused by TAB (such as table field motion or visibility
  16941. cycling) will not occur.")
  16942. (defvar org-tab-after-check-for-table-hook nil
  16943. "Hook for functions to attach themselves to TAB.
  16944. See `org-ctrl-c-ctrl-c-hook' for more information.
  16945. This hook runs after it has been established that the cursor is not in a
  16946. table, but before checking if the cursor is in a headline or if global cycling
  16947. should be done.
  16948. If any function in this hook returns t, not other actions like visibility
  16949. cycling will be done.")
  16950. (defvar org-tab-after-check-for-cycling-hook nil
  16951. "Hook for functions to attach themselves to TAB.
  16952. See `org-ctrl-c-ctrl-c-hook' for more information.
  16953. This hook runs after it has been established that not table field motion and
  16954. not visibility should be done because of current context. This is probably
  16955. the place where a package like yasnippets can hook in.")
  16956. (defvar org-tab-before-tab-emulation-hook nil
  16957. "Hook for functions to attach themselves to TAB.
  16958. See `org-ctrl-c-ctrl-c-hook' for more information.
  16959. This hook runs after every other options for TAB have been exhausted, but
  16960. before indentation and \t insertion takes place.")
  16961. (defvar org-metaleft-hook nil
  16962. "Hook for functions attaching themselves to `M-left'.
  16963. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16964. (defvar org-metaright-hook nil
  16965. "Hook for functions attaching themselves to `M-right'.
  16966. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16967. (defvar org-metaup-hook nil
  16968. "Hook for functions attaching themselves to `M-up'.
  16969. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16970. (defvar org-metadown-hook nil
  16971. "Hook for functions attaching themselves to `M-down'.
  16972. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16973. (defvar org-shiftmetaleft-hook nil
  16974. "Hook for functions attaching themselves to `M-S-left'.
  16975. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16976. (defvar org-shiftmetaright-hook nil
  16977. "Hook for functions attaching themselves to `M-S-right'.
  16978. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16979. (defvar org-shiftmetaup-hook nil
  16980. "Hook for functions attaching themselves to `M-S-up'.
  16981. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16982. (defvar org-shiftmetadown-hook nil
  16983. "Hook for functions attaching themselves to `M-S-down'.
  16984. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16985. (defvar org-metareturn-hook nil
  16986. "Hook for functions attaching themselves to `M-RET'.
  16987. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16988. (defvar org-shiftup-hook nil
  16989. "Hook for functions attaching themselves to `S-up'.
  16990. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16991. (defvar org-shiftup-final-hook nil
  16992. "Hook for functions attaching themselves to `S-up'.
  16993. This one runs after all other options except shift-select have been excluded.
  16994. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16995. (defvar org-shiftdown-hook nil
  16996. "Hook for functions attaching themselves to `S-down'.
  16997. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16998. (defvar org-shiftdown-final-hook nil
  16999. "Hook for functions attaching themselves to `S-down'.
  17000. This one runs after all other options except shift-select have been excluded.
  17001. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17002. (defvar org-shiftleft-hook nil
  17003. "Hook for functions attaching themselves to `S-left'.
  17004. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17005. (defvar org-shiftleft-final-hook nil
  17006. "Hook for functions attaching themselves to `S-left'.
  17007. This one runs after all other options except shift-select have been excluded.
  17008. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17009. (defvar org-shiftright-hook nil
  17010. "Hook for functions attaching themselves to `S-right'.
  17011. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17012. (defvar org-shiftright-final-hook nil
  17013. "Hook for functions attaching themselves to `S-right'.
  17014. This one runs after all other options except shift-select have been excluded.
  17015. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17016. (defun org-modifier-cursor-error ()
  17017. "Throw an error, a modified cursor command was applied in wrong context."
  17018. (error "This command is active in special context like tables, headlines or items"))
  17019. (defun org-shiftselect-error ()
  17020. "Throw an error because Shift-Cursor command was applied in wrong context."
  17021. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17022. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17023. (error "This command works only in special context like headlines or timestamps")))
  17024. (defun org-call-for-shift-select (cmd)
  17025. (let ((this-command-keys-shift-translated t))
  17026. (call-interactively cmd)))
  17027. (defun org-shifttab (&optional arg)
  17028. "Global visibility cycling or move to previous table field.
  17029. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17030. on context.
  17031. See the individual commands for more information."
  17032. (interactive "P")
  17033. (cond
  17034. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17035. ((integerp arg)
  17036. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17037. (message "Content view to level: %d" arg)
  17038. (org-content (prefix-numeric-value arg2))
  17039. (setq org-cycle-global-status 'overview)))
  17040. (t (call-interactively 'org-global-cycle))))
  17041. (defun org-shiftmetaleft ()
  17042. "Promote subtree or delete table column.
  17043. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17044. `org-table-delete-column', depending on context. See the
  17045. individual commands for more information."
  17046. (interactive)
  17047. (cond
  17048. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17049. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17050. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17051. ((if (not (org-region-active-p)) (org-at-item-p)
  17052. (save-excursion (goto-char (region-beginning))
  17053. (org-at-item-p)))
  17054. (call-interactively 'org-outdent-item-tree))
  17055. (t (org-modifier-cursor-error))))
  17056. (defun org-shiftmetaright ()
  17057. "Demote subtree or insert table column.
  17058. Calls `org-demote-subtree', `org-indent-item-tree', or
  17059. `org-table-insert-column', depending on context. See the
  17060. individual commands for more information."
  17061. (interactive)
  17062. (cond
  17063. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17064. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17065. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17066. ((if (not (org-region-active-p)) (org-at-item-p)
  17067. (save-excursion (goto-char (region-beginning))
  17068. (org-at-item-p)))
  17069. (call-interactively 'org-indent-item-tree))
  17070. (t (org-modifier-cursor-error))))
  17071. (defun org-shiftmetaup (&optional arg)
  17072. "Move subtree up or kill table row.
  17073. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17074. `org-move-item-up' or `org-timestamp-up', depending on context.
  17075. See the individual commands for more information."
  17076. (interactive "P")
  17077. (cond
  17078. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17079. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17080. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17081. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17082. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17083. (call-interactively 'org-timestamp-up)))
  17084. (t (org-modifier-cursor-error))))
  17085. (defun org-shiftmetadown (&optional arg)
  17086. "Move subtree down or insert table row.
  17087. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17088. `org-move-item-down' or `org-timestamp-up', depending on context.
  17089. See the individual commands for more information."
  17090. (interactive "P")
  17091. (cond
  17092. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17093. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17094. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17095. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17096. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17097. (call-interactively 'org-timestamp-down)))
  17098. (t (org-modifier-cursor-error))))
  17099. (defsubst org-hidden-tree-error ()
  17100. (error
  17101. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17102. (defun org-metaleft (&optional arg)
  17103. "Promote heading or move table column to left.
  17104. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17105. With no specific context, calls the Emacs default `backward-word'.
  17106. See the individual commands for more information."
  17107. (interactive "P")
  17108. (cond
  17109. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17110. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17111. ((org-with-limited-levels
  17112. (or (org-at-heading-p)
  17113. (and (org-region-active-p)
  17114. (save-excursion
  17115. (goto-char (region-beginning))
  17116. (org-at-heading-p)))))
  17117. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17118. (call-interactively 'org-do-promote))
  17119. ;; At an inline task.
  17120. ((org-at-heading-p)
  17121. (call-interactively 'org-inlinetask-promote))
  17122. ((or (org-at-item-p)
  17123. (and (org-region-active-p)
  17124. (save-excursion
  17125. (goto-char (region-beginning))
  17126. (org-at-item-p))))
  17127. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17128. (call-interactively 'org-outdent-item))
  17129. (t (call-interactively 'backward-word))))
  17130. (defun org-metaright (&optional arg)
  17131. "Demote a subtree, a list item or move table column to right.
  17132. In front of a drawer or a block keyword, indent it correctly.
  17133. With no specific context, calls the Emacs default `forward-word'.
  17134. See the individual commands for more information."
  17135. (interactive "P")
  17136. (cond
  17137. ((run-hook-with-args-until-success 'org-metaright-hook))
  17138. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17139. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17140. ((org-at-block-p) (call-interactively 'org-indent-block))
  17141. ((org-with-limited-levels
  17142. (or (org-at-heading-p)
  17143. (and (org-region-active-p)
  17144. (save-excursion
  17145. (goto-char (region-beginning))
  17146. (org-at-heading-p)))))
  17147. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17148. (call-interactively 'org-do-demote))
  17149. ;; At an inline task.
  17150. ((org-at-heading-p)
  17151. (call-interactively 'org-inlinetask-demote))
  17152. ((or (org-at-item-p)
  17153. (and (org-region-active-p)
  17154. (save-excursion
  17155. (goto-char (region-beginning))
  17156. (org-at-item-p))))
  17157. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17158. (call-interactively 'org-indent-item))
  17159. (t (call-interactively 'forward-word))))
  17160. (defun org-check-for-hidden (what)
  17161. "Check if there are hidden headlines/items in the current visual line.
  17162. WHAT can be either `headlines' or `items'. If the current line is
  17163. an outline or item heading and it has a folded subtree below it,
  17164. this function returns t, nil otherwise."
  17165. (let ((re (cond
  17166. ((eq what 'headlines) org-outline-regexp-bol)
  17167. ((eq what 'items) (org-item-beginning-re))
  17168. (t (error "This should not happen"))))
  17169. beg end)
  17170. (save-excursion
  17171. (catch 'exit
  17172. (unless (org-region-active-p)
  17173. (setq beg (point-at-bol))
  17174. (beginning-of-line 2)
  17175. (while (and (not (eobp)) ;; this is like `next-line'
  17176. (get-char-property (1- (point)) 'invisible))
  17177. (beginning-of-line 2))
  17178. (setq end (point))
  17179. (goto-char beg)
  17180. (goto-char (point-at-eol))
  17181. (setq end (max end (point)))
  17182. (while (re-search-forward re end t)
  17183. (if (get-char-property (match-beginning 0) 'invisible)
  17184. (throw 'exit t))))
  17185. nil))))
  17186. (autoload 'org-element-at-point "org-element")
  17187. (autoload 'org-element-type "org-element")
  17188. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  17189. (declare-function org-element-type "org-element" (element))
  17190. (declare-function org-element-contents "org-element" (element))
  17191. (declare-function org-element-property "org-element" (property element))
  17192. (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion))
  17193. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  17194. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  17195. (declare-function org-element--parse-objects "org-element" (beg end acc restriction))
  17196. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  17197. (defun org-metaup (&optional arg)
  17198. "Move subtree up or move table row up.
  17199. Calls `org-move-subtree-up' or `org-table-move-row' or
  17200. `org-move-item-up', depending on context. See the individual commands
  17201. for more information."
  17202. (interactive "P")
  17203. (cond
  17204. ((run-hook-with-args-until-success 'org-metaup-hook))
  17205. ((org-region-active-p)
  17206. (let* ((a (min (region-beginning) (region-end)))
  17207. (b (1- (max (region-beginning) (region-end))))
  17208. (c (save-excursion (goto-char a)
  17209. (move-beginning-of-line 0)))
  17210. (d (save-excursion (goto-char a)
  17211. (move-end-of-line 0) (point))))
  17212. (transpose-regions a b c d)
  17213. (goto-char c)))
  17214. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17215. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17216. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17217. (t (org-drag-element-backward))))
  17218. (defun org-metadown (&optional arg)
  17219. "Move subtree down or move table row down.
  17220. Calls `org-move-subtree-down' or `org-table-move-row' or
  17221. `org-move-item-down', depending on context. See the individual
  17222. commands for more information."
  17223. (interactive "P")
  17224. (cond
  17225. ((run-hook-with-args-until-success 'org-metadown-hook))
  17226. ((org-region-active-p)
  17227. (let* ((a (min (region-beginning) (region-end)))
  17228. (b (max (region-beginning) (region-end)))
  17229. (c (save-excursion (goto-char b)
  17230. (move-beginning-of-line 1)))
  17231. (d (save-excursion (goto-char b)
  17232. (move-end-of-line 1) (1+ (point)))))
  17233. (transpose-regions a b c d)
  17234. (goto-char d)))
  17235. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17236. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17237. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17238. (t (org-drag-element-forward))))
  17239. (defun org-shiftup (&optional arg)
  17240. "Increase item in timestamp or increase priority of current headline.
  17241. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17242. depending on context. See the individual commands for more information."
  17243. (interactive "P")
  17244. (cond
  17245. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17246. ((and org-support-shift-select (org-region-active-p))
  17247. (org-call-for-shift-select 'previous-line))
  17248. ((org-at-timestamp-p t)
  17249. (call-interactively (if org-edit-timestamp-down-means-later
  17250. 'org-timestamp-down 'org-timestamp-up)))
  17251. ((and (not (eq org-support-shift-select 'always))
  17252. org-enable-priority-commands
  17253. (org-at-heading-p))
  17254. (call-interactively 'org-priority-up))
  17255. ((and (not org-support-shift-select) (org-at-item-p))
  17256. (call-interactively 'org-previous-item))
  17257. ((org-clocktable-try-shift 'up arg))
  17258. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17259. (org-support-shift-select
  17260. (org-call-for-shift-select 'previous-line))
  17261. (t (org-shiftselect-error))))
  17262. (defun org-shiftdown (&optional arg)
  17263. "Decrease item in timestamp or decrease priority of current headline.
  17264. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17265. depending on context. See the individual commands for more information."
  17266. (interactive "P")
  17267. (cond
  17268. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17269. ((and org-support-shift-select (org-region-active-p))
  17270. (org-call-for-shift-select 'next-line))
  17271. ((org-at-timestamp-p t)
  17272. (call-interactively (if org-edit-timestamp-down-means-later
  17273. 'org-timestamp-up 'org-timestamp-down)))
  17274. ((and (not (eq org-support-shift-select 'always))
  17275. org-enable-priority-commands
  17276. (org-at-heading-p))
  17277. (call-interactively 'org-priority-down))
  17278. ((and (not org-support-shift-select) (org-at-item-p))
  17279. (call-interactively 'org-next-item))
  17280. ((org-clocktable-try-shift 'down arg))
  17281. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17282. (org-support-shift-select
  17283. (org-call-for-shift-select 'next-line))
  17284. (t (org-shiftselect-error))))
  17285. (defun org-shiftright (&optional arg)
  17286. "Cycle the thing at point or in the current line, depending on context.
  17287. Depending on context, this does one of the following:
  17288. - switch a timestamp at point one day into the future
  17289. - on a headline, switch to the next TODO keyword.
  17290. - on an item, switch entire list to the next bullet type
  17291. - on a property line, switch to the next allowed value
  17292. - on a clocktable definition line, move time block into the future"
  17293. (interactive "P")
  17294. (cond
  17295. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17296. ((and org-support-shift-select (org-region-active-p))
  17297. (org-call-for-shift-select 'forward-char))
  17298. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17299. ((and (not (eq org-support-shift-select 'always))
  17300. (org-at-heading-p))
  17301. (let ((org-inhibit-logging
  17302. (not org-treat-S-cursor-todo-selection-as-state-change))
  17303. (org-inhibit-blocking
  17304. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17305. (org-call-with-arg 'org-todo 'right)))
  17306. ((or (and org-support-shift-select
  17307. (not (eq org-support-shift-select 'always))
  17308. (org-at-item-bullet-p))
  17309. (and (not org-support-shift-select) (org-at-item-p)))
  17310. (org-call-with-arg 'org-cycle-list-bullet nil))
  17311. ((and (not (eq org-support-shift-select 'always))
  17312. (org-at-property-p))
  17313. (call-interactively 'org-property-next-allowed-value))
  17314. ((org-clocktable-try-shift 'right arg))
  17315. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17316. (org-support-shift-select
  17317. (org-call-for-shift-select 'forward-char))
  17318. (t (org-shiftselect-error))))
  17319. (defun org-shiftleft (&optional arg)
  17320. "Cycle the thing at point or in the current line, depending on context.
  17321. Depending on context, this does one of the following:
  17322. - switch a timestamp at point one day into the past
  17323. - on a headline, switch to the previous TODO keyword.
  17324. - on an item, switch entire list to the previous bullet type
  17325. - on a property line, switch to the previous allowed value
  17326. - on a clocktable definition line, move time block into the past"
  17327. (interactive "P")
  17328. (cond
  17329. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17330. ((and org-support-shift-select (org-region-active-p))
  17331. (org-call-for-shift-select 'backward-char))
  17332. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17333. ((and (not (eq org-support-shift-select 'always))
  17334. (org-at-heading-p))
  17335. (let ((org-inhibit-logging
  17336. (not org-treat-S-cursor-todo-selection-as-state-change))
  17337. (org-inhibit-blocking
  17338. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17339. (org-call-with-arg 'org-todo 'left)))
  17340. ((or (and org-support-shift-select
  17341. (not (eq org-support-shift-select 'always))
  17342. (org-at-item-bullet-p))
  17343. (and (not org-support-shift-select) (org-at-item-p)))
  17344. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17345. ((and (not (eq org-support-shift-select 'always))
  17346. (org-at-property-p))
  17347. (call-interactively 'org-property-previous-allowed-value))
  17348. ((org-clocktable-try-shift 'left arg))
  17349. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17350. (org-support-shift-select
  17351. (org-call-for-shift-select 'backward-char))
  17352. (t (org-shiftselect-error))))
  17353. (defun org-shiftcontrolright ()
  17354. "Switch to next TODO set."
  17355. (interactive)
  17356. (cond
  17357. ((and org-support-shift-select (org-region-active-p))
  17358. (org-call-for-shift-select 'forward-word))
  17359. ((and (not (eq org-support-shift-select 'always))
  17360. (org-at-heading-p))
  17361. (org-call-with-arg 'org-todo 'nextset))
  17362. (org-support-shift-select
  17363. (org-call-for-shift-select 'forward-word))
  17364. (t (org-shiftselect-error))))
  17365. (defun org-shiftcontrolleft ()
  17366. "Switch to previous TODO set."
  17367. (interactive)
  17368. (cond
  17369. ((and org-support-shift-select (org-region-active-p))
  17370. (org-call-for-shift-select 'backward-word))
  17371. ((and (not (eq org-support-shift-select 'always))
  17372. (org-at-heading-p))
  17373. (org-call-with-arg 'org-todo 'previousset))
  17374. (org-support-shift-select
  17375. (org-call-for-shift-select 'backward-word))
  17376. (t (org-shiftselect-error))))
  17377. (defun org-shiftcontrolup ()
  17378. "Change timestamps synchronously up in CLOCK log lines."
  17379. (interactive)
  17380. (cond ((and (not org-support-shift-select)
  17381. (org-at-clock-log-p)
  17382. (org-at-timestamp-p t))
  17383. (org-clock-timestamps-up))
  17384. (t (org-shiftselect-error))))
  17385. (defun org-shiftcontroldown ()
  17386. "Change timestamps synchronously down in CLOCK log lines."
  17387. (interactive)
  17388. (cond ((and (not org-support-shift-select)
  17389. (org-at-clock-log-p)
  17390. (org-at-timestamp-p t))
  17391. (org-clock-timestamps-down))
  17392. (t (org-shiftselect-error))))
  17393. (defun org-ctrl-c-ret ()
  17394. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17395. (interactive)
  17396. (cond
  17397. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17398. (t (call-interactively 'org-insert-heading))))
  17399. (defun org-find-visible ()
  17400. (let ((s (point)))
  17401. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17402. (get-char-property s 'invisible)))
  17403. s))
  17404. (defun org-find-invisible ()
  17405. (let ((s (point)))
  17406. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17407. (not (get-char-property s 'invisible))))
  17408. s))
  17409. (defun org-copy-visible (beg end)
  17410. "Copy the visible parts of the region."
  17411. (interactive "r")
  17412. (let (snippets s)
  17413. (save-excursion
  17414. (save-restriction
  17415. (narrow-to-region beg end)
  17416. (setq s (goto-char (point-min)))
  17417. (while (not (= (point) (point-max)))
  17418. (goto-char (org-find-invisible))
  17419. (push (buffer-substring s (point)) snippets)
  17420. (setq s (goto-char (org-find-visible))))))
  17421. (kill-new (apply 'concat (nreverse snippets)))))
  17422. (defun org-copy-special ()
  17423. "Copy region in table or copy current subtree.
  17424. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17425. See the individual commands for more information."
  17426. (interactive)
  17427. (call-interactively
  17428. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17429. (defun org-cut-special ()
  17430. "Cut region in table or cut current subtree.
  17431. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17432. See the individual commands for more information."
  17433. (interactive)
  17434. (call-interactively
  17435. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17436. (defun org-paste-special (arg)
  17437. "Paste rectangular region into table, or past subtree relative to level.
  17438. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17439. See the individual commands for more information."
  17440. (interactive "P")
  17441. (if (org-at-table-p)
  17442. (org-table-paste-rectangle)
  17443. (org-paste-subtree arg)))
  17444. (defsubst org-in-fixed-width-region-p ()
  17445. "Is point in a fixed-width region?"
  17446. (save-match-data
  17447. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  17448. (defun org-edit-special (&optional arg)
  17449. "Call a special editor for the stuff at point.
  17450. When at a table, call the formula editor with `org-table-edit-formulas'.
  17451. When in a source code block, call `org-edit-src-code'.
  17452. When in a fixed-width region, call `org-edit-fixed-width-region'.
  17453. When in an #+include line, visit the included file.
  17454. On a link, call `ffap' to visit the link at point.
  17455. Otherwise, return a user error."
  17456. (interactive)
  17457. ;; possibly prep session before editing source
  17458. (when (and (org-in-src-block-p) arg)
  17459. (let* ((info (org-babel-get-src-block-info))
  17460. (lang (nth 0 info))
  17461. (params (nth 2 info))
  17462. (session (cdr (assoc :session params))))
  17463. (when (and info session) ;; we are in a source-code block with a session
  17464. (funcall
  17465. (intern (concat "org-babel-prep-session:" lang)) session params))))
  17466. (cond ;; proceed with `org-edit-special'
  17467. ((save-excursion
  17468. (beginning-of-line 1)
  17469. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  17470. (find-file (org-trim (match-string 1))))
  17471. ((or (org-at-table-p)
  17472. (save-excursion
  17473. (beginning-of-line 1)
  17474. (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
  17475. (call-interactively 'org-table-edit-formulas))
  17476. ((or (org-in-block-p '("src" "example" "latex" "html"))
  17477. (org-at-table.el-p))
  17478. (org-edit-src-code))
  17479. ((org-in-fixed-width-region-p) (org-edit-fixed-width-region))
  17480. ((org-at-regexp-p org-any-link-re) (call-interactively 'ffap))
  17481. (t (user-error "No special environment to edit here"))))
  17482. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17483. (defun org-ctrl-c-ctrl-c (&optional arg)
  17484. "Set tags in headline, or update according to changed information at point.
  17485. This command does many different things, depending on context:
  17486. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17487. this is what we do.
  17488. - If the cursor is on a statistics cookie, update it.
  17489. - If the cursor is in a headline, prompt for tags and insert them
  17490. into the current line, aligned to `org-tags-column'. When called
  17491. with prefix arg, realign all tags in the current buffer.
  17492. - If the cursor is in one of the special #+KEYWORD lines, this
  17493. triggers scanning the buffer for these lines and updating the
  17494. information.
  17495. - If the cursor is inside a table, realign the table. This command
  17496. works even if the automatic table editor has been turned off.
  17497. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17498. the entire table.
  17499. - If the cursor is at a footnote reference or definition, jump to
  17500. the corresponding definition or references, respectively.
  17501. - If the cursor is a the beginning of a dynamic block, update it.
  17502. - If the current buffer is a capture buffer, close note and file it.
  17503. - If the cursor is on a <<<target>>>, update radio targets and
  17504. corresponding links in this buffer.
  17505. - If the cursor is on a numbered item in a plain list, renumber the
  17506. ordered list.
  17507. - If the cursor is on a checkbox, toggle it.
  17508. - If the cursor is on a code block, evaluate it. The variable
  17509. `org-confirm-babel-evaluate' can be used to control prompting
  17510. before code block evaluation, by default every code block
  17511. evaluation requires confirmation. Code block evaluation can be
  17512. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17513. (interactive "P")
  17514. (let ((org-enable-table-editor t))
  17515. (cond
  17516. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17517. org-occur-highlights
  17518. org-latex-fragment-image-overlays)
  17519. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17520. (org-remove-occur-highlights)
  17521. (org-remove-latex-fragment-image-overlays)
  17522. (message "Temporary highlights/overlays removed from current buffer"))
  17523. ((and (local-variable-p 'org-finish-function (current-buffer))
  17524. (fboundp org-finish-function))
  17525. (funcall org-finish-function))
  17526. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17527. ((org-in-regexp org-ts-regexp-both)
  17528. (org-timestamp-change 0 'day))
  17529. ((or (looking-at org-property-start-re)
  17530. (org-at-property-p))
  17531. (call-interactively 'org-property-action))
  17532. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  17533. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  17534. (or (org-at-heading-p) (org-at-item-p)))
  17535. (call-interactively 'org-update-statistics-cookies))
  17536. ((org-at-heading-p) (call-interactively 'org-set-tags))
  17537. ((org-at-table.el-p)
  17538. (message "Use C-c ' to edit table.el tables"))
  17539. ((org-at-table-p)
  17540. (org-table-maybe-eval-formula)
  17541. (if arg
  17542. (call-interactively 'org-table-recalculate)
  17543. (org-table-maybe-recalculate-line))
  17544. (call-interactively 'org-table-align)
  17545. (orgtbl-send-table 'maybe))
  17546. ((or (org-footnote-at-reference-p)
  17547. (org-footnote-at-definition-p))
  17548. (call-interactively 'org-footnote-action))
  17549. ((org-at-item-checkbox-p)
  17550. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  17551. ;; list only if at top item.
  17552. (let* ((cbox (match-string 1))
  17553. (struct (org-list-struct))
  17554. (old-struct (copy-tree struct))
  17555. (parents (org-list-parents-alist struct))
  17556. (orderedp (org-entry-get nil "ORDERED"))
  17557. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17558. block-item)
  17559. ;; Use a light version of `org-toggle-checkbox' to avoid
  17560. ;; computing list structure twice.
  17561. (let ((new-box (cond
  17562. ((equal arg '(16)) "[-]")
  17563. ((equal arg '(4)) nil)
  17564. ((equal "[X]" cbox) "[ ]")
  17565. (t "[X]"))))
  17566. (if (and firstp arg)
  17567. ;; If at first item of sub-list, remove check-box from
  17568. ;; every item at the same level.
  17569. (mapc
  17570. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17571. (org-list-get-all-items
  17572. (point-at-bol) struct (org-list-prevs-alist struct)))
  17573. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17574. ;; Replicate `org-list-write-struct', while grabbing a return
  17575. ;; value from `org-list-struct-fix-box'.
  17576. (org-list-struct-fix-ind struct parents 2)
  17577. (org-list-struct-fix-item-end struct)
  17578. (let ((prevs (org-list-prevs-alist struct)))
  17579. (org-list-struct-fix-bul struct prevs)
  17580. (org-list-struct-fix-ind struct parents)
  17581. (setq block-item
  17582. (org-list-struct-fix-box struct parents prevs orderedp)))
  17583. (if (equal struct old-struct)
  17584. (user-error "Cannot toggle this checkbox (unchecked subitems?)")
  17585. (org-list-struct-apply-struct struct old-struct)
  17586. (org-update-checkbox-count-maybe))
  17587. (when block-item
  17588. (message
  17589. "Checkboxes were removed due to unchecked box at line %d"
  17590. (org-current-line block-item)))
  17591. (when firstp (org-list-send-list 'maybe))))
  17592. ((org-at-item-p)
  17593. ;; Cursor at an item: repair list. Do checkbox related actions
  17594. ;; only if function was called with an argument. Send list only
  17595. ;; if at top item.
  17596. (let* ((struct (org-list-struct))
  17597. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17598. old-struct)
  17599. (when arg
  17600. (setq old-struct (copy-tree struct))
  17601. (if firstp
  17602. ;; If at first item of sub-list, add check-box to every
  17603. ;; item at the same level.
  17604. (mapc
  17605. (lambda (pos)
  17606. (unless (org-list-get-checkbox pos struct)
  17607. (org-list-set-checkbox pos struct "[ ]")))
  17608. (org-list-get-all-items
  17609. (point-at-bol) struct (org-list-prevs-alist struct)))
  17610. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17611. (org-list-write-struct
  17612. struct (org-list-parents-alist struct) old-struct)
  17613. (when arg (org-update-checkbox-count-maybe))
  17614. (when firstp (org-list-send-list 'maybe))))
  17615. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17616. ;; Dynamic block
  17617. (beginning-of-line 1)
  17618. (save-excursion (org-update-dblock)))
  17619. ((save-excursion
  17620. (let ((case-fold-search t))
  17621. (beginning-of-line 1)
  17622. (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
  17623. (cond
  17624. ((or (equal (match-string 1) "TBLFM")
  17625. (equal (match-string 1) "tblfm"))
  17626. ;; Recalculate the table before this line
  17627. (save-excursion
  17628. (beginning-of-line 1)
  17629. (skip-chars-backward " \r\n\t")
  17630. (if (org-at-table-p)
  17631. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17632. (t
  17633. (let ((org-inhibit-startup-visibility-stuff t)
  17634. (org-startup-align-all-tables nil))
  17635. (when (boundp 'org-table-coordinate-overlays)
  17636. (mapc 'delete-overlay org-table-coordinate-overlays)
  17637. (setq org-table-coordinate-overlays nil))
  17638. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17639. (message "Local setup has been refreshed"))))
  17640. ((org-clock-update-time-maybe))
  17641. (t
  17642. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17643. (error "C-c C-c can do nothing useful at this location"))))))
  17644. (defun org-mode-restart ()
  17645. "Restart Org-mode, to scan again for special lines.
  17646. Also updates the keyword regular expressions."
  17647. (interactive)
  17648. (org-mode)
  17649. (message "Org-mode restarted"))
  17650. (defun org-kill-note-or-show-branches ()
  17651. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17652. (interactive)
  17653. (if (not org-finish-function)
  17654. (progn
  17655. (hide-subtree)
  17656. (call-interactively 'show-branches))
  17657. (let ((org-note-abort t))
  17658. (funcall org-finish-function))))
  17659. (defun org-return (&optional indent)
  17660. "Goto next table row or insert a newline.
  17661. Calls `org-table-next-row' or `newline', depending on context.
  17662. See the individual commands for more information."
  17663. (interactive)
  17664. (let (org-ts-what)
  17665. (cond
  17666. ((or (bobp) (org-in-src-block-p))
  17667. (if indent (newline-and-indent) (newline)))
  17668. ((org-at-table-p)
  17669. (org-table-justify-field-maybe)
  17670. (call-interactively 'org-table-next-row))
  17671. ;; when `newline-and-indent' is called within a list, make sure
  17672. ;; text moved stays inside the item.
  17673. ((and (org-in-item-p) indent)
  17674. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17675. (progn
  17676. (save-match-data (newline))
  17677. (org-indent-line-to (length (match-string 0))))
  17678. (let ((ind (org-get-indentation)))
  17679. (newline)
  17680. (if (org-looking-back org-list-end-re)
  17681. (org-indent-line)
  17682. (org-indent-line-to ind)))))
  17683. ((and org-return-follows-link
  17684. (org-at-timestamp-p t)
  17685. (not (eq org-ts-what 'after)))
  17686. (org-follow-timestamp-link))
  17687. ((and org-return-follows-link
  17688. (let ((tprop (get-text-property (point) 'face)))
  17689. (or (eq tprop 'org-link)
  17690. (and (listp tprop) (memq 'org-link tprop)))))
  17691. (call-interactively 'org-open-at-point))
  17692. ((and (org-at-heading-p)
  17693. (looking-at
  17694. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17695. (org-show-entry)
  17696. (end-of-line 1)
  17697. (newline))
  17698. (t (if indent (newline-and-indent) (newline))))))
  17699. (defun org-return-indent ()
  17700. "Goto next table row or insert a newline and indent.
  17701. Calls `org-table-next-row' or `newline-and-indent', depending on
  17702. context. See the individual commands for more information."
  17703. (interactive)
  17704. (org-return t))
  17705. (defun org-ctrl-c-star ()
  17706. "Compute table, or change heading status of lines.
  17707. Calls `org-table-recalculate' or `org-toggle-heading',
  17708. depending on context."
  17709. (interactive)
  17710. (cond
  17711. ((org-at-table-p)
  17712. (call-interactively 'org-table-recalculate))
  17713. (t
  17714. ;; Convert all lines in region to list items
  17715. (call-interactively 'org-toggle-heading))))
  17716. (defun org-ctrl-c-minus ()
  17717. "Insert separator line in table or modify bullet status of line.
  17718. Also turns a plain line or a region of lines into list items.
  17719. Calls `org-table-insert-hline', `org-toggle-item', or
  17720. `org-cycle-list-bullet', depending on context."
  17721. (interactive)
  17722. (cond
  17723. ((org-at-table-p)
  17724. (call-interactively 'org-table-insert-hline))
  17725. ((org-region-active-p)
  17726. (call-interactively 'org-toggle-item))
  17727. ((org-in-item-p)
  17728. (call-interactively 'org-cycle-list-bullet))
  17729. (t
  17730. (call-interactively 'org-toggle-item))))
  17731. (defun org-toggle-item (arg)
  17732. "Convert headings or normal lines to items, items to normal lines.
  17733. If there is no active region, only the current line is considered.
  17734. If the first non blank line in the region is an headline, convert
  17735. all headlines to items, shifting text accordingly.
  17736. If it is an item, convert all items to normal lines.
  17737. If it is normal text, change region into an item. With a prefix
  17738. argument ARG, change each line in region into an item."
  17739. (interactive "P")
  17740. (let ((shift-text
  17741. (function
  17742. ;; Shift text in current section to IND, from point to END.
  17743. ;; The function leaves point to END line.
  17744. (lambda (ind end)
  17745. (let ((min-i 1000) (end (copy-marker end)))
  17746. ;; First determine the minimum indentation (MIN-I) of
  17747. ;; the text.
  17748. (save-excursion
  17749. (catch 'exit
  17750. (while (< (point) end)
  17751. (let ((i (org-get-indentation)))
  17752. (cond
  17753. ;; Skip blank lines and inline tasks.
  17754. ((looking-at "^[ \t]*$"))
  17755. ((looking-at org-outline-regexp-bol))
  17756. ;; We can't find less than 0 indentation.
  17757. ((zerop i) (throw 'exit (setq min-i 0)))
  17758. ((< i min-i) (setq min-i i))))
  17759. (forward-line))))
  17760. ;; Then indent each line so that a line indented to
  17761. ;; MIN-I becomes indented to IND. Ignore blank lines
  17762. ;; and inline tasks in the process.
  17763. (let ((delta (- ind min-i)))
  17764. (while (< (point) end)
  17765. (unless (or (looking-at "^[ \t]*$")
  17766. (looking-at org-outline-regexp-bol))
  17767. (org-indent-line-to (+ (org-get-indentation) delta)))
  17768. (forward-line)))))))
  17769. (skip-blanks
  17770. (function
  17771. ;; Return beginning of first non-blank line, starting from
  17772. ;; line at POS.
  17773. (lambda (pos)
  17774. (save-excursion
  17775. (goto-char pos)
  17776. (skip-chars-forward " \r\t\n")
  17777. (point-at-bol)))))
  17778. beg end)
  17779. ;; Determine boundaries of changes.
  17780. (if (org-region-active-p)
  17781. (setq beg (funcall skip-blanks (region-beginning))
  17782. end (copy-marker (region-end)))
  17783. (setq beg (funcall skip-blanks (point-at-bol))
  17784. end (copy-marker (point-at-eol))))
  17785. ;; Depending on the starting line, choose an action on the text
  17786. ;; between BEG and END.
  17787. (org-with-limited-levels
  17788. (save-excursion
  17789. (goto-char beg)
  17790. (cond
  17791. ;; Case 1. Start at an item: de-itemize. Note that it only
  17792. ;; happens when a region is active: `org-ctrl-c-minus'
  17793. ;; would call `org-cycle-list-bullet' otherwise.
  17794. ((org-at-item-p)
  17795. (while (< (point) end)
  17796. (when (org-at-item-p)
  17797. (skip-chars-forward " \t")
  17798. (delete-region (point) (match-end 0)))
  17799. (forward-line)))
  17800. ;; Case 2. Start at an heading: convert to items.
  17801. ((org-at-heading-p)
  17802. (let* ((bul (org-list-bullet-string "-"))
  17803. (bul-len (length bul))
  17804. ;; Indentation of the first heading. It should be
  17805. ;; relative to the indentation of its parent, if any.
  17806. (start-ind (save-excursion
  17807. (cond
  17808. ((not org-adapt-indentation) 0)
  17809. ((not (outline-previous-heading)) 0)
  17810. (t (length (match-string 0))))))
  17811. ;; Level of first heading. Further headings will be
  17812. ;; compared to it to determine hierarchy in the list.
  17813. (ref-level (org-reduced-level (org-outline-level))))
  17814. (while (< (point) end)
  17815. (let* ((level (org-reduced-level (org-outline-level)))
  17816. (delta (max 0 (- level ref-level))))
  17817. ;; If current headline is less indented than the first
  17818. ;; one, set it as reference, in order to preserve
  17819. ;; subtrees.
  17820. (when (< level ref-level) (setq ref-level level))
  17821. (replace-match bul t t)
  17822. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17823. ;; Ensure all text down to END (or SECTION-END) belongs
  17824. ;; to the newly created item.
  17825. (let ((section-end (save-excursion
  17826. (or (outline-next-heading) (point)))))
  17827. (forward-line)
  17828. (funcall shift-text
  17829. (+ start-ind (* (1+ delta) bul-len))
  17830. (min end section-end)))))))
  17831. ;; Case 3. Normal line with ARG: turn each non-item line into
  17832. ;; an item.
  17833. (arg
  17834. (while (< (point) end)
  17835. (unless (or (org-at-heading-p) (org-at-item-p))
  17836. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17837. (replace-match
  17838. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17839. (forward-line)))
  17840. ;; Case 4. Normal line without ARG: make the first line of
  17841. ;; region an item, and shift indentation of others
  17842. ;; lines to set them as item's body.
  17843. (t (let* ((bul (org-list-bullet-string "-"))
  17844. (bul-len (length bul))
  17845. (ref-ind (org-get-indentation)))
  17846. (skip-chars-forward " \t")
  17847. (insert bul)
  17848. (forward-line)
  17849. (while (< (point) end)
  17850. ;; Ensure that lines less indented than first one
  17851. ;; still get included in item body.
  17852. (funcall shift-text
  17853. (+ ref-ind bul-len)
  17854. (min end (save-excursion (or (outline-next-heading)
  17855. (point)))))
  17856. (forward-line)))))))))
  17857. (defun org-toggle-heading (&optional nstars)
  17858. "Convert headings to normal text, or items or text to headings.
  17859. If there is no active region, only the current line is considered.
  17860. With a \\[universal-argument] prefix, convert the whole list at
  17861. point into heading.
  17862. In a region:
  17863. - If the first non blank line is an headline, remove the stars
  17864. from all headlines in the region.
  17865. - If it is a normal line turn each and every normal line (i.e. not an
  17866. heading or an item) in the region into a heading.
  17867. - If it is a plain list item, turn all plain list items into headings.
  17868. When converting a line into a heading, the number of stars is chosen
  17869. such that the lines become children of the current entry. However,
  17870. when a prefix argument is given, its value determines the number of
  17871. stars to add."
  17872. (interactive "P")
  17873. (let ((skip-blanks
  17874. (function
  17875. ;; Return beginning of first non-blank line, starting from
  17876. ;; line at POS.
  17877. (lambda (pos)
  17878. (save-excursion
  17879. (goto-char pos)
  17880. (while (org-at-comment-p) (forward-line))
  17881. (skip-chars-forward " \r\t\n")
  17882. (point-at-bol)))))
  17883. beg end toggled)
  17884. ;; Determine boundaries of changes. If a universal prefix has
  17885. ;; been given, put the list in a region. If region ends at a bol,
  17886. ;; do not consider the last line to be in the region.
  17887. (when (and current-prefix-arg (org-at-item-p))
  17888. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  17889. (org-mark-element))
  17890. (if (org-region-active-p)
  17891. (setq beg (funcall skip-blanks (region-beginning))
  17892. end (copy-marker (save-excursion
  17893. (goto-char (region-end))
  17894. (if (bolp) (point) (point-at-eol)))))
  17895. (setq beg (funcall skip-blanks (point-at-bol))
  17896. end (copy-marker (point-at-eol))))
  17897. ;; Ensure inline tasks don't count as headings.
  17898. (org-with-limited-levels
  17899. (save-excursion
  17900. (goto-char beg)
  17901. (cond
  17902. ;; Case 1. Started at an heading: de-star headings.
  17903. ((org-at-heading-p)
  17904. (while (< (point) end)
  17905. (when (org-at-heading-p t)
  17906. (looking-at org-outline-regexp) (replace-match "")
  17907. (setq toggled t))
  17908. (forward-line)))
  17909. ;; Case 2. Started at an item: change items into headlines.
  17910. ;; One star will be added by `org-list-to-subtree'.
  17911. ((org-at-item-p)
  17912. (let* ((stars (make-string
  17913. (if nstars
  17914. ;; subtract the star that will be added again by
  17915. ;; `org-list-to-subtree'
  17916. (1- (prefix-numeric-value current-prefix-arg))
  17917. (or (org-current-level) 0))
  17918. ?*))
  17919. (add-stars
  17920. (cond (nstars "") ; stars from prefix only
  17921. ((equal stars "") "") ; before first heading
  17922. (org-odd-levels-only "*") ; inside heading, odd
  17923. (t "")))) ; inside heading, oddeven
  17924. (while (< (point) end)
  17925. (when (org-at-item-p)
  17926. ;; Pay attention to cases when region ends before list.
  17927. (let* ((struct (org-list-struct))
  17928. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17929. (save-restriction
  17930. (narrow-to-region (point) list-end)
  17931. (insert
  17932. (org-list-to-subtree
  17933. (org-list-parse-list t)
  17934. '(:istart (concat stars add-stars (funcall get-stars depth))
  17935. :icount (concat stars add-stars (funcall get-stars depth)))))))
  17936. (setq toggled t))
  17937. (forward-line))))
  17938. ;; Case 3. Started at normal text: make every line an heading,
  17939. ;; skipping headlines and items.
  17940. (t (let* ((stars (make-string
  17941. (if nstars
  17942. (prefix-numeric-value current-prefix-arg)
  17943. (or (org-current-level) 0))
  17944. ?*))
  17945. (add-stars
  17946. (cond (nstars "") ; stars from prefix only
  17947. ((equal stars "") "*") ; before first heading
  17948. (org-odd-levels-only "**") ; inside heading, odd
  17949. (t "*"))) ; inside heading, oddeven
  17950. (rpl (concat stars add-stars " ")))
  17951. (while (< (point) end)
  17952. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  17953. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17954. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  17955. (forward-line)))))))
  17956. (unless toggled (message "Cannot toggle heading from here"))))
  17957. (defun org-meta-return (&optional arg)
  17958. "Insert a new heading or wrap a region in a table.
  17959. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17960. See the individual commands for more information."
  17961. (interactive "P")
  17962. (cond
  17963. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17964. ((or (org-at-drawer-p) (org-at-property-p))
  17965. (newline-and-indent))
  17966. ((org-at-table-p)
  17967. (call-interactively 'org-table-wrap-region))
  17968. (t (call-interactively 'org-insert-heading))))
  17969. ;;; Menu entries
  17970. (defsubst org-in-subtree-not-table-p ()
  17971. "Are we in a subtree and not in a table?"
  17972. (and (not (org-before-first-heading-p))
  17973. (not (org-at-table-p))))
  17974. ;; Define the Org-mode menus
  17975. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17976. '("Tbl"
  17977. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17978. ["Next Field" org-cycle (org-at-table-p)]
  17979. ["Previous Field" org-shifttab (org-at-table-p)]
  17980. ["Next Row" org-return (org-at-table-p)]
  17981. "--"
  17982. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17983. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17984. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17985. "--"
  17986. ("Column"
  17987. ["Move Column Left" org-metaleft (org-at-table-p)]
  17988. ["Move Column Right" org-metaright (org-at-table-p)]
  17989. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17990. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17991. ("Row"
  17992. ["Move Row Up" org-metaup (org-at-table-p)]
  17993. ["Move Row Down" org-metadown (org-at-table-p)]
  17994. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17995. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17996. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17997. "--"
  17998. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17999. ("Rectangle"
  18000. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18001. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18002. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18003. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18004. "--"
  18005. ("Calculate"
  18006. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18007. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18008. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18009. "--"
  18010. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18011. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18012. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18013. "--"
  18014. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18015. "--"
  18016. ["Sum Column/Rectangle" org-table-sum
  18017. (or (org-at-table-p) (org-region-active-p))]
  18018. ["Which Column?" org-table-current-column (org-at-table-p)])
  18019. ["Debug Formulas"
  18020. org-table-toggle-formula-debugger
  18021. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18022. ["Show Col/Row Numbers"
  18023. org-table-toggle-coordinate-overlays
  18024. :style toggle
  18025. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18026. "--"
  18027. ["Create" org-table-create (and (not (org-at-table-p))
  18028. org-enable-table-editor)]
  18029. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18030. ["Import from File" org-table-import (not (org-at-table-p))]
  18031. ["Export to File" org-table-export (org-at-table-p)]
  18032. "--"
  18033. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18034. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18035. '("Org"
  18036. ("Show/Hide"
  18037. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18038. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18039. ["Sparse Tree..." org-sparse-tree t]
  18040. ["Reveal Context" org-reveal t]
  18041. ["Show All" show-all t]
  18042. "--"
  18043. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18044. "--"
  18045. ["New Heading" org-insert-heading t]
  18046. ("Navigate Headings"
  18047. ["Up" outline-up-heading t]
  18048. ["Next" outline-next-visible-heading t]
  18049. ["Previous" outline-previous-visible-heading t]
  18050. ["Next Same Level" outline-forward-same-level t]
  18051. ["Previous Same Level" outline-backward-same-level t]
  18052. "--"
  18053. ["Jump" org-goto t])
  18054. ("Edit Structure"
  18055. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18056. "--"
  18057. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18058. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18059. "--"
  18060. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18061. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18062. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18063. "--"
  18064. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18065. "--"
  18066. ["Copy visible text" org-copy-visible t]
  18067. "--"
  18068. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18069. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18070. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18071. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18072. "--"
  18073. ["Sort Region/Children" org-sort t]
  18074. "--"
  18075. ["Convert to odd levels" org-convert-to-odd-levels t]
  18076. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18077. ("Editing"
  18078. ["Emphasis..." org-emphasize t]
  18079. ["Edit Source Example" org-edit-special t]
  18080. "--"
  18081. ["Footnote new/jump" org-footnote-action t]
  18082. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18083. ("Archive"
  18084. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18085. "--"
  18086. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18087. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18088. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18089. )
  18090. "--"
  18091. ("Hyperlinks"
  18092. ["Store Link (Global)" org-store-link t]
  18093. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18094. ["Insert Link" org-insert-link t]
  18095. ["Follow Link" org-open-at-point t]
  18096. "--"
  18097. ["Next link" org-next-link t]
  18098. ["Previous link" org-previous-link t]
  18099. "--"
  18100. ["Descriptive Links"
  18101. org-toggle-link-display
  18102. :style radio
  18103. :selected org-descriptive-links
  18104. ]
  18105. ["Literal Links"
  18106. org-toggle-link-display
  18107. :style radio
  18108. :selected (not org-descriptive-links)])
  18109. "--"
  18110. ("TODO Lists"
  18111. ["TODO/DONE/-" org-todo t]
  18112. ("Select keyword"
  18113. ["Next keyword" org-shiftright (org-at-heading-p)]
  18114. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18115. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18116. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18117. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18118. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18119. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18120. "--"
  18121. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18122. :selected org-enforce-todo-dependencies :style toggle :active t]
  18123. "Settings for tree at point"
  18124. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18125. :selected (org-entry-get nil "ORDERED")
  18126. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18127. ["Do Children parallel" org-toggle-ordered-property :style radio
  18128. :selected (not (org-entry-get nil "ORDERED"))
  18129. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18130. "--"
  18131. ["Set Priority" org-priority t]
  18132. ["Priority Up" org-shiftup t]
  18133. ["Priority Down" org-shiftdown t]
  18134. "--"
  18135. ["Get news from all feeds" org-feed-update-all t]
  18136. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18137. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18138. ("TAGS and Properties"
  18139. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18140. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18141. "--"
  18142. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18143. ["Column view of properties" org-columns t]
  18144. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18145. ("Dates and Scheduling"
  18146. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18147. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18148. ("Change Date"
  18149. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18150. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18151. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18152. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18153. ["Compute Time Range" org-evaluate-time-range t]
  18154. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18155. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18156. "--"
  18157. ["Custom time format" org-toggle-time-stamp-overlays
  18158. :style radio :selected org-display-custom-times]
  18159. "--"
  18160. ["Goto Calendar" org-goto-calendar t]
  18161. ["Date from Calendar" org-date-from-calendar t]
  18162. "--"
  18163. ["Start/Restart Timer" org-timer-start t]
  18164. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18165. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18166. ["Insert Timer String" org-timer t]
  18167. ["Insert Timer Item" org-timer-item t])
  18168. ("Logging work"
  18169. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18170. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18171. ["Clock out" org-clock-out t]
  18172. ["Clock cancel" org-clock-cancel t]
  18173. "--"
  18174. ["Mark as default task" org-clock-mark-default-task t]
  18175. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18176. ["Goto running clock" org-clock-goto t]
  18177. "--"
  18178. ["Display times" org-clock-display t]
  18179. ["Create clock table" org-clock-report t]
  18180. "--"
  18181. ["Record DONE time"
  18182. (progn (setq org-log-done (not org-log-done))
  18183. (message "Switching to %s will %s record a timestamp"
  18184. (car org-done-keywords)
  18185. (if org-log-done "automatically" "not")))
  18186. :style toggle :selected org-log-done])
  18187. "--"
  18188. ["Agenda Command..." org-agenda t]
  18189. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18190. ("File List for Agenda")
  18191. ("Special views current file"
  18192. ["TODO Tree" org-show-todo-tree t]
  18193. ["Check Deadlines" org-check-deadlines t]
  18194. ["Timeline" org-timeline t]
  18195. ["Tags/Property tree" org-match-sparse-tree t])
  18196. "--"
  18197. ["Export/Publish..." org-export t]
  18198. ("LaTeX"
  18199. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18200. :selected org-cdlatex-mode]
  18201. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18202. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18203. ["Modify math symbol" org-cdlatex-math-modify
  18204. (org-inside-LaTeX-fragment-p)]
  18205. ["Insert citation" org-reftex-citation t]
  18206. "--"
  18207. ["Template for BEAMER" (progn (require 'org-beamer)
  18208. (org-insert-beamer-options-template)) t])
  18209. "--"
  18210. ("MobileOrg"
  18211. ["Push Files and Views" org-mobile-push t]
  18212. ["Get Captured and Flagged" org-mobile-pull t]
  18213. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18214. "--"
  18215. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18216. "--"
  18217. ("Documentation"
  18218. ["Show Version" org-version t]
  18219. ["Info Documentation" org-info t])
  18220. ("Customize"
  18221. ["Browse Org Group" org-customize t]
  18222. "--"
  18223. ["Expand This Menu" org-create-customize-menu
  18224. (fboundp 'customize-menu-create)])
  18225. ["Send bug report" org-submit-bug-report t]
  18226. "--"
  18227. ("Refresh/Reload"
  18228. ["Refresh setup current buffer" org-mode-restart t]
  18229. ["Reload Org (after update)" org-reload t]
  18230. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  18231. ))
  18232. (defun org-info (&optional node)
  18233. "Read documentation for Org-mode in the info system.
  18234. With optional NODE, go directly to that node."
  18235. (interactive)
  18236. (info (format "(org)%s" (or node ""))))
  18237. ;;;###autoload
  18238. (defun org-submit-bug-report ()
  18239. "Submit a bug report on Org-mode via mail.
  18240. Don't hesitate to report any problems or inaccurate documentation.
  18241. If you don't have setup sending mail from (X)Emacs, please copy the
  18242. output buffer into your mail program, as it gives us important
  18243. information about your Org-mode version and configuration."
  18244. (interactive)
  18245. (require 'reporter)
  18246. (org-load-modules-maybe)
  18247. (org-require-autoloaded-modules)
  18248. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18249. (reporter-submit-bug-report
  18250. "emacs-orgmode@gnu.org"
  18251. (org-version nil 'full)
  18252. (let (list)
  18253. (save-window-excursion
  18254. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18255. (delete-other-windows)
  18256. (erase-buffer)
  18257. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18258. We would like to add your full Org-mode and Outline configuration to the
  18259. bug report. This greatly simplifies the work of the maintainer and
  18260. other experts on the mailing list.
  18261. HOWEVER, some variables you have customized may contain private
  18262. information. The names of customers, colleagues, or friends, might
  18263. appear in the form of file names, tags, todo states, or search strings.
  18264. If you answer yes to the prompt, you might want to check and remove
  18265. such private information before sending the email.")
  18266. (add-text-properties (point-min) (point-max) '(face org-warning))
  18267. (when (yes-or-no-p "Include your Org-mode configuration ")
  18268. (mapatoms
  18269. (lambda (v)
  18270. (and (boundp v)
  18271. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18272. (or (and (symbol-value v)
  18273. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18274. (and
  18275. (get v 'custom-type) (get v 'standard-value)
  18276. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18277. (push v list)))))
  18278. (kill-buffer (get-buffer "*Warn about privacy*"))
  18279. list))
  18280. nil nil
  18281. "Remember to cover the basics, that is, what you expected to happen and
  18282. what in fact did happen. You don't know how to make a good report? See
  18283. http://orgmode.org/manual/Feedback.html#Feedback
  18284. Your bug report will be posted to the Org-mode mailing list.
  18285. ------------------------------------------------------------------------")
  18286. (save-excursion
  18287. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18288. (replace-match "\\1Bug: \\3 [\\2]")))))
  18289. (defun org-install-agenda-files-menu ()
  18290. (let ((bl (buffer-list)))
  18291. (save-excursion
  18292. (while bl
  18293. (set-buffer (pop bl))
  18294. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18295. (when (derived-mode-p 'org-mode)
  18296. (easy-menu-change
  18297. '("Org") "File List for Agenda"
  18298. (append
  18299. (list
  18300. ["Edit File List" (org-edit-agenda-file-list) t]
  18301. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18302. ["Remove Current File from List" org-remove-file t]
  18303. ["Cycle through agenda files" org-cycle-agenda-files t]
  18304. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18305. "--")
  18306. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18307. ;;;; Documentation
  18308. (defun org-require-autoloaded-modules ()
  18309. (interactive)
  18310. (mapc 'require
  18311. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  18312. org-docbook org-exp org-html org-icalendar
  18313. org-id org-latex
  18314. org-publish org-remember org-table
  18315. org-timer org-xoxo)))
  18316. ;;;###autoload
  18317. (defun org-reload (&optional uncompiled)
  18318. "Reload all org lisp files.
  18319. With prefix arg UNCOMPILED, load the uncompiled versions."
  18320. (interactive "P")
  18321. (require 'find-func)
  18322. (let* ((file-re "^org\\(-.*\\)?\\.el")
  18323. (dir-org (file-name-directory (org-find-library-dir "org")))
  18324. (dir-org-contrib (ignore-errors
  18325. (file-name-directory
  18326. (org-find-library-dir "org-contribdir"))))
  18327. (babel-files
  18328. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  18329. (append (list nil "comint" "eval" "exp" "keys"
  18330. "lob" "ref" "table" "tangle")
  18331. (delq nil
  18332. (mapcar
  18333. (lambda (lang)
  18334. (when (cdr lang) (symbol-name (car lang))))
  18335. org-babel-load-languages)))))
  18336. (files
  18337. (append babel-files
  18338. (and dir-org-contrib
  18339. (directory-files dir-org-contrib t file-re))
  18340. (directory-files dir-org t file-re)))
  18341. (remove-re (concat (if (featurep 'xemacs)
  18342. "org-colview" "org-colview-xemacs")
  18343. "\\'")))
  18344. (setq files (mapcar 'file-name-sans-extension files))
  18345. (setq files (mapcar
  18346. (lambda (x) (if (string-match remove-re x) nil x))
  18347. files))
  18348. (setq files (delq nil files))
  18349. (mapc
  18350. (lambda (f)
  18351. (when (featurep (intern (file-name-nondirectory f)))
  18352. (if (and (not uncompiled)
  18353. (file-exists-p (concat f ".elc")))
  18354. (load (concat f ".elc") nil nil 'nosuffix)
  18355. (load (concat f ".el") nil nil 'nosuffix))))
  18356. files)
  18357. (load (concat dir-org "org-version.el") 'noerror nil 'nosuffix))
  18358. (org-version nil 'full 'message))
  18359. ;;;###autoload
  18360. (defun org-customize ()
  18361. "Call the customize function with org as argument."
  18362. (interactive)
  18363. (org-load-modules-maybe)
  18364. (org-require-autoloaded-modules)
  18365. (customize-browse 'org))
  18366. (defun org-create-customize-menu ()
  18367. "Create a full customization menu for Org-mode, insert it into the menu."
  18368. (interactive)
  18369. (org-load-modules-maybe)
  18370. (org-require-autoloaded-modules)
  18371. (if (fboundp 'customize-menu-create)
  18372. (progn
  18373. (easy-menu-change
  18374. '("Org") "Customize"
  18375. `(["Browse Org group" org-customize t]
  18376. "--"
  18377. ,(customize-menu-create 'org)
  18378. ["Set" Custom-set t]
  18379. ["Save" Custom-save t]
  18380. ["Reset to Current" Custom-reset-current t]
  18381. ["Reset to Saved" Custom-reset-saved t]
  18382. ["Reset to Standard Settings" Custom-reset-standard t]))
  18383. (message "\"Org\"-menu now contains full customization menu"))
  18384. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18385. ;;;; Miscellaneous stuff
  18386. ;;; Generally useful functions
  18387. (defun org-get-at-bol (property)
  18388. "Get text property PROPERTY at beginning of line."
  18389. (get-text-property (point-at-bol) property))
  18390. (defun org-find-text-property-in-string (prop s)
  18391. "Return the first non-nil value of property PROP in string S."
  18392. (or (get-text-property 0 prop s)
  18393. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18394. prop s)))
  18395. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18396. "Display the given MESSAGE as a warning."
  18397. (if (fboundp 'display-warning)
  18398. (display-warning 'org message
  18399. (if (featurep 'xemacs) 'warning :warning))
  18400. (let ((buf (get-buffer-create "*Org warnings*")))
  18401. (with-current-buffer buf
  18402. (goto-char (point-max))
  18403. (insert "Warning (Org): " message)
  18404. (unless (bolp)
  18405. (newline)))
  18406. (display-buffer buf)
  18407. (sit-for 0))))
  18408. (defun org-eval (form)
  18409. "Eval FORM and return result."
  18410. (condition-case error
  18411. (eval form)
  18412. (error (format "%%![Error: %s]" error))))
  18413. (defun org-in-clocktable-p ()
  18414. "Check if the cursor is in a clocktable."
  18415. (let ((pos (point)) start)
  18416. (save-excursion
  18417. (end-of-line 1)
  18418. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18419. (setq start (match-beginning 0))
  18420. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18421. (>= (match-end 0) pos)
  18422. start))))
  18423. (defun org-in-commented-line ()
  18424. "Is point in a line starting with `#'?"
  18425. (equal (char-after (point-at-bol)) ?#))
  18426. (defun org-in-indented-comment-line ()
  18427. "Is point in a line starting with `#' after some white space?"
  18428. (save-excursion
  18429. (save-match-data
  18430. (goto-char (point-at-bol))
  18431. (looking-at "[ \t]*#"))))
  18432. (defun org-in-verbatim-emphasis ()
  18433. (save-match-data
  18434. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18435. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18436. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18437. (if (and marker (marker-buffer marker)
  18438. (buffer-live-p (marker-buffer marker)))
  18439. (progn
  18440. (org-pop-to-buffer-same-window (marker-buffer marker))
  18441. (if (or (> marker (point-max)) (< marker (point-min)))
  18442. (widen))
  18443. (goto-char marker)
  18444. (org-show-context 'org-goto))
  18445. (if bookmark
  18446. (bookmark-jump bookmark)
  18447. (error "Cannot find location"))))
  18448. (defun org-quote-csv-field (s)
  18449. "Quote field for inclusion in CSV material."
  18450. (if (string-match "[\",]" s)
  18451. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18452. s))
  18453. (defun org-force-self-insert (N)
  18454. "Needed to enforce self-insert under remapping."
  18455. (interactive "p")
  18456. (self-insert-command N))
  18457. (defun org-string-width (s)
  18458. "Compute width of string, ignoring invisible characters.
  18459. This ignores character with invisibility property `org-link', and also
  18460. characters with property `org-cwidth', because these will become invisible
  18461. upon the next fontification round."
  18462. (let (b l)
  18463. (when (or (eq t buffer-invisibility-spec)
  18464. (assq 'org-link buffer-invisibility-spec))
  18465. (while (setq b (text-property-any 0 (length s)
  18466. 'invisible 'org-link s))
  18467. (setq s (concat (substring s 0 b)
  18468. (substring s (or (next-single-property-change
  18469. b 'invisible s) (length s)))))))
  18470. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18471. (setq s (concat (substring s 0 b)
  18472. (substring s (or (next-single-property-change
  18473. b 'org-cwidth s) (length s))))))
  18474. (setq l (string-width s) b -1)
  18475. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18476. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18477. l))
  18478. (defun org-shorten-string (s maxlength)
  18479. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18480. If the string is shorter or has length MAXLENGTH, just return the
  18481. original string. If it is longer, the functions finds a space in the
  18482. string, breaks this string off at that locations and adds three dots
  18483. as ellipsis. Including the ellipsis, the string will not be longer
  18484. than MAXLENGTH. If finding a good breaking point in the string does
  18485. not work, the string is just chopped off in the middle of a word
  18486. if necessary."
  18487. (if (<= (length s) maxlength)
  18488. s
  18489. (let* ((n (max (- maxlength 4) 1))
  18490. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18491. (if (string-match re s)
  18492. (concat (match-string 1 s) "...")
  18493. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18494. (defun org-get-indentation (&optional line)
  18495. "Get the indentation of the current line, interpreting tabs.
  18496. When LINE is given, assume it represents a line and compute its indentation."
  18497. (if line
  18498. (if (string-match "^ *" (org-remove-tabs line))
  18499. (match-end 0))
  18500. (save-excursion
  18501. (beginning-of-line 1)
  18502. (skip-chars-forward " \t")
  18503. (current-column))))
  18504. (defun org-get-string-indentation (s)
  18505. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18506. (let ((n -1) (i 0) (w tab-width) c)
  18507. (catch 'exit
  18508. (while (< (setq n (1+ n)) (length s))
  18509. (setq c (aref s n))
  18510. (cond ((= c ?\ ) (setq i (1+ i)))
  18511. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18512. (t (throw 'exit t)))))
  18513. i))
  18514. (defun org-remove-tabs (s &optional width)
  18515. "Replace tabulators in S with spaces.
  18516. Assumes that s is a single line, starting in column 0."
  18517. (setq width (or width tab-width))
  18518. (while (string-match "\t" s)
  18519. (setq s (replace-match
  18520. (make-string
  18521. (- (* width (/ (+ (match-beginning 0) width) width))
  18522. (match-beginning 0)) ?\ )
  18523. t t s)))
  18524. s)
  18525. (defun org-fix-indentation (line ind)
  18526. "Fix indentation in LINE.
  18527. IND is a cons cell with target and minimum indentation.
  18528. If the current indentation in LINE is smaller than the minimum,
  18529. leave it alone. If it is larger than ind, set it to the target."
  18530. (let* ((l (org-remove-tabs line))
  18531. (i (org-get-indentation l))
  18532. (i1 (car ind)) (i2 (cdr ind)))
  18533. (if (>= i i2) (setq l (substring line i2)))
  18534. (if (> i1 0)
  18535. (concat (make-string i1 ?\ ) l)
  18536. l)))
  18537. (defun org-remove-indentation (code &optional n)
  18538. "Remove the maximum common indentation from the lines in CODE.
  18539. N may optionally be the number of spaces to remove."
  18540. (with-temp-buffer
  18541. (insert code)
  18542. (org-do-remove-indentation n)
  18543. (buffer-string)))
  18544. (defun org-do-remove-indentation (&optional n)
  18545. "Remove the maximum common indentation from the buffer."
  18546. (untabify (point-min) (point-max))
  18547. (let ((min 10000) re)
  18548. (if n
  18549. (setq min n)
  18550. (goto-char (point-min))
  18551. (while (re-search-forward "^ *[^ \n]" nil t)
  18552. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  18553. (unless (or (= min 0) (= min 10000))
  18554. (setq re (format "^ \\{%d\\}" min))
  18555. (goto-char (point-min))
  18556. (while (re-search-forward re nil t)
  18557. (replace-match "")
  18558. (end-of-line 1))
  18559. min)))
  18560. (defun org-fill-template (template alist)
  18561. "Find each %key of ALIST in TEMPLATE and replace it."
  18562. (let ((case-fold-search nil)
  18563. entry key value)
  18564. (setq alist (sort (copy-sequence alist)
  18565. (lambda (a b) (< (length (car a)) (length (car b))))))
  18566. (while (setq entry (pop alist))
  18567. (setq template
  18568. (replace-regexp-in-string
  18569. (concat "%" (regexp-quote (car entry)))
  18570. (or (cdr entry) "") template t t)))
  18571. template))
  18572. (defun org-base-buffer (buffer)
  18573. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  18574. (if (not buffer)
  18575. buffer
  18576. (or (buffer-base-buffer buffer)
  18577. buffer)))
  18578. (defun org-trim (s)
  18579. "Remove whitespace at beginning and end of string."
  18580. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  18581. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  18582. s)
  18583. (defun org-wrap (string &optional width lines)
  18584. "Wrap string to either a number of lines, or a width in characters.
  18585. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18586. that costs. If there is a word longer than WIDTH, the text is actually
  18587. wrapped to the length of that word.
  18588. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18589. many lines, whatever width that takes.
  18590. The return value is a list of lines, without newlines at the end."
  18591. (let* ((words (org-split-string string "[ \t\n]+"))
  18592. (maxword (apply 'max (mapcar 'org-string-width words)))
  18593. w ll)
  18594. (cond (width
  18595. (org-do-wrap words (max maxword width)))
  18596. (lines
  18597. (setq w maxword)
  18598. (setq ll (org-do-wrap words maxword))
  18599. (if (<= (length ll) lines)
  18600. ll
  18601. (setq ll words)
  18602. (while (> (length ll) lines)
  18603. (setq w (1+ w))
  18604. (setq ll (org-do-wrap words w)))
  18605. ll))
  18606. (t (error "Cannot wrap this")))))
  18607. (defun org-do-wrap (words width)
  18608. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18609. (let (lines line)
  18610. (while words
  18611. (setq line (pop words))
  18612. (while (and words (< (+ (length line) (length (car words))) width))
  18613. (setq line (concat line " " (pop words))))
  18614. (setq lines (push line lines)))
  18615. (nreverse lines)))
  18616. (defun org-split-string (string &optional separators)
  18617. "Splits STRING into substrings at SEPARATORS.
  18618. No empty strings are returned if there are matches at the beginning
  18619. and end of string."
  18620. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18621. (start 0)
  18622. notfirst
  18623. (list nil))
  18624. (while (and (string-match rexp string
  18625. (if (and notfirst
  18626. (= start (match-beginning 0))
  18627. (< start (length string)))
  18628. (1+ start) start))
  18629. (< (match-beginning 0) (length string)))
  18630. (setq notfirst t)
  18631. (or (eq (match-beginning 0) 0)
  18632. (and (eq (match-beginning 0) (match-end 0))
  18633. (eq (match-beginning 0) start))
  18634. (setq list
  18635. (cons (substring string start (match-beginning 0))
  18636. list)))
  18637. (setq start (match-end 0)))
  18638. (or (eq start (length string))
  18639. (setq list
  18640. (cons (substring string start)
  18641. list)))
  18642. (nreverse list)))
  18643. (defun org-quote-vert (s)
  18644. "Replace \"|\" with \"\\vert\"."
  18645. (while (string-match "|" s)
  18646. (setq s (replace-match "\\vert" t t s)))
  18647. s)
  18648. (defun org-uuidgen-p (s)
  18649. "Is S an ID created by UUIDGEN?"
  18650. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18651. (defun org-in-src-block-p (&optional inside)
  18652. "Whether point is in a code source block.
  18653. When INSIDE is non-nil, don't consider we are within a src block
  18654. when point is at #+BEGIN_SRC or #+END_SRC."
  18655. (let ((case-fold-search t) ov)
  18656. (or (and (setq ov (overlays-at (point)))
  18657. (memq 'org-block-background
  18658. (overlay-properties (car ov))))
  18659. (and (not inside)
  18660. (save-match-data
  18661. (save-excursion
  18662. (beginning-of-line)
  18663. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  18664. (defun org-context ()
  18665. "Return a list of contexts of the current cursor position.
  18666. If several contexts apply, all are returned.
  18667. Each context entry is a list with a symbol naming the context, and
  18668. two positions indicating start and end of the context. Possible
  18669. contexts are:
  18670. :headline anywhere in a headline
  18671. :headline-stars on the leading stars in a headline
  18672. :todo-keyword on a TODO keyword (including DONE) in a headline
  18673. :tags on the TAGS in a headline
  18674. :priority on the priority cookie in a headline
  18675. :item on the first line of a plain list item
  18676. :item-bullet on the bullet/number of a plain list item
  18677. :checkbox on the checkbox in a plain list item
  18678. :table in an org-mode table
  18679. :table-special on a special filed in a table
  18680. :table-table in a table.el table
  18681. :clocktable in a clocktable
  18682. :src-block in a source block
  18683. :link on a hyperlink
  18684. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18685. :target on a <<target>>
  18686. :radio-target on a <<<radio-target>>>
  18687. :latex-fragment on a LaTeX fragment
  18688. :latex-preview on a LaTeX fragment with overlaid preview image
  18689. This function expects the position to be visible because it uses font-lock
  18690. faces as a help to recognize the following contexts: :table-special, :link,
  18691. and :keyword."
  18692. (let* ((f (get-text-property (point) 'face))
  18693. (faces (if (listp f) f (list f)))
  18694. (case-fold-search t)
  18695. (p (point)) clist o)
  18696. ;; First the large context
  18697. (cond
  18698. ((org-at-heading-p t)
  18699. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18700. (when (progn
  18701. (beginning-of-line 1)
  18702. (looking-at org-todo-line-tags-regexp))
  18703. (push (org-point-in-group p 1 :headline-stars) clist)
  18704. (push (org-point-in-group p 2 :todo-keyword) clist)
  18705. (push (org-point-in-group p 4 :tags) clist))
  18706. (goto-char p)
  18707. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18708. (if (looking-at "\\[#[A-Z0-9]\\]")
  18709. (push (org-point-in-group p 0 :priority) clist)))
  18710. ((org-at-item-p)
  18711. (push (org-point-in-group p 2 :item-bullet) clist)
  18712. (push (list :item (point-at-bol)
  18713. (save-excursion (org-end-of-item) (point)))
  18714. clist)
  18715. (and (org-at-item-checkbox-p)
  18716. (push (org-point-in-group p 0 :checkbox) clist)))
  18717. ((org-at-table-p)
  18718. (push (list :table (org-table-begin) (org-table-end)) clist)
  18719. (if (memq 'org-formula faces)
  18720. (push (list :table-special
  18721. (previous-single-property-change p 'face)
  18722. (next-single-property-change p 'face)) clist)))
  18723. ((org-at-table-p 'any)
  18724. (push (list :table-table) clist)))
  18725. (goto-char p)
  18726. (let ((case-fold-search t))
  18727. ;; New the "medium" contexts: clocktables, source blocks
  18728. (cond ((org-in-clocktable-p)
  18729. (push (list :clocktable
  18730. (and (or (looking-at "#\\+BEGIN: clocktable")
  18731. (search-backward "#+BEGIN: clocktable" nil t))
  18732. (match-beginning 0))
  18733. (and (re-search-forward "#\\+END:?" nil t)
  18734. (match-end 0))) clist))
  18735. ((org-in-src-block-p)
  18736. (push (list :src-block
  18737. (and (or (looking-at "#\\+BEGIN_SRC")
  18738. (search-backward "#+BEGIN_SRC" nil t))
  18739. (match-beginning 0))
  18740. (and (search-forward "#+END_SRC" nil t)
  18741. (match-beginning 0))) clist))))
  18742. (goto-char p)
  18743. ;; Now the small context
  18744. (cond
  18745. ((org-at-timestamp-p)
  18746. (push (org-point-in-group p 0 :timestamp) clist))
  18747. ((memq 'org-link faces)
  18748. (push (list :link
  18749. (previous-single-property-change p 'face)
  18750. (next-single-property-change p 'face)) clist))
  18751. ((memq 'org-special-keyword faces)
  18752. (push (list :keyword
  18753. (previous-single-property-change p 'face)
  18754. (next-single-property-change p 'face)) clist))
  18755. ((org-at-target-p)
  18756. (push (org-point-in-group p 0 :target) clist)
  18757. (goto-char (1- (match-beginning 0)))
  18758. (if (looking-at org-radio-target-regexp)
  18759. (push (org-point-in-group p 0 :radio-target) clist))
  18760. (goto-char p))
  18761. ((setq o (car (delq nil
  18762. (mapcar
  18763. (lambda (x)
  18764. (if (memq x org-latex-fragment-image-overlays) x))
  18765. (overlays-at (point))))))
  18766. (push (list :latex-fragment
  18767. (overlay-start o) (overlay-end o)) clist)
  18768. (push (list :latex-preview
  18769. (overlay-start o) (overlay-end o)) clist))
  18770. ((org-inside-LaTeX-fragment-p)
  18771. ;; FIXME: positions wrong.
  18772. (push (list :latex-fragment (point) (point)) clist)))
  18773. (setq clist (nreverse (delq nil clist)))
  18774. clist))
  18775. ;; FIXME: Compare with at-regexp-p Do we need both?
  18776. (defun org-in-regexp (re &optional nlines visually)
  18777. "Check if point is inside a match of regexp.
  18778. Normally only the current line is checked, but you can include NLINES extra
  18779. lines both before and after point into the search.
  18780. If VISUALLY is set, require that the cursor is not after the match but
  18781. really on, so that the block visually is on the match."
  18782. (catch 'exit
  18783. (let ((pos (point))
  18784. (eol (point-at-eol (+ 1 (or nlines 0))))
  18785. (inc (if visually 1 0)))
  18786. (save-excursion
  18787. (beginning-of-line (- 1 (or nlines 0)))
  18788. (while (re-search-forward re eol t)
  18789. (if (and (<= (match-beginning 0) pos)
  18790. (>= (+ inc (match-end 0)) pos))
  18791. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18792. (defun org-at-regexp-p (regexp)
  18793. "Is point inside a match of REGEXP in the current line?"
  18794. (catch 'exit
  18795. (save-excursion
  18796. (let ((pos (point)) (end (point-at-eol)))
  18797. (beginning-of-line 1)
  18798. (while (re-search-forward regexp end t)
  18799. (if (and (<= (match-beginning 0) pos)
  18800. (>= (match-end 0) pos))
  18801. (throw 'exit t)))
  18802. nil))))
  18803. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18804. "Non-nil when point is between matches of START-RE and END-RE.
  18805. Also return a non-nil value when point is on one of the matches.
  18806. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18807. buffer positions. Default values are the positions of headlines
  18808. surrounding the point.
  18809. The functions returns a cons cell whose car (resp. cdr) is the
  18810. position before START-RE (resp. after END-RE)."
  18811. (save-match-data
  18812. (let ((pos (point))
  18813. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18814. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18815. beg end)
  18816. (save-excursion
  18817. ;; Point is on a block when on START-RE or if START-RE can be
  18818. ;; found before it...
  18819. (and (or (org-at-regexp-p start-re)
  18820. (re-search-backward start-re limit-up t))
  18821. (setq beg (match-beginning 0))
  18822. ;; ... and END-RE after it...
  18823. (goto-char (match-end 0))
  18824. (re-search-forward end-re limit-down t)
  18825. (> (setq end (match-end 0)) pos)
  18826. ;; ... without another START-RE in-between.
  18827. (goto-char (match-beginning 0))
  18828. (not (re-search-backward start-re (1+ beg) t))
  18829. ;; Return value.
  18830. (cons beg end))))))
  18831. (defun org-in-block-p (names)
  18832. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18833. NAMES is a list of strings containing names of blocks.
  18834. Return first block name matched, or nil. Beware that in case of
  18835. nested blocks, the returned name may not belong to the closest
  18836. block from point."
  18837. (save-match-data
  18838. (catch 'exit
  18839. (let ((case-fold-search t)
  18840. (lim-up (save-excursion (outline-previous-heading)))
  18841. (lim-down (save-excursion (outline-next-heading))))
  18842. (mapc (lambda (name)
  18843. (let ((n (regexp-quote name)))
  18844. (when (org-between-regexps-p
  18845. (concat "^[ \t]*#\\+begin_" n)
  18846. (concat "^[ \t]*#\\+end_" n)
  18847. lim-up lim-down)
  18848. (throw 'exit n))))
  18849. names))
  18850. nil)))
  18851. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18852. "Call `multi-occur' with buffers for all agenda files."
  18853. (interactive "sOrg-files matching: \np")
  18854. (let* ((files (org-agenda-files))
  18855. (tnames (mapcar 'file-truename files))
  18856. (extra org-agenda-text-search-extra-files)
  18857. f)
  18858. (when (eq (car extra) 'agenda-archives)
  18859. (setq extra (cdr extra))
  18860. (setq files (org-add-archive-files files)))
  18861. (while (setq f (pop extra))
  18862. (unless (member (file-truename f) tnames)
  18863. (add-to-list 'files f 'append)
  18864. (add-to-list 'tnames (file-truename f) 'append)))
  18865. (multi-occur
  18866. (mapcar (lambda (x)
  18867. (with-current-buffer
  18868. (or (get-file-buffer x) (find-file-noselect x))
  18869. (widen)
  18870. (current-buffer)))
  18871. files)
  18872. regexp)))
  18873. (if (boundp 'occur-mode-find-occurrence-hook)
  18874. ;; Emacs 23
  18875. (add-hook 'occur-mode-find-occurrence-hook
  18876. (lambda ()
  18877. (when (derived-mode-p 'org-mode)
  18878. (org-reveal))))
  18879. ;; Emacs 22
  18880. (defadvice occur-mode-goto-occurrence
  18881. (after org-occur-reveal activate)
  18882. (and (derived-mode-p 'org-mode) (org-reveal)))
  18883. (defadvice occur-mode-goto-occurrence-other-window
  18884. (after org-occur-reveal activate)
  18885. (and (derived-mode-p 'org-mode) (org-reveal)))
  18886. (defadvice occur-mode-display-occurrence
  18887. (after org-occur-reveal activate)
  18888. (when (derived-mode-p 'org-mode)
  18889. (let ((pos (occur-mode-find-occurrence)))
  18890. (with-current-buffer (marker-buffer pos)
  18891. (save-excursion
  18892. (goto-char pos)
  18893. (org-reveal)))))))
  18894. (defun org-occur-link-in-agenda-files ()
  18895. "Create a link and search for it in the agendas.
  18896. The link is not stored in `org-stored-links', it is just created
  18897. for the search purpose."
  18898. (interactive)
  18899. (let ((link (condition-case nil
  18900. (org-store-link nil)
  18901. (error "Unable to create a link to here"))))
  18902. (org-occur-in-agenda-files (regexp-quote link))))
  18903. (defun org-uniquify (list)
  18904. "Remove duplicate elements from LIST."
  18905. (let (res)
  18906. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18907. res))
  18908. (defun org-delete-all (elts list)
  18909. "Remove all elements in ELTS from LIST."
  18910. (while elts
  18911. (setq list (delete (pop elts) list)))
  18912. list)
  18913. (defun org-count (cl-item cl-seq)
  18914. "Count the number of occurrences of ITEM in SEQ.
  18915. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18916. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18917. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18918. (while (< cl-start cl-end)
  18919. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18920. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18921. (setq cl-start (1+ cl-start)))
  18922. cl-count))
  18923. (defun org-remove-if (predicate seq)
  18924. "Remove everything from SEQ that fulfills PREDICATE."
  18925. (let (res e)
  18926. (while seq
  18927. (setq e (pop seq))
  18928. (if (not (funcall predicate e)) (push e res)))
  18929. (nreverse res)))
  18930. (defun org-remove-if-not (predicate seq)
  18931. "Remove everything from SEQ that does not fulfill PREDICATE."
  18932. (let (res e)
  18933. (while seq
  18934. (setq e (pop seq))
  18935. (if (funcall predicate e) (push e res)))
  18936. (nreverse res)))
  18937. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18938. "Reduce two-argument FUNCTION across SEQ.
  18939. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18940. \":initial-value\" removed."
  18941. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18942. (cadr (memq :initial-value cl-keys)))
  18943. (cl-seq (pop cl-seq))
  18944. (t (funcall cl-func)))))
  18945. (while cl-seq
  18946. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18947. cl-accum))
  18948. (defun org-back-over-empty-lines ()
  18949. "Move backwards over whitespace, to the beginning of the first empty line.
  18950. Returns the number of empty lines passed."
  18951. (let ((pos (point)))
  18952. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18953. (skip-chars-backward " \t\n\r")
  18954. (unless (eobp)
  18955. (forward-line -1)))
  18956. (beginning-of-line 2)
  18957. (goto-char (min (point) pos))
  18958. (count-lines (point) pos)))
  18959. (defun org-skip-whitespace ()
  18960. (skip-chars-forward " \t\n\r"))
  18961. (defun org-point-in-group (point group &optional context)
  18962. "Check if POINT is in match-group GROUP.
  18963. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18964. match. If the match group does not exist or point is not inside it,
  18965. return nil."
  18966. (and (match-beginning group)
  18967. (>= point (match-beginning group))
  18968. (<= point (match-end group))
  18969. (if context
  18970. (list context (match-beginning group) (match-end group))
  18971. t)))
  18972. (defun org-switch-to-buffer-other-window (&rest args)
  18973. "Switch to buffer in a second window on the current frame.
  18974. In particular, do not allow pop-up frames.
  18975. Returns the newly created buffer."
  18976. (org-no-popups
  18977. (apply 'switch-to-buffer-other-window args)))
  18978. (defun org-combine-plists (&rest plists)
  18979. "Create a single property list from all plists in PLISTS.
  18980. The process starts by copying the first list, and then setting properties
  18981. from the other lists. Settings in the last list are the most significant
  18982. ones and overrule settings in the other lists."
  18983. (let ((rtn (copy-sequence (pop plists)))
  18984. p v ls)
  18985. (while plists
  18986. (setq ls (pop plists))
  18987. (while ls
  18988. (setq p (pop ls) v (pop ls))
  18989. (setq rtn (plist-put rtn p v))))
  18990. rtn))
  18991. (defun org-replace-escapes (string table)
  18992. "Replace %-escapes in STRING with values in TABLE.
  18993. TABLE is an association list with keys like \"%a\" and string values.
  18994. The sequences in STRING may contain normal field width and padding information,
  18995. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18996. so values can contain further %-escapes if they are define later in TABLE."
  18997. (let ((tbl (copy-alist table))
  18998. (case-fold-search nil)
  18999. (pchg 0)
  19000. e re rpl)
  19001. (while (setq e (pop tbl))
  19002. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19003. (when (and (cdr e) (string-match re (cdr e)))
  19004. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19005. (safe "SREF"))
  19006. (add-text-properties 0 3 (list 'sref sref) safe)
  19007. (setcdr e (replace-match safe t t (cdr e)))))
  19008. (while (string-match re string)
  19009. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19010. (cdr e)))
  19011. (setq string (replace-match rpl t t string))))
  19012. (while (setq pchg (next-property-change pchg string))
  19013. (let ((sref (get-text-property pchg 'sref string)))
  19014. (when (and sref (string-match "SREF" string pchg))
  19015. (setq string (replace-match sref t t string)))))
  19016. string))
  19017. (defun org-sublist (list start end)
  19018. "Return a section of LIST, from START to END.
  19019. Counting starts at 1."
  19020. (let (rtn (c start))
  19021. (setq list (nthcdr (1- start) list))
  19022. (while (and list (<= c end))
  19023. (push (pop list) rtn)
  19024. (setq c (1+ c)))
  19025. (nreverse rtn)))
  19026. (defun org-find-base-buffer-visiting (file)
  19027. "Like `find-buffer-visiting' but always return the base buffer and
  19028. not an indirect buffer."
  19029. (let ((buf (or (get-file-buffer file)
  19030. (find-buffer-visiting file))))
  19031. (if buf
  19032. (or (buffer-base-buffer buf) buf)
  19033. nil)))
  19034. (defun org-image-file-name-regexp (&optional extensions)
  19035. "Return regexp matching the file names of images.
  19036. If EXTENSIONS is given, only match these."
  19037. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19038. (image-file-name-regexp)
  19039. (let ((image-file-name-extensions
  19040. (or extensions
  19041. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19042. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19043. (concat "\\."
  19044. (regexp-opt (nconc (mapcar 'upcase
  19045. image-file-name-extensions)
  19046. image-file-name-extensions)
  19047. t)
  19048. "\\'"))))
  19049. (defun org-file-image-p (file &optional extensions)
  19050. "Return non-nil if FILE is an image."
  19051. (save-match-data
  19052. (string-match (org-image-file-name-regexp extensions) file)))
  19053. (defun org-get-cursor-date ()
  19054. "Return the date at cursor in as a time.
  19055. This works in the calendar and in the agenda, anywhere else it just
  19056. returns the current time."
  19057. (let (date day defd)
  19058. (cond
  19059. ((eq major-mode 'calendar-mode)
  19060. (setq date (calendar-cursor-to-date)
  19061. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19062. ((eq major-mode 'org-agenda-mode)
  19063. (setq day (get-text-property (point) 'day))
  19064. (if day
  19065. (setq date (calendar-gregorian-from-absolute day)
  19066. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  19067. (nth 2 date))))))
  19068. (or defd (current-time))))
  19069. (defun org-mark-subtree (&optional up)
  19070. "Mark the current subtree.
  19071. This puts point at the start of the current subtree, and mark at
  19072. the end. If a numeric prefix UP is given, move up into the
  19073. hierarchy of headlines by UP levels before marking the subtree."
  19074. (interactive "P")
  19075. (org-with-limited-levels
  19076. (cond ((org-at-heading-p) (beginning-of-line))
  19077. ((org-before-first-heading-p) (error "Not in a subtree"))
  19078. (t (outline-previous-visible-heading 1))))
  19079. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19080. (if (org-called-interactively-p 'any)
  19081. (call-interactively 'org-mark-element)
  19082. (org-mark-element)))
  19083. ;;; Indentation
  19084. (defun org-indent-line ()
  19085. "Indent line depending on context."
  19086. (interactive)
  19087. (let* ((pos (point))
  19088. (itemp (org-at-item-p))
  19089. (case-fold-search t)
  19090. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19091. (inline-task-p (and (featurep 'org-inlinetask)
  19092. (org-inlinetask-in-task-p)))
  19093. (inline-re (and inline-task-p
  19094. (org-inlinetask-outline-regexp)))
  19095. column)
  19096. (if (and orgstruct-is-++ (eq pos (point)))
  19097. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19098. (indent-according-to-mode))
  19099. (beginning-of-line 1)
  19100. (cond
  19101. ;; Headings
  19102. ((looking-at org-outline-regexp) (setq column 0))
  19103. ;; Included files
  19104. ((looking-at "#\\+include:") (setq column 0))
  19105. ;; Footnote definition
  19106. ((looking-at org-footnote-definition-re) (setq column 0))
  19107. ;; Literal examples
  19108. ((looking-at "[ \t]*:\\( \\|$\\)")
  19109. (setq column (org-get-indentation))) ; do nothing
  19110. ;; Lists
  19111. ((ignore-errors (goto-char (org-in-item-p)))
  19112. (setq column (if itemp
  19113. (org-get-indentation)
  19114. (org-list-item-body-column (point))))
  19115. (goto-char pos))
  19116. ;; Drawers
  19117. ((and (looking-at "[ \t]*:END:")
  19118. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19119. (save-excursion
  19120. (goto-char (1- (match-beginning 1)))
  19121. (setq column (current-column))))
  19122. ;; Special blocks
  19123. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19124. (save-excursion
  19125. (re-search-backward
  19126. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19127. (setq column (org-get-indentation (match-string 0))))
  19128. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19129. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19130. (save-excursion
  19131. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19132. (setq column
  19133. (cond ((equal (downcase (match-string 1)) "src")
  19134. ;; src blocks: let `org-edit-src-exit' handle them
  19135. (org-get-indentation))
  19136. ((equal (downcase (match-string 1)) "example")
  19137. (max (org-get-indentation)
  19138. (org-get-indentation (match-string 0))))
  19139. (t
  19140. (org-get-indentation (match-string 0))))))
  19141. ;; This line has nothing special, look at the previous relevant
  19142. ;; line to compute indentation
  19143. (t
  19144. (beginning-of-line 0)
  19145. (while (and (not (bobp))
  19146. (not (looking-at org-table-line-regexp))
  19147. (not (looking-at org-drawer-regexp))
  19148. ;; When point started in an inline task, do not move
  19149. ;; above task starting line.
  19150. (not (and inline-task-p (looking-at inline-re)))
  19151. ;; Skip drawers, blocks, empty lines, verbatim,
  19152. ;; comments, tables, footnotes definitions, lists,
  19153. ;; inline tasks.
  19154. (or (and (looking-at "[ \t]*:END:")
  19155. (re-search-backward org-drawer-regexp nil t))
  19156. (and (looking-at "[ \t]*#\\+end_")
  19157. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19158. (looking-at "[ \t]*[\n:#|]")
  19159. (looking-at org-footnote-definition-re)
  19160. (and (ignore-errors (goto-char (org-in-item-p)))
  19161. (goto-char
  19162. (org-list-get-top-point (org-list-struct))))
  19163. (and (not inline-task-p)
  19164. (featurep 'org-inlinetask)
  19165. (org-inlinetask-in-task-p)
  19166. (or (org-inlinetask-goto-beginning) t))))
  19167. (beginning-of-line 0))
  19168. (cond
  19169. ;; There was an heading above.
  19170. ((looking-at "\\*+[ \t]+")
  19171. (if (not org-adapt-indentation)
  19172. (setq column 0)
  19173. (goto-char (match-end 0))
  19174. (setq column (current-column))))
  19175. ;; A drawer had started and is unfinished
  19176. ((looking-at org-drawer-regexp)
  19177. (goto-char (1- (match-beginning 1)))
  19178. (setq column (current-column)))
  19179. ;; Else, nothing noticeable found: get indentation and go on.
  19180. (t (setq column (org-get-indentation))))))
  19181. ;; Now apply indentation and move cursor accordingly
  19182. (goto-char pos)
  19183. (if (<= (current-column) (current-indentation))
  19184. (org-indent-line-to column)
  19185. (save-excursion (org-indent-line-to column)))
  19186. ;; Special polishing for properties, see `org-property-format'
  19187. (setq column (current-column))
  19188. (beginning-of-line 1)
  19189. (if (looking-at
  19190. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19191. (replace-match (concat (match-string 1)
  19192. (format org-property-format
  19193. (match-string 2) (match-string 3)))
  19194. t t))
  19195. (org-move-to-column column))))
  19196. (defun org-indent-drawer ()
  19197. "Indent the drawer at point."
  19198. (interactive)
  19199. (let ((p (point))
  19200. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19201. (match-end 0)))
  19202. (folded
  19203. (save-excursion
  19204. (end-of-line)
  19205. (when (overlays-at (point))
  19206. (member 'invisible (overlay-properties
  19207. (car (overlays-at (point)))))))))
  19208. (when folded (org-cycle))
  19209. (indent-for-tab-command)
  19210. (while (and (move-beginning-of-line 2) (< (point) e))
  19211. (indent-for-tab-command))
  19212. (goto-char p)
  19213. (when folded (org-cycle)))
  19214. (message "Drawer at point indented"))
  19215. (defun org-indent-block ()
  19216. "Indent the block at point."
  19217. (interactive)
  19218. (let ((p (point))
  19219. (case-fold-search t)
  19220. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19221. (match-end 0)))
  19222. (folded
  19223. (save-excursion
  19224. (end-of-line)
  19225. (when (overlays-at (point))
  19226. (member 'invisible (overlay-properties
  19227. (car (overlays-at (point)))))))))
  19228. (when folded (org-cycle))
  19229. (indent-for-tab-command)
  19230. (while (and (move-beginning-of-line 2) (< (point) e))
  19231. (indent-for-tab-command))
  19232. (goto-char p)
  19233. (when folded (org-cycle)))
  19234. (message "Block at point indented"))
  19235. (defun org-indent-region (start end)
  19236. "Indent region."
  19237. (interactive "r")
  19238. (save-excursion
  19239. (let ((line-end (org-current-line end)))
  19240. (goto-char start)
  19241. (while (< (org-current-line) line-end)
  19242. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19243. (t (call-interactively 'org-indent-line)))
  19244. (move-beginning-of-line 2)))))
  19245. ;;; Filling
  19246. ;; We use our own fill-paragraph and auto-fill functions.
  19247. ;; `org-fill-paragraph' relies on adaptive filling and context
  19248. ;; checking. Appropriate `fill-prefix' is computed with
  19249. ;; `org-adaptive-fill-function'.
  19250. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19251. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19252. ;; `org-adaptive-fill-function' value. Internally,
  19253. ;; `org-comment-line-break-function' breaks the line.
  19254. ;; `org-setup-filling' installs filling and auto-filling related
  19255. ;; variables during `org-mode' initialization.
  19256. (defun org-setup-filling ()
  19257. (interactive)
  19258. ;; Prevent auto-fill from inserting unwanted new items.
  19259. (when (boundp 'fill-nobreak-predicate)
  19260. (org-set-local
  19261. 'fill-nobreak-predicate
  19262. (org-uniquify
  19263. (append fill-nobreak-predicate
  19264. '(org-fill-paragraph-separate-nobreak-p
  19265. org-fill-line-break-nobreak-p)))))
  19266. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19267. (org-set-local 'auto-fill-inhibit-regexp nil)
  19268. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19269. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19270. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19271. (defvar org-element-paragraph-separate) ; org-element.el
  19272. (defun org-fill-paragraph-separate-nobreak-p ()
  19273. "Non-nil when a line break at point would insert a new item."
  19274. (looking-at (substring org-element-paragraph-separate 1)))
  19275. (defun org-fill-line-break-nobreak-p ()
  19276. "Non-nil when a line break at point would create an Org line break."
  19277. (save-excursion
  19278. (skip-chars-backward "[ \t]")
  19279. (skip-chars-backward "\\\\")
  19280. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19281. (declare-function message-in-body-p "message" ())
  19282. (defvar org-element--affiliated-re) ; From org-element.el
  19283. (defun org-adaptive-fill-function ()
  19284. "Compute a fill prefix for the current line.
  19285. Return fill prefix, as a string, or nil if current line isn't
  19286. meant to be filled."
  19287. (let (prefix)
  19288. (when (and (derived-mode-p 'message-mode) (message-in-body-p))
  19289. (save-excursion
  19290. (beginning-of-line)
  19291. (cond ((looking-at message-cite-prefix-regexp)
  19292. (setq prefix (match-string-no-properties 0)))
  19293. ((looking-at org-outline-regexp)
  19294. (setq prefix "")))))
  19295. (or prefix
  19296. (org-with-wide-buffer
  19297. (let* ((p (line-beginning-position))
  19298. (element (save-excursion (beginning-of-line) (org-element-at-point)))
  19299. (type (org-element-type element))
  19300. (post-affiliated
  19301. (save-excursion
  19302. (goto-char (org-element-property :begin element))
  19303. (while (looking-at org-element--affiliated-re) (forward-line))
  19304. (point))))
  19305. (unless (< p post-affiliated)
  19306. (case type
  19307. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19308. (footnote-definition "")
  19309. ((item plain-list)
  19310. (make-string (org-list-item-body-column post-affiliated) ? ))
  19311. (paragraph
  19312. ;; Fill prefix is usually the same as the current line,
  19313. ;; except if the paragraph is at the beginning of an item.
  19314. (let ((parent (org-element-property :parent element)))
  19315. (cond ((eq (org-element-type parent) 'item)
  19316. (make-string (org-list-item-body-column
  19317. (org-element-property :begin parent))
  19318. ? ))
  19319. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19320. (match-string 0))
  19321. (t ""))))
  19322. (comment-block
  19323. ;; Only fill contents if P is within block boundaries.
  19324. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19325. (forward-line)
  19326. (point)))
  19327. (cend (save-excursion
  19328. (goto-char (org-element-property :end element))
  19329. (skip-chars-backward " \r\t\n")
  19330. (line-beginning-position))))
  19331. (when (and (>= p cbeg) (< p cend))
  19332. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19333. (match-string 0)
  19334. "")))))))))))
  19335. (declare-function message-goto-body "message" ())
  19336. (defvar message-cite-prefix-regexp) ; From message.el
  19337. (defvar org-element-all-objects) ; From org-element.el
  19338. (defun org-fill-paragraph (&optional justify)
  19339. "Fill element at point, when applicable.
  19340. This function only applies to comment blocks, comments, example
  19341. blocks and paragraphs. Also, as a special case, re-align table
  19342. when point is at one.
  19343. If JUSTIFY is non-nil (interactively, with prefix argument),
  19344. justify as well. If `sentence-end-double-space' is non-nil, then
  19345. period followed by one space does not end a sentence, so don't
  19346. break a line there. The variable `fill-column' controls the
  19347. width for filling.
  19348. For convenience, when point is at a plain list, an item or
  19349. a footnote definition, try to fill the first paragraph within."
  19350. (interactive)
  19351. (if (and (derived-mode-p 'message-mode)
  19352. (or (not (message-in-body-p))
  19353. (save-excursion (move-beginning-of-line 1)
  19354. (looking-at message-cite-prefix-regexp))))
  19355. ;; First ensure filling is correct in message-mode.
  19356. (let ((fill-paragraph-function
  19357. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  19358. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  19359. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  19360. (paragraph-separate
  19361. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  19362. (fill-paragraph nil))
  19363. (save-excursion
  19364. ;; Move to end of line in order to get the first paragraph
  19365. ;; within a plain list or a footnote definition.
  19366. (end-of-line)
  19367. (let ((element (org-element-at-point)))
  19368. ;; First check if point is in a blank line at the beginning of
  19369. ;; the buffer. In that case, ignore filling.
  19370. (if (< (point) (org-element-property :begin element)) t
  19371. (case (org-element-type element)
  19372. ;; Use major mode filling function is src blocks.
  19373. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  19374. ;; Align Org tables, leave table.el tables as-is.
  19375. (table-row (org-table-align) t)
  19376. (table
  19377. (when (eq (org-element-property :type element) 'org)
  19378. (org-table-align))
  19379. t)
  19380. (paragraph
  19381. ;; Paragraphs may contain `line-break' type objects.
  19382. (let ((beg (max (point-min)
  19383. (org-element-property :contents-begin element)))
  19384. (end (min (point-max)
  19385. (org-element-property :contents-end element))))
  19386. ;; Do nothing if point is at an affiliated keyword.
  19387. (if (< (point) beg) t
  19388. (when (derived-mode-p 'message-mode)
  19389. ;; In `message-mode', do not fill following
  19390. ;; citation in current paragraph nor text before
  19391. ;; message body.
  19392. (let ((body-start (save-excursion (message-goto-body))))
  19393. (when body-start (setq beg (max body-start beg))))
  19394. (when (save-excursion
  19395. (re-search-forward
  19396. (concat "^" message-cite-prefix-regexp) end t))
  19397. (setq end (match-beginning 0))))
  19398. ;; Fill paragraph, taking line breaks into
  19399. ;; consideration. For that, slice the paragraph
  19400. ;; using line breaks as separators, and fill the
  19401. ;; parts in reverse order to avoid messing with
  19402. ;; markers.
  19403. (save-excursion
  19404. (goto-char end)
  19405. (mapc
  19406. (lambda (pos)
  19407. (fill-region-as-paragraph pos (point) justify)
  19408. (goto-char pos))
  19409. ;; Find the list of ending positions for line
  19410. ;; breaks in the current paragraph. Add paragraph
  19411. ;; beginning to include first slice.
  19412. (nreverse
  19413. (cons
  19414. beg
  19415. (org-element-map
  19416. (org-element--parse-objects
  19417. beg end nil org-element-all-objects)
  19418. 'line-break
  19419. (lambda (lb) (org-element-property :end lb)))))))
  19420. t)))
  19421. ;; Contents of `comment-block' type elements should be
  19422. ;; filled as plain text, but only if point is within block
  19423. ;; markers.
  19424. (comment-block
  19425. (let* ((case-fold-search t)
  19426. (beg (save-excursion
  19427. (goto-char (org-element-property :begin element))
  19428. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  19429. (forward-line)
  19430. (point)))
  19431. (end (save-excursion
  19432. (goto-char (org-element-property :end element))
  19433. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  19434. (line-beginning-position))))
  19435. (when (and (>= (point) beg) (< (point) end))
  19436. (fill-region-as-paragraph
  19437. (save-excursion
  19438. (end-of-line)
  19439. (re-search-backward "^[ \t]*$" beg 'move)
  19440. (line-beginning-position))
  19441. (save-excursion
  19442. (beginning-of-line)
  19443. (re-search-forward "^[ \t]*$" end 'move)
  19444. (line-beginning-position))
  19445. justify)))
  19446. t)
  19447. ;; Fill comments.
  19448. (comment (fill-comment-paragraph justify))
  19449. ;; Ignore every other element.
  19450. (otherwise t)))))))
  19451. (defun org-auto-fill-function ()
  19452. "Auto-fill function."
  19453. ;; Check if auto-filling is meaningful.
  19454. (let ((fc (current-fill-column)))
  19455. (when (and fc (> (current-column) fc))
  19456. (let* ((fill-prefix (org-adaptive-fill-function))
  19457. ;; Enforce empty fill prefix, if required. Otherwise, it
  19458. ;; will be computed again.
  19459. (adaptive-fill-mode (not (equal fill-prefix ""))))
  19460. (when fill-prefix (do-auto-fill))))))
  19461. (defun org-comment-line-break-function (&optional soft)
  19462. "Break line at point and indent, continuing comment if within one.
  19463. The inserted newline is marked hard if variable
  19464. `use-hard-newlines' is true, unless optional argument SOFT is
  19465. non-nil."
  19466. (if soft (insert-and-inherit ?\n) (newline 1))
  19467. (save-excursion (forward-char -1) (delete-horizontal-space))
  19468. (delete-horizontal-space)
  19469. (indent-to-left-margin)
  19470. (insert-before-markers-and-inherit fill-prefix))
  19471. ;;; Comments
  19472. ;; Org comments syntax is quite complex. It requires the entire line
  19473. ;; to be just a comment. Also, even with the right syntax at the
  19474. ;; beginning of line, some some elements (i.e. verse-block or
  19475. ;; example-block) don't accept comments. Usual Emacs comment commands
  19476. ;; cannot cope with those requirements. Therefore, Org replaces them.
  19477. ;; Org still relies on `comment-dwim', but cannot trust
  19478. ;; `comment-only-p'. So, `comment-region-function' and
  19479. ;; `uncomment-region-function' both point
  19480. ;; to`org-comment-or-uncomment-region'. Eventually,
  19481. ;; `org-insert-comment' takes care of insertion of comments at the
  19482. ;; beginning of line.
  19483. ;; `org-setup-comments-handling' install comments related variables
  19484. ;; during `org-mode' initialization.
  19485. (defun org-setup-comments-handling ()
  19486. (interactive)
  19487. (org-set-local 'comment-use-syntax nil)
  19488. (org-set-local 'comment-start "# ")
  19489. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  19490. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  19491. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  19492. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  19493. (defun org-insert-comment ()
  19494. "Insert an empty comment above current line.
  19495. If the line is empty, insert comment at its beginning."
  19496. (beginning-of-line)
  19497. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  19498. (org-indent-line)
  19499. (insert "# "))
  19500. (defvar comment-empty-lines) ; From newcomment.el.
  19501. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  19502. "Comment or uncomment each non-blank line in the region.
  19503. Uncomment each non-blank line between BEG and END if it only
  19504. contains commented lines. Otherwise, comment them."
  19505. (save-restriction
  19506. ;; Restrict region
  19507. (narrow-to-region (save-excursion (goto-char beg)
  19508. (skip-chars-forward " \r\t\n" end)
  19509. (line-beginning-position))
  19510. (save-excursion (goto-char end)
  19511. (skip-chars-backward " \r\t\n" beg)
  19512. (line-end-position)))
  19513. (let ((uncommentp
  19514. ;; UNCOMMENTP is non-nil when every non blank line between
  19515. ;; BEG and END is a comment.
  19516. (save-excursion
  19517. (goto-char (point-min))
  19518. (while (and (not (eobp))
  19519. (let ((element (org-element-at-point)))
  19520. (and (eq (org-element-type element) 'comment)
  19521. (goto-char (min (point-max)
  19522. (org-element-property
  19523. :end element)))))))
  19524. (eobp))))
  19525. (if uncommentp
  19526. ;; Only blank lines and comments in region: uncomment it.
  19527. (save-excursion
  19528. (goto-char (point-min))
  19529. (while (not (eobp))
  19530. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  19531. (replace-match "" nil nil nil 1))
  19532. (forward-line)))
  19533. ;; Comment each line in region.
  19534. (let ((min-indent (point-max)))
  19535. ;; First find the minimum indentation across all lines.
  19536. (save-excursion
  19537. (goto-char (point-min))
  19538. (while (and (not (eobp)) (not (zerop min-indent)))
  19539. (unless (looking-at "[ \t]*$")
  19540. (setq min-indent (min min-indent (current-indentation))))
  19541. (forward-line)))
  19542. ;; Then loop over all lines.
  19543. (save-excursion
  19544. (goto-char (point-min))
  19545. (while (not (eobp))
  19546. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  19547. (org-move-to-column min-indent t)
  19548. (insert comment-start))
  19549. (forward-line))))))))
  19550. ;;; Other stuff.
  19551. (defun org-toggle-fixed-width-section (arg)
  19552. "Toggle the fixed-width export.
  19553. If there is no active region, the QUOTE keyword at the current headline is
  19554. inserted or removed. When present, it causes the text between this headline
  19555. and the next to be exported as fixed-width text, and unmodified.
  19556. If there is an active region, this command adds or removes a colon as the
  19557. first character of this line. If the first character of a line is a colon,
  19558. this line is also exported in fixed-width font."
  19559. (interactive "P")
  19560. (let* ((cc 0)
  19561. (regionp (org-region-active-p))
  19562. (beg (if regionp (region-beginning) (point)))
  19563. (end (if regionp (region-end)))
  19564. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  19565. (case-fold-search nil)
  19566. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  19567. off)
  19568. (if regionp
  19569. (save-excursion
  19570. (goto-char beg)
  19571. (setq cc (current-column))
  19572. (beginning-of-line 1)
  19573. (setq off (looking-at re))
  19574. (while (> nlines 0)
  19575. (setq nlines (1- nlines))
  19576. (beginning-of-line 1)
  19577. (cond
  19578. (arg
  19579. (org-move-to-column cc t)
  19580. (insert ": \n")
  19581. (forward-line -1))
  19582. ((and off (looking-at re))
  19583. (replace-match "" t t nil 1))
  19584. ((not off) (org-move-to-column cc t) (insert ": ")))
  19585. (forward-line 1)))
  19586. (save-excursion
  19587. (org-back-to-heading)
  19588. (cond
  19589. ((looking-at (format org-heading-keyword-regexp-format
  19590. org-quote-string))
  19591. (goto-char (match-end 1))
  19592. (looking-at (concat " +" org-quote-string))
  19593. (replace-match "" t t)
  19594. (when (eolp) (insert " ")))
  19595. ((looking-at org-outline-regexp)
  19596. (goto-char (match-end 0))
  19597. (insert org-quote-string " ")))))))
  19598. (defun org-reftex-citation ()
  19599. "Use reftex-citation to insert a citation into the buffer.
  19600. This looks for a line like
  19601. #+BIBLIOGRAPHY: foo plain option:-d
  19602. and derives from it that foo.bib is the bibliography file relevant
  19603. for this document. It then installs the necessary environment for RefTeX
  19604. to work in this buffer and calls `reftex-citation' to insert a citation
  19605. into the buffer.
  19606. Export of such citations to both LaTeX and HTML is handled by the contributed
  19607. package org-exp-bibtex by Taru Karttunen."
  19608. (interactive)
  19609. (let ((reftex-docstruct-symbol 'rds)
  19610. (reftex-cite-format "\\cite{%l}")
  19611. rds bib)
  19612. (save-excursion
  19613. (save-restriction
  19614. (widen)
  19615. (let ((case-fold-search t)
  19616. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  19617. (if (not (save-excursion
  19618. (or (re-search-forward re nil t)
  19619. (re-search-backward re nil t))))
  19620. (error "No bibliography defined in file")
  19621. (setq bib (concat (match-string 1) ".bib")
  19622. rds (list (list 'bib bib)))))))
  19623. (call-interactively 'reftex-citation)))
  19624. ;;;; Functions extending outline functionality
  19625. (defun org-beginning-of-line (&optional arg)
  19626. "Go to the beginning of the current line. If that is invisible, continue
  19627. to a visible line beginning. This makes the function of C-a more intuitive.
  19628. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19629. first attempt, and only move to after the tags when the cursor is already
  19630. beyond the end of the headline."
  19631. (interactive "P")
  19632. (let ((pos (point))
  19633. (special (if (consp org-special-ctrl-a/e)
  19634. (car org-special-ctrl-a/e)
  19635. org-special-ctrl-a/e))
  19636. refpos)
  19637. (if (org-bound-and-true-p visual-line-mode)
  19638. (beginning-of-visual-line 1)
  19639. (beginning-of-line 1))
  19640. (if (and arg (fboundp 'move-beginning-of-line))
  19641. (call-interactively 'move-beginning-of-line)
  19642. (if (bobp)
  19643. nil
  19644. (backward-char 1)
  19645. (if (org-truely-invisible-p)
  19646. (while (and (not (bobp)) (org-truely-invisible-p))
  19647. (backward-char 1)
  19648. (beginning-of-line 1))
  19649. (forward-char 1))))
  19650. (when special
  19651. (cond
  19652. ((and (looking-at org-complex-heading-regexp)
  19653. (= (char-after (match-end 1)) ?\ ))
  19654. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  19655. (point-at-eol)))
  19656. (goto-char
  19657. (if (eq special t)
  19658. (cond ((> pos refpos) refpos)
  19659. ((= pos (point)) refpos)
  19660. (t (point)))
  19661. (cond ((> pos (point)) (point))
  19662. ((not (eq last-command this-command)) (point))
  19663. (t refpos)))))
  19664. ((org-at-item-p)
  19665. ;; Being at an item and not looking at an the item means point
  19666. ;; was previously moved to beginning of a visual line, which
  19667. ;; doesn't contain the item. Therefore, do nothing special,
  19668. ;; just stay here.
  19669. (when (looking-at org-list-full-item-re)
  19670. ;; Set special position at first white space character after
  19671. ;; bullet, and check-box, if any.
  19672. (let ((after-bullet
  19673. (let ((box (match-end 3)))
  19674. (if (not box) (match-end 1)
  19675. (let ((after (char-after box)))
  19676. (if (and after (= after ? )) (1+ box) box))))))
  19677. ;; Special case: Move point to special position when
  19678. ;; currently after it or at beginning of line.
  19679. (if (eq special t)
  19680. (when (or (> pos after-bullet) (= (point) pos))
  19681. (goto-char after-bullet))
  19682. ;; Reversed case: Move point to special position when
  19683. ;; point was already at beginning of line and command is
  19684. ;; repeated.
  19685. (when (and (= (point) pos) (eq last-command this-command))
  19686. (goto-char after-bullet))))))))
  19687. (org-no-warnings
  19688. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19689. (defun org-end-of-line (&optional arg)
  19690. "Go to the end of the line.
  19691. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  19692. tags on the first attempt, and only move to after the tags when
  19693. the cursor is already beyond the end of the headline."
  19694. (interactive "P")
  19695. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  19696. org-special-ctrl-a/e))
  19697. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  19698. 'end-of-visual-line)
  19699. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19700. (t 'end-of-line))))
  19701. (if (or (not special) arg) (call-interactively move-fun)
  19702. (let* ((element (save-excursion (beginning-of-line)
  19703. (org-element-at-point)))
  19704. (type (org-element-type element)))
  19705. (cond
  19706. ((memq type '(headline inlinetask))
  19707. (let ((pos (point)))
  19708. (beginning-of-line 1)
  19709. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  19710. (if (eq special t)
  19711. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  19712. (goto-char (match-beginning 1))
  19713. (goto-char (match-end 0)))
  19714. (if (or (< pos (match-end 0))
  19715. (not (eq this-command last-command)))
  19716. (goto-char (match-end 0))
  19717. (goto-char (match-beginning 1))))
  19718. (call-interactively move-fun))))
  19719. ((org-element-property :hiddenp element)
  19720. ;; If element is hidden, `move-end-of-line' would put point
  19721. ;; after it. Use `end-of-line' to stay on current line.
  19722. (call-interactively 'end-of-line))
  19723. (t (call-interactively move-fun)))))
  19724. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19725. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  19726. (define-key org-mode-map "\C-e" 'org-end-of-line)
  19727. (defun org-backward-sentence (&optional arg)
  19728. "Go to beginning of sentence, or beginning of table field.
  19729. This will call `backward-sentence' or `org-table-beginning-of-field',
  19730. depending on context."
  19731. (interactive "P")
  19732. (cond
  19733. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19734. (t (call-interactively 'backward-sentence))))
  19735. (defun org-forward-sentence (&optional arg)
  19736. "Go to end of sentence, or end of table field.
  19737. This will call `forward-sentence' or `org-table-end-of-field',
  19738. depending on context."
  19739. (interactive "P")
  19740. (cond
  19741. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19742. (t (call-interactively 'forward-sentence))))
  19743. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19744. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19745. (defun org-kill-line (&optional arg)
  19746. "Kill line, to tags or end of line."
  19747. (interactive "P")
  19748. (cond
  19749. ((or (not org-special-ctrl-k)
  19750. (bolp)
  19751. (not (org-at-heading-p)))
  19752. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19753. org-ctrl-k-protect-subtree)
  19754. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19755. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19756. (error "C-k aborted - would kill hidden subtree")))
  19757. (call-interactively
  19758. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  19759. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19760. (kill-region (point) (match-beginning 1))
  19761. (org-set-tags nil t))
  19762. (t (kill-region (point) (point-at-eol)))))
  19763. (define-key org-mode-map "\C-k" 'org-kill-line)
  19764. (defun org-yank (&optional arg)
  19765. "Yank. If the kill is a subtree, treat it specially.
  19766. This command will look at the current kill and check if is a single
  19767. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19768. cursor is at the beginning of a line or after the stars of a currently
  19769. empty headline, then the yank is handled specially. How exactly depends
  19770. on the value of the following variables, both set by default.
  19771. org-yank-folded-subtrees
  19772. When set, the subtree(s) will be folded after insertion, but only
  19773. if doing so would now swallow text after the yanked text.
  19774. org-yank-adjusted-subtrees
  19775. When set, the subtree will be promoted or demoted in order to
  19776. fit into the local outline tree structure, which means that the level
  19777. will be adjusted so that it becomes the smaller one of the two
  19778. *visible* surrounding headings.
  19779. Any prefix to this command will cause `yank' to be called directly with
  19780. no special treatment. In particular, a simple \\[universal-argument] prefix \
  19781. will just
  19782. plainly yank the text as it is.
  19783. \[1] The test checks if the first non-white line is a heading
  19784. and if there are no other headings with fewer stars."
  19785. (interactive "P")
  19786. (org-yank-generic 'yank arg))
  19787. (defun org-yank-generic (command arg)
  19788. "Perform some yank-like command.
  19789. This function implements the behavior described in the `org-yank'
  19790. documentation. However, it has been generalized to work for any
  19791. interactive command with similar behavior."
  19792. ;; pretend to be command COMMAND
  19793. (setq this-command command)
  19794. (if arg
  19795. (call-interactively command)
  19796. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  19797. (and (org-kill-is-subtree-p)
  19798. (or (bolp)
  19799. (and (looking-at "[ \t]*$")
  19800. (string-match
  19801. "\\`\\*+\\'"
  19802. (buffer-substring (point-at-bol) (point)))))))
  19803. swallowp)
  19804. (cond
  19805. ((and subtreep org-yank-folded-subtrees)
  19806. (let ((beg (point))
  19807. end)
  19808. (if (and subtreep org-yank-adjusted-subtrees)
  19809. (org-paste-subtree nil nil 'for-yank)
  19810. (call-interactively command))
  19811. (setq end (point))
  19812. (goto-char beg)
  19813. (when (and (bolp) subtreep
  19814. (not (setq swallowp
  19815. (org-yank-folding-would-swallow-text beg end))))
  19816. (org-with-limited-levels
  19817. (or (looking-at org-outline-regexp)
  19818. (re-search-forward org-outline-regexp-bol end t))
  19819. (while (and (< (point) end) (looking-at org-outline-regexp))
  19820. (hide-subtree)
  19821. (org-cycle-show-empty-lines 'folded)
  19822. (condition-case nil
  19823. (outline-forward-same-level 1)
  19824. (error (goto-char end))))))
  19825. (when swallowp
  19826. (message
  19827. "Inserted text not folded because that would swallow text"))
  19828. (goto-char end)
  19829. (skip-chars-forward " \t\n\r")
  19830. (beginning-of-line 1)
  19831. (push-mark beg 'nomsg)))
  19832. ((and subtreep org-yank-adjusted-subtrees)
  19833. (let ((beg (point-at-bol)))
  19834. (org-paste-subtree nil nil 'for-yank)
  19835. (push-mark beg 'nomsg)))
  19836. (t
  19837. (call-interactively command))))))
  19838. (defun org-yank-folding-would-swallow-text (beg end)
  19839. "Would hide-subtree at BEG swallow any text after END?"
  19840. (let (level)
  19841. (org-with-limited-levels
  19842. (save-excursion
  19843. (goto-char beg)
  19844. (when (or (looking-at org-outline-regexp)
  19845. (re-search-forward org-outline-regexp-bol end t))
  19846. (setq level (org-outline-level)))
  19847. (goto-char end)
  19848. (skip-chars-forward " \t\r\n\v\f")
  19849. (if (or (eobp)
  19850. (and (bolp) (looking-at org-outline-regexp)
  19851. (<= (org-outline-level) level)))
  19852. nil ; Nothing would be swallowed
  19853. t))))) ; something would swallow
  19854. (define-key org-mode-map "\C-y" 'org-yank)
  19855. (defun org-truely-invisible-p ()
  19856. "Check if point is at a character currently not visible.
  19857. This version does not only check the character property, but also
  19858. `visible-mode'."
  19859. ;; Early versions of noutline don't have `outline-invisible-p'.
  19860. (if (org-bound-and-true-p visible-mode)
  19861. nil
  19862. (outline-invisible-p)))
  19863. (defun org-invisible-p2 ()
  19864. "Check if point is at a character currently not visible."
  19865. (save-excursion
  19866. (if (and (eolp) (not (bobp))) (backward-char 1))
  19867. ;; Early versions of noutline don't have `outline-invisible-p'.
  19868. (outline-invisible-p)))
  19869. (defun org-back-to-heading (&optional invisible-ok)
  19870. "Call `outline-back-to-heading', but provide a better error message."
  19871. (condition-case nil
  19872. (outline-back-to-heading invisible-ok)
  19873. (error (error "Before first headline at position %d in buffer %s"
  19874. (point) (current-buffer)))))
  19875. (defun org-before-first-heading-p ()
  19876. "Before first heading?"
  19877. (save-excursion
  19878. (end-of-line)
  19879. (null (re-search-backward org-outline-regexp-bol nil t))))
  19880. (defun org-at-heading-p (&optional ignored)
  19881. (outline-on-heading-p t))
  19882. ;; Compatibility alias with Org versions < 7.8.03
  19883. (defalias 'org-on-heading-p 'org-at-heading-p)
  19884. (defun org-at-comment-p nil
  19885. "Is cursor in a line starting with a # character?"
  19886. (save-excursion
  19887. (beginning-of-line)
  19888. (looking-at "^#")))
  19889. (defun org-at-drawer-p nil
  19890. "Is cursor at a drawer keyword?"
  19891. (save-excursion
  19892. (move-beginning-of-line 1)
  19893. (looking-at org-drawer-regexp)))
  19894. (defun org-at-block-p nil
  19895. "Is cursor at a block keyword?"
  19896. (save-excursion
  19897. (move-beginning-of-line 1)
  19898. (looking-at org-block-regexp)))
  19899. (defun org-point-at-end-of-empty-headline ()
  19900. "If point is at the end of an empty headline, return t, else nil.
  19901. If the heading only contains a TODO keyword, it is still still considered
  19902. empty."
  19903. (and (looking-at "[ \t]*$")
  19904. (when org-todo-line-regexp
  19905. (save-excursion
  19906. (beginning-of-line 1)
  19907. (let ((case-fold-search nil))
  19908. (looking-at org-todo-line-regexp)
  19909. (string= (match-string 3) ""))))))
  19910. (defun org-at-heading-or-item-p ()
  19911. (or (org-at-heading-p) (org-at-item-p)))
  19912. (defun org-at-target-p ()
  19913. (or (org-in-regexp org-radio-target-regexp)
  19914. (org-in-regexp org-target-regexp)))
  19915. ;; Compatibility alias with Org versions < 7.8.03
  19916. (defalias 'org-on-target-p 'org-at-target-p)
  19917. (defun org-up-heading-all (arg)
  19918. "Move to the heading line of which the present line is a subheading.
  19919. This function considers both visible and invisible heading lines.
  19920. With argument, move up ARG levels."
  19921. (if (fboundp 'outline-up-heading-all)
  19922. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  19923. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  19924. (defun org-up-heading-safe ()
  19925. "Move to the heading line of which the present line is a subheading.
  19926. This version will not throw an error. It will return the level of the
  19927. headline found, or nil if no higher level is found.
  19928. Also, this function will be a lot faster than `outline-up-heading',
  19929. because it relies on stars being the outline starters. This can really
  19930. make a significant difference in outlines with very many siblings."
  19931. (let (start-level re)
  19932. (org-back-to-heading t)
  19933. (setq start-level (funcall outline-level))
  19934. (if (equal start-level 1)
  19935. nil
  19936. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19937. (if (re-search-backward re nil t)
  19938. (funcall outline-level)))))
  19939. (defun org-first-sibling-p ()
  19940. "Is this heading the first child of its parents?"
  19941. (interactive)
  19942. (let ((re org-outline-regexp-bol)
  19943. level l)
  19944. (unless (org-at-heading-p t)
  19945. (error "Not at a heading"))
  19946. (setq level (funcall outline-level))
  19947. (save-excursion
  19948. (if (not (re-search-backward re nil t))
  19949. t
  19950. (setq l (funcall outline-level))
  19951. (< l level)))))
  19952. (defun org-goto-sibling (&optional previous)
  19953. "Goto the next sibling, even if it is invisible.
  19954. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19955. when a sibling was found. When none is found, return nil and don't
  19956. move point."
  19957. (let ((fun (if previous 're-search-backward 're-search-forward))
  19958. (pos (point))
  19959. (re org-outline-regexp-bol)
  19960. level l)
  19961. (when (condition-case nil (org-back-to-heading t) (error nil))
  19962. (setq level (funcall outline-level))
  19963. (catch 'exit
  19964. (or previous (forward-char 1))
  19965. (while (funcall fun re nil t)
  19966. (setq l (funcall outline-level))
  19967. (when (< l level) (goto-char pos) (throw 'exit nil))
  19968. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  19969. (goto-char pos)
  19970. nil))))
  19971. (defun org-show-siblings ()
  19972. "Show all siblings of the current headline."
  19973. (save-excursion
  19974. (while (org-goto-sibling) (org-flag-heading nil)))
  19975. (save-excursion
  19976. (while (org-goto-sibling 'previous)
  19977. (org-flag-heading nil))))
  19978. (defun org-goto-first-child ()
  19979. "Goto the first child, even if it is invisible.
  19980. Return t when a child was found. Otherwise don't move point and
  19981. return nil."
  19982. (let (level (pos (point)) (re org-outline-regexp-bol))
  19983. (when (condition-case nil (org-back-to-heading t) (error nil))
  19984. (setq level (outline-level))
  19985. (forward-char 1)
  19986. (if (and (re-search-forward re nil t) (> (outline-level) level))
  19987. (progn (goto-char (match-beginning 0)) t)
  19988. (goto-char pos) nil))))
  19989. (defun org-show-hidden-entry ()
  19990. "Show an entry where even the heading is hidden."
  19991. (save-excursion
  19992. (org-show-entry)))
  19993. (defun org-flag-heading (flag &optional entry)
  19994. "Flag the current heading. FLAG non-nil means make invisible.
  19995. When ENTRY is non-nil, show the entire entry."
  19996. (save-excursion
  19997. (org-back-to-heading t)
  19998. ;; Check if we should show the entire entry
  19999. (if entry
  20000. (progn
  20001. (org-show-entry)
  20002. (save-excursion
  20003. (and (outline-next-heading)
  20004. (org-flag-heading nil))))
  20005. (outline-flag-region (max (point-min) (1- (point)))
  20006. (save-excursion (outline-end-of-heading) (point))
  20007. flag))))
  20008. (defun org-get-next-sibling ()
  20009. "Move to next heading of the same level, and return point.
  20010. If there is no such heading, return nil.
  20011. This is like outline-next-sibling, but invisible headings are ok."
  20012. (let ((level (funcall outline-level)))
  20013. (outline-next-heading)
  20014. (while (and (not (eobp)) (> (funcall outline-level) level))
  20015. (outline-next-heading))
  20016. (if (or (eobp) (< (funcall outline-level) level))
  20017. nil
  20018. (point))))
  20019. (defun org-get-last-sibling ()
  20020. "Move to previous heading of the same level, and return point.
  20021. If there is no such heading, return nil."
  20022. (let ((opoint (point))
  20023. (level (funcall outline-level)))
  20024. (outline-previous-heading)
  20025. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20026. (while (and (> (funcall outline-level) level)
  20027. (not (bobp)))
  20028. (outline-previous-heading))
  20029. (if (< (funcall outline-level) level)
  20030. nil
  20031. (point)))))
  20032. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20033. "Goto to the end of a subtree."
  20034. ;; This contains an exact copy of the original function, but it uses
  20035. ;; `org-back-to-heading', to make it work also in invisible
  20036. ;; trees. And is uses an invisible-ok argument.
  20037. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20038. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20039. ;; with many children and grandchildren etc, this can be much faster
  20040. ;; than the outline version.
  20041. (org-back-to-heading invisible-ok)
  20042. (let ((first t)
  20043. (level (funcall outline-level)))
  20044. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20045. ;; A true heading (not a plain list item), in Org-mode
  20046. ;; This means we can easily find the end by looking
  20047. ;; only for the right number of stars. Using a regexp to do
  20048. ;; this is so much faster than using a Lisp loop.
  20049. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20050. (forward-char 1)
  20051. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20052. ;; something else, do it the slow way
  20053. (while (and (not (eobp))
  20054. (or first (> (funcall outline-level) level)))
  20055. (setq first nil)
  20056. (outline-next-heading)))
  20057. (unless to-heading
  20058. (if (memq (preceding-char) '(?\n ?\^M))
  20059. (progn
  20060. ;; Go to end of line before heading
  20061. (forward-char -1)
  20062. (if (memq (preceding-char) '(?\n ?\^M))
  20063. ;; leave blank line before heading
  20064. (forward-char -1))))))
  20065. (point))
  20066. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20067. "Use Org version in org-mode, for dramatic speed-up."
  20068. (if (derived-mode-p 'org-mode)
  20069. (progn
  20070. (org-end-of-subtree nil t)
  20071. (unless (eobp) (backward-char 1)))
  20072. ad-do-it))
  20073. (defun org-end-of-meta-data-and-drawers ()
  20074. "Jump to the first text after meta data and drawers in the current entry.
  20075. This will move over empty lines, lines with planning time stamps,
  20076. clocking lines, and drawers."
  20077. (org-back-to-heading t)
  20078. (let ((end (save-excursion (outline-next-heading) (point)))
  20079. (re (concat "\\(" org-drawer-regexp "\\)"
  20080. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20081. (forward-line 1)
  20082. (while (re-search-forward re end t)
  20083. (if (not (match-end 1))
  20084. ;; empty or planning line
  20085. (forward-line 1)
  20086. ;; a drawer, find the end
  20087. (re-search-forward "^[ \t]*:END:" end 'move)
  20088. (forward-line 1)))
  20089. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20090. (point)))
  20091. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20092. "Move forward to the arg'th subheading at same level as this one.
  20093. Stop at the first and last subheadings of a superior heading.
  20094. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20095. non-nil it will also look at invisible ones."
  20096. (interactive "p")
  20097. (org-back-to-heading invisible-ok)
  20098. (org-at-heading-p)
  20099. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20100. (re (format "^\\*\\{1,%d\\} " level))
  20101. l)
  20102. (forward-char 1)
  20103. (while (> arg 0)
  20104. (while (and (re-search-forward re nil 'move)
  20105. (setq l (- (match-end 0) (match-beginning 0) 1))
  20106. (= l level)
  20107. (not invisible-ok)
  20108. (progn (backward-char 1) (outline-invisible-p)))
  20109. (if (< l level) (setq arg 1)))
  20110. (setq arg (1- arg)))
  20111. (beginning-of-line 1)))
  20112. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20113. "Move backward to the arg'th subheading at same level as this one.
  20114. Stop at the first and last subheadings of a superior heading."
  20115. (interactive "p")
  20116. (org-back-to-heading)
  20117. (org-at-heading-p)
  20118. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20119. (re (format "^\\*\\{1,%d\\} " level))
  20120. l)
  20121. (while (> arg 0)
  20122. (while (and (re-search-backward re nil 'move)
  20123. (setq l (- (match-end 0) (match-beginning 0) 1))
  20124. (= l level)
  20125. (not invisible-ok)
  20126. (outline-invisible-p))
  20127. (if (< l level) (setq arg 1)))
  20128. (setq arg (1- arg)))))
  20129. (defun org-forward-element ()
  20130. "Move forward by one element.
  20131. Move to the next element at the same level, when possible."
  20132. (interactive)
  20133. (cond ((eobp) (error "Cannot move further down"))
  20134. ((org-with-limited-levels (org-at-heading-p))
  20135. (let ((origin (point)))
  20136. (org-forward-heading-same-level 1)
  20137. (unless (org-with-limited-levels (org-at-heading-p))
  20138. (goto-char origin)
  20139. (error "Cannot move further down"))))
  20140. (t
  20141. (let* ((elem (org-element-at-point))
  20142. (end (org-element-property :end elem))
  20143. (parent (org-element-property :parent elem)))
  20144. (if (and parent (= (org-element-property :contents-end parent) end))
  20145. (goto-char (org-element-property :end parent))
  20146. (goto-char end))))))
  20147. (defun org-backward-element ()
  20148. "Move backward by one element.
  20149. Move to the previous element at the same level, when possible."
  20150. (interactive)
  20151. (cond ((bobp) (error "Cannot move further up"))
  20152. ((org-with-limited-levels (org-at-heading-p))
  20153. ;; At an headline, move to the previous one, if any, or stay
  20154. ;; here.
  20155. (let ((origin (point)))
  20156. (org-backward-heading-same-level 1)
  20157. (unless (org-with-limited-levels (org-at-heading-p))
  20158. (goto-char origin)
  20159. (error "Cannot move further up"))))
  20160. (t
  20161. (let* ((trail (org-element-at-point 'keep-trail))
  20162. (elem (car trail))
  20163. (prev-elem (nth 1 trail))
  20164. (beg (org-element-property :begin elem)))
  20165. (cond
  20166. ;; Move to beginning of current element if point isn't
  20167. ;; there already.
  20168. ((/= (point) beg) (goto-char beg))
  20169. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20170. ((org-before-first-heading-p) (goto-char (point-min)))
  20171. (t (org-back-to-heading)))))))
  20172. (defun org-up-element ()
  20173. "Move to upper element."
  20174. (interactive)
  20175. (if (org-with-limited-levels (org-at-heading-p))
  20176. (unless (org-up-heading-safe) (error "No surrounding element"))
  20177. (let* ((elem (org-element-at-point))
  20178. (parent (org-element-property :parent elem)))
  20179. (if parent (goto-char (org-element-property :begin parent))
  20180. (if (org-with-limited-levels (org-before-first-heading-p))
  20181. (error "No surrounding element")
  20182. (org-with-limited-levels (org-back-to-heading)))))))
  20183. (defvar org-element-greater-elements)
  20184. (defun org-down-element ()
  20185. "Move to inner element."
  20186. (interactive)
  20187. (let ((element (org-element-at-point)))
  20188. (cond
  20189. ((memq (org-element-type element) '(plain-list table))
  20190. (goto-char (org-element-property :contents-begin element))
  20191. (forward-char))
  20192. ((memq (org-element-type element) org-element-greater-elements)
  20193. ;; If contents are hidden, first disclose them.
  20194. (when (org-element-property :hiddenp element) (org-cycle))
  20195. (goto-char (or (org-element-property :contents-begin element)
  20196. (error "No content for this element"))))
  20197. (t (error "No inner element")))))
  20198. (defun org-drag-element-backward ()
  20199. "Move backward element at point."
  20200. (interactive)
  20201. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20202. (let* ((trail (org-element-at-point 'keep-trail))
  20203. (elem (car trail))
  20204. (prev-elem (nth 1 trail)))
  20205. ;; Error out if no previous element or previous element is
  20206. ;; a parent of the current one.
  20207. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20208. (error "Cannot drag element backward")
  20209. (let ((pos (point)))
  20210. (org-element-swap-A-B prev-elem elem)
  20211. (goto-char (+ (org-element-property :begin prev-elem)
  20212. (- pos (org-element-property :begin elem)))))))))
  20213. (defun org-drag-element-forward ()
  20214. "Move forward element at point."
  20215. (interactive)
  20216. (let* ((pos (point))
  20217. (elem (org-element-at-point)))
  20218. (when (= (point-max) (org-element-property :end elem))
  20219. (error "Cannot drag element forward"))
  20220. (goto-char (org-element-property :end elem))
  20221. (let ((next-elem (org-element-at-point)))
  20222. (when (or (org-element-nested-p elem next-elem)
  20223. (and (eq (org-element-type next-elem) 'headline)
  20224. (not (eq (org-element-type elem) 'headline))))
  20225. (goto-char pos)
  20226. (error "Cannot drag element forward"))
  20227. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20228. ;; body's length (without final blanks) and by the length of
  20229. ;; blanks between ELEM and NEXT-ELEM.
  20230. (let ((size-next (- (save-excursion
  20231. (goto-char (org-element-property :end next-elem))
  20232. (skip-chars-backward " \r\t\n")
  20233. (forward-line)
  20234. ;; Small correction if buffer doesn't end
  20235. ;; with a newline character.
  20236. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20237. (org-element-property :begin next-elem)))
  20238. (size-blank (- (org-element-property :end elem)
  20239. (save-excursion
  20240. (goto-char (org-element-property :end elem))
  20241. (skip-chars-backward " \r\t\n")
  20242. (forward-line)
  20243. (point)))))
  20244. (org-element-swap-A-B elem next-elem)
  20245. (goto-char (+ pos size-next size-blank))))))
  20246. (defun org-mark-element ()
  20247. "Put point at beginning of this element, mark at end.
  20248. Interactively, if this command is repeated or (in Transient Mark
  20249. mode) if the mark is active, it marks the next element after the
  20250. ones already marked."
  20251. (interactive)
  20252. (let (deactivate-mark)
  20253. (if (and (org-called-interactively-p 'any)
  20254. (or (and (eq last-command this-command) (mark t))
  20255. (and transient-mark-mode mark-active)))
  20256. (set-mark
  20257. (save-excursion
  20258. (goto-char (mark))
  20259. (goto-char (org-element-property :end (org-element-at-point)))))
  20260. (let ((element (org-element-at-point)))
  20261. (end-of-line)
  20262. (push-mark (org-element-property :end element) t t)
  20263. (goto-char (org-element-property :begin element))))))
  20264. (defun org-narrow-to-element ()
  20265. "Narrow buffer to current element."
  20266. (interactive)
  20267. (let ((elem (org-element-at-point)))
  20268. (cond
  20269. ((eq (car elem) 'headline)
  20270. (narrow-to-region
  20271. (org-element-property :begin elem)
  20272. (org-element-property :end elem)))
  20273. ((memq (car elem) org-element-greater-elements)
  20274. (narrow-to-region
  20275. (org-element-property :contents-begin elem)
  20276. (org-element-property :contents-end elem)))
  20277. (t
  20278. (narrow-to-region
  20279. (org-element-property :begin elem)
  20280. (org-element-property :end elem))))))
  20281. (defun org-transpose-element ()
  20282. "Transpose current and previous elements, keeping blank lines between.
  20283. Point is moved after both elements."
  20284. (interactive)
  20285. (org-skip-whitespace)
  20286. (let ((end (org-element-property :end (org-element-at-point))))
  20287. (org-drag-element-backward)
  20288. (goto-char end)))
  20289. (defun org-unindent-buffer ()
  20290. "Un-indent the visible part of the buffer.
  20291. Relative indentation (between items, inside blocks, etc.) isn't
  20292. modified."
  20293. (interactive)
  20294. (unless (eq major-mode 'org-mode)
  20295. (error "Cannot un-indent a buffer not in Org mode"))
  20296. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  20297. unindent-tree ; For byte-compiler.
  20298. (unindent-tree
  20299. (function
  20300. (lambda (contents)
  20301. (mapc
  20302. (lambda (element)
  20303. (if (memq (org-element-type element) '(headline section))
  20304. (funcall unindent-tree (org-element-contents element))
  20305. (save-excursion
  20306. (save-restriction
  20307. (narrow-to-region
  20308. (org-element-property :begin element)
  20309. (org-element-property :end element))
  20310. (org-do-remove-indentation)))))
  20311. (reverse contents))))))
  20312. (funcall unindent-tree (org-element-contents parse-tree))))
  20313. (defun org-show-subtree ()
  20314. "Show everything after this heading at deeper levels."
  20315. (interactive)
  20316. (outline-flag-region
  20317. (point)
  20318. (save-excursion
  20319. (org-end-of-subtree t t))
  20320. nil))
  20321. (defun org-show-entry ()
  20322. "Show the body directly following this heading.
  20323. Show the heading too, if it is currently invisible."
  20324. (interactive)
  20325. (save-excursion
  20326. (condition-case nil
  20327. (progn
  20328. (org-back-to-heading t)
  20329. (outline-flag-region
  20330. (max (point-min) (1- (point)))
  20331. (save-excursion
  20332. (if (re-search-forward
  20333. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  20334. (match-beginning 1)
  20335. (point-max)))
  20336. nil)
  20337. (org-cycle-hide-drawers 'children))
  20338. (error nil))))
  20339. (defun org-make-options-regexp (kwds &optional extra)
  20340. "Make a regular expression for keyword lines."
  20341. (concat
  20342. "^#\\+\\("
  20343. (mapconcat 'regexp-quote kwds "\\|")
  20344. (if extra (concat "\\|" extra))
  20345. "\\):[ \t]*\\(.*\\)"))
  20346. ;; Make isearch reveal the necessary context
  20347. (defun org-isearch-end ()
  20348. "Reveal context after isearch exits."
  20349. (when isearch-success ; only if search was successful
  20350. (if (featurep 'xemacs)
  20351. ;; Under XEmacs, the hook is run in the correct place,
  20352. ;; we directly show the context.
  20353. (org-show-context 'isearch)
  20354. ;; In Emacs the hook runs *before* restoring the overlays.
  20355. ;; So we have to use a one-time post-command-hook to do this.
  20356. ;; (Emacs 22 has a special variable, see function `org-mode')
  20357. (unless (and (boundp 'isearch-mode-end-hook-quit)
  20358. isearch-mode-end-hook-quit)
  20359. ;; Only when the isearch was not quitted.
  20360. (org-add-hook 'post-command-hook 'org-isearch-post-command
  20361. 'append 'local)))))
  20362. (defun org-isearch-post-command ()
  20363. "Remove self from hook, and show context."
  20364. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  20365. (org-show-context 'isearch))
  20366. ;;;; Integration with and fixes for other packages
  20367. ;;; Imenu support
  20368. (defvar org-imenu-markers nil
  20369. "All markers currently used by Imenu.")
  20370. (make-variable-buffer-local 'org-imenu-markers)
  20371. (defun org-imenu-new-marker (&optional pos)
  20372. "Return a new marker for use by Imenu, and remember the marker."
  20373. (let ((m (make-marker)))
  20374. (move-marker m (or pos (point)))
  20375. (push m org-imenu-markers)
  20376. m))
  20377. (defun org-imenu-get-tree ()
  20378. "Produce the index for Imenu."
  20379. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  20380. (setq org-imenu-markers nil)
  20381. (let* ((n org-imenu-depth)
  20382. (re (concat "^" (org-get-limited-outline-regexp)))
  20383. (subs (make-vector (1+ n) nil))
  20384. (last-level 0)
  20385. m level head)
  20386. (save-excursion
  20387. (save-restriction
  20388. (widen)
  20389. (goto-char (point-max))
  20390. (while (re-search-backward re nil t)
  20391. (setq level (org-reduced-level (funcall outline-level)))
  20392. (when (and (<= level n)
  20393. (looking-at org-complex-heading-regexp))
  20394. (setq head (org-link-display-format
  20395. (org-match-string-no-properties 4))
  20396. m (org-imenu-new-marker))
  20397. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  20398. (if (>= level last-level)
  20399. (push (cons head m) (aref subs level))
  20400. (push (cons head (aref subs (1+ level))) (aref subs level))
  20401. (loop for i from (1+ level) to n do (aset subs i nil)))
  20402. (setq last-level level)))))
  20403. (aref subs 1)))
  20404. (eval-after-load "imenu"
  20405. '(progn
  20406. (add-hook 'imenu-after-jump-hook
  20407. (lambda ()
  20408. (if (derived-mode-p 'org-mode)
  20409. (org-show-context 'org-goto))))))
  20410. (defun org-link-display-format (link)
  20411. "Replace a link with either the description, or the link target
  20412. if no description is present"
  20413. (save-match-data
  20414. (if (string-match org-bracket-link-analytic-regexp link)
  20415. (replace-match (if (match-end 5)
  20416. (match-string 5 link)
  20417. (concat (match-string 1 link)
  20418. (match-string 3 link)))
  20419. nil t link)
  20420. link)))
  20421. (defun org-toggle-link-display ()
  20422. "Toggle the literal or descriptive display of links."
  20423. (interactive)
  20424. (if org-descriptive-links
  20425. (progn (org-remove-from-invisibility-spec '(org-link))
  20426. (org-restart-font-lock)
  20427. (setq org-descriptive-links nil))
  20428. (progn (add-to-invisibility-spec '(org-link))
  20429. (org-restart-font-lock)
  20430. (setq org-descriptive-links t))))
  20431. ;; Speedbar support
  20432. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  20433. "Overlay marking the agenda restriction line in speedbar.")
  20434. (overlay-put org-speedbar-restriction-lock-overlay
  20435. 'face 'org-agenda-restriction-lock)
  20436. (overlay-put org-speedbar-restriction-lock-overlay
  20437. 'help-echo "Agendas are currently limited to this item.")
  20438. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20439. (defun org-speedbar-set-agenda-restriction ()
  20440. "Restrict future agenda commands to the location at point in speedbar.
  20441. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  20442. (interactive)
  20443. (require 'org-agenda)
  20444. (let (p m tp np dir txt)
  20445. (cond
  20446. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20447. 'org-imenu t))
  20448. (setq m (get-text-property p 'org-imenu-marker))
  20449. (with-current-buffer (marker-buffer m)
  20450. (goto-char m)
  20451. (org-agenda-set-restriction-lock 'subtree)))
  20452. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20453. 'speedbar-function 'speedbar-find-file))
  20454. (setq tp (previous-single-property-change
  20455. (1+ p) 'speedbar-function)
  20456. np (next-single-property-change
  20457. tp 'speedbar-function)
  20458. dir (speedbar-line-directory)
  20459. txt (buffer-substring-no-properties (or tp (point-min))
  20460. (or np (point-max))))
  20461. (with-current-buffer (find-file-noselect
  20462. (let ((default-directory dir))
  20463. (expand-file-name txt)))
  20464. (unless (derived-mode-p 'org-mode)
  20465. (error "Cannot restrict to non-Org-mode file"))
  20466. (org-agenda-set-restriction-lock 'file)))
  20467. (t (error "Don't know how to restrict Org-mode's agenda")))
  20468. (move-overlay org-speedbar-restriction-lock-overlay
  20469. (point-at-bol) (point-at-eol))
  20470. (setq current-prefix-arg nil)
  20471. (org-agenda-maybe-redo)))
  20472. (eval-after-load "speedbar"
  20473. '(progn
  20474. (speedbar-add-supported-extension ".org")
  20475. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  20476. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  20477. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  20478. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  20479. (add-hook 'speedbar-visiting-tag-hook
  20480. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  20481. ;;; Fixes and Hacks for problems with other packages
  20482. ;; Make flyspell not check words in links, to not mess up our keymap
  20483. (defun org-mode-flyspell-verify ()
  20484. "Don't let flyspell put overlays at active buttons, or on
  20485. {todo,all-time,additional-option-like}-keywords."
  20486. (let ((pos (max (1- (point)) (point-min)))
  20487. (word (thing-at-point 'word)))
  20488. (and (not (get-text-property pos 'keymap))
  20489. (not (get-text-property pos 'org-no-flyspell))
  20490. (not (member word org-todo-keywords-1))
  20491. (not (member word org-all-time-keywords))
  20492. (not (member word org-options-keywords))
  20493. (not (member word (mapcar 'car org-startup-options)))
  20494. (not (member word org-additional-option-like-keywords-for-flyspell)))))
  20495. (defun org-remove-flyspell-overlays-in (beg end)
  20496. "Remove flyspell overlays in region."
  20497. (and (org-bound-and-true-p flyspell-mode)
  20498. (fboundp 'flyspell-delete-region-overlays)
  20499. (flyspell-delete-region-overlays beg end))
  20500. (add-text-properties beg end '(org-no-flyspell t)))
  20501. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  20502. (eval-after-load "bookmark"
  20503. '(if (boundp 'bookmark-after-jump-hook)
  20504. ;; We can use the hook
  20505. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  20506. ;; Hook not available, use advice
  20507. (defadvice bookmark-jump (after org-make-visible activate)
  20508. "Make the position visible."
  20509. (org-bookmark-jump-unhide))))
  20510. ;; Make sure saveplace shows the location if it was hidden
  20511. (eval-after-load "saveplace"
  20512. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  20513. "Make the position visible."
  20514. (org-bookmark-jump-unhide)))
  20515. ;; Make sure ecb shows the location if it was hidden
  20516. (eval-after-load "ecb"
  20517. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  20518. "Make hierarchy visible when jumping into location from ECB tree buffer."
  20519. (if (derived-mode-p 'org-mode)
  20520. (org-show-context))))
  20521. (defun org-bookmark-jump-unhide ()
  20522. "Unhide the current position, to show the bookmark location."
  20523. (and (derived-mode-p 'org-mode)
  20524. (or (outline-invisible-p)
  20525. (save-excursion (goto-char (max (point-min) (1- (point))))
  20526. (outline-invisible-p)))
  20527. (org-show-context 'bookmark-jump)))
  20528. ;; Make session.el ignore our circular variable
  20529. (eval-after-load "session"
  20530. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  20531. ;;;; Experimental code
  20532. (defun org-closed-in-range ()
  20533. "Sparse tree of items closed in a certain time range.
  20534. Still experimental, may disappear in the future."
  20535. (interactive)
  20536. ;; Get the time interval from the user.
  20537. (let* ((time1 (org-float-time
  20538. (org-read-date nil 'to-time nil "Starting date: ")))
  20539. (time2 (org-float-time
  20540. (org-read-date nil 'to-time nil "End date:")))
  20541. ;; callback function
  20542. (callback (lambda ()
  20543. (let ((time
  20544. (org-float-time
  20545. (apply 'encode-time
  20546. (org-parse-time-string
  20547. (match-string 1))))))
  20548. ;; check if time in interval
  20549. (and (>= time time1) (<= time time2))))))
  20550. ;; make tree, check each match with the callback
  20551. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  20552. ;;;; Finish up
  20553. (provide 'org)
  20554. (run-hooks 'org-load-hook)
  20555. ;;; org.el ends here