org.el 865 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911
  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. (let ((load-suffixes (list ".el")))
  74. (load "org-loaddefs" 'noerror nil nil 'mustsuffix))
  75. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  76. ;; some places -- e.g. see the macro org-with-limited-levels.
  77. ;;
  78. ;; In Org buffers, the value of `outline-regexp' is that of
  79. ;; `org-outline-regexp'. The only function still directly relying on
  80. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  81. ;; job when `orgstruct-mode' is active.
  82. (defvar org-outline-regexp "\\*+ "
  83. "Regexp to match Org headlines.")
  84. (defvar org-outline-regexp-bol "^\\*+ "
  85. "Regexp to match Org headlines.
  86. This is similar to `org-outline-regexp' but additionally makes
  87. sure that we are at the beginning of the line.")
  88. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  89. "Matches an headline, putting stars and text into groups.
  90. Stars are put in group 1 and the trimmed body in group 2.")
  91. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  92. (when (fboundp 'defvaralias)
  93. (unless (boundp 'calendar-view-holidays-initially-flag)
  94. (defvaralias 'calendar-view-holidays-initially-flag
  95. 'view-calendar-holidays-initially))
  96. (unless (boundp 'calendar-view-diary-initially-flag)
  97. (defvaralias 'calendar-view-diary-initially-flag
  98. 'view-diary-entries-initially))
  99. (unless (boundp 'diary-fancy-buffer)
  100. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  101. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  102. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  103. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  104. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  105. (declare-function org-at-clock-log-p "org-clock" ())
  106. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  107. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  108. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  109. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  110. (declare-function orgtbl-mode "org-table" (&optional arg))
  111. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  112. (declare-function org-beamer-mode "org-beamer" ())
  113. (declare-function org-table-edit-field "org-table" (arg))
  114. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  115. (declare-function org-id-get-create "org-id" (&optional force))
  116. (declare-function org-id-find-id-file "org-id" (id))
  117. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  118. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  119. (declare-function org-table-align "org-table" ())
  120. (declare-function org-table-paste-rectangle "org-table" ())
  121. (declare-function org-table-maybe-eval-formula "org-table" ())
  122. (declare-function org-table-maybe-recalculate-line "org-table" ())
  123. (autoload 'org-element-at-point "org-element")
  124. (autoload 'org-element-type "org-element")
  125. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  126. (declare-function org-element-type "org-element" (element))
  127. (declare-function org-element-context "org-element" ())
  128. (declare-function org-element-contents "org-element" (element))
  129. (declare-function org-element-property "org-element" (property element))
  130. (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion))
  131. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  132. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  133. (declare-function org-element--parse-objects "org-element" (beg end acc restriction))
  134. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  135. ;; load languages based on value of `org-babel-load-languages'
  136. (defvar org-babel-load-languages)
  137. ;;;###autoload
  138. (defun org-babel-do-load-languages (sym value)
  139. "Load the languages defined in `org-babel-load-languages'."
  140. (set-default sym value)
  141. (mapc (lambda (pair)
  142. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  143. (if active
  144. (progn
  145. (require (intern (concat "ob-" lang))))
  146. (progn
  147. (funcall 'fmakunbound
  148. (intern (concat "org-babel-execute:" lang)))
  149. (funcall 'fmakunbound
  150. (intern (concat "org-babel-expand-body:" lang)))))))
  151. org-babel-load-languages))
  152. (defcustom org-babel-load-languages '((emacs-lisp . t))
  153. "Languages which can be evaluated in Org-mode buffers.
  154. This list can be used to load support for any of the languages
  155. below, note that each language will depend on a different set of
  156. system executables and/or Emacs modes. When a language is
  157. \"loaded\", then code blocks in that language can be evaluated
  158. with `org-babel-execute-src-block' bound by default to C-c
  159. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  160. be set to remove code block evaluation from the C-c C-c
  161. keybinding. By default only Emacs Lisp (which has no
  162. requirements) is loaded."
  163. :group 'org-babel
  164. :set 'org-babel-do-load-languages
  165. :version "24.1"
  166. :type '(alist :tag "Babel Languages"
  167. :key-type
  168. (choice
  169. (const :tag "Awk" awk)
  170. (const :tag "C" C)
  171. (const :tag "R" R)
  172. (const :tag "Asymptote" asymptote)
  173. (const :tag "Calc" calc)
  174. (const :tag "Clojure" clojure)
  175. (const :tag "CSS" css)
  176. (const :tag "Ditaa" ditaa)
  177. (const :tag "Dot" dot)
  178. (const :tag "Emacs Lisp" emacs-lisp)
  179. (const :tag "Fortran" fortran)
  180. (const :tag "Gnuplot" gnuplot)
  181. (const :tag "Haskell" haskell)
  182. (const :tag "IO" io)
  183. (const :tag "Java" java)
  184. (const :tag "Javascript" js)
  185. (const :tag "LaTeX" latex)
  186. (const :tag "Ledger" ledger)
  187. (const :tag "Lilypond" lilypond)
  188. (const :tag "Lisp" lisp)
  189. (const :tag "Makefile" makefile)
  190. (const :tag "Maxima" maxima)
  191. (const :tag "Matlab" matlab)
  192. (const :tag "Mscgen" mscgen)
  193. (const :tag "Ocaml" ocaml)
  194. (const :tag "Octave" octave)
  195. (const :tag "Org" org)
  196. (const :tag "Perl" perl)
  197. (const :tag "Pico Lisp" picolisp)
  198. (const :tag "PlantUML" plantuml)
  199. (const :tag "Python" python)
  200. (const :tag "Ruby" ruby)
  201. (const :tag "Sass" sass)
  202. (const :tag "Scala" scala)
  203. (const :tag "Scheme" scheme)
  204. (const :tag "Screen" screen)
  205. (const :tag "Shell Script" sh)
  206. (const :tag "Shen" shen)
  207. (const :tag "Sql" sql)
  208. (const :tag "Sqlite" sqlite))
  209. :value-type (boolean :tag "Activate" :value t)))
  210. ;;;; Customization variables
  211. (defcustom org-clone-delete-id nil
  212. "Remove ID property of clones of a subtree.
  213. When non-nil, clones of a subtree don't inherit the ID property.
  214. Otherwise they inherit the ID property with a new unique
  215. identifier."
  216. :type 'boolean
  217. :version "24.1"
  218. :group 'org-id)
  219. ;;; Version
  220. (require 'org-compat)
  221. (org-check-version)
  222. ;;;###autoload
  223. (defun org-version (&optional here full message)
  224. "Show the org-mode version in the echo area.
  225. With prefix argument HERE, insert it at point.
  226. When FULL is non-nil, use a verbose version string.
  227. When MESSAGE is non-nil, display a message with the version."
  228. (interactive "P")
  229. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  230. (save-load-suffixes load-suffixes)
  231. (load-suffixes (list ".el"))
  232. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  233. (org-trash (or
  234. (and (fboundp 'org-release) (fboundp 'org-git-version))
  235. (load (concat org-dir "org-version")
  236. 'noerror 'nomessage nil 'mustsuffix)))
  237. (load-suffixes save-load-suffixes)
  238. (org-version (org-release))
  239. (git-version (org-git-version))
  240. (version (format "Org-mode version %s (%s @ %s)"
  241. org-version
  242. git-version
  243. (if org-install-dir
  244. (if (string= org-dir org-install-dir)
  245. org-install-dir
  246. (concat "mixed installation! " org-install-dir " and " org-dir))
  247. "org-loaddefs.el can not be found!")))
  248. (_version (if full version org-version)))
  249. (if (org-called-interactively-p 'interactive)
  250. (if here
  251. (insert version)
  252. (message version))
  253. (if message (message _version))
  254. _version)))
  255. (defconst org-version (org-version))
  256. ;;; Compatibility constants
  257. ;;; The custom variables
  258. (defgroup org nil
  259. "Outline-based notes management and organizer."
  260. :tag "Org"
  261. :group 'outlines
  262. :group 'calendar)
  263. (defcustom org-mode-hook nil
  264. "Mode hook for Org-mode, run after the mode was turned on."
  265. :group 'org
  266. :type 'hook)
  267. (defcustom org-load-hook nil
  268. "Hook that is run after org.el has been loaded."
  269. :group 'org
  270. :type 'hook)
  271. (defcustom org-log-buffer-setup-hook nil
  272. "Hook that is run after an Org log buffer is created."
  273. :group 'org
  274. :version "24.1"
  275. :type 'hook)
  276. (defvar org-modules) ; defined below
  277. (defvar org-modules-loaded nil
  278. "Have the modules been loaded already?")
  279. (defun org-load-modules-maybe (&optional force)
  280. "Load all extensions listed in `org-modules'."
  281. (when (or force (not org-modules-loaded))
  282. (mapc (lambda (ext)
  283. (condition-case nil (require ext)
  284. (error (message "Problems while trying to load feature `%s'" ext))))
  285. org-modules)
  286. (setq org-modules-loaded t)))
  287. (defun org-set-modules (var value)
  288. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  289. (set var value)
  290. (when (featurep 'org)
  291. (org-load-modules-maybe 'force)))
  292. (when (org-bound-and-true-p org-modules)
  293. (let ((a (member 'org-infojs org-modules)))
  294. (and a (setcar a 'org-jsinfo))))
  295. (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)
  296. "Modules that should always be loaded together with org.el.
  297. If a description starts with <C>, the file is not part of Emacs
  298. and loading it will require that you have downloaded and properly installed
  299. the org-mode distribution.
  300. You can also use this system to load external packages (i.e. neither Org
  301. core modules, nor modules from the CONTRIB directory). Just add symbols
  302. to the end of the list. If the package is called org-xyz.el, then you need
  303. to add the symbol `xyz', and the package must have a call to
  304. (provide 'org-xyz)"
  305. :group 'org
  306. :set 'org-set-modules
  307. :type
  308. '(set :greedy t
  309. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  310. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  311. (const :tag " crypt: Encryption of subtrees" org-crypt)
  312. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  313. (const :tag " docview: Links to doc-view buffers" org-docview)
  314. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  315. (const :tag " id: Global IDs for identifying entries" org-id)
  316. (const :tag " info: Links to Info nodes" org-info)
  317. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  318. (const :tag " habit: Track your consistency with habits" org-habit)
  319. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  320. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  321. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  322. (const :tag " mew Links to Mew folders/messages" org-mew)
  323. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  324. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  325. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  326. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  327. (const :tag " vm: Links to VM folders/messages" org-vm)
  328. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  329. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  330. (const :tag " mouse: Additional mouse support" org-mouse)
  331. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  332. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  333. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  334. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  335. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  336. (const :tag "C collector: Collect properties into tables" org-collector)
  337. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  338. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  339. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  340. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  341. (const :tag "C eval: Include command output as text" org-eval)
  342. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  343. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  344. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  345. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  346. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  347. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  348. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  349. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  350. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  351. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  352. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  353. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  354. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  355. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  356. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  357. (const :tag "C registry: A registry for Org-mode links" org-registry)
  358. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  359. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  360. (const :tag "C secretary: Team management with org-mode" org-secretary)
  361. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  362. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  363. (const :tag "C track: Keep up with Org-mode development" org-track)
  364. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  365. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  366. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  367. (defcustom org-support-shift-select nil
  368. "Non-nil means make shift-cursor commands select text when possible.
  369. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  370. start selecting a region, or enlarge regions started in this way.
  371. In Org-mode, in special contexts, these same keys are used for
  372. other purposes, important enough to compete with shift selection.
  373. Org tries to balance these needs by supporting `shift-select-mode'
  374. outside these special contexts, under control of this variable.
  375. The default of this variable is nil, to avoid confusing behavior. Shifted
  376. cursor keys will then execute Org commands in the following contexts:
  377. - on a headline, changing TODO state (left/right) and priority (up/down)
  378. - on a time stamp, changing the time
  379. - in a plain list item, changing the bullet type
  380. - in a property definition line, switching between allowed values
  381. - in the BEGIN line of a clock table (changing the time block).
  382. Outside these contexts, the commands will throw an error.
  383. When this variable is t and the cursor is not in a special
  384. context, Org-mode will support shift-selection for making and
  385. enlarging regions. To make this more effective, the bullet
  386. cycling will no longer happen anywhere in an item line, but only
  387. if the cursor is exactly on the bullet.
  388. If you set this variable to the symbol `always', then the keys
  389. will not be special in headlines, property lines, and item lines,
  390. to make shift selection work there as well. If this is what you
  391. want, you can use the following alternative commands: `C-c C-t'
  392. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  393. can be used to switch TODO sets, `C-c -' to cycle item bullet
  394. types, and properties can be edited by hand or in column view.
  395. However, when the cursor is on a timestamp, shift-cursor commands
  396. will still edit the time stamp - this is just too good to give up.
  397. XEmacs user should have this variable set to nil, because
  398. `shift-select-mode' is in Emacs 23 or later only."
  399. :group 'org
  400. :type '(choice
  401. (const :tag "Never" nil)
  402. (const :tag "When outside special context" t)
  403. (const :tag "Everywhere except timestamps" always)))
  404. (defcustom org-loop-over-headlines-in-active-region nil
  405. "Shall some commands act upon headlines in the active region?
  406. When set to `t', some commands will be performed in all headlines
  407. within the active region.
  408. When set to `start-level', some commands will be performed in all
  409. headlines within the active region, provided that these headlines
  410. are of the same level than the first one.
  411. When set to a string, those commands will be performed on the
  412. matching headlines within the active region. Such string must be
  413. a tags/property/todo match as it is used in the agenda tags view.
  414. The list of commands is: `org-schedule', `org-deadline',
  415. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  416. `org-archive-to-archive-sibling'. The archiving commands skip
  417. already archived entries."
  418. :type '(choice (const :tag "Don't loop" nil)
  419. (const :tag "All headlines in active region" t)
  420. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  421. (string :tag "Tags/Property/Todo matcher"))
  422. :version "24.1"
  423. :group 'org-todo
  424. :group 'org-archive)
  425. (defgroup org-startup nil
  426. "Options concerning startup of Org-mode."
  427. :tag "Org Startup"
  428. :group 'org)
  429. (defcustom org-startup-folded t
  430. "Non-nil means entering Org-mode will switch to OVERVIEW.
  431. This can also be configured on a per-file basis by adding one of
  432. the following lines anywhere in the buffer:
  433. #+STARTUP: fold (or `overview', this is equivalent)
  434. #+STARTUP: nofold (or `showall', this is equivalent)
  435. #+STARTUP: content
  436. #+STARTUP: showeverything"
  437. :group 'org-startup
  438. :type '(choice
  439. (const :tag "nofold: show all" nil)
  440. (const :tag "fold: overview" t)
  441. (const :tag "content: all headlines" content)
  442. (const :tag "show everything, even drawers" showeverything)))
  443. (defcustom org-startup-truncated t
  444. "Non-nil means entering Org-mode will set `truncate-lines'.
  445. This is useful since some lines containing links can be very long and
  446. uninteresting. Also tables look terrible when wrapped."
  447. :group 'org-startup
  448. :type 'boolean)
  449. (defcustom org-startup-indented nil
  450. "Non-nil means turn on `org-indent-mode' on startup.
  451. This can also be configured on a per-file basis by adding one of
  452. the following lines anywhere in the buffer:
  453. #+STARTUP: indent
  454. #+STARTUP: noindent"
  455. :group 'org-structure
  456. :type '(choice
  457. (const :tag "Not" nil)
  458. (const :tag "Globally (slow on startup in large files)" t)))
  459. (defcustom org-use-sub-superscripts t
  460. "Non-nil means interpret \"_\" and \"^\" for export.
  461. When this option is turned on, you can use TeX-like syntax for sub- and
  462. superscripts. Several characters after \"_\" or \"^\" will be
  463. considered as a single item - so grouping with {} is normally not
  464. needed. For example, the following things will be parsed as single
  465. sub- or superscripts.
  466. 10^24 or 10^tau several digits will be considered 1 item.
  467. 10^-12 or 10^-tau a leading sign with digits or a word
  468. x^2-y^3 will be read as x^2 - y^3, because items are
  469. terminated by almost any nonword/nondigit char.
  470. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  471. Still, ambiguity is possible - so when in doubt use {} to enclose the
  472. sub/superscript. If you set this variable to the symbol `{}',
  473. the braces are *required* in order to trigger interpretations as
  474. sub/superscript. This can be helpful in documents that need \"_\"
  475. frequently in plain text.
  476. Not all export backends support this, but HTML does.
  477. This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
  478. :group 'org-startup
  479. :group 'org-export-translation
  480. :version "24.1"
  481. :type '(choice
  482. (const :tag "Always interpret" t)
  483. (const :tag "Only with braces" {})
  484. (const :tag "Never interpret" nil)))
  485. (if (fboundp 'defvaralias)
  486. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  487. (defcustom org-startup-with-beamer-mode nil
  488. "Non-nil means turn on `org-beamer-mode' on startup.
  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: beamer"
  492. :group 'org-startup
  493. :version "24.1"
  494. :type 'boolean)
  495. (defcustom org-startup-align-all-tables nil
  496. "Non-nil means align all tables when visiting a file.
  497. This is useful when the column width in tables is forced with <N> cookies
  498. in table fields. Such tables will look correct only after the first re-align.
  499. This can also be configured on a per-file basis by adding one of
  500. the following lines anywhere in the buffer:
  501. #+STARTUP: align
  502. #+STARTUP: noalign"
  503. :group 'org-startup
  504. :type 'boolean)
  505. (defcustom org-startup-with-inline-images nil
  506. "Non-nil means show inline images when loading a new Org file.
  507. This can also be configured on a per-file basis by adding one of
  508. the following lines anywhere in the buffer:
  509. #+STARTUP: inlineimages
  510. #+STARTUP: noinlineimages"
  511. :group 'org-startup
  512. :version "24.1"
  513. :type 'boolean)
  514. (defcustom org-insert-mode-line-in-empty-file nil
  515. "Non-nil means insert the first line setting Org-mode in empty files.
  516. When the function `org-mode' is called interactively in an empty file, this
  517. normally means that the file name does not automatically trigger Org-mode.
  518. To ensure that the file will always be in Org-mode in the future, a
  519. line enforcing Org-mode will be inserted into the buffer, if this option
  520. has been set."
  521. :group 'org-startup
  522. :type 'boolean)
  523. (defcustom org-replace-disputed-keys nil
  524. "Non-nil means use alternative key bindings for some keys.
  525. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  526. These keys are also used by other packages like shift-selection-mode'
  527. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  528. If you want to use Org-mode together with one of these other modes,
  529. or more generally if you would like to move some Org-mode commands to
  530. other keys, set this variable and configure the keys with the variable
  531. `org-disputed-keys'.
  532. This option is only relevant at load-time of Org-mode, and must be set
  533. *before* org.el is loaded. Changing it requires a restart of Emacs to
  534. become effective."
  535. :group 'org-startup
  536. :type 'boolean)
  537. (defcustom org-use-extra-keys nil
  538. "Non-nil means use extra key sequence definitions for certain commands.
  539. This happens automatically if you run XEmacs or if `window-system'
  540. is nil. This variable lets you do the same manually. You must
  541. set it before loading org.
  542. Example: on Carbon Emacs 22 running graphically, with an external
  543. keyboard on a Powerbook, the default way of setting M-left might
  544. not work for either Alt or ESC. Setting this variable will make
  545. it work for ESC."
  546. :group 'org-startup
  547. :type 'boolean)
  548. (if (fboundp 'defvaralias)
  549. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  550. (defcustom org-disputed-keys
  551. '(([(shift up)] . [(meta p)])
  552. ([(shift down)] . [(meta n)])
  553. ([(shift left)] . [(meta -)])
  554. ([(shift right)] . [(meta +)])
  555. ([(control shift right)] . [(meta shift +)])
  556. ([(control shift left)] . [(meta shift -)]))
  557. "Keys for which Org-mode and other modes compete.
  558. This is an alist, cars are the default keys, second element specifies
  559. the alternative to use when `org-replace-disputed-keys' is t.
  560. Keys can be specified in any syntax supported by `define-key'.
  561. The value of this option takes effect only at Org-mode's startup,
  562. therefore you'll have to restart Emacs to apply it after changing."
  563. :group 'org-startup
  564. :type 'alist)
  565. (defun org-key (key)
  566. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  567. Or return the original if not disputed.
  568. Also apply the translations defined in `org-xemacs-key-equivalents'."
  569. (when org-replace-disputed-keys
  570. (let* ((nkey (key-description key))
  571. (x (org-find-if (lambda (x)
  572. (equal (key-description (car x)) nkey))
  573. org-disputed-keys)))
  574. (setq key (if x (cdr x) key))))
  575. (when (featurep 'xemacs)
  576. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  577. key)
  578. (defun org-find-if (predicate seq)
  579. (catch 'exit
  580. (while seq
  581. (if (funcall predicate (car seq))
  582. (throw 'exit (car seq))
  583. (pop seq)))))
  584. (defun org-defkey (keymap key def)
  585. "Define a key, possibly translated, as returned by `org-key'."
  586. (define-key keymap (org-key key) def))
  587. (defcustom org-ellipsis nil
  588. "The ellipsis to use in the Org-mode outline.
  589. When nil, just use the standard three dots. When a string, use that instead,
  590. When a face, use the standard 3 dots, but with the specified face.
  591. The change affects only Org-mode (which will then use its own display table).
  592. Changing this requires executing `M-x org-mode' in a buffer to become
  593. effective."
  594. :group 'org-startup
  595. :type '(choice (const :tag "Default" nil)
  596. (face :tag "Face" :value org-warning)
  597. (string :tag "String" :value "...#")))
  598. (defvar org-display-table nil
  599. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  600. (defgroup org-keywords nil
  601. "Keywords in Org-mode."
  602. :tag "Org Keywords"
  603. :group 'org)
  604. (defcustom org-deadline-string "DEADLINE:"
  605. "String to mark deadline entries.
  606. A deadline is this string, followed by a time stamp. Should be a word,
  607. terminated by a colon. You can insert a schedule keyword and
  608. a timestamp with \\[org-deadline].
  609. Changes become only effective after restarting Emacs."
  610. :group 'org-keywords
  611. :type 'string)
  612. (defcustom org-scheduled-string "SCHEDULED:"
  613. "String to mark scheduled TODO entries.
  614. A schedule is this string, followed by a time stamp. Should be a word,
  615. terminated by a colon. You can insert a schedule keyword and
  616. a timestamp with \\[org-schedule].
  617. Changes become only effective after restarting Emacs."
  618. :group 'org-keywords
  619. :type 'string)
  620. (defcustom org-closed-string "CLOSED:"
  621. "String used as the prefix for timestamps logging closing a TODO entry."
  622. :group 'org-keywords
  623. :type 'string)
  624. (defcustom org-clock-string "CLOCK:"
  625. "String used as prefix for timestamps clocking work hours on an item."
  626. :group 'org-keywords
  627. :type 'string)
  628. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  629. org-scheduled-string "\\|"
  630. org-deadline-string "\\|"
  631. org-closed-string "\\|"
  632. org-clock-string "\\)")
  633. "Matches a line with planning or clock info.")
  634. (defcustom org-comment-string "COMMENT"
  635. "Entries starting with this keyword will never be exported.
  636. An entry can be toggled between COMMENT and normal with
  637. \\[org-toggle-comment].
  638. Changes become only effective after restarting Emacs."
  639. :group 'org-keywords
  640. :type 'string)
  641. (defcustom org-quote-string "QUOTE"
  642. "Entries starting with this keyword will be exported in fixed-width font.
  643. Quoting applies only to the text in the entry following the headline, and does
  644. not extend beyond the next headline, even if that is lower level.
  645. An entry can be toggled between QUOTE and normal with
  646. \\[org-toggle-fixed-width-section]."
  647. :group 'org-keywords
  648. :type 'string)
  649. (defconst org-repeat-re
  650. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  651. "Regular expression for specifying repeated events.
  652. After a match, group 1 contains the repeat expression.")
  653. (defgroup org-structure nil
  654. "Options concerning the general structure of Org-mode files."
  655. :tag "Org Structure"
  656. :group 'org)
  657. (defgroup org-reveal-location nil
  658. "Options about how to make context of a location visible."
  659. :tag "Org Reveal Location"
  660. :group 'org-structure)
  661. (defconst org-context-choice
  662. '(choice
  663. (const :tag "Always" t)
  664. (const :tag "Never" nil)
  665. (repeat :greedy t :tag "Individual contexts"
  666. (cons
  667. (choice :tag "Context"
  668. (const agenda)
  669. (const org-goto)
  670. (const occur-tree)
  671. (const tags-tree)
  672. (const link-search)
  673. (const mark-goto)
  674. (const bookmark-jump)
  675. (const isearch)
  676. (const default))
  677. (boolean))))
  678. "Contexts for the reveal options.")
  679. (defcustom org-show-hierarchy-above '((default . t))
  680. "Non-nil means show full hierarchy when revealing a location.
  681. Org-mode often shows locations in an org-mode file which might have
  682. been invisible before. When this is set, the hierarchy of headings
  683. above the exposed location is shown.
  684. Turning this off for example for sparse trees makes them very compact.
  685. Instead of t, this can also be an alist specifying this option for different
  686. contexts. Valid contexts are
  687. agenda when exposing an entry from the agenda
  688. org-goto when using the command `org-goto' on key C-c C-j
  689. occur-tree when using the command `org-occur' on key C-c /
  690. tags-tree when constructing a sparse tree based on tags matches
  691. link-search when exposing search matches associated with a link
  692. mark-goto when exposing the jump goal of a mark
  693. bookmark-jump when exposing a bookmark location
  694. isearch when exiting from an incremental search
  695. default default for all contexts not set explicitly"
  696. :group 'org-reveal-location
  697. :type org-context-choice)
  698. (defcustom org-show-following-heading '((default . nil))
  699. "Non-nil means show following heading when revealing a location.
  700. Org-mode often shows locations in an org-mode file which might have
  701. been invisible before. When this is set, the heading following the
  702. match is shown.
  703. Turning this off for example for sparse trees makes them very compact,
  704. but makes it harder to edit the location of the match. In such a case,
  705. use the command \\[org-reveal] to show more context.
  706. Instead of t, this can also be an alist specifying this option for different
  707. contexts. See `org-show-hierarchy-above' for valid contexts."
  708. :group 'org-reveal-location
  709. :type org-context-choice)
  710. (defcustom org-show-siblings '((default . nil) (isearch t))
  711. "Non-nil means show all sibling heading when revealing a location.
  712. Org-mode often shows locations in an org-mode file which might have
  713. been invisible before. When this is set, the sibling of the current entry
  714. heading are all made visible. If `org-show-hierarchy-above' is t,
  715. the same happens on each level of the hierarchy above the current entry.
  716. By default this is on for the isearch context, off for all other contexts.
  717. Turning this off for example for sparse trees makes them very compact,
  718. but makes it harder to edit the location of the match. In such a case,
  719. use the command \\[org-reveal] to show more context.
  720. Instead of t, this can also be an alist specifying this option for different
  721. contexts. See `org-show-hierarchy-above' for valid contexts."
  722. :group 'org-reveal-location
  723. :type org-context-choice)
  724. (defcustom org-show-entry-below '((default . nil))
  725. "Non-nil means show the entry below a headline when revealing a location.
  726. Org-mode often shows locations in an org-mode file which might have
  727. been invisible before. When this is set, the text below the headline that is
  728. exposed is also shown.
  729. By default this is off for all contexts.
  730. Instead of t, this can also be an alist specifying this option for different
  731. contexts. See `org-show-hierarchy-above' for valid contexts."
  732. :group 'org-reveal-location
  733. :type org-context-choice)
  734. (defcustom org-indirect-buffer-display 'other-window
  735. "How should indirect tree buffers be displayed?
  736. This applies to indirect buffers created with the commands
  737. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  738. Valid values are:
  739. current-window Display in the current window
  740. other-window Just display in another window.
  741. dedicated-frame Create one new frame, and re-use it each time.
  742. new-frame Make a new frame each time. Note that in this case
  743. previously-made indirect buffers are kept, and you need to
  744. kill these buffers yourself."
  745. :group 'org-structure
  746. :group 'org-agenda-windows
  747. :type '(choice
  748. (const :tag "In current window" current-window)
  749. (const :tag "In current frame, other window" other-window)
  750. (const :tag "Each time a new frame" new-frame)
  751. (const :tag "One dedicated frame" dedicated-frame)))
  752. (defcustom org-use-speed-commands nil
  753. "Non-nil means activate single letter commands at beginning of a headline.
  754. This may also be a function to test for appropriate locations where speed
  755. commands should be active."
  756. :group 'org-structure
  757. :type '(choice
  758. (const :tag "Never" nil)
  759. (const :tag "At beginning of headline stars" t)
  760. (function)))
  761. (defcustom org-speed-commands-user nil
  762. "Alist of additional speed commands.
  763. This list will be checked before `org-speed-commands-default'
  764. when the variable `org-use-speed-commands' is non-nil
  765. and when the cursor is at the beginning of a headline.
  766. The car if each entry is a string with a single letter, which must
  767. be assigned to `self-insert-command' in the global map.
  768. The cdr is either a command to be called interactively, a function
  769. to be called, or a form to be evaluated.
  770. An entry that is just a list with a single string will be interpreted
  771. as a descriptive headline that will be added when listing the speed
  772. commands in the Help buffer using the `?' speed command."
  773. :group 'org-structure
  774. :type '(repeat :value ("k" . ignore)
  775. (choice :value ("k" . ignore)
  776. (list :tag "Descriptive Headline" (string :tag "Headline"))
  777. (cons :tag "Letter and Command"
  778. (string :tag "Command letter")
  779. (choice
  780. (function)
  781. (sexp))))))
  782. (defgroup org-cycle nil
  783. "Options concerning visibility cycling in Org-mode."
  784. :tag "Org Cycle"
  785. :group 'org-structure)
  786. (defcustom org-cycle-skip-children-state-if-no-children t
  787. "Non-nil means skip CHILDREN state in entries that don't have any."
  788. :group 'org-cycle
  789. :type 'boolean)
  790. (defcustom org-cycle-max-level nil
  791. "Maximum level which should still be subject to visibility cycling.
  792. Levels higher than this will, for cycling, be treated as text, not a headline.
  793. When `org-odd-levels-only' is set, a value of N in this variable actually
  794. means 2N-1 stars as the limiting headline.
  795. When nil, cycle all levels.
  796. Note that the limiting level of cycling is also influenced by
  797. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  798. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  799. than its value."
  800. :group 'org-cycle
  801. :type '(choice
  802. (const :tag "No limit" nil)
  803. (integer :tag "Maximum level")))
  804. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  805. "Names of drawers. Drawers are not opened by cycling on the headline above.
  806. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  807. this:
  808. :DRAWERNAME:
  809. .....
  810. :END:
  811. The drawer \"PROPERTIES\" is special for capturing properties through
  812. the property API.
  813. Drawers can be defined on the per-file basis with a line like:
  814. #+DRAWERS: HIDDEN STATE PROPERTIES"
  815. :group 'org-structure
  816. :group 'org-cycle
  817. :type '(repeat (string :tag "Drawer Name")))
  818. (defcustom org-hide-block-startup nil
  819. "Non-nil means entering Org-mode will fold all blocks.
  820. This can also be set in on a per-file basis with
  821. #+STARTUP: hideblocks
  822. #+STARTUP: showblocks"
  823. :group 'org-startup
  824. :group 'org-cycle
  825. :type 'boolean)
  826. (defcustom org-cycle-global-at-bob nil
  827. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  828. This makes it possible to do global cycling without having to use S-TAB or
  829. \\[universal-argument] TAB. For this special case to work, the first line
  830. of the buffer must not be a headline -- it may be empty or some other text.
  831. When used in this way, `org-cycle-hook' is disabled temporarily to make
  832. sure the cursor stays at the beginning of the buffer. When this option is
  833. nil, don't do anything special at the beginning of the buffer."
  834. :group 'org-cycle
  835. :type 'boolean)
  836. (defcustom org-cycle-level-after-item/entry-creation t
  837. "Non-nil means cycle entry level or item indentation in new empty entries.
  838. When the cursor is at the end of an empty headline, i.e with only stars
  839. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  840. and then all possible ancestor states, before returning to the original state.
  841. This makes data entry extremely fast: M-RET to create a new headline,
  842. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  843. When the cursor is at the end of an empty plain list item, one TAB will
  844. make it a subitem, two or more tabs will back up to make this an item
  845. higher up in the item hierarchy."
  846. :group 'org-cycle
  847. :type 'boolean)
  848. (defcustom org-cycle-emulate-tab t
  849. "Where should `org-cycle' emulate TAB.
  850. nil Never
  851. white Only in completely white lines
  852. whitestart Only at the beginning of lines, before the first non-white char
  853. t Everywhere except in headlines
  854. exc-hl-bol Everywhere except at the start of a headline
  855. If TAB is used in a place where it does not emulate TAB, the current subtree
  856. visibility is cycled."
  857. :group 'org-cycle
  858. :type '(choice (const :tag "Never" nil)
  859. (const :tag "Only in completely white lines" white)
  860. (const :tag "Before first char in a line" whitestart)
  861. (const :tag "Everywhere except in headlines" t)
  862. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  863. ))
  864. (defcustom org-cycle-separator-lines 2
  865. "Number of empty lines needed to keep an empty line between collapsed trees.
  866. If you leave an empty line between the end of a subtree and the following
  867. headline, this empty line is hidden when the subtree is folded.
  868. Org-mode will leave (exactly) one empty line visible if the number of
  869. empty lines is equal or larger to the number given in this variable.
  870. So the default 2 means at least 2 empty lines after the end of a subtree
  871. are needed to produce free space between a collapsed subtree and the
  872. following headline.
  873. If the number is negative, and the number of empty lines is at least -N,
  874. all empty lines are shown.
  875. Special case: when 0, never leave empty lines in collapsed view."
  876. :group 'org-cycle
  877. :type 'integer)
  878. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  879. (defcustom org-pre-cycle-hook nil
  880. "Hook that is run before visibility cycling is happening.
  881. The function(s) in this hook must accept a single argument which indicates
  882. the new state that will be set right after running this hook. The
  883. argument is a symbol. Before a global state change, it can have the values
  884. `overview', `content', or `all'. Before a local state change, it can have
  885. the values `folded', `children', or `subtree'."
  886. :group 'org-cycle
  887. :type 'hook)
  888. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  889. org-cycle-hide-drawers
  890. org-cycle-hide-inline-tasks
  891. org-cycle-show-empty-lines
  892. org-optimize-window-after-visibility-change)
  893. "Hook that is run after `org-cycle' has changed the buffer visibility.
  894. The function(s) in this hook must accept a single argument which indicates
  895. the new state that was set by the most recent `org-cycle' command. The
  896. argument is a symbol. After a global state change, it can have the values
  897. `overview', `contents', or `all'. After a local state change, it can have
  898. the values `folded', `children', or `subtree'."
  899. :group 'org-cycle
  900. :type 'hook)
  901. (defgroup org-edit-structure nil
  902. "Options concerning structure editing in Org-mode."
  903. :tag "Org Edit Structure"
  904. :group 'org-structure)
  905. (defcustom org-odd-levels-only nil
  906. "Non-nil means skip even levels and only use odd levels for the outline.
  907. This has the effect that two stars are being added/taken away in
  908. promotion/demotion commands. It also influences how levels are
  909. handled by the exporters.
  910. Changing it requires restart of `font-lock-mode' to become effective
  911. for fontification also in regions already fontified.
  912. You may also set this on a per-file basis by adding one of the following
  913. lines to the buffer:
  914. #+STARTUP: odd
  915. #+STARTUP: oddeven"
  916. :group 'org-edit-structure
  917. :group 'org-appearance
  918. :type 'boolean)
  919. (defcustom org-adapt-indentation t
  920. "Non-nil means adapt indentation to outline node level.
  921. When this variable is set, Org assumes that you write outlines by
  922. indenting text in each node to align with the headline (after the stars).
  923. The following issues are influenced by this variable:
  924. - When this is set and the *entire* text in an entry is indented, the
  925. indentation is increased by one space in a demotion command, and
  926. decreased by one in a promotion command. If any line in the entry
  927. body starts with text at column 0, indentation is not changed at all.
  928. - Property drawers and planning information is inserted indented when
  929. this variable s set. When nil, they will not be indented.
  930. - TAB indents a line relative to context. The lines below a headline
  931. will be indented when this variable is set.
  932. Note that this is all about true indentation, by adding and removing
  933. space characters. See also `org-indent.el' which does level-dependent
  934. indentation in a virtual way, i.e. at display time in Emacs."
  935. :group 'org-edit-structure
  936. :type 'boolean)
  937. (defcustom org-special-ctrl-a/e nil
  938. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  939. When t, `C-a' will bring back the cursor to the beginning of the
  940. headline text, i.e. after the stars and after a possible TODO
  941. keyword. In an item, this will be the position after bullet and
  942. check-box, if any. When the cursor is already at that position,
  943. another `C-a' will bring it to the beginning of the line.
  944. `C-e' will jump to the end of the headline, ignoring the presence
  945. of tags in the headline. A second `C-e' will then jump to the
  946. true end of the line, after any tags. This also means that, when
  947. this variable is non-nil, `C-e' also will never jump beyond the
  948. end of the heading of a folded section, i.e. not after the
  949. ellipses.
  950. When set to the symbol `reversed', the first `C-a' or `C-e' works
  951. normally, going to the true line boundary first. Only a directly
  952. following, identical keypress will bring the cursor to the
  953. special positions.
  954. This may also be a cons cell where the behavior for `C-a' and
  955. `C-e' is set separately."
  956. :group 'org-edit-structure
  957. :type '(choice
  958. (const :tag "off" nil)
  959. (const :tag "on: after stars/bullet and before tags first" t)
  960. (const :tag "reversed: true line boundary first" reversed)
  961. (cons :tag "Set C-a and C-e separately"
  962. (choice :tag "Special C-a"
  963. (const :tag "off" nil)
  964. (const :tag "on: after stars/bullet first" t)
  965. (const :tag "reversed: before stars/bullet first" reversed))
  966. (choice :tag "Special C-e"
  967. (const :tag "off" nil)
  968. (const :tag "on: before tags first" t)
  969. (const :tag "reversed: after tags first" reversed)))))
  970. (if (fboundp 'defvaralias)
  971. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  972. (defcustom org-special-ctrl-k nil
  973. "Non-nil means `C-k' will behave specially in headlines.
  974. When nil, `C-k' will call the default `kill-line' command.
  975. When t, the following will happen while the cursor is in the headline:
  976. - When the cursor is at the beginning of a headline, kill the entire
  977. line and possible the folded subtree below the line.
  978. - When in the middle of the headline text, kill the headline up to the tags.
  979. - When after the headline text, kill the tags."
  980. :group 'org-edit-structure
  981. :type 'boolean)
  982. (defcustom org-ctrl-k-protect-subtree nil
  983. "Non-nil means, do not delete a hidden subtree with C-k.
  984. When set to the symbol `error', simply throw an error when C-k is
  985. used to kill (part-of) a headline that has hidden text behind it.
  986. Any other non-nil value will result in a query to the user, if it is
  987. OK to kill that hidden subtree. When nil, kill without remorse."
  988. :group 'org-edit-structure
  989. :version "24.1"
  990. :type '(choice
  991. (const :tag "Do not protect hidden subtrees" nil)
  992. (const :tag "Protect hidden subtrees with a security query" t)
  993. (const :tag "Never kill a hidden subtree with C-k" error)))
  994. (defcustom org-catch-invisible-edits nil
  995. "Check if in invisible region before inserting or deleting a character.
  996. Valid values are:
  997. nil Do not check, so just do invisible edits.
  998. error Throw an error and do nothing.
  999. show Make point visible, and do the requested edit.
  1000. show-and-error Make point visible, then throw an error and abort the edit.
  1001. smart Make point visible, and do insertion/deletion if it is
  1002. adjacent to visible text and the change feels predictable.
  1003. Never delete a previously invisible character or add in the
  1004. middle or right after an invisible region. Basically, this
  1005. allows insertion and backward-delete right before ellipses.
  1006. FIXME: maybe in this case we should not even show?"
  1007. :group 'org-edit-structure
  1008. :version "24.1"
  1009. :type '(choice
  1010. (const :tag "Do not check" nil)
  1011. (const :tag "Throw error when trying to edit" error)
  1012. (const :tag "Unhide, but do not do the edit" show-and-error)
  1013. (const :tag "Show invisible part and do the edit" show)
  1014. (const :tag "Be smart and do the right thing" smart)))
  1015. (defcustom org-yank-folded-subtrees t
  1016. "Non-nil means when yanking subtrees, fold them.
  1017. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1018. it starts with a heading and all other headings in it are either children
  1019. or siblings, then fold all the subtrees. However, do this only if no
  1020. text after the yank would be swallowed into a folded tree by this action."
  1021. :group 'org-edit-structure
  1022. :type 'boolean)
  1023. (defcustom org-yank-adjusted-subtrees nil
  1024. "Non-nil means when yanking subtrees, adjust the level.
  1025. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1026. this function for details."
  1027. :group 'org-edit-structure
  1028. :type 'boolean)
  1029. (defcustom org-M-RET-may-split-line '((default . t))
  1030. "Non-nil means M-RET will split the line at the cursor position.
  1031. When nil, it will go to the end of the line before making a
  1032. new line.
  1033. You may also set this option in a different way for different
  1034. contexts. Valid contexts are:
  1035. headline when creating a new headline
  1036. item when creating a new item
  1037. table in a table field
  1038. default the value to be used for all contexts not explicitly
  1039. customized"
  1040. :group 'org-structure
  1041. :group 'org-table
  1042. :type '(choice
  1043. (const :tag "Always" t)
  1044. (const :tag "Never" nil)
  1045. (repeat :greedy t :tag "Individual contexts"
  1046. (cons
  1047. (choice :tag "Context"
  1048. (const headline)
  1049. (const item)
  1050. (const table)
  1051. (const default))
  1052. (boolean)))))
  1053. (defcustom org-insert-heading-respect-content nil
  1054. "Non-nil means insert new headings after the current subtree.
  1055. When nil, the new heading is created directly after the current line.
  1056. The commands \\[org-insert-heading-respect-content] and
  1057. \\[org-insert-todo-heading-respect-content] turn this variable on
  1058. for the duration of the command."
  1059. :group 'org-structure
  1060. :type 'boolean)
  1061. (defcustom org-blank-before-new-entry '((heading . auto)
  1062. (plain-list-item . auto))
  1063. "Should `org-insert-heading' leave a blank line before new heading/item?
  1064. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1065. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1066. which case Org will look at the surrounding headings/items and try to
  1067. make an intelligent decision whether to insert a blank line or not.
  1068. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1069. set, the setting here is ignored and no empty line is inserted, to avoid
  1070. breaking the list structure."
  1071. :group 'org-edit-structure
  1072. :type '(list
  1073. (cons (const heading)
  1074. (choice (const :tag "Never" nil)
  1075. (const :tag "Always" t)
  1076. (const :tag "Auto" auto)))
  1077. (cons (const plain-list-item)
  1078. (choice (const :tag "Never" nil)
  1079. (const :tag "Always" t)
  1080. (const :tag "Auto" auto)))))
  1081. (defcustom org-insert-heading-hook nil
  1082. "Hook being run after inserting a new heading."
  1083. :group 'org-edit-structure
  1084. :type 'hook)
  1085. (defcustom org-enable-fixed-width-editor t
  1086. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1087. This currently only means they are never auto-wrapped.
  1088. When nil, such lines will be treated like ordinary lines.
  1089. See also the QUOTE keyword."
  1090. :group 'org-edit-structure
  1091. :type 'boolean)
  1092. (defcustom org-goto-auto-isearch t
  1093. "Non-nil means typing characters in `org-goto' starts incremental search.
  1094. When nil, you can use these keybindings to navigate the buffer:
  1095. q Quit the org-goto interface
  1096. n Go to the next visible heading
  1097. p Go to the previous visible heading
  1098. f Go one heading forward on same level
  1099. b Go one heading backward on same level
  1100. u Go one heading up"
  1101. :group 'org-edit-structure
  1102. :type 'boolean)
  1103. (defgroup org-sparse-trees nil
  1104. "Options concerning sparse trees in Org-mode."
  1105. :tag "Org Sparse Trees"
  1106. :group 'org-structure)
  1107. (defcustom org-highlight-sparse-tree-matches t
  1108. "Non-nil means highlight all matches that define a sparse tree.
  1109. The highlights will automatically disappear the next time the buffer is
  1110. changed by an edit command."
  1111. :group 'org-sparse-trees
  1112. :type 'boolean)
  1113. (defcustom org-remove-highlights-with-change t
  1114. "Non-nil means any change to the buffer will remove temporary highlights.
  1115. Such highlights are created by `org-occur' and `org-clock-display'.
  1116. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1117. The highlights created by `org-preview-latex-fragment' always need
  1118. `C-c C-c' to be removed."
  1119. :group 'org-sparse-trees
  1120. :group 'org-time
  1121. :type 'boolean)
  1122. (defcustom org-occur-hook '(org-first-headline-recenter)
  1123. "Hook that is run after `org-occur' has constructed a sparse tree.
  1124. This can be used to recenter the window to show as much of the structure
  1125. as possible."
  1126. :group 'org-sparse-trees
  1127. :type 'hook)
  1128. (defgroup org-imenu-and-speedbar nil
  1129. "Options concerning imenu and speedbar in Org-mode."
  1130. :tag "Org Imenu and Speedbar"
  1131. :group 'org-structure)
  1132. (defcustom org-imenu-depth 2
  1133. "The maximum level for Imenu access to Org-mode headlines.
  1134. This also applied for speedbar access."
  1135. :group 'org-imenu-and-speedbar
  1136. :type 'integer)
  1137. (defgroup org-table nil
  1138. "Options concerning tables in Org-mode."
  1139. :tag "Org Table"
  1140. :group 'org)
  1141. (defcustom org-enable-table-editor 'optimized
  1142. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1143. When nil, such lines will be treated like ordinary lines.
  1144. When equal to the symbol `optimized', the table editor will be optimized to
  1145. do the following:
  1146. - Automatic overwrite mode in front of whitespace in table fields.
  1147. This makes the structure of the table stay in tact as long as the edited
  1148. field does not exceed the column width.
  1149. - Minimize the number of realigns. Normally, the table is aligned each time
  1150. TAB or RET are pressed to move to another field. With optimization this
  1151. happens only if changes to a field might have changed the column width.
  1152. Optimization requires replacing the functions `self-insert-command',
  1153. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1154. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1155. very good at guessing when a re-align will be necessary, but you can always
  1156. force one with \\[org-ctrl-c-ctrl-c].
  1157. If you would like to use the optimized version in Org-mode, but the
  1158. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1159. This variable can be used to turn on and off the table editor during a session,
  1160. but in order to toggle optimization, a restart is required.
  1161. See also the variable `org-table-auto-blank-field'."
  1162. :group 'org-table
  1163. :type '(choice
  1164. (const :tag "off" nil)
  1165. (const :tag "on" t)
  1166. (const :tag "on, optimized" optimized)))
  1167. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1168. (version<= emacs-version "24.1"))
  1169. "Non-nil means cluster self-insert commands for undo when possible.
  1170. If this is set, then, like in the Emacs command loop, 20 consecutive
  1171. characters will be undone together.
  1172. This is configurable, because there is some impact on typing performance."
  1173. :group 'org-table
  1174. :type 'boolean)
  1175. (defcustom org-table-tab-recognizes-table.el t
  1176. "Non-nil means TAB will automatically notice a table.el table.
  1177. When it sees such a table, it moves point into it and - if necessary -
  1178. calls `table-recognize-table'."
  1179. :group 'org-table-editing
  1180. :type 'boolean)
  1181. (defgroup org-link nil
  1182. "Options concerning links in Org-mode."
  1183. :tag "Org Link"
  1184. :group 'org)
  1185. (defvar org-link-abbrev-alist-local nil
  1186. "Buffer-local version of `org-link-abbrev-alist', which see.
  1187. The value of this is taken from the #+LINK lines.")
  1188. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1189. (defcustom org-link-abbrev-alist nil
  1190. "Alist of link abbreviations.
  1191. The car of each element is a string, to be replaced at the start of a link.
  1192. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1193. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1194. [[linkkey:tag][description]]
  1195. The 'linkkey' must be a word word, starting with a letter, followed
  1196. by letters, numbers, '-' or '_'.
  1197. If REPLACE is a string, the tag will simply be appended to create the link.
  1198. If the string contains \"%s\", the tag will be inserted there. If the string
  1199. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1200. at that point (see the function `url-hexify-string'). If the string contains
  1201. the specifier \"%(my-function)\", then the custom function `my-function' will
  1202. be invoked: this function takes the tag as its only argument and must return
  1203. a string.
  1204. REPLACE may also be a function that will be called with the tag as the
  1205. only argument to create the link, which should be returned as a string.
  1206. See the manual for examples."
  1207. :group 'org-link
  1208. :type '(repeat
  1209. (cons
  1210. (string :tag "Protocol")
  1211. (choice
  1212. (string :tag "Format")
  1213. (function)))))
  1214. (defcustom org-descriptive-links t
  1215. "Non-nil means Org will display descriptive links.
  1216. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1217. \"Org Website\", hiding the link itself and just displaying its
  1218. description. When set to `nil', Org will display the full links
  1219. literally.
  1220. You can interactively set the value of this variable by calling
  1221. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1222. :group 'org-link
  1223. :type 'boolean)
  1224. (defcustom org-link-file-path-type 'adaptive
  1225. "How the path name in file links should be stored.
  1226. Valid values are:
  1227. relative Relative to the current directory, i.e. the directory of the file
  1228. into which the link is being inserted.
  1229. absolute Absolute path, if possible with ~ for home directory.
  1230. noabbrev Absolute path, no abbreviation of home directory.
  1231. adaptive Use relative path for files in the current directory and sub-
  1232. directories of it. For other files, use an absolute path."
  1233. :group 'org-link
  1234. :type '(choice
  1235. (const relative)
  1236. (const absolute)
  1237. (const noabbrev)
  1238. (const adaptive)))
  1239. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1240. "Types of links that should be activated in Org-mode files.
  1241. This is a list of symbols, each leading to the activation of a certain link
  1242. type. In principle, it does not hurt to turn on most link types - there may
  1243. be a small gain when turning off unused link types. The types are:
  1244. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1245. angle Links in angular brackets that may contain whitespace like
  1246. <bbdb:Carsten Dominik>.
  1247. plain Plain links in normal text, no whitespace, like http://google.com.
  1248. radio Text that is matched by a radio target, see manual for details.
  1249. tag Tag settings in a headline (link to tag search).
  1250. date Time stamps (link to calendar).
  1251. footnote Footnote labels.
  1252. Changing this variable requires a restart of Emacs to become effective."
  1253. :group 'org-link
  1254. :type '(set :greedy t
  1255. (const :tag "Double bracket links" bracket)
  1256. (const :tag "Angular bracket links" angle)
  1257. (const :tag "Plain text links" plain)
  1258. (const :tag "Radio target matches" radio)
  1259. (const :tag "Tags" tag)
  1260. (const :tag "Timestamps" date)
  1261. (const :tag "Footnotes" footnote)))
  1262. (defcustom org-make-link-description-function nil
  1263. "Function to use for generating link descriptions from links.
  1264. When nil, the link location will be used. This function must take
  1265. two parameters: the first one is the link, the second one is the
  1266. description generated by `org-insert-link'. The function should
  1267. return the description to use."
  1268. :group 'org-link
  1269. :type 'function)
  1270. (defgroup org-link-store nil
  1271. "Options concerning storing links in Org-mode."
  1272. :tag "Org Store Link"
  1273. :group 'org-link)
  1274. (defcustom org-url-hexify-p t
  1275. "When non-nil, hexify URL when creating a link."
  1276. :type 'boolean
  1277. :version "24.3"
  1278. :group 'org-link-store)
  1279. (defcustom org-email-link-description-format "Email %c: %.30s"
  1280. "Format of the description part of a link to an email or usenet message.
  1281. The following %-escapes will be replaced by corresponding information:
  1282. %F full \"From\" field
  1283. %f name, taken from \"From\" field, address if no name
  1284. %T full \"To\" field
  1285. %t first name in \"To\" field, address if no name
  1286. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1287. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1288. %s subject
  1289. %d date
  1290. %m message-id.
  1291. You may use normal field width specification between the % and the letter.
  1292. This is for example useful to limit the length of the subject.
  1293. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1294. :group 'org-link-store
  1295. :type 'string)
  1296. (defcustom org-from-is-user-regexp
  1297. (let (r1 r2)
  1298. (when (and user-mail-address (not (string= user-mail-address "")))
  1299. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1300. (when (and user-full-name (not (string= user-full-name "")))
  1301. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1302. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1303. "Regexp matched against the \"From:\" header of an email or usenet message.
  1304. It should match if the message is from the user him/herself."
  1305. :group 'org-link-store
  1306. :type 'regexp)
  1307. (defcustom org-context-in-file-links t
  1308. "Non-nil means file links from `org-store-link' contain context.
  1309. A search string will be added to the file name with :: as separator and
  1310. used to find the context when the link is activated by the command
  1311. `org-open-at-point'. When this option is t, the entire active region
  1312. will be placed in the search string of the file link. If set to a
  1313. positive integer, only the first n lines of context will be stored.
  1314. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1315. negates this setting for the duration of the command."
  1316. :group 'org-link-store
  1317. :type '(choice boolean integer))
  1318. (defcustom org-keep-stored-link-after-insertion nil
  1319. "Non-nil means keep link in list for entire session.
  1320. The command `org-store-link' adds a link pointing to the current
  1321. location to an internal list. These links accumulate during a session.
  1322. The command `org-insert-link' can be used to insert links into any
  1323. Org-mode file (offering completion for all stored links). When this
  1324. option is nil, every link which has been inserted once using \\[org-insert-link]
  1325. will be removed from the list, to make completing the unused links
  1326. more efficient."
  1327. :group 'org-link-store
  1328. :type 'boolean)
  1329. (defgroup org-link-follow nil
  1330. "Options concerning following links in Org-mode."
  1331. :tag "Org Follow Link"
  1332. :group 'org-link)
  1333. (defcustom org-link-translation-function nil
  1334. "Function to translate links with different syntax to Org syntax.
  1335. This can be used to translate links created for example by the Planner
  1336. or emacs-wiki packages to Org syntax.
  1337. The function must accept two parameters, a TYPE containing the link
  1338. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1339. which is everything after the link protocol. It should return a cons
  1340. with possibly modified values of type and path.
  1341. Org contains a function for this, so if you set this variable to
  1342. `org-translate-link-from-planner', you should be able follow many
  1343. links created by planner."
  1344. :group 'org-link-follow
  1345. :type 'function)
  1346. (defcustom org-follow-link-hook nil
  1347. "Hook that is run after a link has been followed."
  1348. :group 'org-link-follow
  1349. :type 'hook)
  1350. (defcustom org-tab-follows-link nil
  1351. "Non-nil means on links TAB will follow the link.
  1352. Needs to be set before org.el is loaded.
  1353. This really should not be used, it does not make sense, and the
  1354. implementation is bad."
  1355. :group 'org-link-follow
  1356. :type 'boolean)
  1357. (defcustom org-return-follows-link nil
  1358. "Non-nil means on links RET will follow the link."
  1359. :group 'org-link-follow
  1360. :type 'boolean)
  1361. (defcustom org-mouse-1-follows-link
  1362. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1363. "Non-nil means mouse-1 on a link will follow the link.
  1364. A longer mouse click will still set point. Does not work on XEmacs.
  1365. Needs to be set before org.el is loaded."
  1366. :group 'org-link-follow
  1367. :type 'boolean)
  1368. (defcustom org-mark-ring-length 4
  1369. "Number of different positions to be recorded in the ring.
  1370. Changing this requires a restart of Emacs to work correctly."
  1371. :group 'org-link-follow
  1372. :type 'integer)
  1373. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1374. "Non-nil means internal links in Org files must exactly match a headline.
  1375. When nil, the link search tries to match a phrase with all words
  1376. in the search text."
  1377. :group 'org-link-follow
  1378. :version "24.1"
  1379. :type '(choice
  1380. (const :tag "Use fuzzy text search" nil)
  1381. (const :tag "Match only exact headline" t)
  1382. (const :tag "Match exact headline or query to create it"
  1383. query-to-create)))
  1384. (defcustom org-link-frame-setup
  1385. '((vm . vm-visit-folder-other-frame)
  1386. (vm-imap . vm-visit-imap-folder-other-frame)
  1387. (gnus . org-gnus-no-new-news)
  1388. (file . find-file-other-window)
  1389. (wl . wl-other-frame))
  1390. "Setup the frame configuration for following links.
  1391. When following a link with Emacs, it may often be useful to display
  1392. this link in another window or frame. This variable can be used to
  1393. set this up for the different types of links.
  1394. For VM, use any of
  1395. `vm-visit-folder'
  1396. `vm-visit-folder-other-window'
  1397. `vm-visit-folder-other-frame'
  1398. For Gnus, use any of
  1399. `gnus'
  1400. `gnus-other-frame'
  1401. `org-gnus-no-new-news'
  1402. For FILE, use any of
  1403. `find-file'
  1404. `find-file-other-window'
  1405. `find-file-other-frame'
  1406. For Wanderlust use any of
  1407. `wl'
  1408. `wl-other-frame'
  1409. For the calendar, use the variable `calendar-setup'.
  1410. For BBDB, it is currently only possible to display the matches in
  1411. another window."
  1412. :group 'org-link-follow
  1413. :type '(list
  1414. (cons (const vm)
  1415. (choice
  1416. (const vm-visit-folder)
  1417. (const vm-visit-folder-other-window)
  1418. (const vm-visit-folder-other-frame)))
  1419. (cons (const gnus)
  1420. (choice
  1421. (const gnus)
  1422. (const gnus-other-frame)
  1423. (const org-gnus-no-new-news)))
  1424. (cons (const file)
  1425. (choice
  1426. (const find-file)
  1427. (const find-file-other-window)
  1428. (const find-file-other-frame)))
  1429. (cons (const wl)
  1430. (choice
  1431. (const wl)
  1432. (const wl-other-frame)))))
  1433. (defcustom org-display-internal-link-with-indirect-buffer nil
  1434. "Non-nil means use indirect buffer to display infile links.
  1435. Activating internal links (from one location in a file to another location
  1436. in the same file) normally just jumps to the location. When the link is
  1437. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1438. is displayed in
  1439. another window. When this option is set, the other window actually displays
  1440. an indirect buffer clone of the current buffer, to avoid any visibility
  1441. changes to the current buffer."
  1442. :group 'org-link-follow
  1443. :type 'boolean)
  1444. (defcustom org-open-non-existing-files nil
  1445. "Non-nil means `org-open-file' will open non-existing files.
  1446. When nil, an error will be generated.
  1447. This variable applies only to external applications because they
  1448. might choke on non-existing files. If the link is to a file that
  1449. will be opened in Emacs, the variable is ignored."
  1450. :group 'org-link-follow
  1451. :type 'boolean)
  1452. (defcustom org-open-directory-means-index-dot-org nil
  1453. "Non-nil means a link to a directory really means to index.org.
  1454. When nil, following a directory link will run dired or open a finder/explorer
  1455. window on that directory."
  1456. :group 'org-link-follow
  1457. :type 'boolean)
  1458. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1459. "Function and arguments to call for following mailto links.
  1460. This is a list with the first element being a Lisp function, and the
  1461. remaining elements being arguments to the function. In string arguments,
  1462. %a will be replaced by the address, and %s will be replaced by the subject
  1463. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1464. :group 'org-link-follow
  1465. :type '(choice
  1466. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1467. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1468. (const :tag "message-mail" (message-mail "%a" "%s"))
  1469. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1470. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1471. "Non-nil means ask for confirmation before executing shell links.
  1472. Shell links can be dangerous: just think about a link
  1473. [[shell:rm -rf ~/*][Google Search]]
  1474. This link would show up in your Org-mode document as \"Google Search\",
  1475. but really it would remove your entire home directory.
  1476. Therefore we advise against setting this variable to nil.
  1477. Just change it to `y-or-n-p' if you want to confirm with a
  1478. single keystroke rather than having to type \"yes\"."
  1479. :group 'org-link-follow
  1480. :type '(choice
  1481. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1482. (const :tag "with y-or-n (faster)" y-or-n-p)
  1483. (const :tag "no confirmation (dangerous)" nil)))
  1484. (put 'org-confirm-shell-link-function
  1485. 'safe-local-variable
  1486. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1487. (defcustom org-confirm-shell-link-not-regexp ""
  1488. "A regexp to skip confirmation for shell links."
  1489. :group 'org-link-follow
  1490. :version "24.1"
  1491. :type 'regexp)
  1492. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1493. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1494. Elisp links can be dangerous: just think about a link
  1495. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1496. This link would show up in your Org-mode document as \"Google Search\",
  1497. but really it would remove your entire home directory.
  1498. Therefore we advise against setting this variable to nil.
  1499. Just change it to `y-or-n-p' if you want to confirm with a
  1500. single keystroke rather than having to type \"yes\"."
  1501. :group 'org-link-follow
  1502. :type '(choice
  1503. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1504. (const :tag "with y-or-n (faster)" y-or-n-p)
  1505. (const :tag "no confirmation (dangerous)" nil)))
  1506. (put 'org-confirm-shell-link-function
  1507. 'safe-local-variable
  1508. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1509. (defcustom org-confirm-elisp-link-not-regexp ""
  1510. "A regexp to skip confirmation for Elisp links."
  1511. :group 'org-link-follow
  1512. :version "24.1"
  1513. :type 'regexp)
  1514. (defconst org-file-apps-defaults-gnu
  1515. '((remote . emacs)
  1516. (system . mailcap)
  1517. (t . mailcap))
  1518. "Default file applications on a UNIX or GNU/Linux system.
  1519. See `org-file-apps'.")
  1520. (defconst org-file-apps-defaults-macosx
  1521. '((remote . emacs)
  1522. (t . "open %s")
  1523. (system . "open %s")
  1524. ("ps.gz" . "gv %s")
  1525. ("eps.gz" . "gv %s")
  1526. ("dvi" . "xdvi %s")
  1527. ("fig" . "xfig %s"))
  1528. "Default file applications on a MacOS X system.
  1529. The system \"open\" is known as a default, but we use X11 applications
  1530. for some files for which the OS does not have a good default.
  1531. See `org-file-apps'.")
  1532. (defconst org-file-apps-defaults-windowsnt
  1533. (list
  1534. '(remote . emacs)
  1535. (cons t
  1536. (list (if (featurep 'xemacs)
  1537. 'mswindows-shell-execute
  1538. 'w32-shell-execute)
  1539. "open" 'file))
  1540. (cons 'system
  1541. (list (if (featurep 'xemacs)
  1542. 'mswindows-shell-execute
  1543. 'w32-shell-execute)
  1544. "open" 'file)))
  1545. "Default file applications on a Windows NT system.
  1546. The system \"open\" is used for most files.
  1547. See `org-file-apps'.")
  1548. (defcustom org-file-apps
  1549. '(
  1550. (auto-mode . emacs)
  1551. ("\\.mm\\'" . default)
  1552. ("\\.x?html?\\'" . default)
  1553. ("\\.pdf\\'" . default)
  1554. )
  1555. "External applications for opening `file:path' items in a document.
  1556. Org-mode uses system defaults for different file types, but
  1557. you can use this variable to set the application for a given file
  1558. extension. The entries in this list are cons cells where the car identifies
  1559. files and the cdr the corresponding command. Possible values for the
  1560. file identifier are
  1561. \"string\" A string as a file identifier can be interpreted in different
  1562. ways, depending on its contents:
  1563. - Alphanumeric characters only:
  1564. Match links with this file extension.
  1565. Example: (\"pdf\" . \"evince %s\")
  1566. to open PDFs with evince.
  1567. - Regular expression: Match links where the
  1568. filename matches the regexp. If you want to
  1569. use groups here, use shy groups.
  1570. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1571. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1572. to open *.html and *.xhtml with firefox.
  1573. - Regular expression which contains (non-shy) groups:
  1574. Match links where the whole link, including \"::\", and
  1575. anything after that, matches the regexp.
  1576. In a custom command string, %1, %2, etc. are replaced with
  1577. the parts of the link that were matched by the groups.
  1578. For backwards compatibility, if a command string is given
  1579. that does not use any of the group matches, this case is
  1580. handled identically to the second one (i.e. match against
  1581. file name only).
  1582. In a custom lisp form, you can access the group matches with
  1583. (match-string n link).
  1584. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1585. to open [[file:document.pdf::5]] with evince at page 5.
  1586. `directory' Matches a directory
  1587. `remote' Matches a remote file, accessible through tramp or efs.
  1588. Remote files most likely should be visited through Emacs
  1589. because external applications cannot handle such paths.
  1590. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1591. so all files Emacs knows how to handle. Using this with
  1592. command `emacs' will open most files in Emacs. Beware that this
  1593. will also open html files inside Emacs, unless you add
  1594. (\"html\" . default) to the list as well.
  1595. t Default for files not matched by any of the other options.
  1596. `system' The system command to open files, like `open' on Windows
  1597. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1598. that will be selected if you call `C-c C-o' with a double
  1599. \\[universal-argument] \\[universal-argument] prefix.
  1600. Possible values for the command are:
  1601. `emacs' The file will be visited by the current Emacs process.
  1602. `default' Use the default application for this file type, which is the
  1603. association for t in the list, most likely in the system-specific
  1604. part.
  1605. This can be used to overrule an unwanted setting in the
  1606. system-specific variable.
  1607. `system' Use the system command for opening files, like \"open\".
  1608. This command is specified by the entry whose car is `system'.
  1609. Most likely, the system-specific version of this variable
  1610. does define this command, but you can overrule/replace it
  1611. here.
  1612. string A command to be executed by a shell; %s will be replaced
  1613. by the path to the file.
  1614. sexp A Lisp form which will be evaluated. The file path will
  1615. be available in the Lisp variable `file'.
  1616. For more examples, see the system specific constants
  1617. `org-file-apps-defaults-macosx'
  1618. `org-file-apps-defaults-windowsnt'
  1619. `org-file-apps-defaults-gnu'."
  1620. :group 'org-link-follow
  1621. :type '(repeat
  1622. (cons (choice :value ""
  1623. (string :tag "Extension")
  1624. (const :tag "System command to open files" system)
  1625. (const :tag "Default for unrecognized files" t)
  1626. (const :tag "Remote file" remote)
  1627. (const :tag "Links to a directory" directory)
  1628. (const :tag "Any files that have Emacs modes"
  1629. auto-mode))
  1630. (choice :value ""
  1631. (const :tag "Visit with Emacs" emacs)
  1632. (const :tag "Use default" default)
  1633. (const :tag "Use the system command" system)
  1634. (string :tag "Command")
  1635. (sexp :tag "Lisp form")))))
  1636. (defcustom org-doi-server-url "http://dx.doi.org/"
  1637. "The URL of the DOI server."
  1638. :type 'string
  1639. :version "24.3"
  1640. :group 'org-link-follow)
  1641. (defgroup org-refile nil
  1642. "Options concerning refiling entries in Org-mode."
  1643. :tag "Org Refile"
  1644. :group 'org)
  1645. (defcustom org-directory "~/org"
  1646. "Directory with org files.
  1647. This is just a default location to look for Org files. There is no need
  1648. at all to put your files into this directory. It is only used in the
  1649. following situations:
  1650. 1. When a capture template specifies a target file that is not an
  1651. absolute path. The path will then be interpreted relative to
  1652. `org-directory'
  1653. 2. When a capture note is filed away in an interactive way (when exiting the
  1654. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1655. with `org-directory' as the default path."
  1656. :group 'org-refile
  1657. :group 'org-remember
  1658. :group 'org-capture
  1659. :type 'directory)
  1660. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1661. "Default target for storing notes.
  1662. Used as a fall back file for org-remember.el and org-capture.el, for
  1663. templates that do not specify a target file."
  1664. :group 'org-refile
  1665. :group 'org-remember
  1666. :group 'org-capture
  1667. :type '(choice
  1668. (const :tag "Default from remember-data-file" nil)
  1669. file))
  1670. (defcustom org-goto-interface 'outline
  1671. "The default interface to be used for `org-goto'.
  1672. Allowed values are:
  1673. outline The interface shows an outline of the relevant file
  1674. and the correct heading is found by moving through
  1675. the outline or by searching with incremental search.
  1676. outline-path-completion Headlines in the current buffer are offered via
  1677. completion. This is the interface also used by
  1678. the refile command."
  1679. :group 'org-refile
  1680. :type '(choice
  1681. (const :tag "Outline" outline)
  1682. (const :tag "Outline-path-completion" outline-path-completion)))
  1683. (defcustom org-goto-max-level 5
  1684. "Maximum target level when running `org-goto' with refile interface."
  1685. :group 'org-refile
  1686. :type 'integer)
  1687. (defcustom org-reverse-note-order nil
  1688. "Non-nil means store new notes at the beginning of a file or entry.
  1689. When nil, new notes will be filed to the end of a file or entry.
  1690. This can also be a list with cons cells of regular expressions that
  1691. are matched against file names, and values."
  1692. :group 'org-remember
  1693. :group 'org-capture
  1694. :group 'org-refile
  1695. :type '(choice
  1696. (const :tag "Reverse always" t)
  1697. (const :tag "Reverse never" nil)
  1698. (repeat :tag "By file name regexp"
  1699. (cons regexp boolean))))
  1700. (defcustom org-log-refile nil
  1701. "Information to record when a task is refiled.
  1702. Possible values are:
  1703. nil Don't add anything
  1704. time Add a time stamp to the task
  1705. note Prompt for a note and add it with template `org-log-note-headings'
  1706. This option can also be set with on a per-file-basis with
  1707. #+STARTUP: nologrefile
  1708. #+STARTUP: logrefile
  1709. #+STARTUP: lognoterefile
  1710. You can have local logging settings for a subtree by setting the LOGGING
  1711. property to one or more of these keywords.
  1712. When bulk-refiling from the agenda, the value `note' is forbidden and
  1713. will temporarily be changed to `time'."
  1714. :group 'org-refile
  1715. :group 'org-progress
  1716. :version "24.1"
  1717. :type '(choice
  1718. (const :tag "No logging" nil)
  1719. (const :tag "Record timestamp" time)
  1720. (const :tag "Record timestamp with note." note)))
  1721. (defcustom org-refile-targets nil
  1722. "Targets for refiling entries with \\[org-refile].
  1723. This is a list of cons cells. Each cell contains:
  1724. - a specification of the files to be considered, either a list of files,
  1725. or a symbol whose function or variable value will be used to retrieve
  1726. a file name or a list of file names. If you use `org-agenda-files' for
  1727. that, all agenda files will be scanned for targets. Nil means consider
  1728. headings in the current buffer.
  1729. - A specification of how to find candidate refile targets. This may be
  1730. any of:
  1731. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1732. This tag has to be present in all target headlines, inheritance will
  1733. not be considered.
  1734. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1735. todo keyword.
  1736. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1737. headlines that are refiling targets.
  1738. - a cons cell (:level . N). Any headline of level N is considered a target.
  1739. Note that, when `org-odd-levels-only' is set, level corresponds to
  1740. order in hierarchy, not to the number of stars.
  1741. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1742. Note that, when `org-odd-levels-only' is set, level corresponds to
  1743. order in hierarchy, not to the number of stars.
  1744. Each element of this list generates a set of possible targets.
  1745. The union of these sets is presented (with completion) to
  1746. the user by `org-refile'.
  1747. You can set the variable `org-refile-target-verify-function' to a function
  1748. to verify each headline found by the simple criteria above.
  1749. When this variable is nil, all top-level headlines in the current buffer
  1750. are used, equivalent to the value `((nil . (:level . 1))'."
  1751. :group 'org-refile
  1752. :type '(repeat
  1753. (cons
  1754. (choice :value org-agenda-files
  1755. (const :tag "All agenda files" org-agenda-files)
  1756. (const :tag "Current buffer" nil)
  1757. (function) (variable) (file))
  1758. (choice :tag "Identify target headline by"
  1759. (cons :tag "Specific tag" (const :value :tag) (string))
  1760. (cons :tag "TODO keyword" (const :value :todo) (string))
  1761. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1762. (cons :tag "Level number" (const :value :level) (integer))
  1763. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1764. (defcustom org-refile-target-verify-function nil
  1765. "Function to verify if the headline at point should be a refile target.
  1766. The function will be called without arguments, with point at the
  1767. beginning of the headline. It should return t and leave point
  1768. where it is if the headline is a valid target for refiling.
  1769. If the target should not be selected, the function must return nil.
  1770. In addition to this, it may move point to a place from where the search
  1771. should be continued. For example, the function may decide that the entire
  1772. subtree of the current entry should be excluded and move point to the end
  1773. of the subtree."
  1774. :group 'org-refile
  1775. :type 'function)
  1776. (defcustom org-refile-use-cache nil
  1777. "Non-nil means cache refile targets to speed up the process.
  1778. The cache for a particular file will be updated automatically when
  1779. the buffer has been killed, or when any of the marker used for flagging
  1780. refile targets no longer points at a live buffer.
  1781. If you have added new entries to a buffer that might themselves be targets,
  1782. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1783. find that easier, `C-u C-u C-u C-c C-w'."
  1784. :group 'org-refile
  1785. :version "24.1"
  1786. :type 'boolean)
  1787. (defcustom org-refile-use-outline-path nil
  1788. "Non-nil means provide refile targets as paths.
  1789. So a level 3 headline will be available as level1/level2/level3.
  1790. When the value is `file', also include the file name (without directory)
  1791. into the path. In this case, you can also stop the completion after
  1792. the file name, to get entries inserted as top level in the file.
  1793. When `full-file-path', include the full file path."
  1794. :group 'org-refile
  1795. :type '(choice
  1796. (const :tag "Not" nil)
  1797. (const :tag "Yes" t)
  1798. (const :tag "Start with file name" file)
  1799. (const :tag "Start with full file path" full-file-path)))
  1800. (defcustom org-outline-path-complete-in-steps t
  1801. "Non-nil means complete the outline path in hierarchical steps.
  1802. When Org-mode uses the refile interface to select an outline path
  1803. \(see variable `org-refile-use-outline-path'), the completion of
  1804. the path can be done is a single go, or if can be done in steps down
  1805. the headline hierarchy. Going in steps is probably the best if you
  1806. do not use a special completion package like `ido' or `icicles'.
  1807. However, when using these packages, going in one step can be very
  1808. fast, while still showing the whole path to the entry."
  1809. :group 'org-refile
  1810. :type 'boolean)
  1811. (defcustom org-refile-allow-creating-parent-nodes nil
  1812. "Non-nil means allow to create new nodes as refile targets.
  1813. New nodes are then created by adding \"/new node name\" to the completion
  1814. of an existing node. When the value of this variable is `confirm',
  1815. new node creation must be confirmed by the user (recommended)
  1816. When nil, the completion must match an existing entry.
  1817. Note that, if the new heading is not seen by the criteria
  1818. listed in `org-refile-targets', multiple instances of the same
  1819. heading would be created by trying again to file under the new
  1820. heading."
  1821. :group 'org-refile
  1822. :type '(choice
  1823. (const :tag "Never" nil)
  1824. (const :tag "Always" t)
  1825. (const :tag "Prompt for confirmation" confirm)))
  1826. (defcustom org-refile-active-region-within-subtree nil
  1827. "Non-nil means also refile active region within a subtree.
  1828. By default `org-refile' doesn't allow refiling regions if they
  1829. don't contain a set of subtrees, but it might be convenient to
  1830. do so sometimes: in that case, the first line of the region is
  1831. converted to a headline before refiling."
  1832. :group 'org-refile
  1833. :version "24.1"
  1834. :type 'boolean)
  1835. (defgroup org-todo nil
  1836. "Options concerning TODO items in Org-mode."
  1837. :tag "Org TODO"
  1838. :group 'org)
  1839. (defgroup org-progress nil
  1840. "Options concerning Progress logging in Org-mode."
  1841. :tag "Org Progress"
  1842. :group 'org-time)
  1843. (defvar org-todo-interpretation-widgets
  1844. '((:tag "Sequence (cycling hits every state)" sequence)
  1845. (:tag "Type (cycling directly to DONE)" type))
  1846. "The available interpretation symbols for customizing `org-todo-keywords'.
  1847. Interested libraries should add to this list.")
  1848. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1849. "List of TODO entry keyword sequences and their interpretation.
  1850. \\<org-mode-map>This is a list of sequences.
  1851. Each sequence starts with a symbol, either `sequence' or `type',
  1852. indicating if the keywords should be interpreted as a sequence of
  1853. action steps, or as different types of TODO items. The first
  1854. keywords are states requiring action - these states will select a headline
  1855. for inclusion into the global TODO list Org-mode produces. If one of
  1856. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1857. signify that no further action is necessary. If \"|\" is not found,
  1858. the last keyword is treated as the only DONE state of the sequence.
  1859. The command \\[org-todo] cycles an entry through these states, and one
  1860. additional state where no keyword is present. For details about this
  1861. cycling, see the manual.
  1862. TODO keywords and interpretation can also be set on a per-file basis with
  1863. the special #+SEQ_TODO and #+TYP_TODO lines.
  1864. Each keyword can optionally specify a character for fast state selection
  1865. \(in combination with the variable `org-use-fast-todo-selection')
  1866. and specifiers for state change logging, using the same syntax that
  1867. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1868. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1869. indicates to record a time stamp each time this state is selected.
  1870. Each keyword may also specify if a timestamp or a note should be
  1871. recorded when entering or leaving the state, by adding additional
  1872. characters in the parenthesis after the keyword. This looks like this:
  1873. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1874. record only the time of the state change. With X and Y being either
  1875. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1876. Y when leaving the state if and only if the *target* state does not
  1877. define X. You may omit any of the fast-selection key or X or /Y,
  1878. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1879. For backward compatibility, this variable may also be just a list
  1880. of keywords. In this case the interpretation (sequence or type) will be
  1881. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1882. :group 'org-todo
  1883. :group 'org-keywords
  1884. :type '(choice
  1885. (repeat :tag "Old syntax, just keywords"
  1886. (string :tag "Keyword"))
  1887. (repeat :tag "New syntax"
  1888. (cons
  1889. (choice
  1890. :tag "Interpretation"
  1891. ;;Quick and dirty way to see
  1892. ;;`org-todo-interpretations'. This takes the
  1893. ;;place of item arguments
  1894. :convert-widget
  1895. (lambda (widget)
  1896. (widget-put widget
  1897. :args (mapcar
  1898. #'(lambda (x)
  1899. (widget-convert
  1900. (cons 'const x)))
  1901. org-todo-interpretation-widgets))
  1902. widget))
  1903. (repeat
  1904. (string :tag "Keyword"))))))
  1905. (defvar org-todo-keywords-1 nil
  1906. "All TODO and DONE keywords active in a buffer.")
  1907. (make-variable-buffer-local 'org-todo-keywords-1)
  1908. (defvar org-todo-keywords-for-agenda nil)
  1909. (defvar org-done-keywords-for-agenda nil)
  1910. (defvar org-drawers-for-agenda nil)
  1911. (defvar org-todo-keyword-alist-for-agenda nil)
  1912. (defvar org-tag-alist-for-agenda nil)
  1913. (defvar org-agenda-contributing-files nil)
  1914. (defvar org-not-done-keywords nil)
  1915. (make-variable-buffer-local 'org-not-done-keywords)
  1916. (defvar org-done-keywords nil)
  1917. (make-variable-buffer-local 'org-done-keywords)
  1918. (defvar org-todo-heads nil)
  1919. (make-variable-buffer-local 'org-todo-heads)
  1920. (defvar org-todo-sets nil)
  1921. (make-variable-buffer-local 'org-todo-sets)
  1922. (defvar org-todo-log-states nil)
  1923. (make-variable-buffer-local 'org-todo-log-states)
  1924. (defvar org-todo-kwd-alist nil)
  1925. (make-variable-buffer-local 'org-todo-kwd-alist)
  1926. (defvar org-todo-key-alist nil)
  1927. (make-variable-buffer-local 'org-todo-key-alist)
  1928. (defvar org-todo-key-trigger nil)
  1929. (make-variable-buffer-local 'org-todo-key-trigger)
  1930. (defcustom org-todo-interpretation 'sequence
  1931. "Controls how TODO keywords are interpreted.
  1932. This variable is in principle obsolete and is only used for
  1933. backward compatibility, if the interpretation of todo keywords is
  1934. not given already in `org-todo-keywords'. See that variable for
  1935. more information."
  1936. :group 'org-todo
  1937. :group 'org-keywords
  1938. :type '(choice (const sequence)
  1939. (const type)))
  1940. (defcustom org-use-fast-todo-selection t
  1941. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1942. This variable describes if and under what circumstances the cycling
  1943. mechanism for TODO keywords will be replaced by a single-key, direct
  1944. selection scheme.
  1945. When nil, fast selection is never used.
  1946. When the symbol `prefix', it will be used when `org-todo' is called
  1947. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  1948. `C-u t' in an agenda buffer.
  1949. When t, fast selection is used by default. In this case, the prefix
  1950. argument forces cycling instead.
  1951. In all cases, the special interface is only used if access keys have
  1952. actually been assigned by the user, i.e. if keywords in the configuration
  1953. are followed by a letter in parenthesis, like TODO(t)."
  1954. :group 'org-todo
  1955. :type '(choice
  1956. (const :tag "Never" nil)
  1957. (const :tag "By default" t)
  1958. (const :tag "Only with C-u C-c C-t" prefix)))
  1959. (defcustom org-provide-todo-statistics t
  1960. "Non-nil means update todo statistics after insert and toggle.
  1961. ALL-HEADLINES means update todo statistics by including headlines
  1962. with no TODO keyword as well, counting them as not done.
  1963. A list of TODO keywords means the same, but skip keywords that are
  1964. not in this list.
  1965. When this is set, todo statistics is updated in the parent of the
  1966. current entry each time a todo state is changed."
  1967. :group 'org-todo
  1968. :type '(choice
  1969. (const :tag "Yes, only for TODO entries" t)
  1970. (const :tag "Yes, including all entries" 'all-headlines)
  1971. (repeat :tag "Yes, for TODOs in this list"
  1972. (string :tag "TODO keyword"))
  1973. (other :tag "No TODO statistics" nil)))
  1974. (defcustom org-hierarchical-todo-statistics t
  1975. "Non-nil means TODO statistics covers just direct children.
  1976. When nil, all entries in the subtree are considered.
  1977. This has only an effect if `org-provide-todo-statistics' is set.
  1978. To set this to nil for only a single subtree, use a COOKIE_DATA
  1979. property and include the word \"recursive\" into the value."
  1980. :group 'org-todo
  1981. :type 'boolean)
  1982. (defcustom org-after-todo-state-change-hook nil
  1983. "Hook which is run after the state of a TODO item was changed.
  1984. The new state (a string with a TODO keyword, or nil) is available in the
  1985. Lisp variable `org-state'."
  1986. :group 'org-todo
  1987. :type 'hook)
  1988. (defvar org-blocker-hook nil
  1989. "Hook for functions that are allowed to block a state change.
  1990. Functions in this hook should not modify the buffer.
  1991. Each function gets as its single argument a property list,
  1992. see `org-trigger-hook' for more information about this list.
  1993. If any of the functions in this hook returns nil, the state change
  1994. is blocked.")
  1995. (defvar org-trigger-hook nil
  1996. "Hook for functions that are triggered by a state change.
  1997. Each function gets as its single argument a property list with at
  1998. least the following elements:
  1999. (:type type-of-change :position pos-at-entry-start
  2000. :from old-state :to new-state)
  2001. Depending on the type, more properties may be present.
  2002. This mechanism is currently implemented for:
  2003. TODO state changes
  2004. ------------------
  2005. :type todo-state-change
  2006. :from previous state (keyword as a string), or nil, or a symbol
  2007. 'todo' or 'done', to indicate the general type of state.
  2008. :to new state, like in :from")
  2009. (defcustom org-enforce-todo-dependencies nil
  2010. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2011. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2012. be blocked if any prior sibling is not yet done.
  2013. Finally, if the parent is blocked because of ordered siblings of its own,
  2014. the child will also be blocked."
  2015. :set (lambda (var val)
  2016. (set var val)
  2017. (if val
  2018. (add-hook 'org-blocker-hook
  2019. 'org-block-todo-from-children-or-siblings-or-parent)
  2020. (remove-hook 'org-blocker-hook
  2021. 'org-block-todo-from-children-or-siblings-or-parent)))
  2022. :group 'org-todo
  2023. :type 'boolean)
  2024. (defcustom org-enforce-todo-checkbox-dependencies nil
  2025. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2026. When this is nil, checkboxes have no influence on switching TODO states.
  2027. When non-nil, you first need to check off all check boxes before the TODO
  2028. entry can be switched to DONE.
  2029. This variable needs to be set before org.el is loaded, and you need to
  2030. restart Emacs after a change to make the change effective. The only way
  2031. to change is while Emacs is running is through the customize interface."
  2032. :set (lambda (var val)
  2033. (set var val)
  2034. (if val
  2035. (add-hook 'org-blocker-hook
  2036. 'org-block-todo-from-checkboxes)
  2037. (remove-hook 'org-blocker-hook
  2038. 'org-block-todo-from-checkboxes)))
  2039. :group 'org-todo
  2040. :type 'boolean)
  2041. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2042. "Non-nil means inserting a TODO heading is treated as state change.
  2043. So when the command \\[org-insert-todo-heading] is used, state change
  2044. logging will apply if appropriate. When nil, the new TODO item will
  2045. be inserted directly, and no logging will take place."
  2046. :group 'org-todo
  2047. :type 'boolean)
  2048. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2049. "Non-nil means switching TODO states with S-cursor counts as state change.
  2050. This is the default behavior. However, setting this to nil allows a
  2051. convenient way to select a TODO state and bypass any logging associated
  2052. with that."
  2053. :group 'org-todo
  2054. :type 'boolean)
  2055. (defcustom org-todo-state-tags-triggers nil
  2056. "Tag changes that should be triggered by TODO state changes.
  2057. This is a list. Each entry is
  2058. (state-change (tag . flag) .......)
  2059. State-change can be a string with a state, and empty string to indicate the
  2060. state that has no TODO keyword, or it can be one of the symbols `todo'
  2061. or `done', meaning any not-done or done state, respectively."
  2062. :group 'org-todo
  2063. :group 'org-tags
  2064. :type '(repeat
  2065. (cons (choice :tag "When changing to"
  2066. (const :tag "Not-done state" todo)
  2067. (const :tag "Done state" done)
  2068. (string :tag "State"))
  2069. (repeat
  2070. (cons :tag "Tag action"
  2071. (string :tag "Tag")
  2072. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2073. (defcustom org-log-done nil
  2074. "Information to record when a task moves to the DONE state.
  2075. Possible values are:
  2076. nil Don't add anything, just change the keyword
  2077. time Add a time stamp to the task
  2078. note Prompt for a note and add it with template `org-log-note-headings'
  2079. This option can also be set with on a per-file-basis with
  2080. #+STARTUP: nologdone
  2081. #+STARTUP: logdone
  2082. #+STARTUP: lognotedone
  2083. You can have local logging settings for a subtree by setting the LOGGING
  2084. property to one or more of these keywords."
  2085. :group 'org-todo
  2086. :group 'org-progress
  2087. :type '(choice
  2088. (const :tag "No logging" nil)
  2089. (const :tag "Record CLOSED timestamp" time)
  2090. (const :tag "Record CLOSED timestamp with note." note)))
  2091. ;; Normalize old uses of org-log-done.
  2092. (cond
  2093. ((eq org-log-done t) (setq org-log-done 'time))
  2094. ((and (listp org-log-done) (memq 'done org-log-done))
  2095. (setq org-log-done 'note)))
  2096. (defcustom org-log-reschedule nil
  2097. "Information to record when the scheduling date of a tasks is modified.
  2098. Possible values are:
  2099. nil Don't add anything, just change the date
  2100. time Add a time stamp to the task
  2101. note Prompt for a note and add it with template `org-log-note-headings'
  2102. This option can also be set with on a per-file-basis with
  2103. #+STARTUP: nologreschedule
  2104. #+STARTUP: logreschedule
  2105. #+STARTUP: lognotereschedule"
  2106. :group 'org-todo
  2107. :group 'org-progress
  2108. :type '(choice
  2109. (const :tag "No logging" nil)
  2110. (const :tag "Record timestamp" time)
  2111. (const :tag "Record timestamp with note." note)))
  2112. (defcustom org-log-redeadline nil
  2113. "Information to record when the deadline date of a tasks is modified.
  2114. Possible values are:
  2115. nil Don't add anything, just change the date
  2116. time Add a time stamp to the task
  2117. note Prompt for a note and add it with template `org-log-note-headings'
  2118. This option can also be set with on a per-file-basis with
  2119. #+STARTUP: nologredeadline
  2120. #+STARTUP: logredeadline
  2121. #+STARTUP: lognoteredeadline
  2122. You can have local logging settings for a subtree by setting the LOGGING
  2123. property to one or more of these keywords."
  2124. :group 'org-todo
  2125. :group 'org-progress
  2126. :type '(choice
  2127. (const :tag "No logging" nil)
  2128. (const :tag "Record timestamp" time)
  2129. (const :tag "Record timestamp with note." note)))
  2130. (defcustom org-log-note-clock-out nil
  2131. "Non-nil means record a note when clocking out of an item.
  2132. This can also be configured on a per-file basis by adding one of
  2133. the following lines anywhere in the buffer:
  2134. #+STARTUP: lognoteclock-out
  2135. #+STARTUP: nolognoteclock-out"
  2136. :group 'org-todo
  2137. :group 'org-progress
  2138. :type 'boolean)
  2139. (defcustom org-log-done-with-time t
  2140. "Non-nil means the CLOSED time stamp will contain date and time.
  2141. When nil, only the date will be recorded."
  2142. :group 'org-progress
  2143. :type 'boolean)
  2144. (defcustom org-log-note-headings
  2145. '((done . "CLOSING NOTE %t")
  2146. (state . "State %-12s from %-12S %t")
  2147. (note . "Note taken on %t")
  2148. (reschedule . "Rescheduled from %S on %t")
  2149. (delschedule . "Not scheduled, was %S on %t")
  2150. (redeadline . "New deadline from %S on %t")
  2151. (deldeadline . "Removed deadline, was %S on %t")
  2152. (refile . "Refiled on %t")
  2153. (clock-out . ""))
  2154. "Headings for notes added to entries.
  2155. The value is an alist, with the car being a symbol indicating the note
  2156. context, and the cdr is the heading to be used. The heading may also be the
  2157. empty string.
  2158. %t in the heading will be replaced by a time stamp.
  2159. %T will be an active time stamp instead the default inactive one
  2160. %d will be replaced by a short-format time stamp.
  2161. %D will be replaced by an active short-format time stamp.
  2162. %s will be replaced by the new TODO state, in double quotes.
  2163. %S will be replaced by the old TODO state, in double quotes.
  2164. %u will be replaced by the user name.
  2165. %U will be replaced by the full user name.
  2166. In fact, it is not a good idea to change the `state' entry, because
  2167. agenda log mode depends on the format of these entries."
  2168. :group 'org-todo
  2169. :group 'org-progress
  2170. :type '(list :greedy t
  2171. (cons (const :tag "Heading when closing an item" done) string)
  2172. (cons (const :tag
  2173. "Heading when changing todo state (todo sequence only)"
  2174. state) string)
  2175. (cons (const :tag "Heading when just taking a note" note) string)
  2176. (cons (const :tag "Heading when clocking out" clock-out) string)
  2177. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2178. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2179. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2180. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2181. (cons (const :tag "Heading when refiling" refile) string)))
  2182. (unless (assq 'note org-log-note-headings)
  2183. (push '(note . "%t") org-log-note-headings))
  2184. (defcustom org-log-into-drawer nil
  2185. "Non-nil means insert state change notes and time stamps into a drawer.
  2186. When nil, state changes notes will be inserted after the headline and
  2187. any scheduling and clock lines, but not inside a drawer.
  2188. The value of this variable should be the name of the drawer to use.
  2189. LOGBOOK is proposed as the default drawer for this purpose, you can
  2190. also set this to a string to define the drawer of your choice.
  2191. A value of t is also allowed, representing \"LOGBOOK\".
  2192. A value of t or nil can also be set with on a per-file-basis with
  2193. #+STARTUP: logdrawer
  2194. #+STARTUP: nologdrawer
  2195. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2196. will be ignored.
  2197. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2198. a subtree."
  2199. :group 'org-todo
  2200. :group 'org-progress
  2201. :type '(choice
  2202. (const :tag "Not into a drawer" nil)
  2203. (const :tag "LOGBOOK" t)
  2204. (string :tag "Other")))
  2205. (if (fboundp 'defvaralias)
  2206. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2207. (defun org-log-into-drawer ()
  2208. "Return the value of `org-log-into-drawer', but let properties overrule.
  2209. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2210. used instead of the default value."
  2211. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2212. (cond
  2213. ((not p) org-log-into-drawer)
  2214. ((equal p "nil") nil)
  2215. ((equal p "t") "LOGBOOK")
  2216. (t p))))
  2217. (defcustom org-log-state-notes-insert-after-drawers nil
  2218. "Non-nil means insert state change notes after any drawers in entry.
  2219. Only the drawers that *immediately* follow the headline and the
  2220. deadline/scheduled line are skipped.
  2221. When nil, insert notes right after the heading and perhaps the line
  2222. with deadline/scheduling if present.
  2223. This variable will have no effect if `org-log-into-drawer' is
  2224. set."
  2225. :group 'org-todo
  2226. :group 'org-progress
  2227. :type 'boolean)
  2228. (defcustom org-log-states-order-reversed t
  2229. "Non-nil means the latest state note will be directly after heading.
  2230. When nil, the state change notes will be ordered according to time.
  2231. This option can also be set with on a per-file-basis with
  2232. #+STARTUP: logstatesreversed
  2233. #+STARTUP: nologstatesreversed"
  2234. :group 'org-todo
  2235. :group 'org-progress
  2236. :type 'boolean)
  2237. (defcustom org-todo-repeat-to-state nil
  2238. "The TODO state to which a repeater should return the repeating task.
  2239. By default this is the first task in a TODO sequence, or the previous state
  2240. in a TODO_TYP set. But you can specify another task here.
  2241. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2242. :group 'org-todo
  2243. :version "24.1"
  2244. :type '(choice (const :tag "Head of sequence" nil)
  2245. (string :tag "Specific state")))
  2246. (defcustom org-log-repeat 'time
  2247. "Non-nil means record moving through the DONE state when triggering repeat.
  2248. An auto-repeating task is immediately switched back to TODO when
  2249. marked DONE. If you are not logging state changes (by adding \"@\"
  2250. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2251. record a closing note, there will be no record of the task moving
  2252. through DONE. This variable forces taking a note anyway.
  2253. nil Don't force a record
  2254. time Record a time stamp
  2255. note Prompt for a note and add it with template `org-log-note-headings'
  2256. This option can also be set with on a per-file-basis with
  2257. #+STARTUP: nologrepeat
  2258. #+STARTUP: logrepeat
  2259. #+STARTUP: lognoterepeat
  2260. You can have local logging settings for a subtree by setting the LOGGING
  2261. property to one or more of these keywords."
  2262. :group 'org-todo
  2263. :group 'org-progress
  2264. :type '(choice
  2265. (const :tag "Don't force a record" nil)
  2266. (const :tag "Force recording the DONE state" time)
  2267. (const :tag "Force recording a note with the DONE state" note)))
  2268. (defgroup org-priorities nil
  2269. "Priorities in Org-mode."
  2270. :tag "Org Priorities"
  2271. :group 'org-todo)
  2272. (defcustom org-enable-priority-commands t
  2273. "Non-nil means priority commands are active.
  2274. When nil, these commands will be disabled, so that you never accidentally
  2275. set a priority."
  2276. :group 'org-priorities
  2277. :type 'boolean)
  2278. (defcustom org-highest-priority ?A
  2279. "The highest priority of TODO items. A character like ?A, ?B etc.
  2280. Must have a smaller ASCII number than `org-lowest-priority'."
  2281. :group 'org-priorities
  2282. :type 'character)
  2283. (defcustom org-lowest-priority ?C
  2284. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2285. Must have a larger ASCII number than `org-highest-priority'."
  2286. :group 'org-priorities
  2287. :type 'character)
  2288. (defcustom org-default-priority ?B
  2289. "The default priority of TODO items.
  2290. This is the priority an item gets if no explicit priority is given.
  2291. When starting to cycle on an empty priority the first step in the cycle
  2292. depends on `org-priority-start-cycle-with-default'. The resulting first
  2293. step priority must not exceed the range from `org-highest-priority' to
  2294. `org-lowest-priority' which means that `org-default-priority' has to be
  2295. in this range exclusive or inclusive the range boundaries. Else the
  2296. first step refuses to set the default and the second will fall back
  2297. to (depending on the command used) the highest or lowest priority."
  2298. :group 'org-priorities
  2299. :type 'character)
  2300. (defcustom org-priority-start-cycle-with-default t
  2301. "Non-nil means start with default priority when starting to cycle.
  2302. When this is nil, the first step in the cycle will be (depending on the
  2303. command used) one higher or lower than the default priority.
  2304. See also `org-default-priority'."
  2305. :group 'org-priorities
  2306. :type 'boolean)
  2307. (defcustom org-get-priority-function nil
  2308. "Function to extract the priority from a string.
  2309. The string is normally the headline. If this is nil Org computes the
  2310. priority from the priority cookie like [#A] in the headline. It returns
  2311. an integer, increasing by 1000 for each priority level.
  2312. The user can set a different function here, which should take a string
  2313. as an argument and return the numeric priority."
  2314. :group 'org-priorities
  2315. :version "24.1"
  2316. :type 'function)
  2317. (defgroup org-time nil
  2318. "Options concerning time stamps and deadlines in Org-mode."
  2319. :tag "Org Time"
  2320. :group 'org)
  2321. (defcustom org-insert-labeled-timestamps-at-point nil
  2322. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2323. When nil, these labeled time stamps are forces into the second line of an
  2324. entry, just after the headline. When scheduling from the global TODO list,
  2325. the time stamp will always be forced into the second line."
  2326. :group 'org-time
  2327. :type 'boolean)
  2328. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2329. "Formats for `format-time-string' which are used for time stamps.
  2330. It is not recommended to change this constant.")
  2331. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2332. "Number of minutes to round time stamps to.
  2333. These are two values, the first applies when first creating a time stamp.
  2334. The second applies when changing it with the commands `S-up' and `S-down'.
  2335. When changing the time stamp, this means that it will change in steps
  2336. of N minutes, as given by the second value.
  2337. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2338. numbers should be factors of 60, so for example 5, 10, 15.
  2339. When this is larger than 1, you can still force an exact time stamp by using
  2340. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2341. and by using a prefix arg to `S-up/down' to specify the exact number
  2342. of minutes to shift."
  2343. :group 'org-time
  2344. :get #'(lambda (var) ; Make sure both elements are there
  2345. (if (integerp (default-value var))
  2346. (list (default-value var) 5)
  2347. (default-value var)))
  2348. :type '(list
  2349. (integer :tag "when inserting times")
  2350. (integer :tag "when modifying times")))
  2351. ;; Normalize old customizations of this variable.
  2352. (when (integerp org-time-stamp-rounding-minutes)
  2353. (setq org-time-stamp-rounding-minutes
  2354. (list org-time-stamp-rounding-minutes
  2355. org-time-stamp-rounding-minutes)))
  2356. (defcustom org-display-custom-times nil
  2357. "Non-nil means overlay custom formats over all time stamps.
  2358. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2359. To turn this on on a per-file basis, insert anywhere in the file:
  2360. #+STARTUP: customtime"
  2361. :group 'org-time
  2362. :set 'set-default
  2363. :type 'sexp)
  2364. (make-variable-buffer-local 'org-display-custom-times)
  2365. (defcustom org-time-stamp-custom-formats
  2366. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2367. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2368. These are overlaid over the default ISO format if the variable
  2369. `org-display-custom-times' is set. Time like %H:%M should be at the
  2370. end of the second format. The custom formats are also honored by export
  2371. commands, if custom time display is turned on at the time of export."
  2372. :group 'org-time
  2373. :type 'sexp)
  2374. (defun org-time-stamp-format (&optional long inactive)
  2375. "Get the right format for a time string."
  2376. (let ((f (if long (cdr org-time-stamp-formats)
  2377. (car org-time-stamp-formats))))
  2378. (if inactive
  2379. (concat "[" (substring f 1 -1) "]")
  2380. f)))
  2381. (defcustom org-time-clocksum-format
  2382. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2383. "The format string used when creating CLOCKSUM lines.
  2384. This is also used when Org mode generates a time duration.
  2385. The value can be a single format string containing two
  2386. %-sequences, which will be filled with the number of hours and
  2387. minutes in that order.
  2388. Alternatively, the value can be a plist associating any of the
  2389. keys :years, :months, :weeks, :days, :hours or :minutes with
  2390. format strings. The time duration is formatted using only the
  2391. time components that are needed and concatenating the results.
  2392. If a time unit in absent, it falls back to the next smallest
  2393. unit.
  2394. The keys :require-years, :require-months, :require-days,
  2395. :require-weeks, :require-hours, :require-minutes are also
  2396. meaningful. A non-nil value for these keys indicates that the
  2397. corresponding time component should always be included, even if
  2398. its value is 0.
  2399. For example,
  2400. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2401. :require-minutes t)
  2402. means durations longer than a day will be expressed in days,
  2403. hours and minutes, and durations less than a day will always be
  2404. expressed in hours and minutes (even for durations less than an
  2405. hour).
  2406. The value
  2407. \(:days \"%dd\" :minutes \"%dm\")
  2408. means durations longer than a day will be expressed in days and
  2409. minutes, and durations less than a day will be expressed entirely
  2410. in minutes (even for durations longer than an hour)."
  2411. :group 'org-time
  2412. :type '(choice (string :tag "Format string")
  2413. (set :tag "Plist"
  2414. (group :inline t (const :tag "Years" :years)
  2415. (string :tag "Format string"))
  2416. (group :inline t
  2417. (const :tag "Always show years" :require-years)
  2418. (const t))
  2419. (group :inline t (const :tag "Months" :months)
  2420. (string :tag "Format string"))
  2421. (group :inline t
  2422. (const :tag "Always show months" :require-months)
  2423. (const t))
  2424. (group :inline t (const :tag "Weeks" :weeks)
  2425. (string :tag "Format string"))
  2426. (group :inline t
  2427. (const :tag "Always show weeks" :require-weeks)
  2428. (const t))
  2429. (group :inline t (const :tag "Days" :days)
  2430. (string :tag "Format string"))
  2431. (group :inline t
  2432. (const :tag "Always show days" :require-days)
  2433. (const t))
  2434. (group :inline t (const :tag "Hours" :hours)
  2435. (string :tag "Format string"))
  2436. (group :inline t
  2437. (const :tag "Always show hours" :require-hours)
  2438. (const t))
  2439. (group :inline t (const :tag "Minutes" :minutes)
  2440. (string :tag "Format string"))
  2441. (group :inline t
  2442. (const :tag "Always show minutes" :require-minutes)
  2443. (const t)))))
  2444. (defcustom org-time-clocksum-use-fractional nil
  2445. "If non-nil, \\[org-clock-display] uses fractional times.
  2446. org-mode generates a time duration."
  2447. :group 'org-time
  2448. :type 'boolean)
  2449. (defcustom org-time-clocksum-fractional-format "%.2f"
  2450. "The format string used when creating CLOCKSUM lines,
  2451. or when Org mode generates a time duration, if
  2452. `org-time-clocksum-use-fractional' is enabled.
  2453. The value can be a single format string containing one
  2454. %-sequence, which will be filled with the number of hours as
  2455. a float.
  2456. Alternatively, the value can be a plist associating any of the
  2457. keys :years, :months, :weeks, :days, :hours or :minutes with
  2458. a format string. The time duration is formatted using the
  2459. largest time unit which gives a non-zero integer part. If all
  2460. specified formats have zero integer part, the smallest time unit
  2461. is used."
  2462. :group 'org-time
  2463. :type '(choice (string :tag "Format string")
  2464. (set (group :inline t (const :tag "Years" :years)
  2465. (string :tag "Format string"))
  2466. (group :inline t (const :tag "Months" :months)
  2467. (string :tag "Format string"))
  2468. (group :inline t (const :tag "Weeks" :weeks)
  2469. (string :tag "Format string"))
  2470. (group :inline t (const :tag "Days" :days)
  2471. (string :tag "Format string"))
  2472. (group :inline t (const :tag "Hours" :hours)
  2473. (string :tag "Format string"))
  2474. (group :inline t (const :tag "Minutes" :minutes)
  2475. (string :tag "Format string")))))
  2476. (defcustom org-deadline-warning-days 14
  2477. "No. of days before expiration during which a deadline becomes active.
  2478. This variable governs the display in sparse trees and in the agenda.
  2479. When 0 or negative, it means use this number (the absolute value of it)
  2480. even if a deadline has a different individual lead time specified.
  2481. Custom commands can set this variable in the options section."
  2482. :group 'org-time
  2483. :group 'org-agenda-daily/weekly
  2484. :type 'integer)
  2485. (defcustom org-read-date-prefer-future t
  2486. "Non-nil means assume future for incomplete date input from user.
  2487. This affects the following situations:
  2488. 1. The user gives a month but not a year.
  2489. For example, if it is April and you enter \"feb 2\", this will be read
  2490. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2491. 2. The user gives a day, but no month.
  2492. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2493. read this as the third of *next* month. However, if you enter \"17\",
  2494. it will be considered as *this* month.
  2495. If you set this variable to the symbol `time', then also the following
  2496. will work:
  2497. 3. If the user gives a time.
  2498. If the time is before now, it will be interpreted as tomorrow.
  2499. Currently none of this works for ISO week specifications.
  2500. When this option is nil, the current day, month and year will always be
  2501. used as defaults.
  2502. See also `org-agenda-jump-prefer-future'."
  2503. :group 'org-time
  2504. :type '(choice
  2505. (const :tag "Never" nil)
  2506. (const :tag "Check month and day" t)
  2507. (const :tag "Check month, day, and time" time)))
  2508. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2509. "Should the agenda jump command prefer the future for incomplete dates?
  2510. The default is to do the same as configured in `org-read-date-prefer-future'.
  2511. But you can also set a deviating value here.
  2512. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2513. :group 'org-agenda
  2514. :group 'org-time
  2515. :version "24.1"
  2516. :type '(choice
  2517. (const :tag "Use org-read-date-prefer-future"
  2518. org-read-date-prefer-future)
  2519. (const :tag "Never" nil)
  2520. (const :tag "Always" t)))
  2521. (defcustom org-read-date-force-compatible-dates t
  2522. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2523. Depending on the system Emacs is running on, certain dates cannot
  2524. be represented with the type used internally to represent time.
  2525. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2526. correctly. Some systems allow for earlier dates, some for later,
  2527. some for both. One way to find out it to insert any date into an
  2528. Org buffer, putting the cursor on the year and hitting S-up and
  2529. S-down to test the range.
  2530. When this variable is set to t, the date/time prompt will not let
  2531. you specify dates outside the 1970-2037 range, so it is certain that
  2532. these dates will work in whatever version of Emacs you are
  2533. running, and also that you can move a file from one Emacs implementation
  2534. to another. WHenever Org is forcing the year for you, it will display
  2535. a message and beep.
  2536. When this variable is nil, Org will check if the date is
  2537. representable in the specific Emacs implementation you are using.
  2538. If not, it will force a year, usually the current year, and beep
  2539. to remind you. Currently this setting is not recommended because
  2540. the likelihood that you will open your Org files in an Emacs that
  2541. has limited date range is not negligible.
  2542. A workaround for this problem is to use diary sexp dates for time
  2543. stamps outside of this range."
  2544. :group 'org-time
  2545. :version "24.1"
  2546. :type 'boolean)
  2547. (defcustom org-read-date-display-live t
  2548. "Non-nil means display current interpretation of date prompt live.
  2549. This display will be in an overlay, in the minibuffer."
  2550. :group 'org-time
  2551. :type 'boolean)
  2552. (defcustom org-read-date-popup-calendar t
  2553. "Non-nil means pop up a calendar when prompting for a date.
  2554. In the calendar, the date can be selected with mouse-1. However, the
  2555. minibuffer will also be active, and you can simply enter the date as well.
  2556. When nil, only the minibuffer will be available."
  2557. :group 'org-time
  2558. :type 'boolean)
  2559. (if (fboundp 'defvaralias)
  2560. (defvaralias 'org-popup-calendar-for-date-prompt
  2561. 'org-read-date-popup-calendar))
  2562. (defcustom org-read-date-minibuffer-setup-hook nil
  2563. "Hook to be used to set up keys for the date/time interface.
  2564. Add key definitions to `minibuffer-local-map', which will be a temporary
  2565. copy."
  2566. :group 'org-time
  2567. :type 'hook)
  2568. (defcustom org-extend-today-until 0
  2569. "The hour when your day really ends. Must be an integer.
  2570. This has influence for the following applications:
  2571. - When switching the agenda to \"today\". It it is still earlier than
  2572. the time given here, the day recognized as TODAY is actually yesterday.
  2573. - When a date is read from the user and it is still before the time given
  2574. here, the current date and time will be assumed to be yesterday, 23:59.
  2575. Also, timestamps inserted in capture templates follow this rule.
  2576. IMPORTANT: This is a feature whose implementation is and likely will
  2577. remain incomplete. Really, it is only here because past midnight seems to
  2578. be the favorite working time of John Wiegley :-)"
  2579. :group 'org-time
  2580. :type 'integer)
  2581. (defcustom org-use-effective-time nil
  2582. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2583. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2584. \"effective time\" of any timestamps between midnight and 8am will be
  2585. 23:59 of the previous day."
  2586. :group 'org-time
  2587. :version "24.1"
  2588. :type 'boolean)
  2589. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2590. "When non-nil, use the last clock out time for `org-todo'.
  2591. Note that this option has precedence over the combined use of
  2592. `org-use-effective-time' and `org-extend-today-until'."
  2593. :group 'org-time
  2594. ;; :version "24.3"
  2595. :type 'boolean)
  2596. (defcustom org-edit-timestamp-down-means-later nil
  2597. "Non-nil means S-down will increase the time in a time stamp.
  2598. When nil, S-up will increase."
  2599. :group 'org-time
  2600. :type 'boolean)
  2601. (defcustom org-calendar-follow-timestamp-change t
  2602. "Non-nil means make the calendar window follow timestamp changes.
  2603. When a timestamp is modified and the calendar window is visible, it will be
  2604. moved to the new date."
  2605. :group 'org-time
  2606. :type 'boolean)
  2607. (defgroup org-tags nil
  2608. "Options concerning tags in Org-mode."
  2609. :tag "Org Tags"
  2610. :group 'org)
  2611. (defcustom org-tag-alist nil
  2612. "List of tags allowed in Org-mode files.
  2613. When this list is nil, Org-mode will base TAG input on what is already in the
  2614. buffer.
  2615. The value of this variable is an alist, the car of each entry must be a
  2616. keyword as a string, the cdr may be a character that is used to select
  2617. that tag through the fast-tag-selection interface.
  2618. See the manual for details."
  2619. :group 'org-tags
  2620. :type '(repeat
  2621. (choice
  2622. (cons (string :tag "Tag name")
  2623. (character :tag "Access char"))
  2624. (list :tag "Start radio group"
  2625. (const :startgroup)
  2626. (option (string :tag "Group description")))
  2627. (list :tag "End radio group"
  2628. (const :endgroup)
  2629. (option (string :tag "Group description")))
  2630. (const :tag "New line" (:newline)))))
  2631. (defcustom org-tag-persistent-alist nil
  2632. "List of tags that will always appear in all Org-mode files.
  2633. This is in addition to any in buffer settings or customizations
  2634. of `org-tag-alist'.
  2635. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2636. The value of this variable is an alist, the car of each entry must be a
  2637. keyword as a string, the cdr may be a character that is used to select
  2638. that tag through the fast-tag-selection interface.
  2639. See the manual for details.
  2640. To disable these tags on a per-file basis, insert anywhere in the file:
  2641. #+STARTUP: noptag"
  2642. :group 'org-tags
  2643. :type '(repeat
  2644. (choice
  2645. (cons (string :tag "Tag name")
  2646. (character :tag "Access char"))
  2647. (const :tag "Start radio group" (:startgroup))
  2648. (const :tag "End radio group" (:endgroup))
  2649. (const :tag "New line" (:newline)))))
  2650. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2651. "If non-nil, always offer completion for all tags of all agenda files.
  2652. Instead of customizing this variable directly, you might want to
  2653. set it locally for capture buffers, because there no list of
  2654. tags in that file can be created dynamically (there are none).
  2655. (add-hook 'org-capture-mode-hook
  2656. (lambda ()
  2657. (set (make-local-variable
  2658. 'org-complete-tags-always-offer-all-agenda-tags)
  2659. t)))"
  2660. :group 'org-tags
  2661. :version "24.1"
  2662. :type 'boolean)
  2663. (defvar org-file-tags nil
  2664. "List of tags that can be inherited by all entries in the file.
  2665. The tags will be inherited if the variable `org-use-tag-inheritance'
  2666. says they should be.
  2667. This variable is populated from #+FILETAGS lines.")
  2668. (defcustom org-use-fast-tag-selection 'auto
  2669. "Non-nil means use fast tag selection scheme.
  2670. This is a special interface to select and deselect tags with single keys.
  2671. When nil, fast selection is never used.
  2672. When the symbol `auto', fast selection is used if and only if selection
  2673. characters for tags have been configured, either through the variable
  2674. `org-tag-alist' or through a #+TAGS line in the buffer.
  2675. When t, fast selection is always used and selection keys are assigned
  2676. automatically if necessary."
  2677. :group 'org-tags
  2678. :type '(choice
  2679. (const :tag "Always" t)
  2680. (const :tag "Never" nil)
  2681. (const :tag "When selection characters are configured" 'auto)))
  2682. (defcustom org-fast-tag-selection-single-key nil
  2683. "Non-nil means fast tag selection exits after first change.
  2684. When nil, you have to press RET to exit it.
  2685. During fast tag selection, you can toggle this flag with `C-c'.
  2686. This variable can also have the value `expert'. In this case, the window
  2687. displaying the tags menu is not even shown, until you press C-c again."
  2688. :group 'org-tags
  2689. :type '(choice
  2690. (const :tag "No" nil)
  2691. (const :tag "Yes" t)
  2692. (const :tag "Expert" expert)))
  2693. (defvar org-fast-tag-selection-include-todo nil
  2694. "Non-nil means fast tags selection interface will also offer TODO states.
  2695. This is an undocumented feature, you should not rely on it.")
  2696. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2697. "The column to which tags should be indented in a headline.
  2698. If this number is positive, it specifies the column. If it is negative,
  2699. it means that the tags should be flushright to that column. For example,
  2700. -80 works well for a normal 80 character screen.
  2701. When 0, place tags directly after headline text, with only one space in
  2702. between."
  2703. :group 'org-tags
  2704. :type 'integer)
  2705. (defcustom org-auto-align-tags t
  2706. "Non-nil keeps tags aligned when modifying headlines.
  2707. Some operations (i.e. demoting) change the length of a headline and
  2708. therefore shift the tags around. With this option turned on, after
  2709. each such operation the tags are again aligned to `org-tags-column'."
  2710. :group 'org-tags
  2711. :type 'boolean)
  2712. (defcustom org-use-tag-inheritance t
  2713. "Non-nil means tags in levels apply also for sublevels.
  2714. When nil, only the tags directly given in a specific line apply there.
  2715. This may also be a list of tags that should be inherited, or a regexp that
  2716. matches tags that should be inherited. Additional control is possible
  2717. with the variable `org-tags-exclude-from-inheritance' which gives an
  2718. explicit list of tags to be excluded from inheritance., even if the value of
  2719. `org-use-tag-inheritance' would select it for inheritance.
  2720. If this option is t, a match early-on in a tree can lead to a large
  2721. number of matches in the subtree when constructing the agenda or creating
  2722. a sparse tree. If you only want to see the first match in a tree during
  2723. a search, check out the variable `org-tags-match-list-sublevels'."
  2724. :group 'org-tags
  2725. :type '(choice
  2726. (const :tag "Not" nil)
  2727. (const :tag "Always" t)
  2728. (repeat :tag "Specific tags" (string :tag "Tag"))
  2729. (regexp :tag "Tags matched by regexp")))
  2730. (defcustom org-tags-exclude-from-inheritance nil
  2731. "List of tags that should never be inherited.
  2732. This is a way to exclude a few tags from inheritance. For way to do
  2733. the opposite, to actively allow inheritance for selected tags,
  2734. see the variable `org-use-tag-inheritance'."
  2735. :group 'org-tags
  2736. :type '(repeat (string :tag "Tag")))
  2737. (defun org-tag-inherit-p (tag)
  2738. "Check if TAG is one that should be inherited."
  2739. (cond
  2740. ((member tag org-tags-exclude-from-inheritance) nil)
  2741. ((eq org-use-tag-inheritance t) t)
  2742. ((not org-use-tag-inheritance) nil)
  2743. ((stringp org-use-tag-inheritance)
  2744. (string-match org-use-tag-inheritance tag))
  2745. ((listp org-use-tag-inheritance)
  2746. (member tag org-use-tag-inheritance))
  2747. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2748. (defcustom org-tags-match-list-sublevels t
  2749. "Non-nil means list also sublevels of headlines matching a search.
  2750. This variable applies to tags/property searches, and also to stuck
  2751. projects because this search is based on a tags match as well.
  2752. When set to the symbol `indented', sublevels are indented with
  2753. leading dots.
  2754. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2755. the sublevels of a headline matching a tag search often also match
  2756. the same search. Listing all of them can create very long lists.
  2757. Setting this variable to nil causes subtrees of a match to be skipped.
  2758. This variable is semi-obsolete and probably should always be true. It
  2759. is better to limit inheritance to certain tags using the variables
  2760. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2761. :group 'org-tags
  2762. :type '(choice
  2763. (const :tag "No, don't list them" nil)
  2764. (const :tag "Yes, do list them" t)
  2765. (const :tag "List them, indented with leading dots" indented)))
  2766. (defcustom org-tags-sort-function nil
  2767. "When set, tags are sorted using this function as a comparator."
  2768. :group 'org-tags
  2769. :type '(choice
  2770. (const :tag "No sorting" nil)
  2771. (const :tag "Alphabetical" string<)
  2772. (const :tag "Reverse alphabetical" string>)
  2773. (function :tag "Custom function" nil)))
  2774. (defvar org-tags-history nil
  2775. "History of minibuffer reads for tags.")
  2776. (defvar org-last-tags-completion-table nil
  2777. "The last used completion table for tags.")
  2778. (defvar org-after-tags-change-hook nil
  2779. "Hook that is run after the tags in a line have changed.")
  2780. (defgroup org-properties nil
  2781. "Options concerning properties in Org-mode."
  2782. :tag "Org Properties"
  2783. :group 'org)
  2784. (defcustom org-property-format "%-10s %s"
  2785. "How property key/value pairs should be formatted by `indent-line'.
  2786. When `indent-line' hits a property definition, it will format the line
  2787. according to this format, mainly to make sure that the values are
  2788. lined-up with respect to each other."
  2789. :group 'org-properties
  2790. :type 'string)
  2791. (defcustom org-properties-postprocess-alist nil
  2792. "Alist of properties and functions to adjust inserted values.
  2793. Elements of this alist must be of the form
  2794. ([string] [function])
  2795. where [string] must be a property name and [function] must be a
  2796. lambda expression: this lambda expression must take one argument,
  2797. the value to adjust, and return the new value as a string.
  2798. For example, this element will allow the property \"Remaining\"
  2799. to be updated wrt the relation between the \"Effort\" property
  2800. and the clock summary:
  2801. ((\"Remaining\" (lambda(value)
  2802. (let ((clocksum (org-clock-sum-current-item))
  2803. (effort (org-duration-string-to-minutes
  2804. (org-entry-get (point) \"Effort\"))))
  2805. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2806. :group 'org-properties
  2807. :version "24.1"
  2808. :type '(alist :key-type (string :tag "Property")
  2809. :value-type (function :tag "Function")))
  2810. (defcustom org-use-property-inheritance nil
  2811. "Non-nil means properties apply also for sublevels.
  2812. This setting is chiefly used during property searches. Turning it on can
  2813. cause significant overhead when doing a search, which is why it is not
  2814. on by default.
  2815. When nil, only the properties directly given in the current entry count.
  2816. When t, every property is inherited. The value may also be a list of
  2817. properties that should have inheritance, or a regular expression matching
  2818. properties that should be inherited.
  2819. However, note that some special properties use inheritance under special
  2820. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2821. and the properties ending in \"_ALL\" when they are used as descriptor
  2822. for valid values of a property.
  2823. Note for programmers:
  2824. When querying an entry with `org-entry-get', you can control if inheritance
  2825. should be used. By default, `org-entry-get' looks only at the local
  2826. properties. You can request inheritance by setting the inherit argument
  2827. to t (to force inheritance) or to `selective' (to respect the setting
  2828. in this variable)."
  2829. :group 'org-properties
  2830. :type '(choice
  2831. (const :tag "Not" nil)
  2832. (const :tag "Always" t)
  2833. (repeat :tag "Specific properties" (string :tag "Property"))
  2834. (regexp :tag "Properties matched by regexp")))
  2835. (defun org-property-inherit-p (property)
  2836. "Check if PROPERTY is one that should be inherited."
  2837. (cond
  2838. ((eq org-use-property-inheritance t) t)
  2839. ((not org-use-property-inheritance) nil)
  2840. ((stringp org-use-property-inheritance)
  2841. (string-match org-use-property-inheritance property))
  2842. ((listp org-use-property-inheritance)
  2843. (member property org-use-property-inheritance))
  2844. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2845. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2846. "The default column format, if no other format has been defined.
  2847. This variable can be set on the per-file basis by inserting a line
  2848. #+COLUMNS: %25ITEM ....."
  2849. :group 'org-properties
  2850. :type 'string)
  2851. (defcustom org-columns-ellipses ".."
  2852. "The ellipses to be used when a field in column view is truncated.
  2853. When this is the empty string, as many characters as possible are shown,
  2854. but then there will be no visual indication that the field has been truncated.
  2855. When this is a string of length N, the last N characters of a truncated
  2856. field are replaced by this string. If the column is narrower than the
  2857. ellipses string, only part of the ellipses string will be shown."
  2858. :group 'org-properties
  2859. :type 'string)
  2860. (defcustom org-columns-modify-value-for-display-function nil
  2861. "Function that modifies values for display in column view.
  2862. For example, it can be used to cut out a certain part from a time stamp.
  2863. The function must take 2 arguments:
  2864. column-title The title of the column (*not* the property name)
  2865. value The value that should be modified.
  2866. The function should return the value that should be displayed,
  2867. or nil if the normal value should be used."
  2868. :group 'org-properties
  2869. :type 'function)
  2870. (defcustom org-effort-property "Effort"
  2871. "The property that is being used to keep track of effort estimates.
  2872. Effort estimates given in this property need to have the format H:MM."
  2873. :group 'org-properties
  2874. :group 'org-progress
  2875. :type '(string :tag "Property"))
  2876. (defconst org-global-properties-fixed
  2877. '(("VISIBILITY_ALL" . "folded children content all")
  2878. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2879. "List of property/value pairs that can be inherited by any entry.
  2880. These are fixed values, for the preset properties. The user variable
  2881. that can be used to add to this list is `org-global-properties'.
  2882. The entries in this list are cons cells where the car is a property
  2883. name and cdr is a string with the value. If the value represents
  2884. multiple items like an \"_ALL\" property, separate the items by
  2885. spaces.")
  2886. (defcustom org-global-properties nil
  2887. "List of property/value pairs that can be inherited by any entry.
  2888. This list will be combined with the constant `org-global-properties-fixed'.
  2889. The entries in this list are cons cells where the car is a property
  2890. name and cdr is a string with the value.
  2891. You can set buffer-local values for the same purpose in the variable
  2892. `org-file-properties' this by adding lines like
  2893. #+PROPERTY: NAME VALUE"
  2894. :group 'org-properties
  2895. :type '(repeat
  2896. (cons (string :tag "Property")
  2897. (string :tag "Value"))))
  2898. (defvar org-file-properties nil
  2899. "List of property/value pairs that can be inherited by any entry.
  2900. Valid for the current buffer.
  2901. This variable is populated from #+PROPERTY lines.")
  2902. (make-variable-buffer-local 'org-file-properties)
  2903. (defgroup org-agenda nil
  2904. "Options concerning agenda views in Org-mode."
  2905. :tag "Org Agenda"
  2906. :group 'org)
  2907. (defvar org-category nil
  2908. "Variable used by org files to set a category for agenda display.
  2909. Such files should use a file variable to set it, for example
  2910. # -*- mode: org; org-category: \"ELisp\"
  2911. or contain a special line
  2912. #+CATEGORY: ELisp
  2913. If the file does not specify a category, then file's base name
  2914. is used instead.")
  2915. (make-variable-buffer-local 'org-category)
  2916. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2917. (defcustom org-agenda-files nil
  2918. "The files to be used for agenda display.
  2919. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2920. \\[org-remove-file]. You can also use customize to edit the list.
  2921. If an entry is a directory, all files in that directory that are matched by
  2922. `org-agenda-file-regexp' will be part of the file list.
  2923. If the value of the variable is not a list but a single file name, then
  2924. the list of agenda files is actually stored and maintained in that file, one
  2925. agenda file per line. In this file paths can be given relative to
  2926. `org-directory'. Tilde expansion and environment variable substitution
  2927. are also made."
  2928. :group 'org-agenda
  2929. :type '(choice
  2930. (repeat :tag "List of files and directories" file)
  2931. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2932. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2933. "Regular expression to match files for `org-agenda-files'.
  2934. If any element in the list in that variable contains a directory instead
  2935. of a normal file, all files in that directory that are matched by this
  2936. regular expression will be included."
  2937. :group 'org-agenda
  2938. :type 'regexp)
  2939. (defcustom org-agenda-text-search-extra-files nil
  2940. "List of extra files to be searched by text search commands.
  2941. These files will be search in addition to the agenda files by the
  2942. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2943. Note that these files will only be searched for text search commands,
  2944. not for the other agenda views like todo lists, tag searches or the weekly
  2945. agenda. This variable is intended to list notes and possibly archive files
  2946. that should also be searched by these two commands.
  2947. In fact, if the first element in the list is the symbol `agenda-archives',
  2948. than all archive files of all agenda files will be added to the search
  2949. scope."
  2950. :group 'org-agenda
  2951. :type '(set :greedy t
  2952. (const :tag "Agenda Archives" agenda-archives)
  2953. (repeat :inline t (file))))
  2954. (if (fboundp 'defvaralias)
  2955. (defvaralias 'org-agenda-multi-occur-extra-files
  2956. 'org-agenda-text-search-extra-files))
  2957. (defcustom org-agenda-skip-unavailable-files nil
  2958. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2959. A nil value means to remove them, after a query, from the list."
  2960. :group 'org-agenda
  2961. :type 'boolean)
  2962. (defcustom org-calendar-to-agenda-key [?c]
  2963. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2964. The command `org-calendar-goto-agenda' will be bound to this key. The
  2965. default is the character `c' because then `c' can be used to switch back and
  2966. forth between agenda and calendar."
  2967. :group 'org-agenda
  2968. :type 'sexp)
  2969. (defcustom org-calendar-insert-diary-entry-key [?i]
  2970. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2971. This option is irrelevant until `org-agenda-diary-file' has been configured
  2972. to point to an Org-mode file. When that is the case, the command
  2973. `org-agenda-diary-entry' will be bound to the key given here, by default
  2974. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2975. if you want to continue doing this, you need to change this to a different
  2976. key."
  2977. :group 'org-agenda
  2978. :type 'sexp)
  2979. (defcustom org-agenda-diary-file 'diary-file
  2980. "File to which to add new entries with the `i' key in agenda and calendar.
  2981. When this is the symbol `diary-file', the functionality in the Emacs
  2982. calendar will be used to add entries to the `diary-file'. But when this
  2983. points to a file, `org-agenda-diary-entry' will be used instead."
  2984. :group 'org-agenda
  2985. :type '(choice
  2986. (const :tag "The standard Emacs diary file" diary-file)
  2987. (file :tag "Special Org file diary entries")))
  2988. (eval-after-load "calendar"
  2989. '(progn
  2990. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2991. 'org-calendar-goto-agenda)
  2992. (add-hook 'calendar-mode-hook
  2993. (lambda ()
  2994. (unless (eq org-agenda-diary-file 'diary-file)
  2995. (define-key calendar-mode-map
  2996. org-calendar-insert-diary-entry-key
  2997. 'org-agenda-diary-entry))))))
  2998. (defgroup org-latex nil
  2999. "Options for embedding LaTeX code into Org-mode."
  3000. :tag "Org LaTeX"
  3001. :group 'org)
  3002. (defcustom org-format-latex-options
  3003. '(:foreground default :background default :scale 1.0
  3004. :html-foreground "Black" :html-background "Transparent"
  3005. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3006. "Options for creating images from LaTeX fragments.
  3007. This is a property list with the following properties:
  3008. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3009. `default' means use the foreground of the default face.
  3010. `auto' means use the foreground from the text face.
  3011. :background the background color, or \"Transparent\".
  3012. `default' means use the background of the default face.
  3013. `auto' means use the background from the text face.
  3014. :scale a scaling factor for the size of the images, to get more pixels
  3015. :html-foreground, :html-background, :html-scale
  3016. the same numbers for HTML export.
  3017. :matchers a list indicating which matchers should be used to
  3018. find LaTeX fragments. Valid members of this list are:
  3019. \"begin\" find environments
  3020. \"$1\" find single characters surrounded by $.$
  3021. \"$\" find math expressions surrounded by $...$
  3022. \"$$\" find math expressions surrounded by $$....$$
  3023. \"\\(\" find math expressions surrounded by \\(...\\)
  3024. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3025. :group 'org-latex
  3026. :type 'plist)
  3027. (defcustom org-format-latex-signal-error t
  3028. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3029. When nil, just push out a message."
  3030. :group 'org-latex
  3031. :version "24.1"
  3032. :type 'boolean)
  3033. (defcustom org-latex-to-mathml-jar-file nil
  3034. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3035. Use this to specify additional executable file say a jar file.
  3036. When using MathToWeb as the converter, specify the full-path to
  3037. your mathtoweb.jar file."
  3038. :group 'org-latex
  3039. :version "24.1"
  3040. :type '(choice
  3041. (const :tag "None" nil)
  3042. (file :tag "JAR file" :must-match t)))
  3043. (defcustom org-latex-to-mathml-convert-command nil
  3044. "Command to convert LaTeX fragments to MathML.
  3045. Replace format-specifiers in the command as noted below and use
  3046. `shell-command' to convert LaTeX to MathML.
  3047. %j: Executable file in fully expanded form as specified by
  3048. `org-latex-to-mathml-jar-file'.
  3049. %I: Input LaTeX file in fully expanded form
  3050. %o: Output MathML file
  3051. This command is used by `org-create-math-formula'.
  3052. When using MathToWeb as the converter, set this to
  3053. \"java -jar %j -unicode -force -df %o %I\"."
  3054. :group 'org-latex
  3055. :version "24.1"
  3056. :type '(choice
  3057. (const :tag "None" nil)
  3058. (string :tag "\nShell command")))
  3059. (defcustom org-latex-create-formula-image-program 'dvipng
  3060. "Program to convert LaTeX fragments with.
  3061. dvipng Process the LaTeX fragments to dvi file, then convert
  3062. dvi files to png files using dvipng.
  3063. This will also include processing of non-math environments.
  3064. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3065. to convert pdf files to png files"
  3066. :group 'org-latex
  3067. :version "24.1"
  3068. :type '(choice
  3069. (const :tag "dvipng" dvipng)
  3070. (const :tag "imagemagick" imagemagick)))
  3071. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3072. "Path to store latex preview images.
  3073. A relative path here creates many directories relative to the
  3074. processed org files paths. An absolute path puts all preview
  3075. images at the same place."
  3076. :group 'org-latex
  3077. :version "24.3"
  3078. :type 'string)
  3079. (defun org-format-latex-mathml-available-p ()
  3080. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3081. (save-match-data
  3082. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3083. org-latex-to-mathml-convert-command)
  3084. (let ((executable (car (split-string
  3085. org-latex-to-mathml-convert-command))))
  3086. (when (executable-find executable)
  3087. (if (string-match
  3088. "%j" org-latex-to-mathml-convert-command)
  3089. (file-readable-p org-latex-to-mathml-jar-file)
  3090. t))))))
  3091. (defcustom org-format-latex-header "\\documentclass{article}
  3092. \\usepackage[usenames]{color}
  3093. \\usepackage{amsmath}
  3094. \\usepackage[mathscr]{eucal}
  3095. \\pagestyle{empty} % do not remove
  3096. \[PACKAGES]
  3097. \[DEFAULT-PACKAGES]
  3098. % The settings below are copied from fullpage.sty
  3099. \\setlength{\\textwidth}{\\paperwidth}
  3100. \\addtolength{\\textwidth}{-3cm}
  3101. \\setlength{\\oddsidemargin}{1.5cm}
  3102. \\addtolength{\\oddsidemargin}{-2.54cm}
  3103. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3104. \\setlength{\\textheight}{\\paperheight}
  3105. \\addtolength{\\textheight}{-\\headheight}
  3106. \\addtolength{\\textheight}{-\\headsep}
  3107. \\addtolength{\\textheight}{-\\footskip}
  3108. \\addtolength{\\textheight}{-3cm}
  3109. \\setlength{\\topmargin}{1.5cm}
  3110. \\addtolength{\\topmargin}{-2.54cm}"
  3111. "The document header used for processing LaTeX fragments.
  3112. It is imperative that this header make sure that no page number
  3113. appears on the page. The package defined in the variables
  3114. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  3115. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  3116. will be appended."
  3117. :group 'org-latex
  3118. :type 'string)
  3119. (defvar org-format-latex-header-extra nil)
  3120. (defun org-set-packages-alist (var val)
  3121. "Set the packages alist and make sure it has 3 elements per entry."
  3122. (set var (mapcar (lambda (x)
  3123. (if (and (consp x) (= (length x) 2))
  3124. (list (car x) (nth 1 x) t)
  3125. x))
  3126. val)))
  3127. (defun org-get-packages-alist (var)
  3128. "Get the packages alist and make sure it has 3 elements per entry."
  3129. (mapcar (lambda (x)
  3130. (if (and (consp x) (= (length x) 2))
  3131. (list (car x) (nth 1 x) t)
  3132. x))
  3133. (default-value var)))
  3134. ;; The following variables are defined here because is it also used
  3135. ;; when formatting latex fragments. Originally it was part of the
  3136. ;; LaTeX exporter, which is why the name includes "export".
  3137. (defcustom org-export-latex-default-packages-alist
  3138. '(("AUTO" "inputenc" t)
  3139. ("T1" "fontenc" t)
  3140. ("" "fixltx2e" nil)
  3141. ("" "graphicx" t)
  3142. ("" "longtable" nil)
  3143. ("" "float" nil)
  3144. ("" "wrapfig" nil)
  3145. ("" "soul" t)
  3146. ("" "textcomp" t)
  3147. ("" "marvosym" t)
  3148. ("" "wasysym" t)
  3149. ("" "latexsym" t)
  3150. ("" "amssymb" t)
  3151. ("" "hyperref" nil)
  3152. "\\tolerance=1000"
  3153. )
  3154. "Alist of default packages to be inserted in the header.
  3155. Change this only if one of the packages here causes an incompatibility
  3156. with another package you are using.
  3157. The packages in this list are needed by one part or another of Org-mode
  3158. to function properly.
  3159. - inputenc, fontenc: for basic font and character selection
  3160. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3161. for interpreting the entities in `org-entities'. You can skip some of these
  3162. packages if you don't use any of the symbols in it.
  3163. - graphicx: for including images
  3164. - float, wrapfig: for figure placement
  3165. - longtable: for long tables
  3166. - hyperref: for cross references
  3167. Therefore you should not modify this variable unless you know what you
  3168. are doing. The one reason to change it anyway is that you might be loading
  3169. some other package that conflicts with one of the default packages.
  3170. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3171. If SNIPPET-FLAG is t, the package also needs to be included when
  3172. compiling LaTeX snippets into images for inclusion into HTML."
  3173. :group 'org-export-latex
  3174. :set 'org-set-packages-alist
  3175. :get 'org-get-packages-alist
  3176. :version "24.1"
  3177. :type '(repeat
  3178. (choice
  3179. (list :tag "options/package pair"
  3180. (string :tag "options")
  3181. (string :tag "package")
  3182. (boolean :tag "Snippet"))
  3183. (string :tag "A line of LaTeX"))))
  3184. (defcustom org-export-latex-packages-alist nil
  3185. "Alist of packages to be inserted in every LaTeX header.
  3186. These will be inserted after `org-export-latex-default-packages-alist'.
  3187. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3188. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3189. turning LaTeX snippets into images for inclusion into HTML.
  3190. Make sure that you only list packages here which:
  3191. - you want in every file
  3192. - do not conflict with the default packages in
  3193. `org-export-latex-default-packages-alist'
  3194. - do not conflict with the setup in `org-format-latex-header'."
  3195. :group 'org-export-latex
  3196. :set 'org-set-packages-alist
  3197. :get 'org-get-packages-alist
  3198. :type '(repeat
  3199. (choice
  3200. (list :tag "options/package pair"
  3201. (string :tag "options")
  3202. (string :tag "package")
  3203. (boolean :tag "Snippet"))
  3204. (string :tag "A line of LaTeX"))))
  3205. (defgroup org-appearance nil
  3206. "Settings for Org-mode appearance."
  3207. :tag "Org Appearance"
  3208. :group 'org)
  3209. (defcustom org-level-color-stars-only nil
  3210. "Non-nil means fontify only the stars in each headline.
  3211. When nil, the entire headline is fontified.
  3212. Changing it requires restart of `font-lock-mode' to become effective
  3213. also in regions already fontified."
  3214. :group 'org-appearance
  3215. :type 'boolean)
  3216. (defcustom org-hide-leading-stars nil
  3217. "Non-nil means hide the first N-1 stars in a headline.
  3218. This works by using the face `org-hide' for these stars. This
  3219. face is white for a light background, and black for a dark
  3220. background. You may have to customize the face `org-hide' to
  3221. make this work.
  3222. Changing it requires restart of `font-lock-mode' to become effective
  3223. also in regions already fontified.
  3224. You may also set this on a per-file basis by adding one of the following
  3225. lines to the buffer:
  3226. #+STARTUP: hidestars
  3227. #+STARTUP: showstars"
  3228. :group 'org-appearance
  3229. :type 'boolean)
  3230. (defcustom org-hidden-keywords nil
  3231. "List of symbols corresponding to keywords to be hidden the org buffer.
  3232. For example, a value '(title) for this list will make the document's title
  3233. appear in the buffer without the initial #+TITLE: keyword."
  3234. :group 'org-appearance
  3235. :version "24.1"
  3236. :type '(set (const :tag "#+AUTHOR" author)
  3237. (const :tag "#+DATE" date)
  3238. (const :tag "#+EMAIL" email)
  3239. (const :tag "#+TITLE" title)))
  3240. (defcustom org-custom-properties nil
  3241. "List of properties (as strings) with a special meaning.
  3242. The default use of these custom properties is to let the user
  3243. hide them with `org-toggle-custom-properties-visibility'."
  3244. :group 'org-properties
  3245. :group 'org-appearance
  3246. :version "24.3"
  3247. :type '(repeat (string :tag "Property Name")))
  3248. (defcustom org-fontify-done-headline nil
  3249. "Non-nil means change the face of a headline if it is marked DONE.
  3250. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3251. When this is non-nil, the headline after the keyword is set to the
  3252. `org-headline-done' as an additional indication."
  3253. :group 'org-appearance
  3254. :type 'boolean)
  3255. (defcustom org-fontify-emphasized-text t
  3256. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3257. Changing this variable requires a restart of Emacs to take effect."
  3258. :group 'org-appearance
  3259. :type 'boolean)
  3260. (defcustom org-fontify-whole-heading-line nil
  3261. "Non-nil means fontify the whole line for headings.
  3262. This is useful when setting a background color for the
  3263. org-level-* faces."
  3264. :group 'org-appearance
  3265. :type 'boolean)
  3266. (defcustom org-highlight-latex-fragments-and-specials nil
  3267. "Non-nil means fontify what is treated specially by the exporters."
  3268. :group 'org-appearance
  3269. :type 'boolean)
  3270. (defcustom org-hide-emphasis-markers nil
  3271. "Non-nil mean font-lock should hide the emphasis marker characters."
  3272. :group 'org-appearance
  3273. :type 'boolean)
  3274. (defcustom org-pretty-entities nil
  3275. "Non-nil means show entities as UTF8 characters.
  3276. When nil, the \\name form remains in the buffer."
  3277. :group 'org-appearance
  3278. :version "24.1"
  3279. :type 'boolean)
  3280. (defcustom org-pretty-entities-include-sub-superscripts t
  3281. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3282. :group 'org-appearance
  3283. :version "24.1"
  3284. :type 'boolean)
  3285. (defvar org-emph-re nil
  3286. "Regular expression for matching emphasis.
  3287. After a match, the match groups contain these elements:
  3288. 0 The match of the full regular expression, including the characters
  3289. before and after the proper match
  3290. 1 The character before the proper match, or empty at beginning of line
  3291. 2 The proper match, including the leading and trailing markers
  3292. 3 The leading marker like * or /, indicating the type of highlighting
  3293. 4 The text between the emphasis markers, not including the markers
  3294. 5 The character after the match, empty at the end of a line")
  3295. (defvar org-verbatim-re nil
  3296. "Regular expression for matching verbatim text.")
  3297. (defvar org-emphasis-regexp-components) ; defined just below
  3298. (defvar org-emphasis-alist) ; defined just below
  3299. (defun org-set-emph-re (var val)
  3300. "Set variable and compute the emphasis regular expression."
  3301. (set var val)
  3302. (when (and (boundp 'org-emphasis-alist)
  3303. (boundp 'org-emphasis-regexp-components)
  3304. org-emphasis-alist org-emphasis-regexp-components)
  3305. (let* ((e org-emphasis-regexp-components)
  3306. (pre (car e))
  3307. (post (nth 1 e))
  3308. (border (nth 2 e))
  3309. (body (nth 3 e))
  3310. (nl (nth 4 e))
  3311. (body1 (concat body "*?"))
  3312. (markers (mapconcat 'car org-emphasis-alist ""))
  3313. (vmarkers (mapconcat
  3314. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3315. org-emphasis-alist "")))
  3316. ;; make sure special characters appear at the right position in the class
  3317. (if (string-match "\\^" markers)
  3318. (setq markers (concat (replace-match "" t t markers) "^")))
  3319. (if (string-match "-" markers)
  3320. (setq markers (concat (replace-match "" t t markers) "-")))
  3321. (if (string-match "\\^" vmarkers)
  3322. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3323. (if (string-match "-" vmarkers)
  3324. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3325. (if (> nl 0)
  3326. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3327. (int-to-string nl) "\\}")))
  3328. ;; Make the regexp
  3329. (setq org-emph-re
  3330. (concat "\\([" pre "]\\|^\\)"
  3331. "\\("
  3332. "\\([" markers "]\\)"
  3333. "\\("
  3334. "[^" border "]\\|"
  3335. "[^" border "]"
  3336. body1
  3337. "[^" border "]"
  3338. "\\)"
  3339. "\\3\\)"
  3340. "\\([" post "]\\|$\\)"))
  3341. (setq org-verbatim-re
  3342. (concat "\\([" pre "]\\|^\\)"
  3343. "\\("
  3344. "\\([" vmarkers "]\\)"
  3345. "\\("
  3346. "[^" border "]\\|"
  3347. "[^" border "]"
  3348. body1
  3349. "[^" border "]"
  3350. "\\)"
  3351. "\\3\\)"
  3352. "\\([" post "]\\|$\\)")))))
  3353. (defcustom org-emphasis-regexp-components
  3354. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3355. "Components used to build the regular expression for emphasis.
  3356. This is a list with five entries. Terminology: In an emphasis string
  3357. like \" *strong word* \", we call the initial space PREMATCH, the final
  3358. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3359. and \"trong wor\" is the body. The different components in this variable
  3360. specify what is allowed/forbidden in each part:
  3361. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3362. post Chars allowed as postmatch. End of line will be allowed too.
  3363. border The chars *forbidden* as border characters.
  3364. body-regexp A regexp like \".\" to match a body character. Don't use
  3365. non-shy groups here, and don't allow newline here.
  3366. newline The maximum number of newlines allowed in an emphasis exp.
  3367. Use customize to modify this, or restart Emacs after changing it."
  3368. :group 'org-appearance
  3369. :set 'org-set-emph-re
  3370. :type '(list
  3371. (sexp :tag "Allowed chars in pre ")
  3372. (sexp :tag "Allowed chars in post ")
  3373. (sexp :tag "Forbidden chars in border ")
  3374. (sexp :tag "Regexp for body ")
  3375. (integer :tag "number of newlines allowed")
  3376. (option (boolean :tag "Please ignore this button"))))
  3377. (defcustom org-emphasis-alist
  3378. `(("*" bold "<b>" "</b>")
  3379. ("/" italic "<i>" "</i>")
  3380. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3381. ("=" org-code "<code>" "</code>" verbatim)
  3382. ("~" org-verbatim "<code>" "</code>" verbatim)
  3383. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3384. "<del>" "</del>")
  3385. )
  3386. "Special syntax for emphasized text.
  3387. Text starting and ending with a special character will be emphasized, for
  3388. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3389. characters, the face to be used by font-lock for highlighting in Org-mode
  3390. Emacs buffers, and the HTML tags to be used for this.
  3391. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3392. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3393. Use customize to modify this, or restart Emacs after changing it."
  3394. :group 'org-appearance
  3395. :set 'org-set-emph-re
  3396. :type '(repeat
  3397. (list
  3398. (string :tag "Marker character")
  3399. (choice
  3400. (face :tag "Font-lock-face")
  3401. (plist :tag "Face property list"))
  3402. (string :tag "HTML start tag")
  3403. (string :tag "HTML end tag")
  3404. (option (const verbatim)))))
  3405. (defvar org-syntax-table
  3406. (let ((st (make-syntax-table)))
  3407. (mapc (lambda(c) (modify-syntax-entry
  3408. (string-to-char (car c)) "w p" st))
  3409. org-emphasis-alist)
  3410. st))
  3411. (defvar org-protecting-blocks
  3412. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3413. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3414. This is needed for font-lock setup.")
  3415. ;;; Miscellaneous options
  3416. (defgroup org-completion nil
  3417. "Completion in Org-mode."
  3418. :tag "Org Completion"
  3419. :group 'org)
  3420. (defcustom org-completion-use-ido nil
  3421. "Non-nil means use ido completion wherever possible.
  3422. Note that `ido-mode' must be active for this variable to be relevant.
  3423. If you decide to turn this variable on, you might well want to turn off
  3424. `org-outline-path-complete-in-steps'.
  3425. See also `org-completion-use-iswitchb'."
  3426. :group 'org-completion
  3427. :type 'boolean)
  3428. (defcustom org-completion-use-iswitchb nil
  3429. "Non-nil means use iswitchb completion wherever possible.
  3430. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3431. If you decide to turn this variable on, you might well want to turn off
  3432. `org-outline-path-complete-in-steps'.
  3433. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3434. :group 'org-completion
  3435. :type 'boolean)
  3436. (defcustom org-completion-fallback-command 'hippie-expand
  3437. "The expansion command called by \\[pcomplete] in normal context.
  3438. Normal means, no org-mode-specific context."
  3439. :group 'org-completion
  3440. :type 'function)
  3441. ;;; Functions and variables from their packages
  3442. ;; Declared here to avoid compiler warnings
  3443. ;; XEmacs only
  3444. (defvar outline-mode-menu-heading)
  3445. (defvar outline-mode-menu-show)
  3446. (defvar outline-mode-menu-hide)
  3447. (defvar zmacs-regions) ; XEmacs regions
  3448. ;; Emacs only
  3449. (defvar mark-active)
  3450. ;; Various packages
  3451. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3452. (declare-function calendar-forward-day "cal-move" (arg))
  3453. (declare-function calendar-goto-date "cal-move" (date))
  3454. (declare-function calendar-goto-today "cal-move" ())
  3455. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3456. (defvar calc-embedded-close-formula)
  3457. (defvar calc-embedded-open-formula)
  3458. (declare-function cdlatex-tab "ext:cdlatex" ())
  3459. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3460. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3461. (defvar font-lock-unfontify-region-function)
  3462. (declare-function iswitchb-read-buffer "iswitchb"
  3463. (prompt &optional default require-match start matches-set))
  3464. (defvar iswitchb-temp-buflist)
  3465. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3466. (defvar org-agenda-tags-todo-honor-ignore-options)
  3467. (declare-function org-agenda-skip "org-agenda" ())
  3468. (declare-function
  3469. org-agenda-format-item "org-agenda"
  3470. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3471. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3472. (declare-function org-agenda-change-all-lines "org-agenda"
  3473. (newhead hdmarker &optional fixface just-this))
  3474. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3475. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3476. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3477. (beg end))
  3478. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3479. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3480. "org-agenda" (&optional end))
  3481. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3482. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3483. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3484. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3485. (declare-function org-indent-mode "org-indent" (&optional arg))
  3486. (declare-function parse-time-string "parse-time" (string))
  3487. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3488. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3489. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3490. (defvar remember-data-file)
  3491. (defvar texmathp-why)
  3492. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3493. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3494. (defvar org-latex-regexps)
  3495. ;;; Autoload and prepare some org modules
  3496. ;; Some table stuff that needs to be defined here, because it is used
  3497. ;; by the functions setting up org-mode or checking for table context.
  3498. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3499. "Detect an org-type or table-type table.")
  3500. (defconst org-table-line-regexp "^[ \t]*|"
  3501. "Detect an org-type table line.")
  3502. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3503. "Detect an org-type table line.")
  3504. (defconst org-table-hline-regexp "^[ \t]*|-"
  3505. "Detect an org-type table hline.")
  3506. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3507. "Detect a table-type table hline.")
  3508. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3509. "Detect the first line outside a table when searching from within it.
  3510. This works for both table types.")
  3511. ;; Autoload the functions in org-table.el that are needed by functions here.
  3512. (eval-and-compile
  3513. (org-autoload "org-table"
  3514. '(org-table-begin org-table-blank-field org-table-end)))
  3515. ;;;###autoload
  3516. (defun turn-on-orgtbl ()
  3517. "Unconditionally turn on `orgtbl-mode'."
  3518. (require 'org-table)
  3519. (orgtbl-mode 1))
  3520. (defun org-at-table-p (&optional table-type)
  3521. "Return t if the cursor is inside an org-type table.
  3522. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3523. (if org-enable-table-editor
  3524. (save-excursion
  3525. (beginning-of-line 1)
  3526. (looking-at (if table-type org-table-any-line-regexp
  3527. org-table-line-regexp)))
  3528. nil))
  3529. (defsubst org-table-p () (org-at-table-p))
  3530. (defun org-at-table.el-p ()
  3531. "Return t if and only if we are at a table.el table."
  3532. (and (org-at-table-p 'any)
  3533. (save-excursion
  3534. (goto-char (org-table-begin 'any))
  3535. (looking-at org-table1-hline-regexp))))
  3536. (defun org-table-recognize-table.el ()
  3537. "If there is a table.el table nearby, recognize it and move into it."
  3538. (if org-table-tab-recognizes-table.el
  3539. (if (org-at-table.el-p)
  3540. (progn
  3541. (beginning-of-line 1)
  3542. (if (looking-at org-table-dataline-regexp)
  3543. nil
  3544. (if (looking-at org-table1-hline-regexp)
  3545. (progn
  3546. (beginning-of-line 2)
  3547. (if (looking-at org-table-any-border-regexp)
  3548. (beginning-of-line -1)))))
  3549. (if (re-search-forward "|" (org-table-end t) t)
  3550. (progn
  3551. (require 'table)
  3552. (if (table--at-cell-p (point))
  3553. t
  3554. (message "recognizing table.el table...")
  3555. (table-recognize-table)
  3556. (message "recognizing table.el table...done")))
  3557. (error "This should not happen"))
  3558. t)
  3559. nil)
  3560. nil))
  3561. (defun org-at-table-hline-p ()
  3562. "Return t if the cursor is inside a hline in a table."
  3563. (if org-enable-table-editor
  3564. (save-excursion
  3565. (beginning-of-line 1)
  3566. (looking-at org-table-hline-regexp))
  3567. nil))
  3568. (defvar org-table-clean-did-remove-column nil)
  3569. (defun org-table-map-tables (function &optional quietly)
  3570. "Apply FUNCTION to the start of all tables in the buffer."
  3571. (save-excursion
  3572. (save-restriction
  3573. (widen)
  3574. (goto-char (point-min))
  3575. (while (re-search-forward org-table-any-line-regexp nil t)
  3576. (unless quietly
  3577. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3578. (beginning-of-line 1)
  3579. (when (and (looking-at org-table-line-regexp)
  3580. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3581. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3582. (save-excursion (funcall function))
  3583. (or (looking-at org-table-line-regexp)
  3584. (forward-char 1)))
  3585. (re-search-forward org-table-any-border-regexp nil 1))))
  3586. (unless quietly (message "Mapping tables: done")))
  3587. ;; Declare and autoload functions from org-exp.el & Co
  3588. (declare-function org-default-export-plist "org-exp")
  3589. (declare-function org-infile-export-plist "org-exp")
  3590. (declare-function org-get-current-options "org-exp")
  3591. ;; Declare and autoload functions from org-agenda.el
  3592. (eval-and-compile
  3593. (org-autoload "org-agenda"
  3594. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3595. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3596. (declare-function org-clock-update-mode-line "org-clock" ())
  3597. (declare-function org-resolve-clocks "org-clock"
  3598. (&optional also-non-dangling-p prompt last-valid))
  3599. (defvar org-clock-start-time)
  3600. (defvar org-clock-marker (make-marker)
  3601. "Marker recording the last clock-in.")
  3602. (defvar org-clock-hd-marker (make-marker)
  3603. "Marker recording the last clock-in, but the headline position.")
  3604. (defvar org-clock-heading ""
  3605. "The heading of the current clock entry.")
  3606. (defun org-clock-is-active ()
  3607. "Return non-nil if clock is currently running.
  3608. The return value is actually the clock marker."
  3609. (marker-buffer org-clock-marker))
  3610. (eval-and-compile
  3611. (org-autoload "org-clock" '(org-clock-remove-overlays
  3612. org-clock-update-time-maybe
  3613. org-clocktable-shift)))
  3614. (defun org-check-running-clock ()
  3615. "Check if the current buffer contains the running clock.
  3616. If yes, offer to stop it and to save the buffer with the changes."
  3617. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3618. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3619. (buffer-name))))
  3620. (org-clock-out)
  3621. (when (y-or-n-p "Save changed buffer?")
  3622. (save-buffer))))
  3623. (defun org-clocktable-try-shift (dir n)
  3624. "Check if this line starts a clock table, if yes, shift the time block."
  3625. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3626. (org-clocktable-shift dir n)))
  3627. ;;;###autoload
  3628. (defun org-clock-persistence-insinuate ()
  3629. "Set up hooks for clock persistence."
  3630. (require 'org-clock)
  3631. (add-hook 'org-mode-hook 'org-clock-load)
  3632. (add-hook 'kill-emacs-hook 'org-clock-save))
  3633. ;; Define the variable already here, to make sure we have it.
  3634. (defvar org-indent-mode nil
  3635. "Non-nil if Org-Indent mode is enabled.
  3636. Use the command `org-indent-mode' to change this variable.")
  3637. ;; Autoload archiving code
  3638. ;; The stuff that is needed for cycling and tags has to be defined here.
  3639. (defgroup org-archive nil
  3640. "Options concerning archiving in Org-mode."
  3641. :tag "Org Archive"
  3642. :group 'org-structure)
  3643. (defcustom org-archive-location "%s_archive::"
  3644. "The location where subtrees should be archived.
  3645. The value of this variable is a string, consisting of two parts,
  3646. separated by a double-colon. The first part is a filename and
  3647. the second part is a headline.
  3648. When the filename is omitted, archiving happens in the same file.
  3649. %s in the filename will be replaced by the current file
  3650. name (without the directory part). Archiving to a different file
  3651. is useful to keep archived entries from contributing to the
  3652. Org-mode Agenda.
  3653. The archived entries will be filed as subtrees of the specified
  3654. headline. When the headline is omitted, the subtrees are simply
  3655. filed away at the end of the file, as top-level entries. Also in
  3656. the heading you can use %s to represent the file name, this can be
  3657. useful when using the same archive for a number of different files.
  3658. Here are a few examples:
  3659. \"%s_archive::\"
  3660. If the current file is Projects.org, archive in file
  3661. Projects.org_archive, as top-level trees. This is the default.
  3662. \"::* Archived Tasks\"
  3663. Archive in the current file, under the top-level headline
  3664. \"* Archived Tasks\".
  3665. \"~/org/archive.org::\"
  3666. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3667. \"~/org/archive.org::* From %s\"
  3668. Archive in file ~/org/archive.org (absolute path), under headlines
  3669. \"From FILENAME\" where file name is the current file name.
  3670. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3671. The \"datetree/\" string is special, signifying to archive
  3672. items to the datetree. Items are placed in either the CLOSED
  3673. date of the item, or the current date if there is no CLOSED date.
  3674. The heading will be a subentry to the current date. There doesn't
  3675. need to be a heading, but there always needs to be a slash after
  3676. datetree. For example, to store archived items directly in the
  3677. datetree, use \"~/org/datetree.org::datetree/\".
  3678. \"basement::** Finished Tasks\"
  3679. Archive in file ./basement (relative path), as level 3 trees
  3680. below the level 2 heading \"** Finished Tasks\".
  3681. You may set this option on a per-file basis by adding to the buffer a
  3682. line like
  3683. #+ARCHIVE: basement::** Finished Tasks
  3684. You may also define it locally for a subtree by setting an ARCHIVE property
  3685. in the entry. If such a property is found in an entry, or anywhere up
  3686. the hierarchy, it will be used."
  3687. :group 'org-archive
  3688. :type 'string)
  3689. (defcustom org-archive-tag "ARCHIVE"
  3690. "The tag that marks a subtree as archived.
  3691. An archived subtree does not open during visibility cycling, and does
  3692. not contribute to the agenda listings.
  3693. After changing this, font-lock must be restarted in the relevant buffers to
  3694. get the proper fontification."
  3695. :group 'org-archive
  3696. :group 'org-keywords
  3697. :type 'string)
  3698. (defcustom org-agenda-skip-archived-trees t
  3699. "Non-nil means the agenda will skip any items located in archived trees.
  3700. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3701. variable is no longer recommended, you should leave it at the value t.
  3702. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3703. :group 'org-archive
  3704. :group 'org-agenda-skip
  3705. :type 'boolean)
  3706. (defcustom org-columns-skip-archived-trees t
  3707. "Non-nil means ignore archived trees when creating column view."
  3708. :group 'org-archive
  3709. :group 'org-properties
  3710. :type 'boolean)
  3711. (defcustom org-cycle-open-archived-trees nil
  3712. "Non-nil means `org-cycle' will open archived trees.
  3713. An archived tree is a tree marked with the tag ARCHIVE.
  3714. When nil, archived trees will stay folded. You can still open them with
  3715. normal outline commands like `show-all', but not with the cycling commands."
  3716. :group 'org-archive
  3717. :group 'org-cycle
  3718. :type 'boolean)
  3719. (defcustom org-sparse-tree-open-archived-trees nil
  3720. "Non-nil means sparse tree construction shows matches in archived trees.
  3721. When nil, matches in these trees are highlighted, but the trees are kept in
  3722. collapsed state."
  3723. :group 'org-archive
  3724. :group 'org-sparse-trees
  3725. :type 'boolean)
  3726. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3727. "The default date type when building a sparse tree.
  3728. When this is nil, a date is a scheduled or a deadline timestamp.
  3729. Otherwise, these types are allowed:
  3730. all: all timestamps
  3731. active: only active timestamps (<...>)
  3732. inactive: only inactive timestamps (<...)
  3733. scheduled: only scheduled timestamps
  3734. deadline: only deadline timestamps"
  3735. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3736. (const :tag "All timestamps" all)
  3737. (const :tag "Only active timestamps" active)
  3738. (const :tag "Only inactive timestamps" inactive)
  3739. (const :tag "Only scheduled timestamps" scheduled)
  3740. (const :tag "Only deadline timestamps" deadline))
  3741. :version "24.3"
  3742. :group 'org-sparse-trees)
  3743. (defun org-cycle-hide-archived-subtrees (state)
  3744. "Re-hide all archived subtrees after a visibility state change."
  3745. (when (and (not org-cycle-open-archived-trees)
  3746. (not (memq state '(overview folded))))
  3747. (save-excursion
  3748. (let* ((globalp (memq state '(contents all)))
  3749. (beg (if globalp (point-min) (point)))
  3750. (end (if globalp (point-max) (org-end-of-subtree t))))
  3751. (org-hide-archived-subtrees beg end)
  3752. (goto-char beg)
  3753. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3754. (message "%s" (substitute-command-keys
  3755. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3756. (defun org-force-cycle-archived ()
  3757. "Cycle subtree even if it is archived."
  3758. (interactive)
  3759. (setq this-command 'org-cycle)
  3760. (let ((org-cycle-open-archived-trees t))
  3761. (call-interactively 'org-cycle)))
  3762. (defun org-hide-archived-subtrees (beg end)
  3763. "Re-hide all archived subtrees after a visibility state change."
  3764. (save-excursion
  3765. (let* ((re (concat ":" org-archive-tag ":")))
  3766. (goto-char beg)
  3767. (while (re-search-forward re end t)
  3768. (when (org-at-heading-p)
  3769. (org-flag-subtree t)
  3770. (org-end-of-subtree t))))))
  3771. (declare-function outline-end-of-heading "outline" ())
  3772. (declare-function outline-flag-region "outline" (from to flag))
  3773. (defun org-flag-subtree (flag)
  3774. (save-excursion
  3775. (org-back-to-heading t)
  3776. (outline-end-of-heading)
  3777. (outline-flag-region (point)
  3778. (progn (org-end-of-subtree t) (point))
  3779. flag)))
  3780. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3781. (eval-and-compile
  3782. (org-autoload "org-archive"
  3783. '(org-add-archive-files)))
  3784. ;; Autoload Column View Code
  3785. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3786. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3787. (declare-function org-columns-compute "org-colview" (property))
  3788. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3789. '(org-columns-number-to-string
  3790. org-columns-get-format-and-top-level
  3791. org-columns-compute
  3792. org-columns-remove-overlays))
  3793. ;; Autoload ID code
  3794. (declare-function org-id-store-link "org-id")
  3795. (declare-function org-id-locations-load "org-id")
  3796. (declare-function org-id-locations-save "org-id")
  3797. (defvar org-id-track-globally)
  3798. (org-autoload "org-id"
  3799. '(org-id-new
  3800. org-id-copy
  3801. org-id-get-with-outline-path-completion
  3802. org-id-get-with-outline-drilling))
  3803. ;;; Variables for pre-computed regular expressions, all buffer local
  3804. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3805. "Matches first line of a hidden block.")
  3806. (make-variable-buffer-local 'org-drawer-regexp)
  3807. (defvar org-todo-regexp nil
  3808. "Matches any of the TODO state keywords.")
  3809. (make-variable-buffer-local 'org-todo-regexp)
  3810. (defvar org-not-done-regexp nil
  3811. "Matches any of the TODO state keywords except the last one.")
  3812. (make-variable-buffer-local 'org-not-done-regexp)
  3813. (defvar org-not-done-heading-regexp nil
  3814. "Matches a TODO headline that is not done.")
  3815. (make-variable-buffer-local 'org-not-done-regexp)
  3816. (defvar org-todo-line-regexp nil
  3817. "Matches a headline and puts TODO state into group 2 if present.")
  3818. (make-variable-buffer-local 'org-todo-line-regexp)
  3819. (defvar org-complex-heading-regexp nil
  3820. "Matches a headline and puts everything into groups:
  3821. group 1: the stars
  3822. group 2: The todo keyword, maybe
  3823. group 3: Priority cookie
  3824. group 4: True headline
  3825. group 5: Tags")
  3826. (make-variable-buffer-local 'org-complex-heading-regexp)
  3827. (defvar org-complex-heading-regexp-format nil
  3828. "Printf format to make regexp to match an exact headline.
  3829. This regexp will match the headline of any node which has the
  3830. exact headline text that is put into the format, but may have any
  3831. TODO state, priority and tags.")
  3832. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3833. (defvar org-todo-line-tags-regexp nil
  3834. "Matches a headline and puts TODO state into group 2 if present.
  3835. Also put tags into group 4 if tags are present.")
  3836. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3837. (defvar org-ds-keyword-length 12
  3838. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3839. (make-variable-buffer-local 'org-ds-keyword-length)
  3840. (defvar org-deadline-regexp nil
  3841. "Matches the DEADLINE keyword.")
  3842. (make-variable-buffer-local 'org-deadline-regexp)
  3843. (defvar org-deadline-time-regexp nil
  3844. "Matches the DEADLINE keyword together with a time stamp.")
  3845. (make-variable-buffer-local 'org-deadline-time-regexp)
  3846. (defvar org-deadline-line-regexp nil
  3847. "Matches the DEADLINE keyword and the rest of the line.")
  3848. (make-variable-buffer-local 'org-deadline-line-regexp)
  3849. (defvar org-scheduled-regexp nil
  3850. "Matches the SCHEDULED keyword.")
  3851. (make-variable-buffer-local 'org-scheduled-regexp)
  3852. (defvar org-scheduled-time-regexp nil
  3853. "Matches the SCHEDULED keyword together with a time stamp.")
  3854. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3855. (defvar org-closed-time-regexp nil
  3856. "Matches the CLOSED keyword together with a time stamp.")
  3857. (make-variable-buffer-local 'org-closed-time-regexp)
  3858. (defvar org-keyword-time-regexp nil
  3859. "Matches any of the 4 keywords, together with the time stamp.")
  3860. (make-variable-buffer-local 'org-keyword-time-regexp)
  3861. (defvar org-keyword-time-not-clock-regexp nil
  3862. "Matches any of the 3 keywords, together with the time stamp.")
  3863. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3864. (defvar org-maybe-keyword-time-regexp nil
  3865. "Matches a timestamp, possibly preceded by a keyword.")
  3866. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3867. (defvar org-all-time-keywords nil
  3868. "List of time keywords.")
  3869. (make-variable-buffer-local 'org-all-time-keywords)
  3870. (defconst org-plain-time-of-day-regexp
  3871. (concat
  3872. "\\(\\<[012]?[0-9]"
  3873. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3874. "\\(--?"
  3875. "\\(\\<[012]?[0-9]"
  3876. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3877. "\\)?")
  3878. "Regular expression to match a plain time or time range.
  3879. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3880. groups carry important information:
  3881. 0 the full match
  3882. 1 the first time, range or not
  3883. 8 the second time, if it is a range.")
  3884. (defconst org-plain-time-extension-regexp
  3885. (concat
  3886. "\\(\\<[012]?[0-9]"
  3887. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3888. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3889. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3890. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3891. groups carry important information:
  3892. 0 the full match
  3893. 7 hours of duration
  3894. 9 minutes of duration")
  3895. (defconst org-stamp-time-of-day-regexp
  3896. (concat
  3897. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3898. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3899. "\\(--?"
  3900. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3901. "Regular expression to match a timestamp time or time range.
  3902. After a match, the following groups carry important information:
  3903. 0 the full match
  3904. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3905. 2 the first time, range or not
  3906. 4 the second time, if it is a range.")
  3907. (defconst org-startup-options
  3908. '(("fold" org-startup-folded t)
  3909. ("overview" org-startup-folded t)
  3910. ("nofold" org-startup-folded nil)
  3911. ("showall" org-startup-folded nil)
  3912. ("showeverything" org-startup-folded showeverything)
  3913. ("content" org-startup-folded content)
  3914. ("indent" org-startup-indented t)
  3915. ("noindent" org-startup-indented nil)
  3916. ("hidestars" org-hide-leading-stars t)
  3917. ("showstars" org-hide-leading-stars nil)
  3918. ("odd" org-odd-levels-only t)
  3919. ("oddeven" org-odd-levels-only nil)
  3920. ("align" org-startup-align-all-tables t)
  3921. ("noalign" org-startup-align-all-tables nil)
  3922. ("inlineimages" org-startup-with-inline-images t)
  3923. ("noinlineimages" org-startup-with-inline-images nil)
  3924. ("customtime" org-display-custom-times t)
  3925. ("logdone" org-log-done time)
  3926. ("lognotedone" org-log-done note)
  3927. ("nologdone" org-log-done nil)
  3928. ("lognoteclock-out" org-log-note-clock-out t)
  3929. ("nolognoteclock-out" org-log-note-clock-out nil)
  3930. ("logrepeat" org-log-repeat state)
  3931. ("lognoterepeat" org-log-repeat note)
  3932. ("logdrawer" org-log-into-drawer t)
  3933. ("nologdrawer" org-log-into-drawer nil)
  3934. ("logstatesreversed" org-log-states-order-reversed t)
  3935. ("nologstatesreversed" org-log-states-order-reversed nil)
  3936. ("nologrepeat" org-log-repeat nil)
  3937. ("logreschedule" org-log-reschedule time)
  3938. ("lognotereschedule" org-log-reschedule note)
  3939. ("nologreschedule" org-log-reschedule nil)
  3940. ("logredeadline" org-log-redeadline time)
  3941. ("lognoteredeadline" org-log-redeadline note)
  3942. ("nologredeadline" org-log-redeadline nil)
  3943. ("logrefile" org-log-refile time)
  3944. ("lognoterefile" org-log-refile note)
  3945. ("nologrefile" org-log-refile nil)
  3946. ("fninline" org-footnote-define-inline t)
  3947. ("nofninline" org-footnote-define-inline nil)
  3948. ("fnlocal" org-footnote-section nil)
  3949. ("fnauto" org-footnote-auto-label t)
  3950. ("fnprompt" org-footnote-auto-label nil)
  3951. ("fnconfirm" org-footnote-auto-label confirm)
  3952. ("fnplain" org-footnote-auto-label plain)
  3953. ("fnadjust" org-footnote-auto-adjust t)
  3954. ("nofnadjust" org-footnote-auto-adjust nil)
  3955. ("constcgs" constants-unit-system cgs)
  3956. ("constSI" constants-unit-system SI)
  3957. ("noptag" org-tag-persistent-alist nil)
  3958. ("hideblocks" org-hide-block-startup t)
  3959. ("nohideblocks" org-hide-block-startup nil)
  3960. ("beamer" org-startup-with-beamer-mode t)
  3961. ("entitiespretty" org-pretty-entities t)
  3962. ("entitiesplain" org-pretty-entities nil))
  3963. "Variable associated with STARTUP options for org-mode.
  3964. Each element is a list of three items: the startup options (as written
  3965. in the #+STARTUP line), the corresponding variable, and the value to set
  3966. this variable to if the option is found. An optional forth element PUSH
  3967. means to push this value onto the list in the variable.")
  3968. (defun org-update-property-plist (key val props)
  3969. "Update PROPS with KEY and VAL."
  3970. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3971. (key (if appending (substring key 0 (- (length key) 1)) key))
  3972. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3973. (previous (cdr (assoc key props))))
  3974. (if appending
  3975. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3976. (cons (cons key val) remainder))))
  3977. (defconst org-block-regexp
  3978. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3979. "Regular expression for hiding blocks.")
  3980. (defconst org-heading-keyword-regexp-format
  3981. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3982. "Printf format for a regexp matching an headline with some keyword.
  3983. This regexp will match the headline of any node which has the
  3984. exact keyword that is put into the format. The keyword isn't in
  3985. any group by default, but the stars and the body are.")
  3986. (defconst org-heading-keyword-maybe-regexp-format
  3987. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3988. "Printf format for a regexp matching an headline, possibly with some keyword.
  3989. This regexp can match any headline with the specified keyword, or
  3990. without a keyword. The keyword isn't in any group by default,
  3991. but the stars and the body are.")
  3992. (defun org-set-regexps-and-options ()
  3993. "Precompute regular expressions for current buffer."
  3994. (when (derived-mode-p 'org-mode)
  3995. (org-set-local 'org-todo-kwd-alist nil)
  3996. (org-set-local 'org-todo-key-alist nil)
  3997. (org-set-local 'org-todo-key-trigger nil)
  3998. (org-set-local 'org-todo-keywords-1 nil)
  3999. (org-set-local 'org-done-keywords nil)
  4000. (org-set-local 'org-todo-heads nil)
  4001. (org-set-local 'org-todo-sets nil)
  4002. (org-set-local 'org-todo-log-states nil)
  4003. (org-set-local 'org-file-properties nil)
  4004. (org-set-local 'org-file-tags nil)
  4005. (let ((re (org-make-options-regexp
  4006. '("CATEGORY" "TODO" "COLUMNS"
  4007. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  4008. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  4009. "OPTIONS")
  4010. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4011. (splitre "[ \t]+")
  4012. (scripts org-use-sub-superscripts)
  4013. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4014. tail sep kws1 prio props ftags drawers beamer-p
  4015. ext-setup-or-nil setup-contents (start 0))
  4016. (save-excursion
  4017. (save-restriction
  4018. (widen)
  4019. (goto-char (point-min))
  4020. (while (or (and ext-setup-or-nil
  4021. (string-match re ext-setup-or-nil start)
  4022. (setq start (match-end 0)))
  4023. (and (setq ext-setup-or-nil nil start 0)
  4024. (re-search-forward re nil t)))
  4025. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4026. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4027. (if (stringp value) (setq value (org-trim value)))
  4028. (cond
  4029. ((equal key "CATEGORY")
  4030. (setq cat value))
  4031. ((member key '("SEQ_TODO" "TODO"))
  4032. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4033. ((equal key "TYP_TODO")
  4034. (push (cons 'type (org-split-string value splitre)) kwds))
  4035. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4036. ;; general TODO-like setup
  4037. (push (cons (intern (downcase (match-string 1 key)))
  4038. (org-split-string value splitre)) kwds))
  4039. ((equal key "TAGS")
  4040. (setq tags (append tags (if tags '("\\n") nil)
  4041. (org-split-string value splitre))))
  4042. ((equal key "COLUMNS")
  4043. (org-set-local 'org-columns-default-format value))
  4044. ((equal key "LINK")
  4045. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4046. (push (cons (match-string 1 value)
  4047. (org-trim (match-string 2 value)))
  4048. links)))
  4049. ((equal key "PRIORITIES")
  4050. (setq prio (org-split-string value " +")))
  4051. ((equal key "PROPERTY")
  4052. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4053. (setq props (org-update-property-plist (match-string 1 value)
  4054. (match-string 2 value)
  4055. props))))
  4056. ((equal key "FILETAGS")
  4057. (when (string-match "\\S-" value)
  4058. (setq ftags
  4059. (append
  4060. ftags
  4061. (apply 'append
  4062. (mapcar (lambda (x) (org-split-string x ":"))
  4063. (org-split-string value)))))))
  4064. ((equal key "DRAWERS")
  4065. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4066. ((equal key "CONSTANTS")
  4067. (setq const (append const (org-split-string value splitre))))
  4068. ((equal key "STARTUP")
  4069. (let ((opts (org-split-string value splitre))
  4070. l var val)
  4071. (while (setq l (pop opts))
  4072. (when (setq l (assoc l org-startup-options))
  4073. (setq var (nth 1 l) val (nth 2 l))
  4074. (if (not (nth 3 l))
  4075. (set (make-local-variable var) val)
  4076. (if (not (listp (symbol-value var)))
  4077. (set (make-local-variable var) nil))
  4078. (set (make-local-variable var) (symbol-value var))
  4079. (add-to-list var val))))))
  4080. ((equal key "ARCHIVE")
  4081. (setq arch value)
  4082. (remove-text-properties 0 (length arch)
  4083. '(face t fontified t) arch))
  4084. ((equal key "LATEX_CLASS")
  4085. (setq beamer-p (equal value "beamer")))
  4086. ((equal key "OPTIONS")
  4087. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4088. (setq scripts (read (match-string 2 value)))))
  4089. ((equal key "SETUPFILE")
  4090. (setq setup-contents (org-file-contents
  4091. (expand-file-name
  4092. (org-remove-double-quotes value))
  4093. 'noerror))
  4094. (if (not ext-setup-or-nil)
  4095. (setq ext-setup-or-nil setup-contents start 0)
  4096. (setq ext-setup-or-nil
  4097. (concat (substring ext-setup-or-nil 0 start)
  4098. "\n" setup-contents "\n"
  4099. (substring ext-setup-or-nil start)))))))
  4100. ;; search for property blocks
  4101. (goto-char (point-min))
  4102. (while (re-search-forward org-block-regexp nil t)
  4103. (when (equal "PROPERTY" (upcase (match-string 1)))
  4104. (setq value (replace-regexp-in-string
  4105. "[\n\r]" " " (match-string 4)))
  4106. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4107. (setq props (org-update-property-plist (match-string 1 value)
  4108. (match-string 2 value)
  4109. props)))))))
  4110. (org-set-local 'org-use-sub-superscripts scripts)
  4111. (when cat
  4112. (org-set-local 'org-category (intern cat))
  4113. (push (cons "CATEGORY" cat) props))
  4114. (when prio
  4115. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4116. (setq prio (mapcar 'string-to-char prio))
  4117. (org-set-local 'org-highest-priority (nth 0 prio))
  4118. (org-set-local 'org-lowest-priority (nth 1 prio))
  4119. (org-set-local 'org-default-priority (nth 2 prio)))
  4120. (and props (org-set-local 'org-file-properties (nreverse props)))
  4121. (and ftags (org-set-local 'org-file-tags
  4122. (mapcar 'org-add-prop-inherited ftags)))
  4123. (and drawers (org-set-local 'org-drawers drawers))
  4124. (and arch (org-set-local 'org-archive-location arch))
  4125. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4126. ;; Process the TODO keywords
  4127. (unless kwds
  4128. ;; Use the global values as if they had been given locally.
  4129. (setq kwds (default-value 'org-todo-keywords))
  4130. (if (stringp (car kwds))
  4131. (setq kwds (list (cons org-todo-interpretation
  4132. (default-value 'org-todo-keywords)))))
  4133. (setq kwds (reverse kwds)))
  4134. (setq kwds (nreverse kwds))
  4135. (let (inter kws kw)
  4136. (while (setq kws (pop kwds))
  4137. (let ((kws (or
  4138. (run-hook-with-args-until-success
  4139. 'org-todo-setup-filter-hook kws)
  4140. kws)))
  4141. (setq inter (pop kws) sep (member "|" kws)
  4142. kws0 (delete "|" (copy-sequence kws))
  4143. kwsa nil
  4144. kws1 (mapcar
  4145. (lambda (x)
  4146. ;; 1 2
  4147. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4148. (progn
  4149. (setq kw (match-string 1 x)
  4150. key (and (match-end 2) (match-string 2 x))
  4151. log (org-extract-log-state-settings x))
  4152. (push (cons kw (and key (string-to-char key))) kwsa)
  4153. (and log (push log org-todo-log-states))
  4154. kw)
  4155. (error "Invalid TODO keyword %s" x)))
  4156. kws0)
  4157. kwsa (if kwsa (append '((:startgroup))
  4158. (nreverse kwsa)
  4159. '((:endgroup))))
  4160. hw (car kws1)
  4161. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4162. tail (list inter hw (car dws) (org-last dws))))
  4163. (add-to-list 'org-todo-heads hw 'append)
  4164. (push kws1 org-todo-sets)
  4165. (setq org-done-keywords (append org-done-keywords dws nil))
  4166. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4167. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4168. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4169. (setq org-todo-sets (nreverse org-todo-sets)
  4170. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4171. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4172. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4173. ;; Process the constants
  4174. (when const
  4175. (let (e cst)
  4176. (while (setq e (pop const))
  4177. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4178. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4179. (setq org-table-formula-constants-local cst)))
  4180. ;; Process the tags.
  4181. (when tags
  4182. (let (e tgs)
  4183. (while (setq e (pop tags))
  4184. (cond
  4185. ((equal e "{") (push '(:startgroup) tgs))
  4186. ((equal e "}") (push '(:endgroup) tgs))
  4187. ((equal e "\\n") (push '(:newline) tgs))
  4188. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4189. (push (cons (match-string 1 e)
  4190. (string-to-char (match-string 2 e)))
  4191. tgs))
  4192. (t (push (list e) tgs))))
  4193. (org-set-local 'org-tag-alist nil)
  4194. (while (setq e (pop tgs))
  4195. (or (and (stringp (car e))
  4196. (assoc (car e) org-tag-alist))
  4197. (push e org-tag-alist)))))
  4198. ;; Compute the regular expressions and other local variables.
  4199. ;; Using `org-outline-regexp-bol' would complicate them much,
  4200. ;; because of the fixed white space at the end of that string.
  4201. (if (not org-done-keywords)
  4202. (setq org-done-keywords (and org-todo-keywords-1
  4203. (list (org-last org-todo-keywords-1)))))
  4204. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4205. (length org-scheduled-string)
  4206. (length org-clock-string)
  4207. (length org-closed-string)))
  4208. org-drawer-regexp
  4209. (concat "^[ \t]*:\\("
  4210. (mapconcat 'regexp-quote org-drawers "\\|")
  4211. "\\):[ \t]*$")
  4212. org-not-done-keywords
  4213. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4214. org-todo-regexp
  4215. (concat "\\("
  4216. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4217. "\\)")
  4218. org-not-done-regexp
  4219. (concat "\\("
  4220. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4221. "\\)")
  4222. org-not-done-heading-regexp
  4223. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4224. org-todo-line-regexp
  4225. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4226. org-complex-heading-regexp
  4227. (concat "^\\(\\*+\\)"
  4228. "\\(?: +" org-todo-regexp "\\)?"
  4229. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4230. "\\(?: +\\(.*?\\)\\)??"
  4231. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4232. "[ \t]*$")
  4233. org-complex-heading-regexp-format
  4234. (concat "^\\(\\*+\\)"
  4235. "\\(?: +" org-todo-regexp "\\)?"
  4236. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4237. "\\(?: +"
  4238. ;; Stats cookies can be stuck to body.
  4239. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4240. "\\(%s\\)"
  4241. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4242. "\\)"
  4243. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4244. "[ \t]*$")
  4245. org-todo-line-tags-regexp
  4246. (concat "^\\(\\*+\\)"
  4247. "\\(?: +" org-todo-regexp "\\)?"
  4248. "\\(?: +\\(.*?\\)\\)??"
  4249. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4250. "[ \t]*$")
  4251. org-deadline-regexp (concat "\\<" org-deadline-string)
  4252. org-deadline-time-regexp
  4253. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4254. org-deadline-line-regexp
  4255. (concat "\\<\\(" org-deadline-string "\\).*")
  4256. org-scheduled-regexp
  4257. (concat "\\<" org-scheduled-string)
  4258. org-scheduled-time-regexp
  4259. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4260. org-closed-time-regexp
  4261. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4262. org-keyword-time-regexp
  4263. (concat "\\<\\(" org-scheduled-string
  4264. "\\|" org-deadline-string
  4265. "\\|" org-closed-string
  4266. "\\|" org-clock-string "\\)"
  4267. " *[[<]\\([^]>]+\\)[]>]")
  4268. org-keyword-time-not-clock-regexp
  4269. (concat "\\<\\(" org-scheduled-string
  4270. "\\|" org-deadline-string
  4271. "\\|" org-closed-string
  4272. "\\)"
  4273. " *[[<]\\([^]>]+\\)[]>]")
  4274. org-maybe-keyword-time-regexp
  4275. (concat "\\(\\<\\(" org-scheduled-string
  4276. "\\|" org-deadline-string
  4277. "\\|" org-closed-string
  4278. "\\|" org-clock-string "\\)\\)?"
  4279. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4280. org-all-time-keywords
  4281. (mapcar (lambda (w) (substring w 0 -1))
  4282. (list org-scheduled-string org-deadline-string
  4283. org-clock-string org-closed-string))
  4284. )
  4285. (org-compute-latex-and-specials-regexp)
  4286. (org-set-font-lock-defaults))))
  4287. (defun org-file-contents (file &optional noerror)
  4288. "Return the contents of FILE, as a string."
  4289. (if (or (not file)
  4290. (not (file-readable-p file)))
  4291. (if noerror
  4292. (progn
  4293. (message "Cannot read file \"%s\"" file)
  4294. (ding) (sit-for 2)
  4295. "")
  4296. (error "Cannot read file \"%s\"" file))
  4297. (with-temp-buffer
  4298. (insert-file-contents file)
  4299. (buffer-string))))
  4300. (defun org-extract-log-state-settings (x)
  4301. "Extract the log state setting from a TODO keyword string.
  4302. This will extract info from a string like \"WAIT(w@/!)\"."
  4303. (let (kw key log1 log2)
  4304. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4305. (setq kw (match-string 1 x)
  4306. key (and (match-end 2) (match-string 2 x))
  4307. log1 (and (match-end 3) (match-string 3 x))
  4308. log2 (and (match-end 4) (match-string 4 x)))
  4309. (and (or log1 log2)
  4310. (list kw
  4311. (and log1 (if (equal log1 "!") 'time 'note))
  4312. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4313. (defun org-remove-keyword-keys (list)
  4314. "Remove a pair of parenthesis at the end of each string in LIST."
  4315. (mapcar (lambda (x)
  4316. (if (string-match "(.*)$" x)
  4317. (substring x 0 (match-beginning 0))
  4318. x))
  4319. list))
  4320. (defun org-assign-fast-keys (alist)
  4321. "Assign fast keys to a keyword-key alist.
  4322. Respect keys that are already there."
  4323. (let (new e (alt ?0))
  4324. (while (setq e (pop alist))
  4325. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4326. (cdr e)) ;; Key already assigned.
  4327. (push e new)
  4328. (let ((clist (string-to-list (downcase (car e))))
  4329. (used (append new alist)))
  4330. (when (= (car clist) ?@)
  4331. (pop clist))
  4332. (while (and clist (rassoc (car clist) used))
  4333. (pop clist))
  4334. (unless clist
  4335. (while (rassoc alt used)
  4336. (incf alt)))
  4337. (push (cons (car e) (or (car clist) alt)) new))))
  4338. (nreverse new)))
  4339. ;;; Some variables used in various places
  4340. (defvar org-window-configuration nil
  4341. "Used in various places to store a window configuration.")
  4342. (defvar org-selected-window nil
  4343. "Used in various places to store a window configuration.")
  4344. (defvar org-finish-function nil
  4345. "Function to be called when `C-c C-c' is used.
  4346. This is for getting out of special buffers like capture.")
  4347. ;; FIXME: Occasionally check by commenting these, to make sure
  4348. ;; no other functions uses these, forgetting to let-bind them.
  4349. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4350. (defvar org-last-state)
  4351. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4352. ;; Defined somewhere in this file, but used before definition.
  4353. (defvar org-entities) ;; defined in org-entities.el
  4354. (defvar org-struct-menu)
  4355. (defvar org-org-menu)
  4356. (defvar org-tbl-menu)
  4357. ;;;; Define the Org-mode
  4358. ;; We use a before-change function to check if a table might need
  4359. ;; an update.
  4360. (defvar org-table-may-need-update t
  4361. "Indicates that a table might need an update.
  4362. This variable is set by `org-before-change-function'.
  4363. `org-table-align' sets it back to nil.")
  4364. (defun org-before-change-function (beg end)
  4365. "Every change indicates that a table might need an update."
  4366. (setq org-table-may-need-update t))
  4367. (defvar org-mode-map)
  4368. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4369. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4370. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4371. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4372. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4373. (defvar org-table-buffer-is-an nil)
  4374. (defvar bidi-paragraph-direction)
  4375. (defvar buffer-face-mode-face)
  4376. (require 'outline)
  4377. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4378. (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"))
  4379. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4380. ;; Other stuff we need.
  4381. (require 'time-date)
  4382. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4383. (require 'easymenu)
  4384. (require 'overlay)
  4385. (require 'org-macs)
  4386. (require 'org-entities)
  4387. ;; (require 'org-compat) moved higher up in the file before it is first used
  4388. (require 'org-faces)
  4389. (require 'org-list)
  4390. (require 'org-pcomplete)
  4391. (require 'org-src)
  4392. (require 'org-footnote)
  4393. ;; babel
  4394. (require 'ob)
  4395. ;;;###autoload
  4396. (define-derived-mode org-mode outline-mode "Org"
  4397. "Outline-based notes management and organizer, alias
  4398. \"Carsten's outline-mode for keeping track of everything.\"
  4399. Org-mode develops organizational tasks around a NOTES file which
  4400. contains information about projects as plain text. Org-mode is
  4401. implemented on top of outline-mode, which is ideal to keep the content
  4402. of large files well structured. It supports ToDo items, deadlines and
  4403. time stamps, which magically appear in the diary listing of the Emacs
  4404. calendar. Tables are easily created with a built-in table editor.
  4405. Plain text URL-like links connect to websites, emails (VM), Usenet
  4406. messages (Gnus), BBDB entries, and any files related to the project.
  4407. For printing and sharing of notes, an Org-mode file (or a part of it)
  4408. can be exported as a structured ASCII or HTML file.
  4409. The following commands are available:
  4410. \\{org-mode-map}"
  4411. ;; Get rid of Outline menus, they are not needed
  4412. ;; Need to do this here because define-derived-mode sets up
  4413. ;; the keymap so late. Still, it is a waste to call this each time
  4414. ;; we switch another buffer into org-mode.
  4415. (if (featurep 'xemacs)
  4416. (when (boundp 'outline-mode-menu-heading)
  4417. ;; Assume this is Greg's port, it uses easymenu
  4418. (easy-menu-remove outline-mode-menu-heading)
  4419. (easy-menu-remove outline-mode-menu-show)
  4420. (easy-menu-remove outline-mode-menu-hide))
  4421. (define-key org-mode-map [menu-bar headings] 'undefined)
  4422. (define-key org-mode-map [menu-bar hide] 'undefined)
  4423. (define-key org-mode-map [menu-bar show] 'undefined))
  4424. (org-load-modules-maybe)
  4425. (easy-menu-add org-org-menu)
  4426. (easy-menu-add org-tbl-menu)
  4427. (org-install-agenda-files-menu)
  4428. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4429. (add-to-invisibility-spec '(org-cwidth))
  4430. (add-to-invisibility-spec '(org-hide-block . t))
  4431. (when (featurep 'xemacs)
  4432. (org-set-local 'line-move-ignore-invisible t))
  4433. (org-set-local 'outline-regexp org-outline-regexp)
  4434. (org-set-local 'outline-level 'org-outline-level)
  4435. (setq bidi-paragraph-direction 'left-to-right)
  4436. (when (and org-ellipsis
  4437. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4438. (fboundp 'make-glyph-code))
  4439. (unless org-display-table
  4440. (setq org-display-table (make-display-table)))
  4441. (set-display-table-slot
  4442. org-display-table 4
  4443. (vconcat (mapcar
  4444. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4445. org-ellipsis)))
  4446. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4447. (setq buffer-display-table org-display-table))
  4448. (org-set-regexps-and-options)
  4449. (when (and org-tag-faces (not org-tags-special-faces-re))
  4450. ;; tag faces set outside customize.... force initialization.
  4451. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4452. ;; Calc embedded
  4453. (org-set-local 'calc-embedded-open-mode "# ")
  4454. (modify-syntax-entry ?@ "w")
  4455. (modify-syntax-entry ?\" "\"")
  4456. (if org-startup-truncated (setq truncate-lines t))
  4457. (org-set-local 'font-lock-unfontify-region-function
  4458. 'org-unfontify-region)
  4459. ;; Activate before-change-function
  4460. (org-set-local 'org-table-may-need-update t)
  4461. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4462. 'local)
  4463. ;; Check for running clock before killing a buffer
  4464. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4465. ;; Initialize macros templates.
  4466. (org-macro-initialize-templates)
  4467. ;; Initialize radio targets.
  4468. (org-update-radio-target-regexp)
  4469. ;; Indentation.
  4470. (org-set-local 'indent-line-function 'org-indent-line)
  4471. (org-set-local 'indent-region-function 'org-indent-region)
  4472. ;; Filling and auto-filling.
  4473. (org-setup-filling)
  4474. ;; Comments.
  4475. (org-setup-comments-handling)
  4476. ;; Beginning/end of defun
  4477. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4478. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4479. ;; Next error for sparse trees
  4480. (org-set-local 'next-error-function 'org-occur-next-match)
  4481. ;; Make sure dependence stuff works reliably, even for users who set it
  4482. ;; too late :-(
  4483. (if org-enforce-todo-dependencies
  4484. (add-hook 'org-blocker-hook
  4485. 'org-block-todo-from-children-or-siblings-or-parent)
  4486. (remove-hook 'org-blocker-hook
  4487. 'org-block-todo-from-children-or-siblings-or-parent))
  4488. (if org-enforce-todo-checkbox-dependencies
  4489. (add-hook 'org-blocker-hook
  4490. 'org-block-todo-from-checkboxes)
  4491. (remove-hook 'org-blocker-hook
  4492. 'org-block-todo-from-checkboxes))
  4493. ;; Align options lines
  4494. (org-set-local
  4495. 'align-mode-rules-list
  4496. '((org-in-buffer-settings
  4497. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4498. (modes . '(org-mode)))))
  4499. ;; Imenu
  4500. (org-set-local 'imenu-create-index-function
  4501. 'org-imenu-get-tree)
  4502. ;; Make isearch reveal context
  4503. (if (or (featurep 'xemacs)
  4504. (not (boundp 'outline-isearch-open-invisible-function)))
  4505. ;; Emacs 21 and XEmacs make use of the hook
  4506. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4507. ;; Emacs 22 deals with this through a special variable
  4508. (org-set-local 'outline-isearch-open-invisible-function
  4509. (lambda (&rest ignore) (org-show-context 'isearch))))
  4510. ;; Turn on org-beamer-mode?
  4511. (and org-startup-with-beamer-mode (org-beamer-mode))
  4512. ;; Setup the pcomplete hooks
  4513. (set (make-local-variable 'pcomplete-command-completion-function)
  4514. 'org-pcomplete-initial)
  4515. (set (make-local-variable 'pcomplete-command-name-function)
  4516. 'org-command-at-point)
  4517. (set (make-local-variable 'pcomplete-default-completion-function)
  4518. 'ignore)
  4519. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4520. 'org-parse-arguments)
  4521. (set (make-local-variable 'pcomplete-termination-string) "")
  4522. (when (>= emacs-major-version 23)
  4523. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4524. ;; If empty file that did not turn on org-mode automatically, make it to.
  4525. (if (and org-insert-mode-line-in-empty-file
  4526. (org-called-interactively-p 'any)
  4527. (= (point-min) (point-max)))
  4528. (insert "# -*- mode: org -*-\n\n"))
  4529. (unless org-inhibit-startup
  4530. (when org-startup-align-all-tables
  4531. (let ((bmp (buffer-modified-p)))
  4532. (org-table-map-tables 'org-table-align 'quietly)
  4533. (set-buffer-modified-p bmp)))
  4534. (when org-startup-with-inline-images
  4535. (org-display-inline-images))
  4536. (when org-startup-indented
  4537. (require 'org-indent)
  4538. (org-indent-mode 1))
  4539. (unless org-inhibit-startup-visibility-stuff
  4540. (org-set-startup-visibility)))
  4541. ;; Try to set org-hide correctly
  4542. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4543. (when (fboundp 'abbrev-table-put)
  4544. (abbrev-table-put org-mode-abbrev-table
  4545. :parents (list text-mode-abbrev-table)))
  4546. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4547. (defun org-find-invisible-foreground ()
  4548. (let ((candidates (remove
  4549. "unspecified-bg"
  4550. (nconc
  4551. (list (face-background 'default)
  4552. (face-background 'org-default))
  4553. (mapcar
  4554. (lambda (alist)
  4555. (when (boundp alist)
  4556. (cdr (assoc 'background-color (symbol-value alist)))))
  4557. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4558. (list (face-foreground 'org-hide))))))
  4559. (car (remove nil candidates))))
  4560. (defun org-current-time (&optional rounding-minutes)
  4561. "Current time, possibly rounded to ROUNDING-MINUTES.
  4562. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4563. `org-time-stamp-rounding-minutes'."
  4564. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4565. (car org-time-stamp-rounding-minutes)))
  4566. (time (decode-time)))
  4567. (if (> r 1)
  4568. (apply 'encode-time
  4569. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4570. (nthcdr 2 time)))
  4571. (current-time))))
  4572. (defun org-today ()
  4573. "Return today date, considering `org-extend-today-until'."
  4574. (time-to-days
  4575. (time-subtract (current-time)
  4576. (list 0 (* 3600 org-extend-today-until) 0))))
  4577. ;;;; Font-Lock stuff, including the activators
  4578. (defvar org-mouse-map (make-sparse-keymap))
  4579. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4580. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4581. (when org-mouse-1-follows-link
  4582. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4583. (when org-tab-follows-link
  4584. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4585. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4586. (require 'font-lock)
  4587. (defconst org-non-link-chars "]\t\n\r<>")
  4588. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4589. "shell" "elisp" "doi" "message"))
  4590. (defvar org-link-types-re nil
  4591. "Matches a link that has a url-like prefix like \"http:\"")
  4592. (defvar org-link-re-with-space nil
  4593. "Matches a link with spaces, optional angular brackets around it.")
  4594. (defvar org-link-re-with-space2 nil
  4595. "Matches a link with spaces, optional angular brackets around it.")
  4596. (defvar org-link-re-with-space3 nil
  4597. "Matches a link with spaces, only for internal part in bracket links.")
  4598. (defvar org-angle-link-re nil
  4599. "Matches link with angular brackets, spaces are allowed.")
  4600. (defvar org-plain-link-re nil
  4601. "Matches plain link, without spaces.")
  4602. (defvar org-bracket-link-regexp nil
  4603. "Matches a link in double brackets.")
  4604. (defvar org-bracket-link-analytic-regexp nil
  4605. "Regular expression used to analyze links.
  4606. Here is what the match groups contain after a match:
  4607. 1: http:
  4608. 2: http
  4609. 3: path
  4610. 4: [desc]
  4611. 5: desc")
  4612. (defvar org-bracket-link-analytic-regexp++ nil
  4613. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4614. (defvar org-any-link-re nil
  4615. "Regular expression matching any link.")
  4616. (defcustom org-match-sexp-depth 3
  4617. "Number of stacked braces for sub/superscript matching.
  4618. This has to be set before loading org.el to be effective."
  4619. :group 'org-export-translation ; ??????????????????????????/
  4620. :type 'integer)
  4621. (defun org-create-multibrace-regexp (left right n)
  4622. "Create a regular expression which will match a balanced sexp.
  4623. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4624. as single character strings.
  4625. The regexp returned will match the entire expression including the
  4626. delimiters. It will also define a single group which contains the
  4627. match except for the outermost delimiters. The maximum depth of
  4628. stacked delimiters is N. Escaping delimiters is not possible."
  4629. (let* ((nothing (concat "[^" left right "]*?"))
  4630. (or "\\|")
  4631. (re nothing)
  4632. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4633. (while (> n 1)
  4634. (setq n (1- n)
  4635. re (concat re or next)
  4636. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4637. (concat left "\\(" re "\\)" right)))
  4638. (defvar org-match-substring-regexp
  4639. (concat
  4640. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4641. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4642. "\\|"
  4643. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4644. "\\|"
  4645. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4646. "The regular expression matching a sub- or superscript.")
  4647. (defvar org-match-substring-with-braces-regexp
  4648. (concat
  4649. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4650. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4651. "\\)")
  4652. "The regular expression matching a sub- or superscript, forcing braces.")
  4653. (defun org-make-link-regexps ()
  4654. "Update the link regular expressions.
  4655. This should be called after the variable `org-link-types' has changed."
  4656. (setq org-link-types-re
  4657. (concat
  4658. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4659. org-link-re-with-space
  4660. (concat
  4661. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4662. "\\([^" org-non-link-chars " ]"
  4663. "[^" org-non-link-chars "]*"
  4664. "[^" org-non-link-chars " ]\\)>?")
  4665. org-link-re-with-space2
  4666. (concat
  4667. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4668. "\\([^" org-non-link-chars " ]"
  4669. "[^\t\n\r]*"
  4670. "[^" org-non-link-chars " ]\\)>?")
  4671. org-link-re-with-space3
  4672. (concat
  4673. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4674. "\\([^" org-non-link-chars " ]"
  4675. "[^\t\n\r]*\\)")
  4676. org-angle-link-re
  4677. (concat
  4678. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4679. "\\([^" org-non-link-chars " ]"
  4680. "[^" org-non-link-chars "]*"
  4681. "\\)>")
  4682. org-plain-link-re
  4683. (concat
  4684. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4685. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4686. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4687. org-bracket-link-regexp
  4688. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4689. org-bracket-link-analytic-regexp
  4690. (concat
  4691. "\\[\\["
  4692. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4693. "\\([^]]+\\)"
  4694. "\\]"
  4695. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4696. "\\]")
  4697. org-bracket-link-analytic-regexp++
  4698. (concat
  4699. "\\[\\["
  4700. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4701. "\\([^]]+\\)"
  4702. "\\]"
  4703. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4704. "\\]")
  4705. org-any-link-re
  4706. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4707. org-angle-link-re "\\)\\|\\("
  4708. org-plain-link-re "\\)")))
  4709. (org-make-link-regexps)
  4710. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4711. "Regular expression for fast time stamp matching.")
  4712. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4713. "Regular expression for fast time stamp matching.")
  4714. (defconst org-ts-regexp0
  4715. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4716. "Regular expression matching time strings for analysis.
  4717. This one does not require the space after the date, so it can be used
  4718. on a string that terminates immediately after the date.")
  4719. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4720. "Regular expression matching time strings for analysis.")
  4721. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4722. "Regular expression matching time stamps, with groups.")
  4723. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4724. "Regular expression matching time stamps (also [..]), with groups.")
  4725. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4726. "Regular expression matching a time stamp range.")
  4727. (defconst org-tr-regexp-both
  4728. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4729. "Regular expression matching a time stamp range.")
  4730. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4731. org-ts-regexp "\\)?")
  4732. "Regular expression matching a time stamp or time stamp range.")
  4733. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4734. org-ts-regexp-both "\\)?")
  4735. "Regular expression matching a time stamp or time stamp range.
  4736. The time stamps may be either active or inactive.")
  4737. (defvar org-emph-face nil)
  4738. (defun org-do-emphasis-faces (limit)
  4739. "Run through the buffer and add overlays to emphasized strings."
  4740. (let (rtn a)
  4741. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4742. (if (not (= (char-after (match-beginning 3))
  4743. (char-after (match-beginning 4))))
  4744. (progn
  4745. (setq rtn t)
  4746. (setq a (assoc (match-string 3) org-emphasis-alist))
  4747. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4748. 'face
  4749. (nth 1 a))
  4750. (and (nth 4 a)
  4751. (org-remove-flyspell-overlays-in
  4752. (match-beginning 0) (match-end 0)))
  4753. (add-text-properties (match-beginning 2) (match-end 2)
  4754. '(font-lock-multiline t org-emphasis t))
  4755. (when org-hide-emphasis-markers
  4756. (add-text-properties (match-end 4) (match-beginning 5)
  4757. '(invisible org-link))
  4758. (add-text-properties (match-beginning 3) (match-end 3)
  4759. '(invisible org-link)))))
  4760. (backward-char 1))
  4761. rtn))
  4762. (defun org-emphasize (&optional char)
  4763. "Insert or change an emphasis, i.e. a font like bold or italic.
  4764. If there is an active region, change that region to a new emphasis.
  4765. If there is no region, just insert the marker characters and position
  4766. the cursor between them.
  4767. CHAR should be either the marker character, or the first character of the
  4768. HTML tag associated with that emphasis. If CHAR is a space, the means
  4769. to remove the emphasis of the selected region.
  4770. If char is not given (for example in an interactive call) it
  4771. will be prompted for."
  4772. (interactive)
  4773. (let ((eal org-emphasis-alist) e det
  4774. (erc org-emphasis-regexp-components)
  4775. (prompt "")
  4776. (string "") beg end move tag c s)
  4777. (if (org-region-active-p)
  4778. (setq beg (region-beginning) end (region-end)
  4779. string (buffer-substring beg end))
  4780. (setq move t))
  4781. (while (setq e (pop eal))
  4782. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4783. c (aref tag 0))
  4784. (push (cons c (string-to-char (car e))) det)
  4785. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4786. (substring tag 1)))))
  4787. (setq det (nreverse det))
  4788. (unless char
  4789. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4790. (setq char (read-char-exclusive)))
  4791. (setq char (or (cdr (assoc char det)) char))
  4792. (if (equal char ?\ )
  4793. (setq s "" move nil)
  4794. (unless (assoc (char-to-string char) org-emphasis-alist)
  4795. (error "No such emphasis marker: \"%c\"" char))
  4796. (setq s (char-to-string char)))
  4797. (while (and (> (length string) 1)
  4798. (equal (substring string 0 1) (substring string -1))
  4799. (assoc (substring string 0 1) org-emphasis-alist))
  4800. (setq string (substring string 1 -1)))
  4801. (setq string (concat s string s))
  4802. (if beg (delete-region beg end))
  4803. (unless (or (bolp)
  4804. (string-match (concat "[" (nth 0 erc) "\n]")
  4805. (char-to-string (char-before (point)))))
  4806. (insert " "))
  4807. (unless (or (eobp)
  4808. (string-match (concat "[" (nth 1 erc) "\n]")
  4809. (char-to-string (char-after (point)))))
  4810. (insert " ") (backward-char 1))
  4811. (insert string)
  4812. (and move (backward-char 1))))
  4813. (defconst org-nonsticky-props
  4814. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4815. (defsubst org-rear-nonsticky-at (pos)
  4816. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4817. (defun org-activate-plain-links (limit)
  4818. "Run through the buffer and add overlays to links."
  4819. (catch 'exit
  4820. (let (f hl)
  4821. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4822. (not (org-in-src-block-p)))
  4823. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4824. (setq f (get-text-property (match-beginning 0) 'face))
  4825. (setq hl (org-match-string-no-properties 0))
  4826. (if (or (eq f 'org-tag)
  4827. (and (listp f) (memq 'org-tag f)))
  4828. nil
  4829. (add-text-properties (match-beginning 0) (match-end 0)
  4830. (list 'mouse-face 'highlight
  4831. 'face 'org-link
  4832. 'htmlize-link `(:uri ,hl)
  4833. 'keymap org-mouse-map))
  4834. (org-rear-nonsticky-at (match-end 0)))
  4835. t))))
  4836. (defun org-activate-code (limit)
  4837. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4838. (progn
  4839. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4840. (remove-text-properties (match-beginning 0) (match-end 0)
  4841. '(display t invisible t intangible t))
  4842. t)))
  4843. (defcustom org-src-fontify-natively nil
  4844. "When non-nil, fontify code in code blocks."
  4845. :type 'boolean
  4846. :version "24.1"
  4847. :group 'org-appearance
  4848. :group 'org-babel)
  4849. (defcustom org-allow-promoting-top-level-subtree nil
  4850. "When non-nil, allow promoting a top level subtree.
  4851. The leading star of the top level headline will be replaced
  4852. by a #."
  4853. :type 'boolean
  4854. :version "24.1"
  4855. :group 'org-appearance)
  4856. (defun org-fontify-meta-lines-and-blocks (limit)
  4857. (condition-case nil
  4858. (org-fontify-meta-lines-and-blocks-1 limit)
  4859. (error (message "org-mode fontification error"))))
  4860. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4861. "Fontify #+ lines and blocks, in the correct ways."
  4862. (let ((case-fold-search t))
  4863. (if (re-search-forward
  4864. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4865. limit t)
  4866. (let ((beg (match-beginning 0))
  4867. (block-start (match-end 0))
  4868. (block-end nil)
  4869. (lang (match-string 7))
  4870. (beg1 (line-beginning-position 2))
  4871. (dc1 (downcase (match-string 2)))
  4872. (dc3 (downcase (match-string 3)))
  4873. end end1 quoting block-type ovl)
  4874. (cond
  4875. ((member dc1 '("+html:" "+ascii:" "+latex:" "+docbook:"))
  4876. ;; a single line of backend-specific content
  4877. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4878. (remove-text-properties (match-beginning 0) (match-end 0)
  4879. '(display t invisible t intangible t))
  4880. (add-text-properties (match-beginning 1) (match-end 3)
  4881. '(font-lock-fontified t face org-meta-line))
  4882. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4883. '(font-lock-fontified t face org-block))
  4884. ; for backend-specific code
  4885. t)
  4886. ((and (match-end 4) (equal dc3 "+begin"))
  4887. ;; Truly a block
  4888. (setq block-type (downcase (match-string 5))
  4889. quoting (member block-type org-protecting-blocks))
  4890. (when (re-search-forward
  4891. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4892. nil t) ;; on purpose, we look further than LIMIT
  4893. (setq end (min (point-max) (match-end 0))
  4894. end1 (min (point-max) (1- (match-beginning 0))))
  4895. (setq block-end (match-beginning 0))
  4896. (when quoting
  4897. (remove-text-properties beg end
  4898. '(display t invisible t intangible t)))
  4899. (add-text-properties
  4900. beg end
  4901. '(font-lock-fontified t font-lock-multiline t))
  4902. (add-text-properties beg beg1 '(face org-meta-line))
  4903. (add-text-properties end1 (min (point-max) (1+ end))
  4904. '(face org-meta-line)) ; for end_src
  4905. (cond
  4906. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4907. (org-src-font-lock-fontify-block lang block-start block-end)
  4908. ;; remove old background overlays
  4909. (mapc (lambda (ov)
  4910. (if (eq (overlay-get ov 'face) 'org-block-background)
  4911. (delete-overlay ov)))
  4912. (overlays-at (/ (+ beg1 block-end) 2)))
  4913. ;; add a background overlay
  4914. (setq ovl (make-overlay beg1 block-end))
  4915. (overlay-put ovl 'face 'org-block-background)
  4916. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4917. (quoting
  4918. (add-text-properties beg1 (min (point-max) (1+ end1))
  4919. '(face org-block))) ; end of source block
  4920. ((not org-fontify-quote-and-verse-blocks))
  4921. ((string= block-type "quote")
  4922. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4923. ((string= block-type "verse")
  4924. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4925. (add-text-properties beg beg1 '(face org-block-begin-line))
  4926. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4927. '(face org-block-end-line))
  4928. t))
  4929. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  4930. (add-text-properties
  4931. beg (match-end 3)
  4932. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4933. '(font-lock-fontified t invisible t)
  4934. '(font-lock-fontified t face org-document-info-keyword)))
  4935. (add-text-properties
  4936. (match-beginning 6) (match-end 6)
  4937. (if (string-equal dc1 "+title:")
  4938. '(font-lock-fontified t face org-document-title)
  4939. '(font-lock-fontified t face org-document-info))))
  4940. ((or (equal dc1 "+results")
  4941. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  4942. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  4943. "+call:" "+header:" "+headers:" "+name:"))
  4944. (and (match-end 4) (equal dc3 "+attr")))
  4945. (add-text-properties
  4946. beg (match-end 0)
  4947. '(font-lock-fontified t face org-meta-line))
  4948. t)
  4949. ((member dc3 '(" " ""))
  4950. (add-text-properties
  4951. beg (match-end 0)
  4952. '(font-lock-fontified t face font-lock-comment-face)))
  4953. ((not (member (char-after beg) '(?\ ?\t)))
  4954. ;; just any other in-buffer setting, but not indented
  4955. (add-text-properties
  4956. beg (match-end 0)
  4957. '(font-lock-fontified t face org-meta-line))
  4958. t)
  4959. (t nil))))))
  4960. (defun org-activate-angle-links (limit)
  4961. "Run through the buffer and add overlays to links."
  4962. (if (and (re-search-forward org-angle-link-re limit t)
  4963. (not (org-in-src-block-p)))
  4964. (progn
  4965. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4966. (add-text-properties (match-beginning 0) (match-end 0)
  4967. (list 'mouse-face 'highlight
  4968. 'keymap org-mouse-map))
  4969. (org-rear-nonsticky-at (match-end 0))
  4970. t)))
  4971. (defun org-activate-footnote-links (limit)
  4972. "Run through the buffer and add overlays to footnotes."
  4973. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4974. (when fn
  4975. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4976. (org-remove-flyspell-overlays-in beg end)
  4977. (add-text-properties beg end
  4978. (list 'mouse-face 'highlight
  4979. 'keymap org-mouse-map
  4980. 'help-echo
  4981. (if (= (point-at-bol) beg)
  4982. "Footnote definition"
  4983. "Footnote reference")
  4984. 'font-lock-fontified t
  4985. 'font-lock-multiline t
  4986. 'face 'org-footnote))))))
  4987. (defun org-activate-bracket-links (limit)
  4988. "Run through the buffer and add overlays to bracketed links."
  4989. (if (and (re-search-forward org-bracket-link-regexp limit t)
  4990. (not (org-in-src-block-p)))
  4991. (let* ((hl (org-match-string-no-properties 1))
  4992. (help (concat "LINK: " hl))
  4993. ;; FIXME: Above we should remove the escapes. But that
  4994. ;; requires another match, protecting match data, a lot
  4995. ;; of overhead for font-lock.
  4996. (ip (org-maybe-intangible
  4997. (list 'invisible 'org-link
  4998. 'keymap org-mouse-map 'mouse-face 'highlight
  4999. 'font-lock-multiline t 'help-echo help
  5000. 'htmlize-link `(:uri ,hl))))
  5001. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5002. 'font-lock-multiline t 'help-echo help
  5003. 'htmlize-link `(:uri ,hl))))
  5004. ;; We need to remove the invisible property here. Table narrowing
  5005. ;; may have made some of this invisible.
  5006. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5007. (remove-text-properties (match-beginning 0) (match-end 0)
  5008. '(invisible nil))
  5009. (if (match-end 3)
  5010. (progn
  5011. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5012. (org-rear-nonsticky-at (match-beginning 3))
  5013. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5014. (org-rear-nonsticky-at (match-end 3))
  5015. (add-text-properties (match-end 3) (match-end 0) ip)
  5016. (org-rear-nonsticky-at (match-end 0)))
  5017. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5018. (org-rear-nonsticky-at (match-beginning 1))
  5019. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5020. (org-rear-nonsticky-at (match-end 1))
  5021. (add-text-properties (match-end 1) (match-end 0) ip)
  5022. (org-rear-nonsticky-at (match-end 0)))
  5023. t)))
  5024. (defun org-activate-dates (limit)
  5025. "Run through the buffer and add overlays to dates."
  5026. (if (re-search-forward org-tsr-regexp-both limit t)
  5027. (progn
  5028. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5029. (add-text-properties (match-beginning 0) (match-end 0)
  5030. (list 'mouse-face 'highlight
  5031. 'keymap org-mouse-map))
  5032. (org-rear-nonsticky-at (match-end 0))
  5033. (when org-display-custom-times
  5034. (if (match-end 3)
  5035. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5036. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5037. t)))
  5038. (defvar org-target-link-regexp nil
  5039. "Regular expression matching radio targets in plain text.")
  5040. (make-variable-buffer-local 'org-target-link-regexp)
  5041. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5042. "Regular expression matching a link target.")
  5043. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5044. "Regular expression matching a radio target.")
  5045. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5046. "Regular expression matching any target.")
  5047. (defun org-activate-target-links (limit)
  5048. "Run through the buffer and add overlays to target matches."
  5049. (when org-target-link-regexp
  5050. (let ((case-fold-search t))
  5051. (if (re-search-forward org-target-link-regexp limit t)
  5052. (progn
  5053. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5054. (add-text-properties (match-beginning 0) (match-end 0)
  5055. (list 'mouse-face 'highlight
  5056. 'keymap org-mouse-map
  5057. 'help-echo "Radio target link"
  5058. 'org-linked-text t))
  5059. (org-rear-nonsticky-at (match-end 0))
  5060. t)))))
  5061. (defun org-update-radio-target-regexp ()
  5062. "Find all radio targets in this file and update the regular expression."
  5063. (interactive)
  5064. (when (memq 'radio org-activate-links)
  5065. (setq org-target-link-regexp
  5066. (org-make-target-link-regexp (org-all-targets 'radio)))
  5067. (org-restart-font-lock)))
  5068. (defun org-hide-wide-columns (limit)
  5069. (let (s e)
  5070. (setq s (text-property-any (point) (or limit (point-max))
  5071. 'org-cwidth t))
  5072. (when s
  5073. (setq e (next-single-property-change s 'org-cwidth))
  5074. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5075. (goto-char e)
  5076. t)))
  5077. (defvar org-latex-and-specials-regexp nil
  5078. "Regular expression for highlighting export special stuff.")
  5079. (defvar org-match-substring-regexp)
  5080. (defvar org-match-substring-with-braces-regexp)
  5081. ;; This should be with the exporter code, but we also use if for font-locking
  5082. (defconst org-export-html-special-string-regexps
  5083. '(("\\\\-" . "&shy;")
  5084. ("---\\([^-]\\)" . "&mdash;\\1")
  5085. ("--\\([^-]\\)" . "&ndash;\\1")
  5086. ("\\.\\.\\." . "&hellip;"))
  5087. "Regular expressions for special string conversion.")
  5088. (defun org-compute-latex-and-specials-regexp ()
  5089. "Compute regular expression for stuff treated specially by exporters."
  5090. (if (not org-highlight-latex-fragments-and-specials)
  5091. (org-set-local 'org-latex-and-specials-regexp nil)
  5092. (require 'org-exp)
  5093. (let*
  5094. ((matchers (plist-get org-format-latex-options :matchers))
  5095. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  5096. org-latex-regexps)))
  5097. (org-export-allow-BIND nil)
  5098. (options (org-combine-plists (org-default-export-plist)
  5099. (org-infile-export-plist)))
  5100. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  5101. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  5102. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  5103. (org-export-html-expand (plist-get options :expand-quoted-html))
  5104. (org-export-with-special-strings (plist-get options :special-strings))
  5105. (re-sub
  5106. (cond
  5107. ((equal org-export-with-sub-superscripts '{})
  5108. (list org-match-substring-with-braces-regexp))
  5109. (org-export-with-sub-superscripts
  5110. (list org-match-substring-regexp))))
  5111. (re-latex
  5112. (if org-export-with-LaTeX-fragments
  5113. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5114. (re-macros
  5115. (if org-export-with-TeX-macros
  5116. (list (concat "\\\\"
  5117. (regexp-opt
  5118. (append
  5119. (delq nil
  5120. (mapcar 'car-safe
  5121. (append org-entities-user
  5122. org-entities)))
  5123. (if (boundp 'org-latex-entities)
  5124. (mapcar (lambda (x)
  5125. (or (car-safe x) x))
  5126. org-latex-entities)
  5127. nil))
  5128. 'words))) ; FIXME
  5129. ))
  5130. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5131. (re-special (if org-export-with-special-strings
  5132. (mapcar (lambda (x) (car x))
  5133. org-export-html-special-string-regexps)))
  5134. (re-rest
  5135. (delq nil
  5136. (list
  5137. (if org-export-html-expand "@<[^>\n]+>")
  5138. ))))
  5139. (org-set-local
  5140. 'org-latex-and-specials-regexp
  5141. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5142. re-rest) "\\|")))))
  5143. (defun org-do-latex-and-special-faces (limit)
  5144. "Run through the buffer and add overlays to links."
  5145. (when org-latex-and-specials-regexp
  5146. (let (rtn d)
  5147. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5148. limit t))
  5149. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5150. 'face))
  5151. '(org-code org-verbatim underline)))
  5152. (progn
  5153. (setq rtn t
  5154. d (cond ((member (char-after (1+ (match-beginning 0)))
  5155. '(?_ ?^)) 1)
  5156. (t 0)))
  5157. (font-lock-prepend-text-property
  5158. (+ d (match-beginning 0)) (match-end 0)
  5159. 'face 'org-latex-and-export-specials)
  5160. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5161. '(font-lock-multiline t)))))
  5162. rtn)))
  5163. (defun org-restart-font-lock ()
  5164. "Restart `font-lock-mode', to force refontification."
  5165. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5166. (font-lock-mode -1)
  5167. (font-lock-mode 1)))
  5168. (defun org-all-targets (&optional radio)
  5169. "Return a list of all targets in this file.
  5170. When optional argument RADIO is non-nil, only find radio
  5171. targets."
  5172. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5173. (save-excursion
  5174. (goto-char (point-min))
  5175. (while (re-search-forward re nil t)
  5176. ;; Make sure point is really within the object.
  5177. (backward-char)
  5178. (let ((obj (org-element-context)))
  5179. (when (memq (org-element-type obj) '(radio-target target))
  5180. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5181. rtn)))
  5182. (defun org-make-target-link-regexp (targets)
  5183. "Make regular expression matching all strings in TARGETS.
  5184. The regular expression finds the targets also if there is a line break
  5185. between words."
  5186. (and targets
  5187. (concat
  5188. "\\<\\("
  5189. (mapconcat
  5190. (lambda (x)
  5191. (setq x (regexp-quote x))
  5192. (while (string-match " +" x)
  5193. (setq x (replace-match "\\s-+" t t x)))
  5194. x)
  5195. targets
  5196. "\\|")
  5197. "\\)\\>")))
  5198. (defun org-activate-tags (limit)
  5199. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5200. (progn
  5201. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5202. (add-text-properties (match-beginning 1) (match-end 1)
  5203. (list 'mouse-face 'highlight
  5204. 'keymap org-mouse-map))
  5205. (org-rear-nonsticky-at (match-end 1))
  5206. t)))
  5207. (defun org-outline-level ()
  5208. "Compute the outline level of the heading at point.
  5209. If this is called at a normal headline, the level is the number of stars.
  5210. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5211. (save-excursion
  5212. (if (not (condition-case nil
  5213. (org-back-to-heading t)
  5214. (error nil)))
  5215. 0
  5216. (looking-at org-outline-regexp)
  5217. (1- (- (match-end 0) (match-beginning 0))))))
  5218. (defvar org-font-lock-keywords nil)
  5219. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5220. "Regular expression matching a property line.")
  5221. (defvar org-font-lock-hook nil
  5222. "Functions to be called for special font lock stuff.")
  5223. (defvar org-font-lock-set-keywords-hook nil
  5224. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5225. This is called after `org-font-lock-extra-keywords' is defined, but before
  5226. it is installed to be used by font lock. This can be useful if something
  5227. needs to be inserted at a specific position in the font-lock sequence.")
  5228. (defun org-font-lock-hook (limit)
  5229. "Run `org-font-lock-hook' within LIMIT."
  5230. (run-hook-with-args 'org-font-lock-hook limit))
  5231. (defun org-set-font-lock-defaults ()
  5232. "Set font lock defaults for the current buffer."
  5233. (let* ((em org-fontify-emphasized-text)
  5234. (lk org-activate-links)
  5235. (org-font-lock-extra-keywords
  5236. (list
  5237. ;; Call the hook
  5238. '(org-font-lock-hook)
  5239. ;; Headlines
  5240. `(,(if org-fontify-whole-heading-line
  5241. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5242. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5243. (1 (org-get-level-face 1))
  5244. (2 (org-get-level-face 2))
  5245. (3 (org-get-level-face 3)))
  5246. ;; Table lines
  5247. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5248. (1 'org-table t))
  5249. ;; Table internals
  5250. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5251. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5252. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5253. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5254. ;; Drawers
  5255. (list org-drawer-regexp '(0 'org-special-keyword t))
  5256. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5257. ;; Properties
  5258. (list org-property-re
  5259. '(1 'org-special-keyword t)
  5260. '(3 'org-property-value t))
  5261. ;; Links
  5262. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5263. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5264. (if (memq 'plain lk) '(org-activate-plain-links))
  5265. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5266. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5267. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5268. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5269. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5270. '(org-hide-wide-columns (0 nil append))
  5271. ;; TODO keyword
  5272. (list (format org-heading-keyword-regexp-format
  5273. org-todo-regexp)
  5274. '(2 (org-get-todo-face 2) t))
  5275. ;; DONE
  5276. (if org-fontify-done-headline
  5277. (list (format org-heading-keyword-regexp-format
  5278. (concat
  5279. "\\(?:"
  5280. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5281. "\\)"))
  5282. '(2 'org-headline-done t))
  5283. nil)
  5284. ;; Priorities
  5285. '(org-font-lock-add-priority-faces)
  5286. ;; Tags
  5287. '(org-font-lock-add-tag-faces)
  5288. ;; Special keywords
  5289. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5290. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5291. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5292. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5293. ;; Emphasis
  5294. (if em
  5295. (if (featurep 'xemacs)
  5296. '(org-do-emphasis-faces (0 nil append))
  5297. '(org-do-emphasis-faces)))
  5298. ;; Checkboxes
  5299. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5300. 1 'org-checkbox prepend)
  5301. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5302. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5303. (0 (org-get-checkbox-statistics-face) t)))
  5304. ;; Description list items
  5305. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5306. 1 'org-list-dt prepend)
  5307. ;; ARCHIVEd headings
  5308. (list (concat
  5309. org-outline-regexp-bol
  5310. "\\(.*:" org-archive-tag ":.*\\)")
  5311. '(1 'org-archived prepend))
  5312. ;; Specials
  5313. '(org-do-latex-and-special-faces)
  5314. '(org-fontify-entities)
  5315. '(org-raise-scripts)
  5316. ;; Code
  5317. '(org-activate-code (1 'org-code t))
  5318. ;; COMMENT
  5319. (list (format org-heading-keyword-regexp-format
  5320. (concat "\\("
  5321. org-comment-string "\\|" org-quote-string
  5322. "\\)"))
  5323. '(2 'org-special-keyword t))
  5324. ;; Blocks and meta lines
  5325. '(org-fontify-meta-lines-and-blocks)
  5326. )))
  5327. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5328. (run-hooks 'org-font-lock-set-keywords-hook)
  5329. ;; Now set the full font-lock-keywords
  5330. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5331. (org-set-local 'font-lock-defaults
  5332. '(org-font-lock-keywords t nil nil backward-paragraph))
  5333. (kill-local-variable 'font-lock-keywords) nil))
  5334. (defun org-toggle-pretty-entities ()
  5335. "Toggle the composition display of entities as UTF8 characters."
  5336. (interactive)
  5337. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5338. (org-restart-font-lock)
  5339. (if org-pretty-entities
  5340. (message "Entities are displayed as UTF8 characters")
  5341. (save-restriction
  5342. (widen)
  5343. (org-decompose-region (point-min) (point-max))
  5344. (message "Entities are displayed plain"))))
  5345. (defvar org-custom-properties-overlays nil
  5346. "List of overlays used for custom properties.")
  5347. (make-variable-buffer-local 'org-custom-properties-overlays)
  5348. (defun org-toggle-custom-properties-visibility ()
  5349. "Display or hide properties in `org-custom-properties'."
  5350. (interactive)
  5351. (if org-custom-properties-overlays
  5352. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5353. (setq org-custom-properties-overlays nil))
  5354. (unless (not org-custom-properties)
  5355. (save-excursion
  5356. (save-restriction
  5357. (widen)
  5358. (goto-char (point-min))
  5359. (while (re-search-forward org-property-re nil t)
  5360. (mapc (lambda(p)
  5361. (when (equal p (substring (match-string 1) 1 -1))
  5362. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5363. (overlay-put o 'invisible t)
  5364. (overlay-put o 'org-custom-property t)
  5365. (push o org-custom-properties-overlays))))
  5366. org-custom-properties)))))))
  5367. (defun org-fontify-entities (limit)
  5368. "Find an entity to fontify."
  5369. (let (ee)
  5370. (when org-pretty-entities
  5371. (catch 'match
  5372. (while (re-search-forward
  5373. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5374. limit t)
  5375. (if (and (not (org-in-indented-comment-line))
  5376. (setq ee (org-entity-get (match-string 1)))
  5377. (= (length (nth 6 ee)) 1))
  5378. (let*
  5379. ((end (if (equal (match-string 2) "{}")
  5380. (match-end 2)
  5381. (match-end 1))))
  5382. (add-text-properties
  5383. (match-beginning 0) end
  5384. (list 'font-lock-fontified t))
  5385. (compose-region (match-beginning 0) end
  5386. (nth 6 ee) nil)
  5387. (backward-char 1)
  5388. (throw 'match t))))
  5389. nil))))
  5390. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5391. "Fontify string S like in Org-mode."
  5392. (with-temp-buffer
  5393. (insert s)
  5394. (let ((org-odd-levels-only odd-levels))
  5395. (org-mode)
  5396. (font-lock-fontify-buffer)
  5397. (buffer-string))))
  5398. (defvar org-m nil)
  5399. (defvar org-l nil)
  5400. (defvar org-f nil)
  5401. (defun org-get-level-face (n)
  5402. "Get the right face for match N in font-lock matching of headlines."
  5403. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5404. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5405. (if org-cycle-level-faces
  5406. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5407. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5408. (cond
  5409. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5410. ((eq n 2) org-f)
  5411. (t (if org-level-color-stars-only nil org-f))))
  5412. (defun org-get-todo-face (kwd)
  5413. "Get the right face for a TODO keyword KWD.
  5414. If KWD is a number, get the corresponding match group."
  5415. (if (numberp kwd) (setq kwd (match-string kwd)))
  5416. (or (org-face-from-face-or-color
  5417. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5418. (and (member kwd org-done-keywords) 'org-done)
  5419. 'org-todo))
  5420. (defun org-face-from-face-or-color (context inherit face-or-color)
  5421. "Create a face list that inherits INHERIT, but sets the foreground color.
  5422. When FACE-OR-COLOR is not a string, just return it."
  5423. (if (stringp face-or-color)
  5424. (list :inherit inherit
  5425. (cdr (assoc context org-faces-easy-properties))
  5426. face-or-color)
  5427. face-or-color))
  5428. (defun org-font-lock-add-tag-faces (limit)
  5429. "Add the special tag faces."
  5430. (when (and org-tag-faces org-tags-special-faces-re)
  5431. (while (re-search-forward org-tags-special-faces-re limit t)
  5432. (add-text-properties (match-beginning 1) (match-end 1)
  5433. (list 'face (org-get-tag-face 1)
  5434. 'font-lock-fontified t))
  5435. (backward-char 1))))
  5436. (defun org-font-lock-add-priority-faces (limit)
  5437. "Add the special priority faces."
  5438. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5439. (when (save-match-data (org-at-heading-p))
  5440. (add-text-properties
  5441. (match-beginning 0) (match-end 0)
  5442. (list 'face (or (org-face-from-face-or-color
  5443. 'priority 'org-special-keyword
  5444. (cdr (assoc (char-after (match-beginning 1))
  5445. org-priority-faces)))
  5446. 'org-special-keyword)
  5447. 'font-lock-fontified t)))))
  5448. (defun org-get-tag-face (kwd)
  5449. "Get the right face for a TODO keyword KWD.
  5450. If KWD is a number, get the corresponding match group."
  5451. (if (numberp kwd) (setq kwd (match-string kwd)))
  5452. (or (org-face-from-face-or-color
  5453. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5454. 'org-tag))
  5455. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5456. "Remove fontification and activation overlays from links."
  5457. (font-lock-default-unfontify-region beg end)
  5458. (let* ((buffer-undo-list t)
  5459. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5460. (inhibit-modification-hooks t)
  5461. deactivate-mark buffer-file-name buffer-file-truename)
  5462. (org-decompose-region beg end)
  5463. (remove-text-properties beg end
  5464. '(mouse-face t keymap t org-linked-text t
  5465. invisible t intangible t
  5466. org-no-flyspell t org-emphasis t))
  5467. (org-remove-font-lock-display-properties beg end)))
  5468. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5469. ((raise 0.3) (height 0.7))
  5470. ((raise -0.5))
  5471. ((raise 0.5)))
  5472. "Display properties for showing superscripts and subscripts.")
  5473. (defun org-remove-font-lock-display-properties (beg end)
  5474. "Remove specific display properties that have been added by font lock.
  5475. The will remove the raise properties that are used to show superscripts
  5476. and subscripts."
  5477. (let (next prop)
  5478. (while (< beg end)
  5479. (setq next (next-single-property-change beg 'display nil end)
  5480. prop (get-text-property beg 'display))
  5481. (if (member prop org-script-display)
  5482. (put-text-property beg next 'display nil))
  5483. (setq beg next))))
  5484. (defun org-raise-scripts (limit)
  5485. "Add raise properties to sub/superscripts."
  5486. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5487. (if (re-search-forward
  5488. (if (eq org-use-sub-superscripts t)
  5489. org-match-substring-regexp
  5490. org-match-substring-with-braces-regexp)
  5491. limit t)
  5492. (let* ((pos (point)) table-p comment-p
  5493. (mpos (match-beginning 3))
  5494. (emph-p (get-text-property mpos 'org-emphasis))
  5495. (link-p (get-text-property mpos 'mouse-face))
  5496. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5497. (goto-char (point-at-bol))
  5498. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5499. comment-p (org-looking-at-p "[ \t]*#"))
  5500. (goto-char pos)
  5501. ;; FIXME: Should we go back one character here, for a_b^c
  5502. ;; (goto-char (1- pos)) ;????????????????????
  5503. (if (or comment-p emph-p link-p keyw-p)
  5504. t
  5505. (put-text-property (match-beginning 3) (match-end 0)
  5506. 'display
  5507. (if (equal (char-after (match-beginning 2)) ?^)
  5508. (nth (if table-p 3 1) org-script-display)
  5509. (nth (if table-p 2 0) org-script-display)))
  5510. (add-text-properties (match-beginning 2) (match-end 2)
  5511. (list 'invisible t
  5512. 'org-dwidth t 'org-dwidth-n 1))
  5513. (if (and (eq (char-after (match-beginning 3)) ?{)
  5514. (eq (char-before (match-end 3)) ?}))
  5515. (progn
  5516. (add-text-properties
  5517. (match-beginning 3) (1+ (match-beginning 3))
  5518. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5519. (add-text-properties
  5520. (1- (match-end 3)) (match-end 3)
  5521. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5522. t)))))
  5523. ;;;; Visibility cycling, including org-goto and indirect buffer
  5524. ;;; Cycling
  5525. (defvar org-cycle-global-status nil)
  5526. (make-variable-buffer-local 'org-cycle-global-status)
  5527. (defvar org-cycle-subtree-status nil)
  5528. (make-variable-buffer-local 'org-cycle-subtree-status)
  5529. (defvar org-inlinetask-min-level)
  5530. ;;;###autoload
  5531. (defun org-cycle (&optional arg)
  5532. "TAB-action and visibility cycling for Org-mode.
  5533. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5534. is outline visibility cycling, but it also invokes other actions
  5535. in special contexts.
  5536. - When this function is called with a prefix argument, rotate the entire
  5537. buffer through 3 states (global cycling)
  5538. 1. OVERVIEW: Show only top-level headlines.
  5539. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5540. 3. SHOW ALL: Show everything.
  5541. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5542. determined by the variable `org-startup-folded', and by any VISIBILITY
  5543. properties in the buffer.
  5544. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5545. including any drawers.
  5546. - When inside a table, re-align the table and move to the next field.
  5547. - When point is at the beginning of a headline, rotate the subtree started
  5548. by this line through 3 different states (local cycling)
  5549. 1. FOLDED: Only the main headline is shown.
  5550. 2. CHILDREN: The main headline and the direct children are shown.
  5551. From this state, you can move to one of the children
  5552. and zoom in further.
  5553. 3. SUBTREE: Show the entire subtree, including body text.
  5554. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5555. - When point is at the beginning of an empty headline and the variable
  5556. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5557. of the headline by demoting and promoting it to likely levels. This
  5558. speeds up creation document structure by pressing TAB once or several
  5559. times right after creating a new headline.
  5560. - When there is a numeric prefix, go up to a heading with level ARG, do
  5561. a `show-subtree' and return to the previous cursor position. If ARG
  5562. is negative, go up that many levels.
  5563. - When point is not at the beginning of a headline, execute the global
  5564. binding for TAB, which is re-indenting the line. See the option
  5565. `org-cycle-emulate-tab' for details.
  5566. - Special case: if point is at the beginning of the buffer and there is
  5567. no headline in line 1, this function will act as if called with prefix arg
  5568. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5569. But only if also the variable `org-cycle-global-at-bob' is t."
  5570. (interactive "P")
  5571. (org-load-modules-maybe)
  5572. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5573. (and org-cycle-level-after-item/entry-creation
  5574. (or (org-cycle-level)
  5575. (org-cycle-item-indentation))))
  5576. (let* (message-log-max ; Don't populate the *Messages* buffer
  5577. (limit-level
  5578. (or org-cycle-max-level
  5579. (and (boundp 'org-inlinetask-min-level)
  5580. org-inlinetask-min-level
  5581. (1- org-inlinetask-min-level))))
  5582. (nstars (and limit-level
  5583. (if org-odd-levels-only
  5584. (and limit-level (1- (* limit-level 2)))
  5585. limit-level)))
  5586. (org-outline-regexp
  5587. (if (not (derived-mode-p 'org-mode))
  5588. outline-regexp
  5589. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5590. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5591. (not (looking-at org-outline-regexp))))
  5592. (org-cycle-hook
  5593. (if bob-special
  5594. (delq 'org-optimize-window-after-visibility-change
  5595. (copy-sequence org-cycle-hook))
  5596. org-cycle-hook))
  5597. (pos (point)))
  5598. (if (or bob-special (equal arg '(4)))
  5599. ;; special case: use global cycling
  5600. (setq arg t))
  5601. (cond
  5602. ((equal arg '(16))
  5603. (setq last-command 'dummy)
  5604. (org-set-startup-visibility)
  5605. (message "Startup visibility, plus VISIBILITY properties"))
  5606. ((equal arg '(64))
  5607. (show-all)
  5608. (message "Entire buffer visible, including drawers"))
  5609. ;; Table: enter it or move to the next field.
  5610. ((org-at-table-p 'any)
  5611. (if (org-at-table.el-p)
  5612. (message "Use C-c ' to edit table.el tables")
  5613. (if arg (org-table-edit-field t)
  5614. (org-table-justify-field-maybe)
  5615. (call-interactively 'org-table-next-field))))
  5616. ((run-hook-with-args-until-success
  5617. 'org-tab-after-check-for-table-hook))
  5618. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5619. ((eq arg t) (org-cycle-internal-global))
  5620. ;; Drawers: delegate to `org-flag-drawer'.
  5621. ((and org-drawers org-drawer-regexp
  5622. (save-excursion
  5623. (beginning-of-line 1)
  5624. (looking-at org-drawer-regexp)))
  5625. (org-flag-drawer ; toggle block visibility
  5626. (not (get-char-property (match-end 0) 'invisible))))
  5627. ;; Show-subtree, ARG levels up from here.
  5628. ((integerp arg)
  5629. (save-excursion
  5630. (org-back-to-heading)
  5631. (outline-up-heading (if (< arg 0) (- arg)
  5632. (- (funcall outline-level) arg)))
  5633. (org-show-subtree)))
  5634. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5635. ((and (featurep 'org-inlinetask)
  5636. (org-inlinetask-at-task-p)
  5637. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5638. (org-inlinetask-toggle-visibility))
  5639. ((org-try-cdlatex-tab))
  5640. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5641. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5642. (save-excursion (beginning-of-line 1)
  5643. (looking-at org-outline-regexp)))
  5644. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5645. (org-cycle-internal-local))
  5646. ;; From there: TAB emulation and template completion.
  5647. (buffer-read-only (org-back-to-heading))
  5648. ((run-hook-with-args-until-success
  5649. 'org-tab-after-check-for-cycling-hook))
  5650. ((org-try-structure-completion))
  5651. ((run-hook-with-args-until-success
  5652. 'org-tab-before-tab-emulation-hook))
  5653. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5654. (or (not (bolp))
  5655. (not (looking-at org-outline-regexp))))
  5656. (call-interactively (global-key-binding "\t")))
  5657. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5658. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5659. (or (and (eq org-cycle-emulate-tab 'white)
  5660. (= (match-end 0) (point-at-eol)))
  5661. (and (eq org-cycle-emulate-tab 'whitestart)
  5662. (>= (match-end 0) pos))))
  5663. t
  5664. (eq org-cycle-emulate-tab t))
  5665. (call-interactively (global-key-binding "\t")))
  5666. (t (save-excursion
  5667. (org-back-to-heading)
  5668. (org-cycle)))))))
  5669. (defun org-cycle-internal-global ()
  5670. "Do the global cycling action."
  5671. ;; Hack to avoid display of messages for .org attachments in Gnus
  5672. (let (message-log-max ; Don't populate the *Messages* buffer
  5673. (ga (string-match "\\*fontification" (buffer-name))))
  5674. (cond
  5675. ((and (eq last-command this-command)
  5676. (eq org-cycle-global-status 'overview))
  5677. ;; We just created the overview - now do table of contents
  5678. ;; This can be slow in very large buffers, so indicate action
  5679. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5680. (unless ga (message "CONTENTS..."))
  5681. (org-content)
  5682. (unless ga (message "CONTENTS...done"))
  5683. (setq org-cycle-global-status 'contents)
  5684. (run-hook-with-args 'org-cycle-hook 'contents))
  5685. ((and (eq last-command this-command)
  5686. (eq org-cycle-global-status 'contents))
  5687. ;; We just showed the table of contents - now show everything
  5688. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5689. (show-all)
  5690. (unless ga (message "SHOW ALL"))
  5691. (setq org-cycle-global-status 'all)
  5692. (run-hook-with-args 'org-cycle-hook 'all))
  5693. (t
  5694. ;; Default action: go to overview
  5695. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5696. (org-overview)
  5697. (unless ga (message "OVERVIEW"))
  5698. (setq org-cycle-global-status 'overview)
  5699. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5700. (defun org-cycle-internal-local ()
  5701. "Do the local cycling action."
  5702. (let (message-log-max ; Don't populate the *Messages* buffer
  5703. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5704. ;; First, determine end of headline (EOH), end of subtree or item
  5705. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5706. (save-excursion
  5707. (if (org-at-item-p)
  5708. (progn
  5709. (beginning-of-line)
  5710. (setq struct (org-list-struct))
  5711. (setq eoh (point-at-eol))
  5712. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5713. (setq has-children (org-list-has-child-p (point) struct)))
  5714. (org-back-to-heading)
  5715. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5716. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5717. (setq has-children
  5718. (or (save-excursion
  5719. (let ((level (funcall outline-level)))
  5720. (outline-next-heading)
  5721. (and (org-at-heading-p t)
  5722. (> (funcall outline-level) level))))
  5723. (save-excursion
  5724. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5725. ;; Determine end invisible part of buffer (EOL)
  5726. (beginning-of-line 2)
  5727. ;; XEmacs doesn't have `next-single-char-property-change'
  5728. (if (featurep 'xemacs)
  5729. (while (and (not (eobp)) ;; this is like `next-line'
  5730. (get-char-property (1- (point)) 'invisible))
  5731. (beginning-of-line 2))
  5732. (while (and (not (eobp)) ;; this is like `next-line'
  5733. (get-char-property (1- (point)) 'invisible))
  5734. (goto-char (next-single-char-property-change (point) 'invisible))
  5735. (and (eolp) (beginning-of-line 2))))
  5736. (setq eol (point)))
  5737. ;; Find out what to do next and set `this-command'
  5738. (cond
  5739. ((= eos eoh)
  5740. ;; Nothing is hidden behind this heading
  5741. (unless (org-before-first-heading-p)
  5742. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5743. (message "EMPTY ENTRY")
  5744. (setq org-cycle-subtree-status nil)
  5745. (save-excursion
  5746. (goto-char eos)
  5747. (outline-next-heading)
  5748. (if (outline-invisible-p) (org-flag-heading nil))))
  5749. ((and (or (>= eol eos)
  5750. (not (string-match "\\S-" (buffer-substring eol eos))))
  5751. (or has-children
  5752. (not (setq children-skipped
  5753. org-cycle-skip-children-state-if-no-children))))
  5754. ;; Entire subtree is hidden in one line: children view
  5755. (unless (org-before-first-heading-p)
  5756. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5757. (if (org-at-item-p)
  5758. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5759. (org-show-entry)
  5760. (org-with-limited-levels (show-children))
  5761. ;; FIXME: This slows down the func way too much.
  5762. ;; How keep drawers hidden in subtree anyway?
  5763. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5764. ;; (org-cycle-hide-drawers 'subtree))
  5765. ;; Fold every list in subtree to top-level items.
  5766. (when (eq org-cycle-include-plain-lists 'integrate)
  5767. (save-excursion
  5768. (org-back-to-heading)
  5769. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5770. (beginning-of-line 1)
  5771. (let* ((struct (org-list-struct))
  5772. (prevs (org-list-prevs-alist struct))
  5773. (end (org-list-get-bottom-point struct)))
  5774. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5775. (org-list-get-all-items (point) struct prevs))
  5776. (goto-char end))))))
  5777. (message "CHILDREN")
  5778. (save-excursion
  5779. (goto-char eos)
  5780. (outline-next-heading)
  5781. (if (outline-invisible-p) (org-flag-heading nil)))
  5782. (setq org-cycle-subtree-status 'children)
  5783. (unless (org-before-first-heading-p)
  5784. (run-hook-with-args 'org-cycle-hook 'children)))
  5785. ((or children-skipped
  5786. (and (eq last-command this-command)
  5787. (eq org-cycle-subtree-status 'children)))
  5788. ;; We just showed the children, or no children are there,
  5789. ;; now show everything.
  5790. (unless (org-before-first-heading-p)
  5791. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5792. (outline-flag-region eoh eos nil)
  5793. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5794. (setq org-cycle-subtree-status 'subtree)
  5795. (unless (org-before-first-heading-p)
  5796. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5797. (t
  5798. ;; Default action: hide the subtree.
  5799. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5800. (outline-flag-region eoh eos t)
  5801. (message "FOLDED")
  5802. (setq org-cycle-subtree-status 'folded)
  5803. (unless (org-before-first-heading-p)
  5804. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5805. ;;;###autoload
  5806. (defun org-global-cycle (&optional arg)
  5807. "Cycle the global visibility. For details see `org-cycle'.
  5808. With \\[universal-argument] prefix arg, switch to startup visibility.
  5809. With a numeric prefix, show all headlines up to that level."
  5810. (interactive "P")
  5811. (let ((org-cycle-include-plain-lists
  5812. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5813. (cond
  5814. ((integerp arg)
  5815. (show-all)
  5816. (hide-sublevels arg)
  5817. (setq org-cycle-global-status 'contents))
  5818. ((equal arg '(4))
  5819. (org-set-startup-visibility)
  5820. (message "Startup visibility, plus VISIBILITY properties."))
  5821. (t
  5822. (org-cycle '(4))))))
  5823. (defun org-set-startup-visibility ()
  5824. "Set the visibility required by startup options and properties."
  5825. (cond
  5826. ((eq org-startup-folded t)
  5827. (org-cycle '(4)))
  5828. ((eq org-startup-folded 'content)
  5829. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5830. (org-cycle '(4)) (org-cycle '(4)))))
  5831. (unless (eq org-startup-folded 'showeverything)
  5832. (if org-hide-block-startup (org-hide-block-all))
  5833. (org-set-visibility-according-to-property 'no-cleanup)
  5834. (org-cycle-hide-archived-subtrees 'all)
  5835. (org-cycle-hide-drawers 'all)
  5836. (org-cycle-show-empty-lines t)))
  5837. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5838. "Switch subtree visibilities according to :VISIBILITY: property."
  5839. (interactive)
  5840. (let (org-show-entry-below state)
  5841. (save-excursion
  5842. (goto-char (point-min))
  5843. (while (re-search-forward
  5844. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5845. nil t)
  5846. (setq state (match-string 1))
  5847. (save-excursion
  5848. (org-back-to-heading t)
  5849. (hide-subtree)
  5850. (org-reveal)
  5851. (cond
  5852. ((equal state '("fold" "folded"))
  5853. (hide-subtree))
  5854. ((equal state "children")
  5855. (org-show-hidden-entry)
  5856. (show-children))
  5857. ((equal state "content")
  5858. (save-excursion
  5859. (save-restriction
  5860. (org-narrow-to-subtree)
  5861. (org-content))))
  5862. ((member state '("all" "showall"))
  5863. (show-subtree)))))
  5864. (unless no-cleanup
  5865. (org-cycle-hide-archived-subtrees 'all)
  5866. (org-cycle-hide-drawers 'all)
  5867. (org-cycle-show-empty-lines 'all)))))
  5868. ;; This function uses outline-regexp instead of the more fundamental
  5869. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5870. ;; buffers, where outline-regexp is needed.
  5871. (defun org-overview ()
  5872. "Switch to overview mode, showing only top-level headlines.
  5873. Really, this shows all headlines with level equal or greater than the level
  5874. of the first headline in the buffer. This is important, because if the
  5875. first headline is not level one, then (hide-sublevels 1) gives confusing
  5876. results."
  5877. (interactive)
  5878. (let ((level (save-excursion
  5879. (goto-char (point-min))
  5880. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5881. (progn
  5882. (goto-char (match-beginning 0))
  5883. (funcall outline-level))))))
  5884. (and level (hide-sublevels level))))
  5885. (defun org-content (&optional arg)
  5886. "Show all headlines in the buffer, like a table of contents.
  5887. With numerical argument N, show content up to level N."
  5888. (interactive "P")
  5889. (save-excursion
  5890. ;; Visit all headings and show their offspring
  5891. (and (integerp arg) (org-overview))
  5892. (goto-char (point-max))
  5893. (catch 'exit
  5894. (while (and (progn (condition-case nil
  5895. (outline-previous-visible-heading 1)
  5896. (error (goto-char (point-min))))
  5897. t)
  5898. (looking-at org-outline-regexp))
  5899. (if (integerp arg)
  5900. (show-children (1- arg))
  5901. (show-branches))
  5902. (if (bobp) (throw 'exit nil))))))
  5903. (defun org-optimize-window-after-visibility-change (state)
  5904. "Adjust the window after a change in outline visibility.
  5905. This function is the default value of the hook `org-cycle-hook'."
  5906. (when (get-buffer-window (current-buffer))
  5907. (cond
  5908. ((eq state 'content) nil)
  5909. ((eq state 'all) nil)
  5910. ((eq state 'folded) nil)
  5911. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5912. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5913. (defun org-remove-empty-overlays-at (pos)
  5914. "Remove outline overlays that do not contain non-white stuff."
  5915. (mapc
  5916. (lambda (o)
  5917. (and (eq 'outline (overlay-get o 'invisible))
  5918. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5919. (overlay-end o))))
  5920. (delete-overlay o)))
  5921. (overlays-at pos)))
  5922. (defun org-clean-visibility-after-subtree-move ()
  5923. "Fix visibility issues after moving a subtree."
  5924. ;; First, find a reasonable region to look at:
  5925. ;; Start two siblings above, end three below
  5926. (let* ((beg (save-excursion
  5927. (and (org-get-last-sibling)
  5928. (org-get-last-sibling))
  5929. (point)))
  5930. (end (save-excursion
  5931. (and (org-get-next-sibling)
  5932. (org-get-next-sibling)
  5933. (org-get-next-sibling))
  5934. (if (org-at-heading-p)
  5935. (point-at-eol)
  5936. (point))))
  5937. (level (looking-at "\\*+"))
  5938. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5939. (save-excursion
  5940. (save-restriction
  5941. (narrow-to-region beg end)
  5942. (when re
  5943. ;; Properly fold already folded siblings
  5944. (goto-char (point-min))
  5945. (while (re-search-forward re nil t)
  5946. (if (and (not (outline-invisible-p))
  5947. (save-excursion
  5948. (goto-char (point-at-eol)) (outline-invisible-p)))
  5949. (hide-entry))))
  5950. (org-cycle-show-empty-lines 'overview)
  5951. (org-cycle-hide-drawers 'overview)))))
  5952. (defun org-cycle-show-empty-lines (state)
  5953. "Show empty lines above all visible headlines.
  5954. The region to be covered depends on STATE when called through
  5955. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5956. entire buffer covered. Note that an empty line is only shown if there
  5957. are at least `org-cycle-separator-lines' empty lines before the headline."
  5958. (when (not (= org-cycle-separator-lines 0))
  5959. (save-excursion
  5960. (let* ((n (abs org-cycle-separator-lines))
  5961. (re (cond
  5962. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5963. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5964. (t (let ((ns (number-to-string (- n 2))))
  5965. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5966. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5967. beg end b e)
  5968. (cond
  5969. ((memq state '(overview contents t))
  5970. (setq beg (point-min) end (point-max)))
  5971. ((memq state '(children folded))
  5972. (setq beg (point) end (progn (org-end-of-subtree t t)
  5973. (beginning-of-line 2)
  5974. (point)))))
  5975. (when beg
  5976. (goto-char beg)
  5977. (while (re-search-forward re end t)
  5978. (unless (get-char-property (match-end 1) 'invisible)
  5979. (setq e (match-end 1))
  5980. (if (< org-cycle-separator-lines 0)
  5981. (setq b (save-excursion
  5982. (goto-char (match-beginning 0))
  5983. (org-back-over-empty-lines)
  5984. (if (save-excursion
  5985. (goto-char (max (point-min) (1- (point))))
  5986. (org-at-heading-p))
  5987. (1- (point))
  5988. (point))))
  5989. (setq b (match-beginning 1)))
  5990. (outline-flag-region b e nil)))))))
  5991. ;; Never hide empty lines at the end of the file.
  5992. (save-excursion
  5993. (goto-char (point-max))
  5994. (outline-previous-heading)
  5995. (outline-end-of-heading)
  5996. (if (and (looking-at "[ \t\n]+")
  5997. (= (match-end 0) (point-max)))
  5998. (outline-flag-region (point) (match-end 0) nil))))
  5999. (defun org-show-empty-lines-in-parent ()
  6000. "Move to the parent and re-show empty lines before visible headlines."
  6001. (save-excursion
  6002. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6003. (org-cycle-show-empty-lines context))))
  6004. (defun org-files-list ()
  6005. "Return `org-agenda-files' list, plus all open org-mode files.
  6006. This is useful for operations that need to scan all of a user's
  6007. open and agenda-wise Org files."
  6008. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6009. (dolist (buf (buffer-list))
  6010. (with-current-buffer buf
  6011. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6012. (let ((file (expand-file-name (buffer-file-name))))
  6013. (unless (member file files)
  6014. (push file files))))))
  6015. files))
  6016. (defsubst org-entry-beginning-position ()
  6017. "Return the beginning position of the current entry."
  6018. (save-excursion (outline-back-to-heading t) (point)))
  6019. (defsubst org-entry-end-position ()
  6020. "Return the end position of the current entry."
  6021. (save-excursion (outline-next-heading) (point)))
  6022. (defun org-cycle-hide-drawers (state)
  6023. "Re-hide all drawers after a visibility state change."
  6024. (when (and (derived-mode-p 'org-mode)
  6025. (not (memq state '(overview folded contents))))
  6026. (save-excursion
  6027. (let* ((globalp (memq state '(contents all)))
  6028. (beg (if globalp (point-min) (point)))
  6029. (end (if globalp (point-max)
  6030. (if (eq state 'children)
  6031. (save-excursion (outline-next-heading) (point))
  6032. (org-end-of-subtree t)))))
  6033. (goto-char beg)
  6034. (while (re-search-forward org-drawer-regexp end t)
  6035. (org-flag-drawer t))))))
  6036. (defun org-cycle-hide-inline-tasks (state)
  6037. "Re-hide inline task when switching to 'contents visibility state."
  6038. (when (and (eq state 'contents)
  6039. (boundp 'org-inlinetask-min-level)
  6040. org-inlinetask-min-level)
  6041. (hide-sublevels (1- org-inlinetask-min-level))))
  6042. (defun org-flag-drawer (flag)
  6043. "When FLAG is non-nil, hide the drawer we are within.
  6044. Otherwise make it visible."
  6045. (save-excursion
  6046. (beginning-of-line 1)
  6047. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6048. (let ((b (match-end 0)))
  6049. (if (re-search-forward
  6050. "^[ \t]*:END:"
  6051. (save-excursion (outline-next-heading) (point)) t)
  6052. (outline-flag-region b (point-at-eol) flag)
  6053. (error ":END: line missing at position %s" b))))))
  6054. (defun org-subtree-end-visible-p ()
  6055. "Is the end of the current subtree visible?"
  6056. (pos-visible-in-window-p
  6057. (save-excursion (org-end-of-subtree t) (point))))
  6058. (defun org-first-headline-recenter (&optional N)
  6059. "Move cursor to the first headline and recenter the headline.
  6060. Optional argument N means put the headline into the Nth line of the window."
  6061. (goto-char (point-min))
  6062. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6063. (beginning-of-line)
  6064. (recenter (prefix-numeric-value N))))
  6065. ;;; Saving and restoring visibility
  6066. (defun org-outline-overlay-data (&optional use-markers)
  6067. "Return a list of the locations of all outline overlays.
  6068. These are overlays with the `invisible' property value `outline'.
  6069. The return value is a list of cons cells, with start and stop
  6070. positions for each overlay.
  6071. If USE-MARKERS is set, return the positions as markers."
  6072. (let (beg end)
  6073. (save-excursion
  6074. (save-restriction
  6075. (widen)
  6076. (delq nil
  6077. (mapcar (lambda (o)
  6078. (when (eq (overlay-get o 'invisible) 'outline)
  6079. (setq beg (overlay-start o)
  6080. end (overlay-end o))
  6081. (and beg end (> end beg)
  6082. (if use-markers
  6083. (cons (move-marker (make-marker) beg)
  6084. (move-marker (make-marker) end))
  6085. (cons beg end)))))
  6086. (overlays-in (point-min) (point-max))))))))
  6087. (defun org-set-outline-overlay-data (data)
  6088. "Create visibility overlays for all positions in DATA.
  6089. DATA should have been made by `org-outline-overlay-data'."
  6090. (let (o)
  6091. (save-excursion
  6092. (save-restriction
  6093. (widen)
  6094. (show-all)
  6095. (mapc (lambda (c)
  6096. (outline-flag-region (car c) (cdr c) t))
  6097. data)))))
  6098. ;;; Folding of blocks
  6099. (defvar org-hide-block-overlays nil
  6100. "Overlays hiding blocks.")
  6101. (make-variable-buffer-local 'org-hide-block-overlays)
  6102. (defun org-block-map (function &optional start end)
  6103. "Call FUNCTION at the head of all source blocks in the current buffer.
  6104. Optional arguments START and END can be used to limit the range."
  6105. (let ((start (or start (point-min)))
  6106. (end (or end (point-max))))
  6107. (save-excursion
  6108. (goto-char start)
  6109. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6110. (save-excursion
  6111. (save-match-data
  6112. (goto-char (match-beginning 0))
  6113. (funcall function)))))))
  6114. (defun org-hide-block-toggle-all ()
  6115. "Toggle the visibility of all blocks in the current buffer."
  6116. (org-block-map #'org-hide-block-toggle))
  6117. (defun org-hide-block-all ()
  6118. "Fold all blocks in the current buffer."
  6119. (interactive)
  6120. (org-show-block-all)
  6121. (org-block-map #'org-hide-block-toggle-maybe))
  6122. (defun org-show-block-all ()
  6123. "Unfold all blocks in the current buffer."
  6124. (interactive)
  6125. (mapc 'delete-overlay org-hide-block-overlays)
  6126. (setq org-hide-block-overlays nil))
  6127. (defun org-hide-block-toggle-maybe ()
  6128. "Toggle visibility of block at point."
  6129. (interactive)
  6130. (let ((case-fold-search t))
  6131. (if (save-excursion
  6132. (beginning-of-line 1)
  6133. (looking-at org-block-regexp))
  6134. (progn (org-hide-block-toggle)
  6135. t) ;; to signal that we took action
  6136. nil))) ;; to signal that we did not
  6137. (defun org-hide-block-toggle (&optional force)
  6138. "Toggle the visibility of the current block."
  6139. (interactive)
  6140. (save-excursion
  6141. (beginning-of-line)
  6142. (if (re-search-forward org-block-regexp nil t)
  6143. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6144. (end (match-end 0)) ;; end of entire body
  6145. ov)
  6146. (if (memq t (mapcar (lambda (overlay)
  6147. (eq (overlay-get overlay 'invisible)
  6148. 'org-hide-block))
  6149. (overlays-at start)))
  6150. (if (or (not force) (eq force 'off))
  6151. (mapc (lambda (ov)
  6152. (when (member ov org-hide-block-overlays)
  6153. (setq org-hide-block-overlays
  6154. (delq ov org-hide-block-overlays)))
  6155. (when (eq (overlay-get ov 'invisible)
  6156. 'org-hide-block)
  6157. (delete-overlay ov)))
  6158. (overlays-at start)))
  6159. (setq ov (make-overlay start end))
  6160. (overlay-put ov 'invisible 'org-hide-block)
  6161. ;; make the block accessible to isearch
  6162. (overlay-put
  6163. ov 'isearch-open-invisible
  6164. (lambda (ov)
  6165. (when (member ov org-hide-block-overlays)
  6166. (setq org-hide-block-overlays
  6167. (delq ov org-hide-block-overlays)))
  6168. (when (eq (overlay-get ov 'invisible)
  6169. 'org-hide-block)
  6170. (delete-overlay ov))))
  6171. (push ov org-hide-block-overlays)))
  6172. (error "Not looking at a source block"))))
  6173. ;; org-tab-after-check-for-cycling-hook
  6174. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6175. ;; Remove overlays when changing major mode
  6176. (add-hook 'org-mode-hook
  6177. (lambda () (org-add-hook 'change-major-mode-hook
  6178. 'org-show-block-all 'append 'local)))
  6179. ;;; Org-goto
  6180. (defvar org-goto-window-configuration nil)
  6181. (defvar org-goto-marker nil)
  6182. (defvar org-goto-map)
  6183. (defun org-goto-map ()
  6184. "Set the keymap `org-goto'."
  6185. (setq org-goto-map
  6186. (let ((map (make-sparse-keymap)))
  6187. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6188. mouse-drag-region universal-argument org-occur))
  6189. cmd)
  6190. (while (setq cmd (pop cmds))
  6191. (substitute-key-definition cmd cmd map global-map)))
  6192. (suppress-keymap map)
  6193. (org-defkey map "\C-m" 'org-goto-ret)
  6194. (org-defkey map [(return)] 'org-goto-ret)
  6195. (org-defkey map [(left)] 'org-goto-left)
  6196. (org-defkey map [(right)] 'org-goto-right)
  6197. (org-defkey map [(control ?g)] 'org-goto-quit)
  6198. (org-defkey map "\C-i" 'org-cycle)
  6199. (org-defkey map [(tab)] 'org-cycle)
  6200. (org-defkey map [(down)] 'outline-next-visible-heading)
  6201. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6202. (if org-goto-auto-isearch
  6203. (if (fboundp 'define-key-after)
  6204. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6205. nil)
  6206. (org-defkey map "q" 'org-goto-quit)
  6207. (org-defkey map "n" 'outline-next-visible-heading)
  6208. (org-defkey map "p" 'outline-previous-visible-heading)
  6209. (org-defkey map "f" 'outline-forward-same-level)
  6210. (org-defkey map "b" 'outline-backward-same-level)
  6211. (org-defkey map "u" 'outline-up-heading))
  6212. (org-defkey map "/" 'org-occur)
  6213. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6214. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6215. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6216. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6217. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6218. map)))
  6219. (defconst org-goto-help
  6220. "Browse buffer copy, to find location or copy text.%s
  6221. RET=jump to location C-g=quit and return to previous location
  6222. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6223. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6224. ;; FIXME: Docstring does not mention both interfaces
  6225. (defun org-goto (&optional alternative-interface)
  6226. "Look up a different location in the current file, keeping current visibility.
  6227. When you want look-up or go to a different location in a
  6228. document, the fastest way is often to fold the entire buffer and
  6229. then dive into the tree. This method has the disadvantage, that
  6230. the previous location will be folded, which may not be what you
  6231. want.
  6232. This command works around this by showing a copy of the current
  6233. buffer in an indirect buffer, in overview mode. You can dive
  6234. into the tree in that copy, use org-occur and incremental search
  6235. to find a location. When pressing RET or `Q', the command
  6236. returns to the original buffer in which the visibility is still
  6237. unchanged. After RET it will also jump to the location selected
  6238. in the indirect buffer and expose the headline hierarchy above.
  6239. With a prefix argument, use the alternative interface: e.g. if
  6240. `org-goto-interface' is 'outline use 'outline-path-completion."
  6241. (interactive "P")
  6242. (org-goto-map)
  6243. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6244. (org-refile-use-outline-path t)
  6245. (org-refile-target-verify-function nil)
  6246. (interface
  6247. (if (not alternative-interface)
  6248. org-goto-interface
  6249. (if (eq org-goto-interface 'outline)
  6250. 'outline-path-completion
  6251. 'outline)))
  6252. (org-goto-start-pos (point))
  6253. (selected-point
  6254. (if (eq interface 'outline)
  6255. (car (org-get-location (current-buffer) org-goto-help))
  6256. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6257. (org-refile-check-position pa)
  6258. (nth 3 pa)))))
  6259. (if selected-point
  6260. (progn
  6261. (org-mark-ring-push org-goto-start-pos)
  6262. (goto-char selected-point)
  6263. (if (or (outline-invisible-p) (org-invisible-p2))
  6264. (org-show-context 'org-goto)))
  6265. (message "Quit"))))
  6266. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6267. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6268. (defvar org-goto-local-auto-isearch-map) ; defined below
  6269. (defun org-get-location (buf help)
  6270. "Let the user select a location in the Org-mode buffer BUF.
  6271. This function uses a recursive edit. It returns the selected position
  6272. or nil."
  6273. (org-no-popups
  6274. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6275. (isearch-hide-immediately nil)
  6276. (isearch-search-fun-function
  6277. (lambda () 'org-goto-local-search-headings))
  6278. (org-goto-selected-point org-goto-exit-command))
  6279. (save-excursion
  6280. (save-window-excursion
  6281. (delete-other-windows)
  6282. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6283. (org-pop-to-buffer-same-window
  6284. (condition-case nil
  6285. (make-indirect-buffer (current-buffer) "*org-goto*")
  6286. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6287. (with-output-to-temp-buffer "*Org Help*"
  6288. (princ (format help (if org-goto-auto-isearch
  6289. " Just type for auto-isearch."
  6290. " n/p/f/b/u to navigate, q to quit."))))
  6291. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6292. (setq buffer-read-only nil)
  6293. (let ((org-startup-truncated t)
  6294. (org-startup-folded nil)
  6295. (org-startup-align-all-tables nil))
  6296. (org-mode)
  6297. (org-overview))
  6298. (setq buffer-read-only t)
  6299. (if (and (boundp 'org-goto-start-pos)
  6300. (integer-or-marker-p org-goto-start-pos))
  6301. (let ((org-show-hierarchy-above t)
  6302. (org-show-siblings t)
  6303. (org-show-following-heading t))
  6304. (goto-char org-goto-start-pos)
  6305. (and (outline-invisible-p) (org-show-context)))
  6306. (goto-char (point-min)))
  6307. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6308. (message "Select location and press RET")
  6309. (use-local-map org-goto-map)
  6310. (recursive-edit)))
  6311. (kill-buffer "*org-goto*")
  6312. (cons org-goto-selected-point org-goto-exit-command))))
  6313. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6314. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6315. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6316. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6317. (defun org-goto-local-search-headings (string bound noerror)
  6318. "Search and make sure that any matches are in headlines."
  6319. (catch 'return
  6320. (while (if isearch-forward
  6321. (search-forward string bound noerror)
  6322. (search-backward string bound noerror))
  6323. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6324. (and (member :headline context)
  6325. (not (member :tags context))))
  6326. (throw 'return (point))))))
  6327. (defun org-goto-local-auto-isearch ()
  6328. "Start isearch."
  6329. (interactive)
  6330. (goto-char (point-min))
  6331. (let ((keys (this-command-keys)))
  6332. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6333. (isearch-mode t)
  6334. (isearch-process-search-char (string-to-char keys)))))
  6335. (defun org-goto-ret (&optional arg)
  6336. "Finish `org-goto' by going to the new location."
  6337. (interactive "P")
  6338. (setq org-goto-selected-point (point)
  6339. org-goto-exit-command 'return)
  6340. (throw 'exit nil))
  6341. (defun org-goto-left ()
  6342. "Finish `org-goto' by going to the new location."
  6343. (interactive)
  6344. (if (org-at-heading-p)
  6345. (progn
  6346. (beginning-of-line 1)
  6347. (setq org-goto-selected-point (point)
  6348. org-goto-exit-command 'left)
  6349. (throw 'exit nil))
  6350. (error "Not on a heading")))
  6351. (defun org-goto-right ()
  6352. "Finish `org-goto' by going to the new location."
  6353. (interactive)
  6354. (if (org-at-heading-p)
  6355. (progn
  6356. (setq org-goto-selected-point (point)
  6357. org-goto-exit-command 'right)
  6358. (throw 'exit nil))
  6359. (error "Not on a heading")))
  6360. (defun org-goto-quit ()
  6361. "Finish `org-goto' without cursor motion."
  6362. (interactive)
  6363. (setq org-goto-selected-point nil)
  6364. (setq org-goto-exit-command 'quit)
  6365. (throw 'exit nil))
  6366. ;;; Indirect buffer display of subtrees
  6367. (defvar org-indirect-dedicated-frame nil
  6368. "This is the frame being used for indirect tree display.")
  6369. (defvar org-last-indirect-buffer nil)
  6370. (defun org-tree-to-indirect-buffer (&optional arg)
  6371. "Create indirect buffer and narrow it to current subtree.
  6372. With a numerical prefix ARG, go up to this level and then take that tree.
  6373. If ARG is negative, go up that many levels.
  6374. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6375. indirect buffer previously made with this command, to avoid proliferation of
  6376. indirect buffers. However, when you call the command with a \
  6377. \\[universal-argument] prefix, or
  6378. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6379. is kept so that you can work with several indirect buffers at the same time.
  6380. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6381. \\[universal-argument] prefix also
  6382. requests that a new frame be made for the new buffer, so that the dedicated
  6383. frame is not changed."
  6384. (interactive "P")
  6385. (let ((cbuf (current-buffer))
  6386. (cwin (selected-window))
  6387. (pos (point))
  6388. beg end level heading ibuf)
  6389. (save-excursion
  6390. (org-back-to-heading t)
  6391. (when (numberp arg)
  6392. (setq level (org-outline-level))
  6393. (if (< arg 0) (setq arg (+ level arg)))
  6394. (while (> (setq level (org-outline-level)) arg)
  6395. (org-up-heading-safe)))
  6396. (setq beg (point)
  6397. heading (org-get-heading))
  6398. (org-end-of-subtree t t)
  6399. (if (org-at-heading-p) (backward-char 1))
  6400. (setq end (point)))
  6401. (if (and (buffer-live-p org-last-indirect-buffer)
  6402. (not (eq org-indirect-buffer-display 'new-frame))
  6403. (not arg))
  6404. (kill-buffer org-last-indirect-buffer))
  6405. (setq ibuf (org-get-indirect-buffer cbuf)
  6406. org-last-indirect-buffer ibuf)
  6407. (cond
  6408. ((or (eq org-indirect-buffer-display 'new-frame)
  6409. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6410. (select-frame (make-frame))
  6411. (delete-other-windows)
  6412. (org-pop-to-buffer-same-window ibuf)
  6413. (org-set-frame-title heading))
  6414. ((eq org-indirect-buffer-display 'dedicated-frame)
  6415. (raise-frame
  6416. (select-frame (or (and org-indirect-dedicated-frame
  6417. (frame-live-p org-indirect-dedicated-frame)
  6418. org-indirect-dedicated-frame)
  6419. (setq org-indirect-dedicated-frame (make-frame)))))
  6420. (delete-other-windows)
  6421. (org-pop-to-buffer-same-window ibuf)
  6422. (org-set-frame-title (concat "Indirect: " heading)))
  6423. ((eq org-indirect-buffer-display 'current-window)
  6424. (org-pop-to-buffer-same-window ibuf))
  6425. ((eq org-indirect-buffer-display 'other-window)
  6426. (pop-to-buffer ibuf))
  6427. (t (error "Invalid value")))
  6428. (if (featurep 'xemacs)
  6429. (save-excursion (org-mode) (turn-on-font-lock)))
  6430. (narrow-to-region beg end)
  6431. (show-all)
  6432. (goto-char pos)
  6433. (run-hook-with-args 'org-cycle-hook 'all)
  6434. (and (window-live-p cwin) (select-window cwin))))
  6435. (defun org-get-indirect-buffer (&optional buffer)
  6436. (setq buffer (or buffer (current-buffer)))
  6437. (let ((n 1) (base (buffer-name buffer)) bname)
  6438. (while (buffer-live-p
  6439. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6440. (setq n (1+ n)))
  6441. (condition-case nil
  6442. (make-indirect-buffer buffer bname 'clone)
  6443. (error (make-indirect-buffer buffer bname)))))
  6444. (defun org-set-frame-title (title)
  6445. "Set the title of the current frame to the string TITLE."
  6446. ;; FIXME: how to name a single frame in XEmacs???
  6447. (unless (featurep 'xemacs)
  6448. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6449. ;;;; Structure editing
  6450. ;;; Inserting headlines
  6451. (defun org-previous-line-empty-p ()
  6452. (save-excursion
  6453. (and (not (bobp))
  6454. (or (beginning-of-line 0) t)
  6455. (save-match-data
  6456. (looking-at "[ \t]*$")))))
  6457. (defun org-insert-heading (&optional force-heading invisible-ok)
  6458. "Insert a new heading or item with same depth at point.
  6459. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6460. If point is at the beginning of a headline, insert a sibling before the
  6461. current headline. If point is not at the beginning, split the line,
  6462. create the new headline with the text in the current line after point
  6463. \(but see also the variable `org-M-RET-may-split-line').
  6464. With a double prefix arg, force the heading to be inserted at the
  6465. end of the parent subtree.
  6466. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6467. This is important for non-interactive uses of the command."
  6468. (interactive "P")
  6469. (if (or (= (buffer-size) 0)
  6470. (and (not (save-excursion
  6471. (and (ignore-errors (org-back-to-heading invisible-ok))
  6472. (org-at-heading-p))))
  6473. (or force-heading (not (org-in-item-p)))))
  6474. (progn
  6475. (insert "\n* ")
  6476. (run-hooks 'org-insert-heading-hook))
  6477. (when (or force-heading (not (org-insert-item)))
  6478. (let* ((empty-line-p nil)
  6479. (level nil)
  6480. (on-heading (org-at-heading-p))
  6481. (head (save-excursion
  6482. (condition-case nil
  6483. (progn
  6484. (org-back-to-heading invisible-ok)
  6485. (when (and (not on-heading)
  6486. (featurep 'org-inlinetask)
  6487. (integerp org-inlinetask-min-level)
  6488. (>= (length (match-string 0))
  6489. org-inlinetask-min-level))
  6490. ;; Find a heading level before the inline task
  6491. (while (and (setq level (org-up-heading-safe))
  6492. (>= level org-inlinetask-min-level)))
  6493. (if (org-at-heading-p)
  6494. (org-back-to-heading invisible-ok)
  6495. (error "This should not happen")))
  6496. (setq empty-line-p (org-previous-line-empty-p))
  6497. (match-string 0))
  6498. (error "*"))))
  6499. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6500. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6501. pos hide-previous previous-pos)
  6502. (cond
  6503. ((and (org-at-heading-p) (bolp)
  6504. (or (bobp)
  6505. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6506. ;; insert before the current line
  6507. (open-line (if blank 2 1)))
  6508. ((and (bolp)
  6509. (not org-insert-heading-respect-content)
  6510. (or (bobp)
  6511. (save-excursion
  6512. (backward-char 1) (not (outline-invisible-p)))))
  6513. ;; insert right here
  6514. nil)
  6515. (t
  6516. ;; somewhere in the line
  6517. (save-excursion
  6518. (setq previous-pos (point-at-bol))
  6519. (end-of-line)
  6520. (setq hide-previous (outline-invisible-p)))
  6521. (and org-insert-heading-respect-content (org-show-subtree))
  6522. (let ((split
  6523. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6524. (save-excursion
  6525. (let ((p (point)))
  6526. (goto-char (point-at-bol))
  6527. (and (looking-at org-complex-heading-regexp)
  6528. (match-beginning 4)
  6529. (> p (match-beginning 4)))))))
  6530. tags pos)
  6531. (cond
  6532. (org-insert-heading-respect-content
  6533. (if (not (equal force-heading '(16)))
  6534. (org-end-of-subtree nil t)
  6535. (org-up-heading-safe)
  6536. (org-end-of-subtree nil t))
  6537. (when (featurep 'org-inlinetask)
  6538. (while (and (not (eobp))
  6539. (looking-at "\\(\\*+\\)[ \t]+")
  6540. (>= (length (match-string 1))
  6541. org-inlinetask-min-level))
  6542. (org-end-of-subtree nil t)))
  6543. (or (bolp) (newline))
  6544. (or (org-previous-line-empty-p)
  6545. (and blank (newline)))
  6546. (open-line 1))
  6547. ((org-at-heading-p)
  6548. (when hide-previous
  6549. (show-children)
  6550. (org-show-entry))
  6551. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6552. (setq tags (and (match-end 2) (match-string 2)))
  6553. (and (match-end 1)
  6554. (delete-region (match-beginning 1) (match-end 1)))
  6555. (setq pos (point-at-bol))
  6556. (or split (end-of-line 1))
  6557. (delete-horizontal-space)
  6558. (if (string-match "\\`\\*+\\'"
  6559. (buffer-substring (point-at-bol) (point)))
  6560. (insert " "))
  6561. (newline (if blank 2 1))
  6562. (when tags
  6563. (save-excursion
  6564. (goto-char pos)
  6565. (end-of-line 1)
  6566. (insert " " tags)
  6567. (org-set-tags nil 'align))))
  6568. (t
  6569. (or split (end-of-line 1))
  6570. (newline (if blank 2 1)))))))
  6571. (insert head) (just-one-space)
  6572. (setq pos (point))
  6573. (end-of-line 1)
  6574. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6575. (when (and org-insert-heading-respect-content hide-previous)
  6576. (save-excursion
  6577. (goto-char previous-pos)
  6578. (hide-subtree)))
  6579. (run-hooks 'org-insert-heading-hook)))))
  6580. (defun org-get-heading (&optional no-tags no-todo)
  6581. "Return the heading of the current entry, without the stars.
  6582. When NO-TAGS is non-nil, don't include tags.
  6583. When NO-TODO is non-nil, don't include TODO keywords."
  6584. (save-excursion
  6585. (org-back-to-heading t)
  6586. (cond
  6587. ((and no-tags no-todo)
  6588. (looking-at org-complex-heading-regexp)
  6589. (match-string 4))
  6590. (no-tags
  6591. (looking-at (concat org-outline-regexp
  6592. "\\(.*?\\)"
  6593. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6594. (match-string 1))
  6595. (no-todo
  6596. (looking-at org-todo-line-regexp)
  6597. (match-string 3))
  6598. (t (looking-at org-heading-regexp)
  6599. (match-string 2)))))
  6600. (defun org-heading-components ()
  6601. "Return the components of the current heading.
  6602. This is a list with the following elements:
  6603. - the level as an integer
  6604. - the reduced level, different if `org-odd-levels-only' is set.
  6605. - the TODO keyword, or nil
  6606. - the priority character, like ?A, or nil if no priority is given
  6607. - the headline text itself, or the tags string if no headline text
  6608. - the tags string, or nil."
  6609. (save-excursion
  6610. (org-back-to-heading t)
  6611. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6612. (list (length (match-string 1))
  6613. (org-reduced-level (length (match-string 1)))
  6614. (org-match-string-no-properties 2)
  6615. (and (match-end 3) (aref (match-string 3) 2))
  6616. (org-match-string-no-properties 4)
  6617. (org-match-string-no-properties 5)))))
  6618. (defun org-get-entry ()
  6619. "Get the entry text, after heading, entire subtree."
  6620. (save-excursion
  6621. (org-back-to-heading t)
  6622. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6623. (defun org-insert-heading-after-current ()
  6624. "Insert a new heading with same level as current, after current subtree."
  6625. (interactive)
  6626. (org-back-to-heading)
  6627. (org-insert-heading)
  6628. (org-move-subtree-down)
  6629. (end-of-line 1))
  6630. (defun org-insert-heading-respect-content ()
  6631. (interactive)
  6632. (let ((org-insert-heading-respect-content t))
  6633. (org-insert-heading t)))
  6634. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6635. (interactive "P")
  6636. (let ((org-insert-heading-respect-content t))
  6637. (org-insert-todo-heading force-state t)))
  6638. (defun org-insert-todo-heading (arg &optional force-heading)
  6639. "Insert a new heading with the same level and TODO state as current heading.
  6640. If the heading has no TODO state, or if the state is DONE, use the first
  6641. state (TODO by default). Also one prefix arg, force first state. With two
  6642. prefix args, force inserting at the end of the parent subtree."
  6643. (interactive "P")
  6644. (when (or force-heading (not (org-insert-item 'checkbox)))
  6645. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6646. force-heading))
  6647. (save-excursion
  6648. (org-back-to-heading)
  6649. (outline-previous-heading)
  6650. (looking-at org-todo-line-regexp))
  6651. (let*
  6652. ((new-mark-x
  6653. (if (or arg
  6654. (not (match-beginning 2))
  6655. (member (match-string 2) org-done-keywords))
  6656. (car org-todo-keywords-1)
  6657. (match-string 2)))
  6658. (new-mark
  6659. (or
  6660. (run-hook-with-args-until-success
  6661. 'org-todo-get-default-hook new-mark-x nil)
  6662. new-mark-x)))
  6663. (beginning-of-line 1)
  6664. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6665. (if org-treat-insert-todo-heading-as-state-change
  6666. (org-todo new-mark)
  6667. (insert new-mark " "))))
  6668. (when org-provide-todo-statistics
  6669. (org-update-parent-todo-statistics))))
  6670. (defun org-insert-subheading (arg)
  6671. "Insert a new subheading and demote it.
  6672. Works for outline headings and for plain lists alike."
  6673. (interactive "P")
  6674. (org-insert-heading arg)
  6675. (cond
  6676. ((org-at-heading-p) (org-do-demote))
  6677. ((org-at-item-p) (org-indent-item))))
  6678. (defun org-insert-todo-subheading (arg)
  6679. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6680. Works for outline headings and for plain lists alike."
  6681. (interactive "P")
  6682. (org-insert-todo-heading arg)
  6683. (cond
  6684. ((org-at-heading-p) (org-do-demote))
  6685. ((org-at-item-p) (org-indent-item))))
  6686. ;;; Promotion and Demotion
  6687. (defvar org-after-demote-entry-hook nil
  6688. "Hook run after an entry has been demoted.
  6689. The cursor will be at the beginning of the entry.
  6690. When a subtree is being demoted, the hook will be called for each node.")
  6691. (defvar org-after-promote-entry-hook nil
  6692. "Hook run after an entry has been promoted.
  6693. The cursor will be at the beginning of the entry.
  6694. When a subtree is being promoted, the hook will be called for each node.")
  6695. (defun org-promote-subtree ()
  6696. "Promote the entire subtree.
  6697. See also `org-promote'."
  6698. (interactive)
  6699. (save-excursion
  6700. (org-with-limited-levels (org-map-tree 'org-promote)))
  6701. (org-fix-position-after-promote))
  6702. (defun org-demote-subtree ()
  6703. "Demote the entire subtree. See `org-demote'.
  6704. See also `org-promote'."
  6705. (interactive)
  6706. (save-excursion
  6707. (org-with-limited-levels (org-map-tree 'org-demote)))
  6708. (org-fix-position-after-promote))
  6709. (defun org-do-promote ()
  6710. "Promote the current heading higher up the tree.
  6711. If the region is active in `transient-mark-mode', promote all headings
  6712. in the region."
  6713. (interactive)
  6714. (save-excursion
  6715. (if (org-region-active-p)
  6716. (org-map-region 'org-promote (region-beginning) (region-end))
  6717. (org-promote)))
  6718. (org-fix-position-after-promote))
  6719. (defun org-do-demote ()
  6720. "Demote the current heading lower down the tree.
  6721. If the region is active in `transient-mark-mode', demote all headings
  6722. in the region."
  6723. (interactive)
  6724. (save-excursion
  6725. (if (org-region-active-p)
  6726. (org-map-region 'org-demote (region-beginning) (region-end))
  6727. (org-demote)))
  6728. (org-fix-position-after-promote))
  6729. (defun org-fix-position-after-promote ()
  6730. "Make sure that after pro/demotion cursor position is right."
  6731. (let ((pos (point)))
  6732. (when (save-excursion
  6733. (beginning-of-line 1)
  6734. (looking-at org-todo-line-regexp)
  6735. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6736. (cond ((eobp) (insert " "))
  6737. ((eolp) (insert " "))
  6738. ((equal (char-after) ?\ ) (forward-char 1))))))
  6739. (defun org-current-level ()
  6740. "Return the level of the current entry, or nil if before the first headline.
  6741. The level is the number of stars at the beginning of the headline."
  6742. (save-excursion
  6743. (org-with-limited-levels
  6744. (if (ignore-errors (org-back-to-heading t))
  6745. (funcall outline-level)))))
  6746. (defun org-get-previous-line-level ()
  6747. "Return the outline depth of the last headline before the current line.
  6748. Returns 0 for the first headline in the buffer, and nil if before the
  6749. first headline."
  6750. (let ((current-level (org-current-level))
  6751. (prev-level (when (> (line-number-at-pos) 1)
  6752. (save-excursion
  6753. (beginning-of-line 0)
  6754. (org-current-level)))))
  6755. (cond ((null current-level) nil) ; Before first headline
  6756. ((null prev-level) 0) ; At first headline
  6757. (prev-level))))
  6758. (defun org-reduced-level (l)
  6759. "Compute the effective level of a heading.
  6760. This takes into account the setting of `org-odd-levels-only'."
  6761. (cond
  6762. ((zerop l) 0)
  6763. (org-odd-levels-only (1+ (floor (/ l 2))))
  6764. (t l)))
  6765. (defun org-level-increment ()
  6766. "Return the number of stars that will be added or removed at a
  6767. time to headlines when structure editing, based on the value of
  6768. `org-odd-levels-only'."
  6769. (if org-odd-levels-only 2 1))
  6770. (defun org-get-valid-level (level &optional change)
  6771. "Rectify a level change under the influence of `org-odd-levels-only'
  6772. LEVEL is a current level, CHANGE is by how much the level should be
  6773. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6774. even level numbers will become the next higher odd number."
  6775. (if org-odd-levels-only
  6776. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6777. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6778. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6779. (max 1 (+ level (or change 0)))))
  6780. (if (boundp 'define-obsolete-function-alias)
  6781. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6782. (define-obsolete-function-alias 'org-get-legal-level
  6783. 'org-get-valid-level)
  6784. (define-obsolete-function-alias 'org-get-legal-level
  6785. 'org-get-valid-level "23.1")))
  6786. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6787. ;; ̀org-with-limited-levels'
  6788. (defun org-promote ()
  6789. "Promote the current heading higher up the tree.
  6790. If the region is active in `transient-mark-mode', promote all headings
  6791. in the region."
  6792. (org-back-to-heading t)
  6793. (let* ((level (save-match-data (funcall outline-level)))
  6794. (after-change-functions (remove 'flyspell-after-change-function
  6795. after-change-functions))
  6796. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6797. (diff (abs (- level (length up-head) -1))))
  6798. (cond ((and (= level 1) org-called-with-limited-levels
  6799. org-allow-promoting-top-level-subtree)
  6800. (replace-match "# " nil t))
  6801. ((= level 1)
  6802. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6803. (t (replace-match up-head nil t)))
  6804. ;; Fixup tag positioning
  6805. (unless (= level 1)
  6806. (and org-auto-align-tags (org-set-tags nil t))
  6807. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6808. (run-hooks 'org-after-promote-entry-hook)))
  6809. (defun org-demote ()
  6810. "Demote the current heading lower down the tree.
  6811. If the region is active in `transient-mark-mode', demote all headings
  6812. in the region."
  6813. (org-back-to-heading t)
  6814. (let* ((level (save-match-data (funcall outline-level)))
  6815. (after-change-functions (remove 'flyspell-after-change-function
  6816. after-change-functions))
  6817. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6818. (diff (abs (- level (length down-head) -1))))
  6819. (replace-match down-head nil t)
  6820. ;; Fixup tag positioning
  6821. (and org-auto-align-tags (org-set-tags nil t))
  6822. (if org-adapt-indentation (org-fixup-indentation diff))
  6823. (run-hooks 'org-after-demote-entry-hook)))
  6824. (defun org-cycle-level ()
  6825. "Cycle the level of an empty headline through possible states.
  6826. This goes first to child, then to parent, level, then up the hierarchy.
  6827. After top level, it switches back to sibling level."
  6828. (interactive)
  6829. (let ((org-adapt-indentation nil))
  6830. (when (org-point-at-end-of-empty-headline)
  6831. (setq this-command 'org-cycle-level) ; Only needed for caching
  6832. (let ((cur-level (org-current-level))
  6833. (prev-level (org-get-previous-line-level)))
  6834. (cond
  6835. ;; If first headline in file, promote to top-level.
  6836. ((= prev-level 0)
  6837. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6838. do (org-do-promote)))
  6839. ;; If same level as prev, demote one.
  6840. ((= prev-level cur-level)
  6841. (org-do-demote))
  6842. ;; If parent is top-level, promote to top level if not already.
  6843. ((= prev-level 1)
  6844. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6845. do (org-do-promote)))
  6846. ;; If top-level, return to prev-level.
  6847. ((= cur-level 1)
  6848. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6849. do (org-do-demote)))
  6850. ;; If less than prev-level, promote one.
  6851. ((< cur-level prev-level)
  6852. (org-do-promote))
  6853. ;; If deeper than prev-level, promote until higher than
  6854. ;; prev-level.
  6855. ((> cur-level prev-level)
  6856. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6857. do (org-do-promote))))
  6858. t))))
  6859. (defun org-map-tree (fun)
  6860. "Call FUN for every heading underneath the current one."
  6861. (org-back-to-heading)
  6862. (let ((level (funcall outline-level)))
  6863. (save-excursion
  6864. (funcall fun)
  6865. (while (and (progn
  6866. (outline-next-heading)
  6867. (> (funcall outline-level) level))
  6868. (not (eobp)))
  6869. (funcall fun)))))
  6870. (defun org-map-region (fun beg end)
  6871. "Call FUN for every heading between BEG and END."
  6872. (let ((org-ignore-region t))
  6873. (save-excursion
  6874. (setq end (copy-marker end))
  6875. (goto-char beg)
  6876. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6877. (< (point) end))
  6878. (funcall fun))
  6879. (while (and (progn
  6880. (outline-next-heading)
  6881. (< (point) end))
  6882. (not (eobp)))
  6883. (funcall fun)))))
  6884. (defvar org-property-end-re) ; silence byte-compiler
  6885. (defun org-fixup-indentation (diff)
  6886. "Change the indentation in the current entry by DIFF.
  6887. However, if any line in the current entry has no indentation, or if it
  6888. would end up with no indentation after the change, nothing at all is done."
  6889. (save-excursion
  6890. (let ((end (save-excursion (outline-next-heading)
  6891. (point-marker)))
  6892. (prohibit (if (> diff 0)
  6893. "^\\S-"
  6894. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6895. col)
  6896. (unless (save-excursion (end-of-line 1)
  6897. (re-search-forward prohibit end t))
  6898. (while (and (< (point) end)
  6899. (re-search-forward "^[ \t]+" end t))
  6900. (goto-char (match-end 0))
  6901. (setq col (current-column))
  6902. (if (< diff 0) (replace-match ""))
  6903. (org-indent-to-column (+ diff col))))
  6904. (move-marker end nil))))
  6905. (defun org-convert-to-odd-levels ()
  6906. "Convert an org-mode file with all levels allowed to one with odd levels.
  6907. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6908. level 5 etc."
  6909. (interactive)
  6910. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6911. (let ((outline-level 'org-outline-level)
  6912. (org-odd-levels-only nil) n)
  6913. (save-excursion
  6914. (goto-char (point-min))
  6915. (while (re-search-forward "^\\*\\*+ " nil t)
  6916. (setq n (- (length (match-string 0)) 2))
  6917. (while (>= (setq n (1- n)) 0)
  6918. (org-demote))
  6919. (end-of-line 1))))))
  6920. (defun org-convert-to-oddeven-levels ()
  6921. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6922. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6923. file contains a section with an even level, conversion would
  6924. destroy the structure of the file. An error is signaled in this
  6925. case."
  6926. (interactive)
  6927. (goto-char (point-min))
  6928. ;; First check if there are no even levels
  6929. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6930. (org-show-context t)
  6931. (error "Not all levels are odd in this file. Conversion not possible"))
  6932. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6933. (let ((outline-regexp org-outline-regexp)
  6934. (outline-level 'org-outline-level)
  6935. (org-odd-levels-only nil) n)
  6936. (save-excursion
  6937. (goto-char (point-min))
  6938. (while (re-search-forward "^\\*\\*+ " nil t)
  6939. (setq n (/ (1- (length (match-string 0))) 2))
  6940. (while (>= (setq n (1- n)) 0)
  6941. (org-promote))
  6942. (end-of-line 1))))))
  6943. (defun org-tr-level (n)
  6944. "Make N odd if required."
  6945. (if org-odd-levels-only (1+ (/ n 2)) n))
  6946. ;;; Vertical tree motion, cutting and pasting of subtrees
  6947. (defun org-move-subtree-up (&optional arg)
  6948. "Move the current subtree up past ARG headlines of the same level."
  6949. (interactive "p")
  6950. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6951. (defun org-move-subtree-down (&optional arg)
  6952. "Move the current subtree down past ARG headlines of the same level."
  6953. (interactive "p")
  6954. (setq arg (prefix-numeric-value arg))
  6955. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6956. 'org-get-last-sibling))
  6957. (ins-point (make-marker))
  6958. (cnt (abs arg))
  6959. (col (current-column))
  6960. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6961. ;; Select the tree
  6962. (org-back-to-heading)
  6963. (setq beg0 (point))
  6964. (save-excursion
  6965. (setq ne-beg (org-back-over-empty-lines))
  6966. (setq beg (point)))
  6967. (save-match-data
  6968. (save-excursion (outline-end-of-heading)
  6969. (setq folded (outline-invisible-p)))
  6970. (outline-end-of-subtree))
  6971. (outline-next-heading)
  6972. (setq ne-end (org-back-over-empty-lines))
  6973. (setq end (point))
  6974. (goto-char beg0)
  6975. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6976. ;; include less whitespace
  6977. (save-excursion
  6978. (goto-char beg)
  6979. (forward-line (- ne-beg ne-end))
  6980. (setq beg (point))))
  6981. ;; Find insertion point, with error handling
  6982. (while (> cnt 0)
  6983. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6984. (progn (goto-char beg0)
  6985. (error "Cannot move past superior level or buffer limit")))
  6986. (setq cnt (1- cnt)))
  6987. (if (> arg 0)
  6988. ;; Moving forward - still need to move over subtree
  6989. (progn (org-end-of-subtree t t)
  6990. (save-excursion
  6991. (org-back-over-empty-lines)
  6992. (or (bolp) (newline)))))
  6993. (setq ne-ins (org-back-over-empty-lines))
  6994. (move-marker ins-point (point))
  6995. (setq txt (buffer-substring beg end))
  6996. (org-save-markers-in-region beg end)
  6997. (delete-region beg end)
  6998. (org-remove-empty-overlays-at beg)
  6999. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7000. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7001. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7002. (let ((bbb (point)))
  7003. (insert-before-markers txt)
  7004. (org-reinstall-markers-in-region bbb)
  7005. (move-marker ins-point bbb))
  7006. (or (bolp) (insert "\n"))
  7007. (setq ins-end (point))
  7008. (goto-char ins-point)
  7009. (org-skip-whitespace)
  7010. (when (and (< arg 0)
  7011. (org-first-sibling-p)
  7012. (> ne-ins ne-beg))
  7013. ;; Move whitespace back to beginning
  7014. (save-excursion
  7015. (goto-char ins-end)
  7016. (let ((kill-whole-line t))
  7017. (kill-line (- ne-ins ne-beg)) (point)))
  7018. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7019. (move-marker ins-point nil)
  7020. (if folded
  7021. (hide-subtree)
  7022. (org-show-entry)
  7023. (show-children)
  7024. (org-cycle-hide-drawers 'children))
  7025. (org-clean-visibility-after-subtree-move)
  7026. ;; move back to the initial column we were at
  7027. (move-to-column col)))
  7028. (defvar org-subtree-clip ""
  7029. "Clipboard for cut and paste of subtrees.
  7030. This is actually only a copy of the kill, because we use the normal kill
  7031. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7032. (defvar org-subtree-clip-folded nil
  7033. "Was the last copied subtree folded?
  7034. This is used to fold the tree back after pasting.")
  7035. (defun org-cut-subtree (&optional n)
  7036. "Cut the current subtree into the clipboard.
  7037. With prefix arg N, cut this many sequential subtrees.
  7038. This is a short-hand for marking the subtree and then cutting it."
  7039. (interactive "p")
  7040. (org-copy-subtree n 'cut))
  7041. (defun org-copy-subtree (&optional n cut force-store-markers)
  7042. "Cut the current subtree into the clipboard.
  7043. With prefix arg N, cut this many sequential subtrees.
  7044. This is a short-hand for marking the subtree and then copying it.
  7045. If CUT is non-nil, actually cut the subtree.
  7046. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7047. of some markers in the region, even if CUT is non-nil. This is
  7048. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7049. (interactive "p")
  7050. (let (beg end folded (beg0 (point)))
  7051. (if (org-called-interactively-p 'any)
  7052. (org-back-to-heading nil) ; take what looks like a subtree
  7053. (org-back-to-heading t)) ; take what is really there
  7054. (setq beg (point))
  7055. (skip-chars-forward " \t\r\n")
  7056. (save-match-data
  7057. (save-excursion (outline-end-of-heading)
  7058. (setq folded (outline-invisible-p)))
  7059. (condition-case nil
  7060. (org-forward-heading-same-level (1- n) t)
  7061. (error nil))
  7062. (org-end-of-subtree t t))
  7063. (setq end (point))
  7064. (goto-char beg0)
  7065. (when (> end beg)
  7066. (setq org-subtree-clip-folded folded)
  7067. (when (or cut force-store-markers)
  7068. (org-save-markers-in-region beg end))
  7069. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7070. (setq org-subtree-clip (current-kill 0))
  7071. (message "%s: Subtree(s) with %d characters"
  7072. (if cut "Cut" "Copied")
  7073. (length org-subtree-clip)))))
  7074. (defun org-paste-subtree (&optional level tree for-yank)
  7075. "Paste the clipboard as a subtree, with modification of headline level.
  7076. The entire subtree is promoted or demoted in order to match a new headline
  7077. level.
  7078. If the cursor is at the beginning of a headline, the same level as
  7079. that headline is used to paste the tree
  7080. If not, the new level is derived from the *visible* headings
  7081. before and after the insertion point, and taken to be the inferior headline
  7082. level of the two. So if the previous visible heading is level 3 and the
  7083. next is level 4 (or vice versa), level 4 will be used for insertion.
  7084. This makes sure that the subtree remains an independent subtree and does
  7085. not swallow low level entries.
  7086. You can also force a different level, either by using a numeric prefix
  7087. argument, or by inserting the heading marker by hand. For example, if the
  7088. cursor is after \"*****\", then the tree will be shifted to level 5.
  7089. If optional TREE is given, use this text instead of the kill ring.
  7090. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7091. move back over whitespace before inserting, and move point to the end of
  7092. the inserted text when done."
  7093. (interactive "P")
  7094. (setq tree (or tree (and kill-ring (current-kill 0))))
  7095. (unless (org-kill-is-subtree-p tree)
  7096. (error "%s"
  7097. (substitute-command-keys
  7098. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7099. (org-with-limited-levels
  7100. (let* ((visp (not (outline-invisible-p)))
  7101. (txt tree)
  7102. (^re_ "\\(\\*+\\)[ \t]*")
  7103. (old-level (if (string-match org-outline-regexp-bol txt)
  7104. (- (match-end 0) (match-beginning 0) 1)
  7105. -1))
  7106. (force-level (cond (level (prefix-numeric-value level))
  7107. ((and (looking-at "[ \t]*$")
  7108. (string-match
  7109. "^\\*+$" (buffer-substring
  7110. (point-at-bol) (point))))
  7111. (- (match-end 1) (match-beginning 1)))
  7112. ((and (bolp)
  7113. (looking-at org-outline-regexp))
  7114. (- (match-end 0) (point) 1))))
  7115. (previous-level (save-excursion
  7116. (condition-case nil
  7117. (progn
  7118. (outline-previous-visible-heading 1)
  7119. (if (looking-at ^re_)
  7120. (- (match-end 0) (match-beginning 0) 1)
  7121. 1))
  7122. (error 1))))
  7123. (next-level (save-excursion
  7124. (condition-case nil
  7125. (progn
  7126. (or (looking-at org-outline-regexp)
  7127. (outline-next-visible-heading 1))
  7128. (if (looking-at ^re_)
  7129. (- (match-end 0) (match-beginning 0) 1)
  7130. 1))
  7131. (error 1))))
  7132. (new-level (or force-level (max previous-level next-level)))
  7133. (shift (if (or (= old-level -1)
  7134. (= new-level -1)
  7135. (= old-level new-level))
  7136. 0
  7137. (- new-level old-level)))
  7138. (delta (if (> shift 0) -1 1))
  7139. (func (if (> shift 0) 'org-demote 'org-promote))
  7140. (org-odd-levels-only nil)
  7141. beg end newend)
  7142. ;; Remove the forced level indicator
  7143. (if force-level
  7144. (delete-region (point-at-bol) (point)))
  7145. ;; Paste
  7146. (beginning-of-line (if (bolp) 1 2))
  7147. (setq beg (point))
  7148. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7149. (insert-before-markers txt)
  7150. (unless (string-match "\n\\'" txt) (insert "\n"))
  7151. (setq newend (point))
  7152. (org-reinstall-markers-in-region beg)
  7153. (setq end (point))
  7154. (goto-char beg)
  7155. (skip-chars-forward " \t\n\r")
  7156. (setq beg (point))
  7157. (if (and (outline-invisible-p) visp)
  7158. (save-excursion (outline-show-heading)))
  7159. ;; Shift if necessary
  7160. (unless (= shift 0)
  7161. (save-restriction
  7162. (narrow-to-region beg end)
  7163. (while (not (= shift 0))
  7164. (org-map-region func (point-min) (point-max))
  7165. (setq shift (+ delta shift)))
  7166. (goto-char (point-min))
  7167. (setq newend (point-max))))
  7168. (when (or (org-called-interactively-p 'interactive) for-yank)
  7169. (message "Clipboard pasted as level %d subtree" new-level))
  7170. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7171. kill-ring
  7172. (eq org-subtree-clip (current-kill 0))
  7173. org-subtree-clip-folded)
  7174. ;; The tree was folded before it was killed/copied
  7175. (hide-subtree))
  7176. (and for-yank (goto-char newend)))))
  7177. (defun org-kill-is-subtree-p (&optional txt)
  7178. "Check if the current kill is an outline subtree, or a set of trees.
  7179. Returns nil if kill does not start with a headline, or if the first
  7180. headline level is not the largest headline level in the tree.
  7181. So this will actually accept several entries of equal levels as well,
  7182. which is OK for `org-paste-subtree'.
  7183. If optional TXT is given, check this string instead of the current kill."
  7184. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7185. (re (org-get-limited-outline-regexp))
  7186. (^re (concat "^" re))
  7187. (start-level (and kill
  7188. (string-match
  7189. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7190. kill)
  7191. (- (match-end 2) (match-beginning 2) 1)))
  7192. (start (1+ (or (match-beginning 2) -1))))
  7193. (if (not start-level)
  7194. (progn
  7195. nil) ;; does not even start with a heading
  7196. (catch 'exit
  7197. (while (setq start (string-match ^re kill (1+ start)))
  7198. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7199. (throw 'exit nil)))
  7200. t))))
  7201. (defvar org-markers-to-move nil
  7202. "Markers that should be moved with a cut-and-paste operation.
  7203. Those markers are stored together with their positions relative to
  7204. the start of the region.")
  7205. (defun org-save-markers-in-region (beg end)
  7206. "Check markers in region.
  7207. If these markers are between BEG and END, record their position relative
  7208. to BEG, so that after moving the block of text, we can put the markers back
  7209. into place.
  7210. This function gets called just before an entry or tree gets cut from the
  7211. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7212. called immediately, to move the markers with the entries."
  7213. (setq org-markers-to-move nil)
  7214. (when (featurep 'org-clock)
  7215. (org-clock-save-markers-for-cut-and-paste beg end))
  7216. (when (featurep 'org-agenda)
  7217. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7218. (defun org-check-and-save-marker (marker beg end)
  7219. "Check if MARKER is between BEG and END.
  7220. If yes, remember the marker and the distance to BEG."
  7221. (when (and (marker-buffer marker)
  7222. (equal (marker-buffer marker) (current-buffer)))
  7223. (if (and (>= marker beg) (< marker end))
  7224. (push (cons marker (- marker beg)) org-markers-to-move))))
  7225. (defun org-reinstall-markers-in-region (beg)
  7226. "Move all remembered markers to their position relative to BEG."
  7227. (mapc (lambda (x)
  7228. (move-marker (car x) (+ beg (cdr x))))
  7229. org-markers-to-move)
  7230. (setq org-markers-to-move nil))
  7231. (defun org-narrow-to-subtree ()
  7232. "Narrow buffer to the current subtree."
  7233. (interactive)
  7234. (save-excursion
  7235. (save-match-data
  7236. (org-with-limited-levels
  7237. (narrow-to-region
  7238. (progn (org-back-to-heading t) (point))
  7239. (progn (org-end-of-subtree t t)
  7240. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7241. (point)))))))
  7242. (defun org-narrow-to-block ()
  7243. "Narrow buffer to the current block."
  7244. (interactive)
  7245. (let* ((case-fold-search t)
  7246. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7247. "^[ \t]*#\\+end_.*")))
  7248. (if blockp
  7249. (narrow-to-region (car blockp) (cdr blockp))
  7250. (error "Not in a block"))))
  7251. (eval-when-compile
  7252. (defvar org-property-drawer-re))
  7253. (defvar org-property-start-re) ;; defined below
  7254. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7255. "Clone the task (subtree) at point N times.
  7256. The clones will be inserted as siblings.
  7257. In interactive use, the user will be prompted for the number of
  7258. clones to be produced, and for a time SHIFT, which may be a
  7259. repeater as used in time stamps, for example `+3d'.
  7260. When a valid repeater is given and the entry contains any time
  7261. stamps, the clones will become a sequence in time, with time
  7262. stamps in the subtree shifted for each clone produced. If SHIFT
  7263. is nil or the empty string, time stamps will be left alone. The
  7264. ID property of the original subtree is removed.
  7265. If the original subtree did contain time stamps with a repeater,
  7266. the following will happen:
  7267. - the repeater will be removed in each clone
  7268. - an additional clone will be produced, with the current, unshifted
  7269. date(s) in the entry.
  7270. - the original entry will be placed *after* all the clones, with
  7271. repeater intact.
  7272. - the start days in the repeater in the original entry will be shifted
  7273. to past the last clone.
  7274. In this way you can spell out a number of instances of a repeating task,
  7275. and still retain the repeater to cover future instances of the task."
  7276. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7277. (let (beg end template task idprop
  7278. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7279. (drawer-re org-drawer-regexp))
  7280. (if (not (and (integerp n) (> n 0)))
  7281. (error "Invalid number of replications %s" n))
  7282. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7283. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7284. shift)))
  7285. (error "Invalid shift specification %s" shift))
  7286. (when doshift
  7287. (setq shift-n (string-to-number (match-string 1 shift))
  7288. shift-what (cdr (assoc (match-string 2 shift)
  7289. '(("d" . day) ("w" . week)
  7290. ("m" . month) ("y" . year))))))
  7291. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7292. (setq nmin 1 nmax n)
  7293. (org-back-to-heading t)
  7294. (setq beg (point))
  7295. (setq idprop (org-entry-get nil "ID"))
  7296. (org-end-of-subtree t t)
  7297. (or (bolp) (insert "\n"))
  7298. (setq end (point))
  7299. (setq template (buffer-substring beg end))
  7300. (when (and doshift
  7301. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7302. (delete-region beg end)
  7303. (setq end beg)
  7304. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7305. (goto-char end)
  7306. (loop for n from nmin to nmax do
  7307. ;; prepare clone
  7308. (with-temp-buffer
  7309. (insert template)
  7310. (org-mode)
  7311. (goto-char (point-min))
  7312. (org-show-subtree)
  7313. (and idprop (if org-clone-delete-id
  7314. (org-entry-delete nil "ID")
  7315. (org-id-get-create t)))
  7316. (unless (= n 0)
  7317. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7318. (kill-whole-line))
  7319. (goto-char (point-min))
  7320. (while (re-search-forward drawer-re nil t)
  7321. (mapc (lambda (d)
  7322. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7323. (goto-char (point-min))
  7324. (when doshift
  7325. (while (re-search-forward org-ts-regexp-both nil t)
  7326. (org-timestamp-change (* n shift-n) shift-what))
  7327. (unless (= n n-no-remove)
  7328. (goto-char (point-min))
  7329. (while (re-search-forward org-ts-regexp nil t)
  7330. (save-excursion
  7331. (goto-char (match-beginning 0))
  7332. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7333. (delete-region (match-beginning 1) (match-end 1)))))))
  7334. (setq task (buffer-string)))
  7335. (insert task))
  7336. (goto-char beg)))
  7337. ;;; Outline Sorting
  7338. (defun org-sort (with-case)
  7339. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7340. Optional argument WITH-CASE means sort case-sensitively."
  7341. (interactive "P")
  7342. (cond
  7343. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7344. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7345. (t
  7346. (org-call-with-arg 'org-sort-entries with-case))))
  7347. (defun org-sort-remove-invisible (s)
  7348. (remove-text-properties 0 (length s) org-rm-props s)
  7349. (while (string-match org-bracket-link-regexp s)
  7350. (setq s (replace-match (if (match-end 2)
  7351. (match-string 3 s)
  7352. (match-string 1 s)) t t s)))
  7353. s)
  7354. (defvar org-priority-regexp) ; defined later in the file
  7355. (defvar org-after-sorting-entries-or-items-hook nil
  7356. "Hook that is run after a bunch of entries or items have been sorted.
  7357. When children are sorted, the cursor is in the parent line when this
  7358. hook gets called. When a region or a plain list is sorted, the cursor
  7359. will be in the first entry of the sorted region/list.")
  7360. (defun org-sort-entries
  7361. (&optional with-case sorting-type getkey-func compare-func property)
  7362. "Sort entries on a certain level of an outline tree.
  7363. If there is an active region, the entries in the region are sorted.
  7364. Else, if the cursor is before the first entry, sort the top-level items.
  7365. Else, the children of the entry at point are sorted.
  7366. Sorting can be alphabetically, numerically, by date/time as given by
  7367. a time stamp, by a property or by priority.
  7368. The command prompts for the sorting type unless it has been given to the
  7369. function through the SORTING-TYPE argument, which needs to be a character,
  7370. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7371. precise meaning of each character:
  7372. n Numerically, by converting the beginning of the entry/item to a number.
  7373. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7374. o By order of TODO keywords.
  7375. t By date/time, either the first active time stamp in the entry, or, if
  7376. none exist, by the first inactive one.
  7377. s By the scheduled date/time.
  7378. d By deadline date/time.
  7379. c By creation time, which is assumed to be the first inactive time stamp
  7380. at the beginning of a line.
  7381. p By priority according to the cookie.
  7382. r By the value of a property.
  7383. Capital letters will reverse the sort order.
  7384. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7385. called with point at the beginning of the record. It must return either
  7386. a string or a number that should serve as the sorting key for that record.
  7387. Comparing entries ignores case by default. However, with an optional argument
  7388. WITH-CASE, the sorting considers case as well."
  7389. (interactive "P")
  7390. (let ((case-func (if with-case 'identity 'downcase))
  7391. (cmstr
  7392. ;; The clock marker is lost when using `sort-subr', let's
  7393. ;; store the clocking string.
  7394. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7395. (save-excursion
  7396. (goto-char org-clock-marker)
  7397. (looking-back "^.*") (match-string-no-properties 0))))
  7398. start beg end stars re re2
  7399. txt what tmp)
  7400. ;; Find beginning and end of region to sort
  7401. (cond
  7402. ((org-region-active-p)
  7403. ;; we will sort the region
  7404. (setq end (region-end)
  7405. what "region")
  7406. (goto-char (region-beginning))
  7407. (if (not (org-at-heading-p)) (outline-next-heading))
  7408. (setq start (point)))
  7409. ((or (org-at-heading-p)
  7410. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7411. ;; we will sort the children of the current headline
  7412. (org-back-to-heading)
  7413. (setq start (point)
  7414. end (progn (org-end-of-subtree t t)
  7415. (or (bolp) (insert "\n"))
  7416. (org-back-over-empty-lines)
  7417. (point))
  7418. what "children")
  7419. (goto-char start)
  7420. (show-subtree)
  7421. (outline-next-heading))
  7422. (t
  7423. ;; we will sort the top-level entries in this file
  7424. (goto-char (point-min))
  7425. (or (org-at-heading-p) (outline-next-heading))
  7426. (setq start (point))
  7427. (goto-char (point-max))
  7428. (beginning-of-line 1)
  7429. (when (looking-at ".*?\\S-")
  7430. ;; File ends in a non-white line
  7431. (end-of-line 1)
  7432. (insert "\n"))
  7433. (setq end (point-max))
  7434. (setq what "top-level")
  7435. (goto-char start)
  7436. (show-all)))
  7437. (setq beg (point))
  7438. (if (>= beg end) (error "Nothing to sort"))
  7439. (looking-at "\\(\\*+\\)")
  7440. (setq stars (match-string 1)
  7441. re (concat "^" (regexp-quote stars) " +")
  7442. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7443. txt (buffer-substring beg end))
  7444. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7445. (if (and (not (equal stars "*")) (string-match re2 txt))
  7446. (error "Region to sort contains a level above the first entry"))
  7447. (unless sorting-type
  7448. (message
  7449. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7450. [t]ime [s]cheduled [d]eadline [c]reated
  7451. A/N/P/R/O/F/T/S/D/C means reversed:"
  7452. what)
  7453. (setq sorting-type (read-char-exclusive))
  7454. (unless getkey-func
  7455. (and (= (downcase sorting-type) ?f)
  7456. (setq getkey-func
  7457. (org-icompleting-read "Sort using function: "
  7458. obarray 'fboundp t nil nil))
  7459. (setq getkey-func (intern getkey-func))))
  7460. (and (= (downcase sorting-type) ?r)
  7461. (not property)
  7462. (setq property
  7463. (org-icompleting-read "Property: "
  7464. (mapcar 'list (org-buffer-property-keys t))
  7465. nil t))))
  7466. (message "Sorting entries...")
  7467. (save-restriction
  7468. (narrow-to-region start end)
  7469. (let ((dcst (downcase sorting-type))
  7470. (case-fold-search nil)
  7471. (now (current-time)))
  7472. (sort-subr
  7473. (/= dcst sorting-type)
  7474. ;; This function moves to the beginning character of the "record" to
  7475. ;; be sorted.
  7476. (lambda nil
  7477. (if (re-search-forward re nil t)
  7478. (goto-char (match-beginning 0))
  7479. (goto-char (point-max))))
  7480. ;; This function moves to the last character of the "record" being
  7481. ;; sorted.
  7482. (lambda nil
  7483. (save-match-data
  7484. (condition-case nil
  7485. (outline-forward-same-level 1)
  7486. (error
  7487. (goto-char (point-max))))))
  7488. ;; This function returns the value that gets sorted against.
  7489. (lambda nil
  7490. (cond
  7491. ((= dcst ?n)
  7492. (if (looking-at org-complex-heading-regexp)
  7493. (string-to-number (match-string 4))
  7494. nil))
  7495. ((= dcst ?a)
  7496. (if (looking-at org-complex-heading-regexp)
  7497. (funcall case-func (match-string 4))
  7498. nil))
  7499. ((= dcst ?t)
  7500. (let ((end (save-excursion (outline-next-heading) (point))))
  7501. (if (or (re-search-forward org-ts-regexp end t)
  7502. (re-search-forward org-ts-regexp-both end t))
  7503. (org-time-string-to-seconds (match-string 0))
  7504. (org-float-time now))))
  7505. ((= dcst ?c)
  7506. (let ((end (save-excursion (outline-next-heading) (point))))
  7507. (if (re-search-forward
  7508. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7509. end t)
  7510. (org-time-string-to-seconds (match-string 0))
  7511. (org-float-time now))))
  7512. ((= dcst ?s)
  7513. (let ((end (save-excursion (outline-next-heading) (point))))
  7514. (if (re-search-forward org-scheduled-time-regexp end t)
  7515. (org-time-string-to-seconds (match-string 1))
  7516. (org-float-time now))))
  7517. ((= dcst ?d)
  7518. (let ((end (save-excursion (outline-next-heading) (point))))
  7519. (if (re-search-forward org-deadline-time-regexp end t)
  7520. (org-time-string-to-seconds (match-string 1))
  7521. (org-float-time now))))
  7522. ((= dcst ?p)
  7523. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7524. (string-to-char (match-string 2))
  7525. org-default-priority))
  7526. ((= dcst ?r)
  7527. (or (org-entry-get nil property) ""))
  7528. ((= dcst ?o)
  7529. (if (looking-at org-complex-heading-regexp)
  7530. (- 9999 (length (member (match-string 2)
  7531. org-todo-keywords-1)))))
  7532. ((= dcst ?f)
  7533. (if getkey-func
  7534. (progn
  7535. (setq tmp (funcall getkey-func))
  7536. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7537. tmp)
  7538. (error "Invalid key function `%s'" getkey-func)))
  7539. (t (error "Invalid sorting type `%c'" sorting-type))))
  7540. nil
  7541. (cond
  7542. ((= dcst ?a) 'string<)
  7543. ((= dcst ?f) compare-func)
  7544. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7545. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7546. ;; Reset the clock marker if needed
  7547. (when cmstr
  7548. (save-excursion
  7549. (goto-char start)
  7550. (search-forward cmstr nil t)
  7551. (move-marker org-clock-marker (point))))
  7552. (message "Sorting entries...done")))
  7553. (defun org-do-sort (table what &optional with-case sorting-type)
  7554. "Sort TABLE of WHAT according to SORTING-TYPE.
  7555. The user will be prompted for the SORTING-TYPE if the call to this
  7556. function does not specify it. WHAT is only for the prompt, to indicate
  7557. what is being sorted. The sorting key will be extracted from
  7558. the car of the elements of the table.
  7559. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7560. (unless sorting-type
  7561. (message
  7562. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7563. what)
  7564. (setq sorting-type (read-char-exclusive)))
  7565. (let ((dcst (downcase sorting-type))
  7566. extractfun comparefun)
  7567. ;; Define the appropriate functions
  7568. (cond
  7569. ((= dcst ?n)
  7570. (setq extractfun 'string-to-number
  7571. comparefun (if (= dcst sorting-type) '< '>)))
  7572. ((= dcst ?a)
  7573. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7574. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7575. comparefun (if (= dcst sorting-type)
  7576. 'string<
  7577. (lambda (a b) (and (not (string< a b))
  7578. (not (string= a b)))))))
  7579. ((= dcst ?t)
  7580. (setq extractfun
  7581. (lambda (x)
  7582. (if (or (string-match org-ts-regexp x)
  7583. (string-match org-ts-regexp-both x))
  7584. (org-float-time
  7585. (org-time-string-to-time (match-string 0 x)))
  7586. 0))
  7587. comparefun (if (= dcst sorting-type) '< '>)))
  7588. (t (error "Invalid sorting type `%c'" sorting-type)))
  7589. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7590. table)
  7591. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7592. ;;; The orgstruct minor mode
  7593. ;; Define a minor mode which can be used in other modes in order to
  7594. ;; integrate the org-mode structure editing commands.
  7595. ;; This is really a hack, because the org-mode structure commands use
  7596. ;; keys which normally belong to the major mode. Here is how it
  7597. ;; works: The minor mode defines all the keys necessary to operate the
  7598. ;; structure commands, but wraps the commands into a function which
  7599. ;; tests if the cursor is currently at a headline or a plain list
  7600. ;; item. If that is the case, the structure command is used,
  7601. ;; temporarily setting many Org-mode variables like regular
  7602. ;; expressions for filling etc. However, when any of those keys is
  7603. ;; used at a different location, function uses `key-binding' to look
  7604. ;; up if the key has an associated command in another currently active
  7605. ;; keymap (minor modes, major mode, global), and executes that
  7606. ;; command. There might be problems if any of the keys is otherwise
  7607. ;; used as a prefix key.
  7608. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7609. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7610. ;; addresses this by checking explicitly for both bindings.
  7611. (defvar orgstruct-mode-map (make-sparse-keymap)
  7612. "Keymap for the minor `orgstruct-mode'.")
  7613. (defvar org-local-vars nil
  7614. "List of local variables, for use by `orgstruct-mode'.")
  7615. ;;;###autoload
  7616. (define-minor-mode orgstruct-mode
  7617. "Toggle the minor mode `orgstruct-mode'.
  7618. This mode is for using Org-mode structure commands in other
  7619. modes. The following keys behave as if Org-mode were active, if
  7620. the cursor is on a headline, or on a plain list item (both as
  7621. defined by Org-mode).
  7622. M-up Move entry/item up
  7623. M-down Move entry/item down
  7624. M-left Promote
  7625. M-right Demote
  7626. M-S-up Move entry/item up
  7627. M-S-down Move entry/item down
  7628. M-S-left Promote subtree
  7629. M-S-right Demote subtree
  7630. M-q Fill paragraph and items like in Org-mode
  7631. C-c ^ Sort entries
  7632. C-c - Cycle list bullet
  7633. TAB Cycle item visibility
  7634. M-RET Insert new heading/item
  7635. S-M-RET Insert new TODO heading / Checkbox item
  7636. C-c C-c Set tags / toggle checkbox"
  7637. nil " OrgStruct" nil
  7638. (org-load-modules-maybe)
  7639. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7640. ;;;###autoload
  7641. (defun turn-on-orgstruct ()
  7642. "Unconditionally turn on `orgstruct-mode'."
  7643. (orgstruct-mode 1))
  7644. (defvar org-fb-vars nil)
  7645. (make-variable-buffer-local 'org-fb-vars)
  7646. (defun orgstruct++-mode (&optional arg)
  7647. "Toggle `orgstruct-mode', the enhanced version of it.
  7648. In addition to setting orgstruct-mode, this also exports all
  7649. indentation and autofilling variables from org-mode into the
  7650. buffer. It will also recognize item context in multiline items."
  7651. (interactive "P")
  7652. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7653. (if (< arg 1)
  7654. (progn (orgstruct-mode -1)
  7655. (mapc (lambda(v)
  7656. (org-set-local (car v)
  7657. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7658. org-fb-vars))
  7659. (orgstruct-mode 1)
  7660. (setq org-fb-vars nil)
  7661. (let (var val)
  7662. (mapc
  7663. (lambda (x)
  7664. (when (string-match
  7665. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7666. (symbol-name (car x)))
  7667. (setq var (car x) val (nth 1 x))
  7668. (push (list var `(quote ,(eval var))) org-fb-vars)
  7669. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7670. org-local-vars)
  7671. (org-set-local 'orgstruct-is-++ t))))
  7672. (defvar orgstruct-is-++ nil
  7673. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7674. (make-variable-buffer-local 'orgstruct-is-++)
  7675. ;;;###autoload
  7676. (defun turn-on-orgstruct++ ()
  7677. "Unconditionally turn on `orgstruct++-mode'."
  7678. (orgstruct++-mode 1))
  7679. (defun orgstruct-error ()
  7680. "Error when there is no default binding for a structure key."
  7681. (interactive)
  7682. (error "This key has no function outside structure elements"))
  7683. (defun orgstruct-setup ()
  7684. "Setup orgstruct keymaps."
  7685. (let ((nfunc 0)
  7686. (bindings
  7687. (list
  7688. '([(meta up)] org-metaup)
  7689. '([(meta down)] org-metadown)
  7690. '([(meta left)] org-metaleft)
  7691. '([(meta right)] org-metaright)
  7692. '([(meta shift up)] org-shiftmetaup)
  7693. '([(meta shift down)] org-shiftmetadown)
  7694. '([(meta shift left)] org-shiftmetaleft)
  7695. '([(meta shift right)] org-shiftmetaright)
  7696. '([?\e (up)] org-metaup)
  7697. '([?\e (down)] org-metadown)
  7698. '([?\e (left)] org-metaleft)
  7699. '([?\e (right)] org-metaright)
  7700. '([?\e (shift up)] org-shiftmetaup)
  7701. '([?\e (shift down)] org-shiftmetadown)
  7702. '([?\e (shift left)] org-shiftmetaleft)
  7703. '([?\e (shift right)] org-shiftmetaright)
  7704. '([(shift up)] org-shiftup)
  7705. '([(shift down)] org-shiftdown)
  7706. '([(shift left)] org-shiftleft)
  7707. '([(shift right)] org-shiftright)
  7708. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7709. '("\M-q" fill-paragraph)
  7710. '("\C-c^" org-sort)
  7711. '("\C-c-" org-cycle-list-bullet)))
  7712. elt key fun cmd)
  7713. (while (setq elt (pop bindings))
  7714. (setq nfunc (1+ nfunc))
  7715. (setq key (org-key (car elt))
  7716. fun (nth 1 elt)
  7717. cmd (orgstruct-make-binding fun nfunc key))
  7718. (org-defkey orgstruct-mode-map key cmd))
  7719. ;; Prevent an error for users who forgot to make autoloads
  7720. (require 'org-element)
  7721. ;; Special treatment needed for TAB and RET
  7722. (org-defkey orgstruct-mode-map [(tab)]
  7723. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7724. (org-defkey orgstruct-mode-map "\C-i"
  7725. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7726. (org-defkey orgstruct-mode-map "\M-\C-m"
  7727. (orgstruct-make-binding 'org-insert-heading 105
  7728. "\M-\C-m" [(meta return)]))
  7729. (org-defkey orgstruct-mode-map [(meta return)]
  7730. (orgstruct-make-binding 'org-insert-heading 106
  7731. [(meta return)] "\M-\C-m"))
  7732. (org-defkey orgstruct-mode-map [(shift meta return)]
  7733. (orgstruct-make-binding 'org-insert-todo-heading 107
  7734. [(meta return)] "\M-\C-m"))
  7735. (org-defkey orgstruct-mode-map "\e\C-m"
  7736. (orgstruct-make-binding 'org-insert-heading 108
  7737. "\e\C-m" [?\e (return)]))
  7738. (org-defkey orgstruct-mode-map [?\e (return)]
  7739. (orgstruct-make-binding 'org-insert-heading 109
  7740. [?\e (return)] "\e\C-m"))
  7741. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7742. (orgstruct-make-binding 'org-insert-todo-heading 110
  7743. [?\e (return)] "\e\C-m"))
  7744. (unless org-local-vars
  7745. (setq org-local-vars (org-get-local-variables)))
  7746. t))
  7747. (defun orgstruct-make-binding (fun n &rest keys)
  7748. "Create a function for binding in the structure minor mode.
  7749. FUN is the command to call inside a table. N is used to create a unique
  7750. command name. KEYS are keys that should be checked in for a command
  7751. to execute outside of tables."
  7752. (eval
  7753. (list 'defun
  7754. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7755. '(arg)
  7756. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7757. "Outside of structure, run the binding of `"
  7758. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7759. "'.")
  7760. '(interactive "p")
  7761. (list 'if
  7762. `(org-context-p 'headline 'item
  7763. (and orgstruct-is-++
  7764. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7765. 'item-body))
  7766. (list 'org-run-like-in-org-mode (list 'quote fun))
  7767. (list 'let '(orgstruct-mode)
  7768. (list 'call-interactively
  7769. (append '(or)
  7770. (mapcar (lambda (k)
  7771. (list 'key-binding k))
  7772. keys)
  7773. '('orgstruct-error))))))))
  7774. (defun org-contextualize-keys (alist contexts)
  7775. "Return valid elements in ALIST depending on CONTEXTS.
  7776. `org-agenda-custom-commands' or `org-capture-templates' are the
  7777. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7778. or `org-capture-templates-contexts' are the associated contexts
  7779. definitions."
  7780. (let ((contexts
  7781. ;; normalize contexts
  7782. (mapcar
  7783. (lambda(c) (cond ((listp (cadr c))
  7784. (list (car c) (car c) (cadr c)))
  7785. ((string= "" (cadr c))
  7786. (list (car c) (car c) (caddr c)))
  7787. (t c))) contexts))
  7788. (a alist) c r s)
  7789. ;; loop over all commands or templates
  7790. (while (setq c (pop a))
  7791. (let (vrules repl)
  7792. (cond
  7793. ((not (assoc (car c) contexts))
  7794. (push c r))
  7795. ((and (assoc (car c) contexts)
  7796. (setq vrules (org-contextualize-validate-key
  7797. (car c) contexts)))
  7798. (mapc (lambda (vr)
  7799. (when (not (equal (car vr) (cadr vr)))
  7800. (setq repl vr))) vrules)
  7801. (if (not repl) (push c r)
  7802. (push (cadr repl) s)
  7803. (push
  7804. (cons (car c)
  7805. (cdr (or (assoc (cadr repl) alist)
  7806. (error "Undefined key `%s' as contextual replacement for `%s'"
  7807. (cadr repl) (car c)))))
  7808. r))))))
  7809. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7810. (delq
  7811. nil
  7812. (delete-dups
  7813. (mapcar (lambda (x)
  7814. (let ((tpl (car x)))
  7815. (when (not (delq
  7816. nil
  7817. (mapcar (lambda(y)
  7818. (equal y tpl)) s))) x)))
  7819. (reverse r))))))
  7820. (defun org-contextualize-validate-key (key contexts)
  7821. "Check CONTEXTS for agenda or capture KEY."
  7822. (let (r rr res)
  7823. (while (setq r (pop contexts))
  7824. (mapc
  7825. (lambda (rr)
  7826. (when
  7827. (and (equal key (car r))
  7828. (if (functionp rr) (funcall rr)
  7829. (or (and (eq (car rr) 'in-file)
  7830. (buffer-file-name)
  7831. (string-match (cdr rr) (buffer-file-name)))
  7832. (and (eq (car rr) 'in-mode)
  7833. (string-match (cdr rr) (symbol-name major-mode)))
  7834. (when (and (eq (car rr) 'not-in-file)
  7835. (buffer-file-name))
  7836. (not (string-match (cdr rr) (buffer-file-name))))
  7837. (when (eq (car rr) 'not-in-mode)
  7838. (not (string-match (cdr rr) (symbol-name major-mode)))))))
  7839. (push r res)))
  7840. (car (last r))))
  7841. (delete-dups (delq nil res))))
  7842. (defun org-context-p (&rest contexts)
  7843. "Check if local context is any of CONTEXTS.
  7844. Possible values in the list of contexts are `table', `headline', and `item'."
  7845. (let ((pos (point)))
  7846. (goto-char (point-at-bol))
  7847. (prog1 (or (and (memq 'table contexts)
  7848. (looking-at "[ \t]*|"))
  7849. (and (memq 'headline contexts)
  7850. (looking-at org-outline-regexp))
  7851. (and (memq 'item contexts)
  7852. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7853. (and (memq 'item-body contexts)
  7854. (org-in-item-p)))
  7855. (goto-char pos))))
  7856. (defun org-get-local-variables ()
  7857. "Return a list of all local variables in an Org mode buffer."
  7858. (let (varlist)
  7859. (with-current-buffer (get-buffer-create "*Org tmp*")
  7860. (erase-buffer)
  7861. (org-mode)
  7862. (setq varlist (buffer-local-variables)))
  7863. (kill-buffer "*Org tmp*")
  7864. (delq nil
  7865. (mapcar
  7866. (lambda (x)
  7867. (setq x
  7868. (if (symbolp x)
  7869. (list x)
  7870. (list (car x) (list 'quote (cdr x)))))
  7871. (if (string-match
  7872. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7873. (symbol-name (car x)))
  7874. x nil))
  7875. varlist))))
  7876. (defun org-clone-local-variables (from-buffer &optional regexp)
  7877. "Clone local variables from FROM-BUFFER.
  7878. Optional argument REGEXP selects variables to clone."
  7879. (mapc
  7880. (lambda (pair)
  7881. (and (symbolp (car pair))
  7882. (or (null regexp)
  7883. (string-match regexp (symbol-name (car pair))))
  7884. (set (make-local-variable (car pair))
  7885. (cdr pair))))
  7886. (buffer-local-variables from-buffer)))
  7887. ;;;###autoload
  7888. (defun org-run-like-in-org-mode (cmd)
  7889. "Run a command, pretending that the current buffer is in Org-mode.
  7890. This will temporarily bind local variables that are typically bound in
  7891. Org-mode to the values they have in Org-mode, and then interactively
  7892. call CMD."
  7893. (org-load-modules-maybe)
  7894. (unless org-local-vars
  7895. (setq org-local-vars (org-get-local-variables)))
  7896. (eval (list 'let org-local-vars
  7897. (list 'call-interactively (list 'quote cmd)))))
  7898. ;;;; Archiving
  7899. (defun org-get-category (&optional pos force-refresh)
  7900. "Get the category applying to position POS."
  7901. (save-match-data
  7902. (if force-refresh (org-refresh-category-properties))
  7903. (let ((pos (or pos (point))))
  7904. (or (get-text-property pos 'org-category)
  7905. (progn (org-refresh-category-properties)
  7906. (get-text-property pos 'org-category))))))
  7907. (defun org-refresh-category-properties ()
  7908. "Refresh category text properties in the buffer."
  7909. (let ((case-fold-search t)
  7910. (inhibit-read-only t)
  7911. (def-cat (cond
  7912. ((null org-category)
  7913. (if buffer-file-name
  7914. (file-name-sans-extension
  7915. (file-name-nondirectory buffer-file-name))
  7916. "???"))
  7917. ((symbolp org-category) (symbol-name org-category))
  7918. (t org-category)))
  7919. beg end cat pos optionp)
  7920. (org-unmodified
  7921. (save-excursion
  7922. (save-restriction
  7923. (widen)
  7924. (goto-char (point-min))
  7925. (put-text-property (point) (point-max) 'org-category def-cat)
  7926. (while (re-search-forward
  7927. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7928. (setq pos (match-end 0)
  7929. optionp (equal (char-after (match-beginning 0)) ?#)
  7930. cat (org-trim (match-string 2)))
  7931. (if optionp
  7932. (setq beg (point-at-bol) end (point-max))
  7933. (org-back-to-heading t)
  7934. (setq beg (point) end (org-end-of-subtree t t)))
  7935. (put-text-property beg end 'org-category cat)
  7936. (put-text-property beg end 'org-category-position beg)
  7937. (goto-char pos)))))))
  7938. ;;;; Link Stuff
  7939. ;;; Link abbreviations
  7940. (defun org-link-expand-abbrev (link)
  7941. "Apply replacements as defined in `org-link-abbrev-alist'."
  7942. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7943. (let* ((key (match-string 1 link))
  7944. (as (or (assoc key org-link-abbrev-alist-local)
  7945. (assoc key org-link-abbrev-alist)))
  7946. (tag (and (match-end 2) (match-string 3 link)))
  7947. rpl)
  7948. (if (not as)
  7949. link
  7950. (setq rpl (cdr as))
  7951. (cond
  7952. ((symbolp rpl) (funcall rpl tag))
  7953. ((string-match "%(\\([^)]+\\))" rpl)
  7954. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  7955. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7956. ((string-match "%h" rpl)
  7957. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7958. (t (concat rpl tag)))))
  7959. link))
  7960. ;;; Storing and inserting links
  7961. (defvar org-insert-link-history nil
  7962. "Minibuffer history for links inserted with `org-insert-link'.")
  7963. (defvar org-stored-links nil
  7964. "Contains the links stored with `org-store-link'.")
  7965. (defvar org-store-link-plist nil
  7966. "Plist with info about the most recently link created with `org-store-link'.")
  7967. (defvar org-link-protocols nil
  7968. "Link protocols added to Org-mode using `org-add-link-type'.")
  7969. (defvar org-store-link-functions nil
  7970. "List of functions that are called to create and store a link.
  7971. Each function will be called in turn until one returns a non-nil
  7972. value. Each function should check if it is responsible for creating
  7973. this link (for example by looking at the major mode).
  7974. If not, it must exit and return nil.
  7975. If yes, it should return a non-nil value after a calling
  7976. `org-store-link-props' with a list of properties and values.
  7977. Special properties are:
  7978. :type The link prefix, like \"http\". This must be given.
  7979. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7980. This is obligatory as well.
  7981. :description Optional default description for the second pair
  7982. of brackets in an Org-mode link. The user can still change
  7983. this when inserting this link into an Org-mode buffer.
  7984. In addition to these, any additional properties can be specified
  7985. and then used in capture templates.")
  7986. (defun org-add-link-type (type &optional follow export)
  7987. "Add TYPE to the list of `org-link-types'.
  7988. Re-compute all regular expressions depending on `org-link-types'
  7989. FOLLOW and EXPORT are two functions.
  7990. FOLLOW should take the link path as the single argument and do whatever
  7991. is necessary to follow the link, for example find a file or display
  7992. a mail message.
  7993. EXPORT should format the link path for export to one of the export formats.
  7994. It should be a function accepting three arguments:
  7995. path the path of the link, the text after the prefix (like \"http:\")
  7996. desc the description of the link, if any, or a description added by
  7997. org-export-normalize-links if there is none
  7998. format the export format, a symbol like `html' or `latex' or `ascii'..
  7999. The function may use the FORMAT information to return different values
  8000. depending on the format. The return value will be put literally into
  8001. the exported file. If the return value is nil, this means Org should
  8002. do what it normally does with links which do not have EXPORT defined.
  8003. Org-mode has a built-in default for exporting links. If you are happy with
  8004. this default, there is no need to define an export function for the link
  8005. type. For a simple example of an export function, see `org-bbdb.el'."
  8006. (add-to-list 'org-link-types type t)
  8007. (org-make-link-regexps)
  8008. (if (assoc type org-link-protocols)
  8009. (setcdr (assoc type org-link-protocols) (list follow export))
  8010. (push (list type follow export) org-link-protocols)))
  8011. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8012. (defvar org-link-to-org-use-id) ; Defined in org-id.el
  8013. ;;;###autoload
  8014. (defun org-store-link (arg)
  8015. "\\<org-mode-map>Store an org-link to the current location.
  8016. This link is added to `org-stored-links' and can later be inserted
  8017. into an org-buffer with \\[org-insert-link].
  8018. For some link types, a prefix arg is interpreted.
  8019. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8020. For file links, arg negates `org-context-in-file-links'.
  8021. A double prefix arg force skipping storing functions that are not
  8022. part of Org's core."
  8023. (interactive "P")
  8024. (org-load-modules-maybe)
  8025. (setq org-store-link-plist nil) ; reset
  8026. (org-with-limited-levels
  8027. (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
  8028. (cond
  8029. ((and (not (equal arg '(16)))
  8030. (setq sfuns
  8031. (delq
  8032. nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
  8033. org-store-link-functions))
  8034. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8035. (or (and (cdr sfuns)
  8036. (funcall (intern
  8037. (completing-read "Which function for creating the link? "
  8038. sfunsn t (car sfunsn)))))
  8039. (funcall (caar sfuns)))
  8040. (setq link (plist-get org-store-link-plist :link)
  8041. desc (or (plist-get org-store-link-plist :description) link))))
  8042. ((org-src-edit-buffer-p)
  8043. (let (label gc)
  8044. (while (or (not label)
  8045. (save-excursion
  8046. (save-restriction
  8047. (widen)
  8048. (goto-char (point-min))
  8049. (re-search-forward
  8050. (regexp-quote (format org-coderef-label-format label))
  8051. nil t))))
  8052. (when label (message "Label exists already") (sit-for 2))
  8053. (setq label (read-string "Code line label: " label)))
  8054. (end-of-line 1)
  8055. (setq link (format org-coderef-label-format label))
  8056. (setq gc (- 79 (length link)))
  8057. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8058. (insert link)
  8059. (setq link (concat "(" label ")") desc nil)))
  8060. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8061. ;; We are in the agenda, link to referenced location
  8062. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8063. (get-text-property (point) 'org-marker))))
  8064. (when m
  8065. (org-with-point-at m
  8066. (setq agenda-link
  8067. (if (org-called-interactively-p 'any)
  8068. (call-interactively 'org-store-link)
  8069. (org-store-link nil)))))))
  8070. ((eq major-mode 'calendar-mode)
  8071. (let ((cd (calendar-cursor-to-date)))
  8072. (setq link
  8073. (format-time-string
  8074. (car org-time-stamp-formats)
  8075. (apply 'encode-time
  8076. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8077. nil nil nil))))
  8078. (org-store-link-props :type "calendar" :date cd)))
  8079. ((eq major-mode 'help-mode)
  8080. (setq link (concat "help:" (save-excursion
  8081. (goto-char (point-min))
  8082. (looking-at "^[^ ]+")
  8083. (match-string 0))))
  8084. (org-store-link-props :type "help"))
  8085. ((eq major-mode 'w3-mode)
  8086. (setq cpltxt (if (and (buffer-name)
  8087. (not (string-match "Untitled" (buffer-name))))
  8088. (buffer-name)
  8089. (url-view-url t))
  8090. link (url-view-url t))
  8091. (org-store-link-props :type "w3" :url (url-view-url t)))
  8092. ((setq search (run-hook-with-args-until-success
  8093. 'org-create-file-search-functions))
  8094. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8095. "::" search))
  8096. (setq cpltxt (or description link)))
  8097. ((eq major-mode 'image-mode)
  8098. (setq cpltxt (concat "file:"
  8099. (abbreviate-file-name buffer-file-name))
  8100. link cpltxt)
  8101. (org-store-link-props :type "image" :file buffer-file-name))
  8102. ((eq major-mode 'dired-mode)
  8103. ;; link to the file in the current line
  8104. (let ((file (dired-get-filename nil t)))
  8105. (setq file (if file
  8106. (abbreviate-file-name
  8107. (expand-file-name (dired-get-filename nil t)))
  8108. ;; otherwise, no file so use current directory.
  8109. default-directory))
  8110. (setq cpltxt (concat "file:" file)
  8111. link cpltxt)))
  8112. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8113. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8114. (cond
  8115. ((org-in-regexp "<<\\(.*?\\)>>")
  8116. (setq cpltxt
  8117. (concat "file:"
  8118. (abbreviate-file-name
  8119. (buffer-file-name (buffer-base-buffer)))
  8120. "::" (match-string 1))
  8121. link cpltxt))
  8122. ((and (featurep 'org-id)
  8123. (or (eq org-link-to-org-use-id t)
  8124. (and (org-called-interactively-p 'any)
  8125. (or (eq org-link-to-org-use-id 'create-if-interactive)
  8126. (and (eq org-link-to-org-use-id
  8127. 'create-if-interactive-and-no-custom-id)
  8128. (not custom-id))))
  8129. (and org-link-to-org-use-id (org-entry-get nil "ID"))))
  8130. ;; We can make a link using the ID.
  8131. (setq link (condition-case nil
  8132. (prog1 (org-id-store-link)
  8133. (setq desc (plist-get org-store-link-plist :description)))
  8134. (error
  8135. ;; probably before first headline, link to file only
  8136. (concat "file:"
  8137. (abbreviate-file-name
  8138. (buffer-file-name (buffer-base-buffer))))))))
  8139. (t
  8140. ;; Just link to current headline
  8141. (setq cpltxt (concat "file:"
  8142. (abbreviate-file-name
  8143. (buffer-file-name (buffer-base-buffer)))))
  8144. ;; Add a context search string
  8145. (when (org-xor org-context-in-file-links arg)
  8146. (let* ((ee (org-element-at-point))
  8147. (et (org-element-type ee))
  8148. (ev (plist-get (cadr ee) :value)))
  8149. (setq txt (cond
  8150. ((org-at-heading-p) nil)
  8151. ((eq et 'keyword) ev)
  8152. ((org-region-active-p)
  8153. (buffer-substring (region-beginning) (region-end)))))
  8154. (when (or (null txt) (string-match "\\S-" txt))
  8155. (setq cpltxt
  8156. (concat cpltxt "::"
  8157. (condition-case nil
  8158. (org-make-org-heading-search-string txt)
  8159. (error "")))
  8160. desc (or (and (eq et 'keyword) ev)
  8161. (nth 4 (ignore-errors (org-heading-components)))
  8162. "NONE")))))
  8163. (if (string-match "::\\'" cpltxt)
  8164. (setq cpltxt (substring cpltxt 0 -2)))
  8165. (setq link cpltxt))))
  8166. ((buffer-file-name (buffer-base-buffer))
  8167. ;; Just link to this file here.
  8168. (setq cpltxt (concat "file:"
  8169. (abbreviate-file-name
  8170. (buffer-file-name (buffer-base-buffer)))))
  8171. ;; Add a context string
  8172. (when (org-xor org-context-in-file-links arg)
  8173. (setq txt (if (org-region-active-p)
  8174. (buffer-substring (region-beginning) (region-end))
  8175. (buffer-substring (point-at-bol) (point-at-eol))))
  8176. ;; Only use search option if there is some text.
  8177. (when (string-match "\\S-" txt)
  8178. (setq cpltxt
  8179. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8180. desc "NONE")))
  8181. (setq link cpltxt))
  8182. ((org-called-interactively-p 'interactive)
  8183. (user-error "No method for storing a link from this buffer"))
  8184. (t (setq link nil)))
  8185. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8186. (setq link (or link cpltxt)
  8187. desc (or desc cpltxt))
  8188. (cond ((equal desc "NONE") (setq desc nil))
  8189. ((string-match org-bracket-link-regexp desc)
  8190. (setq desc (replace-regexp-in-string
  8191. org-bracket-link-regexp
  8192. (concat "\\3" (if (equal (length (match-string 0 desc))
  8193. (length desc)) "*" "")) desc))))
  8194. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8195. (progn
  8196. (setq org-stored-links
  8197. (cons (list link desc) org-stored-links))
  8198. (message "Stored: %s" (or desc link))
  8199. (when custom-id
  8200. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8201. "::#" custom-id))
  8202. (setq org-stored-links
  8203. (cons (list link desc) org-stored-links))))
  8204. (or agenda-link (and link (org-make-link-string link desc)))))))
  8205. (defun org-store-link-props (&rest plist)
  8206. "Store link properties, extract names and addresses."
  8207. (let (x adr)
  8208. (when (setq x (plist-get plist :from))
  8209. (setq adr (mail-extract-address-components x))
  8210. (setq plist (plist-put plist :fromname (car adr)))
  8211. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8212. (when (setq x (plist-get plist :to))
  8213. (setq adr (mail-extract-address-components x))
  8214. (setq plist (plist-put plist :toname (car adr)))
  8215. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8216. (let ((from (plist-get plist :from))
  8217. (to (plist-get plist :to)))
  8218. (when (and from to org-from-is-user-regexp)
  8219. (setq plist
  8220. (plist-put plist :fromto
  8221. (if (string-match org-from-is-user-regexp from)
  8222. (concat "to %t")
  8223. (concat "from %f"))))))
  8224. (setq org-store-link-plist plist))
  8225. (defun org-add-link-props (&rest plist)
  8226. "Add these properties to the link property list."
  8227. (let (key value)
  8228. (while plist
  8229. (setq key (pop plist) value (pop plist))
  8230. (setq org-store-link-plist
  8231. (plist-put org-store-link-plist key value)))))
  8232. (defun org-email-link-description (&optional fmt)
  8233. "Return the description part of an email link.
  8234. This takes information from `org-store-link-plist' and formats it
  8235. according to FMT (default from `org-email-link-description-format')."
  8236. (setq fmt (or fmt org-email-link-description-format))
  8237. (let* ((p org-store-link-plist)
  8238. (to (plist-get p :toaddress))
  8239. (from (plist-get p :fromaddress))
  8240. (table
  8241. (list
  8242. (cons "%c" (plist-get p :fromto))
  8243. (cons "%F" (plist-get p :from))
  8244. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8245. (cons "%T" (plist-get p :to))
  8246. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8247. (cons "%s" (plist-get p :subject))
  8248. (cons "%d" (plist-get p :date))
  8249. (cons "%m" (plist-get p :message-id)))))
  8250. (when (string-match "%c" fmt)
  8251. ;; Check if the user wrote this message
  8252. (if (and org-from-is-user-regexp from to
  8253. (save-match-data (string-match org-from-is-user-regexp from)))
  8254. (setq fmt (replace-match "to %t" t t fmt))
  8255. (setq fmt (replace-match "from %f" t t fmt))))
  8256. (org-replace-escapes fmt table)))
  8257. (defun org-make-org-heading-search-string (&optional string)
  8258. "Make search string for the current headline or STRING."
  8259. (let ((s (or string
  8260. (and (derived-mode-p 'org-mode)
  8261. (save-excursion
  8262. (org-back-to-heading t)
  8263. (plist-get (cadr (org-element-at-point))
  8264. :raw-value)))))
  8265. (lines org-context-in-file-links))
  8266. (or string (setq s (concat "*" s))) ; Add * for headlines
  8267. (when (and string (integerp lines) (> lines 0))
  8268. (let ((slines (org-split-string s "\n")))
  8269. (when (< lines (length slines))
  8270. (setq s (mapconcat
  8271. 'identity
  8272. (reverse (nthcdr (- (length slines) lines)
  8273. (reverse slines))) "\n")))))
  8274. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8275. (defun org-make-link-string (link &optional description)
  8276. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8277. (unless (string-match "\\S-" link)
  8278. (error "Empty link"))
  8279. (when (and description
  8280. (stringp description)
  8281. (not (string-match "\\S-" description)))
  8282. (setq description nil))
  8283. (when (stringp description)
  8284. ;; Remove brackets from the description, they are fatal.
  8285. (while (string-match "\\[" description)
  8286. (setq description (replace-match "{" t t description)))
  8287. (while (string-match "\\]" description)
  8288. (setq description (replace-match "}" t t description))))
  8289. (when (equal link description)
  8290. ;; No description needed, it is identical
  8291. (setq description nil))
  8292. (when (and (not description)
  8293. (not (string-match (org-image-file-name-regexp) link))
  8294. (not (equal link (org-link-escape link))))
  8295. (setq description (org-extract-attributes link)))
  8296. (setq link
  8297. (cond ((string-match (org-image-file-name-regexp) link) link)
  8298. ((string-match org-link-types-re link)
  8299. (concat (match-string 1 link)
  8300. (org-link-escape (substring link (match-end 1)))))
  8301. (t (org-link-escape link))))
  8302. (concat "[[" link "]"
  8303. (if description (concat "[" description "]") "")
  8304. "]"))
  8305. (defconst org-link-escape-chars
  8306. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8307. "List of characters that should be escaped in link.
  8308. This is the list that is used for internal purposes.")
  8309. (defconst org-link-escape-chars-browser
  8310. '(?\ )
  8311. "List of escapes for characters that are problematic in links.
  8312. This is the list that is used before handing over to the browser.")
  8313. (defun org-link-escape (text &optional table merge)
  8314. "Return percent escaped representation of TEXT.
  8315. TEXT is a string with the text to escape.
  8316. Optional argument TABLE is a list with characters that should be
  8317. escaped. When nil, `org-link-escape-chars' is used.
  8318. If optional argument MERGE is set, merge TABLE into
  8319. `org-link-escape-chars'."
  8320. (cond
  8321. ((and table merge)
  8322. (mapc (lambda (defchr)
  8323. (unless (member defchr table)
  8324. (setq table (cons defchr table)))) org-link-escape-chars))
  8325. ((null table)
  8326. (setq table org-link-escape-chars)))
  8327. (mapconcat
  8328. (lambda (char)
  8329. (if (or (member char table)
  8330. (and (or (< char 32) (= char 37) (> char 126))
  8331. org-url-hexify-p))
  8332. (mapconcat (lambda (sequence-element)
  8333. (format "%%%.2X" sequence-element))
  8334. (or (encode-coding-char char 'utf-8)
  8335. (error "Unable to percent escape character: %s"
  8336. (char-to-string char))) "")
  8337. (char-to-string char))) text ""))
  8338. (defun org-link-unescape (str)
  8339. "Unhex hexified Unicode strings as returned from the JavaScript function
  8340. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8341. (unless (and (null str) (string= "" str))
  8342. (let ((pos 0) (case-fold-search t) unhexed)
  8343. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8344. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8345. (setq str (replace-match unhexed t t str))
  8346. (setq pos (+ pos (length unhexed))))))
  8347. str)
  8348. (defun org-link-unescape-compound (hex)
  8349. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8350. Note: this function also decodes single byte encodings like
  8351. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8352. (save-match-data
  8353. (let* ((bytes (cdr (split-string hex "%")))
  8354. (ret "")
  8355. (eat 0)
  8356. (sum 0))
  8357. (while bytes
  8358. (let* ((val (string-to-number (pop bytes) 16))
  8359. (shift-xor
  8360. (if (= 0 eat)
  8361. (cond
  8362. ((>= val 252) (cons 6 252))
  8363. ((>= val 248) (cons 5 248))
  8364. ((>= val 240) (cons 4 240))
  8365. ((>= val 224) (cons 3 224))
  8366. ((>= val 192) (cons 2 192))
  8367. (t (cons 0 0)))
  8368. (cons 6 128))))
  8369. (if (>= val 192) (setq eat (car shift-xor)))
  8370. (setq val (logxor val (cdr shift-xor)))
  8371. (setq sum (+ (lsh sum (car shift-xor)) val))
  8372. (if (> eat 0) (setq eat (- eat 1)))
  8373. (cond
  8374. ((= 0 eat) ;multi byte
  8375. (setq ret (concat ret (org-char-to-string sum)))
  8376. (setq sum 0))
  8377. ((not bytes) ; single byte(s)
  8378. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8379. )) ;; end (while bytes
  8380. ret )))
  8381. (defun org-link-unescape-single-byte-sequence (hex)
  8382. "Unhexify hex-encoded single byte character sequences."
  8383. (mapconcat (lambda (byte)
  8384. (char-to-string (string-to-number byte 16)))
  8385. (cdr (split-string hex "%")) ""))
  8386. (defun org-xor (a b)
  8387. "Exclusive or."
  8388. (if a (not b) b))
  8389. (defun org-fixup-message-id-for-http (s)
  8390. "Replace special characters in a message id, so it can be used in an http query."
  8391. (when (string-match "%" s)
  8392. (setq s (mapconcat (lambda (c)
  8393. (if (eq c ?%)
  8394. "%25"
  8395. (char-to-string c)))
  8396. s "")))
  8397. (while (string-match "<" s)
  8398. (setq s (replace-match "%3C" t t s)))
  8399. (while (string-match ">" s)
  8400. (setq s (replace-match "%3E" t t s)))
  8401. (while (string-match "@" s)
  8402. (setq s (replace-match "%40" t t s)))
  8403. s)
  8404. (defun org-link-prettify (link)
  8405. "Return a human-readable representation of LINK.
  8406. The car of LINK must be a raw link the cdr of LINK must be either
  8407. a link description or nil."
  8408. (let ((desc (or (cadr link) "<no description>")))
  8409. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8410. "<" (car link) ">")))
  8411. ;;;###autoload
  8412. (defun org-insert-link-global ()
  8413. "Insert a link like Org-mode does.
  8414. This command can be called in any mode to insert a link in Org-mode syntax."
  8415. (interactive)
  8416. (org-load-modules-maybe)
  8417. (org-run-like-in-org-mode 'org-insert-link))
  8418. (defun org-insert-all-links (&optional keep)
  8419. "Insert all links in `org-stored-links'."
  8420. (interactive "P")
  8421. (let ((links (copy-sequence org-stored-links)) l)
  8422. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8423. (insert "- ")
  8424. (org-insert-link nil (car l) (cadr l))
  8425. (insert "\n"))))
  8426. (defun org-link-fontify-links-to-this-file ()
  8427. "Fontify links to the current file in `org-stored-links'."
  8428. (let ((f (buffer-file-name)) a b)
  8429. (setq a (mapcar (lambda(l)
  8430. (let ((ll (car l)))
  8431. (when (and (string-match "^file:\\(.+\\)::" ll)
  8432. (equal f (expand-file-name (match-string 1 ll))))
  8433. ll)))
  8434. org-stored-links))
  8435. (when (featurep 'org-id)
  8436. (setq b (mapcar (lambda(l)
  8437. (let ((ll (car l)))
  8438. (when (and (string-match "^id:\\(.+\\)$" ll)
  8439. (equal f (expand-file-name
  8440. (or (org-id-find-id-file
  8441. (match-string 1 ll)) ""))))
  8442. ll)))
  8443. org-stored-links)))
  8444. (mapcar (lambda(l)
  8445. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8446. (delq nil (append a b)))))
  8447. (defvar org-link-links-in-this-file nil)
  8448. (defun org-insert-link (&optional complete-file link-location default-description)
  8449. "Insert a link. At the prompt, enter the link.
  8450. Completion can be used to insert any of the link protocol prefixes like
  8451. http or ftp in use.
  8452. The history can be used to select a link previously stored with
  8453. `org-store-link'. When the empty string is entered (i.e. if you just
  8454. press RET at the prompt), the link defaults to the most recently
  8455. stored link. As SPC triggers completion in the minibuffer, you need to
  8456. use M-SPC or C-q SPC to force the insertion of a space character.
  8457. You will also be prompted for a description, and if one is given, it will
  8458. be displayed in the buffer instead of the link.
  8459. If there is already a link at point, this command will allow you to edit link
  8460. and description parts.
  8461. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8462. be selected using completion. The path to the file will be relative to the
  8463. current directory if the file is in the current directory or a subdirectory.
  8464. Otherwise, the link will be the absolute path as completed in the minibuffer
  8465. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8466. option `org-link-file-path-type'.
  8467. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8468. the current directory or below.
  8469. With three \\[universal-argument] prefixes, negate the meaning of
  8470. `org-keep-stored-link-after-insertion'.
  8471. If `org-make-link-description-function' is non-nil, this function will be
  8472. called with the link target, and the result will be the default
  8473. link description.
  8474. If the LINK-LOCATION parameter is non-nil, this value will be
  8475. used as the link location instead of reading one interactively.
  8476. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8477. be used as the default description."
  8478. (interactive "P")
  8479. (let* ((wcf (current-window-configuration))
  8480. (origbuf (current-buffer))
  8481. (region (if (org-region-active-p)
  8482. (buffer-substring (region-beginning) (region-end))))
  8483. (remove (and region (list (region-beginning) (region-end))))
  8484. (desc region)
  8485. tmphist ; byte-compile incorrectly complains about this
  8486. (link link-location)
  8487. (abbrevs org-link-abbrev-alist-local)
  8488. entry file all-prefixes auto-desc)
  8489. (cond
  8490. (link-location) ; specified by arg, just use it.
  8491. ((org-in-regexp org-bracket-link-regexp 1)
  8492. ;; We do have a link at point, and we are going to edit it.
  8493. (setq remove (list (match-beginning 0) (match-end 0)))
  8494. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8495. (setq link (read-string "Link: "
  8496. (org-link-unescape
  8497. (org-match-string-no-properties 1)))))
  8498. ((or (org-in-regexp org-angle-link-re)
  8499. (org-in-regexp org-plain-link-re))
  8500. ;; Convert to bracket link
  8501. (setq remove (list (match-beginning 0) (match-end 0))
  8502. link (read-string "Link: "
  8503. (org-remove-angle-brackets (match-string 0)))))
  8504. ((member complete-file '((4) (16)))
  8505. ;; Completing read for file names.
  8506. (setq link (org-file-complete-link complete-file)))
  8507. (t
  8508. ;; Read link, with completion for stored links.
  8509. (org-link-fontify-links-to-this-file)
  8510. (org-switch-to-buffer-other-window "*Org Links*")
  8511. (with-current-buffer "*Org Links*"
  8512. (erase-buffer)
  8513. (insert "Insert a link.
  8514. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8515. (when org-stored-links
  8516. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8517. (insert (mapconcat 'org-link-prettify
  8518. (reverse org-stored-links) "\n")))
  8519. (goto-char (point-min)))
  8520. (let ((cw (selected-window)))
  8521. (select-window (get-buffer-window "*Org Links*" 'visible))
  8522. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8523. (unless (pos-visible-in-window-p (point-max))
  8524. (org-fit-window-to-buffer))
  8525. (and (window-live-p cw) (select-window cw)))
  8526. ;; Fake a link history, containing the stored links.
  8527. (setq tmphist (append (mapcar 'car org-stored-links)
  8528. org-insert-link-history))
  8529. (setq all-prefixes (append (mapcar 'car abbrevs)
  8530. (mapcar 'car org-link-abbrev-alist)
  8531. org-link-types))
  8532. (unwind-protect
  8533. (progn
  8534. (setq link
  8535. (org-completing-read
  8536. "Link: "
  8537. (append
  8538. (mapcar (lambda (x) (concat x ":"))
  8539. all-prefixes)
  8540. (mapcar 'car org-stored-links))
  8541. nil nil nil
  8542. 'tmphist
  8543. (caar org-stored-links)))
  8544. (if (not (string-match "\\S-" link))
  8545. (error "No link selected"))
  8546. (mapc (lambda(l)
  8547. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8548. org-stored-links)
  8549. (if (or (member link all-prefixes)
  8550. (and (equal ":" (substring link -1))
  8551. (member (substring link 0 -1) all-prefixes)
  8552. (setq link (substring link 0 -1))))
  8553. (setq link (with-current-buffer origbuf
  8554. (org-link-try-special-completion link)))))
  8555. (set-window-configuration wcf)
  8556. (kill-buffer "*Org Links*"))
  8557. (setq entry (assoc link org-stored-links))
  8558. (or entry (push link org-insert-link-history))
  8559. (setq desc (or desc (nth 1 entry)))))
  8560. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8561. (not org-keep-stored-link-after-insertion))
  8562. (setq org-stored-links (delq (assoc link org-stored-links)
  8563. org-stored-links)))
  8564. (if (string-match org-plain-link-re link)
  8565. ;; URL-like link, normalize the use of angular brackets.
  8566. (setq link (org-remove-angle-brackets link)))
  8567. ;; Check if we are linking to the current file with a search
  8568. ;; option If yes, simplify the link by using only the search
  8569. ;; option.
  8570. (when (and buffer-file-name
  8571. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8572. (let* ((path (match-string 1 link))
  8573. (case-fold-search nil)
  8574. (search (match-string 2 link)))
  8575. (save-match-data
  8576. (if (equal (file-truename buffer-file-name) (file-truename path))
  8577. ;; We are linking to this same file, with a search option
  8578. (setq link search)))))
  8579. ;; Check if we can/should use a relative path. If yes, simplify the link
  8580. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8581. (let* ((type (match-string 1 link))
  8582. (path (match-string 2 link))
  8583. (origpath path)
  8584. (case-fold-search nil))
  8585. (cond
  8586. ((or (eq org-link-file-path-type 'absolute)
  8587. (equal complete-file '(16)))
  8588. (setq path (abbreviate-file-name (expand-file-name path))))
  8589. ((eq org-link-file-path-type 'noabbrev)
  8590. (setq path (expand-file-name path)))
  8591. ((eq org-link-file-path-type 'relative)
  8592. (setq path (file-relative-name path)))
  8593. (t
  8594. (save-match-data
  8595. (if (string-match (concat "^" (regexp-quote
  8596. (expand-file-name
  8597. (file-name-as-directory
  8598. default-directory))))
  8599. (expand-file-name path))
  8600. ;; We are linking a file with relative path name.
  8601. (setq path (substring (expand-file-name path)
  8602. (match-end 0)))
  8603. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8604. (setq link (concat type path))
  8605. (if (equal desc origpath)
  8606. (setq desc path))))
  8607. (if org-make-link-description-function
  8608. (setq desc
  8609. (or (condition-case nil
  8610. (funcall org-make-link-description-function link desc)
  8611. (error (progn (message "Can't get link description from `%s'"
  8612. (symbol-name org-make-link-description-function))
  8613. (sit-for 2) nil)))
  8614. (read-string "Description: " default-description)))
  8615. (if default-description (setq desc default-description)
  8616. (setq desc (or (and auto-desc desc)
  8617. (read-string "Description: " desc)))))
  8618. (unless (string-match "\\S-" desc) (setq desc nil))
  8619. (if remove (apply 'delete-region remove))
  8620. (insert (org-make-link-string link desc))))
  8621. (defun org-link-try-special-completion (type)
  8622. "If there is completion support for link type TYPE, offer it."
  8623. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8624. (if (functionp fun)
  8625. (funcall fun)
  8626. (read-string "Link (no completion support): " (concat type ":")))))
  8627. (defun org-file-complete-link (&optional arg)
  8628. "Create a file link using completion."
  8629. (let (file link)
  8630. (setq file (org-iread-file-name "File: "))
  8631. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8632. (pwd1 (file-name-as-directory (abbreviate-file-name
  8633. (expand-file-name ".")))))
  8634. (cond
  8635. ((equal arg '(16))
  8636. (setq link (concat
  8637. "file:"
  8638. (abbreviate-file-name (expand-file-name file)))))
  8639. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8640. (setq link (concat "file:" (match-string 1 file))))
  8641. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8642. (expand-file-name file))
  8643. (setq link (concat
  8644. "file:" (match-string 1 (expand-file-name file)))))
  8645. (t (setq link (concat "file:" file)))))
  8646. link))
  8647. (defun org-iread-file-name (&rest args)
  8648. "Read-file-name using `ido-mode' speedup if available.
  8649. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8650. See `read-file-name' for a description of parameters."
  8651. (org-without-partial-completion
  8652. (if (and org-completion-use-ido
  8653. (fboundp 'ido-read-file-name)
  8654. (boundp 'ido-mode) ido-mode
  8655. (listp (second args)))
  8656. (let ((ido-enter-matching-directory nil))
  8657. (apply 'ido-read-file-name args))
  8658. (apply 'read-file-name args))))
  8659. (defun org-completing-read (&rest args)
  8660. "Completing-read with SPACE being a normal character."
  8661. (let ((enable-recursive-minibuffers t)
  8662. (minibuffer-local-completion-map
  8663. (copy-keymap minibuffer-local-completion-map)))
  8664. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8665. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8666. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8667. (apply 'org-icompleting-read args)))
  8668. (defun org-completing-read-no-i (&rest args)
  8669. (let (org-completion-use-ido org-completion-use-iswitchb)
  8670. (apply 'org-completing-read args)))
  8671. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8672. "Use iswitch as a completing-read replacement to choose from choices.
  8673. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8674. from."
  8675. (let* ((iswitchb-use-virtual-buffers nil)
  8676. (iswitchb-make-buflist-hook
  8677. (lambda ()
  8678. (setq iswitchb-temp-buflist choices))))
  8679. (iswitchb-read-buffer prompt)))
  8680. (defun org-icompleting-read (&rest args)
  8681. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8682. (org-without-partial-completion
  8683. (if (and org-completion-use-ido
  8684. (fboundp 'ido-completing-read)
  8685. (boundp 'ido-mode) ido-mode
  8686. (listp (second args)))
  8687. (let ((ido-enter-matching-directory nil))
  8688. (apply 'ido-completing-read (concat (car args))
  8689. (if (consp (car (nth 1 args)))
  8690. (mapcar 'car (nth 1 args))
  8691. (nth 1 args))
  8692. (cddr args)))
  8693. (if (and org-completion-use-iswitchb
  8694. (boundp 'iswitchb-mode) iswitchb-mode
  8695. (listp (second args)))
  8696. (apply 'org-iswitchb-completing-read (concat (car args))
  8697. (if (consp (car (nth 1 args)))
  8698. (mapcar 'car (nth 1 args))
  8699. (nth 1 args))
  8700. (cddr args))
  8701. (apply 'completing-read args)))))
  8702. (defun org-extract-attributes (s)
  8703. "Extract the attributes cookie from a string and set as text property."
  8704. (let (a attr (start 0) key value)
  8705. (save-match-data
  8706. (when (string-match "{{\\([^}]+\\)}}$" s)
  8707. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8708. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8709. (setq key (match-string 1 a) value (match-string 2 a)
  8710. start (match-end 0)
  8711. attr (plist-put attr (intern key) value))))
  8712. (org-add-props s nil 'org-attr attr))
  8713. s))
  8714. (defun org-extract-attributes-from-string (tag)
  8715. (let (key value attr)
  8716. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8717. (setq key (match-string 1 tag) value (match-string 2 tag)
  8718. tag (replace-match "" t t tag)
  8719. attr (plist-put attr (intern key) value)))
  8720. (cons tag attr)))
  8721. (defun org-attributes-to-string (plist)
  8722. "Format a property list into an HTML attribute list."
  8723. (let ((s "") key value)
  8724. (while plist
  8725. (setq key (pop plist) value (pop plist))
  8726. (and value
  8727. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8728. s))
  8729. ;;; Opening/following a link
  8730. (defvar org-link-search-failed nil)
  8731. (defvar org-open-link-functions nil
  8732. "Hook for functions finding a plain text link.
  8733. These functions must take a single argument, the link content.
  8734. They will be called for links that look like [[link text][description]]
  8735. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8736. like a filename (e.g. \"./blue.png\").
  8737. These functions will be called *before* Org attempts to resolve the
  8738. link by doing text searches in the current buffer - so if you want a
  8739. link \"[[target]]\" to still find \"<<target>>\", your function should
  8740. handle this as a special case.
  8741. When the function does handle the link, it must return a non-nil value.
  8742. If it decides that it is not responsible for this link, it must return
  8743. nil to indicate that that Org-mode can continue with other options
  8744. like exact and fuzzy text search.")
  8745. (defun org-next-link ()
  8746. "Move forward to the next link.
  8747. If the link is in hidden text, expose it."
  8748. (interactive)
  8749. (when (and org-link-search-failed (eq this-command last-command))
  8750. (goto-char (point-min))
  8751. (message "Link search wrapped back to beginning of buffer"))
  8752. (setq org-link-search-failed nil)
  8753. (let* ((pos (point))
  8754. (ct (org-context))
  8755. (a (assoc :link ct)))
  8756. (if a (goto-char (nth 2 a)))
  8757. (if (re-search-forward org-any-link-re nil t)
  8758. (progn
  8759. (goto-char (match-beginning 0))
  8760. (if (outline-invisible-p) (org-show-context)))
  8761. (goto-char pos)
  8762. (setq org-link-search-failed t)
  8763. (error "No further link found"))))
  8764. (defun org-previous-link ()
  8765. "Move backward to the previous link.
  8766. If the link is in hidden text, expose it."
  8767. (interactive)
  8768. (when (and org-link-search-failed (eq this-command last-command))
  8769. (goto-char (point-max))
  8770. (message "Link search wrapped back to end of buffer"))
  8771. (setq org-link-search-failed nil)
  8772. (let* ((pos (point))
  8773. (ct (org-context))
  8774. (a (assoc :link ct)))
  8775. (if a (goto-char (nth 1 a)))
  8776. (if (re-search-backward org-any-link-re nil t)
  8777. (progn
  8778. (goto-char (match-beginning 0))
  8779. (if (outline-invisible-p) (org-show-context)))
  8780. (goto-char pos)
  8781. (setq org-link-search-failed t)
  8782. (error "No further link found"))))
  8783. (defun org-translate-link (s)
  8784. "Translate a link string if a translation function has been defined."
  8785. (if (and org-link-translation-function
  8786. (fboundp org-link-translation-function)
  8787. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8788. (progn
  8789. (setq s (funcall org-link-translation-function
  8790. (match-string 1 s) (match-string 2 s)))
  8791. (concat (car s) ":" (cdr s)))
  8792. s))
  8793. (defun org-translate-link-from-planner (type path)
  8794. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8795. This is still an experimental function, your mileage may vary."
  8796. (cond
  8797. ((member type '("http" "https" "news" "ftp"))
  8798. ;; standard Internet links are the same.
  8799. nil)
  8800. ((and (equal type "irc") (string-match "^//" path))
  8801. ;; Planner has two / at the beginning of an irc link, we have 1.
  8802. ;; We should have zero, actually....
  8803. (setq path (substring path 1)))
  8804. ((and (equal type "lisp") (string-match "^/" path))
  8805. ;; Planner has a slash, we do not.
  8806. (setq type "elisp" path (substring path 1)))
  8807. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8808. ;; A typical message link. Planner has the id after the final slash,
  8809. ;; we separate it with a hash mark
  8810. (setq path (concat (match-string 1 path) "#"
  8811. (org-remove-angle-brackets (match-string 2 path)))))
  8812. )
  8813. (cons type path))
  8814. (defun org-find-file-at-mouse (ev)
  8815. "Open file link or URL at mouse."
  8816. (interactive "e")
  8817. (mouse-set-point ev)
  8818. (org-open-at-point 'in-emacs))
  8819. (defun org-open-at-mouse (ev)
  8820. "Open file link or URL at mouse.
  8821. See the docstring of `org-open-file' for details."
  8822. (interactive "e")
  8823. (mouse-set-point ev)
  8824. (if (eq major-mode 'org-agenda-mode)
  8825. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8826. (org-open-at-point))
  8827. (defvar org-window-config-before-follow-link nil
  8828. "The window configuration before following a link.
  8829. This is saved in case the need arises to restore it.")
  8830. (defvar org-open-link-marker (make-marker)
  8831. "Marker pointing to the location where `org-open-at-point; was called.")
  8832. ;;;###autoload
  8833. (defun org-open-at-point-global ()
  8834. "Follow a link like Org-mode does.
  8835. This command can be called in any mode to follow a link that has
  8836. Org-mode syntax."
  8837. (interactive)
  8838. (org-run-like-in-org-mode 'org-open-at-point))
  8839. ;;;###autoload
  8840. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8841. "Open a link in the string S, as if it was in Org-mode."
  8842. (interactive "sLink: \nP")
  8843. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8844. (with-temp-buffer
  8845. (let ((org-inhibit-startup (not reference-buffer)))
  8846. (org-mode)
  8847. (insert s)
  8848. (goto-char (point-min))
  8849. (when reference-buffer
  8850. (setq org-link-abbrev-alist-local
  8851. (with-current-buffer reference-buffer
  8852. org-link-abbrev-alist-local)))
  8853. (org-open-at-point arg reference-buffer)))))
  8854. (defvar org-open-at-point-functions nil
  8855. "Hook that is run when following a link at point.
  8856. Functions in this hook must return t if they identify and follow
  8857. a link at point. If they don't find anything interesting at point,
  8858. they must return nil.")
  8859. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  8860. (defun org-open-at-point (&optional arg reference-buffer)
  8861. "Open link at or after point.
  8862. If there is no link at point, this function will search forward up to
  8863. the end of the current line.
  8864. Normally, files will be opened by an appropriate application. If the
  8865. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8866. With a double prefix argument, try to open outside of Emacs, in the
  8867. application the system uses for this file type."
  8868. (interactive "P")
  8869. ;; if in a code block, then open the block's results
  8870. (unless (call-interactively #'org-babel-open-src-block-result)
  8871. (org-load-modules-maybe)
  8872. (move-marker org-open-link-marker (point))
  8873. (setq org-window-config-before-follow-link (current-window-configuration))
  8874. (org-remove-occur-highlights nil nil t)
  8875. (cond
  8876. ((and (org-at-heading-p)
  8877. (not (org-at-timestamp-p t))
  8878. (not (org-in-regexp
  8879. (concat org-plain-link-re "\\|"
  8880. org-bracket-link-regexp "\\|"
  8881. org-angle-link-re "\\|"
  8882. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8883. (not (get-text-property (point) 'org-linked-text)))
  8884. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  8885. (lk (car lkall))
  8886. (lkend (cdr lkall)))
  8887. (when lk
  8888. (prog1 (search-forward lk nil lkend)
  8889. (goto-char (match-beginning 0))
  8890. (org-open-at-point))))
  8891. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8892. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8893. ((and (org-at-timestamp-p t)
  8894. (not (org-in-regexp org-bracket-link-regexp)))
  8895. (org-follow-timestamp-link))
  8896. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8897. (not (org-in-regexp org-any-link-re)))
  8898. (org-footnote-action))
  8899. (t
  8900. (let (type path link line search (pos (point)))
  8901. (catch 'match
  8902. (save-excursion
  8903. (skip-chars-forward "^]\n\r")
  8904. (when (org-in-regexp org-bracket-link-regexp 1)
  8905. (setq link (org-extract-attributes
  8906. (org-link-unescape (org-match-string-no-properties 1))))
  8907. (while (string-match " *\n *" link)
  8908. (setq link (replace-match " " t t link)))
  8909. (setq link (org-link-expand-abbrev link))
  8910. (cond
  8911. ((or (file-name-absolute-p link)
  8912. (string-match "^\\.\\.?/" link))
  8913. (setq type "file" path link))
  8914. ((string-match org-link-re-with-space3 link)
  8915. (setq type (match-string 1 link) path (match-string 2 link)))
  8916. ((string-match "^help:+\\(.+\\)" link)
  8917. (setq type "help" path (match-string 1 link)))
  8918. (t (setq type "thisfile" path link)))
  8919. (throw 'match t)))
  8920. (when (get-text-property (point) 'org-linked-text)
  8921. (setq type "thisfile"
  8922. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8923. (1+ (point)) (point))
  8924. path (buffer-substring
  8925. (or (previous-single-property-change pos 'org-linked-text)
  8926. (point-min))
  8927. (or (next-single-property-change pos 'org-linked-text)
  8928. (point-max))))
  8929. (throw 'match t))
  8930. (save-excursion
  8931. (let ((plinkpos (org-in-regexp org-plain-link-re)))
  8932. (when (or (org-in-regexp org-angle-link-re)
  8933. (and plinkpos (goto-char (car plinkpos))
  8934. (save-match-data (not (looking-back "\\[\\[")))))
  8935. (setq type (match-string 1)
  8936. path (org-link-unescape (match-string 2)))
  8937. (throw 'match t))))
  8938. (save-excursion
  8939. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8940. (setq type "tags"
  8941. path (match-string 1))
  8942. (while (string-match ":" path)
  8943. (setq path (replace-match "+" t t path)))
  8944. (throw 'match t)))
  8945. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8946. (setq type "tree-match"
  8947. path (match-string 1))
  8948. (throw 'match t)))
  8949. (unless path
  8950. (user-error "No link found"))
  8951. ;; switch back to reference buffer
  8952. ;; needed when if called in a temporary buffer through
  8953. ;; org-open-link-from-string
  8954. (with-current-buffer (or reference-buffer (current-buffer))
  8955. ;; Remove any trailing spaces in path
  8956. (if (string-match " +\\'" path)
  8957. (setq path (replace-match "" t t path)))
  8958. (if (and org-link-translation-function
  8959. (fboundp org-link-translation-function))
  8960. ;; Check if we need to translate the link
  8961. (let ((tmp (funcall org-link-translation-function type path)))
  8962. (setq type (car tmp) path (cdr tmp))))
  8963. (cond
  8964. ((assoc type org-link-protocols)
  8965. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8966. ((equal type "help")
  8967. (let ((f-or-v (intern path)))
  8968. (cond ((fboundp f-or-v)
  8969. (describe-function f-or-v))
  8970. ((boundp f-or-v)
  8971. (describe-variable f-or-v))
  8972. (t (error "Not a known function or variable")))))
  8973. ((equal type "mailto")
  8974. (let ((cmd (car org-link-mailto-program))
  8975. (args (cdr org-link-mailto-program)) args1
  8976. (address path) (subject "") a)
  8977. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8978. (setq address (match-string 1 path)
  8979. subject (org-link-escape (match-string 2 path))))
  8980. (while args
  8981. (cond
  8982. ((not (stringp (car args))) (push (pop args) args1))
  8983. (t (setq a (pop args))
  8984. (if (string-match "%a" a)
  8985. (setq a (replace-match address t t a)))
  8986. (if (string-match "%s" a)
  8987. (setq a (replace-match subject t t a)))
  8988. (push a args1))))
  8989. (apply cmd (nreverse args1))))
  8990. ((member type '("http" "https" "ftp" "news"))
  8991. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8992. (org-link-escape
  8993. path org-link-escape-chars-browser)
  8994. path))))
  8995. ((string= type "doi")
  8996. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  8997. (org-link-escape
  8998. path org-link-escape-chars-browser)
  8999. path))))
  9000. ((member type '("message"))
  9001. (browse-url (concat type ":" path)))
  9002. ((string= type "tags")
  9003. (org-tags-view arg path))
  9004. ((string= type "tree-match")
  9005. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9006. ((string= type "file")
  9007. (if (string-match "::\\([0-9]+\\)\\'" path)
  9008. (setq line (string-to-number (match-string 1 path))
  9009. path (substring path 0 (match-beginning 0)))
  9010. (if (string-match "::\\(.+\\)\\'" path)
  9011. (setq search (match-string 1 path)
  9012. path (substring path 0 (match-beginning 0)))))
  9013. (if (string-match "[*?{]" (file-name-nondirectory path))
  9014. (dired path)
  9015. (org-open-file path arg line search)))
  9016. ((string= type "shell")
  9017. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9018. (cmd path))
  9019. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9020. (string-match org-confirm-shell-link-not-regexp cmd))
  9021. (not org-confirm-shell-link-function)
  9022. (funcall org-confirm-shell-link-function
  9023. (format "Execute \"%s\" in shell? "
  9024. (org-add-props cmd nil
  9025. 'face 'org-warning))))
  9026. (progn
  9027. (message "Executing %s" cmd)
  9028. (shell-command cmd buf)
  9029. (if (featurep 'midnight)
  9030. (setq clean-buffer-list-kill-buffer-names
  9031. (cons buf clean-buffer-list-kill-buffer-names))))
  9032. (error "Abort"))))
  9033. ((string= type "elisp")
  9034. (let ((cmd path))
  9035. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9036. (string-match org-confirm-elisp-link-not-regexp cmd))
  9037. (not org-confirm-elisp-link-function)
  9038. (funcall org-confirm-elisp-link-function
  9039. (format "Execute \"%s\" as elisp? "
  9040. (org-add-props cmd nil
  9041. 'face 'org-warning))))
  9042. (message "%s => %s" cmd
  9043. (if (equal (string-to-char cmd) ?\()
  9044. (eval (read cmd))
  9045. (call-interactively (read cmd))))
  9046. (error "Abort"))))
  9047. ((and (string= type "thisfile")
  9048. (run-hook-with-args-until-success
  9049. 'org-open-link-functions path)))
  9050. ((string= type "thisfile")
  9051. (if arg
  9052. (switch-to-buffer-other-window
  9053. (org-get-buffer-for-internal-link (current-buffer)))
  9054. (org-mark-ring-push))
  9055. (let ((cmd `(org-link-search
  9056. ,path
  9057. ,(cond ((equal arg '(4)) ''occur)
  9058. ((equal arg '(16)) ''org-occur))
  9059. ,pos)))
  9060. (condition-case nil (let ((org-link-search-inhibit-query t))
  9061. (eval cmd))
  9062. (error (progn (widen) (eval cmd))))))
  9063. (t (browse-url-at-point)))))))
  9064. (move-marker org-open-link-marker nil)
  9065. (run-hook-with-args 'org-follow-link-hook)))
  9066. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9067. "Offer links in the current entry and return the selected link.
  9068. If there is only one link, return it.
  9069. If NTH is an integer, return the NTH link found.
  9070. If ZERO is a string, check also this string for a link, and if
  9071. there is one, return it."
  9072. (with-current-buffer buffer
  9073. (save-excursion
  9074. (save-restriction
  9075. (widen)
  9076. (goto-char marker)
  9077. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9078. "\\(" org-angle-link-re "\\)\\|"
  9079. "\\(" org-plain-link-re "\\)"))
  9080. (cnt ?0)
  9081. (in-emacs (if (integerp nth) nil nth))
  9082. have-zero end links link c)
  9083. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9084. (push (match-string 0 zero) links)
  9085. (setq cnt (1- cnt) have-zero t))
  9086. (save-excursion
  9087. (org-back-to-heading t)
  9088. (setq end (save-excursion (outline-next-heading) (point)))
  9089. (while (re-search-forward re end t)
  9090. (push (match-string 0) links))
  9091. (setq links (org-uniquify (reverse links))))
  9092. (cond
  9093. ((null links)
  9094. (message "No links"))
  9095. ((equal (length links) 1)
  9096. (setq link (car links)))
  9097. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9098. (setq link (nth (if have-zero nth (1- nth)) links)))
  9099. (t ; we have to select a link
  9100. (save-excursion
  9101. (save-window-excursion
  9102. (delete-other-windows)
  9103. (with-output-to-temp-buffer "*Select Link*"
  9104. (mapc (lambda (l)
  9105. (if (not (string-match org-bracket-link-regexp l))
  9106. (princ (format "[%c] %s\n" (incf cnt)
  9107. (org-remove-angle-brackets l)))
  9108. (if (match-end 3)
  9109. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9110. (match-string 3 l) (match-string 1 l)))
  9111. (princ (format "[%c] %s\n" (incf cnt)
  9112. (match-string 1 l))))))
  9113. links))
  9114. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9115. (message "Select link to open, RET to open all:")
  9116. (setq c (read-char-exclusive))
  9117. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9118. (when (equal c ?q) (error "Abort"))
  9119. (if (equal c ?\C-m)
  9120. (setq link links)
  9121. (setq nth (- c ?0))
  9122. (if have-zero (setq nth (1+ nth)))
  9123. (unless (and (integerp nth) (>= (length links) nth))
  9124. (error "Invalid link selection"))
  9125. (setq link (nth (1- nth) links)))))
  9126. (cons link end))))))
  9127. ;; Add special file links that specify the way of opening
  9128. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9129. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9130. (defun org-open-file-with-system (path)
  9131. "Open file at PATH using the system way of opening it."
  9132. (org-open-file path 'system))
  9133. (defun org-open-file-with-emacs (path)
  9134. "Open file at PATH in Emacs."
  9135. (org-open-file path 'emacs))
  9136. (defun org-remove-file-link-modifiers ()
  9137. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  9138. (goto-char (point-min))
  9139. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  9140. (org-if-unprotected
  9141. (replace-match "file:" t t))))
  9142. (eval-after-load "org-exp"
  9143. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  9144. 'org-remove-file-link-modifiers))
  9145. ;;;; Time estimates
  9146. (defun org-get-effort (&optional pom)
  9147. "Get the effort estimate for the current entry."
  9148. (org-entry-get pom org-effort-property))
  9149. ;;; File search
  9150. (defvar org-create-file-search-functions nil
  9151. "List of functions to construct the right search string for a file link.
  9152. These functions are called in turn with point at the location to
  9153. which the link should point.
  9154. A function in the hook should first test if it would like to
  9155. handle this file type, for example by checking the `major-mode'
  9156. or the file extension. If it decides not to handle this file, it
  9157. should just return nil to give other functions a chance. If it
  9158. does handle the file, it must return the search string to be used
  9159. when following the link. The search string will be part of the
  9160. file link, given after a double colon, and `org-open-at-point'
  9161. will automatically search for it. If special measures must be
  9162. taken to make the search successful, another function should be
  9163. added to the companion hook `org-execute-file-search-functions',
  9164. which see.
  9165. A function in this hook may also use `setq' to set the variable
  9166. `description' to provide a suggestion for the descriptive text to
  9167. be used for this link when it gets inserted into an Org-mode
  9168. buffer with \\[org-insert-link].")
  9169. (defvar org-execute-file-search-functions nil
  9170. "List of functions to execute a file search triggered by a link.
  9171. Functions added to this hook must accept a single argument, the
  9172. search string that was part of the file link, the part after the
  9173. double colon. The function must first check if it would like to
  9174. handle this search, for example by checking the `major-mode' or
  9175. the file extension. If it decides not to handle this search, it
  9176. should just return nil to give other functions a chance. If it
  9177. does handle the search, it must return a non-nil value to keep
  9178. other functions from trying.
  9179. Each function can access the current prefix argument through the
  9180. variable `current-prefix-argument'. Note that a single prefix is
  9181. used to force opening a link in Emacs, so it may be good to only
  9182. use a numeric or double prefix to guide the search function.
  9183. In case this is needed, a function in this hook can also restore
  9184. the window configuration before `org-open-at-point' was called using:
  9185. (set-window-configuration org-window-config-before-follow-link)")
  9186. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9187. (defun org-link-search (s &optional type avoid-pos stealth)
  9188. "Search for a link search option.
  9189. If S is surrounded by forward slashes, it is interpreted as a
  9190. regular expression. In org-mode files, this will create an `org-occur'
  9191. sparse tree. In ordinary files, `occur' will be used to list matches.
  9192. If the current buffer is in `dired-mode', grep will be used to search
  9193. in all files. If AVOID-POS is given, ignore matches near that position.
  9194. When optional argument STEALTH is non-nil, do not modify
  9195. visibility around point, thus ignoring
  9196. `org-show-hierarchy-above', `org-show-following-heading' and
  9197. `org-show-siblings' variables."
  9198. (let ((case-fold-search t)
  9199. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9200. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9201. (append '(("") (" ") ("\t") ("\n"))
  9202. org-emphasis-alist)
  9203. "\\|") "\\)"))
  9204. (pos (point))
  9205. (pre nil) (post nil)
  9206. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9207. (cond
  9208. ;; First check if there are any special search functions
  9209. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9210. ;; Now try the builtin stuff
  9211. ((and (equal (string-to-char s0) ?#)
  9212. (> (length s0) 1)
  9213. (save-excursion
  9214. (goto-char (point-min))
  9215. (and
  9216. (re-search-forward
  9217. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9218. (setq type 'dedicated
  9219. pos (match-beginning 0))))
  9220. ;; There is an exact target for this
  9221. (goto-char pos)
  9222. (org-back-to-heading t)))
  9223. ((save-excursion
  9224. (goto-char (point-min))
  9225. (and
  9226. (re-search-forward
  9227. (concat "<<" (regexp-quote s0) ">>") nil t)
  9228. (setq type 'dedicated
  9229. pos (match-beginning 0))))
  9230. ;; There is an exact target for this
  9231. (goto-char pos))
  9232. ((save-excursion
  9233. (goto-char (point-min))
  9234. (and
  9235. (re-search-forward
  9236. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9237. (setq type 'dedicated pos (match-beginning 0))))
  9238. ;; Found an invisible target.
  9239. (goto-char pos))
  9240. ((save-excursion
  9241. (goto-char (point-min))
  9242. (and
  9243. (re-search-forward
  9244. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9245. (setq type 'dedicated pos (match-beginning 0))))
  9246. ;; Found an element with a matching #+name affiliated keyword.
  9247. (goto-char pos))
  9248. ((and (string-match "^(\\(.*\\))$" s0)
  9249. (save-excursion
  9250. (goto-char (point-min))
  9251. (and
  9252. (re-search-forward
  9253. (concat "[^[]" (regexp-quote
  9254. (format org-coderef-label-format
  9255. (match-string 1 s0))))
  9256. nil t)
  9257. (setq type 'dedicated
  9258. pos (1+ (match-beginning 0))))))
  9259. ;; There is a coderef target for this
  9260. (goto-char pos))
  9261. ((string-match "^/\\(.*\\)/$" s)
  9262. ;; A regular expression
  9263. (cond
  9264. ((derived-mode-p 'org-mode)
  9265. (org-occur (match-string 1 s)))
  9266. ;;((eq major-mode 'dired-mode)
  9267. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9268. (t (org-do-occur (match-string 1 s)))))
  9269. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9270. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9271. (goto-char (point-min))
  9272. (cond
  9273. ((let (case-fold-search)
  9274. (re-search-forward (format org-complex-heading-regexp-format
  9275. (regexp-quote s))
  9276. nil t))
  9277. ;; OK, found a match
  9278. (setq type 'dedicated)
  9279. (goto-char (match-beginning 0)))
  9280. ((and (not org-link-search-inhibit-query)
  9281. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9282. (y-or-n-p "No match - create this as a new heading? "))
  9283. (goto-char (point-max))
  9284. (or (bolp) (newline))
  9285. (insert "* " s "\n")
  9286. (beginning-of-line 0))
  9287. (t
  9288. (goto-char pos)
  9289. (error "No match"))))
  9290. (t
  9291. ;; A normal search string
  9292. (when (equal (string-to-char s) ?*)
  9293. ;; Anchor on headlines, post may include tags.
  9294. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9295. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9296. s (substring s 1)))
  9297. (remove-text-properties
  9298. 0 (length s)
  9299. '(face nil mouse-face nil keymap nil fontified nil) s)
  9300. ;; Make a series of regular expressions to find a match
  9301. (setq words (org-split-string s "[ \n\r\t]+")
  9302. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9303. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9304. "\\)" markers)
  9305. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9306. re2a (concat "[ \t\r\n]" re2a_)
  9307. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9308. re4 (concat "[^a-zA-Z_]" re4_)
  9309. re1 (concat pre re2 post)
  9310. re3 (concat pre (if pre re4_ re4) post)
  9311. re5 (concat pre ".*" re4)
  9312. re2 (concat pre re2)
  9313. re2a (concat pre (if pre re2a_ re2a))
  9314. re4 (concat pre (if pre re4_ re4))
  9315. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9316. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9317. re5 "\\)"
  9318. ))
  9319. (cond
  9320. ((eq type 'org-occur) (org-occur reall))
  9321. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9322. (t (goto-char (point-min))
  9323. (setq type 'fuzzy)
  9324. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9325. (org-search-not-self 1 re1 nil t)
  9326. (org-search-not-self 1 re2 nil t)
  9327. (org-search-not-self 1 re2a nil t)
  9328. (org-search-not-self 1 re3 nil t)
  9329. (org-search-not-self 1 re4 nil t)
  9330. (org-search-not-self 1 re5 nil t)
  9331. )
  9332. (goto-char (match-beginning 1))
  9333. (goto-char pos)
  9334. (error "No match"))))))
  9335. (and (derived-mode-p 'org-mode)
  9336. (not stealth)
  9337. (org-show-context 'link-search))
  9338. type))
  9339. (defun org-search-not-self (group &rest args)
  9340. "Execute `re-search-forward', but only accept matches that do not
  9341. enclose the position of `org-open-link-marker'."
  9342. (let ((m org-open-link-marker))
  9343. (catch 'exit
  9344. (while (apply 're-search-forward args)
  9345. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9346. (goto-char (match-end group))
  9347. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9348. (> (match-beginning 0) (marker-position m))
  9349. (< (match-end 0) (marker-position m)))
  9350. (save-match-data
  9351. (or (not (org-in-regexp
  9352. org-bracket-link-analytic-regexp 1))
  9353. (not (match-end 4)) ; no description
  9354. (and (<= (match-beginning 4) (point))
  9355. (>= (match-end 4) (point))))))
  9356. (throw 'exit (point))))))))
  9357. (defun org-get-buffer-for-internal-link (buffer)
  9358. "Return a buffer to be used for displaying the link target of internal links."
  9359. (cond
  9360. ((not org-display-internal-link-with-indirect-buffer)
  9361. buffer)
  9362. ((string-match "(Clone)$" (buffer-name buffer))
  9363. (message "Buffer is already a clone, not making another one")
  9364. ;; we also do not modify visibility in this case
  9365. buffer)
  9366. (t ; make a new indirect buffer for displaying the link
  9367. (let* ((bn (buffer-name buffer))
  9368. (ibn (concat bn "(Clone)"))
  9369. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9370. (with-current-buffer ib (org-overview))
  9371. ib))))
  9372. (defun org-do-occur (regexp &optional cleanup)
  9373. "Call the Emacs command `occur'.
  9374. If CLEANUP is non-nil, remove the printout of the regular expression
  9375. in the *Occur* buffer. This is useful if the regex is long and not useful
  9376. to read."
  9377. (occur regexp)
  9378. (when cleanup
  9379. (let ((cwin (selected-window)) win beg end)
  9380. (when (setq win (get-buffer-window "*Occur*"))
  9381. (select-window win))
  9382. (goto-char (point-min))
  9383. (when (re-search-forward "match[a-z]+" nil t)
  9384. (setq beg (match-end 0))
  9385. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9386. (setq end (1- (match-beginning 0)))))
  9387. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9388. (goto-char (point-min))
  9389. (select-window cwin))))
  9390. ;;; The mark ring for links jumps
  9391. (defvar org-mark-ring nil
  9392. "Mark ring for positions before jumps in Org-mode.")
  9393. (defvar org-mark-ring-last-goto nil
  9394. "Last position in the mark ring used to go back.")
  9395. ;; Fill and close the ring
  9396. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9397. (loop for i from 1 to org-mark-ring-length do
  9398. (push (make-marker) org-mark-ring))
  9399. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9400. org-mark-ring)
  9401. (defun org-mark-ring-push (&optional pos buffer)
  9402. "Put the current position or POS into the mark ring and rotate it."
  9403. (interactive)
  9404. (setq pos (or pos (point)))
  9405. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9406. (move-marker (car org-mark-ring)
  9407. (or pos (point))
  9408. (or buffer (current-buffer)))
  9409. (message "%s"
  9410. (substitute-command-keys
  9411. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9412. (defun org-mark-ring-goto (&optional n)
  9413. "Jump to the previous position in the mark ring.
  9414. With prefix arg N, jump back that many stored positions. When
  9415. called several times in succession, walk through the entire ring.
  9416. Org-mode commands jumping to a different position in the current file,
  9417. or to another Org-mode file, automatically push the old position
  9418. onto the ring."
  9419. (interactive "p")
  9420. (let (p m)
  9421. (if (eq last-command this-command)
  9422. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9423. (setq p org-mark-ring))
  9424. (setq org-mark-ring-last-goto p)
  9425. (setq m (car p))
  9426. (org-pop-to-buffer-same-window (marker-buffer m))
  9427. (goto-char m)
  9428. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9429. (defun org-remove-angle-brackets (s)
  9430. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9431. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9432. s)
  9433. (defun org-add-angle-brackets (s)
  9434. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9435. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9436. s)
  9437. (defun org-remove-double-quotes (s)
  9438. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9439. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9440. s)
  9441. ;;; Following specific links
  9442. (defun org-follow-timestamp-link ()
  9443. "Open an agenda view for the time-stamp date/range at point."
  9444. (cond
  9445. ((org-at-date-range-p t)
  9446. (let ((org-agenda-start-on-weekday)
  9447. (t1 (match-string 1))
  9448. (t2 (match-string 2)) tt1 tt2)
  9449. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9450. tt2 (time-to-days (org-time-string-to-time t2)))
  9451. (let ((org-agenda-buffer-tmp-name
  9452. (format "*Org Agenda(a:%s)"
  9453. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9454. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9455. ((org-at-timestamp-p t)
  9456. (let ((org-agenda-buffer-tmp-name
  9457. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9458. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9459. (substring (match-string 1) 0 10)))
  9460. 1)))
  9461. (t (error "This should not happen"))))
  9462. ;;; Following file links
  9463. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9464. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9465. (declare-function mailcap-mime-info
  9466. "mailcap" (string &optional request no-decode))
  9467. (defvar org-wait nil)
  9468. (defun org-open-file (path &optional in-emacs line search)
  9469. "Open the file at PATH.
  9470. First, this expands any special file name abbreviations. Then the
  9471. configuration variable `org-file-apps' is checked if it contains an
  9472. entry for this file type, and if yes, the corresponding command is launched.
  9473. If no application is found, Emacs simply visits the file.
  9474. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9475. With a double \\[universal-argument] \\[universal-argument] \
  9476. prefix arg, Org tries to avoid opening in Emacs
  9477. and to use an external application to visit the file.
  9478. Optional LINE specifies a line to go to, optional SEARCH a string
  9479. to search for. If LINE or SEARCH is given, the file will be
  9480. opened in Emacs, unless an entry from org-file-apps that makes
  9481. use of groups in a regexp matches.
  9482. If you want to change the way frames are used when following a
  9483. link, please customize `org-link-frame-setup'.
  9484. If the file does not exist, an error is thrown."
  9485. (let* ((file (if (equal path "")
  9486. buffer-file-name
  9487. (substitute-in-file-name (expand-file-name path))))
  9488. (file-apps (append org-file-apps (org-default-apps)))
  9489. (apps (org-remove-if
  9490. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9491. (apps-dlink (org-remove-if-not
  9492. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9493. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9494. (dirp (if remp nil (file-directory-p file)))
  9495. (file (if (and dirp org-open-directory-means-index-dot-org)
  9496. (concat (file-name-as-directory file) "index.org")
  9497. file))
  9498. (a-m-a-p (assq 'auto-mode apps))
  9499. (dfile (downcase file))
  9500. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9501. (link (cond ((and (eq line nil)
  9502. (eq search nil))
  9503. file)
  9504. (line
  9505. (concat file "::" (number-to-string line)))
  9506. (search
  9507. (concat file "::" search))))
  9508. (dlink (downcase link))
  9509. (old-buffer (current-buffer))
  9510. (old-pos (point))
  9511. (old-mode major-mode)
  9512. ext cmd link-match-data)
  9513. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9514. (setq ext (match-string 1 dfile))
  9515. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9516. (setq ext (match-string 1 dfile))))
  9517. (cond
  9518. ((member in-emacs '((16) system))
  9519. (setq cmd (cdr (assoc 'system apps))))
  9520. (in-emacs (setq cmd 'emacs))
  9521. (t
  9522. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9523. (and dirp (cdr (assoc 'directory apps)))
  9524. ; first, try matching against apps-dlink
  9525. ; if we get a match here, store the match data for later
  9526. (let ((match (assoc-default dlink apps-dlink
  9527. 'string-match)))
  9528. (if match
  9529. (progn (setq link-match-data (match-data))
  9530. match)
  9531. (progn (setq in-emacs (or in-emacs line search))
  9532. nil))) ; if we have no match in apps-dlink,
  9533. ; always open the file in emacs if line or search
  9534. ; is given (for backwards compatibility)
  9535. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9536. 'string-match)
  9537. (cdr (assoc ext apps))
  9538. (cdr (assoc t apps))))))
  9539. (when (eq cmd 'system)
  9540. (setq cmd (cdr (assoc 'system apps))))
  9541. (when (eq cmd 'default)
  9542. (setq cmd (cdr (assoc t apps))))
  9543. (when (eq cmd 'mailcap)
  9544. (require 'mailcap)
  9545. (mailcap-parse-mailcaps)
  9546. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9547. (command (mailcap-mime-info mime-type)))
  9548. (if (stringp command)
  9549. (setq cmd command)
  9550. (setq cmd 'emacs))))
  9551. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9552. (not (file-exists-p file))
  9553. (not org-open-non-existing-files))
  9554. (error "No such file: %s" file))
  9555. (cond
  9556. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9557. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9558. (while (string-match "['\"]%s['\"]" cmd)
  9559. (setq cmd (replace-match "%s" t t cmd)))
  9560. (while (string-match "%s" cmd)
  9561. (setq cmd (replace-match
  9562. (save-match-data
  9563. (shell-quote-argument
  9564. (convert-standard-filename file)))
  9565. t t cmd)))
  9566. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9567. (save-match-data
  9568. (let ((match-index 1)
  9569. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9570. (set-match-data link-match-data)
  9571. (while (<= match-index number-of-groups)
  9572. (let ((regex (concat "%" (number-to-string match-index)))
  9573. (replace-with (match-string match-index dlink)))
  9574. (while (string-match regex cmd)
  9575. (setq cmd (replace-match replace-with t t cmd))))
  9576. (setq match-index (+ match-index 1)))))
  9577. (save-window-excursion
  9578. (message "Running %s...done" cmd)
  9579. (start-process-shell-command cmd nil cmd)
  9580. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9581. ((or (stringp cmd)
  9582. (eq cmd 'emacs))
  9583. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9584. (widen)
  9585. (if line (org-goto-line line)
  9586. (if search (org-link-search search))))
  9587. ((consp cmd)
  9588. (let ((file (convert-standard-filename file)))
  9589. (save-match-data
  9590. (set-match-data link-match-data)
  9591. (eval cmd))))
  9592. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9593. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9594. (or (not (equal old-buffer (current-buffer)))
  9595. (not (equal old-pos (point))))
  9596. (org-mark-ring-push old-pos old-buffer))))
  9597. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9598. "This function returns non-nil if `entry' uses a regular
  9599. expression which should be matched against the whole link by
  9600. org-open-file.
  9601. It assumes that is the case when the entry uses a regular
  9602. expression which has at least one grouping construct and the
  9603. action is either a lisp form or a command string containing
  9604. '%1', i.e. using at least one subexpression match as a
  9605. parameter."
  9606. (let ((selector (car entry))
  9607. (action (cdr entry)))
  9608. (if (stringp selector)
  9609. (and (> (regexp-opt-depth selector) 0)
  9610. (or (and (stringp action)
  9611. (string-match "%[0-9]" action))
  9612. (consp action)))
  9613. nil)))
  9614. (defun org-default-apps ()
  9615. "Return the default applications for this operating system."
  9616. (cond
  9617. ((eq system-type 'darwin)
  9618. org-file-apps-defaults-macosx)
  9619. ((eq system-type 'windows-nt)
  9620. org-file-apps-defaults-windowsnt)
  9621. (t org-file-apps-defaults-gnu)))
  9622. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9623. "Convert extensions to regular expressions in the cars of LIST.
  9624. Also, weed out any non-string entries, because the return value is used
  9625. only for regexp matching.
  9626. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9627. point to the symbol `emacs', indicating that the file should
  9628. be opened in Emacs."
  9629. (append
  9630. (delq nil
  9631. (mapcar (lambda (x)
  9632. (if (not (stringp (car x)))
  9633. nil
  9634. (if (string-match "\\W" (car x))
  9635. x
  9636. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9637. list))
  9638. (if add-auto-mode
  9639. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9640. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9641. (defun org-file-remote-p (file)
  9642. "Test whether FILE specifies a location on a remote system.
  9643. Return non-nil if the location is indeed remote.
  9644. For example, the filename \"/user@host:/foo\" specifies a location
  9645. on the system \"/user@host:\"."
  9646. (cond ((fboundp 'file-remote-p)
  9647. (file-remote-p file))
  9648. ((fboundp 'tramp-handle-file-remote-p)
  9649. (tramp-handle-file-remote-p file))
  9650. ((and (boundp 'ange-ftp-name-format)
  9651. (string-match (car ange-ftp-name-format) file))
  9652. t)))
  9653. ;;;; Refiling
  9654. (defun org-get-org-file ()
  9655. "Read a filename, with default directory `org-directory'."
  9656. (let ((default (or org-default-notes-file remember-data-file)))
  9657. (read-file-name (format "File name [%s]: " default)
  9658. (file-name-as-directory org-directory)
  9659. default)))
  9660. (defun org-notes-order-reversed-p ()
  9661. "Check if the current file should receive notes in reversed order."
  9662. (cond
  9663. ((not org-reverse-note-order) nil)
  9664. ((eq t org-reverse-note-order) t)
  9665. ((not (listp org-reverse-note-order)) nil)
  9666. (t (catch 'exit
  9667. (let ((all org-reverse-note-order)
  9668. entry)
  9669. (while (setq entry (pop all))
  9670. (if (string-match (car entry) buffer-file-name)
  9671. (throw 'exit (cdr entry))))
  9672. nil)))))
  9673. (defvar org-refile-target-table nil
  9674. "The list of refile targets, created by `org-refile'.")
  9675. (defvar org-agenda-new-buffers nil
  9676. "Buffers created to visit agenda files.")
  9677. (defvar org-refile-cache nil
  9678. "Cache for refile targets.")
  9679. (defvar org-refile-markers nil
  9680. "All the markers used for caching refile locations.")
  9681. (defun org-refile-marker (pos)
  9682. "Get a new refile marker, but only if caching is in use."
  9683. (if (not org-refile-use-cache)
  9684. pos
  9685. (let ((m (make-marker)))
  9686. (move-marker m pos)
  9687. (push m org-refile-markers)
  9688. m)))
  9689. (defun org-refile-cache-clear ()
  9690. "Clear the refile cache and disable all the markers."
  9691. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9692. (setq org-refile-markers nil)
  9693. (setq org-refile-cache nil)
  9694. (message "Refile cache has been cleared"))
  9695. (defun org-refile-cache-check-set (set)
  9696. "Check if all the markers in the cache still have live buffers."
  9697. (let (marker)
  9698. (catch 'exit
  9699. (while (and set (setq marker (nth 3 (pop set))))
  9700. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9701. (when (and marker (null (marker-buffer marker)))
  9702. (message "not found") (sit-for 3)
  9703. (throw 'exit nil)))
  9704. t)))
  9705. (defun org-refile-cache-put (set &rest identifiers)
  9706. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9707. (let* ((key (sha1 (prin1-to-string identifiers)))
  9708. (entry (assoc key org-refile-cache)))
  9709. (if entry
  9710. (setcdr entry set)
  9711. (push (cons key set) org-refile-cache))))
  9712. (defun org-refile-cache-get (&rest identifiers)
  9713. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9714. (cond
  9715. ((not org-refile-cache) nil)
  9716. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9717. (t
  9718. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9719. org-refile-cache))))
  9720. (and set (org-refile-cache-check-set set) set)))))
  9721. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9722. "Produce a table with refile targets."
  9723. (let ((case-fold-search nil)
  9724. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9725. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9726. targets tgs txt re files f desc descre fast-path-p level pos0)
  9727. (message "Getting targets...")
  9728. (with-current-buffer (or default-buffer (current-buffer))
  9729. (while (setq entry (pop entries))
  9730. (setq files (car entry) desc (cdr entry))
  9731. (setq fast-path-p nil)
  9732. (cond
  9733. ((null files) (setq files (list (current-buffer))))
  9734. ((eq files 'org-agenda-files)
  9735. (setq files (org-agenda-files 'unrestricted)))
  9736. ((and (symbolp files) (fboundp files))
  9737. (setq files (funcall files)))
  9738. ((and (symbolp files) (boundp files))
  9739. (setq files (symbol-value files))))
  9740. (if (stringp files) (setq files (list files)))
  9741. (cond
  9742. ((eq (car desc) :tag)
  9743. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9744. ((eq (car desc) :todo)
  9745. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9746. ((eq (car desc) :regexp)
  9747. (setq descre (cdr desc)))
  9748. ((eq (car desc) :level)
  9749. (setq descre (concat "^\\*\\{" (number-to-string
  9750. (if org-odd-levels-only
  9751. (1- (* 2 (cdr desc)))
  9752. (cdr desc)))
  9753. "\\}[ \t]")))
  9754. ((eq (car desc) :maxlevel)
  9755. (setq fast-path-p t)
  9756. (setq descre (concat "^\\*\\{1," (number-to-string
  9757. (if org-odd-levels-only
  9758. (1- (* 2 (cdr desc)))
  9759. (cdr desc)))
  9760. "\\}[ \t]")))
  9761. (t (error "Bad refiling target description %s" desc)))
  9762. (while (setq f (pop files))
  9763. (with-current-buffer
  9764. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9765. (or
  9766. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9767. (progn
  9768. (if (bufferp f) (setq f (buffer-file-name
  9769. (buffer-base-buffer f))))
  9770. (setq f (and f (expand-file-name f)))
  9771. (if (eq org-refile-use-outline-path 'file)
  9772. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9773. (save-excursion
  9774. (save-restriction
  9775. (widen)
  9776. (goto-char (point-min))
  9777. (while (re-search-forward descre nil t)
  9778. (goto-char (setq pos0 (point-at-bol)))
  9779. (catch 'next
  9780. (when org-refile-target-verify-function
  9781. (save-match-data
  9782. (or (funcall org-refile-target-verify-function)
  9783. (throw 'next t))))
  9784. (when (and (looking-at org-complex-heading-regexp)
  9785. (not (member (match-string 4) excluded-entries))
  9786. (match-string 4))
  9787. (setq level (org-reduced-level
  9788. (- (match-end 1) (match-beginning 1)))
  9789. txt (org-link-display-format (match-string 4))
  9790. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9791. re (format org-complex-heading-regexp-format
  9792. (regexp-quote (match-string 4))))
  9793. (when org-refile-use-outline-path
  9794. (setq txt (mapconcat
  9795. 'org-protect-slash
  9796. (append
  9797. (if (eq org-refile-use-outline-path
  9798. 'file)
  9799. (list (file-name-nondirectory
  9800. (buffer-file-name
  9801. (buffer-base-buffer))))
  9802. (if (eq org-refile-use-outline-path
  9803. 'full-file-path)
  9804. (list (buffer-file-name
  9805. (buffer-base-buffer)))))
  9806. (org-get-outline-path fast-path-p
  9807. level txt)
  9808. (list txt))
  9809. "/")))
  9810. (push (list txt f re (org-refile-marker (point)))
  9811. tgs)))
  9812. (when (= (point) pos0)
  9813. ;; verification function has not moved point
  9814. (goto-char (point-at-eol))))))))
  9815. (when org-refile-use-cache
  9816. (org-refile-cache-put tgs (buffer-file-name) descre))
  9817. (setq targets (append tgs targets))
  9818. ))))
  9819. (message "Getting targets...done")
  9820. (nreverse targets)))
  9821. (defun org-protect-slash (s)
  9822. (while (string-match "/" s)
  9823. (setq s (replace-match "\\" t t s)))
  9824. s)
  9825. (defvar org-olpa (make-vector 20 nil))
  9826. (defun org-get-outline-path (&optional fastp level heading)
  9827. "Return the outline path to the current entry, as a list.
  9828. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9829. routine which makes outline path derivations for an entire file,
  9830. avoiding backtracing. Refile target collection makes use of that."
  9831. (if fastp
  9832. (progn
  9833. (if (> level 19)
  9834. (error "Outline path failure, more than 19 levels"))
  9835. (loop for i from level upto 19 do
  9836. (aset org-olpa i nil))
  9837. (prog1
  9838. (delq nil (append org-olpa nil))
  9839. (aset org-olpa level heading)))
  9840. (let (rtn case-fold-search)
  9841. (save-excursion
  9842. (save-restriction
  9843. (widen)
  9844. (while (org-up-heading-safe)
  9845. (when (looking-at org-complex-heading-regexp)
  9846. (push (org-match-string-no-properties 4) rtn)))
  9847. rtn)))))
  9848. (defun org-format-outline-path (path &optional width prefix separator)
  9849. "Format the outline path PATH for display.
  9850. WIDTH is the maximum number of characters that is available.
  9851. PREFIX is a prefix to be included in the returned string,
  9852. such as the file name.
  9853. SEPARATOR is inserted between the different parts of the path,
  9854. the default is \"/\"."
  9855. (setq width (or width 79))
  9856. (if prefix (setq width (- width (length prefix))))
  9857. (if (not path)
  9858. (or prefix "")
  9859. (let* ((nsteps (length path))
  9860. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9861. (maxwidth (if (<= total-width width)
  9862. 10000 ;; everything fits
  9863. ;; we need to shorten the level headings
  9864. (/ (- width nsteps) nsteps)))
  9865. (org-odd-levels-only nil)
  9866. (n 0)
  9867. (total (1+ (length prefix))))
  9868. (setq maxwidth (max maxwidth 10))
  9869. (concat prefix
  9870. (if prefix (or separator "/"))
  9871. (mapconcat
  9872. (lambda (h)
  9873. (setq n (1+ n))
  9874. (if (and (= n nsteps) (< maxwidth 10000))
  9875. (setq maxwidth (- total-width total)))
  9876. (if (< (length h) maxwidth)
  9877. (progn (setq total (+ total (length h) 1)) h)
  9878. (setq h (substring h 0 (- maxwidth 2))
  9879. total (+ total maxwidth 1))
  9880. (if (string-match "[ \t]+\\'" h)
  9881. (setq h (substring h 0 (match-beginning 0))))
  9882. (setq h (concat h "..")))
  9883. (org-add-props h nil 'face
  9884. (nth (% (1- n) org-n-level-faces)
  9885. org-level-faces))
  9886. h)
  9887. path (or separator "/"))))))
  9888. (defun org-display-outline-path (&optional file current separator just-return-string)
  9889. "Display the current outline path in the echo area.
  9890. If FILE is non-nil, prepend the output with the file name.
  9891. If CURRENT is non-nil, append the current heading to the output.
  9892. SEPARATOR is passed through to `org-format-outline-path'. It separates
  9893. the different parts of the path and defaults to \"/\".
  9894. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  9895. (interactive "P")
  9896. (let* (case-fold-search
  9897. message-log-max ; Don't populate the *Messages* buffer
  9898. (bfn (buffer-file-name (buffer-base-buffer)))
  9899. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  9900. res)
  9901. (if current (setq path (append path
  9902. (save-excursion
  9903. (org-back-to-heading t)
  9904. (if (looking-at org-complex-heading-regexp)
  9905. (list (match-string 4)))))))
  9906. (setq res
  9907. (org-format-outline-path
  9908. path
  9909. (1- (frame-width))
  9910. (and file bfn (concat (file-name-nondirectory bfn) separator))
  9911. separator))
  9912. (if just-return-string
  9913. (org-no-properties res)
  9914. (message "%s" res))))
  9915. (defvar org-refile-history nil
  9916. "History for refiling operations.")
  9917. (defvar org-after-refile-insert-hook nil
  9918. "Hook run after `org-refile' has inserted its stuff at the new location.
  9919. Note that this is still *before* the stuff will be removed from
  9920. the *old* location.")
  9921. (defvar org-capture-last-stored-marker)
  9922. (defvar org-refile-keep nil
  9923. "Non-nil means `org-refile' will copy instead of refile.")
  9924. (defun org-copy ()
  9925. "Like `org-refile', but copy."
  9926. (interactive)
  9927. (let ((org-refile-keep t))
  9928. (funcall 'org-refile nil nil nil "Copy")))
  9929. (defun org-refile (&optional goto default-buffer rfloc msg)
  9930. "Move the entry or entries at point to another heading.
  9931. The list of target headings is compiled using the information in
  9932. `org-refile-targets', which see.
  9933. At the target location, the entry is filed as a subitem of the target
  9934. heading. Depending on `org-reverse-note-order', the new subitem will
  9935. either be the first or the last subitem.
  9936. If there is an active region, all entries in that region will be moved.
  9937. However, the region must fulfill the requirement that the first heading
  9938. is the first one sets the top-level of the moved text - at most siblings
  9939. below it are allowed.
  9940. With prefix arg GOTO, the command will only visit the target location
  9941. and not actually move anything.
  9942. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9943. go to the location where the last refiling operation has put the subtree.
  9944. With a prefix argument of `2', refile to the running clock.
  9945. RFLOC can be a refile location obtained in a different way.
  9946. MSG is a string to replace \"Refile\" in the default prompt with
  9947. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  9948. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9949. If you are using target caching (see `org-refile-use-cache'),
  9950. you have to clear the target cache in order to find new targets.
  9951. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9952. prefix argument (`C-u C-u C-u C-c C-w')."
  9953. (interactive "P")
  9954. (if (member goto '(0 (64)))
  9955. (org-refile-cache-clear)
  9956. (let* ((actionmsg (or msg "Refile"))
  9957. (cbuf (current-buffer))
  9958. (regionp (org-region-active-p))
  9959. (region-start (and regionp (region-beginning)))
  9960. (region-end (and regionp (region-end)))
  9961. (region-length (and regionp (- region-end region-start)))
  9962. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9963. pos it nbuf file re level reversed)
  9964. (setq last-command nil)
  9965. (when regionp
  9966. (goto-char region-start)
  9967. (or (bolp) (goto-char (point-at-bol)))
  9968. (setq region-start (point))
  9969. (unless (or (org-kill-is-subtree-p
  9970. (buffer-substring region-start region-end))
  9971. (prog1 org-refile-active-region-within-subtree
  9972. (org-toggle-heading)))
  9973. (error "The region is not a (sequence of) subtree(s)")))
  9974. (if (equal goto '(16))
  9975. (org-refile-goto-last-stored)
  9976. (when (or
  9977. (and (equal goto 2)
  9978. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9979. (prog1
  9980. (setq it (list (or org-clock-heading "running clock")
  9981. (buffer-file-name
  9982. (marker-buffer org-clock-hd-marker))
  9983. ""
  9984. (marker-position org-clock-hd-marker)))
  9985. (setq goto nil)))
  9986. (setq it (or rfloc
  9987. (let (heading-text)
  9988. (save-excursion
  9989. (unless goto
  9990. (org-back-to-heading t)
  9991. (setq heading-text
  9992. (nth 4 (org-heading-components))))
  9993. (org-refile-get-location
  9994. (cond (goto "Goto")
  9995. (regionp (concat actionmsg " region to"))
  9996. (t (concat actionmsg " subtree \""
  9997. heading-text "\" to")))
  9998. default-buffer
  9999. (and (not (equal '(4) goto))
  10000. org-refile-allow-creating-parent-nodes)
  10001. goto))))))
  10002. (setq file (nth 1 it)
  10003. re (nth 2 it)
  10004. pos (nth 3 it))
  10005. (if (and (not goto)
  10006. pos
  10007. (equal (buffer-file-name) file)
  10008. (if regionp
  10009. (and (>= pos region-start)
  10010. (<= pos region-end))
  10011. (and (>= pos (point))
  10012. (< pos (save-excursion
  10013. (org-end-of-subtree t t))))))
  10014. (error "Cannot refile to position inside the tree or region"))
  10015. (setq nbuf (or (find-buffer-visiting file)
  10016. (find-file-noselect file)))
  10017. (if goto
  10018. (progn
  10019. (org-pop-to-buffer-same-window nbuf)
  10020. (goto-char pos)
  10021. (org-show-context 'org-goto))
  10022. (if regionp
  10023. (progn
  10024. (org-kill-new (buffer-substring region-start region-end))
  10025. (org-save-markers-in-region region-start region-end))
  10026. (org-copy-subtree 1 nil t))
  10027. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10028. (find-file-noselect file)))
  10029. (setq reversed (org-notes-order-reversed-p))
  10030. (save-excursion
  10031. (save-restriction
  10032. (widen)
  10033. (if pos
  10034. (progn
  10035. (goto-char pos)
  10036. (looking-at org-outline-regexp)
  10037. (setq level (org-get-valid-level (funcall outline-level) 1))
  10038. (goto-char
  10039. (if reversed
  10040. (or (outline-next-heading) (point-max))
  10041. (or (save-excursion (org-get-next-sibling))
  10042. (org-end-of-subtree t t)
  10043. (point-max)))))
  10044. (setq level 1)
  10045. (if (not reversed)
  10046. (goto-char (point-max))
  10047. (goto-char (point-min))
  10048. (or (outline-next-heading) (goto-char (point-max)))))
  10049. (if (not (bolp)) (newline))
  10050. (org-paste-subtree level)
  10051. (when org-log-refile
  10052. (org-add-log-setup 'refile nil nil 'findpos
  10053. org-log-refile)
  10054. (unless (eq org-log-refile 'note)
  10055. (save-excursion (org-add-log-note))))
  10056. (and org-auto-align-tags
  10057. (let ((org-loop-over-headlines-in-active-region nil))
  10058. (org-set-tags nil t)))
  10059. (with-demoted-errors
  10060. (bookmark-set "org-refile-last-stored"))
  10061. ;; If we are refiling for capture, make sure that the
  10062. ;; last-capture pointers point here
  10063. (when (org-bound-and-true-p org-refile-for-capture)
  10064. (with-demoted-errors
  10065. (bookmark-set "org-capture-last-stored-marker"))
  10066. (move-marker org-capture-last-stored-marker (point)))
  10067. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10068. (run-hooks 'org-after-refile-insert-hook))))
  10069. (unless org-refile-keep
  10070. (if regionp
  10071. (delete-region (point) (+ (point) region-length))
  10072. (org-cut-subtree)))
  10073. (when (featurep 'org-inlinetask)
  10074. (org-inlinetask-remove-END-maybe))
  10075. (setq org-markers-to-move nil)
  10076. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10077. (defun org-refile-goto-last-stored ()
  10078. "Go to the location where the last refile was stored."
  10079. (interactive)
  10080. (bookmark-jump "org-refile-last-stored")
  10081. (message "This is the location of the last refile"))
  10082. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10083. no-exclude)
  10084. "Prompt the user for a refile location, using PROMPT.
  10085. PROMPT should not be suffixed with a colon and a space, because
  10086. this function appends the default value from
  10087. `org-refile-history' automatically, if that is not empty.
  10088. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10089. this is used for the GOTO interface."
  10090. (let ((org-refile-targets org-refile-targets)
  10091. (org-refile-use-outline-path org-refile-use-outline-path)
  10092. excluded-entries)
  10093. (when (and (derived-mode-p 'org-mode)
  10094. (not org-refile-use-cache)
  10095. (not no-exclude))
  10096. (org-map-tree
  10097. (lambda()
  10098. (setq excluded-entries
  10099. (append excluded-entries (list (org-get-heading t t)))))))
  10100. (setq org-refile-target-table
  10101. (org-refile-get-targets default-buffer excluded-entries)))
  10102. (unless org-refile-target-table
  10103. (error "No refile targets"))
  10104. (let* ((cbuf (current-buffer))
  10105. (partial-completion-mode nil)
  10106. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10107. (cfunc (if (and org-refile-use-outline-path
  10108. org-outline-path-complete-in-steps)
  10109. 'org-olpath-completing-read
  10110. 'org-icompleting-read))
  10111. (extra (if org-refile-use-outline-path "/" ""))
  10112. (cbnex (concat (buffer-name) extra))
  10113. (filename (and cfn (expand-file-name cfn)))
  10114. (tbl (mapcar
  10115. (lambda (x)
  10116. (if (and (not (member org-refile-use-outline-path
  10117. '(file full-file-path)))
  10118. (not (equal filename (nth 1 x))))
  10119. (cons (concat (car x) extra " ("
  10120. (file-name-nondirectory (nth 1 x)) ")")
  10121. (cdr x))
  10122. (cons (concat (car x) extra) (cdr x))))
  10123. org-refile-target-table))
  10124. (completion-ignore-case t)
  10125. cdef
  10126. (prompt (concat prompt
  10127. (or (and (car org-refile-history)
  10128. (concat " (default " (car org-refile-history) ")"))
  10129. (and (assoc cbnex tbl) (setq cdef cbnex)
  10130. (concat " (default " cbnex ")"))) ": "))
  10131. pa answ parent-target child parent old-hist)
  10132. (setq old-hist org-refile-history)
  10133. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10134. nil 'org-refile-history (or cdef (car org-refile-history))))
  10135. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10136. (org-refile-check-position pa)
  10137. (if pa
  10138. (progn
  10139. (when (or (not org-refile-history)
  10140. (not (eq old-hist org-refile-history))
  10141. (not (equal (car pa) (car org-refile-history))))
  10142. (setq org-refile-history
  10143. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10144. org-refile-history
  10145. (cdr org-refile-history))))
  10146. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10147. (pop org-refile-history)))
  10148. pa)
  10149. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10150. (progn
  10151. (setq parent (match-string 1 answ)
  10152. child (match-string 2 answ))
  10153. (setq parent-target (or (assoc parent tbl)
  10154. (assoc (concat parent "/") tbl)))
  10155. (when (and parent-target
  10156. (or (eq new-nodes t)
  10157. (and (eq new-nodes 'confirm)
  10158. (y-or-n-p (format "Create new node \"%s\"? "
  10159. child)))))
  10160. (org-refile-new-child parent-target child)))
  10161. (error "Invalid target location")))))
  10162. (declare-function org-string-nw-p "org-macs" (s))
  10163. (defun org-refile-check-position (refile-pointer)
  10164. "Check if the refile pointer matches the headline to which it points."
  10165. (let* ((file (nth 1 refile-pointer))
  10166. (re (nth 2 refile-pointer))
  10167. (pos (nth 3 refile-pointer))
  10168. buffer)
  10169. (if (and (not (markerp pos)) (not file))
  10170. (error "Please save the buffer to a file before refiling")
  10171. (when (org-string-nw-p re)
  10172. (setq buffer (if (markerp pos)
  10173. (marker-buffer pos)
  10174. (or (find-buffer-visiting file)
  10175. (find-file-noselect file))))
  10176. (with-current-buffer buffer
  10177. (save-excursion
  10178. (save-restriction
  10179. (widen)
  10180. (goto-char pos)
  10181. (beginning-of-line 1)
  10182. (unless (org-looking-at-p re)
  10183. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10184. (defun org-refile-new-child (parent-target child)
  10185. "Use refile target PARENT-TARGET to add new CHILD below it."
  10186. (unless parent-target
  10187. (error "Cannot find parent for new node"))
  10188. (let ((file (nth 1 parent-target))
  10189. (pos (nth 3 parent-target))
  10190. level)
  10191. (with-current-buffer (or (find-buffer-visiting file)
  10192. (find-file-noselect file))
  10193. (save-excursion
  10194. (save-restriction
  10195. (widen)
  10196. (if pos
  10197. (goto-char pos)
  10198. (goto-char (point-max))
  10199. (if (not (bolp)) (newline)))
  10200. (when (looking-at org-outline-regexp)
  10201. (setq level (funcall outline-level))
  10202. (org-end-of-subtree t t))
  10203. (org-back-over-empty-lines)
  10204. (insert "\n" (make-string
  10205. (if pos (org-get-valid-level level 1) 1) ?*)
  10206. " " child "\n")
  10207. (beginning-of-line 0)
  10208. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10209. (defun org-olpath-completing-read (prompt collection &rest args)
  10210. "Read an outline path like a file name."
  10211. (let ((thetable collection)
  10212. (org-completion-use-ido nil) ; does not work with ido.
  10213. (org-completion-use-iswitchb nil)) ; or iswitchb
  10214. (apply
  10215. 'org-icompleting-read prompt
  10216. (lambda (string predicate &optional flag)
  10217. (let (rtn r f (l (length string)))
  10218. (cond
  10219. ((eq flag nil)
  10220. ;; try completion
  10221. (try-completion string thetable))
  10222. ((eq flag t)
  10223. ;; all-completions
  10224. (setq rtn (all-completions string thetable predicate))
  10225. (mapcar
  10226. (lambda (x)
  10227. (setq r (substring x l))
  10228. (if (string-match " ([^)]*)$" x)
  10229. (setq f (match-string 0 x))
  10230. (setq f ""))
  10231. (if (string-match "/" r)
  10232. (concat string (substring r 0 (match-end 0)) f)
  10233. x))
  10234. rtn))
  10235. ((eq flag 'lambda)
  10236. ;; exact match?
  10237. (assoc string thetable)))))
  10238. args)))
  10239. ;;;; Dynamic blocks
  10240. (defun org-find-dblock (name)
  10241. "Find the first dynamic block with name NAME in the buffer.
  10242. If not found, stay at current position and return nil."
  10243. (let ((case-fold-search t) pos)
  10244. (save-excursion
  10245. (goto-char (point-min))
  10246. (setq pos (and (re-search-forward
  10247. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10248. (match-beginning 0))))
  10249. (if pos (goto-char pos))
  10250. pos))
  10251. (defconst org-dblock-start-re
  10252. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10253. "Matches the start line of a dynamic block, with parameters.")
  10254. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10255. "Matches the end of a dynamic block.")
  10256. (defun org-create-dblock (plist)
  10257. "Create a dynamic block section, with parameters taken from PLIST.
  10258. PLIST must contain a :name entry which is used as name of the block."
  10259. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10260. (end-of-line 1)
  10261. (newline))
  10262. (let ((col (current-column))
  10263. (name (plist-get plist :name)))
  10264. (insert "#+BEGIN: " name)
  10265. (while plist
  10266. (if (eq (car plist) :name)
  10267. (setq plist (cddr plist))
  10268. (insert " " (prin1-to-string (pop plist)))))
  10269. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10270. (beginning-of-line -2)))
  10271. (defun org-prepare-dblock ()
  10272. "Prepare dynamic block for refresh.
  10273. This empties the block, puts the cursor at the insert position and returns
  10274. the property list including an extra property :name with the block name."
  10275. (unless (looking-at org-dblock-start-re)
  10276. (error "Not at a dynamic block"))
  10277. (let* ((begdel (1+ (match-end 0)))
  10278. (name (org-no-properties (match-string 1)))
  10279. (params (append (list :name name)
  10280. (read (concat "(" (match-string 3) ")")))))
  10281. (save-excursion
  10282. (beginning-of-line 1)
  10283. (skip-chars-forward " \t")
  10284. (setq params (plist-put params :indentation-column (current-column))))
  10285. (unless (re-search-forward org-dblock-end-re nil t)
  10286. (error "Dynamic block not terminated"))
  10287. (setq params
  10288. (append params
  10289. (list :content (buffer-substring
  10290. begdel (match-beginning 0)))))
  10291. (delete-region begdel (match-beginning 0))
  10292. (goto-char begdel)
  10293. (open-line 1)
  10294. params))
  10295. (defun org-map-dblocks (&optional command)
  10296. "Apply COMMAND to all dynamic blocks in the current buffer.
  10297. If COMMAND is not given, use `org-update-dblock'."
  10298. (let ((cmd (or command 'org-update-dblock)))
  10299. (save-excursion
  10300. (goto-char (point-min))
  10301. (while (re-search-forward org-dblock-start-re nil t)
  10302. (goto-char (match-beginning 0))
  10303. (save-excursion
  10304. (condition-case nil
  10305. (funcall cmd)
  10306. (error (message "Error during update of dynamic block"))))
  10307. (unless (re-search-forward org-dblock-end-re nil t)
  10308. (error "Dynamic block not terminated"))))))
  10309. (defun org-dblock-update (&optional arg)
  10310. "User command for updating dynamic blocks.
  10311. Update the dynamic block at point. With prefix ARG, update all dynamic
  10312. blocks in the buffer."
  10313. (interactive "P")
  10314. (if arg
  10315. (org-update-all-dblocks)
  10316. (or (looking-at org-dblock-start-re)
  10317. (org-beginning-of-dblock))
  10318. (org-update-dblock)))
  10319. (defun org-update-dblock ()
  10320. "Update the dynamic block at point.
  10321. This means to empty the block, parse for parameters and then call
  10322. the correct writing function."
  10323. (interactive)
  10324. (save-window-excursion
  10325. (let* ((pos (point))
  10326. (line (org-current-line))
  10327. (params (org-prepare-dblock))
  10328. (name (plist-get params :name))
  10329. (indent (plist-get params :indentation-column))
  10330. (cmd (intern (concat "org-dblock-write:" name))))
  10331. (message "Updating dynamic block `%s' at line %d..." name line)
  10332. (funcall cmd params)
  10333. (message "Updating dynamic block `%s' at line %d...done" name line)
  10334. (goto-char pos)
  10335. (when (and indent (> indent 0))
  10336. (setq indent (make-string indent ?\ ))
  10337. (save-excursion
  10338. (org-beginning-of-dblock)
  10339. (forward-line 1)
  10340. (while (not (looking-at org-dblock-end-re))
  10341. (insert indent)
  10342. (beginning-of-line 2))
  10343. (when (looking-at org-dblock-end-re)
  10344. (and (looking-at "[ \t]+")
  10345. (replace-match ""))
  10346. (insert indent)))))))
  10347. (defun org-beginning-of-dblock ()
  10348. "Find the beginning of the dynamic block at point.
  10349. Error if there is no such block at point."
  10350. (let ((pos (point))
  10351. beg)
  10352. (end-of-line 1)
  10353. (if (and (re-search-backward org-dblock-start-re nil t)
  10354. (setq beg (match-beginning 0))
  10355. (re-search-forward org-dblock-end-re nil t)
  10356. (> (match-end 0) pos))
  10357. (goto-char beg)
  10358. (goto-char pos)
  10359. (error "Not in a dynamic block"))))
  10360. (defun org-update-all-dblocks ()
  10361. "Update all dynamic blocks in the buffer.
  10362. This function can be used in a hook."
  10363. (interactive)
  10364. (when (derived-mode-p 'org-mode)
  10365. (org-map-dblocks 'org-update-dblock)))
  10366. ;;;; Completion
  10367. (defconst org-additional-option-like-keywords
  10368. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10369. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10370. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10371. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10372. "BEGIN:" "END:"
  10373. "ORGTBL" "TBLFM:" "TBLNAME:"
  10374. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10375. "BEGIN_VERBATIM" "END_VERBATIM"
  10376. "BEGIN_QUOTE" "END_QUOTE"
  10377. "BEGIN_VERSE" "END_VERSE"
  10378. "BEGIN_CENTER" "END_CENTER"
  10379. "BEGIN_SRC" "END_SRC"
  10380. "BEGIN_RESULT" "END_RESULT"
  10381. "BEGIN_lstlisting" "END_lstlisting"
  10382. "NAME:" "RESULTS:"
  10383. "HEADER:" "HEADERS:"
  10384. "COLUMNS:" "PROPERTY:"
  10385. "CAPTION:" "LABEL:"
  10386. "SETUPFILE:"
  10387. "INCLUDE:" "INDEX:"
  10388. "BIND:"
  10389. "MACRO:"))
  10390. (defconst org-options-keywords
  10391. '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
  10392. "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
  10393. "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
  10394. "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
  10395. "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
  10396. "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:" "STYLE:"
  10397. "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
  10398. (defconst org-additional-option-like-keywords-for-flyspell
  10399. (delete-dups
  10400. (split-string
  10401. (mapconcat (lambda(k)
  10402. (replace-regexp-in-string
  10403. "_\\|:" " "
  10404. (concat k " " (downcase k) " " (upcase k))))
  10405. (append org-options-keywords org-additional-option-like-keywords)
  10406. " ")
  10407. " +" t)))
  10408. (defcustom org-structure-template-alist
  10409. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10410. "<src lang=\"?\">\n\n</src>")
  10411. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10412. "<example>\n?\n</example>")
  10413. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10414. "<quote>\n?\n</quote>")
  10415. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10416. "<verse>\n?\n</verse>")
  10417. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10418. "<verbatim>\n?\n</verbatim>")
  10419. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10420. "<center>\n?\n</center>")
  10421. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10422. "<literal style=\"latex\">\n?\n</literal>")
  10423. ("L" "#+LaTeX: "
  10424. "<literal style=\"latex\">?</literal>")
  10425. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10426. "<literal style=\"html\">\n?\n</literal>")
  10427. ("H" "#+HTML: "
  10428. "<literal style=\"html\">?</literal>")
  10429. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10430. ("A" "#+ASCII: ")
  10431. ("i" "#+INDEX: ?"
  10432. "#+INDEX: ?")
  10433. ("I" "#+INCLUDE: %file ?"
  10434. "<include file=%file markup=\"?\">"))
  10435. "Structure completion elements.
  10436. This is a list of abbreviation keys and values. The value gets inserted
  10437. if you type `<' followed by the key and then press the completion key,
  10438. usually `M-TAB'. %file will be replaced by a file name after prompting
  10439. for the file using completion. The cursor will be placed at the position
  10440. of the `?` in the template.
  10441. There are two templates for each key, the first uses the original Org syntax,
  10442. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10443. the default when the /org-mtags.el/ module has been loaded. See also the
  10444. variable `org-mtags-prefer-muse-templates'."
  10445. :group 'org-completion
  10446. :type '(repeat
  10447. (string :tag "Key")
  10448. (string :tag "Template")
  10449. (string :tag "Muse Template")))
  10450. (defun org-try-structure-completion ()
  10451. "Try to complete a structure template before point.
  10452. This looks for strings like \"<e\" on an otherwise empty line and
  10453. expands them."
  10454. (let ((l (buffer-substring (point-at-bol) (point)))
  10455. a)
  10456. (when (and (looking-at "[ \t]*$")
  10457. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10458. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10459. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10460. (match-beginning 1)) a)
  10461. t)))
  10462. (defun org-complete-expand-structure-template (start cell)
  10463. "Expand a structure template."
  10464. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10465. (rpl (nth (if musep 2 1) cell))
  10466. (ind ""))
  10467. (delete-region start (point))
  10468. (when (string-match "\\`#\\+" rpl)
  10469. (cond
  10470. ((bolp))
  10471. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10472. (setq ind (buffer-substring (point-at-bol) (point))))
  10473. (t (newline))))
  10474. (setq start (point))
  10475. (if (string-match "%file" rpl)
  10476. (setq rpl (replace-match
  10477. (concat
  10478. "\""
  10479. (save-match-data
  10480. (abbreviate-file-name (read-file-name "Include file: ")))
  10481. "\"")
  10482. t t rpl)))
  10483. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10484. (concat "\n" ind)))
  10485. (insert rpl)
  10486. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10487. ;;;; TODO, DEADLINE, Comments
  10488. (defun org-toggle-comment ()
  10489. "Change the COMMENT state of an entry."
  10490. (interactive)
  10491. (save-excursion
  10492. (org-back-to-heading)
  10493. (let (case-fold-search)
  10494. (cond
  10495. ((looking-at (format org-heading-keyword-regexp-format
  10496. org-comment-string))
  10497. (goto-char (match-end 1))
  10498. (looking-at (concat " +" org-comment-string))
  10499. (replace-match "" t t)
  10500. (when (eolp) (insert " ")))
  10501. ((looking-at org-outline-regexp)
  10502. (goto-char (match-end 0))
  10503. (insert org-comment-string " "))))))
  10504. (defvar org-last-todo-state-is-todo nil
  10505. "This is non-nil when the last TODO state change led to a TODO state.
  10506. If the last change removed the TODO tag or switched to DONE, then
  10507. this is nil.")
  10508. (defvar org-setting-tags nil) ; dynamically skipped
  10509. (defvar org-todo-setup-filter-hook nil
  10510. "Hook for functions that pre-filter todo specs.
  10511. Each function takes a todo spec and returns either nil or the spec
  10512. transformed into canonical form." )
  10513. (defvar org-todo-get-default-hook nil
  10514. "Hook for functions that get a default item for todo.
  10515. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10516. nil or a string to be used for the todo mark." )
  10517. (defvar org-agenda-headline-snapshot-before-repeat)
  10518. (defun org-current-effective-time ()
  10519. "Return current time adjusted for `org-extend-today-until' variable."
  10520. (let* ((ct (org-current-time))
  10521. (dct (decode-time ct))
  10522. (ct1
  10523. (cond
  10524. (org-use-last-clock-out-time-as-effective-time
  10525. (or (org-clock-get-last-clock-out-time) ct))
  10526. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10527. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10528. (t ct))))
  10529. ct1))
  10530. (defun org-todo-yesterday (&optional arg)
  10531. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10532. (interactive "P")
  10533. (if (eq major-mode 'org-agenda-mode)
  10534. (apply 'org-agenda-todo-yesterday arg)
  10535. (let* ((hour (third (decode-time
  10536. (org-current-time))))
  10537. (org-extend-today-until (1+ hour)))
  10538. (org-todo arg))))
  10539. (defvar org-block-entry-blocking ""
  10540. "First entry preventing the TODO state change.")
  10541. (defun org-todo (&optional arg)
  10542. "Change the TODO state of an item.
  10543. The state of an item is given by a keyword at the start of the heading,
  10544. like
  10545. *** TODO Write paper
  10546. *** DONE Call mom
  10547. The different keywords are specified in the variable `org-todo-keywords'.
  10548. By default the available states are \"TODO\" and \"DONE\".
  10549. So for this example: when the item starts with TODO, it is changed to DONE.
  10550. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10551. DONE are present, add TODO at the beginning of the heading.
  10552. With \\[universal-argument] prefix arg, use completion to determine the new \
  10553. state.
  10554. With numeric prefix arg, switch to that state.
  10555. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10556. keywords (nextset).
  10557. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10558. With a numeric prefix arg of 0, inhibit note taking for the change.
  10559. For calling through lisp, arg is also interpreted in the following way:
  10560. 'none -> empty state
  10561. \"\"(empty string) -> switch to empty state
  10562. 'done -> switch to DONE
  10563. 'nextset -> switch to the next set of keywords
  10564. 'previousset -> switch to the previous set of keywords
  10565. \"WAITING\" -> switch to the specified keyword, but only if it
  10566. really is a member of `org-todo-keywords'."
  10567. (interactive "P")
  10568. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10569. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10570. 'region-start-level 'region))
  10571. org-loop-over-headlines-in-active-region)
  10572. (org-map-entries
  10573. `(org-todo ,arg)
  10574. org-loop-over-headlines-in-active-region
  10575. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10576. (if (equal arg '(16)) (setq arg 'nextset))
  10577. (let ((org-blocker-hook org-blocker-hook)
  10578. commentp
  10579. case-fold-search)
  10580. (when (equal arg '(64))
  10581. (setq arg nil org-blocker-hook nil))
  10582. (when (and org-blocker-hook
  10583. (or org-inhibit-blocking
  10584. (org-entry-get nil "NOBLOCKING")))
  10585. (setq org-blocker-hook nil))
  10586. (save-excursion
  10587. (catch 'exit
  10588. (org-back-to-heading t)
  10589. (when (looking-at (concat "^\\*+ " org-comment-string))
  10590. (org-toggle-comment)
  10591. (setq commentp t))
  10592. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10593. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10594. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10595. (let* ((match-data (match-data))
  10596. (startpos (point-at-bol))
  10597. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10598. (org-log-done org-log-done)
  10599. (org-log-repeat org-log-repeat)
  10600. (org-todo-log-states org-todo-log-states)
  10601. (org-inhibit-logging
  10602. (if (equal arg 0)
  10603. (progn (setq arg nil) 'note) org-inhibit-logging))
  10604. (this (match-string 1))
  10605. (hl-pos (match-beginning 0))
  10606. (head (org-get-todo-sequence-head this))
  10607. (ass (assoc head org-todo-kwd-alist))
  10608. (interpret (nth 1 ass))
  10609. (done-word (nth 3 ass))
  10610. (final-done-word (nth 4 ass))
  10611. (org-last-state (or this ""))
  10612. (completion-ignore-case t)
  10613. (member (member this org-todo-keywords-1))
  10614. (tail (cdr member))
  10615. (org-state (cond
  10616. ((and org-todo-key-trigger
  10617. (or (and (equal arg '(4))
  10618. (eq org-use-fast-todo-selection 'prefix))
  10619. (and (not arg) org-use-fast-todo-selection
  10620. (not (eq org-use-fast-todo-selection
  10621. 'prefix)))))
  10622. ;; Use fast selection
  10623. (org-fast-todo-selection))
  10624. ((and (equal arg '(4))
  10625. (or (not org-use-fast-todo-selection)
  10626. (not org-todo-key-trigger)))
  10627. ;; Read a state with completion
  10628. (org-icompleting-read
  10629. "State: " (mapcar (lambda(x) (list x))
  10630. org-todo-keywords-1)
  10631. nil t))
  10632. ((eq arg 'right)
  10633. (if this
  10634. (if tail (car tail) nil)
  10635. (car org-todo-keywords-1)))
  10636. ((eq arg 'left)
  10637. (if (equal member org-todo-keywords-1)
  10638. nil
  10639. (if this
  10640. (nth (- (length org-todo-keywords-1)
  10641. (length tail) 2)
  10642. org-todo-keywords-1)
  10643. (org-last org-todo-keywords-1))))
  10644. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10645. (setq arg nil))) ; hack to fall back to cycling
  10646. (arg
  10647. ;; user or caller requests a specific state
  10648. (cond
  10649. ((equal arg "") nil)
  10650. ((eq arg 'none) nil)
  10651. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10652. ((eq arg 'nextset)
  10653. (or (car (cdr (member head org-todo-heads)))
  10654. (car org-todo-heads)))
  10655. ((eq arg 'previousset)
  10656. (let ((org-todo-heads (reverse org-todo-heads)))
  10657. (or (car (cdr (member head org-todo-heads)))
  10658. (car org-todo-heads))))
  10659. ((car (member arg org-todo-keywords-1)))
  10660. ((stringp arg)
  10661. (error "State `%s' not valid in this file" arg))
  10662. ((nth (1- (prefix-numeric-value arg))
  10663. org-todo-keywords-1))))
  10664. ((null member) (or head (car org-todo-keywords-1)))
  10665. ((equal this final-done-word) nil) ;; -> make empty
  10666. ((null tail) nil) ;; -> first entry
  10667. ((memq interpret '(type priority))
  10668. (if (eq this-command last-command)
  10669. (car tail)
  10670. (if (> (length tail) 0)
  10671. (or done-word (car org-done-keywords))
  10672. nil)))
  10673. (t
  10674. (car tail))))
  10675. (org-state (or
  10676. (run-hook-with-args-until-success
  10677. 'org-todo-get-default-hook org-state org-last-state)
  10678. org-state))
  10679. (next (if org-state (concat " " org-state " ") " "))
  10680. (change-plist (list :type 'todo-state-change :from this :to org-state
  10681. :position startpos))
  10682. dolog now-done-p)
  10683. (when org-blocker-hook
  10684. (setq org-last-todo-state-is-todo
  10685. (not (member this org-done-keywords)))
  10686. (unless (save-excursion
  10687. (save-match-data
  10688. (org-with-wide-buffer
  10689. (run-hook-with-args-until-failure
  10690. 'org-blocker-hook change-plist))))
  10691. (if (org-called-interactively-p 'interactive)
  10692. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  10693. this org-state org-block-entry-blocking)
  10694. ;; fail silently
  10695. (message "TODO state change from %s to %s blocked (by \"%s\")"
  10696. this org-state org-block-entry-blocking)
  10697. (throw 'exit nil))))
  10698. (store-match-data match-data)
  10699. (replace-match next t t)
  10700. (unless (pos-visible-in-window-p hl-pos)
  10701. (message "TODO state changed to %s" (org-trim next)))
  10702. (unless head
  10703. (setq head (org-get-todo-sequence-head org-state)
  10704. ass (assoc head org-todo-kwd-alist)
  10705. interpret (nth 1 ass)
  10706. done-word (nth 3 ass)
  10707. final-done-word (nth 4 ass)))
  10708. (when (memq arg '(nextset previousset))
  10709. (message "Keyword-Set %d/%d: %s"
  10710. (- (length org-todo-sets) -1
  10711. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10712. (length org-todo-sets)
  10713. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10714. (setq org-last-todo-state-is-todo
  10715. (not (member org-state org-done-keywords)))
  10716. (setq now-done-p (and (member org-state org-done-keywords)
  10717. (not (member this org-done-keywords))))
  10718. (and logging (org-local-logging logging))
  10719. (when (and (or org-todo-log-states org-log-done)
  10720. (not (eq org-inhibit-logging t))
  10721. (not (memq arg '(nextset previousset))))
  10722. ;; we need to look at recording a time and note
  10723. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10724. (nth 2 (assoc this org-todo-log-states))))
  10725. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10726. (setq dolog 'time))
  10727. (when (and org-state
  10728. (member org-state org-not-done-keywords)
  10729. (not (member this org-not-done-keywords)))
  10730. ;; This is now a todo state and was not one before
  10731. ;; If there was a CLOSED time stamp, get rid of it.
  10732. (org-add-planning-info nil nil 'closed))
  10733. (when (and now-done-p org-log-done)
  10734. ;; It is now done, and it was not done before
  10735. (org-add-planning-info 'closed (org-current-effective-time))
  10736. (if (and (not dolog) (eq 'note org-log-done))
  10737. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10738. (when (and org-state dolog)
  10739. ;; This is a non-nil state, and we need to log it
  10740. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10741. ;; Fixup tag positioning
  10742. (org-todo-trigger-tag-changes org-state)
  10743. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10744. (when org-provide-todo-statistics
  10745. (org-update-parent-todo-statistics))
  10746. (run-hooks 'org-after-todo-state-change-hook)
  10747. (if (and arg (not (member org-state org-done-keywords)))
  10748. (setq head (org-get-todo-sequence-head org-state)))
  10749. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10750. ;; Do we need to trigger a repeat?
  10751. (when now-done-p
  10752. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10753. ;; This is for the agenda, take a snapshot of the headline.
  10754. (save-match-data
  10755. (setq org-agenda-headline-snapshot-before-repeat
  10756. (org-get-heading))))
  10757. (org-auto-repeat-maybe org-state))
  10758. ;; Fixup cursor location if close to the keyword
  10759. (if (and (outline-on-heading-p)
  10760. (not (bolp))
  10761. (save-excursion (beginning-of-line 1)
  10762. (looking-at org-todo-line-regexp))
  10763. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10764. (progn
  10765. (goto-char (or (match-end 2) (match-end 1)))
  10766. (and (looking-at " ") (just-one-space))))
  10767. (when org-trigger-hook
  10768. (save-excursion
  10769. (run-hook-with-args 'org-trigger-hook change-plist)))
  10770. (when commentp (org-toggle-comment))))))))
  10771. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10772. "Block turning an entry into a TODO, using the hierarchy.
  10773. This checks whether the current task should be blocked from state
  10774. changes. Such blocking occurs when:
  10775. 1. The task has children which are not all in a completed state.
  10776. 2. A task has a parent with the property :ORDERED:, and there
  10777. are siblings prior to the current task with incomplete
  10778. status.
  10779. 3. The parent of the task is blocked because it has siblings that should
  10780. be done first, or is child of a block grandparent TODO entry."
  10781. (if (not org-enforce-todo-dependencies)
  10782. t ; if locally turned off don't block
  10783. (catch 'dont-block
  10784. ;; If this is not a todo state change, or if this entry is already DONE,
  10785. ;; do not block
  10786. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10787. (member (plist-get change-plist :from)
  10788. (cons 'done org-done-keywords))
  10789. (member (plist-get change-plist :to)
  10790. (cons 'todo org-not-done-keywords))
  10791. (not (plist-get change-plist :to)))
  10792. (throw 'dont-block t))
  10793. ;; If this task has children, and any are undone, it's blocked
  10794. (save-excursion
  10795. (org-back-to-heading t)
  10796. (let ((this-level (funcall outline-level)))
  10797. (outline-next-heading)
  10798. (let ((child-level (funcall outline-level)))
  10799. (while (and (not (eobp))
  10800. (> child-level this-level))
  10801. ;; this todo has children, check whether they are all
  10802. ;; completed
  10803. (if (and (not (org-entry-is-done-p))
  10804. (org-entry-is-todo-p))
  10805. (progn (setq org-block-entry-blocking (org-get-heading))
  10806. (throw 'dont-block nil)))
  10807. (outline-next-heading)
  10808. (setq child-level (funcall outline-level))))))
  10809. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10810. ;; any previous siblings are undone, it's blocked
  10811. (save-excursion
  10812. (org-back-to-heading t)
  10813. (let* ((pos (point))
  10814. (parent-pos (and (org-up-heading-safe) (point))))
  10815. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10816. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10817. (forward-line 1)
  10818. (re-search-forward org-not-done-heading-regexp pos t))
  10819. (setq org-block-entry-blocking (match-string 0))
  10820. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10821. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10822. (while t
  10823. (goto-char parent-pos)
  10824. (if (not (looking-at org-not-done-heading-regexp))
  10825. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10826. (setq pos (point))
  10827. (setq parent-pos (and (org-up-heading-safe) (point)))
  10828. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10829. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10830. (forward-line 1)
  10831. (re-search-forward org-not-done-heading-regexp pos t)
  10832. (setq org-block-entry-blocking (org-get-heading)))
  10833. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10834. (defcustom org-track-ordered-property-with-tag nil
  10835. "Should the ORDERED property also be shown as a tag?
  10836. The ORDERED property decides if an entry should require subtasks to be
  10837. completed in sequence. Since a property is not very visible, setting
  10838. this option means that toggling the ORDERED property with the command
  10839. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10840. not relevant for the behavior, but it makes things more visible.
  10841. Note that toggling the tag with tags commands will not change the property
  10842. and therefore not influence behavior!
  10843. This can be t, meaning the tag ORDERED should be used, It can also be a
  10844. string to select a different tag for this task."
  10845. :group 'org-todo
  10846. :type '(choice
  10847. (const :tag "No tracking" nil)
  10848. (const :tag "Track with ORDERED tag" t)
  10849. (string :tag "Use other tag")))
  10850. (defun org-toggle-ordered-property ()
  10851. "Toggle the ORDERED property of the current entry.
  10852. For better visibility, you can track the value of this property with a tag.
  10853. See variable `org-track-ordered-property-with-tag'."
  10854. (interactive)
  10855. (let* ((t1 org-track-ordered-property-with-tag)
  10856. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10857. (save-excursion
  10858. (org-back-to-heading)
  10859. (if (org-entry-get nil "ORDERED")
  10860. (progn
  10861. (org-delete-property "ORDERED")
  10862. (and tag (org-toggle-tag tag 'off))
  10863. (message "Subtasks can be completed in arbitrary order"))
  10864. (org-entry-put nil "ORDERED" "t")
  10865. (and tag (org-toggle-tag tag 'on))
  10866. (message "Subtasks must be completed in sequence")))))
  10867. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10868. (defun org-block-todo-from-checkboxes (change-plist)
  10869. "Block turning an entry into a TODO, using checkboxes.
  10870. This checks whether the current task should be blocked from state
  10871. changes because there are unchecked boxes in this entry."
  10872. (if (not org-enforce-todo-checkbox-dependencies)
  10873. t ; if locally turned off don't block
  10874. (catch 'dont-block
  10875. ;; If this is not a todo state change, or if this entry is already DONE,
  10876. ;; do not block
  10877. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10878. (member (plist-get change-plist :from)
  10879. (cons 'done org-done-keywords))
  10880. (member (plist-get change-plist :to)
  10881. (cons 'todo org-not-done-keywords))
  10882. (not (plist-get change-plist :to)))
  10883. (throw 'dont-block t))
  10884. ;; If this task has checkboxes that are not checked, it's blocked
  10885. (save-excursion
  10886. (org-back-to-heading t)
  10887. (let ((beg (point)) end)
  10888. (outline-next-heading)
  10889. (setq end (point))
  10890. (goto-char beg)
  10891. (if (org-list-search-forward
  10892. (concat (org-item-beginning-re)
  10893. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10894. "\\[[- ]\\]")
  10895. end t)
  10896. (progn
  10897. (if (boundp 'org-blocked-by-checkboxes)
  10898. (setq org-blocked-by-checkboxes t))
  10899. (throw 'dont-block nil)))))
  10900. t))) ; do not block
  10901. (defun org-entry-blocked-p ()
  10902. "Is the current entry blocked?"
  10903. (org-with-buffer-modified-unmodified
  10904. (if (org-entry-get nil "NOBLOCKING")
  10905. nil ;; Never block this entry
  10906. (not
  10907. (run-hook-with-args-until-failure
  10908. 'org-blocker-hook
  10909. (list :type 'todo-state-change
  10910. :position (point)
  10911. :from 'todo
  10912. :to 'done))))))
  10913. (defun org-update-statistics-cookies (all)
  10914. "Update the statistics cookie, either from TODO or from checkboxes.
  10915. This should be called with the cursor in a line with a statistics cookie."
  10916. (interactive "P")
  10917. (if all
  10918. (progn
  10919. (org-update-checkbox-count 'all)
  10920. (org-map-entries 'org-update-parent-todo-statistics))
  10921. (if (not (org-at-heading-p))
  10922. (org-update-checkbox-count)
  10923. (let ((pos (point-marker))
  10924. end l1 l2)
  10925. (ignore-errors (org-back-to-heading t))
  10926. (if (not (org-at-heading-p))
  10927. (org-update-checkbox-count)
  10928. (setq l1 (org-outline-level))
  10929. (setq end (save-excursion
  10930. (outline-next-heading)
  10931. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10932. (point)))
  10933. (if (and (save-excursion
  10934. (re-search-forward
  10935. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10936. (not (save-excursion (re-search-forward
  10937. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10938. (org-update-checkbox-count)
  10939. (if (and l2 (> l2 l1))
  10940. (progn
  10941. (goto-char end)
  10942. (org-update-parent-todo-statistics))
  10943. (goto-char pos)
  10944. (beginning-of-line 1)
  10945. (while (re-search-forward
  10946. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10947. (point-at-eol) t)
  10948. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10949. (goto-char pos)
  10950. (move-marker pos nil)))))
  10951. (defvar org-entry-property-inherited-from) ;; defined below
  10952. (defun org-update-parent-todo-statistics ()
  10953. "Update any statistics cookie in the parent of the current headline.
  10954. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10955. the entire subtree and this will travel up the hierarchy and update
  10956. statistics everywhere."
  10957. (let* ((prop (save-excursion (org-up-heading-safe)
  10958. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10959. (recursive (or (not org-hierarchical-todo-statistics)
  10960. (and prop (string-match "\\<recursive\\>" prop))))
  10961. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10962. 0))
  10963. (first t)
  10964. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10965. level ltoggle l1 new ndel
  10966. (cnt-all 0) (cnt-done 0) is-percent kwd
  10967. checkbox-beg ov ovs ove cookie-present)
  10968. (catch 'exit
  10969. (save-excursion
  10970. (beginning-of-line 1)
  10971. (setq ltoggle (funcall outline-level))
  10972. ;; Three situations are to consider:
  10973. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10974. ;; to the top-level ancestor on the headline;
  10975. ;; 2. If parent has "recursive" property, repeat up to the
  10976. ;; headline setting that property, taking inheritance into
  10977. ;; account;
  10978. ;; 3. Else, move up to direct parent and proceed only once.
  10979. (while (and (setq level (org-up-heading-safe))
  10980. (or recursive first)
  10981. (>= (point) lim))
  10982. (setq first nil cookie-present nil)
  10983. (unless (and level
  10984. (not (string-match
  10985. "\\<checkbox\\>"
  10986. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10987. "")))))
  10988. (throw 'exit nil))
  10989. (while (re-search-forward box-re (point-at-eol) t)
  10990. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10991. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10992. (save-match-data
  10993. (unless (outline-next-heading) (throw 'exit nil))
  10994. (while (and (looking-at org-complex-heading-regexp)
  10995. (> (setq l1 (length (match-string 1))) level))
  10996. (setq kwd (and (or recursive (= l1 ltoggle))
  10997. (match-string 2)))
  10998. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10999. (and (listp org-provide-todo-statistics)
  11000. (or (member kwd org-provide-todo-statistics)
  11001. (member kwd org-done-keywords))))
  11002. (setq cnt-all (1+ cnt-all))
  11003. (if (eq org-provide-todo-statistics t)
  11004. (and kwd (setq cnt-all (1+ cnt-all)))))
  11005. (and (member kwd org-done-keywords)
  11006. (setq cnt-done (1+ cnt-done)))
  11007. (outline-next-heading)))
  11008. (setq new
  11009. (if is-percent
  11010. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11011. (format "[%d/%d]" cnt-done cnt-all))
  11012. ndel (- (match-end 0) checkbox-beg))
  11013. ;; handle overlays when updating cookie from column view
  11014. (when (setq ov (car (overlays-at checkbox-beg)))
  11015. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11016. (delete-overlay ov))
  11017. (goto-char checkbox-beg)
  11018. (insert new)
  11019. (delete-region (point) (+ (point) ndel))
  11020. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11021. (when ov (move-overlay ov ovs ove)))
  11022. (when cookie-present
  11023. (run-hook-with-args 'org-after-todo-statistics-hook
  11024. cnt-done (- cnt-all cnt-done))))))
  11025. (run-hooks 'org-todo-statistics-hook)))
  11026. (defvar org-after-todo-statistics-hook nil
  11027. "Hook that is called after a TODO statistics cookie has been updated.
  11028. Each function is called with two arguments: the number of not-done entries
  11029. and the number of done entries.
  11030. For example, the following function, when added to this hook, will switch
  11031. an entry to DONE when all children are done, and back to TODO when new
  11032. entries are set to a TODO status. Note that this hook is only called
  11033. when there is a statistics cookie in the headline!
  11034. (defun org-summary-todo (n-done n-not-done)
  11035. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11036. (let (org-log-done org-log-states) ; turn off logging
  11037. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11038. ")
  11039. (defvar org-todo-statistics-hook nil
  11040. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11041. This hook runs even if there is no statistics cookie present, in which case
  11042. `org-after-todo-statistics-hook' would not run.")
  11043. (defun org-todo-trigger-tag-changes (state)
  11044. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11045. (let ((l org-todo-state-tags-triggers)
  11046. changes)
  11047. (when (or (not state) (equal state ""))
  11048. (setq changes (append changes (cdr (assoc "" l)))))
  11049. (when (and (stringp state) (> (length state) 0))
  11050. (setq changes (append changes (cdr (assoc state l)))))
  11051. (when (member state org-not-done-keywords)
  11052. (setq changes (append changes (cdr (assoc 'todo l)))))
  11053. (when (member state org-done-keywords)
  11054. (setq changes (append changes (cdr (assoc 'done l)))))
  11055. (dolist (c changes)
  11056. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11057. (defun org-local-logging (value)
  11058. "Get logging settings from a property VALUE."
  11059. (let* (words w a)
  11060. ;; directly set the variables, they are already local.
  11061. (setq org-log-done nil
  11062. org-log-repeat nil
  11063. org-todo-log-states nil)
  11064. (setq words (org-split-string value))
  11065. (while (setq w (pop words))
  11066. (cond
  11067. ((setq a (assoc w org-startup-options))
  11068. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11069. (set (nth 1 a) (nth 2 a))))
  11070. ((setq a (org-extract-log-state-settings w))
  11071. (and (member (car a) org-todo-keywords-1)
  11072. (push a org-todo-log-states)))))))
  11073. (defun org-get-todo-sequence-head (kwd)
  11074. "Return the head of the TODO sequence to which KWD belongs.
  11075. If KWD is not set, check if there is a text property remembering the
  11076. right sequence."
  11077. (let (p)
  11078. (cond
  11079. ((not kwd)
  11080. (or (get-text-property (point-at-bol) 'org-todo-head)
  11081. (progn
  11082. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11083. nil (point-at-eol)))
  11084. (get-text-property p 'org-todo-head))))
  11085. ((not (member kwd org-todo-keywords-1))
  11086. (car org-todo-keywords-1))
  11087. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11088. (defun org-fast-todo-selection ()
  11089. "Fast TODO keyword selection with single keys.
  11090. Returns the new TODO keyword, or nil if no state change should occur."
  11091. (let* ((fulltable org-todo-key-alist)
  11092. (done-keywords org-done-keywords) ;; needed for the faces.
  11093. (maxlen (apply 'max (mapcar
  11094. (lambda (x)
  11095. (if (stringp (car x)) (string-width (car x)) 0))
  11096. fulltable)))
  11097. (expert nil)
  11098. (fwidth (+ maxlen 3 1 3))
  11099. (ncol (/ (- (window-width) 4) fwidth))
  11100. tg cnt e c tbl
  11101. groups ingroup)
  11102. (save-excursion
  11103. (save-window-excursion
  11104. (if expert
  11105. (set-buffer (get-buffer-create " *Org todo*"))
  11106. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11107. (erase-buffer)
  11108. (org-set-local 'org-done-keywords done-keywords)
  11109. (setq tbl fulltable cnt 0)
  11110. (while (setq e (pop tbl))
  11111. (cond
  11112. ((equal e '(:startgroup))
  11113. (push '() groups) (setq ingroup t)
  11114. (when (not (= cnt 0))
  11115. (setq cnt 0)
  11116. (insert "\n"))
  11117. (insert "{ "))
  11118. ((equal e '(:endgroup))
  11119. (setq ingroup nil cnt 0)
  11120. (insert "}\n"))
  11121. ((equal e '(:newline))
  11122. (when (not (= cnt 0))
  11123. (setq cnt 0)
  11124. (insert "\n")
  11125. (setq e (car tbl))
  11126. (while (equal (car tbl) '(:newline))
  11127. (insert "\n")
  11128. (setq tbl (cdr tbl)))))
  11129. (t
  11130. (setq tg (car e) c (cdr e))
  11131. (if ingroup (push tg (car groups)))
  11132. (setq tg (org-add-props tg nil 'face
  11133. (org-get-todo-face tg)))
  11134. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11135. (insert "[" c "] " tg (make-string
  11136. (- fwidth 4 (length tg)) ?\ ))
  11137. (when (= (setq cnt (1+ cnt)) ncol)
  11138. (insert "\n")
  11139. (if ingroup (insert " "))
  11140. (setq cnt 0)))))
  11141. (insert "\n")
  11142. (goto-char (point-min))
  11143. (if (not expert) (org-fit-window-to-buffer))
  11144. (message "[a-z..]:Set [SPC]:clear")
  11145. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11146. (cond
  11147. ((or (= c ?\C-g)
  11148. (and (= c ?q) (not (rassoc c fulltable))))
  11149. (setq quit-flag t))
  11150. ((= c ?\ ) nil)
  11151. ((setq e (rassoc c fulltable) tg (car e))
  11152. tg)
  11153. (t (setq quit-flag t)))))))
  11154. (defun org-entry-is-todo-p ()
  11155. (member (org-get-todo-state) org-not-done-keywords))
  11156. (defun org-entry-is-done-p ()
  11157. (member (org-get-todo-state) org-done-keywords))
  11158. (defun org-get-todo-state ()
  11159. (save-excursion
  11160. (org-back-to-heading t)
  11161. (and (looking-at org-todo-line-regexp)
  11162. (match-end 2)
  11163. (match-string 2))))
  11164. (defun org-at-date-range-p (&optional inactive-ok)
  11165. "Is the cursor inside a date range?"
  11166. (interactive)
  11167. (save-excursion
  11168. (catch 'exit
  11169. (let ((pos (point)))
  11170. (skip-chars-backward "^[<\r\n")
  11171. (skip-chars-backward "<[")
  11172. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11173. (>= (match-end 0) pos)
  11174. (throw 'exit t))
  11175. (skip-chars-backward "^<[\r\n")
  11176. (skip-chars-backward "<[")
  11177. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11178. (>= (match-end 0) pos)
  11179. (throw 'exit t)))
  11180. nil)))
  11181. (defun org-get-repeat (&optional tagline)
  11182. "Check if there is a deadline/schedule with repeater in this entry."
  11183. (save-match-data
  11184. (save-excursion
  11185. (org-back-to-heading t)
  11186. (and (re-search-forward (if tagline
  11187. (concat tagline "\\s-*" org-repeat-re)
  11188. org-repeat-re)
  11189. (org-entry-end-position) t)
  11190. (match-string-no-properties 1)))))
  11191. (defvar org-last-changed-timestamp)
  11192. (defvar org-last-inserted-timestamp)
  11193. (defvar org-log-post-message)
  11194. (defvar org-log-note-purpose)
  11195. (defvar org-log-note-how)
  11196. (defvar org-log-note-extra)
  11197. (defun org-auto-repeat-maybe (done-word)
  11198. "Check if the current headline contains a repeated deadline/schedule.
  11199. If yes, set TODO state back to what it was and change the base date
  11200. of repeating deadline/scheduled time stamps to new date.
  11201. This function is run automatically after each state change to a DONE state."
  11202. ;; last-state is dynamically scoped into this function
  11203. (let* ((repeat (org-get-repeat))
  11204. (aa (assoc org-last-state org-todo-kwd-alist))
  11205. (interpret (nth 1 aa))
  11206. (head (nth 2 aa))
  11207. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11208. (msg "Entry repeats: ")
  11209. (org-log-done nil)
  11210. (org-todo-log-states nil)
  11211. re type n what ts time to-state)
  11212. (when repeat
  11213. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11214. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11215. org-todo-repeat-to-state))
  11216. (unless (and to-state (member to-state org-todo-keywords-1))
  11217. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11218. (org-todo to-state)
  11219. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11220. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11221. (org-time-stamp-format t t))))
  11222. (when org-log-repeat
  11223. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11224. (memq 'org-add-log-note post-command-hook))
  11225. ;; OK, we are already setup for some record
  11226. (if (eq org-log-repeat 'note)
  11227. ;; make sure we take a note, not only a time stamp
  11228. (setq org-log-note-how 'note))
  11229. ;; Set up for taking a record
  11230. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11231. org-last-state
  11232. 'findpos org-log-repeat)))
  11233. (org-back-to-heading t)
  11234. (org-add-planning-info nil nil 'closed)
  11235. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11236. org-deadline-time-regexp "\\)\\|\\("
  11237. org-ts-regexp "\\)"))
  11238. (while (re-search-forward
  11239. re (save-excursion (outline-next-heading) (point)) t)
  11240. (setq type (if (match-end 1) org-scheduled-string
  11241. (if (match-end 3) org-deadline-string "Plain:"))
  11242. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11243. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11244. (setq n (string-to-number (match-string 2 ts))
  11245. what (match-string 3 ts))
  11246. (if (equal what "w") (setq n (* n 7) what "d"))
  11247. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11248. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11249. ;; Preparation, see if we need to modify the start date for the change
  11250. (when (match-end 1)
  11251. (setq time (save-match-data (org-time-string-to-time ts)))
  11252. (cond
  11253. ((equal (match-string 1 ts) ".")
  11254. ;; Shift starting date to today
  11255. (org-timestamp-change
  11256. (- (org-today) (time-to-days time))
  11257. 'day))
  11258. ((equal (match-string 1 ts) "+")
  11259. (let ((nshiftmax 10) (nshift 0))
  11260. (while (or (= nshift 0)
  11261. (<= (time-to-days time)
  11262. (time-to-days (current-time))))
  11263. (when (= (incf nshift) nshiftmax)
  11264. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11265. (error "Abort")))
  11266. (org-timestamp-change n (cdr (assoc what whata)))
  11267. (org-at-timestamp-p t)
  11268. (setq ts (match-string 1))
  11269. (setq time (save-match-data (org-time-string-to-time ts)))))
  11270. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11271. ;; rematch, so that we have everything in place for the real shift
  11272. (org-at-timestamp-p t)
  11273. (setq ts (match-string 1))
  11274. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11275. (org-timestamp-change n (cdr (assoc what whata)))
  11276. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11277. (setq org-log-post-message msg)
  11278. (message "%s" msg))))
  11279. (defun org-show-todo-tree (arg)
  11280. "Make a compact tree which shows all headlines marked with TODO.
  11281. The tree will show the lines where the regexp matches, and all higher
  11282. headlines above the match.
  11283. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11284. With a numeric prefix N, construct a sparse tree for the Nth element
  11285. of `org-todo-keywords-1'."
  11286. (interactive "P")
  11287. (let ((case-fold-search nil)
  11288. (kwd-re
  11289. (cond ((null arg) org-not-done-regexp)
  11290. ((equal arg '(4))
  11291. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11292. (mapcar 'list org-todo-keywords-1))))
  11293. (concat "\\("
  11294. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11295. "\\)\\>")))
  11296. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11297. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11298. org-todo-keywords-1)))
  11299. (t (error "Invalid prefix argument: %s" arg)))))
  11300. (message "%d TODO entries found"
  11301. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11302. (defun org-deadline (&optional remove time)
  11303. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11304. With argument REMOVE, remove any deadline from the item.
  11305. With argument TIME, set the deadline at the corresponding date. TIME
  11306. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11307. (interactive "P")
  11308. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11309. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11310. 'region-start-level 'region))
  11311. org-loop-over-headlines-in-active-region)
  11312. (org-map-entries
  11313. `(org-deadline ',remove ,time)
  11314. org-loop-over-headlines-in-active-region
  11315. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11316. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11317. (repeater (and old-date
  11318. (string-match
  11319. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11320. old-date)
  11321. (match-string 1 old-date))))
  11322. (if remove
  11323. (progn
  11324. (when (and old-date org-log-redeadline)
  11325. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11326. org-log-redeadline))
  11327. (org-remove-timestamp-with-keyword org-deadline-string)
  11328. (message "Item no longer has a deadline."))
  11329. (org-add-planning-info 'deadline time 'closed)
  11330. (when (and old-date org-log-redeadline
  11331. (not (equal old-date
  11332. (substring org-last-inserted-timestamp 1 -1))))
  11333. (org-add-log-setup 'redeadline nil old-date 'findpos
  11334. org-log-redeadline))
  11335. (when repeater
  11336. (save-excursion
  11337. (org-back-to-heading t)
  11338. (when (re-search-forward (concat org-deadline-string " "
  11339. org-last-inserted-timestamp)
  11340. (save-excursion
  11341. (outline-next-heading) (point)) t)
  11342. (goto-char (1- (match-end 0)))
  11343. (insert " " repeater)
  11344. (setq org-last-inserted-timestamp
  11345. (concat (substring org-last-inserted-timestamp 0 -1)
  11346. " " repeater
  11347. (substring org-last-inserted-timestamp -1))))))
  11348. (message "Deadline on %s" org-last-inserted-timestamp)))))
  11349. (defun org-schedule (&optional remove time)
  11350. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11351. With argument REMOVE, remove any scheduling date from the item.
  11352. With argument TIME, scheduled at the corresponding date. TIME can
  11353. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11354. (interactive "P")
  11355. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11356. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11357. 'region-start-level 'region))
  11358. org-loop-over-headlines-in-active-region)
  11359. (org-map-entries
  11360. `(org-schedule ',remove ,time)
  11361. org-loop-over-headlines-in-active-region
  11362. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11363. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11364. (repeater (and old-date
  11365. (string-match
  11366. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11367. old-date)
  11368. (match-string 1 old-date))))
  11369. (if remove
  11370. (progn
  11371. (when (and old-date org-log-reschedule)
  11372. (org-add-log-setup 'delschedule nil old-date 'findpos
  11373. org-log-reschedule))
  11374. (org-remove-timestamp-with-keyword org-scheduled-string)
  11375. (message "Item is no longer scheduled."))
  11376. (org-add-planning-info 'scheduled time 'closed)
  11377. (when (and old-date org-log-reschedule
  11378. (not (equal old-date
  11379. (substring org-last-inserted-timestamp 1 -1))))
  11380. (org-add-log-setup 'reschedule nil old-date 'findpos
  11381. org-log-reschedule))
  11382. (when repeater
  11383. (save-excursion
  11384. (org-back-to-heading t)
  11385. (when (re-search-forward (concat org-scheduled-string " "
  11386. org-last-inserted-timestamp)
  11387. (save-excursion
  11388. (outline-next-heading) (point)) t)
  11389. (goto-char (1- (match-end 0)))
  11390. (insert " " repeater)
  11391. (setq org-last-inserted-timestamp
  11392. (concat (substring org-last-inserted-timestamp 0 -1)
  11393. " " repeater
  11394. (substring org-last-inserted-timestamp -1))))))
  11395. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11396. (defun org-get-scheduled-time (pom &optional inherit)
  11397. "Get the scheduled time as a time tuple, of a format suitable
  11398. for calling org-schedule with, or if there is no scheduling,
  11399. returns nil."
  11400. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11401. (when time
  11402. (apply 'encode-time (org-parse-time-string time)))))
  11403. (defun org-get-deadline-time (pom &optional inherit)
  11404. "Get the deadline as a time tuple, of a format suitable for
  11405. calling org-deadline with, or if there is no scheduling, returns
  11406. nil."
  11407. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11408. (when time
  11409. (apply 'encode-time (org-parse-time-string time)))))
  11410. (defun org-remove-timestamp-with-keyword (keyword)
  11411. "Remove all time stamps with KEYWORD in the current entry."
  11412. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11413. beg)
  11414. (save-excursion
  11415. (org-back-to-heading t)
  11416. (setq beg (point))
  11417. (outline-next-heading)
  11418. (while (re-search-backward re beg t)
  11419. (replace-match "")
  11420. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11421. (equal (char-before) ?\ ))
  11422. (backward-delete-char 1)
  11423. (if (string-match "^[ \t]*$" (buffer-substring
  11424. (point-at-bol) (point-at-eol)))
  11425. (delete-region (point-at-bol)
  11426. (min (point-max) (1+ (point-at-eol))))))))))
  11427. (defun org-add-planning-info (what &optional time &rest remove)
  11428. "Insert new timestamp with keyword in the line directly after the headline.
  11429. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11430. If non is given, the user is prompted for a date.
  11431. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11432. be removed."
  11433. (interactive)
  11434. (let (org-time-was-given org-end-time-was-given ts
  11435. end default-time default-input)
  11436. (catch 'exit
  11437. (when (and (memq what '(scheduled deadline))
  11438. (or (not time)
  11439. (and (stringp time)
  11440. (string-match "^[-+]+[0-9]" time))))
  11441. ;; Try to get a default date/time from existing timestamp
  11442. (save-excursion
  11443. (org-back-to-heading t)
  11444. (setq end (save-excursion (outline-next-heading) (point)))
  11445. (when (re-search-forward (if (eq what 'scheduled)
  11446. org-scheduled-time-regexp
  11447. org-deadline-time-regexp)
  11448. end t)
  11449. (setq ts (match-string 1)
  11450. default-time
  11451. (apply 'encode-time (org-parse-time-string ts))
  11452. default-input (and ts (org-get-compact-tod ts))))))
  11453. (when what
  11454. (setq time
  11455. (if (stringp time)
  11456. ;; This is a string (relative or absolute), set proper date
  11457. (apply 'encode-time
  11458. (org-read-date-analyze
  11459. time default-time (decode-time default-time)))
  11460. ;; If necessary, get the time from the user
  11461. (or time (org-read-date nil 'to-time nil nil
  11462. default-time default-input)))))
  11463. (when (and org-insert-labeled-timestamps-at-point
  11464. (member what '(scheduled deadline)))
  11465. (insert
  11466. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11467. (org-insert-time-stamp time org-time-was-given
  11468. nil nil nil (list org-end-time-was-given))
  11469. (setq what nil))
  11470. (save-excursion
  11471. (save-restriction
  11472. (let (col list elt ts buffer-invisibility-spec)
  11473. (org-back-to-heading t)
  11474. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11475. (goto-char (match-end 1))
  11476. (setq col (current-column))
  11477. (goto-char (match-end 0))
  11478. (if (eobp) (insert "\n") (forward-char 1))
  11479. (when (and (not what)
  11480. (not (looking-at
  11481. (concat "[ \t]*"
  11482. org-keyword-time-not-clock-regexp))))
  11483. ;; Nothing to add, nothing to remove...... :-)
  11484. (throw 'exit nil))
  11485. (if (and (not (looking-at org-outline-regexp))
  11486. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11487. "[^\r\n]*"))
  11488. (not (equal (match-string 1) org-clock-string)))
  11489. (narrow-to-region (match-beginning 0) (match-end 0))
  11490. (insert-before-markers "\n")
  11491. (backward-char 1)
  11492. (narrow-to-region (point) (point))
  11493. (and org-adapt-indentation (org-indent-to-column col)))
  11494. ;; Check if we have to remove something.
  11495. (setq list (cons what remove))
  11496. (while list
  11497. (setq elt (pop list))
  11498. (when (or (and (eq elt 'scheduled)
  11499. (re-search-forward org-scheduled-time-regexp nil t))
  11500. (and (eq elt 'deadline)
  11501. (re-search-forward org-deadline-time-regexp nil t))
  11502. (and (eq elt 'closed)
  11503. (re-search-forward org-closed-time-regexp nil t)))
  11504. (replace-match "")
  11505. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11506. (and (looking-at "[ \t]+") (replace-match ""))
  11507. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11508. (when what
  11509. (insert
  11510. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11511. (cond ((eq what 'scheduled) org-scheduled-string)
  11512. ((eq what 'deadline) org-deadline-string)
  11513. ((eq what 'closed) org-closed-string))
  11514. " ")
  11515. (setq ts (org-insert-time-stamp
  11516. time
  11517. (or org-time-was-given
  11518. (and (eq what 'closed) org-log-done-with-time))
  11519. (eq what 'closed)
  11520. nil nil (list org-end-time-was-given)))
  11521. (insert
  11522. (if (not (or (bolp) (eq (char-before) ?\ )
  11523. (memq (char-after) '(32 10))
  11524. (eobp))) " " ""))
  11525. (end-of-line 1))
  11526. (goto-char (point-min))
  11527. (widen)
  11528. (if (and (looking-at "[ \t]*\n")
  11529. (equal (char-before) ?\n))
  11530. (delete-region (1- (point)) (point-at-eol)))
  11531. ts))))))
  11532. (defvar org-log-note-marker (make-marker))
  11533. (defvar org-log-note-purpose nil)
  11534. (defvar org-log-note-state nil)
  11535. (defvar org-log-note-previous-state nil)
  11536. (defvar org-log-note-how nil)
  11537. (defvar org-log-note-extra nil)
  11538. (defvar org-log-note-window-configuration nil)
  11539. (defvar org-log-note-return-to (make-marker))
  11540. (defvar org-log-note-effective-time nil
  11541. "Remembered current time so that dynamically scoped
  11542. `org-extend-today-until' affects tha timestamps in state change
  11543. log")
  11544. (defvar org-log-post-message nil
  11545. "Message to be displayed after a log note has been stored.
  11546. The auto-repeater uses this.")
  11547. (defun org-add-note ()
  11548. "Add a note to the current entry.
  11549. This is done in the same way as adding a state change note."
  11550. (interactive)
  11551. (org-add-log-setup 'note nil nil 'findpos nil))
  11552. (defvar org-property-end-re)
  11553. (defun org-add-log-setup (&optional purpose state prev-state
  11554. findpos how extra)
  11555. "Set up the post command hook to take a note.
  11556. If this is about to TODO state change, the new state is expected in STATE.
  11557. When FINDPOS is non-nil, find the correct position for the note in
  11558. the current entry. If not, assume that it can be inserted at point.
  11559. HOW is an indicator what kind of note should be created.
  11560. EXTRA is additional text that will be inserted into the notes buffer."
  11561. (let* ((org-log-into-drawer (org-log-into-drawer))
  11562. (drawer (cond ((stringp org-log-into-drawer)
  11563. org-log-into-drawer)
  11564. (org-log-into-drawer "LOGBOOK"))))
  11565. (save-restriction
  11566. (save-excursion
  11567. (when findpos
  11568. (org-back-to-heading t)
  11569. (narrow-to-region (point) (save-excursion
  11570. (outline-next-heading) (point)))
  11571. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11572. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11573. "[^\r\n]*\\)?"))
  11574. (goto-char (match-end 0))
  11575. (cond
  11576. (drawer
  11577. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11578. nil t)
  11579. (progn
  11580. (goto-char (match-end 0))
  11581. (or org-log-states-order-reversed
  11582. (and (re-search-forward org-property-end-re nil t)
  11583. (goto-char (1- (match-beginning 0))))))
  11584. (insert "\n:" drawer ":\n:END:")
  11585. (beginning-of-line 0)
  11586. (org-indent-line)
  11587. (beginning-of-line 2)
  11588. (org-indent-line)
  11589. (end-of-line 0)))
  11590. ((and org-log-state-notes-insert-after-drawers
  11591. (save-excursion
  11592. (forward-line) (looking-at org-drawer-regexp)))
  11593. (forward-line)
  11594. (while (looking-at org-drawer-regexp)
  11595. (goto-char (match-end 0))
  11596. (re-search-forward org-property-end-re (point-max) t)
  11597. (forward-line))
  11598. (forward-line -1)))
  11599. (unless org-log-states-order-reversed
  11600. (and (= (char-after) ?\n) (forward-char 1))
  11601. (org-skip-over-state-notes)
  11602. (skip-chars-backward " \t\n\r")))
  11603. (move-marker org-log-note-marker (point))
  11604. (setq org-log-note-purpose purpose
  11605. org-log-note-state state
  11606. org-log-note-previous-state prev-state
  11607. org-log-note-how how
  11608. org-log-note-extra extra
  11609. org-log-note-effective-time (org-current-effective-time))
  11610. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11611. (defun org-skip-over-state-notes ()
  11612. "Skip past the list of State notes in an entry."
  11613. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11614. (when (ignore-errors (goto-char (org-in-item-p)))
  11615. (let* ((struct (org-list-struct))
  11616. (prevs (org-list-prevs-alist struct)))
  11617. (while (looking-at "[ \t]*- State")
  11618. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11619. (org-list-get-item-end (point) struct)))))))
  11620. (defun org-add-log-note (&optional purpose)
  11621. "Pop up a window for taking a note, and add this note later at point."
  11622. (remove-hook 'post-command-hook 'org-add-log-note)
  11623. (setq org-log-note-window-configuration (current-window-configuration))
  11624. (delete-other-windows)
  11625. (move-marker org-log-note-return-to (point))
  11626. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11627. (goto-char org-log-note-marker)
  11628. (org-switch-to-buffer-other-window "*Org Note*")
  11629. (erase-buffer)
  11630. (if (memq org-log-note-how '(time state))
  11631. (let (current-prefix-arg) (org-store-log-note))
  11632. (let ((org-inhibit-startup t)) (org-mode))
  11633. (insert (format "# Insert note for %s.
  11634. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11635. (cond
  11636. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11637. ((eq org-log-note-purpose 'done) "closed todo item")
  11638. ((eq org-log-note-purpose 'state)
  11639. (format "state change from \"%s\" to \"%s\""
  11640. (or org-log-note-previous-state "")
  11641. (or org-log-note-state "")))
  11642. ((eq org-log-note-purpose 'reschedule)
  11643. "rescheduling")
  11644. ((eq org-log-note-purpose 'delschedule)
  11645. "no longer scheduled")
  11646. ((eq org-log-note-purpose 'redeadline)
  11647. "changing deadline")
  11648. ((eq org-log-note-purpose 'deldeadline)
  11649. "removing deadline")
  11650. ((eq org-log-note-purpose 'refile)
  11651. "refiling")
  11652. ((eq org-log-note-purpose 'note)
  11653. "this entry")
  11654. (t (error "This should not happen")))))
  11655. (if org-log-note-extra (insert org-log-note-extra))
  11656. (org-set-local 'org-finish-function 'org-store-log-note)
  11657. (run-hooks 'org-log-buffer-setup-hook)))
  11658. (defvar org-note-abort nil) ; dynamically scoped
  11659. (defun org-store-log-note ()
  11660. "Finish taking a log note, and insert it to where it belongs."
  11661. (let ((txt (buffer-string)))
  11662. (kill-buffer (current-buffer))
  11663. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11664. lines ind bul)
  11665. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11666. (setq txt (replace-match "" t t txt)))
  11667. (if (string-match "\\s-+\\'" txt)
  11668. (setq txt (replace-match "" t t txt)))
  11669. (setq lines (org-split-string txt "\n"))
  11670. (when (and note (string-match "\\S-" note))
  11671. (setq note
  11672. (org-replace-escapes
  11673. note
  11674. (list (cons "%u" (user-login-name))
  11675. (cons "%U" user-full-name)
  11676. (cons "%t" (format-time-string
  11677. (org-time-stamp-format 'long 'inactive)
  11678. org-log-note-effective-time))
  11679. (cons "%T" (format-time-string
  11680. (org-time-stamp-format 'long nil)
  11681. org-log-note-effective-time))
  11682. (cons "%d" (format-time-string
  11683. (org-time-stamp-format nil 'inactive)
  11684. org-log-note-effective-time))
  11685. (cons "%D" (format-time-string
  11686. (org-time-stamp-format nil nil)
  11687. org-log-note-effective-time))
  11688. (cons "%s" (if org-log-note-state
  11689. (concat "\"" org-log-note-state "\"")
  11690. ""))
  11691. (cons "%S" (if org-log-note-previous-state
  11692. (concat "\"" org-log-note-previous-state "\"")
  11693. "\"\"")))))
  11694. (if lines (setq note (concat note " \\\\")))
  11695. (push note lines))
  11696. (when (or current-prefix-arg org-note-abort)
  11697. (when org-log-into-drawer
  11698. (org-remove-empty-drawer-at
  11699. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11700. org-log-note-marker))
  11701. (setq lines nil))
  11702. (when lines
  11703. (with-current-buffer (marker-buffer org-log-note-marker)
  11704. (save-excursion
  11705. (goto-char org-log-note-marker)
  11706. (move-marker org-log-note-marker nil)
  11707. (end-of-line 1)
  11708. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11709. (setq ind (save-excursion
  11710. (if (ignore-errors (goto-char (org-in-item-p)))
  11711. (let ((struct (org-list-struct)))
  11712. (org-list-get-ind
  11713. (org-list-get-top-point struct) struct))
  11714. (skip-chars-backward " \r\t\n")
  11715. (cond
  11716. ((and (org-at-heading-p)
  11717. org-adapt-indentation)
  11718. (1+ (org-current-level)))
  11719. ((org-at-heading-p) 0)
  11720. (t (org-get-indentation))))))
  11721. (setq bul (org-list-bullet-string "-"))
  11722. (org-indent-line-to ind)
  11723. (insert bul (pop lines))
  11724. (let ((ind-body (+ (length bul) ind)))
  11725. (while lines
  11726. (insert "\n")
  11727. (org-indent-line-to ind-body)
  11728. (insert (pop lines))))
  11729. (message "Note stored")
  11730. (org-back-to-heading t)
  11731. (org-cycle-hide-drawers 'children))))))
  11732. (set-window-configuration org-log-note-window-configuration)
  11733. (with-current-buffer (marker-buffer org-log-note-return-to)
  11734. (goto-char org-log-note-return-to))
  11735. (move-marker org-log-note-return-to nil)
  11736. (and org-log-post-message (message "%s" org-log-post-message)))
  11737. (defun org-remove-empty-drawer-at (drawer pos)
  11738. "Remove an empty drawer DRAWER at position POS.
  11739. POS may also be a marker."
  11740. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11741. (save-excursion
  11742. (save-restriction
  11743. (widen)
  11744. (goto-char pos)
  11745. (if (org-in-regexp
  11746. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11747. (replace-match ""))))))
  11748. (defvar org-ts-type nil)
  11749. (defun org-sparse-tree (&optional arg type)
  11750. "Create a sparse tree, prompt for the details.
  11751. This command can create sparse trees. You first need to select the type
  11752. of match used to create the tree:
  11753. t Show all TODO entries.
  11754. T Show entries with a specific TODO keyword.
  11755. m Show entries selected by a tags/property match.
  11756. p Enter a property name and its value (both with completion on existing
  11757. names/values) and show entries with that property.
  11758. r Show entries matching a regular expression (`/' can be used as well).
  11759. b Show deadlines and scheduled items before a date.
  11760. a Show deadlines and scheduled items after a date.
  11761. d Show deadlines due within `org-deadline-warning-days'.
  11762. D Show deadlines and scheduled items between a date range."
  11763. (interactive "P")
  11764. (let (ans kwd value ts-type)
  11765. (setq type (or type org-sparse-tree-default-date-type))
  11766. (setq org-ts-type type)
  11767. (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"
  11768. (cond ((eq type 'all) "all timestamps")
  11769. ((eq type 'scheduled) "only scheduled")
  11770. ((eq type 'deadline) "only deadline")
  11771. ((eq type 'active) "only active timestamps")
  11772. ((eq type 'inactive) "only inactive timestamps")
  11773. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11774. (t "scheduled/deadline")))
  11775. (setq ans (read-char-exclusive))
  11776. (cond
  11777. ((equal ans ?c)
  11778. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11779. ((equal ans ?d)
  11780. (call-interactively 'org-check-deadlines))
  11781. ((equal ans ?b)
  11782. (call-interactively 'org-check-before-date))
  11783. ((equal ans ?a)
  11784. (call-interactively 'org-check-after-date))
  11785. ((equal ans ?D)
  11786. (call-interactively 'org-check-dates-range))
  11787. ((equal ans ?t)
  11788. (call-interactively 'org-show-todo-tree))
  11789. ((equal ans ?T)
  11790. (org-show-todo-tree '(4)))
  11791. ((member ans '(?T ?m))
  11792. (call-interactively 'org-match-sparse-tree))
  11793. ((member ans '(?p ?P))
  11794. (setq kwd (org-icompleting-read "Property: "
  11795. (mapcar 'list (org-buffer-property-keys))))
  11796. (setq value (org-icompleting-read "Value: "
  11797. (mapcar 'list (org-property-values kwd))))
  11798. (unless (string-match "\\`{.*}\\'" value)
  11799. (setq value (concat "\"" value "\"")))
  11800. (org-match-sparse-tree arg (concat kwd "=" value)))
  11801. ((member ans '(?r ?R ?/))
  11802. (call-interactively 'org-occur))
  11803. (t (error "No such sparse tree command \"%c\"" ans)))))
  11804. (defvar org-occur-highlights nil
  11805. "List of overlays used for occur matches.")
  11806. (make-variable-buffer-local 'org-occur-highlights)
  11807. (defvar org-occur-parameters nil
  11808. "Parameters of the active org-occur calls.
  11809. This is a list, each call to org-occur pushes as cons cell,
  11810. containing the regular expression and the callback, onto the list.
  11811. The list can contain several entries if `org-occur' has been called
  11812. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11813. will only contain one set of parameters. When the highlights are
  11814. removed (for example with `C-c C-c', or with the next edit (depending
  11815. on `org-remove-highlights-with-change'), this variable is emptied
  11816. as well.")
  11817. (make-variable-buffer-local 'org-occur-parameters)
  11818. (defun org-occur (regexp &optional keep-previous callback)
  11819. "Make a compact tree which shows all matches of REGEXP.
  11820. The tree will show the lines where the regexp matches, and all higher
  11821. headlines above the match. It will also show the heading after the match,
  11822. to make sure editing the matching entry is easy.
  11823. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11824. call to `org-occur' will be kept, to allow stacking of calls to this
  11825. command.
  11826. If CALLBACK is non-nil, it is a function which is called to confirm
  11827. that the match should indeed be shown."
  11828. (interactive "sRegexp: \nP")
  11829. (when (equal regexp "")
  11830. (error "Regexp cannot be empty"))
  11831. (unless keep-previous
  11832. (org-remove-occur-highlights nil nil t))
  11833. (push (cons regexp callback) org-occur-parameters)
  11834. (let ((cnt 0))
  11835. (save-excursion
  11836. (goto-char (point-min))
  11837. (if (or (not keep-previous) ; do not want to keep
  11838. (not org-occur-highlights)) ; no previous matches
  11839. ;; hide everything
  11840. (org-overview))
  11841. (while (re-search-forward regexp nil t)
  11842. (when (or (not callback)
  11843. (save-match-data (funcall callback)))
  11844. (setq cnt (1+ cnt))
  11845. (when org-highlight-sparse-tree-matches
  11846. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11847. (org-show-context 'occur-tree))))
  11848. (when org-remove-highlights-with-change
  11849. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11850. nil 'local))
  11851. (unless org-sparse-tree-open-archived-trees
  11852. (org-hide-archived-subtrees (point-min) (point-max)))
  11853. (run-hooks 'org-occur-hook)
  11854. (if (org-called-interactively-p 'interactive)
  11855. (message "%d match(es) for regexp %s" cnt regexp))
  11856. cnt))
  11857. (defun org-occur-next-match (&optional n reset)
  11858. "Function for `next-error-function' to find sparse tree matches.
  11859. N is the number of matches to move, when negative move backwards.
  11860. RESET is entirely ignored - this function always goes back to the
  11861. starting point when no match is found."
  11862. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11863. (search-func (if (< n 0)
  11864. 'previous-single-char-property-change
  11865. 'next-single-char-property-change))
  11866. (n (abs n))
  11867. (pos (point))
  11868. p1)
  11869. (catch 'exit
  11870. (while (setq p1 (funcall search-func (point) 'org-type))
  11871. (when (equal p1 limit)
  11872. (goto-char pos)
  11873. (error "No more matches"))
  11874. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11875. (setq n (1- n))
  11876. (when (= n 0)
  11877. (goto-char p1)
  11878. (throw 'exit (point))))
  11879. (goto-char p1))
  11880. (goto-char p1)
  11881. (error "No more matches"))))
  11882. (defun org-show-context (&optional key)
  11883. "Make sure point and context are visible.
  11884. How much context is shown depends upon the variables
  11885. `org-show-hierarchy-above', `org-show-following-heading',
  11886. `org-show-entry-below' and `org-show-siblings'."
  11887. (let ((heading-p (org-at-heading-p t))
  11888. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11889. (following-p (org-get-alist-option org-show-following-heading key))
  11890. (entry-p (org-get-alist-option org-show-entry-below key))
  11891. (siblings-p (org-get-alist-option org-show-siblings key)))
  11892. (catch 'exit
  11893. ;; Show heading or entry text
  11894. (if (and heading-p (not entry-p))
  11895. (org-flag-heading nil) ; only show the heading
  11896. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11897. (org-show-hidden-entry))) ; show entire entry
  11898. (when following-p
  11899. ;; Show next sibling, or heading below text
  11900. (save-excursion
  11901. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11902. (org-flag-heading nil))))
  11903. (when siblings-p (org-show-siblings))
  11904. (when hierarchy-p
  11905. ;; show all higher headings, possibly with siblings
  11906. (save-excursion
  11907. (while (and (condition-case nil
  11908. (progn (org-up-heading-all 1) t)
  11909. (error nil))
  11910. (not (bobp)))
  11911. (org-flag-heading nil)
  11912. (when siblings-p (org-show-siblings))))))))
  11913. (defvar org-reveal-start-hook nil
  11914. "Hook run before revealing a location.")
  11915. (defun org-reveal (&optional siblings)
  11916. "Show current entry, hierarchy above it, and the following headline.
  11917. This can be used to show a consistent set of context around locations
  11918. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11919. not t for the search context.
  11920. With optional argument SIBLINGS, on each level of the hierarchy all
  11921. siblings are shown. This repairs the tree structure to what it would
  11922. look like when opened with hierarchical calls to `org-cycle'.
  11923. With double optional argument \\[universal-argument] \\[universal-argument], \
  11924. go to the parent and show the
  11925. entire tree."
  11926. (interactive "P")
  11927. (run-hooks 'org-reveal-start-hook)
  11928. (let ((org-show-hierarchy-above t)
  11929. (org-show-following-heading t)
  11930. (org-show-siblings (if siblings t org-show-siblings)))
  11931. (org-show-context nil))
  11932. (when (equal siblings '(16))
  11933. (save-excursion
  11934. (when (org-up-heading-safe)
  11935. (org-show-subtree)
  11936. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11937. (defun org-highlight-new-match (beg end)
  11938. "Highlight from BEG to END and mark the highlight is an occur headline."
  11939. (let ((ov (make-overlay beg end)))
  11940. (overlay-put ov 'face 'secondary-selection)
  11941. (overlay-put ov 'org-type 'org-occur)
  11942. (push ov org-occur-highlights)))
  11943. (defun org-remove-occur-highlights (&optional beg end noremove)
  11944. "Remove the occur highlights from the buffer.
  11945. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11946. from the `before-change-functions' in the current buffer."
  11947. (interactive)
  11948. (unless org-inhibit-highlight-removal
  11949. (mapc 'delete-overlay org-occur-highlights)
  11950. (setq org-occur-highlights nil)
  11951. (setq org-occur-parameters nil)
  11952. (unless noremove
  11953. (remove-hook 'before-change-functions
  11954. 'org-remove-occur-highlights 'local))))
  11955. ;;;; Priorities
  11956. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11957. "Regular expression matching the priority indicator.")
  11958. (defvar org-remove-priority-next-time nil)
  11959. (defun org-priority-up ()
  11960. "Increase the priority of the current item."
  11961. (interactive)
  11962. (org-priority 'up))
  11963. (defun org-priority-down ()
  11964. "Decrease the priority of the current item."
  11965. (interactive)
  11966. (org-priority 'down))
  11967. (defun org-priority (&optional action show)
  11968. "Change the priority of an item.
  11969. ACTION can be `set', `up', `down', or a character."
  11970. (interactive "P")
  11971. (if (equal action '(4))
  11972. (org-show-priority)
  11973. (unless org-enable-priority-commands
  11974. (error "Priority commands are disabled"))
  11975. (setq action (or action 'set))
  11976. (let (current new news have remove)
  11977. (save-excursion
  11978. (org-back-to-heading t)
  11979. (if (looking-at org-priority-regexp)
  11980. (setq current (string-to-char (match-string 2))
  11981. have t))
  11982. (cond
  11983. ((eq action 'remove)
  11984. (setq remove t new ?\ ))
  11985. ((or (eq action 'set)
  11986. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11987. (if (not (eq action 'set))
  11988. (setq new action)
  11989. (message "Priority %c-%c, SPC to remove: "
  11990. org-highest-priority org-lowest-priority)
  11991. (save-match-data
  11992. (setq new (read-char-exclusive))))
  11993. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11994. (= (upcase org-lowest-priority) org-lowest-priority))
  11995. (setq new (upcase new)))
  11996. (cond ((equal new ?\ ) (setq remove t))
  11997. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11998. (error "Priority must be between `%c' and `%c'"
  11999. org-highest-priority org-lowest-priority))))
  12000. ((eq action 'up)
  12001. (setq new (if have
  12002. (1- current) ; normal cycling
  12003. ;; last priority was empty
  12004. (if (eq last-command this-command)
  12005. org-lowest-priority ; wrap around empty to lowest
  12006. ;; default
  12007. (if org-priority-start-cycle-with-default
  12008. org-default-priority
  12009. (1- org-default-priority))))))
  12010. ((eq action 'down)
  12011. (setq new (if have
  12012. (1+ current) ; normal cycling
  12013. ;; last priority was empty
  12014. (if (eq last-command this-command)
  12015. org-highest-priority ; wrap around empty to highest
  12016. ;; default
  12017. (if org-priority-start-cycle-with-default
  12018. org-default-priority
  12019. (1+ org-default-priority))))))
  12020. (t (error "Invalid action")))
  12021. (if (or (< (upcase new) org-highest-priority)
  12022. (> (upcase new) org-lowest-priority))
  12023. (if (and (memq action '(up down))
  12024. (not have) (not (eq last-command this-command)))
  12025. ;; `new' is from default priority
  12026. (error
  12027. "The default can not be set, see `org-default-priority' why")
  12028. ;; normal cycling: `new' is beyond highest/lowest priority
  12029. ;; and is wrapped around to the empty priority
  12030. (setq remove t)))
  12031. (setq news (format "%c" new))
  12032. (if have
  12033. (if remove
  12034. (replace-match "" t t nil 1)
  12035. (replace-match news t t nil 2))
  12036. (if remove
  12037. (error "No priority cookie found in line")
  12038. (let ((case-fold-search nil))
  12039. (looking-at org-todo-line-regexp))
  12040. (if (match-end 2)
  12041. (progn
  12042. (goto-char (match-end 2))
  12043. (insert " [#" news "]"))
  12044. (goto-char (match-beginning 3))
  12045. (insert "[#" news "] "))))
  12046. (org-preserve-lc (org-set-tags nil 'align)))
  12047. (if remove
  12048. (message "Priority removed")
  12049. (message "Priority of current item set to %s" news)))))
  12050. (defun org-show-priority ()
  12051. "Show the priority of the current item.
  12052. This priority is composed of the main priority given with the [#A] cookies,
  12053. and by additional input from the age of a schedules or deadline entry."
  12054. (interactive)
  12055. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12056. (org-get-at-bol 'priority)
  12057. (save-excursion
  12058. (save-match-data
  12059. (beginning-of-line)
  12060. (and (looking-at org-heading-regexp)
  12061. (org-get-priority (match-string 0))))))))
  12062. (message "Priority is %d" (if pri pri -1000))))
  12063. (defun org-get-priority (s)
  12064. "Find priority cookie and return priority."
  12065. (if (functionp org-get-priority-function)
  12066. (funcall org-get-priority-function)
  12067. (save-match-data
  12068. (if (not (string-match org-priority-regexp s))
  12069. (* 1000 (- org-lowest-priority org-default-priority))
  12070. (* 1000 (- org-lowest-priority
  12071. (string-to-char (match-string 2 s))))))))
  12072. ;;;; Tags
  12073. (defvar org-agenda-archives-mode)
  12074. (defvar org-map-continue-from nil
  12075. "Position from where mapping should continue.
  12076. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12077. (defvar org-scanner-tags nil
  12078. "The current tag list while the tags scanner is running.")
  12079. (defvar org-trust-scanner-tags nil
  12080. "Should `org-get-tags-at' use the tags for the scanner.
  12081. This is for internal dynamical scoping only.
  12082. When this is non-nil, the function `org-get-tags-at' will return the value
  12083. of `org-scanner-tags' instead of building the list by itself. This
  12084. can lead to large speed-ups when the tags scanner is used in a file with
  12085. many entries, and when the list of tags is retrieved, for example to
  12086. obtain a list of properties. Building the tags list for each entry in such
  12087. a file becomes an N^2 operation - but with this variable set, it scales
  12088. as N.")
  12089. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12090. "Scan headline tags with inheritance and produce output ACTION.
  12091. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12092. or `agenda' to produce an entry list for an agenda view. It can also be
  12093. a Lisp form or a function that should be called at each matched headline, in
  12094. this case the return value is a list of all return values from these calls.
  12095. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12096. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12097. only lines with a not-done TODO keyword are included in the output.
  12098. This should be the same variable that was scoped into
  12099. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12100. START-LEVEL can be a string with asterisks, reducing the scope to
  12101. headlines matching this string."
  12102. (require 'org-agenda)
  12103. (let* ((re (concat "^"
  12104. (if start-level
  12105. ;; Get the correct level to match
  12106. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12107. org-outline-regexp)
  12108. " *\\(\\<\\("
  12109. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12110. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12111. (props (list 'face 'default
  12112. 'done-face 'org-agenda-done
  12113. 'undone-face 'default
  12114. 'mouse-face 'highlight
  12115. 'org-not-done-regexp org-not-done-regexp
  12116. 'org-todo-regexp org-todo-regexp
  12117. 'org-complex-heading-regexp org-complex-heading-regexp
  12118. 'help-echo
  12119. (format "mouse-2 or RET jump to org file %s"
  12120. (abbreviate-file-name
  12121. (or (buffer-file-name (buffer-base-buffer))
  12122. (buffer-name (buffer-base-buffer)))))))
  12123. (case-fold-search nil)
  12124. (org-map-continue-from nil)
  12125. lspos tags tags-list
  12126. (tags-alist (list (cons 0 org-file-tags)))
  12127. (llast 0) rtn rtn1 level category i txt
  12128. todo marker entry priority)
  12129. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12130. (setq action (list 'lambda nil action)))
  12131. (save-excursion
  12132. (goto-char (point-min))
  12133. (when (eq action 'sparse-tree)
  12134. (org-overview)
  12135. (org-remove-occur-highlights))
  12136. (while (re-search-forward re nil t)
  12137. (setq org-map-continue-from nil)
  12138. (catch :skip
  12139. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12140. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12141. (goto-char (setq lspos (match-beginning 0)))
  12142. (setq level (org-reduced-level (org-outline-level))
  12143. category (org-get-category))
  12144. (setq i llast llast level)
  12145. ;; remove tag lists from same and sublevels
  12146. (while (>= i level)
  12147. (when (setq entry (assoc i tags-alist))
  12148. (setq tags-alist (delete entry tags-alist)))
  12149. (setq i (1- i)))
  12150. ;; add the next tags
  12151. (when tags
  12152. (setq tags (org-split-string tags ":")
  12153. tags-alist
  12154. (cons (cons level tags) tags-alist)))
  12155. ;; compile tags for current headline
  12156. (setq tags-list
  12157. (if org-use-tag-inheritance
  12158. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12159. tags)
  12160. org-scanner-tags tags-list)
  12161. (when org-use-tag-inheritance
  12162. (setcdr (car tags-alist)
  12163. (mapcar (lambda (x)
  12164. (setq x (copy-sequence x))
  12165. (org-add-prop-inherited x))
  12166. (cdar tags-alist))))
  12167. (when (and tags org-use-tag-inheritance
  12168. (or (not (eq t org-use-tag-inheritance))
  12169. org-tags-exclude-from-inheritance))
  12170. ;; selective inheritance, remove uninherited ones
  12171. (setcdr (car tags-alist)
  12172. (org-remove-uninherited-tags (cdar tags-alist))))
  12173. (when (and
  12174. ;; eval matcher only when the todo condition is OK
  12175. (and (or (not todo-only) (member todo org-not-done-keywords))
  12176. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12177. (eval matcher)))
  12178. ;; Call the skipper, but return t if it does not skip,
  12179. ;; so that the `and' form continues evaluating
  12180. (progn
  12181. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12182. t)
  12183. ;; Check if timestamps are deselecting this entry
  12184. (or (not todo-only)
  12185. (and (member todo org-not-done-keywords)
  12186. (or (not org-agenda-tags-todo-honor-ignore-options)
  12187. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  12188. ;; Extra check for the archive tag
  12189. ;; FIXME: Does the skipper already do this????
  12190. (or
  12191. (not (member org-archive-tag tags-list))
  12192. ;; we have an archive tag, should we use this anyway?
  12193. (or (not org-agenda-skip-archived-trees)
  12194. (and (eq action 'agenda) org-agenda-archives-mode))))
  12195. ;; select this headline
  12196. (cond
  12197. ((eq action 'sparse-tree)
  12198. (and org-highlight-sparse-tree-matches
  12199. (org-get-heading) (match-end 0)
  12200. (org-highlight-new-match
  12201. (match-beginning 1) (match-end 1)))
  12202. (org-show-context 'tags-tree))
  12203. ((eq action 'agenda)
  12204. (setq txt (org-agenda-format-item
  12205. ""
  12206. (concat
  12207. (if (eq org-tags-match-list-sublevels 'indented)
  12208. (make-string (1- level) ?.) "")
  12209. (org-get-heading))
  12210. level category
  12211. tags-list)
  12212. priority (org-get-priority txt))
  12213. (goto-char lspos)
  12214. (setq marker (org-agenda-new-marker))
  12215. (org-add-props txt props
  12216. 'org-marker marker 'org-hd-marker marker 'org-category category
  12217. 'todo-state todo
  12218. 'priority priority 'type "tagsmatch")
  12219. (push txt rtn))
  12220. ((functionp action)
  12221. (setq org-map-continue-from nil)
  12222. (save-excursion
  12223. (setq rtn1 (funcall action))
  12224. (push rtn1 rtn)))
  12225. (t (error "Invalid action")))
  12226. ;; if we are to skip sublevels, jump to end of subtree
  12227. (unless org-tags-match-list-sublevels
  12228. (org-end-of-subtree t)
  12229. (backward-char 1))))
  12230. ;; Get the correct position from where to continue
  12231. (if org-map-continue-from
  12232. (goto-char org-map-continue-from)
  12233. (and (= (point) lspos) (end-of-line 1)))))
  12234. (when (and (eq action 'sparse-tree)
  12235. (not org-sparse-tree-open-archived-trees))
  12236. (org-hide-archived-subtrees (point-min) (point-max)))
  12237. (nreverse rtn)))
  12238. (defun org-remove-uninherited-tags (tags)
  12239. "Remove all tags that are not inherited from the list TAGS."
  12240. (cond
  12241. ((eq org-use-tag-inheritance t)
  12242. (if org-tags-exclude-from-inheritance
  12243. (org-delete-all org-tags-exclude-from-inheritance tags)
  12244. tags))
  12245. ((not org-use-tag-inheritance) nil)
  12246. ((stringp org-use-tag-inheritance)
  12247. (delq nil (mapcar
  12248. (lambda (x)
  12249. (if (and (string-match org-use-tag-inheritance x)
  12250. (not (member x org-tags-exclude-from-inheritance)))
  12251. x nil))
  12252. tags)))
  12253. ((listp org-use-tag-inheritance)
  12254. (delq nil (mapcar
  12255. (lambda (x)
  12256. (if (member x org-use-tag-inheritance) x nil))
  12257. tags)))))
  12258. (defun org-match-sparse-tree (&optional todo-only match)
  12259. "Create a sparse tree according to tags string MATCH.
  12260. MATCH can contain positive and negative selection of tags, like
  12261. \"+WORK+URGENT-WITHBOSS\".
  12262. If optional argument TODO-ONLY is non-nil, only select lines that are
  12263. also TODO lines."
  12264. (interactive "P")
  12265. (org-agenda-prepare-buffers (list (current-buffer)))
  12266. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12267. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12268. (defvar org-cached-props nil)
  12269. (defun org-cached-entry-get (pom property)
  12270. (if (or (eq t org-use-property-inheritance)
  12271. (and (stringp org-use-property-inheritance)
  12272. (string-match org-use-property-inheritance property))
  12273. (and (listp org-use-property-inheritance)
  12274. (member property org-use-property-inheritance)))
  12275. ;; Caching is not possible, check it directly
  12276. (org-entry-get pom property 'inherit)
  12277. ;; Get all properties, so that we can do complicated checks easily
  12278. (cdr (assoc property (or org-cached-props
  12279. (setq org-cached-props
  12280. (org-entry-properties pom)))))))
  12281. (defun org-global-tags-completion-table (&optional files)
  12282. "Return the list of all tags in all agenda buffer/files.
  12283. Optional FILES argument is a list of files which can be used
  12284. instead of the agenda files."
  12285. (save-excursion
  12286. (org-uniquify
  12287. (delq nil
  12288. (apply 'append
  12289. (mapcar
  12290. (lambda (file)
  12291. (set-buffer (find-file-noselect file))
  12292. (append (org-get-buffer-tags)
  12293. (mapcar (lambda (x) (if (stringp (car-safe x))
  12294. (list (car-safe x)) nil))
  12295. org-tag-alist)))
  12296. (if (and files (car files))
  12297. files
  12298. (org-agenda-files))))))))
  12299. (defun org-make-tags-matcher (match)
  12300. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12301. The variable `todo-only' is scoped dynamically into this function.
  12302. It will be set to t if the matcher restricts matching to TODO entries,
  12303. otherwise will not be touched.
  12304. Returns a cons of the selection string MATCH and the constructed
  12305. lisp form implementing the matcher. The matcher is to be evaluated
  12306. at an Org entry, with point on the headline, and returns t if the
  12307. entry matches the selection string MATCH. The returned lisp form
  12308. references two variables with information about the entry, which
  12309. must be bound around the form's evaluation: todo, the TODO keyword
  12310. at the entry (or nil of none); and tags-list, the list of all tags
  12311. at the entry including inherited ones. Additionally, the category
  12312. of the entry (if any) must be specified as the text property
  12313. 'org-category on the headline.
  12314. See also `org-scan-tags'.
  12315. "
  12316. (declare (special todo-only))
  12317. (unless (boundp 'todo-only)
  12318. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12319. (unless match
  12320. ;; Get a new match request, with completion
  12321. (let ((org-last-tags-completion-table
  12322. (org-global-tags-completion-table)))
  12323. (setq match (org-completing-read-no-i
  12324. "Match: " 'org-tags-completion-function nil nil nil
  12325. 'org-tags-history))))
  12326. ;; Parse the string and create a lisp form
  12327. (let ((match0 match)
  12328. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12329. minus tag mm
  12330. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12331. orterms term orlist re-p str-p level-p level-op time-p
  12332. prop-p pn pv po gv rest)
  12333. (if (string-match "/+" match)
  12334. ;; match contains also a todo-matching request
  12335. (progn
  12336. (setq tagsmatch (substring match 0 (match-beginning 0))
  12337. todomatch (substring match (match-end 0)))
  12338. (if (string-match "^!" todomatch)
  12339. (setq todo-only t todomatch (substring todomatch 1)))
  12340. (if (string-match "^\\s-*$" todomatch)
  12341. (setq todomatch nil)))
  12342. ;; only matching tags
  12343. (setq tagsmatch match todomatch nil))
  12344. ;; Make the tags matcher
  12345. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12346. (setq tagsmatcher t)
  12347. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12348. (while (setq term (pop orterms))
  12349. (while (and (equal (substring term -1) "\\") orterms)
  12350. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12351. (while (string-match re term)
  12352. (setq rest (substring term (match-end 0))
  12353. minus (and (match-end 1)
  12354. (equal (match-string 1 term) "-"))
  12355. tag (save-match-data (replace-regexp-in-string
  12356. "\\\\-" "-"
  12357. (match-string 2 term)))
  12358. re-p (equal (string-to-char tag) ?{)
  12359. level-p (match-end 4)
  12360. prop-p (match-end 5)
  12361. mm (cond
  12362. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12363. (level-p
  12364. (setq level-op (org-op-to-function (match-string 3 term)))
  12365. `(,level-op level ,(string-to-number
  12366. (match-string 4 term))))
  12367. (prop-p
  12368. (setq pn (match-string 5 term)
  12369. po (match-string 6 term)
  12370. pv (match-string 7 term)
  12371. re-p (equal (string-to-char pv) ?{)
  12372. str-p (equal (string-to-char pv) ?\")
  12373. time-p (save-match-data
  12374. (string-match "^\"[[<].*[]>]\"$" pv))
  12375. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12376. (if time-p (setq pv (org-matcher-time pv)))
  12377. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12378. (cond
  12379. ((equal pn "CATEGORY")
  12380. (setq gv '(get-text-property (point) 'org-category)))
  12381. ((equal pn "TODO")
  12382. (setq gv 'todo))
  12383. (t
  12384. (setq gv `(org-cached-entry-get nil ,pn))))
  12385. (if re-p
  12386. (if (eq po 'org<>)
  12387. `(not (string-match ,pv (or ,gv "")))
  12388. `(string-match ,pv (or ,gv "")))
  12389. (if str-p
  12390. `(,po (or ,gv "") ,pv)
  12391. `(,po (string-to-number (or ,gv ""))
  12392. ,(string-to-number pv) ))))
  12393. (t `(member ,tag tags-list)))
  12394. mm (if minus (list 'not mm) mm)
  12395. term rest)
  12396. (push mm tagsmatcher))
  12397. (push (if (> (length tagsmatcher) 1)
  12398. (cons 'and tagsmatcher)
  12399. (car tagsmatcher))
  12400. orlist)
  12401. (setq tagsmatcher nil))
  12402. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12403. (setq tagsmatcher
  12404. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12405. ;; Make the todo matcher
  12406. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12407. (setq todomatcher t)
  12408. (setq orterms (org-split-string todomatch "|") orlist nil)
  12409. (while (setq term (pop orterms))
  12410. (while (string-match re term)
  12411. (setq minus (and (match-end 1)
  12412. (equal (match-string 1 term) "-"))
  12413. kwd (match-string 2 term)
  12414. re-p (equal (string-to-char kwd) ?{)
  12415. term (substring term (match-end 0))
  12416. mm (if re-p
  12417. `(string-match ,(substring kwd 1 -1) todo)
  12418. (list 'equal 'todo kwd))
  12419. mm (if minus (list 'not mm) mm))
  12420. (push mm todomatcher))
  12421. (push (if (> (length todomatcher) 1)
  12422. (cons 'and todomatcher)
  12423. (car todomatcher))
  12424. orlist)
  12425. (setq todomatcher nil))
  12426. (setq todomatcher (if (> (length orlist) 1)
  12427. (cons 'or orlist) (car orlist))))
  12428. ;; Return the string and lisp forms of the matcher
  12429. (setq matcher (if todomatcher
  12430. (list 'and tagsmatcher todomatcher)
  12431. tagsmatcher))
  12432. (when todo-only
  12433. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12434. matcher)))
  12435. (cons match0 matcher)))
  12436. (defun org-op-to-function (op &optional stringp)
  12437. "Turn an operator into the appropriate function."
  12438. (setq op
  12439. (cond
  12440. ((equal op "<" ) '(< string< org-time<))
  12441. ((equal op ">" ) '(> org-string> org-time>))
  12442. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12443. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12444. ((member op '("=" "==")) '(= string= org-time=))
  12445. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12446. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12447. (defun org<> (a b) (not (= a b)))
  12448. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12449. (defun org-string>= (a b) (not (string< a b)))
  12450. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12451. (defun org-string<> (a b) (not (string= a b)))
  12452. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12453. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12454. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12455. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12456. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12457. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12458. (defun org-2ft (s)
  12459. "Convert S to a floating point time.
  12460. If S is already a number, just return it. If it is a string, parse
  12461. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12462. (cond
  12463. ((numberp s) s)
  12464. ((stringp s)
  12465. (condition-case nil
  12466. (float-time (apply 'encode-time (org-parse-time-string s)))
  12467. (error 0.)))
  12468. (t 0.)))
  12469. (defun org-time-today ()
  12470. "Time in seconds today at 0:00.
  12471. Returns the float number of seconds since the beginning of the
  12472. epoch to the beginning of today (00:00)."
  12473. (float-time (apply 'encode-time
  12474. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12475. (defun org-matcher-time (s)
  12476. "Interpret a time comparison value."
  12477. (save-match-data
  12478. (cond
  12479. ((string= s "<now>") (float-time))
  12480. ((string= s "<today>") (org-time-today))
  12481. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12482. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12483. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12484. (+ (org-time-today)
  12485. (* (string-to-number (match-string 1 s))
  12486. (cdr (assoc (match-string 2 s)
  12487. '(("d" . 86400.0) ("w" . 604800.0)
  12488. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12489. (t (org-2ft s)))))
  12490. (defun org-match-any-p (re list)
  12491. "Does re match any element of list?"
  12492. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12493. (delq nil list))
  12494. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12495. (defvar org-tags-overlay (make-overlay 1 1))
  12496. (org-detach-overlay org-tags-overlay)
  12497. (defun org-get-local-tags-at (&optional pos)
  12498. "Get a list of tags defined in the current headline."
  12499. (org-get-tags-at pos 'local))
  12500. (defun org-get-local-tags ()
  12501. "Get a list of tags defined in the current headline."
  12502. (org-get-tags-at nil 'local))
  12503. (defun org-get-tags-at (&optional pos local)
  12504. "Get a list of all headline tags applicable at POS.
  12505. POS defaults to point. If tags are inherited, the list contains
  12506. the targets in the same sequence as the headlines appear, i.e.
  12507. the tags of the current headline come last.
  12508. When LOCAL is non-nil, only return tags from the current headline,
  12509. ignore inherited ones."
  12510. (interactive)
  12511. (if (and org-trust-scanner-tags
  12512. (or (not pos) (equal pos (point)))
  12513. (not local))
  12514. org-scanner-tags
  12515. (let (tags ltags lastpos parent)
  12516. (save-excursion
  12517. (save-restriction
  12518. (widen)
  12519. (goto-char (or pos (point)))
  12520. (save-match-data
  12521. (catch 'done
  12522. (condition-case nil
  12523. (progn
  12524. (org-back-to-heading t)
  12525. (while (not (equal lastpos (point)))
  12526. (setq lastpos (point))
  12527. (when (looking-at
  12528. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12529. (setq ltags (org-split-string
  12530. (org-match-string-no-properties 1) ":"))
  12531. (when parent
  12532. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12533. (setq tags (append
  12534. (if parent
  12535. (org-remove-uninherited-tags ltags)
  12536. ltags)
  12537. tags)))
  12538. (or org-use-tag-inheritance (throw 'done t))
  12539. (if local (throw 'done t))
  12540. (or (org-up-heading-safe) (error nil))
  12541. (setq parent t)))
  12542. (error nil)))))
  12543. (if local
  12544. tags
  12545. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12546. (defun org-add-prop-inherited (s)
  12547. (add-text-properties 0 (length s) '(inherited t) s)
  12548. s)
  12549. (defun org-toggle-tag (tag &optional onoff)
  12550. "Toggle the tag TAG for the current line.
  12551. If ONOFF is `on' or `off', don't toggle but set to this state."
  12552. (let (res current)
  12553. (save-excursion
  12554. (org-back-to-heading t)
  12555. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12556. (point-at-eol) t)
  12557. (progn
  12558. (setq current (match-string 1))
  12559. (replace-match ""))
  12560. (setq current ""))
  12561. (setq current (nreverse (org-split-string current ":")))
  12562. (cond
  12563. ((eq onoff 'on)
  12564. (setq res t)
  12565. (or (member tag current) (push tag current)))
  12566. ((eq onoff 'off)
  12567. (or (not (member tag current)) (setq current (delete tag current))))
  12568. (t (if (member tag current)
  12569. (setq current (delete tag current))
  12570. (setq res t)
  12571. (push tag current))))
  12572. (end-of-line 1)
  12573. (if current
  12574. (progn
  12575. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12576. (org-set-tags nil t))
  12577. (delete-horizontal-space))
  12578. (run-hooks 'org-after-tags-change-hook))
  12579. res))
  12580. (defun org-align-tags-here (to-col)
  12581. ;; Assumes that this is a headline
  12582. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12583. (beginning-of-line 1)
  12584. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12585. (< pos (match-beginning 2)))
  12586. (progn
  12587. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12588. (goto-char (match-beginning 1))
  12589. (insert " ")
  12590. (delete-region (point) (1+ (match-beginning 2)))
  12591. (setq ncol (max (current-column)
  12592. (1+ col)
  12593. (if (> to-col 0)
  12594. to-col
  12595. (- (abs to-col) tags-l))))
  12596. (setq p (point))
  12597. (insert (make-string (- ncol (current-column)) ?\ ))
  12598. (setq ncol (current-column))
  12599. (when indent-tabs-mode (tabify p (point-at-eol)))
  12600. (org-move-to-column (min ncol col) t))
  12601. (goto-char pos))))
  12602. (defun org-set-tags-command (&optional arg just-align)
  12603. "Call the set-tags command for the current entry."
  12604. (interactive "P")
  12605. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12606. (org-set-tags arg just-align)
  12607. (save-excursion
  12608. (org-back-to-heading t)
  12609. (org-set-tags arg just-align))))
  12610. (defun org-set-tags-to (data)
  12611. "Set the tags of the current entry to DATA, replacing the current tags.
  12612. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12613. If DATA is nil or the empty string, any tags will be removed."
  12614. (interactive "sTags: ")
  12615. (setq data
  12616. (cond
  12617. ((eq data nil) "")
  12618. ((equal data "") "")
  12619. ((stringp data)
  12620. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12621. ":"))
  12622. ((listp data)
  12623. (concat ":" (mapconcat 'identity data ":") ":"))))
  12624. (when data
  12625. (save-excursion
  12626. (org-back-to-heading t)
  12627. (when (looking-at org-complex-heading-regexp)
  12628. (if (match-end 5)
  12629. (progn
  12630. (goto-char (match-beginning 5))
  12631. (insert data)
  12632. (delete-region (point) (point-at-eol))
  12633. (org-set-tags nil 'align))
  12634. (goto-char (point-at-eol))
  12635. (insert " " data)
  12636. (org-set-tags nil 'align)))
  12637. (beginning-of-line 1)
  12638. (if (looking-at ".*?\\([ \t]+\\)$")
  12639. (delete-region (match-beginning 1) (match-end 1))))))
  12640. (defun org-align-all-tags ()
  12641. "Align the tags i all headings."
  12642. (interactive)
  12643. (save-excursion
  12644. (or (ignore-errors (org-back-to-heading t))
  12645. (outline-next-heading))
  12646. (if (org-at-heading-p)
  12647. (org-set-tags t)
  12648. (message "No headings"))))
  12649. (defvar org-indent-indentation-per-level)
  12650. (defun org-set-tags (&optional arg just-align)
  12651. "Set the tags for the current headline.
  12652. With prefix ARG, realign all tags in headings in the current buffer."
  12653. (interactive "P")
  12654. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12655. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12656. 'region-start-level 'region))
  12657. org-loop-over-headlines-in-active-region)
  12658. (org-map-entries
  12659. ;; We don't use ARG and JUST-ALIGN here these args are not
  12660. ;; useful when looping over headlines
  12661. `(org-set-tags)
  12662. org-loop-over-headlines-in-active-region
  12663. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12664. (let* ((re org-outline-regexp-bol)
  12665. (current (unless arg (org-get-tags-string)))
  12666. (col (current-column))
  12667. (org-setting-tags t)
  12668. table current-tags inherited-tags ; computed below when needed
  12669. tags p0 c0 c1 rpl di tc level)
  12670. (if arg
  12671. (save-excursion
  12672. (goto-char (point-min))
  12673. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12674. (while (re-search-forward re nil t)
  12675. (org-set-tags nil t)
  12676. (end-of-line 1)))
  12677. (message "All tags realigned to column %d" org-tags-column))
  12678. (if just-align
  12679. (setq tags current)
  12680. ;; Get a new set of tags from the user
  12681. (save-excursion
  12682. (setq table (append org-tag-persistent-alist
  12683. (or org-tag-alist (org-get-buffer-tags))
  12684. (and
  12685. org-complete-tags-always-offer-all-agenda-tags
  12686. (org-global-tags-completion-table
  12687. (org-agenda-files))))
  12688. org-last-tags-completion-table table
  12689. current-tags (org-split-string current ":")
  12690. inherited-tags (nreverse
  12691. (nthcdr (length current-tags)
  12692. (nreverse (org-get-tags-at))))
  12693. tags
  12694. (if (or (eq t org-use-fast-tag-selection)
  12695. (and org-use-fast-tag-selection
  12696. (delq nil (mapcar 'cdr table))))
  12697. (org-fast-tag-selection
  12698. current-tags inherited-tags table
  12699. (if org-fast-tag-selection-include-todo
  12700. org-todo-key-alist))
  12701. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12702. (org-trim
  12703. (org-icompleting-read "Tags: "
  12704. 'org-tags-completion-function
  12705. nil nil current 'org-tags-history))))))
  12706. (while (string-match "[-+&]+" tags)
  12707. ;; No boolean logic, just a list
  12708. (setq tags (replace-match ":" t t tags))))
  12709. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12710. (if org-tags-sort-function
  12711. (setq tags (mapconcat 'identity
  12712. (sort (org-split-string
  12713. tags (org-re "[^[:alnum:]_@#%]+"))
  12714. org-tags-sort-function) ":")))
  12715. (if (string-match "\\`[\t ]*\\'" tags)
  12716. (setq tags "")
  12717. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12718. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12719. ;; Insert new tags at the correct column
  12720. (beginning-of-line 1)
  12721. (setq level (or (and (looking-at org-outline-regexp)
  12722. (- (match-end 0) (point) 1))
  12723. 1))
  12724. (cond
  12725. ((and (equal current "") (equal tags "")))
  12726. ((re-search-forward
  12727. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12728. (point-at-eol) t)
  12729. (if (equal tags "")
  12730. (setq rpl "")
  12731. (goto-char (match-beginning 0))
  12732. (setq c0 (current-column)
  12733. ;; compute offset for the case of org-indent-mode active
  12734. di (if org-indent-mode
  12735. (* (1- org-indent-indentation-per-level) (1- level))
  12736. 0)
  12737. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12738. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12739. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12740. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12741. (replace-match rpl t t)
  12742. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12743. tags)
  12744. (t (error "Tags alignment failed")))
  12745. (org-move-to-column col)
  12746. (unless just-align
  12747. (run-hooks 'org-after-tags-change-hook))))))
  12748. (defun org-change-tag-in-region (beg end tag off)
  12749. "Add or remove TAG for each entry in the region.
  12750. This works in the agenda, and also in an org-mode buffer."
  12751. (interactive
  12752. (list (region-beginning) (region-end)
  12753. (let ((org-last-tags-completion-table
  12754. (if (derived-mode-p 'org-mode)
  12755. (org-get-buffer-tags)
  12756. (org-global-tags-completion-table))))
  12757. (org-icompleting-read
  12758. "Tag: " 'org-tags-completion-function nil nil nil
  12759. 'org-tags-history))
  12760. (progn
  12761. (message "[s]et or [r]emove? ")
  12762. (equal (read-char-exclusive) ?r))))
  12763. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12764. (let ((agendap (equal major-mode 'org-agenda-mode))
  12765. l1 l2 m buf pos newhead (cnt 0))
  12766. (goto-char end)
  12767. (setq l2 (1- (org-current-line)))
  12768. (goto-char beg)
  12769. (setq l1 (org-current-line))
  12770. (loop for l from l1 to l2 do
  12771. (org-goto-line l)
  12772. (setq m (get-text-property (point) 'org-hd-marker))
  12773. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12774. (and agendap m))
  12775. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12776. pos (if agendap m (point)))
  12777. (with-current-buffer buf
  12778. (save-excursion
  12779. (save-restriction
  12780. (goto-char pos)
  12781. (setq cnt (1+ cnt))
  12782. (org-toggle-tag tag (if off 'off 'on))
  12783. (setq newhead (org-get-heading)))))
  12784. (and agendap (org-agenda-change-all-lines newhead m))))
  12785. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12786. (defun org-tags-completion-function (string predicate &optional flag)
  12787. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12788. (confirm (lambda (x) (stringp (car x)))))
  12789. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12790. (setq s1 (match-string 1 string)
  12791. s2 (match-string 2 string))
  12792. (setq s1 "" s2 string))
  12793. (cond
  12794. ((eq flag nil)
  12795. ;; try completion
  12796. (setq rtn (try-completion s2 ctable confirm))
  12797. (if (stringp rtn)
  12798. (setq rtn
  12799. (concat s1 s2 (substring rtn (length s2))
  12800. (if (and org-add-colon-after-tag-completion
  12801. (assoc rtn ctable))
  12802. ":" ""))))
  12803. rtn)
  12804. ((eq flag t)
  12805. ;; all-completions
  12806. (all-completions s2 ctable confirm)
  12807. )
  12808. ((eq flag 'lambda)
  12809. ;; exact match?
  12810. (assoc s2 ctable)))
  12811. ))
  12812. (defun org-fast-tag-insert (kwd tags face &optional end)
  12813. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12814. (insert (format "%-12s" (concat kwd ":"))
  12815. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12816. (or end "")))
  12817. (defun org-fast-tag-show-exit (flag)
  12818. (save-excursion
  12819. (org-goto-line 3)
  12820. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12821. (replace-match ""))
  12822. (when flag
  12823. (end-of-line 1)
  12824. (org-move-to-column (- (window-width) 19) t)
  12825. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12826. (defun org-set-current-tags-overlay (current prefix)
  12827. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12828. (if (featurep 'xemacs)
  12829. (org-overlay-display org-tags-overlay (concat prefix s)
  12830. 'secondary-selection)
  12831. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12832. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12833. (defvar org-last-tag-selection-key nil)
  12834. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12835. "Fast tag selection with single keys.
  12836. CURRENT is the current list of tags in the headline, INHERITED is the
  12837. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12838. possibly with grouping information. TODO-TABLE is a similar table with
  12839. TODO keywords, should these have keys assigned to them.
  12840. If the keys are nil, a-z are automatically assigned.
  12841. Returns the new tags string, or nil to not change the current settings."
  12842. (let* ((fulltable (append table todo-table))
  12843. (maxlen (apply 'max (mapcar
  12844. (lambda (x)
  12845. (if (stringp (car x)) (string-width (car x)) 0))
  12846. fulltable)))
  12847. (buf (current-buffer))
  12848. (expert (eq org-fast-tag-selection-single-key 'expert))
  12849. (buffer-tags nil)
  12850. (fwidth (+ maxlen 3 1 3))
  12851. (ncol (/ (- (window-width) 4) fwidth))
  12852. (i-face 'org-done)
  12853. (c-face 'org-todo)
  12854. tg cnt e c char c1 c2 ntable tbl rtn
  12855. ov-start ov-end ov-prefix
  12856. (exit-after-next org-fast-tag-selection-single-key)
  12857. (done-keywords org-done-keywords)
  12858. groups ingroup)
  12859. (save-excursion
  12860. (beginning-of-line 1)
  12861. (if (looking-at
  12862. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12863. (setq ov-start (match-beginning 1)
  12864. ov-end (match-end 1)
  12865. ov-prefix "")
  12866. (setq ov-start (1- (point-at-eol))
  12867. ov-end (1+ ov-start))
  12868. (skip-chars-forward "^\n\r")
  12869. (setq ov-prefix
  12870. (concat
  12871. (buffer-substring (1- (point)) (point))
  12872. (if (> (current-column) org-tags-column)
  12873. " "
  12874. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12875. (move-overlay org-tags-overlay ov-start ov-end)
  12876. (save-window-excursion
  12877. (if expert
  12878. (set-buffer (get-buffer-create " *Org tags*"))
  12879. (delete-other-windows)
  12880. (split-window-vertically)
  12881. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12882. (erase-buffer)
  12883. (org-set-local 'org-done-keywords done-keywords)
  12884. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12885. (org-fast-tag-insert "Current" current c-face "\n\n")
  12886. (org-fast-tag-show-exit exit-after-next)
  12887. (org-set-current-tags-overlay current ov-prefix)
  12888. (setq tbl fulltable char ?a cnt 0)
  12889. (while (setq e (pop tbl))
  12890. (cond
  12891. ((equal (car e) :startgroup)
  12892. (push '() groups) (setq ingroup t)
  12893. (when (not (= cnt 0))
  12894. (setq cnt 0)
  12895. (insert "\n"))
  12896. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12897. ((equal (car e) :endgroup)
  12898. (setq ingroup nil cnt 0)
  12899. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12900. ((equal e '(:newline))
  12901. (when (not (= cnt 0))
  12902. (setq cnt 0)
  12903. (insert "\n")
  12904. (setq e (car tbl))
  12905. (while (equal (car tbl) '(:newline))
  12906. (insert "\n")
  12907. (setq tbl (cdr tbl)))))
  12908. (t
  12909. (setq tg (copy-sequence (car e)) c2 nil)
  12910. (if (cdr e)
  12911. (setq c (cdr e))
  12912. ;; automatically assign a character.
  12913. (setq c1 (string-to-char
  12914. (downcase (substring
  12915. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12916. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12917. (while (or (rassoc char ntable) (rassoc char table))
  12918. (setq char (1+ char)))
  12919. (setq c2 c1))
  12920. (setq c (or c2 char)))
  12921. (if ingroup (push tg (car groups)))
  12922. (setq tg (org-add-props tg nil 'face
  12923. (cond
  12924. ((not (assoc tg table))
  12925. (org-get-todo-face tg))
  12926. ((member tg current) c-face)
  12927. ((member tg inherited) i-face))))
  12928. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12929. (insert "[" c "] " tg (make-string
  12930. (- fwidth 4 (length tg)) ?\ ))
  12931. (push (cons tg c) ntable)
  12932. (when (= (setq cnt (1+ cnt)) ncol)
  12933. (insert "\n")
  12934. (if ingroup (insert " "))
  12935. (setq cnt 0)))))
  12936. (setq ntable (nreverse ntable))
  12937. (insert "\n")
  12938. (goto-char (point-min))
  12939. (if (not expert) (org-fit-window-to-buffer))
  12940. (setq rtn
  12941. (catch 'exit
  12942. (while t
  12943. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12944. (if (not groups) "no " "")
  12945. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12946. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12947. (setq org-last-tag-selection-key c)
  12948. (cond
  12949. ((= c ?\r) (throw 'exit t))
  12950. ((= c ?!)
  12951. (setq groups (not groups))
  12952. (goto-char (point-min))
  12953. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12954. ((= c ?\C-c)
  12955. (if (not expert)
  12956. (org-fast-tag-show-exit
  12957. (setq exit-after-next (not exit-after-next)))
  12958. (setq expert nil)
  12959. (delete-other-windows)
  12960. (set-window-buffer (split-window-vertically) " *Org tags*")
  12961. (org-switch-to-buffer-other-window " *Org tags*")
  12962. (org-fit-window-to-buffer)))
  12963. ((or (= c ?\C-g)
  12964. (and (= c ?q) (not (rassoc c ntable))))
  12965. (org-detach-overlay org-tags-overlay)
  12966. (setq quit-flag t))
  12967. ((= c ?\ )
  12968. (setq current nil)
  12969. (if exit-after-next (setq exit-after-next 'now)))
  12970. ((= c ?\t)
  12971. (condition-case nil
  12972. (setq tg (org-icompleting-read
  12973. "Tag: "
  12974. (or buffer-tags
  12975. (with-current-buffer buf
  12976. (org-get-buffer-tags)))))
  12977. (quit (setq tg "")))
  12978. (when (string-match "\\S-" tg)
  12979. (add-to-list 'buffer-tags (list tg))
  12980. (if (member tg current)
  12981. (setq current (delete tg current))
  12982. (push tg current)))
  12983. (if exit-after-next (setq exit-after-next 'now)))
  12984. ((setq e (rassoc c todo-table) tg (car e))
  12985. (with-current-buffer buf
  12986. (save-excursion (org-todo tg)))
  12987. (if exit-after-next (setq exit-after-next 'now)))
  12988. ((setq e (rassoc c ntable) tg (car e))
  12989. (if (member tg current)
  12990. (setq current (delete tg current))
  12991. (loop for g in groups do
  12992. (if (member tg g)
  12993. (mapc (lambda (x)
  12994. (setq current (delete x current)))
  12995. g)))
  12996. (push tg current))
  12997. (if exit-after-next (setq exit-after-next 'now))))
  12998. ;; Create a sorted list
  12999. (setq current
  13000. (sort current
  13001. (lambda (a b)
  13002. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13003. (if (eq exit-after-next 'now) (throw 'exit t))
  13004. (goto-char (point-min))
  13005. (beginning-of-line 2)
  13006. (delete-region (point) (point-at-eol))
  13007. (org-fast-tag-insert "Current" current c-face)
  13008. (org-set-current-tags-overlay current ov-prefix)
  13009. (while (re-search-forward
  13010. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13011. (setq tg (match-string 1))
  13012. (add-text-properties
  13013. (match-beginning 1) (match-end 1)
  13014. (list 'face
  13015. (cond
  13016. ((member tg current) c-face)
  13017. ((member tg inherited) i-face)
  13018. (t (get-text-property (match-beginning 1) 'face))))))
  13019. (goto-char (point-min)))))
  13020. (org-detach-overlay org-tags-overlay)
  13021. (if rtn
  13022. (mapconcat 'identity current ":")
  13023. nil))))
  13024. (defun org-get-tags-string ()
  13025. "Get the TAGS string in the current headline."
  13026. (unless (org-at-heading-p t)
  13027. (error "Not on a heading"))
  13028. (save-excursion
  13029. (beginning-of-line 1)
  13030. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13031. (org-match-string-no-properties 1)
  13032. "")))
  13033. (defun org-get-tags ()
  13034. "Get the list of tags specified in the current headline."
  13035. (org-split-string (org-get-tags-string) ":"))
  13036. (defun org-get-buffer-tags ()
  13037. "Get a table of all tags used in the buffer, for completion."
  13038. (let (tags)
  13039. (save-excursion
  13040. (goto-char (point-min))
  13041. (while (re-search-forward
  13042. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13043. (when (equal (char-after (point-at-bol 0)) ?*)
  13044. (mapc (lambda (x) (add-to-list 'tags x))
  13045. (org-split-string (org-match-string-no-properties 1) ":")))))
  13046. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13047. (mapcar 'list tags)))
  13048. ;;;; The mapping API
  13049. (defun org-map-entries (func &optional match scope &rest skip)
  13050. "Call FUNC at each headline selected by MATCH in SCOPE.
  13051. FUNC is a function or a lisp form. The function will be called without
  13052. arguments, with the cursor positioned at the beginning of the headline.
  13053. The return values of all calls to the function will be collected and
  13054. returned as a list.
  13055. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13056. does not need to preserve point. After evaluation, the cursor will be
  13057. moved to the end of the line (presumably of the headline of the
  13058. processed entry) and search continues from there. Under some
  13059. circumstances, this may not produce the wanted results. For example,
  13060. if you have removed (e.g. archived) the current (sub)tree it could
  13061. mean that the next entry will be skipped entirely. In such cases, you
  13062. can specify the position from where search should continue by making
  13063. FUNC set the variable `org-map-continue-from' to the desired buffer
  13064. position.
  13065. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13066. Only headlines that are matched by this query will be considered during
  13067. the iteration. When MATCH is nil or t, all headlines will be
  13068. visited by the iteration.
  13069. SCOPE determines the scope of this command. It can be any of:
  13070. nil The current buffer, respecting the restriction if any
  13071. tree The subtree started with the entry at point
  13072. region The entries within the active region, if any
  13073. region-start-level
  13074. The entries within the active region, but only those at
  13075. the same level than the first one.
  13076. file The current buffer, without restriction
  13077. file-with-archives
  13078. The current buffer, and any archives associated with it
  13079. agenda All agenda files
  13080. agenda-with-archives
  13081. All agenda files with any archive files associated with them
  13082. \(file1 file2 ...)
  13083. If this is a list, all files in the list will be scanned
  13084. The remaining args are treated as settings for the skipping facilities of
  13085. the scanner. The following items can be given here:
  13086. archive skip trees with the archive tag.
  13087. comment skip trees with the COMMENT keyword
  13088. function or Emacs Lisp form:
  13089. will be used as value for `org-agenda-skip-function', so whenever
  13090. the function returns t, FUNC will not be called for that
  13091. entry and search will continue from the point where the
  13092. function leaves it.
  13093. If your function needs to retrieve the tags including inherited tags
  13094. at the *current* entry, you can use the value of the variable
  13095. `org-scanner-tags' which will be much faster than getting the value
  13096. with `org-get-tags-at'. If your function gets properties with
  13097. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13098. to t around the call to `org-entry-properties' to get the same speedup.
  13099. Note that if your function moves around to retrieve tags and properties at
  13100. a *different* entry, you cannot use these techniques."
  13101. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13102. (not (org-region-active-p)))
  13103. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13104. (org-agenda-skip-archived-trees (memq 'archive skip))
  13105. (org-agenda-skip-comment-trees (memq 'comment skip))
  13106. (org-agenda-skip-function
  13107. (car (org-delete-all '(comment archive) skip)))
  13108. (org-tags-match-list-sublevels t)
  13109. (start-level (eq scope 'region-start-level))
  13110. matcher file res
  13111. org-todo-keywords-for-agenda
  13112. org-done-keywords-for-agenda
  13113. org-todo-keyword-alist-for-agenda
  13114. org-drawers-for-agenda
  13115. org-tag-alist-for-agenda
  13116. todo-only)
  13117. (cond
  13118. ((eq match t) (setq matcher t))
  13119. ((eq match nil) (setq matcher t))
  13120. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13121. (save-excursion
  13122. (save-restriction
  13123. (cond ((eq scope 'tree)
  13124. (org-back-to-heading t)
  13125. (org-narrow-to-subtree)
  13126. (setq scope nil))
  13127. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13128. (org-region-active-p))
  13129. ;; If needed, set start-level to a string like "2"
  13130. (when start-level
  13131. (save-excursion
  13132. (goto-char (region-beginning))
  13133. (unless (org-at-heading-p) (outline-next-heading))
  13134. (setq start-level (org-current-level))))
  13135. (narrow-to-region (region-beginning)
  13136. (save-excursion
  13137. (goto-char (region-end))
  13138. (unless (and (bolp) (org-at-heading-p))
  13139. (outline-next-heading))
  13140. (point)))
  13141. (setq scope nil)))
  13142. (if (not scope)
  13143. (progn
  13144. (org-agenda-prepare-buffers
  13145. (list (buffer-file-name (current-buffer))))
  13146. (setq res (org-scan-tags func matcher todo-only start-level)))
  13147. ;; Get the right scope
  13148. (cond
  13149. ((and scope (listp scope) (symbolp (car scope)))
  13150. (setq scope (eval scope)))
  13151. ((eq scope 'agenda)
  13152. (setq scope (org-agenda-files t)))
  13153. ((eq scope 'agenda-with-archives)
  13154. (setq scope (org-agenda-files t))
  13155. (setq scope (org-add-archive-files scope)))
  13156. ((eq scope 'file)
  13157. (setq scope (list (buffer-file-name))))
  13158. ((eq scope 'file-with-archives)
  13159. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13160. (org-agenda-prepare-buffers scope)
  13161. (while (setq file (pop scope))
  13162. (with-current-buffer (org-find-base-buffer-visiting file)
  13163. (save-excursion
  13164. (save-restriction
  13165. (widen)
  13166. (goto-char (point-min))
  13167. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13168. res)))
  13169. ;;;; Properties
  13170. ;;; Setting and retrieving properties
  13171. (defconst org-special-properties
  13172. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13173. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13174. "The special properties valid in Org-mode.
  13175. These are properties that are not defined in the property drawer,
  13176. but in some other way.")
  13177. (defconst org-default-properties
  13178. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13179. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13180. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13181. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13182. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13183. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13184. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13185. "Some properties that are used by Org-mode for various purposes.
  13186. Being in this list makes sure that they are offered for completion.")
  13187. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13188. "Regular expression matching the first line of a property drawer.")
  13189. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13190. "Regular expression matching the last line of a property drawer.")
  13191. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13192. "Regular expression matching the first line of a property drawer.")
  13193. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13194. "Regular expression matching the first line of a property drawer.")
  13195. (defconst org-property-drawer-re
  13196. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13197. org-property-end-re "\\)\n?")
  13198. "Matches an entire property drawer.")
  13199. (defconst org-clock-drawer-re
  13200. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13201. org-property-end-re "\\)\n?")
  13202. "Matches an entire clock drawer.")
  13203. (defsubst org-re-property (property)
  13204. "Return a regexp matching a PROPERTY line.
  13205. Match group 1 will be set to the value."
  13206. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13207. (defsubst org-re-property-keyword (property)
  13208. "Return a regexp matching a PROPERTY line, possibly with no
  13209. value for the property."
  13210. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13211. (defun org-property-action ()
  13212. "Do an action on properties."
  13213. (interactive)
  13214. (let (c)
  13215. (org-at-property-p)
  13216. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13217. (setq c (read-char-exclusive))
  13218. (cond
  13219. ((equal c ?s)
  13220. (call-interactively 'org-set-property))
  13221. ((equal c ?d)
  13222. (call-interactively 'org-delete-property))
  13223. ((equal c ?D)
  13224. (call-interactively 'org-delete-property-globally))
  13225. ((equal c ?c)
  13226. (call-interactively 'org-compute-property-at-point))
  13227. (t (error "No such property action %c" c)))))
  13228. (defun org-inc-effort ()
  13229. "Increment the value of the effort property in the current entry."
  13230. (interactive)
  13231. (org-set-effort nil t))
  13232. (defun org-set-effort (&optional value increment)
  13233. "Set the effort property of the current entry.
  13234. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13235. 10th allowed value.
  13236. When INCREMENT is non-nil, set the property to the next allowed value."
  13237. (interactive "P")
  13238. (if (equal value 0) (setq value 10))
  13239. (let* ((completion-ignore-case t)
  13240. (prop org-effort-property)
  13241. (cur (org-entry-get nil prop))
  13242. (allowed (org-property-get-allowed-values nil prop 'table))
  13243. (existing (mapcar 'list (org-property-values prop)))
  13244. rpl
  13245. (val (cond
  13246. ((stringp value) value)
  13247. ((and allowed (integerp value))
  13248. (or (car (nth (1- value) allowed))
  13249. (car (org-last allowed))))
  13250. ((and allowed increment)
  13251. (or (caadr (member (list cur) allowed))
  13252. (error "Allowed effort values are not set")))
  13253. (allowed
  13254. (message "Select 1-9,0, [RET%s]: %s"
  13255. (if cur (concat "=" cur) "")
  13256. (mapconcat 'car allowed " "))
  13257. (setq rpl (read-char-exclusive))
  13258. (if (equal rpl ?\r)
  13259. cur
  13260. (setq rpl (- rpl ?0))
  13261. (if (equal rpl 0) (setq rpl 10))
  13262. (if (and (> rpl 0) (<= rpl (length allowed)))
  13263. (car (nth (1- rpl) allowed))
  13264. (org-completing-read "Effort: " allowed nil))))
  13265. (t
  13266. (let (org-completion-use-ido org-completion-use-iswitchb)
  13267. (org-completing-read
  13268. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13269. (concat "[" cur "]") "")
  13270. ": ")
  13271. existing nil nil "" nil cur))))))
  13272. (unless (equal (org-entry-get nil prop) val)
  13273. (org-entry-put nil prop val))
  13274. (message "%s is now %s" prop val)))
  13275. (defun org-at-property-p ()
  13276. "Is cursor inside a property drawer?"
  13277. (save-excursion
  13278. (beginning-of-line 1)
  13279. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13280. (save-match-data ;; Used by calling procedures
  13281. (let ((p (point))
  13282. (range (unless (org-before-first-heading-p)
  13283. (org-get-property-block))))
  13284. (and range (<= (car range) p) (< p (cdr range))))))))
  13285. (defun org-get-property-block (&optional beg end force)
  13286. "Return the (beg . end) range of the body of the property drawer.
  13287. BEG and END are the beginning and end of the current subtree, or of
  13288. the part before the first headline. If they are not given, they will
  13289. be found. If the drawer does not exist and FORCE is non-nil, create
  13290. the drawer."
  13291. (catch 'exit
  13292. (save-excursion
  13293. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13294. (progn (org-back-to-heading t) (point))))
  13295. (end (or end (and (not (outline-next-heading)) (point-max))
  13296. (point))))
  13297. (goto-char beg)
  13298. (if (re-search-forward org-property-start-re end t)
  13299. (setq beg (1+ (match-end 0)))
  13300. (if force
  13301. (save-excursion
  13302. (org-insert-property-drawer)
  13303. (setq end (progn (outline-next-heading) (point))))
  13304. (throw 'exit nil))
  13305. (goto-char beg)
  13306. (if (re-search-forward org-property-start-re end t)
  13307. (setq beg (1+ (match-end 0)))))
  13308. (if (re-search-forward org-property-end-re end t)
  13309. (setq end (match-beginning 0))
  13310. (or force (throw 'exit nil))
  13311. (goto-char beg)
  13312. (setq end beg)
  13313. (org-indent-line)
  13314. (insert ":END:\n"))
  13315. (cons beg end)))))
  13316. (defun org-entry-properties (&optional pom which specific)
  13317. "Get all properties of the entry at point-or-marker POM.
  13318. This includes the TODO keyword, the tags, time strings for deadline,
  13319. scheduled, and clocking, and any additional properties defined in the
  13320. entry. The return value is an alist, keys may occur multiple times
  13321. if the property key was used several times.
  13322. POM may also be nil, in which case the current entry is used.
  13323. If WHICH is nil or `all', get all properties. If WHICH is
  13324. `special' or `standard', only get that subclass. If WHICH
  13325. is a string only get exactly this property. SPECIFIC can be a string, the
  13326. specific property we are interested in. Specifying it can speed
  13327. things up because then unnecessary parsing is avoided."
  13328. (setq which (or which 'all))
  13329. (org-with-point-at pom
  13330. (let ((clockstr (substring org-clock-string 0 -1))
  13331. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13332. (case-fold-search nil)
  13333. beg end range props sum-props key key1 value string clocksum clocksumt)
  13334. (save-excursion
  13335. (when (condition-case nil
  13336. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13337. (error nil))
  13338. (setq beg (point))
  13339. (setq sum-props (get-text-property (point) 'org-summaries))
  13340. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13341. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13342. (outline-next-heading)
  13343. (setq end (point))
  13344. (when (memq which '(all special))
  13345. ;; Get the special properties, like TODO and tags
  13346. (goto-char beg)
  13347. (when (and (or (not specific) (string= specific "TODO"))
  13348. (looking-at org-todo-line-regexp) (match-end 2))
  13349. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13350. (when (and (or (not specific) (string= specific "PRIORITY"))
  13351. (looking-at org-priority-regexp))
  13352. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13353. (when (or (not specific) (string= specific "FILE"))
  13354. (push (cons "FILE" buffer-file-name) props))
  13355. (when (and (or (not specific) (string= specific "TAGS"))
  13356. (setq value (org-get-tags-string))
  13357. (string-match "\\S-" value))
  13358. (push (cons "TAGS" value) props))
  13359. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13360. (setq value (org-get-tags-at)))
  13361. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13362. ":"))
  13363. props))
  13364. (when (or (not specific) (string= specific "BLOCKED"))
  13365. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13366. (when (or (not specific)
  13367. (member specific
  13368. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13369. "TIMESTAMP" "TIMESTAMP_IA")))
  13370. (catch 'match
  13371. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13372. (setq key (if (match-end 1)
  13373. (substring (org-match-string-no-properties 1)
  13374. 0 -1))
  13375. string (if (equal key clockstr)
  13376. (org-trim
  13377. (buffer-substring-no-properties
  13378. (match-beginning 3) (goto-char
  13379. (point-at-eol))))
  13380. (substring (org-match-string-no-properties 3)
  13381. 1 -1)))
  13382. ;; Get the correct property name from the key. This is
  13383. ;; necessary if the user has configured time keywords.
  13384. (setq key1 (concat key ":"))
  13385. (cond
  13386. ((not key)
  13387. (setq key
  13388. (if (= (char-after (match-beginning 3)) ?\[)
  13389. "TIMESTAMP_IA" "TIMESTAMP")))
  13390. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13391. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13392. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13393. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13394. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13395. (progn
  13396. (push (cons key string) props)
  13397. ;; no need to search further if match is found
  13398. (throw 'match t))
  13399. (when (or (equal key "CLOCK") (not (assoc key props)))
  13400. (push (cons key string) props)))))))
  13401. (when (memq which '(all standard))
  13402. ;; Get the standard properties, like :PROP: ...
  13403. (setq range (org-get-property-block beg end))
  13404. (when range
  13405. (goto-char (car range))
  13406. (while (re-search-forward
  13407. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13408. (cdr range) t)
  13409. (setq key (org-match-string-no-properties 1)
  13410. value (org-trim (or (org-match-string-no-properties 2) "")))
  13411. (unless (member key excluded)
  13412. (push (cons key (or value "")) props)))))
  13413. (if clocksum
  13414. (push (cons "CLOCKSUM"
  13415. (org-columns-number-to-string (/ (float clocksum) 60.)
  13416. 'add_times))
  13417. props))
  13418. (if clocksumt
  13419. (push (cons "CLOCKSUM_T"
  13420. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13421. 'add_times))
  13422. props))
  13423. (unless (assoc "CATEGORY" props)
  13424. (push (cons "CATEGORY" (org-get-category)) props))
  13425. (append sum-props (nreverse props)))))))
  13426. (defun org-entry-get (pom property &optional inherit literal-nil)
  13427. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13428. If INHERIT is non-nil and the entry does not have the property,
  13429. then also check higher levels of the hierarchy.
  13430. If INHERIT is the symbol `selective', use inheritance only if the setting
  13431. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13432. If the property is present but empty, the return value is the empty string.
  13433. If the property is not present at all, nil is returned.
  13434. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13435. do not interpret it as the list atom nil. This is used for inheritance
  13436. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13437. (org-with-point-at pom
  13438. (if (and inherit (if (eq inherit 'selective)
  13439. (org-property-inherit-p property)
  13440. t))
  13441. (org-entry-get-with-inheritance property literal-nil)
  13442. (if (member property org-special-properties)
  13443. ;; We need a special property. Use `org-entry-properties' to
  13444. ;; retrieve it, but specify the wanted property
  13445. (cdr (assoc property (org-entry-properties nil 'special property)))
  13446. (let* ((range (org-get-property-block))
  13447. (props (list (or (assoc property org-file-properties)
  13448. (assoc property org-global-properties)
  13449. (assoc property org-global-properties-fixed))))
  13450. (ap (lambda (key)
  13451. (when (re-search-forward
  13452. (org-re-property key) (cdr range) t)
  13453. (setq props
  13454. (org-update-property-plist
  13455. key
  13456. (if (match-end 1)
  13457. (org-match-string-no-properties 1) "")
  13458. props)))))
  13459. val)
  13460. (when (and range (goto-char (car range)))
  13461. (funcall ap property)
  13462. (goto-char (car range))
  13463. (while (funcall ap (concat property "+")))
  13464. (setq val (cdr (assoc property props)))
  13465. (when val (if literal-nil val (org-not-nil val)))))))))
  13466. (defun org-property-or-variable-value (var &optional inherit)
  13467. "Check if there is a property fixing the value of VAR.
  13468. If yes, return this value. If not, return the current value of the variable."
  13469. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13470. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13471. (read prop)
  13472. (symbol-value var))))
  13473. (defun org-entry-delete (pom property)
  13474. "Delete the property PROPERTY from entry at point-or-marker POM."
  13475. (org-with-point-at pom
  13476. (if (member property org-special-properties)
  13477. nil ; cannot delete these properties.
  13478. (let ((range (org-get-property-block)))
  13479. (if (and range
  13480. (goto-char (car range))
  13481. (re-search-forward
  13482. (org-re-property property)
  13483. (cdr range) t))
  13484. (progn
  13485. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13486. t)
  13487. nil)))))
  13488. ;; Multi-values properties are properties that contain multiple values
  13489. ;; These values are assumed to be single words, separated by whitespace.
  13490. (defun org-entry-add-to-multivalued-property (pom property value)
  13491. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13492. (let* ((old (org-entry-get pom property))
  13493. (values (and old (org-split-string old "[ \t]"))))
  13494. (setq value (org-entry-protect-space value))
  13495. (unless (member value values)
  13496. (setq values (cons value values))
  13497. (org-entry-put pom property
  13498. (mapconcat 'identity values " ")))))
  13499. (defun org-entry-remove-from-multivalued-property (pom property value)
  13500. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13501. (let* ((old (org-entry-get pom property))
  13502. (values (and old (org-split-string old "[ \t]"))))
  13503. (setq value (org-entry-protect-space value))
  13504. (when (member value values)
  13505. (setq values (delete value values))
  13506. (org-entry-put pom property
  13507. (mapconcat 'identity values " ")))))
  13508. (defun org-entry-member-in-multivalued-property (pom property value)
  13509. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13510. (let* ((old (org-entry-get pom property))
  13511. (values (and old (org-split-string old "[ \t]"))))
  13512. (setq value (org-entry-protect-space value))
  13513. (member value values)))
  13514. (defun org-entry-get-multivalued-property (pom property)
  13515. "Return a list of values in a multivalued property."
  13516. (let* ((value (org-entry-get pom property))
  13517. (values (and value (org-split-string value "[ \t]"))))
  13518. (mapcar 'org-entry-restore-space values)))
  13519. (defun org-entry-put-multivalued-property (pom property &rest values)
  13520. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13521. VALUES should be a list of strings. Spaces will be protected."
  13522. (org-entry-put pom property
  13523. (mapconcat 'org-entry-protect-space values " "))
  13524. (let* ((value (org-entry-get pom property))
  13525. (values (and value (org-split-string value "[ \t]"))))
  13526. (mapcar 'org-entry-restore-space values)))
  13527. (defun org-entry-protect-space (s)
  13528. "Protect spaces and newline in string S."
  13529. (while (string-match " " s)
  13530. (setq s (replace-match "%20" t t s)))
  13531. (while (string-match "\n" s)
  13532. (setq s (replace-match "%0A" t t s)))
  13533. s)
  13534. (defun org-entry-restore-space (s)
  13535. "Restore spaces and newline in string S."
  13536. (while (string-match "%20" s)
  13537. (setq s (replace-match " " t t s)))
  13538. (while (string-match "%0A" s)
  13539. (setq s (replace-match "\n" t t s)))
  13540. s)
  13541. (defvar org-entry-property-inherited-from (make-marker)
  13542. "Marker pointing to the entry from where a property was inherited.
  13543. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13544. location of the entry where the inheritance search matched. If there was
  13545. no match, the marker will point nowhere.
  13546. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13547. is set.")
  13548. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13549. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13550. The search will stop at the first ancestor which has the property defined.
  13551. If the value found is \"nil\", return nil to show that the property
  13552. should be considered as undefined (this is the meaning of nil here).
  13553. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13554. (move-marker org-entry-property-inherited-from nil)
  13555. (let (tmp)
  13556. (save-excursion
  13557. (save-restriction
  13558. (widen)
  13559. (catch 'ex
  13560. (while t
  13561. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13562. (or (ignore-errors (org-back-to-heading t))
  13563. (goto-char (point-min)))
  13564. (move-marker org-entry-property-inherited-from (point))
  13565. (throw 'ex tmp))
  13566. (or (ignore-errors (org-up-heading-safe))
  13567. (throw 'ex nil))))))
  13568. (setq tmp (or tmp
  13569. (cdr (assoc property org-file-properties))
  13570. (cdr (assoc property org-global-properties))
  13571. (cdr (assoc property org-global-properties-fixed))))
  13572. (if literal-nil tmp (org-not-nil tmp))))
  13573. (defvar org-property-changed-functions nil
  13574. "Hook called when the value of a property has changed.
  13575. Each hook function should accept two arguments, the name of the property
  13576. and the new value.")
  13577. (defun org-entry-put (pom property value)
  13578. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13579. (org-with-point-at pom
  13580. (org-back-to-heading t)
  13581. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13582. range)
  13583. (cond
  13584. ((equal property "TODO")
  13585. (when (and (stringp value) (string-match "\\S-" value)
  13586. (not (member value org-todo-keywords-1)))
  13587. (error "\"%s\" is not a valid TODO state" value))
  13588. (if (or (not value)
  13589. (not (string-match "\\S-" value)))
  13590. (setq value 'none))
  13591. (org-todo value)
  13592. (org-set-tags nil 'align))
  13593. ((equal property "PRIORITY")
  13594. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13595. (string-to-char value) ?\ ))
  13596. (org-set-tags nil 'align))
  13597. ((equal property "CLOCKSUM")
  13598. (if (not (re-search-forward
  13599. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13600. (error "Cannot find a clock log")
  13601. (goto-char (- (match-end 1) 2))
  13602. (cond
  13603. ((eq value 'earlier) (org-timestamp-down))
  13604. ((eq value 'later) (org-timestamp-up)))
  13605. (org-clock-sum-current-item)))
  13606. ((equal property "SCHEDULED")
  13607. (if (re-search-forward org-scheduled-time-regexp end t)
  13608. (cond
  13609. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13610. ((eq value 'later) (org-timestamp-change 1 'day))
  13611. (t (call-interactively 'org-schedule)))
  13612. (call-interactively 'org-schedule)))
  13613. ((equal property "DEADLINE")
  13614. (if (re-search-forward org-deadline-time-regexp end t)
  13615. (cond
  13616. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13617. ((eq value 'later) (org-timestamp-change 1 'day))
  13618. (t (call-interactively 'org-deadline)))
  13619. (call-interactively 'org-deadline)))
  13620. ((member property org-special-properties)
  13621. (error "The %s property can not yet be set with `org-entry-put'"
  13622. property))
  13623. (t ; a non-special property
  13624. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13625. (setq range (org-get-property-block beg end 'force))
  13626. (goto-char (car range))
  13627. (if (re-search-forward
  13628. (org-re-property-keyword property) (cdr range) t)
  13629. (progn
  13630. (delete-region (match-beginning 0) (match-end 0))
  13631. (goto-char (match-beginning 0)))
  13632. (goto-char (cdr range))
  13633. (insert "\n")
  13634. (backward-char 1)
  13635. (org-indent-line))
  13636. (insert ":" property ":")
  13637. (and value (insert " " value))
  13638. (org-indent-line)))))
  13639. (run-hook-with-args 'org-property-changed-functions property value)))
  13640. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13641. "Get all property keys in the current buffer.
  13642. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13643. like tags and TODO state.
  13644. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13645. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13646. and others.
  13647. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13648. formats in the current buffer."
  13649. (let (rtn range cfmt s p)
  13650. (save-excursion
  13651. (save-restriction
  13652. (widen)
  13653. (goto-char (point-min))
  13654. (while (re-search-forward org-property-start-re nil t)
  13655. (setq range (org-get-property-block))
  13656. (goto-char (car range))
  13657. (while (re-search-forward
  13658. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13659. (cdr range) t)
  13660. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13661. (outline-next-heading))))
  13662. (when include-specials
  13663. (setq rtn (append org-special-properties rtn)))
  13664. (when include-defaults
  13665. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13666. (add-to-list 'rtn org-effort-property))
  13667. (when include-columns
  13668. (save-excursion
  13669. (save-restriction
  13670. (widen)
  13671. (goto-char (point-min))
  13672. (while (re-search-forward
  13673. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13674. nil t)
  13675. (setq cfmt (match-string 2) s 0)
  13676. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13677. cfmt s)
  13678. (setq s (match-end 0)
  13679. p (match-string 1 cfmt))
  13680. (unless (or (equal p "ITEM")
  13681. (member p org-special-properties))
  13682. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13683. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13684. (defun org-property-values (key)
  13685. "Return a list of all values of property KEY in the current buffer."
  13686. (save-excursion
  13687. (save-restriction
  13688. (widen)
  13689. (goto-char (point-min))
  13690. (let ((re (org-re-property key))
  13691. values)
  13692. (while (re-search-forward re nil t)
  13693. (add-to-list 'values (org-trim (match-string 1))))
  13694. (delete "" values)))))
  13695. (defun org-insert-property-drawer ()
  13696. "Insert a property drawer into the current entry."
  13697. (org-back-to-heading t)
  13698. (looking-at org-outline-regexp)
  13699. (let ((indent (if org-adapt-indentation
  13700. (- (match-end 0) (match-beginning 0))
  13701. 0))
  13702. (beg (point))
  13703. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13704. end hiddenp)
  13705. (outline-next-heading)
  13706. (setq end (point))
  13707. (goto-char beg)
  13708. (while (re-search-forward re end t))
  13709. (setq hiddenp (outline-invisible-p))
  13710. (end-of-line 1)
  13711. (and (equal (char-after) ?\n) (forward-char 1))
  13712. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13713. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13714. ;; just skip this line
  13715. (beginning-of-line 2)
  13716. ;; Drawer start, find the end
  13717. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13718. (beginning-of-line 1)))
  13719. (org-skip-over-state-notes)
  13720. (skip-chars-backward " \t\n\r")
  13721. (if (eq (char-before) ?*) (forward-char 1))
  13722. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13723. (beginning-of-line 0)
  13724. (org-indent-to-column indent)
  13725. (beginning-of-line 2)
  13726. (org-indent-to-column indent)
  13727. (beginning-of-line 0)
  13728. (if hiddenp
  13729. (save-excursion
  13730. (org-back-to-heading t)
  13731. (hide-entry))
  13732. (org-flag-drawer t))))
  13733. (defun org-insert-drawer (&optional arg drawer)
  13734. "Insert a drawer at point.
  13735. Optional argument DRAWER, when non-nil, is a string representing
  13736. drawer's name. Otherwise, the user is prompted for a name.
  13737. If a region is active, insert the drawer around that region
  13738. instead.
  13739. Point is left between drawer's boundaries."
  13740. (interactive "P")
  13741. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13742. "LOGBOOK"))
  13743. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13744. ;; internally by Org. They are meant to be inserted
  13745. ;; automatically.
  13746. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13747. ;; Remove system drawers from list. Note: For some reason,
  13748. ;; `org-completing-read' ignores the predicate while
  13749. ;; `completing-read' handles it fine.
  13750. (drawer (if arg "PROPERTIES"
  13751. (or drawer
  13752. (completing-read
  13753. "Drawer: " org-drawers
  13754. (lambda (d) (not (member d system-drawers))))))))
  13755. (cond
  13756. ;; With C-u, fall back on `org-insert-property-drawer'
  13757. (arg (org-insert-property-drawer))
  13758. ;; With an active region, insert a drawer at point.
  13759. ((not (org-region-active-p))
  13760. (progn
  13761. (unless (bolp) (insert "\n"))
  13762. (insert (format ":%s:\n\n:END:\n" drawer))
  13763. (forward-line -2)))
  13764. ;; Otherwise, insert the drawer at point
  13765. (t
  13766. (let ((rbeg (region-beginning))
  13767. (rend (copy-marker (region-end))))
  13768. (unwind-protect
  13769. (progn
  13770. (goto-char rbeg)
  13771. (beginning-of-line)
  13772. (when (save-excursion
  13773. (re-search-forward org-outline-regexp-bol rend t))
  13774. (error "Drawers cannot contain headlines"))
  13775. ;; Position point at the beginning of the first
  13776. ;; non-blank line in region. Insert drawer's opening
  13777. ;; there, then indent it.
  13778. (org-skip-whitespace)
  13779. (beginning-of-line)
  13780. (insert ":" drawer ":\n")
  13781. (forward-line -1)
  13782. (indent-for-tab-command)
  13783. ;; Move point to the beginning of the first blank line
  13784. ;; after the last non-blank line in region. Insert
  13785. ;; drawer's closing, then indent it.
  13786. (goto-char rend)
  13787. (skip-chars-backward " \r\t\n")
  13788. (insert "\n:END:")
  13789. (deactivate-mark t)
  13790. (indent-for-tab-command)
  13791. (unless (eolp) (insert "\n")))
  13792. ;; Clear marker, whatever the outcome of insertion is.
  13793. (set-marker rend nil)))))))
  13794. (defvar org-property-set-functions-alist nil
  13795. "Property set function alist.
  13796. Each entry should have the following format:
  13797. (PROPERTY . READ-FUNCTION)
  13798. The read function will be called with the same argument as
  13799. `org-completing-read'.")
  13800. (defun org-set-property-function (property)
  13801. "Get the function that should be used to set PROPERTY.
  13802. This is computed according to `org-property-set-functions-alist'."
  13803. (or (cdr (assoc property org-property-set-functions-alist))
  13804. 'org-completing-read))
  13805. (defun org-read-property-value (property)
  13806. "Read PROPERTY value from user."
  13807. (let* ((completion-ignore-case t)
  13808. (allowed (org-property-get-allowed-values nil property 'table))
  13809. (cur (org-entry-get nil property))
  13810. (prompt (concat property " value"
  13811. (if (and cur (string-match "\\S-" cur))
  13812. (concat " [" cur "]") "") ": "))
  13813. (set-function (org-set-property-function property))
  13814. (val (if allowed
  13815. (funcall set-function prompt allowed nil
  13816. (not (get-text-property 0 'org-unrestricted
  13817. (caar allowed))))
  13818. (let (org-completion-use-ido org-completion-use-iswitchb)
  13819. (funcall set-function prompt
  13820. (mapcar 'list (org-property-values property))
  13821. nil nil "" nil cur)))))
  13822. (if (equal val "")
  13823. cur
  13824. val)))
  13825. (defvar org-last-set-property nil)
  13826. (defvar org-last-set-property-value nil)
  13827. (defun org-read-property-name ()
  13828. "Read a property name."
  13829. (let* ((completion-ignore-case t)
  13830. (keys (org-buffer-property-keys nil t t))
  13831. (default-prop (or (save-excursion
  13832. (save-match-data
  13833. (beginning-of-line)
  13834. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13835. (null (string= (match-string 1) "END"))
  13836. (match-string 1))))
  13837. org-last-set-property))
  13838. (property (org-icompleting-read
  13839. (concat "Property"
  13840. (if default-prop (concat " [" default-prop "]") "")
  13841. ": ")
  13842. (mapcar 'list keys)
  13843. nil nil nil nil
  13844. default-prop)))
  13845. (if (member property keys)
  13846. property
  13847. (or (cdr (assoc (downcase property)
  13848. (mapcar (lambda (x) (cons (downcase x) x))
  13849. keys)))
  13850. property))))
  13851. (defun org-set-property-and-value (use-last)
  13852. "Allow to set [PROPERTY]: [value] direction from prompt.
  13853. When use-default, don't even ask, just use the last
  13854. \"[PROPERTY]: [value]\" string from the history."
  13855. (interactive "P")
  13856. (let* ((completion-ignore-case t)
  13857. (pv (or (and use-last org-last-set-property-value)
  13858. (org-completing-read
  13859. "Enter a \"[Property]: [value]\" pair: "
  13860. nil nil nil nil nil
  13861. org-last-set-property-value)))
  13862. prop val)
  13863. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  13864. (setq prop (match-string 1 pv)
  13865. val (match-string 2 pv))
  13866. (org-set-property prop val))))
  13867. (defun org-set-property (property value)
  13868. "In the current entry, set PROPERTY to VALUE.
  13869. When called interactively, this will prompt for a property name, offering
  13870. completion on existing and default properties. And then it will prompt
  13871. for a value, offering completion either on allowed values (via an inherited
  13872. xxx_ALL property) or on existing values in other instances of this property
  13873. in the current file."
  13874. (interactive (list nil nil))
  13875. (let* ((property (or property (org-read-property-name)))
  13876. (value (or value (org-read-property-value property)))
  13877. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13878. (setq org-last-set-property property)
  13879. (setq org-last-set-property-value (concat property ": " value))
  13880. ;; Possibly postprocess the inserted value:
  13881. (when fn (setq value (funcall fn value)))
  13882. (unless (equal (org-entry-get nil property) value)
  13883. (org-entry-put nil property value))))
  13884. (defun org-delete-property (property)
  13885. "In the current entry, delete PROPERTY."
  13886. (interactive
  13887. (let* ((completion-ignore-case t)
  13888. (prop (org-icompleting-read "Property: "
  13889. (org-entry-properties nil 'standard))))
  13890. (list prop)))
  13891. (message "Property %s %s" property
  13892. (if (org-entry-delete nil property)
  13893. "deleted"
  13894. "was not present in the entry")))
  13895. (defun org-delete-property-globally (property)
  13896. "Remove PROPERTY globally, from all entries."
  13897. (interactive
  13898. (let* ((completion-ignore-case t)
  13899. (prop (org-icompleting-read
  13900. "Globally remove property: "
  13901. (mapcar 'list (org-buffer-property-keys)))))
  13902. (list prop)))
  13903. (save-excursion
  13904. (save-restriction
  13905. (widen)
  13906. (goto-char (point-min))
  13907. (let ((cnt 0))
  13908. (while (re-search-forward
  13909. (org-re-property property)
  13910. nil t)
  13911. (setq cnt (1+ cnt))
  13912. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13913. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13914. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13915. (defun org-compute-property-at-point ()
  13916. "Compute the property at point.
  13917. This looks for an enclosing column format, extracts the operator and
  13918. then applies it to the property in the column format's scope."
  13919. (interactive)
  13920. (unless (org-at-property-p)
  13921. (error "Not at a property"))
  13922. (let ((prop (org-match-string-no-properties 2)))
  13923. (org-columns-get-format-and-top-level)
  13924. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13925. (error "No operator defined for property %s" prop))
  13926. (org-columns-compute prop)))
  13927. (defvar org-property-allowed-value-functions nil
  13928. "Hook for functions supplying allowed values for a specific property.
  13929. The functions must take a single argument, the name of the property, and
  13930. return a flat list of allowed values. If \":ETC\" is one of
  13931. the values, this means that these values are intended as defaults for
  13932. completion, but that other values should be allowed too.
  13933. The functions must return nil if they are not responsible for this
  13934. property.")
  13935. (defun org-property-get-allowed-values (pom property &optional table)
  13936. "Get allowed values for the property PROPERTY.
  13937. When TABLE is non-nil, return an alist that can directly be used for
  13938. completion."
  13939. (let (vals)
  13940. (cond
  13941. ((equal property "TODO")
  13942. (setq vals (org-with-point-at pom
  13943. (append org-todo-keywords-1 '("")))))
  13944. ((equal property "PRIORITY")
  13945. (let ((n org-lowest-priority))
  13946. (while (>= n org-highest-priority)
  13947. (push (char-to-string n) vals)
  13948. (setq n (1- n)))))
  13949. ((member property org-special-properties))
  13950. ((setq vals (run-hook-with-args-until-success
  13951. 'org-property-allowed-value-functions property)))
  13952. (t
  13953. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13954. (when (and vals (string-match "\\S-" vals))
  13955. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13956. (setq vals (mapcar (lambda (x)
  13957. (cond ((stringp x) x)
  13958. ((numberp x) (number-to-string x))
  13959. ((symbolp x) (symbol-name x))
  13960. (t "???")))
  13961. vals)))))
  13962. (when (member ":ETC" vals)
  13963. (setq vals (remove ":ETC" vals))
  13964. (org-add-props (car vals) '(org-unrestricted t)))
  13965. (if table (mapcar 'list vals) vals)))
  13966. (defun org-property-previous-allowed-value (&optional previous)
  13967. "Switch to the next allowed value for this property."
  13968. (interactive)
  13969. (org-property-next-allowed-value t))
  13970. (defun org-property-next-allowed-value (&optional previous)
  13971. "Switch to the next allowed value for this property."
  13972. (interactive)
  13973. (unless (org-at-property-p)
  13974. (error "Not at a property"))
  13975. (let* ((key (match-string 2))
  13976. (value (match-string 3))
  13977. (allowed (or (org-property-get-allowed-values (point) key)
  13978. (and (member value '("[ ]" "[-]" "[X]"))
  13979. '("[ ]" "[X]"))))
  13980. nval)
  13981. (unless allowed
  13982. (error "Allowed values for this property have not been defined"))
  13983. (if previous (setq allowed (reverse allowed)))
  13984. (if (member value allowed)
  13985. (setq nval (car (cdr (member value allowed)))))
  13986. (setq nval (or nval (car allowed)))
  13987. (if (equal nval value)
  13988. (error "Only one allowed value for this property"))
  13989. (org-at-property-p)
  13990. (replace-match (concat " :" key ": " nval) t t)
  13991. (org-indent-line)
  13992. (beginning-of-line 1)
  13993. (skip-chars-forward " \t")
  13994. (run-hook-with-args 'org-property-changed-functions key nval)))
  13995. (defun org-find-olp (path &optional this-buffer)
  13996. "Return a marker pointing to the entry at outline path OLP.
  13997. If anything goes wrong, throw an error.
  13998. You can wrap this call to catch the error like this:
  13999. (condition-case msg
  14000. (org-mobile-locate-entry (match-string 4))
  14001. (error (nth 1 msg)))
  14002. The return value will then be either a string with the error message,
  14003. or a marker if everything is OK.
  14004. If THIS-BUFFER is set, the outline path does not contain a file,
  14005. only headings."
  14006. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14007. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14008. (level 1)
  14009. (lmin 1)
  14010. (lmax 1)
  14011. limit re end found pos heading cnt flevel)
  14012. (unless buffer (error "File not found :%s" file))
  14013. (with-current-buffer buffer
  14014. (save-excursion
  14015. (save-restriction
  14016. (widen)
  14017. (setq limit (point-max))
  14018. (goto-char (point-min))
  14019. (while (setq heading (pop path))
  14020. (setq re (format org-complex-heading-regexp-format
  14021. (regexp-quote heading)))
  14022. (setq cnt 0 pos (point))
  14023. (while (re-search-forward re end t)
  14024. (setq level (- (match-end 1) (match-beginning 1)))
  14025. (if (and (>= level lmin) (<= level lmax))
  14026. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14027. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14028. lmax heading))
  14029. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14030. lmax heading))
  14031. (goto-char found)
  14032. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14033. (setq end (save-excursion (org-end-of-subtree t t))))
  14034. (when (org-at-heading-p)
  14035. (point-marker)))))))
  14036. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14037. "Find node HEADING in BUFFER.
  14038. Return a marker to the heading if it was found, or nil if not.
  14039. If POS-ONLY is set, return just the position instead of a marker.
  14040. The heading text must match exact, but it may have a TODO keyword,
  14041. a priority cookie and tags in the standard locations."
  14042. (with-current-buffer (or buffer (current-buffer))
  14043. (save-excursion
  14044. (save-restriction
  14045. (widen)
  14046. (goto-char (point-min))
  14047. (let (case-fold-search)
  14048. (if (re-search-forward
  14049. (format org-complex-heading-regexp-format
  14050. (regexp-quote heading)) nil t)
  14051. (if pos-only
  14052. (match-beginning 0)
  14053. (move-marker (make-marker) (match-beginning 0)))))))))
  14054. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14055. "Find Org node headline HEADING in all .org files in directory DIR.
  14056. When the target headline is found, return a marker to this location."
  14057. (let ((files (directory-files (or dir default-directory)
  14058. nil "\\`[^.#].*\\.org\\'"))
  14059. file visiting m buffer)
  14060. (catch 'found
  14061. (while (setq file (pop files))
  14062. (message "trying %s" file)
  14063. (setq visiting (org-find-base-buffer-visiting file))
  14064. (setq buffer (or visiting (find-file-noselect file)))
  14065. (setq m (org-find-exact-headline-in-buffer
  14066. heading buffer))
  14067. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14068. (and m (throw 'found m))))))
  14069. (defun org-find-entry-with-id (ident)
  14070. "Locate the entry that contains the ID property with exact value IDENT.
  14071. IDENT can be a string, a symbol or a number, this function will search for
  14072. the string representation of it.
  14073. Return the position where this entry starts, or nil if there is no such entry."
  14074. (interactive "sID: ")
  14075. (let ((id (cond
  14076. ((stringp ident) ident)
  14077. ((symbol-name ident) (symbol-name ident))
  14078. ((numberp ident) (number-to-string ident))
  14079. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14080. (case-fold-search nil))
  14081. (save-excursion
  14082. (save-restriction
  14083. (widen)
  14084. (goto-char (point-min))
  14085. (when (re-search-forward
  14086. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14087. nil t)
  14088. (org-back-to-heading t)
  14089. (point))))))
  14090. ;;;; Timestamps
  14091. (defvar org-last-changed-timestamp nil)
  14092. (defvar org-last-inserted-timestamp nil
  14093. "The last time stamp inserted with `org-insert-time-stamp'.")
  14094. (defvar org-time-was-given) ; dynamically scoped parameter
  14095. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14096. (defvar org-ts-what) ; dynamically scoped parameter
  14097. (defun org-time-stamp (arg &optional inactive)
  14098. "Prompt for a date/time and insert a time stamp.
  14099. If the user specifies a time like HH:MM or if this command is
  14100. called with at least one prefix argument, the time stamp contains
  14101. the date and the time. Otherwise, only the date is be included.
  14102. All parts of a date not specified by the user is filled in from
  14103. the current date/time. So if you just press return without
  14104. typing anything, the time stamp will represent the current
  14105. date/time.
  14106. If there is already a timestamp at the cursor, it will be
  14107. modified.
  14108. With two universal prefix arguments, insert an active timestamp
  14109. with the current time without prompting the user."
  14110. (interactive "P")
  14111. (let* ((ts nil)
  14112. (default-time
  14113. ;; Default time is either today, or, when entering a range,
  14114. ;; the range start.
  14115. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14116. (save-excursion
  14117. (re-search-backward
  14118. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14119. (- (point) 20) t)))
  14120. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14121. (current-time)))
  14122. (default-input (and ts (org-get-compact-tod ts)))
  14123. (repeater (save-excursion
  14124. (save-match-data
  14125. (beginning-of-line)
  14126. (when (re-search-forward
  14127. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14128. (save-excursion (progn (end-of-line) (point))) t)
  14129. (match-string 0)))))
  14130. org-time-was-given org-end-time-was-given time)
  14131. (cond
  14132. ((and (org-at-timestamp-p t)
  14133. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14134. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14135. (insert "--")
  14136. (setq time (let ((this-command this-command))
  14137. (org-read-date arg 'totime nil nil
  14138. default-time default-input inactive)))
  14139. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14140. ((org-at-timestamp-p t)
  14141. (setq time (let ((this-command this-command))
  14142. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14143. (when (org-at-timestamp-p t) ; just to get the match data
  14144. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14145. (replace-match "")
  14146. (setq org-last-changed-timestamp
  14147. (org-insert-time-stamp
  14148. time (or org-time-was-given arg)
  14149. inactive nil nil (list org-end-time-was-given)))
  14150. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14151. (setq org-last-changed-timestamp
  14152. (concat (substring org-last-inserted-timestamp 0 -1)
  14153. " " repeater ">"))))
  14154. (message "Timestamp updated"))
  14155. ((equal arg '(16))
  14156. (org-insert-time-stamp (current-time) t))
  14157. (t
  14158. (setq time (let ((this-command this-command))
  14159. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14160. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14161. nil nil (list org-end-time-was-given))))))
  14162. ;; FIXME: can we use this for something else, like computing time differences?
  14163. (defun org-get-compact-tod (s)
  14164. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14165. (let* ((t1 (match-string 1 s))
  14166. (h1 (string-to-number (match-string 2 s)))
  14167. (m1 (string-to-number (match-string 3 s)))
  14168. (t2 (and (match-end 4) (match-string 5 s)))
  14169. (h2 (and t2 (string-to-number (match-string 6 s))))
  14170. (m2 (and t2 (string-to-number (match-string 7 s))))
  14171. dh dm)
  14172. (if (not t2)
  14173. t1
  14174. (setq dh (- h2 h1) dm (- m2 m1))
  14175. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14176. (concat t1 "+" (number-to-string dh)
  14177. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14178. (defun org-time-stamp-inactive (&optional arg)
  14179. "Insert an inactive time stamp.
  14180. An inactive time stamp is enclosed in square brackets instead of angle
  14181. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14182. does not link to the calendar and cannot be changed with the S-cursor keys.
  14183. So these are more for recording a certain time/date."
  14184. (interactive "P")
  14185. (org-time-stamp arg 'inactive))
  14186. (defvar org-date-ovl (make-overlay 1 1))
  14187. (overlay-put org-date-ovl 'face 'org-date-selected)
  14188. (org-detach-overlay org-date-ovl)
  14189. (defvar org-ans1) ; dynamically scoped parameter
  14190. (defvar org-ans2) ; dynamically scoped parameter
  14191. (defvar org-plain-time-of-day-regexp) ; defined below
  14192. (defvar org-overriding-default-time nil) ; dynamically scoped
  14193. (defvar org-read-date-overlay nil)
  14194. (defvar org-dcst nil) ; dynamically scoped
  14195. (defvar org-read-date-history nil)
  14196. (defvar org-read-date-final-answer nil)
  14197. (defvar org-read-date-analyze-futurep nil)
  14198. (defvar org-read-date-analyze-forced-year nil)
  14199. (defvar org-read-date-inactive)
  14200. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14201. default-time default-input inactive)
  14202. "Read a date, possibly a time, and make things smooth for the user.
  14203. The prompt will suggest to enter an ISO date, but you can also enter anything
  14204. which will at least partially be understood by `parse-time-string'.
  14205. Unrecognized parts of the date will default to the current day, month, year,
  14206. hour and minute. If this command is called to replace a timestamp at point,
  14207. or to enter the second timestamp of a range, the default time is taken
  14208. from the existing stamp. Furthermore, the command prefers the future,
  14209. so if you are giving a date where the year is not given, and the day-month
  14210. combination is already past in the current year, it will assume you
  14211. mean next year. For details, see the manual. A few examples:
  14212. 3-2-5 --> 2003-02-05
  14213. feb 15 --> currentyear-02-15
  14214. 2/15 --> currentyear-02-15
  14215. sep 12 9 --> 2009-09-12
  14216. 12:45 --> today 12:45
  14217. 22 sept 0:34 --> currentyear-09-22 0:34
  14218. 12 --> currentyear-currentmonth-12
  14219. Fri --> nearest Friday (today or later)
  14220. etc.
  14221. Furthermore you can specify a relative date by giving, as the *first* thing
  14222. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14223. change in days weeks, months, years.
  14224. With a single plus or minus, the date is relative to today. With a double
  14225. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14226. +4d --> four days from today
  14227. +4 --> same as above
  14228. +2w --> two weeks from today
  14229. ++5 --> five days from default date
  14230. The function understands only English month and weekday abbreviations.
  14231. While prompting, a calendar is popped up - you can also select the
  14232. date with the mouse (button 1). The calendar shows a period of three
  14233. months. To scroll it to other months, use the keys `>' and `<'.
  14234. If you don't like the calendar, turn it off with
  14235. \(setq org-read-date-popup-calendar nil)
  14236. With optional argument TO-TIME, the date will immediately be converted
  14237. to an internal time.
  14238. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14239. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14240. still enter a time, and this function will inform the calling routine
  14241. about this change. The calling routine may then choose to change the
  14242. format used to insert the time stamp into the buffer to include the time.
  14243. With optional argument FROM-STRING, read from this string instead from
  14244. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14245. the time/date that is used for everything that is not specified by the
  14246. user."
  14247. (require 'parse-time)
  14248. (let* ((org-time-stamp-rounding-minutes
  14249. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14250. (org-dcst org-display-custom-times)
  14251. (ct (org-current-time))
  14252. (org-def (or org-overriding-default-time default-time ct))
  14253. (org-defdecode (decode-time org-def))
  14254. (dummy (progn
  14255. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14256. (setcar (nthcdr 2 org-defdecode) -1)
  14257. (setcar (nthcdr 1 org-defdecode) 59)
  14258. (setq org-def (apply 'encode-time org-defdecode)
  14259. org-defdecode (decode-time org-def)))))
  14260. (calendar-frame-setup nil)
  14261. (calendar-setup nil)
  14262. (calendar-move-hook nil)
  14263. (calendar-view-diary-initially-flag nil)
  14264. (calendar-view-holidays-initially-flag nil)
  14265. (timestr (format-time-string
  14266. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14267. (prompt (concat (if prompt (concat prompt " ") "")
  14268. (format "Date+time [%s]: " timestr)))
  14269. ans (org-ans0 "") org-ans1 org-ans2 final)
  14270. (cond
  14271. (from-string (setq ans from-string))
  14272. (org-read-date-popup-calendar
  14273. (save-excursion
  14274. (save-window-excursion
  14275. (calendar)
  14276. (org-eval-in-calendar '(setq cursor-type nil) t)
  14277. (unwind-protect
  14278. (progn
  14279. (calendar-forward-day (- (time-to-days org-def)
  14280. (calendar-absolute-from-gregorian
  14281. (calendar-current-date))))
  14282. (org-eval-in-calendar nil t)
  14283. (let* ((old-map (current-local-map))
  14284. (map (copy-keymap calendar-mode-map))
  14285. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  14286. (org-defkey map (kbd "RET") 'org-calendar-select)
  14287. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14288. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14289. (org-defkey minibuffer-local-map [(meta shift left)]
  14290. (lambda () (interactive)
  14291. (org-eval-in-calendar '(calendar-backward-month 1))))
  14292. (org-defkey minibuffer-local-map [(meta shift right)]
  14293. (lambda () (interactive)
  14294. (org-eval-in-calendar '(calendar-forward-month 1))))
  14295. (org-defkey minibuffer-local-map [(meta shift up)]
  14296. (lambda () (interactive)
  14297. (org-eval-in-calendar '(calendar-backward-year 1))))
  14298. (org-defkey minibuffer-local-map [(meta shift down)]
  14299. (lambda () (interactive)
  14300. (org-eval-in-calendar '(calendar-forward-year 1))))
  14301. (org-defkey minibuffer-local-map [?\e (shift left)]
  14302. (lambda () (interactive)
  14303. (org-eval-in-calendar '(calendar-backward-month 1))))
  14304. (org-defkey minibuffer-local-map [?\e (shift right)]
  14305. (lambda () (interactive)
  14306. (org-eval-in-calendar '(calendar-forward-month 1))))
  14307. (org-defkey minibuffer-local-map [?\e (shift up)]
  14308. (lambda () (interactive)
  14309. (org-eval-in-calendar '(calendar-backward-year 1))))
  14310. (org-defkey minibuffer-local-map [?\e (shift down)]
  14311. (lambda () (interactive)
  14312. (org-eval-in-calendar '(calendar-forward-year 1))))
  14313. (org-defkey minibuffer-local-map [(shift up)]
  14314. (lambda () (interactive)
  14315. (org-eval-in-calendar '(calendar-backward-week 1))))
  14316. (org-defkey minibuffer-local-map [(shift down)]
  14317. (lambda () (interactive)
  14318. (org-eval-in-calendar '(calendar-forward-week 1))))
  14319. (org-defkey minibuffer-local-map [(shift left)]
  14320. (lambda () (interactive)
  14321. (org-eval-in-calendar '(calendar-backward-day 1))))
  14322. (org-defkey minibuffer-local-map [(shift right)]
  14323. (lambda () (interactive)
  14324. (org-eval-in-calendar '(calendar-forward-day 1))))
  14325. (org-defkey minibuffer-local-map ">"
  14326. (lambda () (interactive)
  14327. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14328. (org-defkey minibuffer-local-map "<"
  14329. (lambda () (interactive)
  14330. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14331. (org-defkey minibuffer-local-map "\C-v"
  14332. (lambda () (interactive)
  14333. (org-eval-in-calendar
  14334. '(calendar-scroll-left-three-months 1))))
  14335. (org-defkey minibuffer-local-map "\M-v"
  14336. (lambda () (interactive)
  14337. (org-eval-in-calendar
  14338. '(calendar-scroll-right-three-months 1))))
  14339. (run-hooks 'org-read-date-minibuffer-setup-hook)
  14340. (unwind-protect
  14341. (progn
  14342. (use-local-map map)
  14343. (setq org-read-date-inactive inactive)
  14344. (add-hook 'post-command-hook 'org-read-date-display)
  14345. (setq org-ans0 (read-string prompt default-input
  14346. 'org-read-date-history nil))
  14347. ;; org-ans0: from prompt
  14348. ;; org-ans1: from mouse click
  14349. ;; org-ans2: from calendar motion
  14350. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14351. (remove-hook 'post-command-hook 'org-read-date-display)
  14352. (use-local-map old-map)
  14353. (when org-read-date-overlay
  14354. (delete-overlay org-read-date-overlay)
  14355. (setq org-read-date-overlay nil)))))
  14356. (bury-buffer "*Calendar*")))))
  14357. (t ; Naked prompt only
  14358. (unwind-protect
  14359. (setq ans (read-string prompt default-input
  14360. 'org-read-date-history timestr))
  14361. (when org-read-date-overlay
  14362. (delete-overlay org-read-date-overlay)
  14363. (setq org-read-date-overlay nil)))))
  14364. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14365. (when org-read-date-analyze-forced-year
  14366. (message "Year was forced into %s"
  14367. (if org-read-date-force-compatible-dates
  14368. "compatible range (1970-2037)"
  14369. "range representable on this machine"))
  14370. (ding))
  14371. ;; One round trip to get rid of 34th of August and stuff like that....
  14372. (setq final (decode-time (apply 'encode-time final)))
  14373. (setq org-read-date-final-answer ans)
  14374. (if to-time
  14375. (apply 'encode-time final)
  14376. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14377. (format "%04d-%02d-%02d %02d:%02d"
  14378. (nth 5 final) (nth 4 final) (nth 3 final)
  14379. (nth 2 final) (nth 1 final))
  14380. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14381. (defvar org-def)
  14382. (defvar org-defdecode)
  14383. (defvar org-with-time)
  14384. (defun org-read-date-display ()
  14385. "Display the current date prompt interpretation in the minibuffer."
  14386. (when org-read-date-display-live
  14387. (when org-read-date-overlay
  14388. (delete-overlay org-read-date-overlay))
  14389. (when (minibufferp (current-buffer))
  14390. (save-excursion
  14391. (end-of-line 1)
  14392. (while (not (equal (buffer-substring
  14393. (max (point-min) (- (point) 4)) (point))
  14394. " "))
  14395. (insert " ")))
  14396. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14397. " " (or org-ans1 org-ans2)))
  14398. (org-end-time-was-given nil)
  14399. (f (org-read-date-analyze ans org-def org-defdecode))
  14400. (fmts (if org-dcst
  14401. org-time-stamp-custom-formats
  14402. org-time-stamp-formats))
  14403. (fmt (if (or org-with-time
  14404. (and (boundp 'org-time-was-given) org-time-was-given))
  14405. (cdr fmts)
  14406. (car fmts)))
  14407. (txt (format-time-string fmt (apply 'encode-time f)))
  14408. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14409. (txt (concat "=> " txt)))
  14410. (when (and org-end-time-was-given
  14411. (string-match org-plain-time-of-day-regexp txt))
  14412. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14413. org-end-time-was-given
  14414. (substring txt (match-end 0)))))
  14415. (when org-read-date-analyze-futurep
  14416. (setq txt (concat txt " (=>F)")))
  14417. (setq org-read-date-overlay
  14418. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14419. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14420. (defun org-read-date-analyze (ans org-def org-defdecode)
  14421. "Analyze the combined answer of the date prompt."
  14422. ;; FIXME: cleanup and comment
  14423. (let ((nowdecode (decode-time (current-time)))
  14424. delta deltan deltaw deltadef year month day
  14425. hour minute second wday pm h2 m2 tl wday1
  14426. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14427. (setq org-read-date-analyze-futurep nil
  14428. org-read-date-analyze-forced-year nil)
  14429. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14430. (setq ans "+0"))
  14431. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14432. (setq ans (replace-match "" t t ans)
  14433. deltan (car delta)
  14434. deltaw (nth 1 delta)
  14435. deltadef (nth 2 delta)))
  14436. ;; Check if there is an iso week date in there. If yes, store the
  14437. ;; info and postpone interpreting it until the rest of the parsing
  14438. ;; is done.
  14439. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14440. (setq iso-year (if (match-end 1)
  14441. (org-small-year-to-year
  14442. (string-to-number (match-string 1 ans))))
  14443. iso-weekday (if (match-end 3)
  14444. (string-to-number (match-string 3 ans)))
  14445. iso-week (string-to-number (match-string 2 ans)))
  14446. (setq ans (replace-match "" t t ans)))
  14447. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14448. (when (string-match
  14449. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14450. (setq year (if (match-end 2)
  14451. (string-to-number (match-string 2 ans))
  14452. (progn (setq kill-year t)
  14453. (string-to-number (format-time-string "%Y"))))
  14454. month (string-to-number (match-string 3 ans))
  14455. day (string-to-number (match-string 4 ans)))
  14456. (if (< year 100) (setq year (+ 2000 year)))
  14457. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14458. t nil ans)))
  14459. ;; Help matching dotted european dates
  14460. (when (string-match
  14461. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14462. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14463. (setq kill-year t)
  14464. (string-to-number (format-time-string "%Y")))
  14465. day (string-to-number (match-string 1 ans))
  14466. month (string-to-number (match-string 2 ans))
  14467. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14468. t nil ans)))
  14469. ;; Help matching american dates, like 5/30 or 5/30/7
  14470. (when (string-match
  14471. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14472. (setq year (if (match-end 4)
  14473. (string-to-number (match-string 4 ans))
  14474. (progn (setq kill-year t)
  14475. (string-to-number (format-time-string "%Y"))))
  14476. month (string-to-number (match-string 1 ans))
  14477. day (string-to-number (match-string 2 ans)))
  14478. (if (< year 100) (setq year (+ 2000 year)))
  14479. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14480. t nil ans)))
  14481. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14482. ;; If there is a time with am/pm, and *no* time without it, we convert
  14483. ;; so that matching will be successful.
  14484. (loop for i from 1 to 2 do ; twice, for end time as well
  14485. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14486. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14487. (setq hour (string-to-number (match-string 1 ans))
  14488. minute (if (match-end 3)
  14489. (string-to-number (match-string 3 ans))
  14490. 0)
  14491. pm (equal ?p
  14492. (string-to-char (downcase (match-string 4 ans)))))
  14493. (if (and (= hour 12) (not pm))
  14494. (setq hour 0)
  14495. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14496. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14497. t t ans))))
  14498. ;; Check if a time range is given as a duration
  14499. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14500. (setq hour (string-to-number (match-string 1 ans))
  14501. h2 (+ hour (string-to-number (match-string 3 ans)))
  14502. minute (string-to-number (match-string 2 ans))
  14503. m2 (+ minute (if (match-end 5) (string-to-number
  14504. (match-string 5 ans))0)))
  14505. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14506. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14507. t t ans)))
  14508. ;; Check if there is a time range
  14509. (when (boundp 'org-end-time-was-given)
  14510. (setq org-time-was-given nil)
  14511. (when (and (string-match org-plain-time-of-day-regexp ans)
  14512. (match-end 8))
  14513. (setq org-end-time-was-given (match-string 8 ans))
  14514. (setq ans (concat (substring ans 0 (match-beginning 7))
  14515. (substring ans (match-end 7))))))
  14516. (setq tl (parse-time-string ans)
  14517. day (or (nth 3 tl) (nth 3 org-defdecode))
  14518. month (or (nth 4 tl)
  14519. (if (and org-read-date-prefer-future
  14520. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14521. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14522. (nth 4 org-defdecode)))
  14523. year (or (and (not kill-year) (nth 5 tl))
  14524. (if (and org-read-date-prefer-future
  14525. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14526. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14527. (nth 5 org-defdecode)))
  14528. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14529. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14530. second (or (nth 0 tl) 0)
  14531. wday (nth 6 tl))
  14532. (when (and (eq org-read-date-prefer-future 'time)
  14533. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14534. (equal day (nth 3 nowdecode))
  14535. (equal month (nth 4 nowdecode))
  14536. (equal year (nth 5 nowdecode))
  14537. (nth 2 tl)
  14538. (or (< (nth 2 tl) (nth 2 nowdecode))
  14539. (and (= (nth 2 tl) (nth 2 nowdecode))
  14540. (nth 1 tl)
  14541. (< (nth 1 tl) (nth 1 nowdecode)))))
  14542. (setq day (1+ day)
  14543. futurep t))
  14544. ;; Special date definitions below
  14545. (cond
  14546. (iso-week
  14547. ;; There was an iso week
  14548. (require 'cal-iso)
  14549. (setq futurep nil)
  14550. (setq year (or iso-year year)
  14551. day (or iso-weekday wday 1)
  14552. wday nil ; to make sure that the trigger below does not match
  14553. iso-date (calendar-gregorian-from-absolute
  14554. (calendar-absolute-from-iso
  14555. (list iso-week day year))))
  14556. ; FIXME: Should we also push ISO weeks into the future?
  14557. ; (when (and org-read-date-prefer-future
  14558. ; (not iso-year)
  14559. ; (< (calendar-absolute-from-gregorian iso-date)
  14560. ; (time-to-days (current-time))))
  14561. ; (setq year (1+ year)
  14562. ; iso-date (calendar-gregorian-from-absolute
  14563. ; (calendar-absolute-from-iso
  14564. ; (list iso-week day year)))))
  14565. (setq month (car iso-date)
  14566. year (nth 2 iso-date)
  14567. day (nth 1 iso-date)))
  14568. (deltan
  14569. (setq futurep nil)
  14570. (unless deltadef
  14571. (let ((now (decode-time (current-time))))
  14572. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14573. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14574. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14575. ((equal deltaw "m") (setq month (+ month deltan)))
  14576. ((equal deltaw "y") (setq year (+ year deltan)))))
  14577. ((and wday (not (nth 3 tl)))
  14578. ;; Weekday was given, but no day, so pick that day in the week
  14579. ;; on or after the derived date.
  14580. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14581. (unless (equal wday wday1)
  14582. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14583. (if (and (boundp 'org-time-was-given)
  14584. (nth 2 tl))
  14585. (setq org-time-was-given t))
  14586. (if (< year 100) (setq year (+ 2000 year)))
  14587. ;; Check of the date is representable
  14588. (if org-read-date-force-compatible-dates
  14589. (progn
  14590. (if (< year 1970)
  14591. (setq year 1970 org-read-date-analyze-forced-year t))
  14592. (if (> year 2037)
  14593. (setq year 2037 org-read-date-analyze-forced-year t)))
  14594. (condition-case nil
  14595. (ignore (encode-time second minute hour day month year))
  14596. (error
  14597. (setq year (nth 5 org-defdecode))
  14598. (setq org-read-date-analyze-forced-year t))))
  14599. (setq org-read-date-analyze-futurep futurep)
  14600. (list second minute hour day month year)))
  14601. (defvar parse-time-weekdays)
  14602. (defun org-read-date-get-relative (s today default)
  14603. "Check string S for special relative date string.
  14604. TODAY and DEFAULT are internal times, for today and for a default.
  14605. Return shift list (N what def-flag)
  14606. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14607. N is the number of WHATs to shift.
  14608. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14609. the DEFAULT date rather than TODAY."
  14610. (require 'parse-time)
  14611. (when (and
  14612. (string-match
  14613. (concat
  14614. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14615. "\\([0-9]+\\)?"
  14616. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14617. "\\([ \t]\\|$\\)") s)
  14618. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14619. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14620. (string-to-char (substring (match-string 1 s) -1))
  14621. ?+))
  14622. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14623. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14624. (what (if (match-end 3) (match-string 3 s) "d"))
  14625. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14626. (date (if rel default today))
  14627. (wday (nth 6 (decode-time date)))
  14628. delta)
  14629. (if wday1
  14630. (progn
  14631. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14632. (if (= dir ?-) (setq delta (- delta 7)))
  14633. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14634. (list delta "d" rel))
  14635. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14636. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14637. "Turn a user-specified date into the internal representation.
  14638. The internal representation needed by the calendar is (month day year).
  14639. This is a wrapper to handle the brain-dead convention in calendar that
  14640. user function argument order change dependent on argument order."
  14641. (if (boundp 'calendar-date-style)
  14642. (cond
  14643. ((eq calendar-date-style 'american)
  14644. (list arg1 arg2 arg3))
  14645. ((eq calendar-date-style 'european)
  14646. (list arg2 arg1 arg3))
  14647. ((eq calendar-date-style 'iso)
  14648. (list arg2 arg3 arg1)))
  14649. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14650. (if (org-bound-and-true-p european-calendar-style)
  14651. (list arg2 arg1 arg3)
  14652. (list arg1 arg2 arg3)))))
  14653. (defun org-eval-in-calendar (form &optional keepdate)
  14654. "Eval FORM in the calendar window and return to current window.
  14655. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14656. otherwise stick to the current value of `org-ans2'."
  14657. (let ((sf (selected-frame))
  14658. (sw (selected-window)))
  14659. (select-window (get-buffer-window "*Calendar*" t))
  14660. (eval form)
  14661. (when (and (not keepdate) (calendar-cursor-to-date))
  14662. (let* ((date (calendar-cursor-to-date))
  14663. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14664. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14665. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14666. (select-window sw)
  14667. (org-select-frame-set-input-focus sf)))
  14668. (defun org-calendar-select ()
  14669. "Return to `org-read-date' with the date currently selected.
  14670. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14671. (interactive)
  14672. (when (calendar-cursor-to-date)
  14673. (let* ((date (calendar-cursor-to-date))
  14674. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14675. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14676. (if (active-minibuffer-window) (exit-minibuffer))))
  14677. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14678. "Insert a date stamp for the date given by the internal TIME.
  14679. WITH-HM means use the stamp format that includes the time of the day.
  14680. INACTIVE means use square brackets instead of angular ones, so that the
  14681. stamp will not contribute to the agenda.
  14682. PRE and POST are optional strings to be inserted before and after the
  14683. stamp.
  14684. The command returns the inserted time stamp."
  14685. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14686. stamp)
  14687. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14688. (insert-before-markers (or pre ""))
  14689. (when (listp extra)
  14690. (setq extra (car extra))
  14691. (if (and (stringp extra)
  14692. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14693. (setq extra (format "-%02d:%02d"
  14694. (string-to-number (match-string 1 extra))
  14695. (string-to-number (match-string 2 extra))))
  14696. (setq extra nil)))
  14697. (when extra
  14698. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14699. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14700. (insert-before-markers (or post ""))
  14701. (setq org-last-inserted-timestamp stamp)))
  14702. (defun org-toggle-time-stamp-overlays ()
  14703. "Toggle the use of custom time stamp formats."
  14704. (interactive)
  14705. (setq org-display-custom-times (not org-display-custom-times))
  14706. (unless org-display-custom-times
  14707. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14708. (while (setq p (next-single-property-change p 'display))
  14709. (if (and (get-text-property p 'display)
  14710. (eq (get-text-property p 'face) 'org-date))
  14711. (remove-text-properties
  14712. p (setq p (next-single-property-change p 'display))
  14713. '(display t))))
  14714. (set-buffer-modified-p bmp)))
  14715. (if (featurep 'xemacs)
  14716. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14717. (org-restart-font-lock)
  14718. (setq org-table-may-need-update t)
  14719. (if org-display-custom-times
  14720. (message "Time stamps are overlaid with custom format")
  14721. (message "Time stamp overlays removed")))
  14722. (defun org-display-custom-time (beg end)
  14723. "Overlay modified time stamp format over timestamp between BEG and END."
  14724. (let* ((ts (buffer-substring beg end))
  14725. t1 w1 with-hm tf time str w2 (off 0))
  14726. (save-match-data
  14727. (setq t1 (org-parse-time-string ts t))
  14728. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14729. (setq off (- (match-end 0) (match-beginning 0)))))
  14730. (setq end (- end off))
  14731. (setq w1 (- end beg)
  14732. with-hm (and (nth 1 t1) (nth 2 t1))
  14733. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14734. time (org-fix-decoded-time t1)
  14735. str (org-add-props
  14736. (format-time-string
  14737. (substring tf 1 -1) (apply 'encode-time time))
  14738. nil 'mouse-face 'highlight)
  14739. w2 (length str))
  14740. (if (not (= w2 w1))
  14741. (add-text-properties (1+ beg) (+ 2 beg)
  14742. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14743. (if (featurep 'xemacs)
  14744. (progn
  14745. (put-text-property beg end 'invisible t)
  14746. (put-text-property beg end 'end-glyph (make-glyph str)))
  14747. (put-text-property beg end 'display str))))
  14748. (defun org-translate-time (string)
  14749. "Translate all timestamps in STRING to custom format.
  14750. But do this only if the variable `org-display-custom-times' is set."
  14751. (when org-display-custom-times
  14752. (save-match-data
  14753. (let* ((start 0)
  14754. (re org-ts-regexp-both)
  14755. t1 with-hm inactive tf time str beg end)
  14756. (while (setq start (string-match re string start))
  14757. (setq beg (match-beginning 0)
  14758. end (match-end 0)
  14759. t1 (save-match-data
  14760. (org-parse-time-string (substring string beg end) t))
  14761. with-hm (and (nth 1 t1) (nth 2 t1))
  14762. inactive (equal (substring string beg (1+ beg)) "[")
  14763. tf (funcall (if with-hm 'cdr 'car)
  14764. org-time-stamp-custom-formats)
  14765. time (org-fix-decoded-time t1)
  14766. str (format-time-string
  14767. (concat
  14768. (if inactive "[" "<") (substring tf 1 -1)
  14769. (if inactive "]" ">"))
  14770. (apply 'encode-time time))
  14771. string (replace-match str t t string)
  14772. start (+ start (length str)))))))
  14773. string)
  14774. (defun org-fix-decoded-time (time)
  14775. "Set 0 instead of nil for the first 6 elements of time.
  14776. Don't touch the rest."
  14777. (let ((n 0))
  14778. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14779. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.3")
  14780. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  14781. "Difference between TIMESTAMP-STRING and now in days.
  14782. If SECONDS is non-nil, return the difference in seconds."
  14783. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  14784. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  14785. (funcall fdiff (current-time)))))
  14786. (defun org-deadline-close (timestamp-string &optional ndays)
  14787. "Is the time in TIMESTAMP-STRING close to the current date?"
  14788. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14789. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  14790. (not (org-entry-is-done-p))))
  14791. (defun org-get-wdays (ts)
  14792. "Get the deadline lead time appropriate for timestring TS."
  14793. (cond
  14794. ((<= org-deadline-warning-days 0)
  14795. ;; 0 or negative, enforce this value no matter what
  14796. (- org-deadline-warning-days))
  14797. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14798. ;; lead time is specified.
  14799. (floor (* (string-to-number (match-string 1 ts))
  14800. (cdr (assoc (match-string 2 ts)
  14801. '(("d" . 1) ("w" . 7)
  14802. ("m" . 30.4) ("y" . 365.25)))))))
  14803. ;; go for the default.
  14804. (t org-deadline-warning-days)))
  14805. (defun org-calendar-select-mouse (ev)
  14806. "Return to `org-read-date' with the date currently selected.
  14807. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14808. (interactive "e")
  14809. (mouse-set-point ev)
  14810. (when (calendar-cursor-to-date)
  14811. (let* ((date (calendar-cursor-to-date))
  14812. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14813. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14814. (if (active-minibuffer-window) (exit-minibuffer))))
  14815. (defun org-check-deadlines (ndays)
  14816. "Check if there are any deadlines due or past due.
  14817. A deadline is considered due if it happens within `org-deadline-warning-days'
  14818. days from today's date. If the deadline appears in an entry marked DONE,
  14819. it is not shown. The prefix arg NDAYS can be used to test that many
  14820. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14821. (interactive "P")
  14822. (let* ((org-warn-days
  14823. (cond
  14824. ((equal ndays '(4)) 100000)
  14825. (ndays (prefix-numeric-value ndays))
  14826. (t (abs org-deadline-warning-days))))
  14827. (case-fold-search nil)
  14828. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14829. (callback
  14830. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14831. (message "%d deadlines past-due or due within %d days"
  14832. (org-occur regexp nil callback)
  14833. org-warn-days)))
  14834. (defsubst org-re-timestamp (type)
  14835. "Return a regexp for timestamp TYPE.
  14836. Allowed values for TYPE are:
  14837. all: all timestamps
  14838. active: only active timestamps (<...>)
  14839. inactive: only inactive timestamps ([...])
  14840. scheduled: only scheduled timestamps
  14841. deadline: only deadline timestamps
  14842. When TYPE is nil, fall back on returning a regexp that matches
  14843. both scheduled and deadline timestamps."
  14844. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  14845. ((eq type 'active) org-ts-regexp)
  14846. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  14847. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  14848. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14849. ((eq type 'scheduled-or-deadline)
  14850. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  14851. (defun org-check-before-date (date)
  14852. "Check if there are deadlines or scheduled entries before DATE."
  14853. (interactive (list (org-read-date)))
  14854. (let ((case-fold-search nil)
  14855. (regexp (org-re-timestamp org-ts-type))
  14856. (callback
  14857. (lambda () (time-less-p
  14858. (org-time-string-to-time (match-string 1))
  14859. (org-time-string-to-time date)))))
  14860. (message "%d entries before %s"
  14861. (org-occur regexp nil callback) date)))
  14862. (defun org-check-after-date (date)
  14863. "Check if there are deadlines or scheduled entries after DATE."
  14864. (interactive (list (org-read-date)))
  14865. (let ((case-fold-search nil)
  14866. (regexp (org-re-timestamp org-ts-type))
  14867. (callback
  14868. (lambda () (not
  14869. (time-less-p
  14870. (org-time-string-to-time (match-string 1))
  14871. (org-time-string-to-time date))))))
  14872. (message "%d entries after %s"
  14873. (org-occur regexp nil callback) date)))
  14874. (defun org-check-dates-range (start-date end-date)
  14875. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14876. (interactive (list (org-read-date nil nil nil "Range starts")
  14877. (org-read-date nil nil nil "Range end")))
  14878. (let ((case-fold-search nil)
  14879. (regexp (org-re-timestamp org-ts-type))
  14880. (callback
  14881. (lambda ()
  14882. (let ((match (match-string 1)))
  14883. (and
  14884. (not (time-less-p
  14885. (org-time-string-to-time match)
  14886. (org-time-string-to-time start-date)))
  14887. (time-less-p
  14888. (org-time-string-to-time match)
  14889. (org-time-string-to-time end-date)))))))
  14890. (message "%d entries between %s and %s"
  14891. (org-occur regexp nil callback) start-date end-date)))
  14892. (defun org-evaluate-time-range (&optional to-buffer)
  14893. "Evaluate a time range by computing the difference between start and end.
  14894. Normally the result is just printed in the echo area, but with prefix arg
  14895. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14896. If the time range is actually in a table, the result is inserted into the
  14897. next column.
  14898. For time difference computation, a year is assumed to be exactly 365
  14899. days in order to avoid rounding problems."
  14900. (interactive "P")
  14901. (or
  14902. (org-clock-update-time-maybe)
  14903. (save-excursion
  14904. (unless (org-at-date-range-p t)
  14905. (goto-char (point-at-bol))
  14906. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14907. (if (not (org-at-date-range-p t))
  14908. (error "Not at a time-stamp range, and none found in current line")))
  14909. (let* ((ts1 (match-string 1))
  14910. (ts2 (match-string 2))
  14911. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14912. (match-end (match-end 0))
  14913. (time1 (org-time-string-to-time ts1))
  14914. (time2 (org-time-string-to-time ts2))
  14915. (t1 (org-float-time time1))
  14916. (t2 (org-float-time time2))
  14917. (diff (abs (- t2 t1)))
  14918. (negative (< (- t2 t1) 0))
  14919. ;; (ys (floor (* 365 24 60 60)))
  14920. (ds (* 24 60 60))
  14921. (hs (* 60 60))
  14922. (fy "%dy %dd %02d:%02d")
  14923. (fy1 "%dy %dd")
  14924. (fd "%dd %02d:%02d")
  14925. (fd1 "%dd")
  14926. (fh "%02d:%02d")
  14927. y d h m align)
  14928. (if havetime
  14929. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14930. y 0
  14931. d (floor (/ diff ds)) diff (mod diff ds)
  14932. h (floor (/ diff hs)) diff (mod diff hs)
  14933. m (floor (/ diff 60)))
  14934. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14935. y 0
  14936. d (floor (+ (/ diff ds) 0.5))
  14937. h 0 m 0))
  14938. (if (not to-buffer)
  14939. (message "%s" (org-make-tdiff-string y d h m))
  14940. (if (org-at-table-p)
  14941. (progn
  14942. (goto-char match-end)
  14943. (setq align t)
  14944. (and (looking-at " *|") (goto-char (match-end 0))))
  14945. (goto-char match-end))
  14946. (if (looking-at
  14947. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14948. (replace-match ""))
  14949. (if negative (insert " -"))
  14950. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14951. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14952. (insert " " (format fh h m))))
  14953. (if align (org-table-align))
  14954. (message "Time difference inserted")))))
  14955. (defun org-make-tdiff-string (y d h m)
  14956. (let ((fmt "")
  14957. (l nil))
  14958. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14959. l (push y l)))
  14960. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14961. l (push d l)))
  14962. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14963. l (push h l)))
  14964. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14965. l (push m l)))
  14966. (apply 'format fmt (nreverse l))))
  14967. (defun org-time-string-to-time (s &optional buffer pos)
  14968. "Convert a timestamp string into internal time."
  14969. (condition-case errdata
  14970. (apply 'encode-time (org-parse-time-string s))
  14971. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14972. s (if (not (and buffer pos))
  14973. ""
  14974. (format " at %d in buffer `%s'" pos buffer))
  14975. (cdr errdata)))))
  14976. (defun org-time-string-to-seconds (s)
  14977. "Convert a timestamp string to a number of seconds."
  14978. (org-float-time (org-time-string-to-time s)))
  14979. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14980. "Convert a time stamp to an absolute day number.
  14981. If there is a specifier for a cyclic time stamp, get the closest date to
  14982. DAYNR.
  14983. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14984. The variable date is bound by the calendar when this is called."
  14985. (cond
  14986. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14987. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14988. daynr
  14989. (+ daynr 1000)))
  14990. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  14991. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14992. (time-to-days (current-time))) (match-string 0 s)
  14993. prefer show-all))
  14994. (t (time-to-days
  14995. (condition-case errdata
  14996. (apply 'encode-time (org-parse-time-string s))
  14997. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14998. s (if (not (and buffer pos))
  14999. ""
  15000. (format " at %d in buffer `%s'" pos buffer))
  15001. (cdr errdata))))))))
  15002. (defun org-days-to-iso-week (days)
  15003. "Return the iso week number."
  15004. (require 'cal-iso)
  15005. (car (calendar-iso-from-absolute days)))
  15006. (defun org-small-year-to-year (year)
  15007. "Convert 2-digit years into 4-digit years.
  15008. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15009. The year 2000 cannot be abbreviated. Any year larger than 99
  15010. is returned unchanged."
  15011. (if (< year 38)
  15012. (setq year (+ 2000 year))
  15013. (if (< year 100)
  15014. (setq year (+ 1900 year))))
  15015. year)
  15016. (defun org-time-from-absolute (d)
  15017. "Return the time corresponding to date D.
  15018. D may be an absolute day number, or a calendar-type list (month day year)."
  15019. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15020. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15021. (defun org-calendar-holiday ()
  15022. "List of holidays, for Diary display in Org-mode."
  15023. (require 'holidays)
  15024. (let ((hl (funcall
  15025. (if (fboundp 'calendar-check-holidays)
  15026. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15027. (if hl (mapconcat 'identity hl "; "))))
  15028. (defun org-diary-sexp-entry (sexp entry date)
  15029. "Process a SEXP diary ENTRY for DATE."
  15030. (require 'diary-lib)
  15031. (let ((result (if calendar-debug-sexp
  15032. (let ((stack-trace-on-error t))
  15033. (eval (car (read-from-string sexp))))
  15034. (condition-case nil
  15035. (eval (car (read-from-string sexp)))
  15036. (error
  15037. (beep)
  15038. (message "Bad sexp at line %d in %s: %s"
  15039. (org-current-line)
  15040. (buffer-file-name) sexp)
  15041. (sleep-for 2))))))
  15042. (cond ((stringp result) (split-string result "; "))
  15043. ((and (consp result)
  15044. (not (consp (cdr result)))
  15045. (stringp (cdr result))) (cdr result))
  15046. ((and (consp result)
  15047. (stringp (car result))) result)
  15048. (result entry))))
  15049. (defun org-diary-to-ical-string (frombuf)
  15050. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15051. This uses the icalendar.el library."
  15052. (let* ((tmpdir (if (featurep 'xemacs)
  15053. (temp-directory)
  15054. temporary-file-directory))
  15055. (tmpfile (make-temp-name
  15056. (expand-file-name "orgics" tmpdir)))
  15057. buf rtn b e)
  15058. (with-current-buffer frombuf
  15059. (icalendar-export-region (point-min) (point-max) tmpfile)
  15060. (setq buf (find-buffer-visiting tmpfile))
  15061. (set-buffer buf)
  15062. (goto-char (point-min))
  15063. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15064. (setq b (match-beginning 0)))
  15065. (goto-char (point-max))
  15066. (if (re-search-backward "^END:VEVENT" nil t)
  15067. (setq e (match-end 0)))
  15068. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15069. (kill-buffer buf)
  15070. (delete-file tmpfile)
  15071. rtn))
  15072. (defun org-closest-date (start current change prefer show-all)
  15073. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15074. When PREFER is `past', return a date that is either CURRENT or past.
  15075. When PREFER is `future', return a date that is either CURRENT or future.
  15076. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15077. ;; Make the proper lists from the dates
  15078. (catch 'exit
  15079. (let ((a1 '(("h" . hour)
  15080. ("d" . day)
  15081. ("w" . week)
  15082. ("m" . month)
  15083. ("y" . year)))
  15084. (shour (nth 2 (org-parse-time-string start)))
  15085. dn dw sday cday n1 n2 n0
  15086. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15087. (setq start (org-date-to-gregorian start)
  15088. current (org-date-to-gregorian
  15089. (if show-all
  15090. current
  15091. (time-to-days (current-time))))
  15092. sday (calendar-absolute-from-gregorian start)
  15093. cday (calendar-absolute-from-gregorian current))
  15094. (if (<= cday sday) (throw 'exit sday))
  15095. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15096. (setq dn (string-to-number (match-string 1 change))
  15097. dw (cdr (assoc (match-string 2 change) a1)))
  15098. (error "Invalid change specifier: %s" change))
  15099. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15100. (cond
  15101. ((eq dw 'hour)
  15102. (let ((missing-hours
  15103. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15104. dn)))
  15105. (setq n1 (if (zerop missing-hours) cday
  15106. (- cday (1+ (floor (/ missing-hours 24)))))
  15107. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15108. ((eq dw 'day)
  15109. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15110. n2 (+ n1 dn)))
  15111. ((eq dw 'year)
  15112. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15113. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15114. (setq date1 (list m d y1)
  15115. n1 (calendar-absolute-from-gregorian date1)
  15116. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15117. n2 (calendar-absolute-from-gregorian date2)))
  15118. ((eq dw 'month)
  15119. ;; approx number of month between the two dates
  15120. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15121. ;; How often does dn fit in there?
  15122. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15123. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15124. m (+ m nm)
  15125. ny (floor (/ m 12))
  15126. y (+ y ny)
  15127. m (- m (* ny 12)))
  15128. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15129. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15130. (setq m2 (+ m dn) y2 y)
  15131. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15132. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15133. (while (<= n2 cday)
  15134. (setq n1 n2 m m2 y y2)
  15135. (setq m2 (+ m dn) y2 y)
  15136. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15137. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15138. ;; Make sure n1 is the earlier date
  15139. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15140. (if show-all
  15141. (cond
  15142. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15143. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15144. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15145. (cond
  15146. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15147. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15148. (t (if (= cday n1) n1 n2)))))))
  15149. (defun org-date-to-gregorian (date)
  15150. "Turn any specification of DATE into a Gregorian date for the calendar."
  15151. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15152. ((and (listp date) (= (length date) 3)) date)
  15153. ((stringp date)
  15154. (setq date (org-parse-time-string date))
  15155. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15156. ((listp date)
  15157. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15158. (defun org-parse-time-string (s &optional nodefault)
  15159. "Parse the standard Org-mode time string.
  15160. This should be a lot faster than the normal `parse-time-string'.
  15161. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15162. hour and minute fields will be nil if not given."
  15163. (cond ((string-match org-ts-regexp0 s)
  15164. (list 0
  15165. (if (or (match-beginning 8) (not nodefault))
  15166. (string-to-number (or (match-string 8 s) "0")))
  15167. (if (or (match-beginning 7) (not nodefault))
  15168. (string-to-number (or (match-string 7 s) "0")))
  15169. (string-to-number (match-string 4 s))
  15170. (string-to-number (match-string 3 s))
  15171. (string-to-number (match-string 2 s))
  15172. nil nil nil))
  15173. ((string-match "^<[^>]+>$" s)
  15174. (decode-time (seconds-to-time (org-matcher-time s))))
  15175. (t (error "Not a standard Org-mode time string: %s" s))))
  15176. (defun org-timestamp-up (&optional arg)
  15177. "Increase the date item at the cursor by one.
  15178. If the cursor is on the year, change the year. If it is on the month,
  15179. the day or the time, change that.
  15180. With prefix ARG, change by that many units."
  15181. (interactive "p")
  15182. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15183. (defun org-timestamp-down (&optional arg)
  15184. "Decrease the date item at the cursor by one.
  15185. If the cursor is on the year, change the year. If it is on the month,
  15186. the day or the time, change that.
  15187. With prefix ARG, change by that many units."
  15188. (interactive "p")
  15189. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15190. (defun org-timestamp-up-day (&optional arg)
  15191. "Increase the date in the time stamp by one day.
  15192. With prefix ARG, change that many days."
  15193. (interactive "p")
  15194. (if (and (not (org-at-timestamp-p t))
  15195. (org-at-heading-p))
  15196. (org-todo 'up)
  15197. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15198. (defun org-timestamp-down-day (&optional arg)
  15199. "Decrease the date in the time stamp by one day.
  15200. With prefix ARG, change that many days."
  15201. (interactive "p")
  15202. (if (and (not (org-at-timestamp-p t))
  15203. (org-at-heading-p))
  15204. (org-todo 'down)
  15205. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15206. (defun org-at-timestamp-p (&optional inactive-ok)
  15207. "Determine if the cursor is in or at a timestamp."
  15208. (interactive)
  15209. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15210. (pos (point))
  15211. (ans (or (looking-at tsr)
  15212. (save-excursion
  15213. (skip-chars-backward "^[<\n\r\t")
  15214. (if (> (point) (point-min)) (backward-char 1))
  15215. (and (looking-at tsr)
  15216. (> (- (match-end 0) pos) -1))))))
  15217. (and ans
  15218. (boundp 'org-ts-what)
  15219. (setq org-ts-what
  15220. (cond
  15221. ((= pos (match-beginning 0)) 'bracket)
  15222. ;; Point is considered to be "on the bracket" whether
  15223. ;; it's really on it or right after it.
  15224. ((= pos (1- (match-end 0))) 'bracket)
  15225. ((= pos (match-end 0)) 'after)
  15226. ((org-pos-in-match-range pos 2) 'year)
  15227. ((org-pos-in-match-range pos 3) 'month)
  15228. ((org-pos-in-match-range pos 7) 'hour)
  15229. ((org-pos-in-match-range pos 8) 'minute)
  15230. ((or (org-pos-in-match-range pos 4)
  15231. (org-pos-in-match-range pos 5)) 'day)
  15232. ((and (> pos (or (match-end 8) (match-end 5)))
  15233. (< pos (match-end 0)))
  15234. (- pos (or (match-end 8) (match-end 5))))
  15235. (t 'day))))
  15236. ans))
  15237. (defun org-toggle-timestamp-type ()
  15238. "Toggle the type (<active> or [inactive]) of a time stamp."
  15239. (interactive)
  15240. (when (org-at-timestamp-p t)
  15241. (let ((beg (match-beginning 0)) (end (match-end 0))
  15242. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15243. (save-excursion
  15244. (goto-char beg)
  15245. (while (re-search-forward "[][<>]" end t)
  15246. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15247. t t)))
  15248. (message "Timestamp is now %sactive"
  15249. (if (equal (char-after beg) ?<) "" "in")))))
  15250. (defvar org-clock-history) ; defined in org-clock.el
  15251. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15252. (defun org-timestamp-change (n &optional what updown)
  15253. "Change the date in the time stamp at point.
  15254. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15255. `year', `minute', `second'. If WHAT is not given, the cursor position
  15256. in the timestamp determines what will be changed."
  15257. (let ((origin (point)) origin-cat
  15258. with-hm inactive
  15259. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15260. org-ts-what
  15261. extra rem
  15262. ts time time0 fixnext clrgx)
  15263. (if (not (org-at-timestamp-p t))
  15264. (error "Not at a timestamp"))
  15265. (if (and (not what) (eq org-ts-what 'bracket))
  15266. (org-toggle-timestamp-type)
  15267. ;; Point isn't on brackets. Remember the part of the time-stamp
  15268. ;; the point was in. Indeed, size of time-stamps may change,
  15269. ;; but point must be kept in the same category nonetheless.
  15270. (setq origin-cat org-ts-what)
  15271. (if (and (not what) (not (eq org-ts-what 'day))
  15272. org-display-custom-times
  15273. (get-text-property (point) 'display)
  15274. (not (get-text-property (1- (point)) 'display)))
  15275. (setq org-ts-what 'day))
  15276. (setq org-ts-what (or what org-ts-what)
  15277. inactive (= (char-after (match-beginning 0)) ?\[)
  15278. ts (match-string 0))
  15279. (replace-match "")
  15280. (if (string-match
  15281. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15282. ts)
  15283. (setq extra (match-string 1 ts)))
  15284. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15285. (setq with-hm t))
  15286. (setq time0 (org-parse-time-string ts))
  15287. (when (and updown
  15288. (eq org-ts-what 'minute)
  15289. (not current-prefix-arg))
  15290. ;; This looks like s-up and s-down. Change by one rounding step.
  15291. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15292. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15293. (setcar (cdr time0) (+ (nth 1 time0)
  15294. (if (> n 0) (- rem) (- dm rem))))))
  15295. (setq time
  15296. (encode-time (or (car time0) 0)
  15297. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15298. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15299. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15300. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15301. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15302. (nthcdr 6 time0)))
  15303. (when (and (member org-ts-what '(hour minute))
  15304. extra
  15305. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15306. (setq extra (org-modify-ts-extra
  15307. extra
  15308. (if (eq org-ts-what 'hour) 2 5)
  15309. n dm)))
  15310. (when (integerp org-ts-what)
  15311. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15312. (if (eq what 'calendar)
  15313. (let ((cal-date (org-get-date-from-calendar)))
  15314. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15315. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15316. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15317. (setcar time0 (or (car time0) 0))
  15318. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15319. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15320. (setq time (apply 'encode-time time0))))
  15321. ;; Insert the new time-stamp, and ensure point stays in the same
  15322. ;; category as before (i.e. not after the last position in that
  15323. ;; category).
  15324. (let ((pos (point)))
  15325. ;; Stay before inserted string. `save-excursion' is of no use.
  15326. (setq org-last-changed-timestamp
  15327. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15328. (goto-char pos))
  15329. (save-match-data
  15330. (looking-at org-ts-regexp3)
  15331. (goto-char (cond
  15332. ;; `day' category ends before `hour' if any, or at
  15333. ;; the end of the day name.
  15334. ((eq origin-cat 'day)
  15335. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15336. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15337. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15338. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15339. ;; `year' and `month' have both fixed size: point
  15340. ;; couldn't have moved into another part.
  15341. (t origin))))
  15342. ;; Update clock if on a CLOCK line.
  15343. (org-clock-update-time-maybe)
  15344. ;; Maybe adjust the closest clock in `org-clock-history'
  15345. (when org-clock-adjust-closest
  15346. (if (not (and (org-at-clock-log-p)
  15347. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15348. org-clock-history))))))
  15349. (message "No clock to adjust")
  15350. (cond ((save-excursion ; fix previous clock?
  15351. (re-search-backward org-ts-regexp0 nil t)
  15352. (org-looking-back (concat org-clock-string " \\[")))
  15353. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15354. ((save-excursion ; fix next clock?
  15355. (re-search-backward org-ts-regexp0 nil t)
  15356. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15357. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15358. (save-window-excursion
  15359. ;; Find closest clock to point, adjust the previous/next one in history
  15360. (let* ((p (save-excursion (org-back-to-heading t)))
  15361. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15362. (clfixnth
  15363. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15364. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15365. (if (not clfixpos)
  15366. (message "No clock to adjust")
  15367. (save-excursion
  15368. (org-goto-marker-or-bmk clfixpos)
  15369. (org-show-subtree)
  15370. (when (re-search-forward clrgx nil t)
  15371. (goto-char (match-beginning 1))
  15372. (let (org-clock-adjust-closest)
  15373. (org-timestamp-change n org-ts-what updown))
  15374. (message "Clock adjusted in %s for heading: %s"
  15375. (file-name-nondirectory (buffer-file-name))
  15376. (org-get-heading t t)))))))))
  15377. ;; Try to recenter the calendar window, if any.
  15378. (if (and org-calendar-follow-timestamp-change
  15379. (get-buffer-window "*Calendar*" t)
  15380. (memq org-ts-what '(day month year)))
  15381. (org-recenter-calendar (time-to-days time))))))
  15382. (defun org-modify-ts-extra (s pos n dm)
  15383. "Change the different parts of the lead-time and repeat fields in timestamp."
  15384. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15385. ng h m new rem)
  15386. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15387. (cond
  15388. ((or (org-pos-in-match-range pos 2)
  15389. (org-pos-in-match-range pos 3))
  15390. (setq m (string-to-number (match-string 3 s))
  15391. h (string-to-number (match-string 2 s)))
  15392. (if (org-pos-in-match-range pos 2)
  15393. (setq h (+ h n))
  15394. (setq n (* dm (org-no-warnings (signum n))))
  15395. (when (not (= 0 (setq rem (% m dm))))
  15396. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15397. (setq m (+ m n)))
  15398. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15399. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15400. (setq h (min 24 (max 0 h)))
  15401. (setq ng 1 new (format "-%02d:%02d" h m)))
  15402. ((org-pos-in-match-range pos 6)
  15403. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15404. ((org-pos-in-match-range pos 5)
  15405. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15406. ((org-pos-in-match-range pos 9)
  15407. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15408. ((org-pos-in-match-range pos 8)
  15409. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15410. (when ng
  15411. (setq s (concat
  15412. (substring s 0 (match-beginning ng))
  15413. new
  15414. (substring s (match-end ng))))))
  15415. s))
  15416. (defun org-recenter-calendar (date)
  15417. "If the calendar is visible, recenter it to DATE."
  15418. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15419. (when cwin
  15420. (let ((calendar-move-hook nil))
  15421. (with-selected-window cwin
  15422. (calendar-goto-date (if (listp date) date
  15423. (calendar-gregorian-from-absolute date))))))))
  15424. (defun org-goto-calendar (&optional arg)
  15425. "Go to the Emacs calendar at the current date.
  15426. If there is a time stamp in the current line, go to that date.
  15427. A prefix ARG can be used to force the current date."
  15428. (interactive "P")
  15429. (let ((tsr org-ts-regexp) diff
  15430. (calendar-move-hook nil)
  15431. (calendar-view-holidays-initially-flag nil)
  15432. (calendar-view-diary-initially-flag nil))
  15433. (if (or (org-at-timestamp-p)
  15434. (save-excursion
  15435. (beginning-of-line 1)
  15436. (looking-at (concat ".*" tsr))))
  15437. (let ((d1 (time-to-days (current-time)))
  15438. (d2 (time-to-days
  15439. (org-time-string-to-time (match-string 1)))))
  15440. (setq diff (- d2 d1))))
  15441. (calendar)
  15442. (calendar-goto-today)
  15443. (if (and diff (not arg)) (calendar-forward-day diff))))
  15444. (defun org-get-date-from-calendar ()
  15445. "Return a list (month day year) of date at point in calendar."
  15446. (with-current-buffer "*Calendar*"
  15447. (save-match-data
  15448. (calendar-cursor-to-date))))
  15449. (defun org-date-from-calendar ()
  15450. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15451. If there is already a time stamp at the cursor position, update it."
  15452. (interactive)
  15453. (if (org-at-timestamp-p t)
  15454. (org-timestamp-change 0 'calendar)
  15455. (let ((cal-date (org-get-date-from-calendar)))
  15456. (org-insert-time-stamp
  15457. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15458. (defun org-minutes-to-clocksum-string (m)
  15459. "Format number of minutes as a clocksum string.
  15460. The format is determined by `org-time-clocksum-format',
  15461. `org-time-clocksum-use-fractional' and
  15462. `org-time-clocksum-fractional-format'."
  15463. (let ((clocksum "") fmt n)
  15464. ;; fractional format
  15465. (if org-time-clocksum-use-fractional
  15466. (cond
  15467. ;; single format string
  15468. ((stringp org-time-clocksum-fractional-format)
  15469. (format org-time-clocksum-fractional-format (/ m 60.0)))
  15470. ;; choice of fractional formats for different time units
  15471. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15472. (> (/ (truncate m) (* 365 24 60)) 0))
  15473. (format fmt (/ m (* 365 24 60.0))))
  15474. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15475. (> (/ (truncate m) (* 30 24 60)) 0))
  15476. (format fmt (/ m (* 30 24 60.0))))
  15477. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15478. (> (/ (truncate m) (* 7 24 60)) 0))
  15479. (format fmt (/ m (* 7 24 60.0))))
  15480. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15481. (> (/ (truncate m) (* 24 60)) 0))
  15482. (format fmt (/ m (* 24 60.0))))
  15483. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15484. (> (/ (truncate m) 60) 0))
  15485. (format fmt (/ m 60.0)))
  15486. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  15487. (format fmt m))
  15488. ;; fall back to smallest time unit with a format
  15489. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15490. (format fmt (/ m 60.0)))
  15491. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15492. (format fmt (/ m (* 24 60.0))))
  15493. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15494. (format fmt (/ m (* 7 24 60.0))))
  15495. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15496. (format fmt (/ m (* 30 24 60.0))))
  15497. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15498. (format fmt (/ m (* 365 24 60.0)))))
  15499. ;; standard (non-fractional) format, with single format string
  15500. (if (stringp org-time-clocksum-format)
  15501. (format org-time-clocksum-format (setq n (/ m 60)) (- m (* 60 n)))
  15502. ;; separate formats components
  15503. (and (setq fmt (plist-get org-time-clocksum-format :years))
  15504. (or (> (setq n (/ (truncate m) (* 365 24 60))) 0)
  15505. (plist-get org-time-clocksum-format :require-years))
  15506. (setq clocksum (concat clocksum (format fmt n))
  15507. m (- m (* n 365 24 60))))
  15508. (and (setq fmt (plist-get org-time-clocksum-format :months))
  15509. (or (> (setq n (/ (truncate m) (* 30 24 60))) 0)
  15510. (plist-get org-time-clocksum-format :require-months))
  15511. (setq clocksum (concat clocksum (format fmt n))
  15512. m (- m (* n 30 24 60))))
  15513. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  15514. (or (> (setq n (/ (truncate m) (* 7 24 60))) 0)
  15515. (plist-get org-time-clocksum-format :require-weeks))
  15516. (setq clocksum (concat clocksum (format fmt n))
  15517. m (- m (* n 7 24 60))))
  15518. (and (setq fmt (plist-get org-time-clocksum-format :days))
  15519. (or (> (setq n (/ (truncate m) (* 24 60))) 0)
  15520. (plist-get org-time-clocksum-format :require-days))
  15521. (setq clocksum (concat clocksum (format fmt n))
  15522. m (- m (* n 24 60))))
  15523. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  15524. (or (> (setq n (/ (truncate m) 60)) 0)
  15525. (plist-get org-time-clocksum-format :require-hours))
  15526. (setq clocksum (concat clocksum (format fmt n))
  15527. m (- m (* n 60))))
  15528. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  15529. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  15530. (setq clocksum (concat clocksum (format fmt m))))
  15531. ;; return formatted time duration
  15532. clocksum))))
  15533. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  15534. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  15535. "Org mode version 8.0")
  15536. (defun org-hours-to-clocksum-string (n)
  15537. (org-minutes-to-clocksum-string (* n 60)))
  15538. (defun org-hh:mm-string-to-minutes (s)
  15539. "Convert a string H:MM to a number of minutes.
  15540. If the string is just a number, interpret it as minutes.
  15541. In fact, the first hh:mm or number in the string will be taken,
  15542. there can be extra stuff in the string.
  15543. If no number is found, the return value is 0."
  15544. (cond
  15545. ((integerp s) s)
  15546. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15547. (+ (* (string-to-number (match-string 1 s)) 60)
  15548. (string-to-number (match-string 2 s))))
  15549. ((string-match "\\([0-9]+\\)" s)
  15550. (string-to-number (match-string 1 s)))
  15551. (t 0)))
  15552. (defcustom org-effort-durations
  15553. `(("h" . 60)
  15554. ("d" . ,(* 60 8))
  15555. ("w" . ,(* 60 8 5))
  15556. ("m" . ,(* 60 8 5 4))
  15557. ("y" . ,(* 60 8 5 40)))
  15558. "Conversion factor to minutes for an effort modifier.
  15559. Each entry has the form (MODIFIER . MINUTES).
  15560. In an effort string, a number followed by MODIFIER is multiplied
  15561. by the specified number of MINUTES to obtain an effort in
  15562. minutes.
  15563. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15564. effort string \"2hours\" is equivalent to 120 minutes."
  15565. :group 'org-agenda
  15566. :version "24.1"
  15567. :type '(alist :key-type (string :tag "Modifier")
  15568. :value-type (number :tag "Minutes")))
  15569. (defcustom org-image-actual-width t
  15570. "Should we use the actual width of images when inlining them?
  15571. When set to `t', always use the image width.
  15572. When set to a number, use imagemagick (when available) to set
  15573. the image's width to this value.
  15574. When set to a number in a list, try to get the width from the
  15575. #+ATTR.* keyword if it matches a width specification like
  15576. width=\"[0-9]+\", and fall back on that number if none is found.
  15577. When set to nil, try to get the width from an #+ATTR.* keyword
  15578. and fall back on the original width if none is found.
  15579. This requires Emacs >= 24.1, build with imagemagick support."
  15580. :group 'org-appearance
  15581. :version "24.3"
  15582. :type '(choice
  15583. (const :tag "Use the image width" t)
  15584. (integer :tag "Use a number of pixels")
  15585. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15586. (const :tag "Use #+ATTR* or don't resize" nil)))
  15587. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15588. "Convert a duration string S to minutes.
  15589. A bare number is interpreted as minutes, modifiers can be set by
  15590. customizing `org-effort-durations' (which see).
  15591. Entries containing a colon are interpreted as H:MM by
  15592. `org-hh:mm-string-to-minutes'."
  15593. (let ((result 0)
  15594. (re (concat "\\([0-9.]+\\) *\\("
  15595. (regexp-opt (mapcar 'car org-effort-durations))
  15596. "\\)")))
  15597. (while (string-match re s)
  15598. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15599. (string-to-number (match-string 1 s))))
  15600. (setq s (replace-match "" nil t s)))
  15601. (setq result (floor result))
  15602. (incf result (org-hh:mm-string-to-minutes s))
  15603. (if output-to-string (number-to-string result) result)))
  15604. ;;;; Files
  15605. (defun org-save-all-org-buffers ()
  15606. "Save all Org-mode buffers without user confirmation."
  15607. (interactive)
  15608. (message "Saving all Org-mode buffers...")
  15609. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15610. (when (featurep 'org-id) (org-id-locations-save))
  15611. (message "Saving all Org-mode buffers... done"))
  15612. (defun org-revert-all-org-buffers ()
  15613. "Revert all Org-mode buffers.
  15614. Prompt for confirmation when there are unsaved changes.
  15615. Be sure you know what you are doing before letting this function
  15616. overwrite your changes.
  15617. This function is useful in a setup where one tracks org files
  15618. with a version control system, to revert on one machine after pulling
  15619. changes from another. I believe the procedure must be like this:
  15620. 1. M-x org-save-all-org-buffers
  15621. 2. Pull changes from the other machine, resolve conflicts
  15622. 3. M-x org-revert-all-org-buffers"
  15623. (interactive)
  15624. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15625. (error "Abort"))
  15626. (save-excursion
  15627. (save-window-excursion
  15628. (mapc
  15629. (lambda (b)
  15630. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15631. (with-current-buffer b buffer-file-name))
  15632. (org-pop-to-buffer-same-window b)
  15633. (revert-buffer t 'no-confirm)))
  15634. (buffer-list))
  15635. (when (and (featurep 'org-id) org-id-track-globally)
  15636. (org-id-locations-load)))))
  15637. ;;;; Agenda files
  15638. ;;;###autoload
  15639. (defun org-switchb (&optional arg)
  15640. "Switch between Org buffers.
  15641. With one prefix argument, restrict available buffers to files.
  15642. With two prefix arguments, restrict available buffers to agenda files.
  15643. Defaults to `iswitchb' for buffer name completion.
  15644. Set `org-completion-use-ido' to make it use ido instead."
  15645. (interactive "P")
  15646. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15647. ((equal arg '(16)) (org-buffer-list 'agenda))
  15648. (t (org-buffer-list))))
  15649. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15650. (org-completion-use-ido org-completion-use-ido))
  15651. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15652. (setq org-completion-use-iswitchb t))
  15653. (org-pop-to-buffer-same-window
  15654. (org-icompleting-read "Org buffer: "
  15655. (mapcar 'list (mapcar 'buffer-name blist))
  15656. nil t))))
  15657. ;;; Define some older names previously used for this functionality
  15658. ;;;###autoload
  15659. (defalias 'org-ido-switchb 'org-switchb)
  15660. ;;;###autoload
  15661. (defalias 'org-iswitchb 'org-switchb)
  15662. (defun org-buffer-list (&optional predicate exclude-tmp)
  15663. "Return a list of Org buffers.
  15664. PREDICATE can be `export', `files' or `agenda'.
  15665. export restrict the list to Export buffers.
  15666. files restrict the list to buffers visiting Org files.
  15667. agenda restrict the list to buffers visiting agenda files.
  15668. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15669. (let* ((bfn nil)
  15670. (agenda-files (and (eq predicate 'agenda)
  15671. (mapcar 'file-truename (org-agenda-files t))))
  15672. (filter
  15673. (cond
  15674. ((eq predicate 'files)
  15675. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15676. ((eq predicate 'export)
  15677. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15678. ((eq predicate 'agenda)
  15679. (lambda (b)
  15680. (with-current-buffer b
  15681. (and (derived-mode-p 'org-mode)
  15682. (setq bfn (buffer-file-name b))
  15683. (member (file-truename bfn) agenda-files)))))
  15684. (t (lambda (b) (with-current-buffer b
  15685. (or (derived-mode-p 'org-mode)
  15686. (string-match "\*Org .*Export"
  15687. (buffer-name b)))))))))
  15688. (delq nil
  15689. (mapcar
  15690. (lambda(b)
  15691. (if (and (funcall filter b)
  15692. (or (not exclude-tmp)
  15693. (not (string-match "tmp" (buffer-name b)))))
  15694. b
  15695. nil))
  15696. (buffer-list)))))
  15697. (defun org-agenda-files (&optional unrestricted archives)
  15698. "Get the list of agenda files.
  15699. Optional UNRESTRICTED means return the full list even if a restriction
  15700. is currently in place.
  15701. When ARCHIVES is t, include all archive files that are really being
  15702. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15703. `org-agenda-archives-mode' is t."
  15704. (let ((files
  15705. (cond
  15706. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15707. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15708. ((listp org-agenda-files) org-agenda-files)
  15709. (t (error "Invalid value of `org-agenda-files'")))))
  15710. (setq files (apply 'append
  15711. (mapcar (lambda (f)
  15712. (if (file-directory-p f)
  15713. (directory-files
  15714. f t org-agenda-file-regexp)
  15715. (list f)))
  15716. files)))
  15717. (when org-agenda-skip-unavailable-files
  15718. (setq files (delq nil
  15719. (mapcar (function
  15720. (lambda (file)
  15721. (and (file-readable-p file) file)))
  15722. files))))
  15723. (when (or (eq archives t)
  15724. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15725. (setq files (org-add-archive-files files)))
  15726. files))
  15727. (defun org-agenda-file-p (&optional file)
  15728. "Return non-nil, if FILE is an agenda file.
  15729. If FILE is omitted, use the file associated with the current
  15730. buffer."
  15731. (member (or file (buffer-file-name))
  15732. (org-agenda-files t)))
  15733. (defun org-edit-agenda-file-list ()
  15734. "Edit the list of agenda files.
  15735. Depending on setup, this either uses customize to edit the variable
  15736. `org-agenda-files', or it visits the file that is holding the list. In the
  15737. latter case, the buffer is set up in a way that saving it automatically kills
  15738. the buffer and restores the previous window configuration."
  15739. (interactive)
  15740. (if (stringp org-agenda-files)
  15741. (let ((cw (current-window-configuration)))
  15742. (find-file org-agenda-files)
  15743. (org-set-local 'org-window-configuration cw)
  15744. (org-add-hook 'after-save-hook
  15745. (lambda ()
  15746. (set-window-configuration
  15747. (prog1 org-window-configuration
  15748. (kill-buffer (current-buffer))))
  15749. (org-install-agenda-files-menu)
  15750. (message "New agenda file list installed"))
  15751. nil 'local)
  15752. (message "%s" (substitute-command-keys
  15753. "Edit list and finish with \\[save-buffer]")))
  15754. (customize-variable 'org-agenda-files)))
  15755. (defun org-store-new-agenda-file-list (list)
  15756. "Set new value for the agenda file list and save it correctly."
  15757. (if (stringp org-agenda-files)
  15758. (let ((fe (org-read-agenda-file-list t)) b u)
  15759. (while (setq b (find-buffer-visiting org-agenda-files))
  15760. (kill-buffer b))
  15761. (with-temp-file org-agenda-files
  15762. (insert
  15763. (mapconcat
  15764. (lambda (f) ;; Keep un-expanded entries.
  15765. (if (setq u (assoc f fe))
  15766. (cdr u)
  15767. f))
  15768. list "\n")
  15769. "\n")))
  15770. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15771. (org-insert-mode-line-in-empty-file nil))
  15772. (setq org-agenda-files list)
  15773. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15774. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15775. "Read the list of agenda files from a file.
  15776. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15777. filenames, used by `org-store-new-agenda-file-list' to write back
  15778. un-expanded file names."
  15779. (when (file-directory-p org-agenda-files)
  15780. (error "`org-agenda-files' cannot be a single directory"))
  15781. (when (stringp org-agenda-files)
  15782. (with-temp-buffer
  15783. (insert-file-contents org-agenda-files)
  15784. (mapcar
  15785. (lambda (f)
  15786. (let ((e (expand-file-name (substitute-in-file-name f)
  15787. org-directory)))
  15788. (if pair-with-expansion
  15789. (cons e f)
  15790. e)))
  15791. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15792. ;;;###autoload
  15793. (defun org-cycle-agenda-files ()
  15794. "Cycle through the files in `org-agenda-files'.
  15795. If the current buffer visits an agenda file, find the next one in the list.
  15796. If the current buffer does not, find the first agenda file."
  15797. (interactive)
  15798. (let* ((fs (org-agenda-files t))
  15799. (files (append fs (list (car fs))))
  15800. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15801. file)
  15802. (unless files (error "No agenda files"))
  15803. (catch 'exit
  15804. (while (setq file (pop files))
  15805. (if (equal (file-truename file) tcf)
  15806. (when (car files)
  15807. (find-file (car files))
  15808. (throw 'exit t))))
  15809. (find-file (car fs)))
  15810. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15811. (defun org-agenda-file-to-front (&optional to-end)
  15812. "Move/add the current file to the top of the agenda file list.
  15813. If the file is not present in the list, it is added to the front. If it is
  15814. present, it is moved there. With optional argument TO-END, add/move to the
  15815. end of the list."
  15816. (interactive "P")
  15817. (let ((org-agenda-skip-unavailable-files nil)
  15818. (file-alist (mapcar (lambda (x)
  15819. (cons (file-truename x) x))
  15820. (org-agenda-files t)))
  15821. (ctf (file-truename
  15822. (or buffer-file-name
  15823. (error "Please save the current buffer to a file"))))
  15824. x had)
  15825. (setq x (assoc ctf file-alist) had x)
  15826. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15827. (if to-end
  15828. (setq file-alist (append (delq x file-alist) (list x)))
  15829. (setq file-alist (cons x (delq x file-alist))))
  15830. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15831. (org-install-agenda-files-menu)
  15832. (message "File %s to %s of agenda file list"
  15833. (if had "moved" "added") (if to-end "end" "front"))))
  15834. (defun org-remove-file (&optional file)
  15835. "Remove current file from the list of files in variable `org-agenda-files'.
  15836. These are the files which are being checked for agenda entries.
  15837. Optional argument FILE means use this file instead of the current."
  15838. (interactive)
  15839. (let* ((org-agenda-skip-unavailable-files nil)
  15840. (file (or file buffer-file-name
  15841. (error "Current buffer does not visit a file")))
  15842. (true-file (file-truename file))
  15843. (afile (abbreviate-file-name file))
  15844. (files (delq nil (mapcar
  15845. (lambda (x)
  15846. (if (equal true-file
  15847. (file-truename x))
  15848. nil x))
  15849. (org-agenda-files t)))))
  15850. (if (not (= (length files) (length (org-agenda-files t))))
  15851. (progn
  15852. (org-store-new-agenda-file-list files)
  15853. (org-install-agenda-files-menu)
  15854. (message "Removed file: %s" afile))
  15855. (message "File was not in list: %s (not removed)" afile))))
  15856. (defun org-file-menu-entry (file)
  15857. (vector file (list 'find-file file) t))
  15858. (defun org-check-agenda-file (file)
  15859. "Make sure FILE exists. If not, ask user what to do."
  15860. (when (not (file-exists-p file))
  15861. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  15862. (abbreviate-file-name file))
  15863. (let ((r (downcase (read-char-exclusive))))
  15864. (cond
  15865. ((equal r ?r)
  15866. (org-remove-file file)
  15867. (throw 'nextfile t))
  15868. (t (error "Abort"))))))
  15869. (defun org-get-agenda-file-buffer (file)
  15870. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15871. it to the list of buffers which might be released later."
  15872. (let ((buf (org-find-base-buffer-visiting file)))
  15873. (if buf
  15874. buf ; just return it
  15875. ;; Make a new buffer and remember it
  15876. (setq buf (find-file-noselect file))
  15877. (if buf (push buf org-agenda-new-buffers))
  15878. buf)))
  15879. (defun org-release-buffers (blist)
  15880. "Release all buffers in list, asking the user for confirmation when needed.
  15881. When a buffer is unmodified, it is just killed. When modified, it is saved
  15882. \(if the user agrees) and then killed."
  15883. (let (buf file)
  15884. (while (setq buf (pop blist))
  15885. (setq file (buffer-file-name buf))
  15886. (when (and (buffer-modified-p buf)
  15887. file
  15888. (y-or-n-p (format "Save file %s? " file)))
  15889. (with-current-buffer buf (save-buffer)))
  15890. (kill-buffer buf))))
  15891. (defun org-agenda-prepare-buffers (files)
  15892. "Create buffers for all agenda files, protect archived trees and comments."
  15893. (interactive)
  15894. (let ((pa '(:org-archived t))
  15895. (pc '(:org-comment t))
  15896. (pall '(:org-archived t :org-comment t))
  15897. (inhibit-read-only t)
  15898. (rea (concat ":" org-archive-tag ":"))
  15899. bmp file re)
  15900. (save-excursion
  15901. (save-restriction
  15902. (while (setq file (pop files))
  15903. (catch 'nextfile
  15904. (if (bufferp file)
  15905. (set-buffer file)
  15906. (org-check-agenda-file file)
  15907. (set-buffer (org-get-agenda-file-buffer file)))
  15908. (widen)
  15909. (setq bmp (buffer-modified-p))
  15910. (org-refresh-category-properties)
  15911. (setq org-todo-keywords-for-agenda
  15912. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15913. (setq org-done-keywords-for-agenda
  15914. (append org-done-keywords-for-agenda org-done-keywords))
  15915. (setq org-todo-keyword-alist-for-agenda
  15916. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15917. (setq org-drawers-for-agenda
  15918. (append org-drawers-for-agenda org-drawers))
  15919. (setq org-tag-alist-for-agenda
  15920. (append org-tag-alist-for-agenda org-tag-alist))
  15921. (save-excursion
  15922. (remove-text-properties (point-min) (point-max) pall)
  15923. (when org-agenda-skip-archived-trees
  15924. (goto-char (point-min))
  15925. (while (re-search-forward rea nil t)
  15926. (if (org-at-heading-p t)
  15927. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15928. (goto-char (point-min))
  15929. (setq re (format org-heading-keyword-regexp-format
  15930. org-comment-string))
  15931. (while (re-search-forward re nil t)
  15932. (add-text-properties
  15933. (match-beginning 0) (org-end-of-subtree t) pc)))
  15934. (set-buffer-modified-p bmp)))))
  15935. (setq org-todo-keywords-for-agenda
  15936. (org-uniquify org-todo-keywords-for-agenda))
  15937. (setq org-todo-keyword-alist-for-agenda
  15938. (org-uniquify org-todo-keyword-alist-for-agenda)
  15939. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15940. ;;;; Embedded LaTeX
  15941. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15942. "Keymap for the minor `org-cdlatex-mode'.")
  15943. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15944. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15945. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15946. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15947. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15948. (defvar org-cdlatex-texmathp-advice-is-done nil
  15949. "Flag remembering if we have applied the advice to texmathp already.")
  15950. (define-minor-mode org-cdlatex-mode
  15951. "Toggle the minor `org-cdlatex-mode'.
  15952. This mode supports entering LaTeX environment and math in LaTeX fragments
  15953. in Org-mode.
  15954. \\{org-cdlatex-mode-map}"
  15955. nil " OCDL" nil
  15956. (when org-cdlatex-mode
  15957. (require 'cdlatex)
  15958. (run-hooks 'cdlatex-mode-hook)
  15959. (cdlatex-compute-tables))
  15960. (unless org-cdlatex-texmathp-advice-is-done
  15961. (setq org-cdlatex-texmathp-advice-is-done t)
  15962. (defadvice texmathp (around org-math-always-on activate)
  15963. "Always return t in org-mode buffers.
  15964. This is because we want to insert math symbols without dollars even outside
  15965. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15966. an embedded LaTeX fragment, let texmathp do its job.
  15967. \\[org-cdlatex-mode-map]"
  15968. (interactive)
  15969. (let (p)
  15970. (cond
  15971. ((not (derived-mode-p 'org-mode)) ad-do-it)
  15972. ((eq this-command 'cdlatex-math-symbol)
  15973. (setq ad-return-value t
  15974. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15975. (t
  15976. (let ((p (org-inside-LaTeX-fragment-p)))
  15977. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15978. (setq ad-return-value t
  15979. texmathp-why '("Org-mode embedded math" . 0))
  15980. (if p ad-do-it)))))))))
  15981. (defun turn-on-org-cdlatex ()
  15982. "Unconditionally turn on `org-cdlatex-mode'."
  15983. (org-cdlatex-mode 1))
  15984. (defun org-inside-LaTeX-fragment-p ()
  15985. "Test if point is inside a LaTeX fragment.
  15986. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15987. sequence appearing also before point.
  15988. Even though the matchers for math are configurable, this function assumes
  15989. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15990. delimiters are skipped when they have been removed by customization.
  15991. The return value is nil, or a cons cell with the delimiter and the
  15992. position of this delimiter.
  15993. This function does a reasonably good job, but can locally be fooled by
  15994. for example currency specifications. For example it will assume being in
  15995. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15996. fragments that are properly closed, but during editing, we have to live
  15997. with the uncertainty caused by missing closing delimiters. This function
  15998. looks only before point, not after."
  15999. (catch 'exit
  16000. (let ((pos (point))
  16001. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16002. (lim (progn
  16003. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16004. (point)))
  16005. dd-on str (start 0) m re)
  16006. (goto-char pos)
  16007. (when dodollar
  16008. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16009. re (nth 1 (assoc "$" org-latex-regexps)))
  16010. (while (string-match re str start)
  16011. (cond
  16012. ((= (match-end 0) (length str))
  16013. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16014. ((= (match-end 0) (- (length str) 5))
  16015. (throw 'exit nil))
  16016. (t (setq start (match-end 0))))))
  16017. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16018. (goto-char pos)
  16019. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16020. (and (match-beginning 2) (throw 'exit nil))
  16021. ;; count $$
  16022. (while (re-search-backward "\\$\\$" lim t)
  16023. (setq dd-on (not dd-on)))
  16024. (goto-char pos)
  16025. (if dd-on (cons "$$" m))))))
  16026. (defun org-inside-latex-macro-p ()
  16027. "Is point inside a LaTeX macro or its arguments?"
  16028. (save-match-data
  16029. (org-in-regexp
  16030. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16031. (defun org-try-cdlatex-tab ()
  16032. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16033. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16034. - inside a LaTeX fragment, or
  16035. - after the first word in a line, where an abbreviation expansion could
  16036. insert a LaTeX environment."
  16037. (when org-cdlatex-mode
  16038. (cond
  16039. ;; Before any word on the line: No expansion possible.
  16040. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16041. ;; Just after first word on the line: Expand it. Make sure it
  16042. ;; cannot happen on headlines, though.
  16043. ((save-excursion
  16044. (skip-chars-backward "a-zA-Z0-9*")
  16045. (skip-chars-backward " \t")
  16046. (and (bolp) (not (org-at-heading-p))))
  16047. (cdlatex-tab) t)
  16048. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16049. (defun org-cdlatex-underscore-caret (&optional arg)
  16050. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16051. Revert to the normal definition outside of these fragments."
  16052. (interactive "P")
  16053. (if (org-inside-LaTeX-fragment-p)
  16054. (call-interactively 'cdlatex-sub-superscript)
  16055. (let (org-cdlatex-mode)
  16056. (call-interactively (key-binding (vector last-input-event))))))
  16057. (defun org-cdlatex-math-modify (&optional arg)
  16058. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16059. Revert to the normal definition outside of these fragments."
  16060. (interactive "P")
  16061. (if (org-inside-LaTeX-fragment-p)
  16062. (call-interactively 'cdlatex-math-modify)
  16063. (let (org-cdlatex-mode)
  16064. (call-interactively (key-binding (vector last-input-event))))))
  16065. (defvar org-latex-fragment-image-overlays nil
  16066. "List of overlays carrying the images of latex fragments.")
  16067. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16068. (defun org-remove-latex-fragment-image-overlays ()
  16069. "Remove all overlays with LaTeX fragment images in current buffer."
  16070. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16071. (setq org-latex-fragment-image-overlays nil))
  16072. (defun org-preview-latex-fragment (&optional subtree)
  16073. "Preview the LaTeX fragment at point, or all locally or globally.
  16074. If the cursor is in a LaTeX fragment, create the image and overlay
  16075. it over the source code. If there is no fragment at point, display
  16076. all fragments in the current text, from one headline to the next. With
  16077. prefix SUBTREE, display all fragments in the current subtree. With a
  16078. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16079. the cursor is before the first headline,
  16080. display all fragments in the buffer.
  16081. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16082. (interactive "P")
  16083. (unless buffer-file-name
  16084. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16085. (org-remove-latex-fragment-image-overlays)
  16086. (save-excursion
  16087. (save-restriction
  16088. (let (beg end at msg)
  16089. (cond
  16090. ((or (equal subtree '(16))
  16091. (not (save-excursion
  16092. (re-search-backward org-outline-regexp-bol nil t))))
  16093. (setq beg (point-min) end (point-max)
  16094. msg "Creating images for buffer...%s"))
  16095. ((equal subtree '(4))
  16096. (org-back-to-heading)
  16097. (setq beg (point) end (org-end-of-subtree t)
  16098. msg "Creating images for subtree...%s"))
  16099. (t
  16100. (if (setq at (org-inside-LaTeX-fragment-p))
  16101. (goto-char (max (point-min) (- (cdr at) 2)))
  16102. (org-back-to-heading))
  16103. (setq beg (point) end (progn (outline-next-heading) (point))
  16104. msg (if at "Creating image...%s"
  16105. "Creating images for entry...%s"))))
  16106. (message msg "")
  16107. (narrow-to-region beg end)
  16108. (goto-char beg)
  16109. (org-format-latex
  16110. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16111. (file-name-nondirectory
  16112. buffer-file-name)))
  16113. default-directory 'overlays msg at 'forbuffer
  16114. org-latex-create-formula-image-program)
  16115. (message msg "done. Use `C-c C-c' to remove images.")))))
  16116. (defvar org-latex-regexps
  16117. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16118. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16119. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16120. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16121. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16122. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16123. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16124. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16125. "Regular expressions for matching embedded LaTeX.")
  16126. (defvar org-export-have-math nil) ;; dynamic scoping
  16127. (defun org-format-latex (prefix &optional dir overlays msg at
  16128. forbuffer processing-type)
  16129. "Replace LaTeX fragments with links to an image, and produce images.
  16130. Some of the options can be changed using the variable
  16131. `org-format-latex-options'."
  16132. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16133. (let* ((prefixnodir (file-name-nondirectory prefix))
  16134. (absprefix (expand-file-name prefix dir))
  16135. (todir (file-name-directory absprefix))
  16136. (opt org-format-latex-options)
  16137. (optnew org-format-latex-options)
  16138. (matchers (plist-get opt :matchers))
  16139. (re-list org-latex-regexps)
  16140. (org-format-latex-header-extra
  16141. (plist-get (org-infile-export-plist) :latex-header-extra))
  16142. (cnt 0) txt hash link beg end re e checkdir
  16143. string
  16144. m n block-type block linkfile movefile ov)
  16145. ;; Check the different regular expressions
  16146. (while (setq e (pop re-list))
  16147. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16148. block (if block-type "\n\n" ""))
  16149. (when (member m matchers)
  16150. (goto-char (point-min))
  16151. (while (re-search-forward re nil t)
  16152. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16153. (not (get-text-property (match-beginning n)
  16154. 'org-protected))
  16155. (or (not overlays)
  16156. (not (eq (get-char-property (match-beginning n)
  16157. 'org-overlay-type)
  16158. 'org-latex-overlay))))
  16159. (setq org-export-have-math t)
  16160. (cond
  16161. ((eq processing-type 'verbatim)
  16162. ;; Leave the text verbatim, just protect it
  16163. (add-text-properties (match-beginning n) (match-end n)
  16164. '(org-protected t)))
  16165. ((eq processing-type 'mathjax)
  16166. ;; Prepare for MathJax processing
  16167. (setq string (match-string n))
  16168. (if (member m '("$" "$1"))
  16169. (save-excursion
  16170. (delete-region (match-beginning n) (match-end n))
  16171. (goto-char (match-beginning n))
  16172. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  16173. "\\)")
  16174. '(org-protected t))))
  16175. (add-text-properties (match-beginning n) (match-end n)
  16176. '(org-protected t))))
  16177. ((or (eq processing-type 'dvipng)
  16178. (eq processing-type 'imagemagick))
  16179. ;; Process to an image
  16180. (setq txt (match-string n)
  16181. beg (match-beginning n) end (match-end n)
  16182. cnt (1+ cnt))
  16183. (let ((face (face-at-point))
  16184. (fg (plist-get opt :foreground))
  16185. (bg (plist-get opt :background))
  16186. print-length print-level) ; make sure full list is printed
  16187. (when forbuffer
  16188. ; Get the colors from the face at point
  16189. (goto-char beg)
  16190. (when (eq fg 'auto)
  16191. (setq fg (face-attribute face :foreground nil 'default)))
  16192. (when (eq bg 'auto)
  16193. (setq bg (face-attribute face :background nil 'default)))
  16194. (setq optnew (copy-sequence opt))
  16195. (plist-put optnew :foreground fg)
  16196. (plist-put optnew :background bg))
  16197. (setq hash (sha1 (prin1-to-string
  16198. (list org-format-latex-header
  16199. org-format-latex-header-extra
  16200. org-export-latex-default-packages-alist
  16201. org-export-latex-packages-alist
  16202. org-format-latex-options
  16203. forbuffer txt fg bg)))
  16204. linkfile (format "%s_%s.png" prefix hash)
  16205. movefile (format "%s_%s.png" absprefix hash)))
  16206. (setq link (concat block "[[file:" linkfile "]]" block))
  16207. (if msg (message msg cnt))
  16208. (goto-char beg)
  16209. (unless checkdir ; make sure the directory exists
  16210. (setq checkdir t)
  16211. (or (file-directory-p todir) (make-directory todir t)))
  16212. (unless (file-exists-p movefile)
  16213. (org-create-formula-image
  16214. txt movefile optnew forbuffer processing-type))
  16215. (if overlays
  16216. (progn
  16217. (mapc (lambda (o)
  16218. (if (eq (overlay-get o 'org-overlay-type)
  16219. 'org-latex-overlay)
  16220. (delete-overlay o)))
  16221. (overlays-in beg end))
  16222. (setq ov (make-overlay beg end))
  16223. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16224. (if (featurep 'xemacs)
  16225. (progn
  16226. (overlay-put ov 'invisible t)
  16227. (overlay-put
  16228. ov 'end-glyph
  16229. (make-glyph (vector 'png :file movefile))))
  16230. (overlay-put
  16231. ov 'display
  16232. (list 'image :type 'png :file movefile :ascent 'center)))
  16233. (push ov org-latex-fragment-image-overlays)
  16234. (goto-char end))
  16235. (delete-region beg end)
  16236. (insert (org-add-props link
  16237. (list 'org-latex-src
  16238. (replace-regexp-in-string
  16239. "\"" "" txt)
  16240. 'org-latex-src-embed-type
  16241. (if block-type 'paragraph 'character))))))
  16242. ((eq processing-type 'mathml)
  16243. ;; Process to MathML
  16244. (unless (save-match-data (org-format-latex-mathml-available-p))
  16245. (error "LaTeX to MathML converter not configured"))
  16246. (setq txt (match-string n)
  16247. beg (match-beginning n) end (match-end n)
  16248. cnt (1+ cnt))
  16249. (if msg (message msg cnt))
  16250. (goto-char beg)
  16251. (delete-region beg end)
  16252. (insert (org-format-latex-as-mathml
  16253. txt block-type prefix dir)))
  16254. (t
  16255. (error "Unknown conversion type %s for latex fragments"
  16256. processing-type)))))))))
  16257. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16258. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16259. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16260. sucessful, return the portion between \"<math...> </math>\"
  16261. elements otherwise return nil. When MATHML-FILE is specified,
  16262. write the results in to that file. When invoked as an
  16263. interactive command, prompt for LATEX-FRAG, with initial value
  16264. set to the current active region and echo the results for user
  16265. inspection."
  16266. (interactive (list (let ((frag (when (org-region-active-p)
  16267. (buffer-substring-no-properties
  16268. (region-beginning) (region-end)))))
  16269. (read-string "LaTeX Fragment: " frag nil frag))))
  16270. (unless latex-frag (error "Invalid latex-frag"))
  16271. (let* ((tmp-in-file (file-relative-name
  16272. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16273. (ignore (write-region latex-frag nil tmp-in-file))
  16274. (tmp-out-file (file-relative-name
  16275. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16276. (cmd (format-spec
  16277. org-latex-to-mathml-convert-command
  16278. `((?j . ,(shell-quote-argument
  16279. (expand-file-name org-latex-to-mathml-jar-file)))
  16280. (?I . ,(shell-quote-argument tmp-in-file))
  16281. (?o . ,(shell-quote-argument tmp-out-file)))))
  16282. mathml shell-command-output)
  16283. (when (org-called-interactively-p 'any)
  16284. (unless (org-format-latex-mathml-available-p)
  16285. (error "LaTeX to MathML converter not configured")))
  16286. (message "Running %s" cmd)
  16287. (setq shell-command-output (shell-command-to-string cmd))
  16288. (setq mathml
  16289. (when (file-readable-p tmp-out-file)
  16290. (with-current-buffer (find-file-noselect tmp-out-file t)
  16291. (goto-char (point-min))
  16292. (when (re-search-forward
  16293. (concat
  16294. (regexp-quote
  16295. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16296. "\\(.\\|\n\\)*"
  16297. (regexp-quote "</math>")) nil t)
  16298. (prog1 (match-string 0) (kill-buffer))))))
  16299. (cond
  16300. (mathml
  16301. (setq mathml
  16302. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16303. (when mathml-file
  16304. (write-region mathml nil mathml-file))
  16305. (when (org-called-interactively-p 'any)
  16306. (message mathml)))
  16307. ((message "LaTeX to MathML conversion failed")
  16308. (message shell-command-output)))
  16309. (delete-file tmp-in-file)
  16310. (when (file-exists-p tmp-out-file)
  16311. (delete-file tmp-out-file))
  16312. mathml))
  16313. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16314. prefix &optional dir)
  16315. "Use `org-create-math-formula' but check local cache first."
  16316. (let* ((absprefix (expand-file-name prefix dir))
  16317. (print-length nil) (print-level nil)
  16318. (formula-id (concat
  16319. "formula-"
  16320. (sha1
  16321. (prin1-to-string
  16322. (list latex-frag
  16323. org-latex-to-mathml-convert-command)))))
  16324. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16325. (formula-cache-dir (file-name-directory formula-cache)))
  16326. (unless (file-directory-p formula-cache-dir)
  16327. (make-directory formula-cache-dir t))
  16328. (unless (file-exists-p formula-cache)
  16329. (org-create-math-formula latex-frag formula-cache))
  16330. (if (file-exists-p formula-cache)
  16331. ;; Successful conversion. Return the link to MathML file.
  16332. (org-add-props
  16333. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16334. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16335. 'org-latex-src-embed-type (if latex-frag-type
  16336. 'paragraph 'character)))
  16337. ;; Failed conversion. Return the LaTeX fragment verbatim
  16338. (add-text-properties
  16339. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  16340. latex-frag)))
  16341. (defun org-create-formula-image (string tofile options buffer &optional type)
  16342. "Create an image from LaTeX source using dvipng or convert.
  16343. This function calls either `org-create-formula-image-with-dvipng'
  16344. or `org-create-formula-image-with-imagemagick' depending on the
  16345. value of `org-latex-create-formula-image-program' or on the value
  16346. of the optional TYPE variable.
  16347. Note: ultimately these two function should be combined as they
  16348. share a good deal of logic."
  16349. (org-check-external-command
  16350. "latex" "needed to convert LaTeX fragments to images")
  16351. (funcall
  16352. (case (or type org-latex-create-formula-image-program)
  16353. ('dvipng
  16354. (org-check-external-command
  16355. "dvipng" "needed to convert LaTeX fragments to images")
  16356. #'org-create-formula-image-with-dvipng)
  16357. ('imagemagick
  16358. (org-check-external-command
  16359. "convert" "you need to install imagemagick")
  16360. #'org-create-formula-image-with-imagemagick)
  16361. (t (error
  16362. "invalid value of `org-latex-create-formula-image-program'")))
  16363. string tofile options buffer))
  16364. ;; This function borrows from Ganesh Swami's latex2png.el
  16365. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16366. "This calls dvipng."
  16367. (require 'org-latex)
  16368. (let* ((tmpdir (if (featurep 'xemacs)
  16369. (temp-directory)
  16370. temporary-file-directory))
  16371. (texfilebase (make-temp-name
  16372. (expand-file-name "orgtex" tmpdir)))
  16373. (texfile (concat texfilebase ".tex"))
  16374. (dvifile (concat texfilebase ".dvi"))
  16375. (pngfile (concat texfilebase ".png"))
  16376. (fnh (if (featurep 'xemacs)
  16377. (font-height (face-font 'default))
  16378. (face-attribute 'default :height nil)))
  16379. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16380. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16381. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16382. "Black"))
  16383. (bg (or (plist-get options (if buffer :background :html-background))
  16384. "Transparent")))
  16385. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16386. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16387. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16388. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16389. (with-temp-file texfile
  16390. (insert (org-splice-latex-header
  16391. org-format-latex-header
  16392. org-export-latex-default-packages-alist
  16393. org-export-latex-packages-alist t
  16394. org-format-latex-header-extra))
  16395. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  16396. (require 'org-latex)
  16397. (org-export-latex-fix-inputenc))
  16398. (let ((dir default-directory))
  16399. (condition-case nil
  16400. (progn
  16401. (cd tmpdir)
  16402. (call-process "latex" nil nil nil texfile))
  16403. (error nil))
  16404. (cd dir))
  16405. (if (not (file-exists-p dvifile))
  16406. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16407. (condition-case nil
  16408. (if (featurep 'xemacs)
  16409. (call-process "dvipng" nil nil nil
  16410. "-fg" fg "-bg" bg
  16411. "-T" "tight"
  16412. "-o" pngfile
  16413. dvifile)
  16414. (call-process "dvipng" nil nil nil
  16415. "-fg" fg "-bg" bg
  16416. "-D" dpi
  16417. ;;"-x" scale "-y" scale
  16418. "-T" "tight"
  16419. "-o" pngfile
  16420. dvifile))
  16421. (error nil))
  16422. (if (not (file-exists-p pngfile))
  16423. (if org-format-latex-signal-error
  16424. (error "Failed to create png file from %s" texfile)
  16425. (message "Failed to create png file from %s" texfile)
  16426. nil)
  16427. ;; Use the requested file name and clean up
  16428. (copy-file pngfile tofile 'replace)
  16429. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16430. (if (file-exists-p (concat texfilebase e))
  16431. (delete-file (concat texfilebase e))))
  16432. pngfile))))
  16433. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  16434. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16435. "This calls convert, which is included into imagemagick."
  16436. (require 'org-latex)
  16437. (let* ((tmpdir (if (featurep 'xemacs)
  16438. (temp-directory)
  16439. temporary-file-directory))
  16440. (texfilebase (make-temp-name
  16441. (expand-file-name "orgtex" tmpdir)))
  16442. (texfile (concat texfilebase ".tex"))
  16443. (pdffile (concat texfilebase ".pdf"))
  16444. (pngfile (concat texfilebase ".png"))
  16445. (fnh (if (featurep 'xemacs)
  16446. (font-height (face-font 'default))
  16447. (face-attribute 'default :height nil)))
  16448. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16449. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16450. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16451. "black"))
  16452. (bg (or (plist-get options (if buffer :background :html-background))
  16453. "white")))
  16454. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16455. (setq fg (org-latex-color-format fg)))
  16456. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16457. (setq bg (org-latex-color-format
  16458. (if (string= bg "Transparent") "white" bg))))
  16459. (with-temp-file texfile
  16460. (insert (org-splice-latex-header
  16461. org-format-latex-header
  16462. org-export-latex-default-packages-alist
  16463. org-export-latex-packages-alist t
  16464. org-format-latex-header-extra))
  16465. (insert "\n\\begin{document}\n"
  16466. "\\definecolor{fg}{rgb}{" fg "}\n"
  16467. "\\definecolor{bg}{rgb}{" bg "}\n"
  16468. "\n\\pagecolor{bg}\n"
  16469. "\n{\\color{fg}\n"
  16470. string
  16471. "\n}\n"
  16472. "\n\\end{document}\n" )
  16473. (require 'org-latex)
  16474. (org-export-latex-fix-inputenc))
  16475. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16476. (condition-case nil
  16477. (progn
  16478. (cd tmpdir)
  16479. (setq cmds org-latex-to-pdf-process)
  16480. (while cmds
  16481. (setq latex-frags-cmds (pop cmds))
  16482. (if (listp latex-frags-cmds)
  16483. (setq cmds nil)
  16484. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  16485. (while latex-frags-cmds
  16486. (setq cmd (pop latex-frags-cmds))
  16487. (while (string-match "%b" cmd)
  16488. (setq cmd (replace-match
  16489. (save-match-data
  16490. (shell-quote-argument texfile))
  16491. t t cmd)))
  16492. (while (string-match "%f" cmd)
  16493. (setq cmd (replace-match
  16494. (save-match-data
  16495. (shell-quote-argument (file-name-nondirectory texfile)))
  16496. t t cmd)))
  16497. (while (string-match "%o" cmd)
  16498. (setq cmd (replace-match
  16499. (save-match-data
  16500. (shell-quote-argument (file-name-directory texfile)))
  16501. t t cmd)))
  16502. (setq cmd (split-string cmd))
  16503. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16504. (error nil))
  16505. (cd dir))
  16506. (if (not (file-exists-p pdffile))
  16507. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16508. (condition-case nil
  16509. (if (featurep 'xemacs)
  16510. (call-process "convert" nil nil nil
  16511. "-density" "96"
  16512. "-trim"
  16513. "-antialias"
  16514. pdffile
  16515. "-quality" "100"
  16516. ;; "-sharpen" "0x1.0"
  16517. pngfile)
  16518. (call-process "convert" nil nil nil
  16519. "-density" dpi
  16520. "-trim"
  16521. "-antialias"
  16522. pdffile
  16523. "-quality" "100"
  16524. ; "-sharpen" "0x1.0"
  16525. pngfile))
  16526. (error nil))
  16527. (if (not (file-exists-p pngfile))
  16528. (if org-format-latex-signal-error
  16529. (error "Failed to create png file from %s" texfile)
  16530. (message "Failed to create png file from %s" texfile)
  16531. nil)
  16532. ;; Use the requested file name and clean up
  16533. (copy-file pngfile tofile 'replace)
  16534. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16535. (if (file-exists-p (concat texfilebase e))
  16536. (delete-file (concat texfilebase e))))
  16537. pngfile))))
  16538. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16539. "Fill a LaTeX header template TPL.
  16540. In the template, the following place holders will be recognized:
  16541. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16542. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16543. [PACKAGES] \\usepackage statements for PKG
  16544. [NO-PACKAGES] do not include PKG
  16545. [EXTRA] the string EXTRA
  16546. [NO-EXTRA] do not include EXTRA
  16547. For backward compatibility, if both the positive and the negative place
  16548. holder is missing, the positive one (without the \"NO-\") will be
  16549. assumed to be present at the end of the template.
  16550. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16551. EXTRA is a string.
  16552. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16553. (let (rpl (end ""))
  16554. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16555. (setq rpl (if (or (match-end 1) (not def-pkg))
  16556. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16557. tpl (replace-match rpl t t tpl))
  16558. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16559. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16560. (setq rpl (if (or (match-end 1) (not pkg))
  16561. "" (org-latex-packages-to-string pkg snippets-p t))
  16562. tpl (replace-match rpl t t tpl))
  16563. (if pkg (setq end
  16564. (concat end "\n"
  16565. (org-latex-packages-to-string pkg snippets-p)))))
  16566. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16567. (setq rpl (if (or (match-end 1) (not extra))
  16568. "" (concat extra "\n"))
  16569. tpl (replace-match rpl t t tpl))
  16570. (if (and extra (string-match "\\S-" extra))
  16571. (setq end (concat end "\n" extra))))
  16572. (if (string-match "\\S-" end)
  16573. (concat tpl "\n" end)
  16574. tpl)))
  16575. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16576. "Turn an alist of packages into a string with the \\usepackage macros."
  16577. (setq pkg (mapconcat (lambda(p)
  16578. (cond
  16579. ((stringp p) p)
  16580. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16581. (format "%% Package %s omitted" (cadr p)))
  16582. ((equal "" (car p))
  16583. (format "\\usepackage{%s}" (cadr p)))
  16584. (t
  16585. (format "\\usepackage[%s]{%s}"
  16586. (car p) (cadr p)))))
  16587. pkg
  16588. "\n"))
  16589. (if newline (concat pkg "\n") pkg))
  16590. (defun org-dvipng-color (attr)
  16591. "Return a RGB color specification for dvipng."
  16592. (apply 'format "rgb %s %s %s"
  16593. (mapcar 'org-normalize-color
  16594. (if (featurep 'xemacs)
  16595. (color-rgb-components
  16596. (face-property 'default
  16597. (cond ((eq attr :foreground) 'foreground)
  16598. ((eq attr :background) 'background))))
  16599. (color-values (face-attribute 'default attr nil))))))
  16600. (defun org-dvipng-color-format (color-name)
  16601. "Convert COLOR-NAME to a RGB color value for dvipng."
  16602. (apply 'format "rgb %s %s %s"
  16603. (mapcar 'org-normalize-color
  16604. (color-values color-name))))
  16605. (defun org-latex-color (attr)
  16606. "Return a RGB color for the LaTeX color package."
  16607. (apply 'format "%s,%s,%s"
  16608. (mapcar 'org-normalize-color
  16609. (if (featurep 'xemacs)
  16610. (color-rgb-components
  16611. (face-property 'default
  16612. (cond ((eq attr :foreground) 'foreground)
  16613. ((eq attr :background) 'background))))
  16614. (color-values (face-attribute 'default attr nil))))))
  16615. (defun org-latex-color-format (color-name)
  16616. "Convert COLOR-NAME to a RGB color value."
  16617. (apply 'format "%s,%s,%s"
  16618. (mapcar 'org-normalize-color
  16619. (color-values color-name))))
  16620. (defun org-normalize-color (value)
  16621. "Return string to be used as color value for an RGB component."
  16622. (format "%g" (/ value 65535.0)))
  16623. ;; Image display
  16624. (defvar org-inline-image-overlays nil)
  16625. (make-variable-buffer-local 'org-inline-image-overlays)
  16626. (defun org-toggle-inline-images (&optional include-linked)
  16627. "Toggle the display of inline images.
  16628. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16629. (interactive "P")
  16630. (if org-inline-image-overlays
  16631. (progn
  16632. (org-remove-inline-images)
  16633. (message "Inline image display turned off"))
  16634. (org-display-inline-images include-linked)
  16635. (if (and (org-called-interactively-p)
  16636. org-inline-image-overlays)
  16637. (message "%d images displayed inline"
  16638. (length org-inline-image-overlays))
  16639. (message "No images to display inline"))))
  16640. (defun org-redisplay-inline-images ()
  16641. "Refresh the display of inline images."
  16642. (interactive)
  16643. (if (not org-inline-image-overlays)
  16644. (org-toggle-inline-images)
  16645. (org-toggle-inline-images)
  16646. (org-toggle-inline-images)))
  16647. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16648. "Display inline images.
  16649. Normally only links without a description part are inlined, because this
  16650. is how it will work for export. When INCLUDE-LINKED is set, also links
  16651. with a description part will be inlined. This can be nice for a quick
  16652. look at those images, but it does not reflect what exported files will look
  16653. like.
  16654. When REFRESH is set, refresh existing images between BEG and END.
  16655. This will create new image displays only if necessary.
  16656. BEG and END default to the buffer boundaries."
  16657. (interactive "P")
  16658. (unless refresh
  16659. (org-remove-inline-images)
  16660. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16661. (save-excursion
  16662. (save-restriction
  16663. (widen)
  16664. (setq beg (or beg (point-min)) end (or end (point-max)))
  16665. (goto-char beg)
  16666. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16667. (substring (org-image-file-name-regexp) 0 -2)
  16668. "\\)\\]" (if include-linked "" "\\]")))
  16669. old file ov img type attrwidth width)
  16670. (while (re-search-forward re end t)
  16671. (setq old (get-char-property-and-overlay (match-beginning 1)
  16672. 'org-image-overlay)
  16673. file (expand-file-name
  16674. (concat (or (match-string 3) "") (match-string 4))))
  16675. (when (image-type-available-p 'imagemagick)
  16676. (setq attrwidth (if (or (listp org-image-actual-width)
  16677. (null org-image-actual-width))
  16678. (save-excursion
  16679. (save-match-data
  16680. (when (re-search-backward
  16681. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16682. (save-excursion
  16683. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  16684. (string-to-number (match-string 1))))))
  16685. width (cond ((eq org-image-actual-width t) nil)
  16686. ((null org-image-actual-width) attrwidth)
  16687. ((numberp org-image-actual-width)
  16688. org-image-actual-width)
  16689. ((listp org-image-actual-width)
  16690. (or attrwidth (car org-image-actual-width))))
  16691. type (if width 'imagemagick)))
  16692. (when (file-exists-p file)
  16693. (if (and (car-safe old) refresh)
  16694. (image-refresh (overlay-get (cdr old) 'display))
  16695. (setq img (save-match-data (create-image file type nil :width width)))
  16696. (when img
  16697. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16698. (overlay-put ov 'display img)
  16699. (overlay-put ov 'face 'default)
  16700. (overlay-put ov 'org-image-overlay t)
  16701. (overlay-put ov 'modification-hooks
  16702. (list 'org-display-inline-remove-overlay))
  16703. (push ov org-inline-image-overlays)))))))))
  16704. (define-obsolete-function-alias
  16705. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16706. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16707. "Remove inline-display overlay if a corresponding region is modified."
  16708. (let ((inhibit-modification-hooks t))
  16709. (when (and ov after)
  16710. (delete ov org-inline-image-overlays)
  16711. (delete-overlay ov))))
  16712. (defun org-remove-inline-images ()
  16713. "Remove inline display of images."
  16714. (interactive)
  16715. (mapc 'delete-overlay org-inline-image-overlays)
  16716. (setq org-inline-image-overlays nil))
  16717. ;;;; Key bindings
  16718. ;; Outline functions from `outline-mode-prefix-map'
  16719. ;; that can be remapped in Org:
  16720. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16721. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16722. (define-key org-mode-map [remap outline-forward-same-level]
  16723. 'org-forward-heading-same-level)
  16724. (define-key org-mode-map [remap outline-backward-same-level]
  16725. 'org-backward-heading-same-level)
  16726. (define-key org-mode-map [remap show-branches]
  16727. 'org-kill-note-or-show-branches)
  16728. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16729. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16730. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16731. ;; Outline functions from `outline-mode-prefix-map' that can not
  16732. ;; be remapped in Org:
  16733. ;;
  16734. ;; - the column "key binding" shows whether the Outline function is still
  16735. ;; available in Org mode on the same key that it has been bound to in
  16736. ;; Outline mode:
  16737. ;; - "overridden": key used for a different functionality in Org mode
  16738. ;; - else: key still bound to the same Outline function in Org mode
  16739. ;;
  16740. ;; | Outline function | key binding | Org replacement |
  16741. ;; |------------------------------------+-------------+-----------------------|
  16742. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16743. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16744. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16745. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16746. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16747. ;; | `show-entry' | overridden | no replacement |
  16748. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16749. ;; | `show-branches' | `C-c C-k' | still same function |
  16750. ;; | `show-subtree' | overridden | visibility cycling |
  16751. ;; | `show-all' | overridden | no replacement |
  16752. ;; | `hide-subtree' | overridden | visibility cycling |
  16753. ;; | `hide-body' | overridden | no replacement |
  16754. ;; | `hide-entry' | overridden | visibility cycling |
  16755. ;; | `hide-leaves' | overridden | no replacement |
  16756. ;; | `hide-sublevels' | overridden | no replacement |
  16757. ;; | `hide-other' | overridden | no replacement |
  16758. ;; Make `C-c C-x' a prefix key
  16759. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16760. ;; TAB key with modifiers
  16761. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16762. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16763. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16764. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16765. ;; The following line is necessary under Suse GNU/Linux
  16766. (unless (featurep 'xemacs)
  16767. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16768. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16769. (define-key org-mode-map [backtab] 'org-shifttab)
  16770. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16771. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16772. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16773. ;; Cursor keys with modifiers
  16774. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16775. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16776. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16777. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16778. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16779. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16780. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16781. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16782. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16783. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16784. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16785. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16786. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16787. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16788. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16789. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16790. ;; Babel keys
  16791. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16792. (mapc (lambda (pair)
  16793. (define-key org-babel-map (car pair) (cdr pair)))
  16794. org-babel-key-bindings)
  16795. ;;; Extra keys for tty access.
  16796. ;; We only set them when really needed because otherwise the
  16797. ;; menus don't show the simple keys
  16798. (when (or org-use-extra-keys
  16799. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16800. (not window-system))
  16801. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16802. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16803. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16804. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16805. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16806. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16807. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16808. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16809. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16810. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16811. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16812. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16813. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16814. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16815. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16816. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16817. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16818. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16819. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16820. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16821. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16822. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16823. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16824. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16825. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16826. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16827. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16828. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16829. ;; All the other keys
  16830. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16831. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16832. (if (boundp 'narrow-map)
  16833. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16834. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16835. (if (boundp 'narrow-map)
  16836. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16837. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16838. (if (boundp 'narrow-map)
  16839. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16840. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16841. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  16842. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  16843. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  16844. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  16845. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  16846. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  16847. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  16848. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16849. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16850. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16851. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16852. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16853. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16854. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16855. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16856. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16857. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16858. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16859. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16860. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16861. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16862. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  16863. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16864. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16865. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16866. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16867. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16868. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16869. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16870. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16871. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16872. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16873. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16874. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  16875. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16876. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16877. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16878. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16879. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16880. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16881. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16882. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16883. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16884. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16885. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16886. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16887. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16888. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16889. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16890. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16891. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16892. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16893. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16894. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16895. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16896. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16897. (org-defkey org-mode-map "\C-m" 'org-return)
  16898. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16899. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16900. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16901. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16902. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16903. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16904. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16905. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16906. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16907. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16908. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16909. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16910. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16911. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16912. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16913. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16914. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16915. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16916. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16917. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16918. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  16919. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16920. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16921. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16922. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16923. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16924. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16925. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16926. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  16927. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  16928. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16929. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16930. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  16931. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16932. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16933. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16934. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16935. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16936. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  16937. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16938. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16939. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16940. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  16941. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16942. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  16943. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16944. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16945. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16946. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16947. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16948. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16949. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16950. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16951. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16952. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16953. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16954. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16955. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16956. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16957. (when (featurep 'xemacs)
  16958. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16959. (defconst org-speed-commands-default
  16960. '(
  16961. ("Outline Navigation")
  16962. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16963. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16964. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  16965. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  16966. ("u" . (org-speed-move-safe 'outline-up-heading))
  16967. ("j" . org-goto)
  16968. ("g" . (org-refile t))
  16969. ("Outline Visibility")
  16970. ("c" . org-cycle)
  16971. ("C" . org-shifttab)
  16972. (" " . org-display-outline-path)
  16973. ("=" . org-columns)
  16974. ("Outline Structure Editing")
  16975. ("U" . org-shiftmetaup)
  16976. ("D" . org-shiftmetadown)
  16977. ("r" . org-metaright)
  16978. ("l" . org-metaleft)
  16979. ("R" . org-shiftmetaright)
  16980. ("L" . org-shiftmetaleft)
  16981. ("i" . (progn (forward-char 1) (call-interactively
  16982. 'org-insert-heading-respect-content)))
  16983. ("^" . org-sort)
  16984. ("w" . org-refile)
  16985. ("a" . org-archive-subtree-default-with-confirmation)
  16986. ("." . org-mark-subtree)
  16987. ("#" . org-toggle-comment)
  16988. ("Clock Commands")
  16989. ("I" . org-clock-in)
  16990. ("O" . org-clock-out)
  16991. ("Meta Data Editing")
  16992. ("t" . org-todo)
  16993. ("," . (org-priority))
  16994. ("0" . (org-priority ?\ ))
  16995. ("1" . (org-priority ?A))
  16996. ("2" . (org-priority ?B))
  16997. ("3" . (org-priority ?C))
  16998. (":" . org-set-tags-command)
  16999. ("e" . org-set-effort)
  17000. ("E" . org-inc-effort)
  17001. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17002. (org-entry-put (point) "APPT_WARNTIME" m)))
  17003. ("Agenda Views etc")
  17004. ("v" . org-agenda)
  17005. ("/" . org-sparse-tree)
  17006. ("Misc")
  17007. ("o" . org-open-at-point)
  17008. ("?" . org-speed-command-help)
  17009. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17010. (">" . (org-agenda-remove-restriction-lock))
  17011. )
  17012. "The default speed commands.")
  17013. (defun org-print-speed-command (e)
  17014. (if (> (length (car e)) 1)
  17015. (progn
  17016. (princ "\n")
  17017. (princ (car e))
  17018. (princ "\n")
  17019. (princ (make-string (length (car e)) ?-))
  17020. (princ "\n"))
  17021. (princ (car e))
  17022. (princ " ")
  17023. (if (symbolp (cdr e))
  17024. (princ (symbol-name (cdr e)))
  17025. (prin1 (cdr e)))
  17026. (princ "\n")))
  17027. (defun org-speed-command-help ()
  17028. "Show the available speed commands."
  17029. (interactive)
  17030. (if (not org-use-speed-commands)
  17031. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17032. (with-output-to-temp-buffer "*Help*"
  17033. (princ "User-defined Speed commands\n===========================\n")
  17034. (mapc 'org-print-speed-command org-speed-commands-user)
  17035. (princ "\n")
  17036. (princ "Built-in Speed commands\n=======================\n")
  17037. (mapc 'org-print-speed-command org-speed-commands-default))
  17038. (with-current-buffer "*Help*"
  17039. (setq truncate-lines t))))
  17040. (defun org-speed-move-safe (cmd)
  17041. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17042. If not, return to the original position and throw an error."
  17043. (interactive)
  17044. (let ((pos (point)))
  17045. (call-interactively cmd)
  17046. (unless (and (bolp) (org-at-heading-p))
  17047. (goto-char pos)
  17048. (error "Boundary reached while executing %s" cmd))))
  17049. (defvar org-self-insert-command-undo-counter 0)
  17050. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17051. (defvar org-speed-command nil)
  17052. (define-obsolete-function-alias
  17053. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17054. (defun org-speed-command-activate (keys)
  17055. "Hook for activating single-letter speed commands.
  17056. `org-speed-commands-default' specifies a minimal command set.
  17057. Use `org-speed-commands-user' for further customization."
  17058. (when (or (and (bolp) (looking-at org-outline-regexp))
  17059. (and (functionp org-use-speed-commands)
  17060. (funcall org-use-speed-commands)))
  17061. (cdr (assoc keys (append org-speed-commands-user
  17062. org-speed-commands-default)))))
  17063. (define-obsolete-function-alias
  17064. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17065. (defun org-babel-speed-command-activate (keys)
  17066. "Hook for activating single-letter code block commands."
  17067. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17068. (cdr (assoc keys org-babel-key-bindings))))
  17069. (defcustom org-speed-command-hook
  17070. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17071. "Hook for activating speed commands at strategic locations.
  17072. Hook functions are called in sequence until a valid handler is
  17073. found.
  17074. Each hook takes a single argument, a user-pressed command key
  17075. which is also a `self-insert-command' from the global map.
  17076. Within the hook, examine the cursor position and the command key
  17077. and return nil or a valid handler as appropriate. Handler could
  17078. be one of an interactive command, a function, or a form.
  17079. Set `org-use-speed-commands' to non-nil value to enable this
  17080. hook. The default setting is `org-speed-command-activate'."
  17081. :group 'org-structure
  17082. :version "24.1"
  17083. :type 'hook)
  17084. (defun org-self-insert-command (N)
  17085. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17086. If the cursor is in a table looking at whitespace, the whitespace is
  17087. overwritten, and the table is not marked as requiring realignment."
  17088. (interactive "p")
  17089. (org-check-before-invisible-edit 'insert)
  17090. (cond
  17091. ((and org-use-speed-commands
  17092. (setq org-speed-command
  17093. (run-hook-with-args-until-success
  17094. 'org-speed-command-hook (this-command-keys))))
  17095. (cond
  17096. ((commandp org-speed-command)
  17097. (setq this-command org-speed-command)
  17098. (call-interactively org-speed-command))
  17099. ((functionp org-speed-command)
  17100. (funcall org-speed-command))
  17101. ((and org-speed-command (listp org-speed-command))
  17102. (eval org-speed-command))
  17103. (t (let (org-use-speed-commands)
  17104. (call-interactively 'org-self-insert-command)))))
  17105. ((and
  17106. (org-table-p)
  17107. (progn
  17108. ;; check if we blank the field, and if that triggers align
  17109. (and (featurep 'org-table) org-table-auto-blank-field
  17110. (member last-command
  17111. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17112. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17113. ;; got extra space, this field does not determine column width
  17114. (let (org-table-may-need-update) (org-table-blank-field))
  17115. ;; no extra space, this field may determine column width
  17116. (org-table-blank-field)))
  17117. t)
  17118. (eq N 1)
  17119. (looking-at "[^|\n]* |"))
  17120. (let (org-table-may-need-update)
  17121. (goto-char (1- (match-end 0)))
  17122. (backward-delete-char 1)
  17123. (goto-char (match-beginning 0))
  17124. (self-insert-command N)))
  17125. (t
  17126. (setq org-table-may-need-update t)
  17127. (self-insert-command N)
  17128. (org-fix-tags-on-the-fly)
  17129. (if org-self-insert-cluster-for-undo
  17130. (if (not (eq last-command 'org-self-insert-command))
  17131. (setq org-self-insert-command-undo-counter 1)
  17132. (if (>= org-self-insert-command-undo-counter 20)
  17133. (setq org-self-insert-command-undo-counter 1)
  17134. (and (> org-self-insert-command-undo-counter 0)
  17135. buffer-undo-list (listp buffer-undo-list)
  17136. (not (cadr buffer-undo-list)) ; remove nil entry
  17137. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17138. (setq org-self-insert-command-undo-counter
  17139. (1+ org-self-insert-command-undo-counter))))))))
  17140. (defun org-check-before-invisible-edit (kind)
  17141. "Check is editing if kind KIND would be dangerous with invisible text around.
  17142. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17143. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17144. (if (and org-catch-invisible-edits
  17145. (or (not (boundp 'visible-mode)) (not visible-mode))
  17146. (or (get-char-property (point) 'invisible)
  17147. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17148. ;; OK, we need to take a closer look
  17149. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17150. (invisible-before-point (if (bobp) nil (get-char-property
  17151. (1- (point)) 'invisible)))
  17152. (border-and-ok-direction
  17153. (or
  17154. ;; Check if we are acting predictably before invisible text
  17155. (and invisible-at-point (not invisible-before-point)
  17156. (memq kind '(insert delete-backward)))
  17157. ;; Check if we are acting predictably after invisible text
  17158. ;; This works not well, and I have turned it off. It seems
  17159. ;; better to always show and stop after invisible text.
  17160. ;; (and (not invisible-at-point) invisible-before-point
  17161. ;; (memq kind '(insert delete)))
  17162. )))
  17163. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17164. (memq invisible-before-point '(outline org-hide-block t)))
  17165. (if (eq org-catch-invisible-edits 'error)
  17166. (error "Editing in invisible areas is prohibited - make visible first"))
  17167. (if (and org-custom-properties-overlays
  17168. (y-or-n-p "Display invisible properties in this buffer? "))
  17169. (org-toggle-custom-properties-visibility)
  17170. ;; Make the area visible
  17171. (save-excursion
  17172. (if invisible-before-point
  17173. (goto-char (previous-single-char-property-change
  17174. (point) 'invisible)))
  17175. (org-cycle))
  17176. (cond
  17177. ((eq org-catch-invisible-edits 'show)
  17178. ;; That's it, we do the edit after showing
  17179. (message
  17180. "Unfolding invisible region around point before editing")
  17181. (sit-for 1))
  17182. ((and (eq org-catch-invisible-edits 'smart)
  17183. border-and-ok-direction)
  17184. (message "Unfolding invisible region around point before editing"))
  17185. (t
  17186. ;; Don't do the edit, make the user repeat it in full visibility
  17187. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17188. (defun org-fix-tags-on-the-fly ()
  17189. (when (and (equal (char-after (point-at-bol)) ?*)
  17190. (org-at-heading-p))
  17191. (org-align-tags-here org-tags-column)))
  17192. (defun org-delete-backward-char (N)
  17193. "Like `delete-backward-char', insert whitespace at field end in tables.
  17194. When deleting backwards, in tables this function will insert whitespace in
  17195. front of the next \"|\" separator, to keep the table aligned. The table will
  17196. still be marked for re-alignment if the field did fill the entire column,
  17197. because, in this case the deletion might narrow the column."
  17198. (interactive "p")
  17199. (save-match-data
  17200. (org-check-before-invisible-edit 'delete-backward)
  17201. (if (and (org-table-p)
  17202. (eq N 1)
  17203. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17204. (looking-at ".*?|"))
  17205. (let ((pos (point))
  17206. (noalign (looking-at "[^|\n\r]* |"))
  17207. (c org-table-may-need-update))
  17208. (backward-delete-char N)
  17209. (if (not overwrite-mode)
  17210. (progn
  17211. (skip-chars-forward "^|")
  17212. (insert " ")
  17213. (goto-char (1- pos))))
  17214. ;; noalign: if there were two spaces at the end, this field
  17215. ;; does not determine the width of the column.
  17216. (if noalign (setq org-table-may-need-update c)))
  17217. (backward-delete-char N)
  17218. (org-fix-tags-on-the-fly))))
  17219. (defun org-delete-char (N)
  17220. "Like `delete-char', but insert whitespace at field end in tables.
  17221. When deleting characters, in tables this function will insert whitespace in
  17222. front of the next \"|\" separator, to keep the table aligned. The table will
  17223. still be marked for re-alignment if the field did fill the entire column,
  17224. because, in this case the deletion might narrow the column."
  17225. (interactive "p")
  17226. (save-match-data
  17227. (org-check-before-invisible-edit 'delete)
  17228. (if (and (org-table-p)
  17229. (not (bolp))
  17230. (not (= (char-after) ?|))
  17231. (eq N 1))
  17232. (if (looking-at ".*?|")
  17233. (let ((pos (point))
  17234. (noalign (looking-at "[^|\n\r]* |"))
  17235. (c org-table-may-need-update))
  17236. (replace-match (concat
  17237. (substring (match-string 0) 1 -1)
  17238. " |"))
  17239. (goto-char pos)
  17240. ;; noalign: if there were two spaces at the end, this field
  17241. ;; does not determine the width of the column.
  17242. (if noalign (setq org-table-may-need-update c)))
  17243. (delete-char N))
  17244. (delete-char N)
  17245. (org-fix-tags-on-the-fly))))
  17246. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17247. (put 'org-self-insert-command 'delete-selection t)
  17248. (put 'orgtbl-self-insert-command 'delete-selection t)
  17249. (put 'org-delete-char 'delete-selection 'supersede)
  17250. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17251. (put 'org-yank 'delete-selection 'yank)
  17252. ;; Make `flyspell-mode' delay after some commands
  17253. (put 'org-self-insert-command 'flyspell-delayed t)
  17254. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17255. (put 'org-delete-char 'flyspell-delayed t)
  17256. (put 'org-delete-backward-char 'flyspell-delayed t)
  17257. ;; Make pabbrev-mode expand after org-mode commands
  17258. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17259. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17260. ;; How to do this: Measure non-white length of current string
  17261. ;; If equal to column width, we should realign.
  17262. (defun org-remap (map &rest commands)
  17263. "In MAP, remap the functions given in COMMANDS.
  17264. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17265. (let (new old)
  17266. (while commands
  17267. (setq old (pop commands) new (pop commands))
  17268. (if (fboundp 'command-remapping)
  17269. (org-defkey map (vector 'remap old) new)
  17270. (substitute-key-definition old new map global-map)))))
  17271. (when (eq org-enable-table-editor 'optimized)
  17272. ;; If the user wants maximum table support, we need to hijack
  17273. ;; some standard editing functions
  17274. (org-remap org-mode-map
  17275. 'self-insert-command 'org-self-insert-command
  17276. 'delete-char 'org-delete-char
  17277. 'delete-backward-char 'org-delete-backward-char)
  17278. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17279. (defvar org-ctrl-c-ctrl-c-hook nil
  17280. "Hook for functions attaching themselves to `C-c C-c'.
  17281. This can be used to add additional functionality to the C-c C-c
  17282. key which executes context-dependent commands. This hook is run
  17283. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17284. run after the last test.
  17285. Each function will be called with no arguments. The function
  17286. must check if the context is appropriate for it to act. If yes,
  17287. it should do its thing and then return a non-nil value. If the
  17288. context is wrong, just do nothing and return nil.")
  17289. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17290. "Hook for functions attaching themselves to `C-c C-c'.
  17291. This can be used to add additional functionality to the C-c C-c
  17292. key which executes context-dependent commands. This hook is run
  17293. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17294. before the first test.
  17295. Each function will be called with no arguments. The function
  17296. must check if the context is appropriate for it to act. If yes,
  17297. it should do its thing and then return a non-nil value. If the
  17298. context is wrong, just do nothing and return nil.")
  17299. (defvar org-tab-first-hook nil
  17300. "Hook for functions to attach themselves to TAB.
  17301. See `org-ctrl-c-ctrl-c-hook' for more information.
  17302. This hook runs as the first action when TAB is pressed, even before
  17303. `org-cycle' messes around with the `outline-regexp' to cater for
  17304. inline tasks and plain list item folding.
  17305. If any function in this hook returns t, any other actions that
  17306. would have been caused by TAB (such as table field motion or visibility
  17307. cycling) will not occur.")
  17308. (defvar org-tab-after-check-for-table-hook nil
  17309. "Hook for functions to attach themselves to TAB.
  17310. See `org-ctrl-c-ctrl-c-hook' for more information.
  17311. This hook runs after it has been established that the cursor is not in a
  17312. table, but before checking if the cursor is in a headline or if global cycling
  17313. should be done.
  17314. If any function in this hook returns t, not other actions like visibility
  17315. cycling will be done.")
  17316. (defvar org-tab-after-check-for-cycling-hook nil
  17317. "Hook for functions to attach themselves to TAB.
  17318. See `org-ctrl-c-ctrl-c-hook' for more information.
  17319. This hook runs after it has been established that not table field motion and
  17320. not visibility should be done because of current context. This is probably
  17321. the place where a package like yasnippets can hook in.")
  17322. (defvar org-tab-before-tab-emulation-hook nil
  17323. "Hook for functions to attach themselves to TAB.
  17324. See `org-ctrl-c-ctrl-c-hook' for more information.
  17325. This hook runs after every other options for TAB have been exhausted, but
  17326. before indentation and \t insertion takes place.")
  17327. (defvar org-metaleft-hook nil
  17328. "Hook for functions attaching themselves to `M-left'.
  17329. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17330. (defvar org-metaright-hook nil
  17331. "Hook for functions attaching themselves to `M-right'.
  17332. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17333. (defvar org-metaup-hook nil
  17334. "Hook for functions attaching themselves to `M-up'.
  17335. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17336. (defvar org-metadown-hook nil
  17337. "Hook for functions attaching themselves to `M-down'.
  17338. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17339. (defvar org-shiftmetaleft-hook nil
  17340. "Hook for functions attaching themselves to `M-S-left'.
  17341. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17342. (defvar org-shiftmetaright-hook nil
  17343. "Hook for functions attaching themselves to `M-S-right'.
  17344. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17345. (defvar org-shiftmetaup-hook nil
  17346. "Hook for functions attaching themselves to `M-S-up'.
  17347. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17348. (defvar org-shiftmetadown-hook nil
  17349. "Hook for functions attaching themselves to `M-S-down'.
  17350. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17351. (defvar org-metareturn-hook nil
  17352. "Hook for functions attaching themselves to `M-RET'.
  17353. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17354. (defvar org-shiftup-hook nil
  17355. "Hook for functions attaching themselves to `S-up'.
  17356. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17357. (defvar org-shiftup-final-hook nil
  17358. "Hook for functions attaching themselves to `S-up'.
  17359. This one runs after all other options except shift-select have been excluded.
  17360. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17361. (defvar org-shiftdown-hook nil
  17362. "Hook for functions attaching themselves to `S-down'.
  17363. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17364. (defvar org-shiftdown-final-hook nil
  17365. "Hook for functions attaching themselves to `S-down'.
  17366. This one runs after all other options except shift-select have been excluded.
  17367. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17368. (defvar org-shiftleft-hook nil
  17369. "Hook for functions attaching themselves to `S-left'.
  17370. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17371. (defvar org-shiftleft-final-hook nil
  17372. "Hook for functions attaching themselves to `S-left'.
  17373. This one runs after all other options except shift-select have been excluded.
  17374. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17375. (defvar org-shiftright-hook nil
  17376. "Hook for functions attaching themselves to `S-right'.
  17377. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17378. (defvar org-shiftright-final-hook nil
  17379. "Hook for functions attaching themselves to `S-right'.
  17380. This one runs after all other options except shift-select have been excluded.
  17381. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17382. (defun org-modifier-cursor-error ()
  17383. "Throw an error, a modified cursor command was applied in wrong context."
  17384. (error "This command is active in special context like tables, headlines or items"))
  17385. (defun org-shiftselect-error ()
  17386. "Throw an error because Shift-Cursor command was applied in wrong context."
  17387. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17388. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17389. (error "This command works only in special context like headlines or timestamps")))
  17390. (defun org-call-for-shift-select (cmd)
  17391. (let ((this-command-keys-shift-translated t))
  17392. (call-interactively cmd)))
  17393. (defun org-shifttab (&optional arg)
  17394. "Global visibility cycling or move to previous table field.
  17395. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17396. on context.
  17397. See the individual commands for more information."
  17398. (interactive "P")
  17399. (cond
  17400. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17401. ((integerp arg)
  17402. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17403. (message "Content view to level: %d" arg)
  17404. (org-content (prefix-numeric-value arg2))
  17405. (setq org-cycle-global-status 'overview)))
  17406. (t (call-interactively 'org-global-cycle))))
  17407. (defun org-shiftmetaleft ()
  17408. "Promote subtree or delete table column.
  17409. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17410. `org-table-delete-column', depending on context. See the
  17411. individual commands for more information."
  17412. (interactive)
  17413. (cond
  17414. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17415. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17416. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17417. ((if (not (org-region-active-p)) (org-at-item-p)
  17418. (save-excursion (goto-char (region-beginning))
  17419. (org-at-item-p)))
  17420. (call-interactively 'org-outdent-item-tree))
  17421. (t (org-modifier-cursor-error))))
  17422. (defun org-shiftmetaright ()
  17423. "Demote subtree or insert table column.
  17424. Calls `org-demote-subtree', `org-indent-item-tree', or
  17425. `org-table-insert-column', depending on context. See the
  17426. individual commands for more information."
  17427. (interactive)
  17428. (cond
  17429. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17430. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17431. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17432. ((if (not (org-region-active-p)) (org-at-item-p)
  17433. (save-excursion (goto-char (region-beginning))
  17434. (org-at-item-p)))
  17435. (call-interactively 'org-indent-item-tree))
  17436. (t (org-modifier-cursor-error))))
  17437. (defun org-shiftmetaup (&optional arg)
  17438. "Move subtree up or kill table row.
  17439. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17440. `org-move-item-up' or `org-timestamp-up', depending on context.
  17441. See the individual commands for more information."
  17442. (interactive "P")
  17443. (cond
  17444. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17445. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17446. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17447. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17448. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17449. (call-interactively 'org-timestamp-up)))
  17450. (t (org-modifier-cursor-error))))
  17451. (defun org-shiftmetadown (&optional arg)
  17452. "Move subtree down or insert table row.
  17453. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17454. `org-move-item-down' or `org-timestamp-up', depending on context.
  17455. See the individual commands for more information."
  17456. (interactive "P")
  17457. (cond
  17458. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17459. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17460. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17461. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17462. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17463. (call-interactively 'org-timestamp-down)))
  17464. (t (org-modifier-cursor-error))))
  17465. (defsubst org-hidden-tree-error ()
  17466. (error
  17467. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17468. (defun org-metaleft (&optional arg)
  17469. "Promote heading or move table column to left.
  17470. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17471. With no specific context, calls the Emacs default `backward-word'.
  17472. See the individual commands for more information."
  17473. (interactive "P")
  17474. (cond
  17475. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17476. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17477. ((org-with-limited-levels
  17478. (or (org-at-heading-p)
  17479. (and (org-region-active-p)
  17480. (save-excursion
  17481. (goto-char (region-beginning))
  17482. (org-at-heading-p)))))
  17483. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17484. (call-interactively 'org-do-promote))
  17485. ;; At an inline task.
  17486. ((org-at-heading-p)
  17487. (call-interactively 'org-inlinetask-promote))
  17488. ((or (org-at-item-p)
  17489. (and (org-region-active-p)
  17490. (save-excursion
  17491. (goto-char (region-beginning))
  17492. (org-at-item-p))))
  17493. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17494. (call-interactively 'org-outdent-item))
  17495. (t (call-interactively 'backward-word))))
  17496. (defun org-metaright (&optional arg)
  17497. "Demote a subtree, a list item or move table column to right.
  17498. In front of a drawer or a block keyword, indent it correctly.
  17499. With no specific context, calls the Emacs default `forward-word'.
  17500. See the individual commands for more information."
  17501. (interactive "P")
  17502. (cond
  17503. ((run-hook-with-args-until-success 'org-metaright-hook))
  17504. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17505. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17506. ((org-at-block-p) (call-interactively 'org-indent-block))
  17507. ((org-with-limited-levels
  17508. (or (org-at-heading-p)
  17509. (and (org-region-active-p)
  17510. (save-excursion
  17511. (goto-char (region-beginning))
  17512. (org-at-heading-p)))))
  17513. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17514. (call-interactively 'org-do-demote))
  17515. ;; At an inline task.
  17516. ((org-at-heading-p)
  17517. (call-interactively 'org-inlinetask-demote))
  17518. ((or (org-at-item-p)
  17519. (and (org-region-active-p)
  17520. (save-excursion
  17521. (goto-char (region-beginning))
  17522. (org-at-item-p))))
  17523. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17524. (call-interactively 'org-indent-item))
  17525. (t (call-interactively 'forward-word))))
  17526. (defun org-check-for-hidden (what)
  17527. "Check if there are hidden headlines/items in the current visual line.
  17528. WHAT can be either `headlines' or `items'. If the current line is
  17529. an outline or item heading and it has a folded subtree below it,
  17530. this function returns t, nil otherwise."
  17531. (let ((re (cond
  17532. ((eq what 'headlines) org-outline-regexp-bol)
  17533. ((eq what 'items) (org-item-beginning-re))
  17534. (t (error "This should not happen"))))
  17535. beg end)
  17536. (save-excursion
  17537. (catch 'exit
  17538. (unless (org-region-active-p)
  17539. (setq beg (point-at-bol))
  17540. (beginning-of-line 2)
  17541. (while (and (not (eobp)) ;; this is like `next-line'
  17542. (get-char-property (1- (point)) 'invisible))
  17543. (beginning-of-line 2))
  17544. (setq end (point))
  17545. (goto-char beg)
  17546. (goto-char (point-at-eol))
  17547. (setq end (max end (point)))
  17548. (while (re-search-forward re end t)
  17549. (if (get-char-property (match-beginning 0) 'invisible)
  17550. (throw 'exit t))))
  17551. nil))))
  17552. (defun org-metaup (&optional arg)
  17553. "Move subtree up or move table row up.
  17554. Calls `org-move-subtree-up' or `org-table-move-row' or
  17555. `org-move-item-up', depending on context. See the individual commands
  17556. for more information."
  17557. (interactive "P")
  17558. (cond
  17559. ((run-hook-with-args-until-success 'org-metaup-hook))
  17560. ((org-region-active-p)
  17561. (let* ((a (min (region-beginning) (region-end)))
  17562. (b (1- (max (region-beginning) (region-end))))
  17563. (c (save-excursion (goto-char a)
  17564. (move-beginning-of-line 0)))
  17565. (d (save-excursion (goto-char a)
  17566. (move-end-of-line 0) (point))))
  17567. (transpose-regions a b c d)
  17568. (goto-char c)))
  17569. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17570. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17571. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17572. (t (org-drag-element-backward))))
  17573. (defun org-metadown (&optional arg)
  17574. "Move subtree down or move table row down.
  17575. Calls `org-move-subtree-down' or `org-table-move-row' or
  17576. `org-move-item-down', depending on context. See the individual
  17577. commands for more information."
  17578. (interactive "P")
  17579. (cond
  17580. ((run-hook-with-args-until-success 'org-metadown-hook))
  17581. ((org-region-active-p)
  17582. (let* ((a (min (region-beginning) (region-end)))
  17583. (b (max (region-beginning) (region-end)))
  17584. (c (save-excursion (goto-char b)
  17585. (move-beginning-of-line 1)))
  17586. (d (save-excursion (goto-char b)
  17587. (move-end-of-line 1) (1+ (point)))))
  17588. (transpose-regions a b c d)
  17589. (goto-char d)))
  17590. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17591. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17592. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17593. (t (org-drag-element-forward))))
  17594. (defun org-shiftup (&optional arg)
  17595. "Increase item in timestamp or increase priority of current headline.
  17596. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17597. depending on context. See the individual commands for more information."
  17598. (interactive "P")
  17599. (cond
  17600. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17601. ((and org-support-shift-select (org-region-active-p))
  17602. (org-call-for-shift-select 'previous-line))
  17603. ((org-at-timestamp-p t)
  17604. (call-interactively (if org-edit-timestamp-down-means-later
  17605. 'org-timestamp-down 'org-timestamp-up)))
  17606. ((and (not (eq org-support-shift-select 'always))
  17607. org-enable-priority-commands
  17608. (org-at-heading-p))
  17609. (call-interactively 'org-priority-up))
  17610. ((and (not org-support-shift-select) (org-at-item-p))
  17611. (call-interactively 'org-previous-item))
  17612. ((org-clocktable-try-shift 'up arg))
  17613. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17614. (org-support-shift-select
  17615. (org-call-for-shift-select 'previous-line))
  17616. (t (org-shiftselect-error))))
  17617. (defun org-shiftdown (&optional arg)
  17618. "Decrease item in timestamp or decrease priority of current headline.
  17619. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17620. depending on context. See the individual commands for more information."
  17621. (interactive "P")
  17622. (cond
  17623. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17624. ((and org-support-shift-select (org-region-active-p))
  17625. (org-call-for-shift-select 'next-line))
  17626. ((org-at-timestamp-p t)
  17627. (call-interactively (if org-edit-timestamp-down-means-later
  17628. 'org-timestamp-up 'org-timestamp-down)))
  17629. ((and (not (eq org-support-shift-select 'always))
  17630. org-enable-priority-commands
  17631. (org-at-heading-p))
  17632. (call-interactively 'org-priority-down))
  17633. ((and (not org-support-shift-select) (org-at-item-p))
  17634. (call-interactively 'org-next-item))
  17635. ((org-clocktable-try-shift 'down arg))
  17636. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17637. (org-support-shift-select
  17638. (org-call-for-shift-select 'next-line))
  17639. (t (org-shiftselect-error))))
  17640. (defun org-shiftright (&optional arg)
  17641. "Cycle the thing at point or in the current line, depending on context.
  17642. Depending on context, this does one of the following:
  17643. - switch a timestamp at point one day into the future
  17644. - on a headline, switch to the next TODO keyword.
  17645. - on an item, switch entire list to the next bullet type
  17646. - on a property line, switch to the next allowed value
  17647. - on a clocktable definition line, move time block into the future"
  17648. (interactive "P")
  17649. (cond
  17650. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17651. ((and org-support-shift-select (org-region-active-p))
  17652. (org-call-for-shift-select 'forward-char))
  17653. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17654. ((and (not (eq org-support-shift-select 'always))
  17655. (org-at-heading-p))
  17656. (let ((org-inhibit-logging
  17657. (not org-treat-S-cursor-todo-selection-as-state-change))
  17658. (org-inhibit-blocking
  17659. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17660. (org-call-with-arg 'org-todo 'right)))
  17661. ((or (and org-support-shift-select
  17662. (not (eq org-support-shift-select 'always))
  17663. (org-at-item-bullet-p))
  17664. (and (not org-support-shift-select) (org-at-item-p)))
  17665. (org-call-with-arg 'org-cycle-list-bullet nil))
  17666. ((and (not (eq org-support-shift-select 'always))
  17667. (org-at-property-p))
  17668. (call-interactively 'org-property-next-allowed-value))
  17669. ((org-clocktable-try-shift 'right arg))
  17670. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17671. (org-support-shift-select
  17672. (org-call-for-shift-select 'forward-char))
  17673. (t (org-shiftselect-error))))
  17674. (defun org-shiftleft (&optional arg)
  17675. "Cycle the thing at point or in the current line, depending on context.
  17676. Depending on context, this does one of the following:
  17677. - switch a timestamp at point one day into the past
  17678. - on a headline, switch to the previous TODO keyword.
  17679. - on an item, switch entire list to the previous bullet type
  17680. - on a property line, switch to the previous allowed value
  17681. - on a clocktable definition line, move time block into the past"
  17682. (interactive "P")
  17683. (cond
  17684. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17685. ((and org-support-shift-select (org-region-active-p))
  17686. (org-call-for-shift-select 'backward-char))
  17687. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17688. ((and (not (eq org-support-shift-select 'always))
  17689. (org-at-heading-p))
  17690. (let ((org-inhibit-logging
  17691. (not org-treat-S-cursor-todo-selection-as-state-change))
  17692. (org-inhibit-blocking
  17693. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17694. (org-call-with-arg 'org-todo 'left)))
  17695. ((or (and org-support-shift-select
  17696. (not (eq org-support-shift-select 'always))
  17697. (org-at-item-bullet-p))
  17698. (and (not org-support-shift-select) (org-at-item-p)))
  17699. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17700. ((and (not (eq org-support-shift-select 'always))
  17701. (org-at-property-p))
  17702. (call-interactively 'org-property-previous-allowed-value))
  17703. ((org-clocktable-try-shift 'left arg))
  17704. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17705. (org-support-shift-select
  17706. (org-call-for-shift-select 'backward-char))
  17707. (t (org-shiftselect-error))))
  17708. (defun org-shiftcontrolright ()
  17709. "Switch to next TODO set."
  17710. (interactive)
  17711. (cond
  17712. ((and org-support-shift-select (org-region-active-p))
  17713. (org-call-for-shift-select 'forward-word))
  17714. ((and (not (eq org-support-shift-select 'always))
  17715. (org-at-heading-p))
  17716. (org-call-with-arg 'org-todo 'nextset))
  17717. (org-support-shift-select
  17718. (org-call-for-shift-select 'forward-word))
  17719. (t (org-shiftselect-error))))
  17720. (defun org-shiftcontrolleft ()
  17721. "Switch to previous TODO set."
  17722. (interactive)
  17723. (cond
  17724. ((and org-support-shift-select (org-region-active-p))
  17725. (org-call-for-shift-select 'backward-word))
  17726. ((and (not (eq org-support-shift-select 'always))
  17727. (org-at-heading-p))
  17728. (org-call-with-arg 'org-todo 'previousset))
  17729. (org-support-shift-select
  17730. (org-call-for-shift-select 'backward-word))
  17731. (t (org-shiftselect-error))))
  17732. (defun org-shiftcontrolup (&optional n)
  17733. "Change timestamps synchronously up in CLOCK log lines.
  17734. Optional argument N tells to change by that many units."
  17735. (interactive "P")
  17736. (cond ((and (not org-support-shift-select)
  17737. (org-at-clock-log-p)
  17738. (org-at-timestamp-p t))
  17739. (org-clock-timestamps-up n))
  17740. (t (org-shiftselect-error))))
  17741. (defun org-shiftcontroldown (&optional n)
  17742. "Change timestamps synchronously down in CLOCK log lines.
  17743. Optional argument N tells to change by that many units."
  17744. (interactive "P")
  17745. (cond ((and (not org-support-shift-select)
  17746. (org-at-clock-log-p)
  17747. (org-at-timestamp-p t))
  17748. (org-clock-timestamps-down n))
  17749. (t (org-shiftselect-error))))
  17750. (defun org-ctrl-c-ret ()
  17751. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17752. (interactive)
  17753. (cond
  17754. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17755. (t (call-interactively 'org-insert-heading))))
  17756. (defun org-find-visible ()
  17757. (let ((s (point)))
  17758. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17759. (get-char-property s 'invisible)))
  17760. s))
  17761. (defun org-find-invisible ()
  17762. (let ((s (point)))
  17763. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17764. (not (get-char-property s 'invisible))))
  17765. s))
  17766. (defun org-copy-visible (beg end)
  17767. "Copy the visible parts of the region."
  17768. (interactive "r")
  17769. (let (snippets s)
  17770. (save-excursion
  17771. (save-restriction
  17772. (narrow-to-region beg end)
  17773. (setq s (goto-char (point-min)))
  17774. (while (not (= (point) (point-max)))
  17775. (goto-char (org-find-invisible))
  17776. (push (buffer-substring s (point)) snippets)
  17777. (setq s (goto-char (org-find-visible))))))
  17778. (kill-new (apply 'concat (nreverse snippets)))))
  17779. (defun org-copy-special ()
  17780. "Copy region in table or copy current subtree.
  17781. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17782. See the individual commands for more information."
  17783. (interactive)
  17784. (call-interactively
  17785. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17786. (defun org-cut-special ()
  17787. "Cut region in table or cut current subtree.
  17788. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17789. See the individual commands for more information."
  17790. (interactive)
  17791. (call-interactively
  17792. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17793. (defun org-paste-special (arg)
  17794. "Paste rectangular region into table, or past subtree relative to level.
  17795. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17796. See the individual commands for more information."
  17797. (interactive "P")
  17798. (if (org-at-table-p)
  17799. (org-table-paste-rectangle)
  17800. (org-paste-subtree arg)))
  17801. (defsubst org-in-fixed-width-region-p ()
  17802. "Is point in a fixed-width region?"
  17803. (save-match-data
  17804. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  17805. (defun org-edit-special (&optional arg)
  17806. "Call a special editor for the stuff at point.
  17807. When at a table, call the formula editor with `org-table-edit-formulas'.
  17808. When in a source code block, call `org-edit-src-code'.
  17809. When in a fixed-width region, call `org-edit-fixed-width-region'.
  17810. When in an #+include line, visit the included file.
  17811. On a link, call `ffap' to visit the link at point.
  17812. Otherwise, return a user error."
  17813. (interactive)
  17814. ;; possibly prep session before editing source
  17815. (when (and (org-in-src-block-p) arg)
  17816. (let* ((info (org-babel-get-src-block-info))
  17817. (lang (nth 0 info))
  17818. (params (nth 2 info))
  17819. (session (cdr (assoc :session params))))
  17820. (when (and info session) ;; we are in a source-code block with a session
  17821. (funcall
  17822. (intern (concat "org-babel-prep-session:" lang)) session params))))
  17823. (cond ;; proceed with `org-edit-special'
  17824. ((save-excursion
  17825. (beginning-of-line 1)
  17826. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  17827. (find-file (org-trim (match-string 1))))
  17828. ((or (org-at-table-p)
  17829. (save-excursion
  17830. (beginning-of-line 1)
  17831. (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
  17832. (call-interactively 'org-table-edit-formulas))
  17833. ((or (org-in-block-p '("src" "example" "latex" "html"))
  17834. (org-at-table.el-p))
  17835. (org-edit-src-code))
  17836. ((org-in-fixed-width-region-p) (org-edit-fixed-width-region))
  17837. ((org-at-regexp-p org-any-link-re) (call-interactively 'ffap))
  17838. (t (user-error "No special environment to edit here"))))
  17839. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17840. (defun org-ctrl-c-ctrl-c (&optional arg)
  17841. "Set tags in headline, or update according to changed information at point.
  17842. This command does many different things, depending on context:
  17843. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17844. this is what we do.
  17845. - If the cursor is on a statistics cookie, update it.
  17846. - If the cursor is in a headline, prompt for tags and insert them
  17847. into the current line, aligned to `org-tags-column'. When called
  17848. with prefix arg, realign all tags in the current buffer.
  17849. - If the cursor is in one of the special #+KEYWORD lines, this
  17850. triggers scanning the buffer for these lines and updating the
  17851. information.
  17852. - If the cursor is inside a table, realign the table. This command
  17853. works even if the automatic table editor has been turned off.
  17854. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17855. the entire table.
  17856. - If the cursor is at a footnote reference or definition, jump to
  17857. the corresponding definition or references, respectively.
  17858. - If the cursor is a the beginning of a dynamic block, update it.
  17859. - If the current buffer is a capture buffer, close note and file it.
  17860. - If the cursor is on a <<<target>>>, update radio targets and
  17861. corresponding links in this buffer.
  17862. - If the cursor is on a numbered item in a plain list, renumber the
  17863. ordered list.
  17864. - If the cursor is on a checkbox, toggle it.
  17865. - If the cursor is on a code block, evaluate it. The variable
  17866. `org-confirm-babel-evaluate' can be used to control prompting
  17867. before code block evaluation, by default every code block
  17868. evaluation requires confirmation. Code block evaluation can be
  17869. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17870. (interactive "P")
  17871. (let ((org-enable-table-editor t))
  17872. (cond
  17873. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17874. org-occur-highlights
  17875. org-latex-fragment-image-overlays)
  17876. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17877. (org-remove-occur-highlights)
  17878. (org-remove-latex-fragment-image-overlays)
  17879. (message "Temporary highlights/overlays removed from current buffer"))
  17880. ((and (local-variable-p 'org-finish-function (current-buffer))
  17881. (fboundp org-finish-function))
  17882. (funcall org-finish-function))
  17883. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17884. ((org-in-regexp org-ts-regexp-both)
  17885. (org-timestamp-change 0 'day))
  17886. ((or (looking-at org-property-start-re)
  17887. (org-at-property-p))
  17888. (call-interactively 'org-property-action))
  17889. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  17890. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  17891. (or (org-at-heading-p) (org-at-item-p)))
  17892. (call-interactively 'org-update-statistics-cookies))
  17893. ((org-at-heading-p) (call-interactively 'org-set-tags))
  17894. ((org-at-table.el-p)
  17895. (message "Use C-c ' to edit table.el tables"))
  17896. ((org-at-table-p)
  17897. (org-table-maybe-eval-formula)
  17898. (if arg
  17899. (call-interactively 'org-table-recalculate)
  17900. (org-table-maybe-recalculate-line))
  17901. (call-interactively 'org-table-align)
  17902. (orgtbl-send-table 'maybe))
  17903. ((or (org-footnote-at-reference-p)
  17904. (org-footnote-at-definition-p))
  17905. (call-interactively 'org-footnote-action))
  17906. ((org-at-item-checkbox-p)
  17907. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  17908. ;; list only if at top item.
  17909. (let* ((cbox (match-string 1))
  17910. (struct (org-list-struct))
  17911. (old-struct (copy-tree struct))
  17912. (parents (org-list-parents-alist struct))
  17913. (orderedp (org-entry-get nil "ORDERED"))
  17914. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17915. block-item)
  17916. ;; Use a light version of `org-toggle-checkbox' to avoid
  17917. ;; computing list structure twice.
  17918. (let ((new-box (cond
  17919. ((equal arg '(16)) "[-]")
  17920. ((equal arg '(4)) nil)
  17921. ((equal "[X]" cbox) "[ ]")
  17922. (t "[X]"))))
  17923. (if (and firstp arg)
  17924. ;; If at first item of sub-list, remove check-box from
  17925. ;; every item at the same level.
  17926. (mapc
  17927. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17928. (org-list-get-all-items
  17929. (point-at-bol) struct (org-list-prevs-alist struct)))
  17930. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17931. ;; Replicate `org-list-write-struct', while grabbing a return
  17932. ;; value from `org-list-struct-fix-box'.
  17933. (org-list-struct-fix-ind struct parents 2)
  17934. (org-list-struct-fix-item-end struct)
  17935. (let ((prevs (org-list-prevs-alist struct)))
  17936. (org-list-struct-fix-bul struct prevs)
  17937. (org-list-struct-fix-ind struct parents)
  17938. (setq block-item
  17939. (org-list-struct-fix-box struct parents prevs orderedp)))
  17940. (if (equal struct old-struct)
  17941. (user-error "Cannot toggle this checkbox (unchecked subitems?)")
  17942. (org-list-struct-apply-struct struct old-struct)
  17943. (org-update-checkbox-count-maybe))
  17944. (when block-item
  17945. (message
  17946. "Checkboxes were removed due to unchecked box at line %d"
  17947. (org-current-line block-item)))
  17948. (when firstp (org-list-send-list 'maybe))))
  17949. ((org-at-item-p)
  17950. ;; Cursor at an item: repair list. Do checkbox related actions
  17951. ;; only if function was called with an argument. Send list only
  17952. ;; if at top item.
  17953. (let* ((struct (org-list-struct))
  17954. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17955. old-struct)
  17956. (when arg
  17957. (setq old-struct (copy-tree struct))
  17958. (if firstp
  17959. ;; If at first item of sub-list, add check-box to every
  17960. ;; item at the same level.
  17961. (mapc
  17962. (lambda (pos)
  17963. (unless (org-list-get-checkbox pos struct)
  17964. (org-list-set-checkbox pos struct "[ ]")))
  17965. (org-list-get-all-items
  17966. (point-at-bol) struct (org-list-prevs-alist struct)))
  17967. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17968. (org-list-write-struct
  17969. struct (org-list-parents-alist struct) old-struct)
  17970. (when arg (org-update-checkbox-count-maybe))
  17971. (when firstp (org-list-send-list 'maybe))))
  17972. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17973. ;; Dynamic block
  17974. (beginning-of-line 1)
  17975. (save-excursion (org-update-dblock)))
  17976. ((save-excursion
  17977. (let ((case-fold-search t))
  17978. (beginning-of-line 1)
  17979. (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
  17980. (cond
  17981. ((or (equal (match-string 1) "TBLFM")
  17982. (equal (match-string 1) "tblfm"))
  17983. ;; Recalculate the table before this line
  17984. (save-excursion
  17985. (beginning-of-line 1)
  17986. (skip-chars-backward " \r\n\t")
  17987. (if (org-at-table-p)
  17988. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17989. (t
  17990. (let ((org-inhibit-startup-visibility-stuff t)
  17991. (org-startup-align-all-tables nil))
  17992. (when (boundp 'org-table-coordinate-overlays)
  17993. (mapc 'delete-overlay org-table-coordinate-overlays)
  17994. (setq org-table-coordinate-overlays nil))
  17995. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17996. (message "Local setup has been refreshed"))))
  17997. ((org-clock-update-time-maybe))
  17998. (t
  17999. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18000. (error "C-c C-c can do nothing useful at this location"))))))
  18001. (defun org-mode-restart ()
  18002. "Restart Org-mode, to scan again for special lines.
  18003. Also updates the keyword regular expressions."
  18004. (interactive)
  18005. (org-mode)
  18006. (message "Org-mode restarted"))
  18007. (defun org-kill-note-or-show-branches ()
  18008. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18009. (interactive)
  18010. (if (not org-finish-function)
  18011. (progn
  18012. (hide-subtree)
  18013. (call-interactively 'show-branches))
  18014. (let ((org-note-abort t))
  18015. (funcall org-finish-function))))
  18016. (defun org-return (&optional indent)
  18017. "Goto next table row or insert a newline.
  18018. Calls `org-table-next-row' or `newline', depending on context.
  18019. See the individual commands for more information."
  18020. (interactive)
  18021. (let (org-ts-what)
  18022. (cond
  18023. ((or (bobp) (org-in-src-block-p))
  18024. (if indent (newline-and-indent) (newline)))
  18025. ((org-at-table-p)
  18026. (org-table-justify-field-maybe)
  18027. (call-interactively 'org-table-next-row))
  18028. ;; when `newline-and-indent' is called within a list, make sure
  18029. ;; text moved stays inside the item.
  18030. ((and (org-in-item-p) indent)
  18031. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18032. (progn
  18033. (save-match-data (newline))
  18034. (org-indent-line-to (length (match-string 0))))
  18035. (let ((ind (org-get-indentation)))
  18036. (newline)
  18037. (if (org-looking-back org-list-end-re)
  18038. (org-indent-line)
  18039. (org-indent-line-to ind)))))
  18040. ((and org-return-follows-link
  18041. (org-at-timestamp-p t)
  18042. (not (eq org-ts-what 'after)))
  18043. (org-follow-timestamp-link))
  18044. ((and org-return-follows-link
  18045. (let ((tprop (get-text-property (point) 'face)))
  18046. (or (eq tprop 'org-link)
  18047. (and (listp tprop) (memq 'org-link tprop)))))
  18048. (call-interactively 'org-open-at-point))
  18049. ((and (org-at-heading-p)
  18050. (looking-at
  18051. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18052. (org-show-entry)
  18053. (end-of-line 1)
  18054. (newline))
  18055. (t (if indent (newline-and-indent) (newline))))))
  18056. (defun org-return-indent ()
  18057. "Goto next table row or insert a newline and indent.
  18058. Calls `org-table-next-row' or `newline-and-indent', depending on
  18059. context. See the individual commands for more information."
  18060. (interactive)
  18061. (org-return t))
  18062. (defun org-ctrl-c-star ()
  18063. "Compute table, or change heading status of lines.
  18064. Calls `org-table-recalculate' or `org-toggle-heading',
  18065. depending on context."
  18066. (interactive)
  18067. (cond
  18068. ((org-at-table-p)
  18069. (call-interactively 'org-table-recalculate))
  18070. (t
  18071. ;; Convert all lines in region to list items
  18072. (call-interactively 'org-toggle-heading))))
  18073. (defun org-ctrl-c-minus ()
  18074. "Insert separator line in table or modify bullet status of line.
  18075. Also turns a plain line or a region of lines into list items.
  18076. Calls `org-table-insert-hline', `org-toggle-item', or
  18077. `org-cycle-list-bullet', depending on context."
  18078. (interactive)
  18079. (cond
  18080. ((org-at-table-p)
  18081. (call-interactively 'org-table-insert-hline))
  18082. ((org-region-active-p)
  18083. (call-interactively 'org-toggle-item))
  18084. ((org-in-item-p)
  18085. (call-interactively 'org-cycle-list-bullet))
  18086. (t
  18087. (call-interactively 'org-toggle-item))))
  18088. (defun org-toggle-item (arg)
  18089. "Convert headings or normal lines to items, items to normal lines.
  18090. If there is no active region, only the current line is considered.
  18091. If the first non blank line in the region is an headline, convert
  18092. all headlines to items, shifting text accordingly.
  18093. If it is an item, convert all items to normal lines.
  18094. If it is normal text, change region into an item. With a prefix
  18095. argument ARG, change each line in region into an item."
  18096. (interactive "P")
  18097. (let ((shift-text
  18098. (function
  18099. ;; Shift text in current section to IND, from point to END.
  18100. ;; The function leaves point to END line.
  18101. (lambda (ind end)
  18102. (let ((min-i 1000) (end (copy-marker end)))
  18103. ;; First determine the minimum indentation (MIN-I) of
  18104. ;; the text.
  18105. (save-excursion
  18106. (catch 'exit
  18107. (while (< (point) end)
  18108. (let ((i (org-get-indentation)))
  18109. (cond
  18110. ;; Skip blank lines and inline tasks.
  18111. ((looking-at "^[ \t]*$"))
  18112. ((looking-at org-outline-regexp-bol))
  18113. ;; We can't find less than 0 indentation.
  18114. ((zerop i) (throw 'exit (setq min-i 0)))
  18115. ((< i min-i) (setq min-i i))))
  18116. (forward-line))))
  18117. ;; Then indent each line so that a line indented to
  18118. ;; MIN-I becomes indented to IND. Ignore blank lines
  18119. ;; and inline tasks in the process.
  18120. (let ((delta (- ind min-i)))
  18121. (while (< (point) end)
  18122. (unless (or (looking-at "^[ \t]*$")
  18123. (looking-at org-outline-regexp-bol))
  18124. (org-indent-line-to (+ (org-get-indentation) delta)))
  18125. (forward-line)))))))
  18126. (skip-blanks
  18127. (function
  18128. ;; Return beginning of first non-blank line, starting from
  18129. ;; line at POS.
  18130. (lambda (pos)
  18131. (save-excursion
  18132. (goto-char pos)
  18133. (skip-chars-forward " \r\t\n")
  18134. (point-at-bol)))))
  18135. beg end)
  18136. ;; Determine boundaries of changes.
  18137. (if (org-region-active-p)
  18138. (setq beg (funcall skip-blanks (region-beginning))
  18139. end (copy-marker (region-end)))
  18140. (setq beg (funcall skip-blanks (point-at-bol))
  18141. end (copy-marker (point-at-eol))))
  18142. ;; Depending on the starting line, choose an action on the text
  18143. ;; between BEG and END.
  18144. (org-with-limited-levels
  18145. (save-excursion
  18146. (goto-char beg)
  18147. (cond
  18148. ;; Case 1. Start at an item: de-itemize. Note that it only
  18149. ;; happens when a region is active: `org-ctrl-c-minus'
  18150. ;; would call `org-cycle-list-bullet' otherwise.
  18151. ((org-at-item-p)
  18152. (while (< (point) end)
  18153. (when (org-at-item-p)
  18154. (skip-chars-forward " \t")
  18155. (delete-region (point) (match-end 0)))
  18156. (forward-line)))
  18157. ;; Case 2. Start at an heading: convert to items.
  18158. ((org-at-heading-p)
  18159. (let* ((bul (org-list-bullet-string "-"))
  18160. (bul-len (length bul))
  18161. ;; Indentation of the first heading. It should be
  18162. ;; relative to the indentation of its parent, if any.
  18163. (start-ind (save-excursion
  18164. (cond
  18165. ((not org-adapt-indentation) 0)
  18166. ((not (outline-previous-heading)) 0)
  18167. (t (length (match-string 0))))))
  18168. ;; Level of first heading. Further headings will be
  18169. ;; compared to it to determine hierarchy in the list.
  18170. (ref-level (org-reduced-level (org-outline-level))))
  18171. (while (< (point) end)
  18172. (let* ((level (org-reduced-level (org-outline-level)))
  18173. (delta (max 0 (- level ref-level))))
  18174. ;; If current headline is less indented than the first
  18175. ;; one, set it as reference, in order to preserve
  18176. ;; subtrees.
  18177. (when (< level ref-level) (setq ref-level level))
  18178. (replace-match bul t t)
  18179. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18180. ;; Ensure all text down to END (or SECTION-END) belongs
  18181. ;; to the newly created item.
  18182. (let ((section-end (save-excursion
  18183. (or (outline-next-heading) (point)))))
  18184. (forward-line)
  18185. (funcall shift-text
  18186. (+ start-ind (* (1+ delta) bul-len))
  18187. (min end section-end)))))))
  18188. ;; Case 3. Normal line with ARG: turn each non-item line into
  18189. ;; an item.
  18190. (arg
  18191. (while (< (point) end)
  18192. (unless (or (org-at-heading-p) (org-at-item-p))
  18193. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18194. (replace-match
  18195. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18196. (forward-line)))
  18197. ;; Case 4. Normal line without ARG: make the first line of
  18198. ;; region an item, and shift indentation of others
  18199. ;; lines to set them as item's body.
  18200. (t (let* ((bul (org-list-bullet-string "-"))
  18201. (bul-len (length bul))
  18202. (ref-ind (org-get-indentation)))
  18203. (skip-chars-forward " \t")
  18204. (insert bul)
  18205. (forward-line)
  18206. (while (< (point) end)
  18207. ;; Ensure that lines less indented than first one
  18208. ;; still get included in item body.
  18209. (funcall shift-text
  18210. (+ ref-ind bul-len)
  18211. (min end (save-excursion (or (outline-next-heading)
  18212. (point)))))
  18213. (forward-line)))))))))
  18214. (defun org-toggle-heading (&optional nstars)
  18215. "Convert headings to normal text, or items or text to headings.
  18216. If there is no active region, only the current line is considered.
  18217. With a \\[universal-argument] prefix, convert the whole list at
  18218. point into heading.
  18219. In a region:
  18220. - If the first non blank line is an headline, remove the stars
  18221. from all headlines in the region.
  18222. - If it is a normal line turn each and every normal line (i.e. not an
  18223. heading or an item) in the region into a heading.
  18224. - If it is a plain list item, turn all plain list items into headings.
  18225. When converting a line into a heading, the number of stars is chosen
  18226. such that the lines become children of the current entry. However,
  18227. when a prefix argument is given, its value determines the number of
  18228. stars to add."
  18229. (interactive "P")
  18230. (let ((skip-blanks
  18231. (function
  18232. ;; Return beginning of first non-blank line, starting from
  18233. ;; line at POS.
  18234. (lambda (pos)
  18235. (save-excursion
  18236. (goto-char pos)
  18237. (while (org-at-comment-p) (forward-line))
  18238. (skip-chars-forward " \r\t\n")
  18239. (point-at-bol)))))
  18240. beg end toggled)
  18241. ;; Determine boundaries of changes. If a universal prefix has
  18242. ;; been given, put the list in a region. If region ends at a bol,
  18243. ;; do not consider the last line to be in the region.
  18244. (when (and current-prefix-arg (org-at-item-p))
  18245. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  18246. (org-mark-element))
  18247. (if (org-region-active-p)
  18248. (setq beg (funcall skip-blanks (region-beginning))
  18249. end (copy-marker (save-excursion
  18250. (goto-char (region-end))
  18251. (if (bolp) (point) (point-at-eol)))))
  18252. (setq beg (funcall skip-blanks (point-at-bol))
  18253. end (copy-marker (point-at-eol))))
  18254. ;; Ensure inline tasks don't count as headings.
  18255. (org-with-limited-levels
  18256. (save-excursion
  18257. (goto-char beg)
  18258. (cond
  18259. ;; Case 1. Started at an heading: de-star headings.
  18260. ((org-at-heading-p)
  18261. (while (< (point) end)
  18262. (when (org-at-heading-p t)
  18263. (looking-at org-outline-regexp) (replace-match "")
  18264. (setq toggled t))
  18265. (forward-line)))
  18266. ;; Case 2. Started at an item: change items into headlines.
  18267. ;; One star will be added by `org-list-to-subtree'.
  18268. ((org-at-item-p)
  18269. (let* ((stars (make-string
  18270. (if nstars
  18271. ;; subtract the star that will be added again by
  18272. ;; `org-list-to-subtree'
  18273. (1- (prefix-numeric-value current-prefix-arg))
  18274. (or (org-current-level) 0))
  18275. ?*))
  18276. (add-stars
  18277. (cond (nstars "") ; stars from prefix only
  18278. ((equal stars "") "") ; before first heading
  18279. (org-odd-levels-only "*") ; inside heading, odd
  18280. (t "")))) ; inside heading, oddeven
  18281. (while (< (point) end)
  18282. (when (org-at-item-p)
  18283. ;; Pay attention to cases when region ends before list.
  18284. (let* ((struct (org-list-struct))
  18285. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18286. (save-restriction
  18287. (narrow-to-region (point) list-end)
  18288. (insert
  18289. (org-list-to-subtree
  18290. (org-list-parse-list t)
  18291. '(:istart (concat stars add-stars (funcall get-stars depth))
  18292. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18293. (setq toggled t))
  18294. (forward-line))))
  18295. ;; Case 3. Started at normal text: make every line an heading,
  18296. ;; skipping headlines and items.
  18297. (t (let* ((stars (make-string
  18298. (if nstars
  18299. (prefix-numeric-value current-prefix-arg)
  18300. (or (org-current-level) 0))
  18301. ?*))
  18302. (add-stars
  18303. (cond (nstars "") ; stars from prefix only
  18304. ((equal stars "") "*") ; before first heading
  18305. (org-odd-levels-only "**") ; inside heading, odd
  18306. (t "*"))) ; inside heading, oddeven
  18307. (rpl (concat stars add-stars " ")))
  18308. (while (< (point) end)
  18309. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18310. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18311. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18312. (forward-line)))))))
  18313. (unless toggled (message "Cannot toggle heading from here"))))
  18314. (defun org-meta-return (&optional arg)
  18315. "Insert a new heading or wrap a region in a table.
  18316. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18317. See the individual commands for more information."
  18318. (interactive "P")
  18319. (cond
  18320. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18321. ((or (org-at-drawer-p) (org-at-property-p))
  18322. (newline-and-indent))
  18323. ((org-at-table-p)
  18324. (call-interactively 'org-table-wrap-region))
  18325. (t (call-interactively 'org-insert-heading))))
  18326. ;;; Menu entries
  18327. (defsubst org-in-subtree-not-table-p ()
  18328. "Are we in a subtree and not in a table?"
  18329. (and (not (org-before-first-heading-p))
  18330. (not (org-at-table-p))))
  18331. ;; Define the Org-mode menus
  18332. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18333. '("Tbl"
  18334. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18335. ["Next Field" org-cycle (org-at-table-p)]
  18336. ["Previous Field" org-shifttab (org-at-table-p)]
  18337. ["Next Row" org-return (org-at-table-p)]
  18338. "--"
  18339. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18340. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18341. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18342. "--"
  18343. ("Column"
  18344. ["Move Column Left" org-metaleft (org-at-table-p)]
  18345. ["Move Column Right" org-metaright (org-at-table-p)]
  18346. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18347. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18348. ("Row"
  18349. ["Move Row Up" org-metaup (org-at-table-p)]
  18350. ["Move Row Down" org-metadown (org-at-table-p)]
  18351. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18352. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18353. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18354. "--"
  18355. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18356. ("Rectangle"
  18357. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18358. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18359. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18360. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18361. "--"
  18362. ("Calculate"
  18363. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18364. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18365. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18366. "--"
  18367. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18368. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18369. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18370. "--"
  18371. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18372. "--"
  18373. ["Sum Column/Rectangle" org-table-sum
  18374. (or (org-at-table-p) (org-region-active-p))]
  18375. ["Which Column?" org-table-current-column (org-at-table-p)])
  18376. ["Debug Formulas"
  18377. org-table-toggle-formula-debugger
  18378. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18379. ["Show Col/Row Numbers"
  18380. org-table-toggle-coordinate-overlays
  18381. :style toggle
  18382. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18383. "--"
  18384. ["Create" org-table-create (and (not (org-at-table-p))
  18385. org-enable-table-editor)]
  18386. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18387. ["Import from File" org-table-import (not (org-at-table-p))]
  18388. ["Export to File" org-table-export (org-at-table-p)]
  18389. "--"
  18390. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18391. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18392. '("Org"
  18393. ("Show/Hide"
  18394. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18395. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18396. ["Sparse Tree..." org-sparse-tree t]
  18397. ["Reveal Context" org-reveal t]
  18398. ["Show All" show-all t]
  18399. "--"
  18400. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18401. "--"
  18402. ["New Heading" org-insert-heading t]
  18403. ("Navigate Headings"
  18404. ["Up" outline-up-heading t]
  18405. ["Next" outline-next-visible-heading t]
  18406. ["Previous" outline-previous-visible-heading t]
  18407. ["Next Same Level" outline-forward-same-level t]
  18408. ["Previous Same Level" outline-backward-same-level t]
  18409. "--"
  18410. ["Jump" org-goto t])
  18411. ("Edit Structure"
  18412. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18413. "--"
  18414. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18415. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18416. "--"
  18417. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18418. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18419. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18420. "--"
  18421. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18422. "--"
  18423. ["Copy visible text" org-copy-visible t]
  18424. "--"
  18425. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18426. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18427. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18428. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18429. "--"
  18430. ["Sort Region/Children" org-sort t]
  18431. "--"
  18432. ["Convert to odd levels" org-convert-to-odd-levels t]
  18433. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18434. ("Editing"
  18435. ["Emphasis..." org-emphasize t]
  18436. ["Edit Source Example" org-edit-special t]
  18437. "--"
  18438. ["Footnote new/jump" org-footnote-action t]
  18439. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18440. ("Archive"
  18441. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18442. "--"
  18443. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18444. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18445. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18446. )
  18447. "--"
  18448. ("Hyperlinks"
  18449. ["Store Link (Global)" org-store-link t]
  18450. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18451. ["Insert Link" org-insert-link t]
  18452. ["Follow Link" org-open-at-point t]
  18453. "--"
  18454. ["Next link" org-next-link t]
  18455. ["Previous link" org-previous-link t]
  18456. "--"
  18457. ["Descriptive Links"
  18458. org-toggle-link-display
  18459. :style radio
  18460. :selected org-descriptive-links
  18461. ]
  18462. ["Literal Links"
  18463. org-toggle-link-display
  18464. :style radio
  18465. :selected (not org-descriptive-links)])
  18466. "--"
  18467. ("TODO Lists"
  18468. ["TODO/DONE/-" org-todo t]
  18469. ("Select keyword"
  18470. ["Next keyword" org-shiftright (org-at-heading-p)]
  18471. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18472. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18473. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18474. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18475. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18476. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18477. "--"
  18478. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18479. :selected org-enforce-todo-dependencies :style toggle :active t]
  18480. "Settings for tree at point"
  18481. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18482. :selected (org-entry-get nil "ORDERED")
  18483. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18484. ["Do Children parallel" org-toggle-ordered-property :style radio
  18485. :selected (not (org-entry-get nil "ORDERED"))
  18486. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18487. "--"
  18488. ["Set Priority" org-priority t]
  18489. ["Priority Up" org-shiftup t]
  18490. ["Priority Down" org-shiftdown t]
  18491. "--"
  18492. ["Get news from all feeds" org-feed-update-all t]
  18493. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18494. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18495. ("TAGS and Properties"
  18496. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18497. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18498. "--"
  18499. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18500. ["Column view of properties" org-columns t]
  18501. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18502. ("Dates and Scheduling"
  18503. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18504. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18505. ("Change Date"
  18506. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18507. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18508. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18509. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18510. ["Compute Time Range" org-evaluate-time-range t]
  18511. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18512. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18513. "--"
  18514. ["Custom time format" org-toggle-time-stamp-overlays
  18515. :style radio :selected org-display-custom-times]
  18516. "--"
  18517. ["Goto Calendar" org-goto-calendar t]
  18518. ["Date from Calendar" org-date-from-calendar t]
  18519. "--"
  18520. ["Start/Restart Timer" org-timer-start t]
  18521. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18522. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18523. ["Insert Timer String" org-timer t]
  18524. ["Insert Timer Item" org-timer-item t])
  18525. ("Logging work"
  18526. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18527. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18528. ["Clock out" org-clock-out t]
  18529. ["Clock cancel" org-clock-cancel t]
  18530. "--"
  18531. ["Mark as default task" org-clock-mark-default-task t]
  18532. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18533. ["Goto running clock" org-clock-goto t]
  18534. "--"
  18535. ["Display times" org-clock-display t]
  18536. ["Create clock table" org-clock-report t]
  18537. "--"
  18538. ["Record DONE time"
  18539. (progn (setq org-log-done (not org-log-done))
  18540. (message "Switching to %s will %s record a timestamp"
  18541. (car org-done-keywords)
  18542. (if org-log-done "automatically" "not")))
  18543. :style toggle :selected org-log-done])
  18544. "--"
  18545. ["Agenda Command..." org-agenda t]
  18546. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18547. ("File List for Agenda")
  18548. ("Special views current file"
  18549. ["TODO Tree" org-show-todo-tree t]
  18550. ["Check Deadlines" org-check-deadlines t]
  18551. ["Timeline" org-timeline t]
  18552. ["Tags/Property tree" org-match-sparse-tree t])
  18553. "--"
  18554. ["Export/Publish..." org-export t]
  18555. ("LaTeX"
  18556. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18557. :selected org-cdlatex-mode]
  18558. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18559. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18560. ["Modify math symbol" org-cdlatex-math-modify
  18561. (org-inside-LaTeX-fragment-p)]
  18562. ["Insert citation" org-reftex-citation t]
  18563. "--"
  18564. ["Template for BEAMER" (progn (require 'org-beamer)
  18565. (org-insert-beamer-options-template)) t])
  18566. "--"
  18567. ("MobileOrg"
  18568. ["Push Files and Views" org-mobile-push t]
  18569. ["Get Captured and Flagged" org-mobile-pull t]
  18570. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18571. "--"
  18572. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18573. "--"
  18574. ("Documentation"
  18575. ["Show Version" org-version t]
  18576. ["Info Documentation" org-info t])
  18577. ("Customize"
  18578. ["Browse Org Group" org-customize t]
  18579. "--"
  18580. ["Expand This Menu" org-create-customize-menu
  18581. (fboundp 'customize-menu-create)])
  18582. ["Send bug report" org-submit-bug-report t]
  18583. "--"
  18584. ("Refresh/Reload"
  18585. ["Refresh setup current buffer" org-mode-restart t]
  18586. ["Reload Org (after update)" org-reload t]
  18587. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  18588. ))
  18589. (defun org-info (&optional node)
  18590. "Read documentation for Org-mode in the info system.
  18591. With optional NODE, go directly to that node."
  18592. (interactive)
  18593. (info (format "(org)%s" (or node ""))))
  18594. ;;;###autoload
  18595. (defun org-submit-bug-report ()
  18596. "Submit a bug report on Org-mode via mail.
  18597. Don't hesitate to report any problems or inaccurate documentation.
  18598. If you don't have setup sending mail from (X)Emacs, please copy the
  18599. output buffer into your mail program, as it gives us important
  18600. information about your Org-mode version and configuration."
  18601. (interactive)
  18602. (require 'reporter)
  18603. (org-load-modules-maybe)
  18604. (org-require-autoloaded-modules)
  18605. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18606. (reporter-submit-bug-report
  18607. "emacs-orgmode@gnu.org"
  18608. (org-version nil 'full)
  18609. (let (list)
  18610. (save-window-excursion
  18611. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18612. (delete-other-windows)
  18613. (erase-buffer)
  18614. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18615. We would like to add your full Org-mode and Outline configuration to the
  18616. bug report. This greatly simplifies the work of the maintainer and
  18617. other experts on the mailing list.
  18618. HOWEVER, some variables you have customized may contain private
  18619. information. The names of customers, colleagues, or friends, might
  18620. appear in the form of file names, tags, todo states, or search strings.
  18621. If you answer yes to the prompt, you might want to check and remove
  18622. such private information before sending the email.")
  18623. (add-text-properties (point-min) (point-max) '(face org-warning))
  18624. (when (yes-or-no-p "Include your Org-mode configuration ")
  18625. (mapatoms
  18626. (lambda (v)
  18627. (and (boundp v)
  18628. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18629. (or (and (symbol-value v)
  18630. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18631. (and
  18632. (get v 'custom-type) (get v 'standard-value)
  18633. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18634. (push v list)))))
  18635. (kill-buffer (get-buffer "*Warn about privacy*"))
  18636. list))
  18637. nil nil
  18638. "Remember to cover the basics, that is, what you expected to happen and
  18639. what in fact did happen. You don't know how to make a good report? See
  18640. http://orgmode.org/manual/Feedback.html#Feedback
  18641. Your bug report will be posted to the Org-mode mailing list.
  18642. ------------------------------------------------------------------------")
  18643. (save-excursion
  18644. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18645. (replace-match "\\1Bug: \\3 [\\2]")))))
  18646. (defun org-install-agenda-files-menu ()
  18647. (let ((bl (buffer-list)))
  18648. (save-excursion
  18649. (while bl
  18650. (set-buffer (pop bl))
  18651. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18652. (when (derived-mode-p 'org-mode)
  18653. (easy-menu-change
  18654. '("Org") "File List for Agenda"
  18655. (append
  18656. (list
  18657. ["Edit File List" (org-edit-agenda-file-list) t]
  18658. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18659. ["Remove Current File from List" org-remove-file t]
  18660. ["Cycle through agenda files" org-cycle-agenda-files t]
  18661. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18662. "--")
  18663. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18664. ;;;; Documentation
  18665. (defun org-require-autoloaded-modules ()
  18666. (interactive)
  18667. (mapc 'require
  18668. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  18669. org-docbook org-exp org-html org-icalendar
  18670. org-id org-latex
  18671. org-publish org-remember org-table
  18672. org-timer org-xoxo)))
  18673. ;;;###autoload
  18674. (defun org-reload (&optional uncompiled)
  18675. "Reload all org lisp files.
  18676. With prefix arg UNCOMPILED, load the uncompiled versions."
  18677. (interactive "P")
  18678. (require 'loadhist)
  18679. (let* ((org-dir (org-find-library-dir "org"))
  18680. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  18681. (feature-re "^\\(org\\|ob\\)\\(-.*\\)?")
  18682. (remove-re (mapconcat 'identity
  18683. (mapcar (lambda (f) (concat "^" f "$"))
  18684. (list (if (featurep 'xemacs)
  18685. "org-colview"
  18686. "org-colview-xemacs")
  18687. "org" "org-loaddefs" "org-version"))
  18688. "\\|"))
  18689. (feats (delete-dups
  18690. (mapcar 'file-name-sans-extension
  18691. (mapcar 'file-name-nondirectory
  18692. (delq nil
  18693. (mapcar 'feature-file
  18694. features))))))
  18695. (lfeat (append
  18696. (sort
  18697. (setq feats
  18698. (delq nil (mapcar
  18699. (lambda (f)
  18700. (if (and (string-match feature-re f)
  18701. (not (string-match remove-re f)))
  18702. f nil))
  18703. feats)))
  18704. 'string-lessp)
  18705. (list "org-version" "org")))
  18706. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  18707. load-uncore load-misses)
  18708. (setq load-misses
  18709. (delq 't
  18710. (mapcar (lambda (f)
  18711. (or (load (concat org-dir f) 'noerror nil nil 'mustsuffix)
  18712. (and (string= org-dir contrib-dir)
  18713. (load (concat contrib-dir f) 'noerror nil nil 'mustsuffix))
  18714. (and (load (concat (org-find-library-dir f) f) 'noerror nil nil 'mustsuffix)
  18715. (add-to-list 'load-uncore f 'append)
  18716. 't)
  18717. f))
  18718. lfeat)))
  18719. (if load-uncore
  18720. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  18721. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  18722. (if load-misses
  18723. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  18724. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  18725. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  18726. ;;;###autoload
  18727. (defun org-customize ()
  18728. "Call the customize function with org as argument."
  18729. (interactive)
  18730. (org-load-modules-maybe)
  18731. (org-require-autoloaded-modules)
  18732. (customize-browse 'org))
  18733. (defun org-create-customize-menu ()
  18734. "Create a full customization menu for Org-mode, insert it into the menu."
  18735. (interactive)
  18736. (org-load-modules-maybe)
  18737. (org-require-autoloaded-modules)
  18738. (if (fboundp 'customize-menu-create)
  18739. (progn
  18740. (easy-menu-change
  18741. '("Org") "Customize"
  18742. `(["Browse Org group" org-customize t]
  18743. "--"
  18744. ,(customize-menu-create 'org)
  18745. ["Set" Custom-set t]
  18746. ["Save" Custom-save t]
  18747. ["Reset to Current" Custom-reset-current t]
  18748. ["Reset to Saved" Custom-reset-saved t]
  18749. ["Reset to Standard Settings" Custom-reset-standard t]))
  18750. (message "\"Org\"-menu now contains full customization menu"))
  18751. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18752. ;;;; Miscellaneous stuff
  18753. ;;; Generally useful functions
  18754. (defun org-get-at-bol (property)
  18755. "Get text property PROPERTY at beginning of line."
  18756. (get-text-property (point-at-bol) property))
  18757. (defun org-find-text-property-in-string (prop s)
  18758. "Return the first non-nil value of property PROP in string S."
  18759. (or (get-text-property 0 prop s)
  18760. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18761. prop s)))
  18762. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18763. "Display the given MESSAGE as a warning."
  18764. (if (fboundp 'display-warning)
  18765. (display-warning 'org message
  18766. (if (featurep 'xemacs) 'warning :warning))
  18767. (let ((buf (get-buffer-create "*Org warnings*")))
  18768. (with-current-buffer buf
  18769. (goto-char (point-max))
  18770. (insert "Warning (Org): " message)
  18771. (unless (bolp)
  18772. (newline)))
  18773. (display-buffer buf)
  18774. (sit-for 0))))
  18775. (defun org-eval (form)
  18776. "Eval FORM and return result."
  18777. (condition-case error
  18778. (eval form)
  18779. (error (format "%%![Error: %s]" error))))
  18780. (defun org-in-clocktable-p ()
  18781. "Check if the cursor is in a clocktable."
  18782. (let ((pos (point)) start)
  18783. (save-excursion
  18784. (end-of-line 1)
  18785. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18786. (setq start (match-beginning 0))
  18787. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18788. (>= (match-end 0) pos)
  18789. start))))
  18790. (defun org-in-commented-line ()
  18791. "Is point in a line starting with `#'?"
  18792. (equal (char-after (point-at-bol)) ?#))
  18793. (defun org-in-indented-comment-line ()
  18794. "Is point in a line starting with `#' after some white space?"
  18795. (save-excursion
  18796. (save-match-data
  18797. (goto-char (point-at-bol))
  18798. (looking-at "[ \t]*#"))))
  18799. (defun org-in-verbatim-emphasis ()
  18800. (save-match-data
  18801. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18802. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18803. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18804. (if (and marker (marker-buffer marker)
  18805. (buffer-live-p (marker-buffer marker)))
  18806. (progn
  18807. (org-pop-to-buffer-same-window (marker-buffer marker))
  18808. (if (or (> marker (point-max)) (< marker (point-min)))
  18809. (widen))
  18810. (goto-char marker)
  18811. (org-show-context 'org-goto))
  18812. (if bookmark
  18813. (bookmark-jump bookmark)
  18814. (error "Cannot find location"))))
  18815. (defun org-quote-csv-field (s)
  18816. "Quote field for inclusion in CSV material."
  18817. (if (string-match "[\",]" s)
  18818. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18819. s))
  18820. (defun org-force-self-insert (N)
  18821. "Needed to enforce self-insert under remapping."
  18822. (interactive "p")
  18823. (self-insert-command N))
  18824. (defun org-string-width (s)
  18825. "Compute width of string, ignoring invisible characters.
  18826. This ignores character with invisibility property `org-link', and also
  18827. characters with property `org-cwidth', because these will become invisible
  18828. upon the next fontification round."
  18829. (let (b l)
  18830. (when (or (eq t buffer-invisibility-spec)
  18831. (assq 'org-link buffer-invisibility-spec))
  18832. (while (setq b (text-property-any 0 (length s)
  18833. 'invisible 'org-link s))
  18834. (setq s (concat (substring s 0 b)
  18835. (substring s (or (next-single-property-change
  18836. b 'invisible s) (length s)))))))
  18837. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18838. (setq s (concat (substring s 0 b)
  18839. (substring s (or (next-single-property-change
  18840. b 'org-cwidth s) (length s))))))
  18841. (setq l (string-width s) b -1)
  18842. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18843. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18844. l))
  18845. (defun org-shorten-string (s maxlength)
  18846. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18847. If the string is shorter or has length MAXLENGTH, just return the
  18848. original string. If it is longer, the functions finds a space in the
  18849. string, breaks this string off at that locations and adds three dots
  18850. as ellipsis. Including the ellipsis, the string will not be longer
  18851. than MAXLENGTH. If finding a good breaking point in the string does
  18852. not work, the string is just chopped off in the middle of a word
  18853. if necessary."
  18854. (if (<= (length s) maxlength)
  18855. s
  18856. (let* ((n (max (- maxlength 4) 1))
  18857. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18858. (if (string-match re s)
  18859. (concat (match-string 1 s) "...")
  18860. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18861. (defun org-get-indentation (&optional line)
  18862. "Get the indentation of the current line, interpreting tabs.
  18863. When LINE is given, assume it represents a line and compute its indentation."
  18864. (if line
  18865. (if (string-match "^ *" (org-remove-tabs line))
  18866. (match-end 0))
  18867. (save-excursion
  18868. (beginning-of-line 1)
  18869. (skip-chars-forward " \t")
  18870. (current-column))))
  18871. (defun org-get-string-indentation (s)
  18872. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18873. (let ((n -1) (i 0) (w tab-width) c)
  18874. (catch 'exit
  18875. (while (< (setq n (1+ n)) (length s))
  18876. (setq c (aref s n))
  18877. (cond ((= c ?\ ) (setq i (1+ i)))
  18878. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18879. (t (throw 'exit t)))))
  18880. i))
  18881. (defun org-remove-tabs (s &optional width)
  18882. "Replace tabulators in S with spaces.
  18883. Assumes that s is a single line, starting in column 0."
  18884. (setq width (or width tab-width))
  18885. (while (string-match "\t" s)
  18886. (setq s (replace-match
  18887. (make-string
  18888. (- (* width (/ (+ (match-beginning 0) width) width))
  18889. (match-beginning 0)) ?\ )
  18890. t t s)))
  18891. s)
  18892. (defun org-fix-indentation (line ind)
  18893. "Fix indentation in LINE.
  18894. IND is a cons cell with target and minimum indentation.
  18895. If the current indentation in LINE is smaller than the minimum,
  18896. leave it alone. If it is larger than ind, set it to the target."
  18897. (let* ((l (org-remove-tabs line))
  18898. (i (org-get-indentation l))
  18899. (i1 (car ind)) (i2 (cdr ind)))
  18900. (if (>= i i2) (setq l (substring line i2)))
  18901. (if (> i1 0)
  18902. (concat (make-string i1 ?\ ) l)
  18903. l)))
  18904. (defun org-remove-indentation (code &optional n)
  18905. "Remove the maximum common indentation from the lines in CODE.
  18906. N may optionally be the number of spaces to remove."
  18907. (with-temp-buffer
  18908. (insert code)
  18909. (org-do-remove-indentation n)
  18910. (buffer-string)))
  18911. (defun org-do-remove-indentation (&optional n)
  18912. "Remove the maximum common indentation from the buffer."
  18913. (untabify (point-min) (point-max))
  18914. (let ((min 10000) re)
  18915. (if n
  18916. (setq min n)
  18917. (goto-char (point-min))
  18918. (while (re-search-forward "^ *[^ \n]" nil t)
  18919. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  18920. (unless (or (= min 0) (= min 10000))
  18921. (setq re (format "^ \\{%d\\}" min))
  18922. (goto-char (point-min))
  18923. (while (re-search-forward re nil t)
  18924. (replace-match "")
  18925. (end-of-line 1))
  18926. min)))
  18927. (defun org-fill-template (template alist)
  18928. "Find each %key of ALIST in TEMPLATE and replace it."
  18929. (let ((case-fold-search nil)
  18930. entry key value)
  18931. (setq alist (sort (copy-sequence alist)
  18932. (lambda (a b) (< (length (car a)) (length (car b))))))
  18933. (while (setq entry (pop alist))
  18934. (setq template
  18935. (replace-regexp-in-string
  18936. (concat "%" (regexp-quote (car entry)))
  18937. (or (cdr entry) "") template t t)))
  18938. template))
  18939. (defun org-base-buffer (buffer)
  18940. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  18941. (if (not buffer)
  18942. buffer
  18943. (or (buffer-base-buffer buffer)
  18944. buffer)))
  18945. (defun org-trim (s)
  18946. "Remove whitespace at beginning and end of string."
  18947. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  18948. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  18949. s)
  18950. (defun org-wrap (string &optional width lines)
  18951. "Wrap string to either a number of lines, or a width in characters.
  18952. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18953. that costs. If there is a word longer than WIDTH, the text is actually
  18954. wrapped to the length of that word.
  18955. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18956. many lines, whatever width that takes.
  18957. The return value is a list of lines, without newlines at the end."
  18958. (let* ((words (org-split-string string "[ \t\n]+"))
  18959. (maxword (apply 'max (mapcar 'org-string-width words)))
  18960. w ll)
  18961. (cond (width
  18962. (org-do-wrap words (max maxword width)))
  18963. (lines
  18964. (setq w maxword)
  18965. (setq ll (org-do-wrap words maxword))
  18966. (if (<= (length ll) lines)
  18967. ll
  18968. (setq ll words)
  18969. (while (> (length ll) lines)
  18970. (setq w (1+ w))
  18971. (setq ll (org-do-wrap words w)))
  18972. ll))
  18973. (t (error "Cannot wrap this")))))
  18974. (defun org-do-wrap (words width)
  18975. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18976. (let (lines line)
  18977. (while words
  18978. (setq line (pop words))
  18979. (while (and words (< (+ (length line) (length (car words))) width))
  18980. (setq line (concat line " " (pop words))))
  18981. (setq lines (push line lines)))
  18982. (nreverse lines)))
  18983. (defun org-split-string (string &optional separators)
  18984. "Splits STRING into substrings at SEPARATORS.
  18985. No empty strings are returned if there are matches at the beginning
  18986. and end of string."
  18987. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18988. (start 0)
  18989. notfirst
  18990. (list nil))
  18991. (while (and (string-match rexp string
  18992. (if (and notfirst
  18993. (= start (match-beginning 0))
  18994. (< start (length string)))
  18995. (1+ start) start))
  18996. (< (match-beginning 0) (length string)))
  18997. (setq notfirst t)
  18998. (or (eq (match-beginning 0) 0)
  18999. (and (eq (match-beginning 0) (match-end 0))
  19000. (eq (match-beginning 0) start))
  19001. (setq list
  19002. (cons (substring string start (match-beginning 0))
  19003. list)))
  19004. (setq start (match-end 0)))
  19005. (or (eq start (length string))
  19006. (setq list
  19007. (cons (substring string start)
  19008. list)))
  19009. (nreverse list)))
  19010. (defun org-quote-vert (s)
  19011. "Replace \"|\" with \"\\vert\"."
  19012. (while (string-match "|" s)
  19013. (setq s (replace-match "\\vert" t t s)))
  19014. s)
  19015. (defun org-uuidgen-p (s)
  19016. "Is S an ID created by UUIDGEN?"
  19017. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19018. (defun org-in-src-block-p (&optional inside)
  19019. "Whether point is in a code source block.
  19020. When INSIDE is non-nil, don't consider we are within a src block
  19021. when point is at #+BEGIN_SRC or #+END_SRC."
  19022. (let ((case-fold-search t) ov)
  19023. (or (and (setq ov (overlays-at (point)))
  19024. (memq 'org-block-background
  19025. (overlay-properties (car ov))))
  19026. (and (not inside)
  19027. (save-match-data
  19028. (save-excursion
  19029. (beginning-of-line)
  19030. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19031. (defun org-context ()
  19032. "Return a list of contexts of the current cursor position.
  19033. If several contexts apply, all are returned.
  19034. Each context entry is a list with a symbol naming the context, and
  19035. two positions indicating start and end of the context. Possible
  19036. contexts are:
  19037. :headline anywhere in a headline
  19038. :headline-stars on the leading stars in a headline
  19039. :todo-keyword on a TODO keyword (including DONE) in a headline
  19040. :tags on the TAGS in a headline
  19041. :priority on the priority cookie in a headline
  19042. :item on the first line of a plain list item
  19043. :item-bullet on the bullet/number of a plain list item
  19044. :checkbox on the checkbox in a plain list item
  19045. :table in an org-mode table
  19046. :table-special on a special filed in a table
  19047. :table-table in a table.el table
  19048. :clocktable in a clocktable
  19049. :src-block in a source block
  19050. :link on a hyperlink
  19051. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19052. :target on a <<target>>
  19053. :radio-target on a <<<radio-target>>>
  19054. :latex-fragment on a LaTeX fragment
  19055. :latex-preview on a LaTeX fragment with overlaid preview image
  19056. This function expects the position to be visible because it uses font-lock
  19057. faces as a help to recognize the following contexts: :table-special, :link,
  19058. and :keyword."
  19059. (let* ((f (get-text-property (point) 'face))
  19060. (faces (if (listp f) f (list f)))
  19061. (case-fold-search t)
  19062. (p (point)) clist o)
  19063. ;; First the large context
  19064. (cond
  19065. ((org-at-heading-p t)
  19066. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19067. (when (progn
  19068. (beginning-of-line 1)
  19069. (looking-at org-todo-line-tags-regexp))
  19070. (push (org-point-in-group p 1 :headline-stars) clist)
  19071. (push (org-point-in-group p 2 :todo-keyword) clist)
  19072. (push (org-point-in-group p 4 :tags) clist))
  19073. (goto-char p)
  19074. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19075. (if (looking-at "\\[#[A-Z0-9]\\]")
  19076. (push (org-point-in-group p 0 :priority) clist)))
  19077. ((org-at-item-p)
  19078. (push (org-point-in-group p 2 :item-bullet) clist)
  19079. (push (list :item (point-at-bol)
  19080. (save-excursion (org-end-of-item) (point)))
  19081. clist)
  19082. (and (org-at-item-checkbox-p)
  19083. (push (org-point-in-group p 0 :checkbox) clist)))
  19084. ((org-at-table-p)
  19085. (push (list :table (org-table-begin) (org-table-end)) clist)
  19086. (if (memq 'org-formula faces)
  19087. (push (list :table-special
  19088. (previous-single-property-change p 'face)
  19089. (next-single-property-change p 'face)) clist)))
  19090. ((org-at-table-p 'any)
  19091. (push (list :table-table) clist)))
  19092. (goto-char p)
  19093. (let ((case-fold-search t))
  19094. ;; New the "medium" contexts: clocktables, source blocks
  19095. (cond ((org-in-clocktable-p)
  19096. (push (list :clocktable
  19097. (and (or (looking-at "#\\+BEGIN: clocktable")
  19098. (search-backward "#+BEGIN: clocktable" nil t))
  19099. (match-beginning 0))
  19100. (and (re-search-forward "#\\+END:?" nil t)
  19101. (match-end 0))) clist))
  19102. ((org-in-src-block-p)
  19103. (push (list :src-block
  19104. (and (or (looking-at "#\\+BEGIN_SRC")
  19105. (search-backward "#+BEGIN_SRC" nil t))
  19106. (match-beginning 0))
  19107. (and (search-forward "#+END_SRC" nil t)
  19108. (match-beginning 0))) clist))))
  19109. (goto-char p)
  19110. ;; Now the small context
  19111. (cond
  19112. ((org-at-timestamp-p)
  19113. (push (org-point-in-group p 0 :timestamp) clist))
  19114. ((memq 'org-link faces)
  19115. (push (list :link
  19116. (previous-single-property-change p 'face)
  19117. (next-single-property-change p 'face)) clist))
  19118. ((memq 'org-special-keyword faces)
  19119. (push (list :keyword
  19120. (previous-single-property-change p 'face)
  19121. (next-single-property-change p 'face)) clist))
  19122. ((org-at-target-p)
  19123. (push (org-point-in-group p 0 :target) clist)
  19124. (goto-char (1- (match-beginning 0)))
  19125. (if (looking-at org-radio-target-regexp)
  19126. (push (org-point-in-group p 0 :radio-target) clist))
  19127. (goto-char p))
  19128. ((setq o (car (delq nil
  19129. (mapcar
  19130. (lambda (x)
  19131. (if (memq x org-latex-fragment-image-overlays) x))
  19132. (overlays-at (point))))))
  19133. (push (list :latex-fragment
  19134. (overlay-start o) (overlay-end o)) clist)
  19135. (push (list :latex-preview
  19136. (overlay-start o) (overlay-end o)) clist))
  19137. ((org-inside-LaTeX-fragment-p)
  19138. ;; FIXME: positions wrong.
  19139. (push (list :latex-fragment (point) (point)) clist)))
  19140. (setq clist (nreverse (delq nil clist)))
  19141. clist))
  19142. ;; FIXME: Compare with at-regexp-p Do we need both?
  19143. (defun org-in-regexp (re &optional nlines visually)
  19144. "Check if point is inside a match of regexp.
  19145. Normally only the current line is checked, but you can include NLINES extra
  19146. lines both before and after point into the search.
  19147. If VISUALLY is set, require that the cursor is not after the match but
  19148. really on, so that the block visually is on the match."
  19149. (catch 'exit
  19150. (let ((pos (point))
  19151. (eol (point-at-eol (+ 1 (or nlines 0))))
  19152. (inc (if visually 1 0)))
  19153. (save-excursion
  19154. (beginning-of-line (- 1 (or nlines 0)))
  19155. (while (re-search-forward re eol t)
  19156. (if (and (<= (match-beginning 0) pos)
  19157. (>= (+ inc (match-end 0)) pos))
  19158. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19159. (defun org-at-regexp-p (regexp)
  19160. "Is point inside a match of REGEXP in the current line?"
  19161. (catch 'exit
  19162. (save-excursion
  19163. (let ((pos (point)) (end (point-at-eol)))
  19164. (beginning-of-line 1)
  19165. (while (re-search-forward regexp end t)
  19166. (if (and (<= (match-beginning 0) pos)
  19167. (>= (match-end 0) pos))
  19168. (throw 'exit t)))
  19169. nil))))
  19170. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19171. "Non-nil when point is between matches of START-RE and END-RE.
  19172. Also return a non-nil value when point is on one of the matches.
  19173. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19174. buffer positions. Default values are the positions of headlines
  19175. surrounding the point.
  19176. The functions returns a cons cell whose car (resp. cdr) is the
  19177. position before START-RE (resp. after END-RE)."
  19178. (save-match-data
  19179. (let ((pos (point))
  19180. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19181. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19182. beg end)
  19183. (save-excursion
  19184. ;; Point is on a block when on START-RE or if START-RE can be
  19185. ;; found before it...
  19186. (and (or (org-at-regexp-p start-re)
  19187. (re-search-backward start-re limit-up t))
  19188. (setq beg (match-beginning 0))
  19189. ;; ... and END-RE after it...
  19190. (goto-char (match-end 0))
  19191. (re-search-forward end-re limit-down t)
  19192. (> (setq end (match-end 0)) pos)
  19193. ;; ... without another START-RE in-between.
  19194. (goto-char (match-beginning 0))
  19195. (not (re-search-backward start-re (1+ beg) t))
  19196. ;; Return value.
  19197. (cons beg end))))))
  19198. (defun org-in-block-p (names)
  19199. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19200. NAMES is a list of strings containing names of blocks.
  19201. Return first block name matched, or nil. Beware that in case of
  19202. nested blocks, the returned name may not belong to the closest
  19203. block from point."
  19204. (save-match-data
  19205. (catch 'exit
  19206. (let ((case-fold-search t)
  19207. (lim-up (save-excursion (outline-previous-heading)))
  19208. (lim-down (save-excursion (outline-next-heading))))
  19209. (mapc (lambda (name)
  19210. (let ((n (regexp-quote name)))
  19211. (when (org-between-regexps-p
  19212. (concat "^[ \t]*#\\+begin_" n)
  19213. (concat "^[ \t]*#\\+end_" n)
  19214. lim-up lim-down)
  19215. (throw 'exit n))))
  19216. names))
  19217. nil)))
  19218. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19219. "Call `multi-occur' with buffers for all agenda files."
  19220. (interactive "sOrg-files matching: \np")
  19221. (let* ((files (org-agenda-files))
  19222. (tnames (mapcar 'file-truename files))
  19223. (extra org-agenda-text-search-extra-files)
  19224. f)
  19225. (when (eq (car extra) 'agenda-archives)
  19226. (setq extra (cdr extra))
  19227. (setq files (org-add-archive-files files)))
  19228. (while (setq f (pop extra))
  19229. (unless (member (file-truename f) tnames)
  19230. (add-to-list 'files f 'append)
  19231. (add-to-list 'tnames (file-truename f) 'append)))
  19232. (multi-occur
  19233. (mapcar (lambda (x)
  19234. (with-current-buffer
  19235. (or (get-file-buffer x) (find-file-noselect x))
  19236. (widen)
  19237. (current-buffer)))
  19238. files)
  19239. regexp)))
  19240. (if (boundp 'occur-mode-find-occurrence-hook)
  19241. ;; Emacs 23
  19242. (add-hook 'occur-mode-find-occurrence-hook
  19243. (lambda ()
  19244. (when (derived-mode-p 'org-mode)
  19245. (org-reveal))))
  19246. ;; Emacs 22
  19247. (defadvice occur-mode-goto-occurrence
  19248. (after org-occur-reveal activate)
  19249. (and (derived-mode-p 'org-mode) (org-reveal)))
  19250. (defadvice occur-mode-goto-occurrence-other-window
  19251. (after org-occur-reveal activate)
  19252. (and (derived-mode-p 'org-mode) (org-reveal)))
  19253. (defadvice occur-mode-display-occurrence
  19254. (after org-occur-reveal activate)
  19255. (when (derived-mode-p 'org-mode)
  19256. (let ((pos (occur-mode-find-occurrence)))
  19257. (with-current-buffer (marker-buffer pos)
  19258. (save-excursion
  19259. (goto-char pos)
  19260. (org-reveal)))))))
  19261. (defun org-occur-link-in-agenda-files ()
  19262. "Create a link and search for it in the agendas.
  19263. The link is not stored in `org-stored-links', it is just created
  19264. for the search purpose."
  19265. (interactive)
  19266. (let ((link (condition-case nil
  19267. (org-store-link nil)
  19268. (error "Unable to create a link to here"))))
  19269. (org-occur-in-agenda-files (regexp-quote link))))
  19270. (defun org-uniquify (list)
  19271. "Remove duplicate elements from LIST."
  19272. (let (res)
  19273. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19274. res))
  19275. (defun org-delete-all (elts list)
  19276. "Remove all elements in ELTS from LIST."
  19277. (while elts
  19278. (setq list (delete (pop elts) list)))
  19279. list)
  19280. (defun org-count (cl-item cl-seq)
  19281. "Count the number of occurrences of ITEM in SEQ.
  19282. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19283. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19284. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19285. (while (< cl-start cl-end)
  19286. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19287. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19288. (setq cl-start (1+ cl-start)))
  19289. cl-count))
  19290. (defun org-remove-if (predicate seq)
  19291. "Remove everything from SEQ that fulfills PREDICATE."
  19292. (let (res e)
  19293. (while seq
  19294. (setq e (pop seq))
  19295. (if (not (funcall predicate e)) (push e res)))
  19296. (nreverse res)))
  19297. (defun org-remove-if-not (predicate seq)
  19298. "Remove everything from SEQ that does not fulfill PREDICATE."
  19299. (let (res e)
  19300. (while seq
  19301. (setq e (pop seq))
  19302. (if (funcall predicate e) (push e res)))
  19303. (nreverse res)))
  19304. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19305. "Reduce two-argument FUNCTION across SEQ.
  19306. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19307. \":initial-value\" removed."
  19308. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19309. (cadr (memq :initial-value cl-keys)))
  19310. (cl-seq (pop cl-seq))
  19311. (t (funcall cl-func)))))
  19312. (while cl-seq
  19313. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19314. cl-accum))
  19315. (defun org-back-over-empty-lines ()
  19316. "Move backwards over whitespace, to the beginning of the first empty line.
  19317. Returns the number of empty lines passed."
  19318. (let ((pos (point)))
  19319. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19320. (skip-chars-backward " \t\n\r")
  19321. (unless (eobp)
  19322. (forward-line -1)))
  19323. (beginning-of-line 2)
  19324. (goto-char (min (point) pos))
  19325. (count-lines (point) pos)))
  19326. (defun org-skip-whitespace ()
  19327. (skip-chars-forward " \t\n\r"))
  19328. (defun org-point-in-group (point group &optional context)
  19329. "Check if POINT is in match-group GROUP.
  19330. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19331. match. If the match group does not exist or point is not inside it,
  19332. return nil."
  19333. (and (match-beginning group)
  19334. (>= point (match-beginning group))
  19335. (<= point (match-end group))
  19336. (if context
  19337. (list context (match-beginning group) (match-end group))
  19338. t)))
  19339. (defun org-switch-to-buffer-other-window (&rest args)
  19340. "Switch to buffer in a second window on the current frame.
  19341. In particular, do not allow pop-up frames.
  19342. Returns the newly created buffer."
  19343. (org-no-popups
  19344. (apply 'switch-to-buffer-other-window args)))
  19345. (defun org-combine-plists (&rest plists)
  19346. "Create a single property list from all plists in PLISTS.
  19347. The process starts by copying the first list, and then setting properties
  19348. from the other lists. Settings in the last list are the most significant
  19349. ones and overrule settings in the other lists."
  19350. (let ((rtn (copy-sequence (pop plists)))
  19351. p v ls)
  19352. (while plists
  19353. (setq ls (pop plists))
  19354. (while ls
  19355. (setq p (pop ls) v (pop ls))
  19356. (setq rtn (plist-put rtn p v))))
  19357. rtn))
  19358. (defun org-replace-escapes (string table)
  19359. "Replace %-escapes in STRING with values in TABLE.
  19360. TABLE is an association list with keys like \"%a\" and string values.
  19361. The sequences in STRING may contain normal field width and padding information,
  19362. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19363. so values can contain further %-escapes if they are define later in TABLE."
  19364. (let ((tbl (copy-alist table))
  19365. (case-fold-search nil)
  19366. (pchg 0)
  19367. e re rpl)
  19368. (while (setq e (pop tbl))
  19369. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19370. (when (and (cdr e) (string-match re (cdr e)))
  19371. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19372. (safe "SREF"))
  19373. (add-text-properties 0 3 (list 'sref sref) safe)
  19374. (setcdr e (replace-match safe t t (cdr e)))))
  19375. (while (string-match re string)
  19376. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19377. (cdr e)))
  19378. (setq string (replace-match rpl t t string))))
  19379. (while (setq pchg (next-property-change pchg string))
  19380. (let ((sref (get-text-property pchg 'sref string)))
  19381. (when (and sref (string-match "SREF" string pchg))
  19382. (setq string (replace-match sref t t string)))))
  19383. string))
  19384. (defun org-sublist (list start end)
  19385. "Return a section of LIST, from START to END.
  19386. Counting starts at 1."
  19387. (let (rtn (c start))
  19388. (setq list (nthcdr (1- start) list))
  19389. (while (and list (<= c end))
  19390. (push (pop list) rtn)
  19391. (setq c (1+ c)))
  19392. (nreverse rtn)))
  19393. (defun org-find-base-buffer-visiting (file)
  19394. "Like `find-buffer-visiting' but always return the base buffer and
  19395. not an indirect buffer."
  19396. (let ((buf (or (get-file-buffer file)
  19397. (find-buffer-visiting file))))
  19398. (if buf
  19399. (or (buffer-base-buffer buf) buf)
  19400. nil)))
  19401. (defun org-image-file-name-regexp (&optional extensions)
  19402. "Return regexp matching the file names of images.
  19403. If EXTENSIONS is given, only match these."
  19404. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19405. (image-file-name-regexp)
  19406. (let ((image-file-name-extensions
  19407. (or extensions
  19408. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19409. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19410. (concat "\\."
  19411. (regexp-opt (nconc (mapcar 'upcase
  19412. image-file-name-extensions)
  19413. image-file-name-extensions)
  19414. t)
  19415. "\\'"))))
  19416. (defun org-file-image-p (file &optional extensions)
  19417. "Return non-nil if FILE is an image."
  19418. (save-match-data
  19419. (string-match (org-image-file-name-regexp extensions) file)))
  19420. (defun org-get-cursor-date (&optional with-time)
  19421. "Return the date at cursor in as a time.
  19422. This works in the calendar and in the agenda, anywhere else it just
  19423. returns the current time.
  19424. If WITH-TIME is non-nil, returns the time of the event at point (in
  19425. the agenda) or the current time of the day."
  19426. (let (date day defd tp tm hod mod)
  19427. (when with-time
  19428. (setq tp (get-text-property (point) 'time))
  19429. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19430. (setq hod (string-to-number (match-string 1 tp))
  19431. mod (string-to-number (match-string 2 tp))))
  19432. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19433. mod (nth 1 (decode-time (current-time))))))
  19434. (cond
  19435. ((eq major-mode 'calendar-mode)
  19436. (setq date (calendar-cursor-to-date)
  19437. defd (encode-time 0 (or mod 0) (or hod 0)
  19438. (nth 1 date) (nth 0 date) (nth 2 date))))
  19439. ((eq major-mode 'org-agenda-mode)
  19440. (setq day (get-text-property (point) 'day))
  19441. (if day
  19442. (setq date (calendar-gregorian-from-absolute day)
  19443. defd (encode-time 0 (or mod 0) (or hod 0)
  19444. (nth 1 date) (nth 0 date) (nth 2 date))))))
  19445. (or defd (current-time))))
  19446. (defun org-mark-subtree (&optional up)
  19447. "Mark the current subtree.
  19448. This puts point at the start of the current subtree, and mark at
  19449. the end. If a numeric prefix UP is given, move up into the
  19450. hierarchy of headlines by UP levels before marking the subtree."
  19451. (interactive "P")
  19452. (org-with-limited-levels
  19453. (cond ((org-at-heading-p) (beginning-of-line))
  19454. ((org-before-first-heading-p) (error "Not in a subtree"))
  19455. (t (outline-previous-visible-heading 1))))
  19456. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19457. (if (org-called-interactively-p 'any)
  19458. (call-interactively 'org-mark-element)
  19459. (org-mark-element)))
  19460. ;;; Macros
  19461. ;; Macros are expanded with `org-macro-replace-all', which relies
  19462. ;; internally on `org-macro-expand'.
  19463. ;; Default templates for expansion are stored in the buffer-local
  19464. ;; variable `org-macro-templates'. This variable is updated by
  19465. ;; `org-macro-initialize-templates'.
  19466. ;; Along with macros defined through #+MACRO: keyword, default
  19467. ;; templates include the following hard-coded macros:
  19468. ;; {{{time(format-string)}}}, {{{property(node-property)}}},
  19469. ;; {{{input-file}}} and {{{modification-time(format-string)}}}.
  19470. ;; During export, {{{author}}}, {{{date}}}, {{{email}}} and
  19471. ;; {{{title}}} will also be provided.
  19472. (defvar org-macro-templates nil
  19473. "Alist containing all macro templates in current buffer.
  19474. Associations are in the shape of (NAME . TEMPLATE) where NAME
  19475. stands for macro's name and template for its replacement value,
  19476. both as strings. This is an internal variable. Do not set it
  19477. directly, use instead:
  19478. #+MACRO: name template")
  19479. (make-variable-buffer-local 'org-macro-templates)
  19480. (defun org-macro-expand (macro templates)
  19481. "Return expanded MACRO, as a string.
  19482. MACRO is an object, obtained, for example, with
  19483. `org-element-context'. TEMPLATES is an alist of templates used
  19484. for expansion. See `org-macro-templates' for a buffer-local
  19485. default value. Return nil if no template was found."
  19486. (let ((template
  19487. ;; Macro names are case-insensitive.
  19488. (cdr (assoc-string (org-element-property :key macro) templates t))))
  19489. (when template
  19490. (let ((value (replace-regexp-in-string
  19491. "\\$[0-9]+"
  19492. (lambda (arg)
  19493. (or (nth (1- (string-to-number (substring arg 1)))
  19494. (org-element-property :args macro))
  19495. ;; No argument provided: remove
  19496. ;; place-holder.
  19497. ""))
  19498. template)))
  19499. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  19500. (when (string-match "\\`(eval\\>" value)
  19501. (setq value (eval (read value))))
  19502. ;; Return string.
  19503. (format "%s" (or value ""))))))
  19504. (defun org-macro-replace-all (templates)
  19505. "Replace all macros in current buffer by their expansion.
  19506. TEMPLATES is an alist of templates used for expansion. See
  19507. `org-macro-templates' for a buffer-local default value."
  19508. (save-excursion
  19509. (goto-char (point-min))
  19510. (let (record)
  19511. (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t)
  19512. (let ((object (org-element-context)))
  19513. (when (eq (org-element-type object) 'macro)
  19514. (let* ((value (org-macro-expand object templates))
  19515. (begin (org-element-property :begin object))
  19516. (signature (list begin
  19517. object
  19518. (org-element-property :args object))))
  19519. ;; Avoid circular dependencies by checking if the same
  19520. ;; macro with the same arguments is expanded at the same
  19521. ;; position twice.
  19522. (if (member signature record)
  19523. (error "Circular macro expansion: %s"
  19524. (org-element-property :key object))
  19525. (when value
  19526. (push signature record)
  19527. (delete-region
  19528. begin
  19529. ;; Preserve white spaces after the macro.
  19530. (progn (goto-char (org-element-property :end object))
  19531. (skip-chars-backward " \t")
  19532. (point)))
  19533. ;; Leave point before replacement in case of recursive
  19534. ;; expansions.
  19535. (save-excursion (insert value)))))))))))
  19536. (defun org-macro-initialize-templates ()
  19537. "Collect macro templates defined in current buffer.
  19538. Templates are stored in buffer-local variable
  19539. `org-macro-templates'. In addition to buffer-defined macros, the
  19540. function installs the following ones: \"property\",
  19541. \"time\". and, if the buffer is associated to a file,
  19542. \"input-file\" and \"modification-time\"."
  19543. (let ((case-fold-search t)
  19544. (set-template
  19545. (lambda (cell)
  19546. ;; Add CELL to `org-macro-templates' if there's no
  19547. ;; association matching its name already. Otherwise,
  19548. ;; replace old association with the new one in that
  19549. ;; variable.
  19550. (let ((old-template (assoc (car cell) org-macro-templates)))
  19551. (if old-template (setcdr old-template (cdr cell))
  19552. (push cell org-macro-templates))))))
  19553. ;; Install buffer-local macros.
  19554. (org-with-wide-buffer
  19555. (goto-char (point-min))
  19556. (while (re-search-forward "^[ \t]*#\\+MACRO:" nil t)
  19557. (let ((element (org-element-at-point)))
  19558. (when (eq (org-element-type element) 'keyword)
  19559. (let ((value (org-element-property :value element)))
  19560. (when (string-match "^\\(.*?\\)\\(?:\\s-+\\(.*\\)\\)?\\s-*$" value)
  19561. (funcall set-template
  19562. (cons (match-string 1 value)
  19563. (or (match-string 2 value) "")))))))))
  19564. ;; Install hard-coded macros.
  19565. (mapc (lambda (cell) (funcall set-template cell))
  19566. (list
  19567. (cons "property" "(eval (org-entry-get nil \"$1\" 'selective))")
  19568. (cons "time" "(eval (format-time-string \"$1\"))")))
  19569. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  19570. (when (and visited-file (file-exists-p visited-file))
  19571. (mapc (lambda (cell) (funcall set-template cell))
  19572. (list
  19573. (cons "input-file" (file-name-nondirectory visited-file))
  19574. (cons "modification-time"
  19575. (format "(eval (format-time-string \"$1\" '%s))"
  19576. (prin1-to-string
  19577. (nth 5 (file-attributes visited-file)))))))))))
  19578. ;;; Indentation
  19579. (defun org-indent-line ()
  19580. "Indent line depending on context."
  19581. (interactive)
  19582. (let* ((pos (point))
  19583. (itemp (org-at-item-p))
  19584. (case-fold-search t)
  19585. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19586. (inline-task-p (and (featurep 'org-inlinetask)
  19587. (org-inlinetask-in-task-p)))
  19588. (inline-re (and inline-task-p
  19589. (org-inlinetask-outline-regexp)))
  19590. column)
  19591. (if (and orgstruct-is-++ (eq pos (point)))
  19592. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19593. (indent-according-to-mode))
  19594. (beginning-of-line 1)
  19595. (cond
  19596. ;; Headings
  19597. ((looking-at org-outline-regexp) (setq column 0))
  19598. ;; Included files
  19599. ((looking-at "#\\+include:") (setq column 0))
  19600. ;; Footnote definition
  19601. ((looking-at org-footnote-definition-re) (setq column 0))
  19602. ;; Literal examples
  19603. ((looking-at "[ \t]*:\\( \\|$\\)")
  19604. (setq column (org-get-indentation))) ; do nothing
  19605. ;; Lists
  19606. ((ignore-errors (goto-char (org-in-item-p)))
  19607. (setq column (if itemp
  19608. (org-get-indentation)
  19609. (org-list-item-body-column (point))))
  19610. (goto-char pos))
  19611. ;; Drawers
  19612. ((and (looking-at "[ \t]*:END:")
  19613. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19614. (save-excursion
  19615. (goto-char (1- (match-beginning 1)))
  19616. (setq column (current-column))))
  19617. ;; Special blocks
  19618. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19619. (save-excursion
  19620. (re-search-backward
  19621. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19622. (setq column (org-get-indentation (match-string 0))))
  19623. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19624. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19625. (save-excursion
  19626. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19627. (setq column
  19628. (cond ((equal (downcase (match-string 1)) "src")
  19629. ;; src blocks: let `org-edit-src-exit' handle them
  19630. (org-get-indentation))
  19631. ((equal (downcase (match-string 1)) "example")
  19632. (max (org-get-indentation)
  19633. (org-get-indentation (match-string 0))))
  19634. (t
  19635. (org-get-indentation (match-string 0))))))
  19636. ;; This line has nothing special, look at the previous relevant
  19637. ;; line to compute indentation
  19638. (t
  19639. (beginning-of-line 0)
  19640. (while (and (not (bobp))
  19641. (not (looking-at org-table-line-regexp))
  19642. (not (looking-at org-drawer-regexp))
  19643. ;; When point started in an inline task, do not move
  19644. ;; above task starting line.
  19645. (not (and inline-task-p (looking-at inline-re)))
  19646. ;; Skip drawers, blocks, empty lines, verbatim,
  19647. ;; comments, tables, footnotes definitions, lists,
  19648. ;; inline tasks.
  19649. (or (and (looking-at "[ \t]*:END:")
  19650. (re-search-backward org-drawer-regexp nil t))
  19651. (and (looking-at "[ \t]*#\\+end_")
  19652. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19653. (looking-at "[ \t]*[\n:#|]")
  19654. (looking-at org-footnote-definition-re)
  19655. (and (ignore-errors (goto-char (org-in-item-p)))
  19656. (goto-char
  19657. (org-list-get-top-point (org-list-struct))))
  19658. (and (not inline-task-p)
  19659. (featurep 'org-inlinetask)
  19660. (org-inlinetask-in-task-p)
  19661. (or (org-inlinetask-goto-beginning) t))))
  19662. (beginning-of-line 0))
  19663. (cond
  19664. ;; There was an heading above.
  19665. ((looking-at "\\*+[ \t]+")
  19666. (if (not org-adapt-indentation)
  19667. (setq column 0)
  19668. (goto-char (match-end 0))
  19669. (setq column (current-column))))
  19670. ;; A drawer had started and is unfinished
  19671. ((looking-at org-drawer-regexp)
  19672. (goto-char (1- (match-beginning 1)))
  19673. (setq column (current-column)))
  19674. ;; Else, nothing noticeable found: get indentation and go on.
  19675. (t (setq column (org-get-indentation))))))
  19676. ;; Now apply indentation and move cursor accordingly
  19677. (goto-char pos)
  19678. (if (<= (current-column) (current-indentation))
  19679. (org-indent-line-to column)
  19680. (save-excursion (org-indent-line-to column)))
  19681. ;; Special polishing for properties, see `org-property-format'
  19682. (setq column (current-column))
  19683. (beginning-of-line 1)
  19684. (if (looking-at
  19685. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19686. (replace-match (concat (match-string 1)
  19687. (format org-property-format
  19688. (match-string 2) (match-string 3)))
  19689. t t))
  19690. (org-move-to-column column))))
  19691. (defun org-indent-drawer ()
  19692. "Indent the drawer at point."
  19693. (interactive)
  19694. (let ((p (point))
  19695. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19696. (match-end 0)))
  19697. (folded
  19698. (save-excursion
  19699. (end-of-line)
  19700. (when (overlays-at (point))
  19701. (member 'invisible (overlay-properties
  19702. (car (overlays-at (point)))))))))
  19703. (when folded (org-cycle))
  19704. (indent-for-tab-command)
  19705. (while (and (move-beginning-of-line 2) (< (point) e))
  19706. (indent-for-tab-command))
  19707. (goto-char p)
  19708. (when folded (org-cycle)))
  19709. (message "Drawer at point indented"))
  19710. (defun org-indent-block ()
  19711. "Indent the block at point."
  19712. (interactive)
  19713. (let ((p (point))
  19714. (case-fold-search t)
  19715. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19716. (match-end 0)))
  19717. (folded
  19718. (save-excursion
  19719. (end-of-line)
  19720. (when (overlays-at (point))
  19721. (member 'invisible (overlay-properties
  19722. (car (overlays-at (point)))))))))
  19723. (when folded (org-cycle))
  19724. (indent-for-tab-command)
  19725. (while (and (move-beginning-of-line 2) (< (point) e))
  19726. (indent-for-tab-command))
  19727. (goto-char p)
  19728. (when folded (org-cycle)))
  19729. (message "Block at point indented"))
  19730. (defun org-indent-region (start end)
  19731. "Indent region."
  19732. (interactive "r")
  19733. (save-excursion
  19734. (let ((line-end (org-current-line end)))
  19735. (goto-char start)
  19736. (while (< (org-current-line) line-end)
  19737. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19738. (t (call-interactively 'org-indent-line)))
  19739. (move-beginning-of-line 2)))))
  19740. ;;; Filling
  19741. ;; We use our own fill-paragraph and auto-fill functions.
  19742. ;; `org-fill-paragraph' relies on adaptive filling and context
  19743. ;; checking. Appropriate `fill-prefix' is computed with
  19744. ;; `org-adaptive-fill-function'.
  19745. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19746. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19747. ;; `org-adaptive-fill-function' value. Internally,
  19748. ;; `org-comment-line-break-function' breaks the line.
  19749. ;; `org-setup-filling' installs filling and auto-filling related
  19750. ;; variables during `org-mode' initialization.
  19751. (defun org-setup-filling ()
  19752. (interactive)
  19753. ;; Prevent auto-fill from inserting unwanted new items.
  19754. (when (boundp 'fill-nobreak-predicate)
  19755. (org-set-local
  19756. 'fill-nobreak-predicate
  19757. (org-uniquify
  19758. (append fill-nobreak-predicate
  19759. '(org-fill-paragraph-separate-nobreak-p
  19760. org-fill-line-break-nobreak-p)))))
  19761. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19762. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19763. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19764. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19765. (defvar org-element-paragraph-separate) ; org-element.el
  19766. (defun org-fill-paragraph-separate-nobreak-p ()
  19767. "Non-nil when a line break at point would insert a new item."
  19768. (looking-at (substring org-element-paragraph-separate 1)))
  19769. (defun org-fill-line-break-nobreak-p ()
  19770. "Non-nil when a line break at point would create an Org line break."
  19771. (save-excursion
  19772. (skip-chars-backward "[ \t]")
  19773. (skip-chars-backward "\\\\")
  19774. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19775. (declare-function message-in-body-p "message" ())
  19776. (defun org-adaptive-fill-function ()
  19777. "Compute a fill prefix for the current line.
  19778. Return fill prefix, as a string, or nil if current line isn't
  19779. meant to be filled."
  19780. (org-with-wide-buffer
  19781. (unless (and (derived-mode-p 'message-mode) (not (message-in-body-p)))
  19782. ;; FIXME: This is really the job of orgstruct++-mode
  19783. (let* ((p (line-beginning-position))
  19784. (element (save-excursion (beginning-of-line)
  19785. (org-element-at-point)))
  19786. (type (org-element-type element))
  19787. (post-affiliated (org-element-property :post-affiliated element)))
  19788. (unless (and post-affiliated (< p post-affiliated))
  19789. (case type
  19790. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19791. (footnote-definition "")
  19792. ((item plain-list)
  19793. (make-string (org-list-item-body-column
  19794. (or post-affiliated
  19795. (org-element-property :begin element)))
  19796. ? ))
  19797. (paragraph
  19798. ;; Fill prefix is usually the same as the current line,
  19799. ;; except if the paragraph is at the beginning of an item.
  19800. (let ((parent (org-element-property :parent element)))
  19801. (cond ((eq (org-element-type parent) 'item)
  19802. (make-string (org-list-item-body-column
  19803. (org-element-property :begin parent))
  19804. ? ))
  19805. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19806. (match-string 0))
  19807. (t ""))))
  19808. (comment-block
  19809. ;; Only fill contents if P is within block boundaries.
  19810. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19811. (forward-line)
  19812. (point)))
  19813. (cend (save-excursion
  19814. (goto-char (org-element-property :end element))
  19815. (skip-chars-backward " \r\t\n")
  19816. (line-beginning-position))))
  19817. (when (and (>= p cbeg) (< p cend))
  19818. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19819. (match-string 0)
  19820. ""))))))))))
  19821. (declare-function message-goto-body "message" ())
  19822. (defvar message-cite-prefix-regexp) ; From message.el
  19823. (defvar org-element-all-objects) ; From org-element.el
  19824. (defun org-fill-paragraph (&optional justify)
  19825. "Fill element at point, when applicable.
  19826. This function only applies to comment blocks, comments, example
  19827. blocks and paragraphs. Also, as a special case, re-align table
  19828. when point is at one.
  19829. If JUSTIFY is non-nil (interactively, with prefix argument),
  19830. justify as well. If `sentence-end-double-space' is non-nil, then
  19831. period followed by one space does not end a sentence, so don't
  19832. break a line there. The variable `fill-column' controls the
  19833. width for filling.
  19834. For convenience, when point is at a plain list, an item or
  19835. a footnote definition, try to fill the first paragraph within."
  19836. (interactive)
  19837. (if (and (derived-mode-p 'message-mode)
  19838. (or (not (message-in-body-p))
  19839. (save-excursion (move-beginning-of-line 1)
  19840. (looking-at message-cite-prefix-regexp))))
  19841. ;; First ensure filling is correct in message-mode.
  19842. (let ((fill-paragraph-function
  19843. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  19844. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  19845. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  19846. (paragraph-separate
  19847. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  19848. (fill-paragraph nil))
  19849. (save-excursion
  19850. ;; Move to end of line in order to get the first paragraph
  19851. ;; within a plain list or a footnote definition.
  19852. (end-of-line)
  19853. (let ((element (org-element-at-point)))
  19854. ;; First check if point is in a blank line at the beginning of
  19855. ;; the buffer. In that case, ignore filling.
  19856. (if (< (point) (org-element-property :begin element)) t
  19857. (case (org-element-type element)
  19858. ;; Use major mode filling function is src blocks.
  19859. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  19860. ;; Align Org tables, leave table.el tables as-is.
  19861. (table-row (org-table-align) t)
  19862. (table
  19863. (when (eq (org-element-property :type element) 'org)
  19864. (org-table-align))
  19865. t)
  19866. (paragraph
  19867. ;; Paragraphs may contain `line-break' type objects.
  19868. (let ((beg (max (point-min)
  19869. (org-element-property :contents-begin element)))
  19870. (end (min (point-max)
  19871. (org-element-property :contents-end element))))
  19872. ;; Do nothing if point is at an affiliated keyword.
  19873. (if (< (point) beg) t
  19874. (when (derived-mode-p 'message-mode)
  19875. ;; In `message-mode', do not fill following
  19876. ;; citation in current paragraph nor text before
  19877. ;; message body.
  19878. (let ((body-start (save-excursion (message-goto-body))))
  19879. (when body-start (setq beg (max body-start beg))))
  19880. (when (save-excursion
  19881. (re-search-forward
  19882. (concat "^" message-cite-prefix-regexp) end t))
  19883. (setq end (match-beginning 0))))
  19884. ;; Fill paragraph, taking line breaks into
  19885. ;; consideration. For that, slice the paragraph
  19886. ;; using line breaks as separators, and fill the
  19887. ;; parts in reverse order to avoid messing with
  19888. ;; markers.
  19889. (save-excursion
  19890. (goto-char end)
  19891. (mapc
  19892. (lambda (pos)
  19893. (fill-region-as-paragraph pos (point) justify)
  19894. (goto-char pos))
  19895. ;; Find the list of ending positions for line
  19896. ;; breaks in the current paragraph. Add paragraph
  19897. ;; beginning to include first slice.
  19898. (nreverse
  19899. (cons
  19900. beg
  19901. (org-element-map
  19902. (org-element--parse-objects
  19903. beg end nil org-element-all-objects)
  19904. 'line-break
  19905. (lambda (lb) (org-element-property :end lb)))))))
  19906. t)))
  19907. ;; Contents of `comment-block' type elements should be
  19908. ;; filled as plain text, but only if point is within block
  19909. ;; markers.
  19910. (comment-block
  19911. (let* ((case-fold-search t)
  19912. (beg (save-excursion
  19913. (goto-char (org-element-property :begin element))
  19914. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  19915. (forward-line)
  19916. (point)))
  19917. (end (save-excursion
  19918. (goto-char (org-element-property :end element))
  19919. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  19920. (line-beginning-position))))
  19921. (when (and (>= (point) beg) (< (point) end))
  19922. (fill-region-as-paragraph
  19923. (save-excursion
  19924. (end-of-line)
  19925. (re-search-backward "^[ \t]*$" beg 'move)
  19926. (line-beginning-position))
  19927. (save-excursion
  19928. (beginning-of-line)
  19929. (re-search-forward "^[ \t]*$" end 'move)
  19930. (line-beginning-position))
  19931. justify)))
  19932. t)
  19933. ;; Fill comments.
  19934. (comment (fill-comment-paragraph justify))
  19935. ;; Ignore every other element.
  19936. (otherwise t)))))))
  19937. (defun org-auto-fill-function ()
  19938. "Auto-fill function."
  19939. ;; Check if auto-filling is meaningful.
  19940. (let ((fc (current-fill-column)))
  19941. (when (and fc (> (current-column) fc))
  19942. (let* ((fill-prefix (org-adaptive-fill-function))
  19943. ;; Enforce empty fill prefix, if required. Otherwise, it
  19944. ;; will be computed again.
  19945. (adaptive-fill-mode (not (equal fill-prefix ""))))
  19946. (when fill-prefix (do-auto-fill))))))
  19947. (defun org-comment-line-break-function (&optional soft)
  19948. "Break line at point and indent, continuing comment if within one.
  19949. The inserted newline is marked hard if variable
  19950. `use-hard-newlines' is true, unless optional argument SOFT is
  19951. non-nil."
  19952. (if soft (insert-and-inherit ?\n) (newline 1))
  19953. (save-excursion (forward-char -1) (delete-horizontal-space))
  19954. (delete-horizontal-space)
  19955. (indent-to-left-margin)
  19956. (insert-before-markers-and-inherit fill-prefix))
  19957. ;;; Comments
  19958. ;; Org comments syntax is quite complex. It requires the entire line
  19959. ;; to be just a comment. Also, even with the right syntax at the
  19960. ;; beginning of line, some some elements (i.e. verse-block or
  19961. ;; example-block) don't accept comments. Usual Emacs comment commands
  19962. ;; cannot cope with those requirements. Therefore, Org replaces them.
  19963. ;; Org still relies on `comment-dwim', but cannot trust
  19964. ;; `comment-only-p'. So, `comment-region-function' and
  19965. ;; `uncomment-region-function' both point
  19966. ;; to`org-comment-or-uncomment-region'. Eventually,
  19967. ;; `org-insert-comment' takes care of insertion of comments at the
  19968. ;; beginning of line.
  19969. ;; `org-setup-comments-handling' install comments related variables
  19970. ;; during `org-mode' initialization.
  19971. (defun org-setup-comments-handling ()
  19972. (interactive)
  19973. (org-set-local 'comment-use-syntax nil)
  19974. (org-set-local 'comment-start "# ")
  19975. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  19976. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  19977. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  19978. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  19979. (defun org-insert-comment ()
  19980. "Insert an empty comment above current line.
  19981. If the line is empty, insert comment at its beginning."
  19982. (beginning-of-line)
  19983. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  19984. (org-indent-line)
  19985. (insert "# "))
  19986. (defvar comment-empty-lines) ; From newcomment.el.
  19987. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  19988. "Comment or uncomment each non-blank line in the region.
  19989. Uncomment each non-blank line between BEG and END if it only
  19990. contains commented lines. Otherwise, comment them."
  19991. (save-restriction
  19992. ;; Restrict region
  19993. (narrow-to-region (save-excursion (goto-char beg)
  19994. (skip-chars-forward " \r\t\n" end)
  19995. (line-beginning-position))
  19996. (save-excursion (goto-char end)
  19997. (skip-chars-backward " \r\t\n" beg)
  19998. (line-end-position)))
  19999. (let ((uncommentp
  20000. ;; UNCOMMENTP is non-nil when every non blank line between
  20001. ;; BEG and END is a comment.
  20002. (save-excursion
  20003. (goto-char (point-min))
  20004. (while (and (not (eobp))
  20005. (let ((element (org-element-at-point)))
  20006. (and (eq (org-element-type element) 'comment)
  20007. (goto-char (min (point-max)
  20008. (org-element-property
  20009. :end element)))))))
  20010. (eobp))))
  20011. (if uncommentp
  20012. ;; Only blank lines and comments in region: uncomment it.
  20013. (save-excursion
  20014. (goto-char (point-min))
  20015. (while (not (eobp))
  20016. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20017. (replace-match "" nil nil nil 1))
  20018. (forward-line)))
  20019. ;; Comment each line in region.
  20020. (let ((min-indent (point-max)))
  20021. ;; First find the minimum indentation across all lines.
  20022. (save-excursion
  20023. (goto-char (point-min))
  20024. (while (and (not (eobp)) (not (zerop min-indent)))
  20025. (unless (looking-at "[ \t]*$")
  20026. (setq min-indent (min min-indent (current-indentation))))
  20027. (forward-line)))
  20028. ;; Then loop over all lines.
  20029. (save-excursion
  20030. (goto-char (point-min))
  20031. (while (not (eobp))
  20032. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20033. (org-move-to-column min-indent t)
  20034. (insert comment-start))
  20035. (forward-line))))))))
  20036. ;;; Other stuff.
  20037. (defun org-toggle-fixed-width-section (arg)
  20038. "Toggle the fixed-width export.
  20039. If there is no active region, the QUOTE keyword at the current headline is
  20040. inserted or removed. When present, it causes the text between this headline
  20041. and the next to be exported as fixed-width text, and unmodified.
  20042. If there is an active region, this command adds or removes a colon as the
  20043. first character of this line. If the first character of a line is a colon,
  20044. this line is also exported in fixed-width font."
  20045. (interactive "P")
  20046. (let* ((cc 0)
  20047. (regionp (org-region-active-p))
  20048. (beg (if regionp (region-beginning) (point)))
  20049. (end (if regionp (region-end)))
  20050. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20051. (case-fold-search nil)
  20052. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20053. off)
  20054. (if regionp
  20055. (save-excursion
  20056. (goto-char beg)
  20057. (setq cc (current-column))
  20058. (beginning-of-line 1)
  20059. (setq off (looking-at re))
  20060. (while (> nlines 0)
  20061. (setq nlines (1- nlines))
  20062. (beginning-of-line 1)
  20063. (cond
  20064. (arg
  20065. (org-move-to-column cc t)
  20066. (insert ": \n")
  20067. (forward-line -1))
  20068. ((and off (looking-at re))
  20069. (replace-match "" t t nil 1))
  20070. ((not off) (org-move-to-column cc t) (insert ": ")))
  20071. (forward-line 1)))
  20072. (save-excursion
  20073. (org-back-to-heading)
  20074. (cond
  20075. ((looking-at (format org-heading-keyword-regexp-format
  20076. org-quote-string))
  20077. (goto-char (match-end 1))
  20078. (looking-at (concat " +" org-quote-string))
  20079. (replace-match "" t t)
  20080. (when (eolp) (insert " ")))
  20081. ((looking-at org-outline-regexp)
  20082. (goto-char (match-end 0))
  20083. (insert org-quote-string " ")))))))
  20084. (defun org-reftex-citation ()
  20085. "Use reftex-citation to insert a citation into the buffer.
  20086. This looks for a line like
  20087. #+BIBLIOGRAPHY: foo plain option:-d
  20088. and derives from it that foo.bib is the bibliography file relevant
  20089. for this document. It then installs the necessary environment for RefTeX
  20090. to work in this buffer and calls `reftex-citation' to insert a citation
  20091. into the buffer.
  20092. Export of such citations to both LaTeX and HTML is handled by the contributed
  20093. package org-exp-bibtex by Taru Karttunen."
  20094. (interactive)
  20095. (let ((reftex-docstruct-symbol 'rds)
  20096. (reftex-cite-format "\\cite{%l}")
  20097. rds bib)
  20098. (save-excursion
  20099. (save-restriction
  20100. (widen)
  20101. (let ((case-fold-search t)
  20102. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20103. (if (not (save-excursion
  20104. (or (re-search-forward re nil t)
  20105. (re-search-backward re nil t))))
  20106. (error "No bibliography defined in file")
  20107. (setq bib (concat (match-string 1) ".bib")
  20108. rds (list (list 'bib bib)))))))
  20109. (call-interactively 'reftex-citation)))
  20110. ;;;; Functions extending outline functionality
  20111. (defun org-beginning-of-line (&optional arg)
  20112. "Go to the beginning of the current line. If that is invisible, continue
  20113. to a visible line beginning. This makes the function of C-a more intuitive.
  20114. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20115. first attempt, and only move to after the tags when the cursor is already
  20116. beyond the end of the headline."
  20117. (interactive "P")
  20118. (let ((pos (point))
  20119. (special (if (consp org-special-ctrl-a/e)
  20120. (car org-special-ctrl-a/e)
  20121. org-special-ctrl-a/e))
  20122. refpos)
  20123. (if (org-bound-and-true-p visual-line-mode)
  20124. (beginning-of-visual-line 1)
  20125. (beginning-of-line 1))
  20126. (if (and arg (fboundp 'move-beginning-of-line))
  20127. (call-interactively 'move-beginning-of-line)
  20128. (if (bobp)
  20129. nil
  20130. (backward-char 1)
  20131. (if (org-truely-invisible-p)
  20132. (while (and (not (bobp)) (org-truely-invisible-p))
  20133. (backward-char 1)
  20134. (beginning-of-line 1))
  20135. (forward-char 1))))
  20136. (when special
  20137. (cond
  20138. ((and (looking-at org-complex-heading-regexp)
  20139. (= (char-after (match-end 1)) ?\ ))
  20140. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20141. (point-at-eol)))
  20142. (goto-char
  20143. (if (eq special t)
  20144. (cond ((> pos refpos) refpos)
  20145. ((= pos (point)) refpos)
  20146. (t (point)))
  20147. (cond ((> pos (point)) (point))
  20148. ((not (eq last-command this-command)) (point))
  20149. (t refpos)))))
  20150. ((org-at-item-p)
  20151. ;; Being at an item and not looking at an the item means point
  20152. ;; was previously moved to beginning of a visual line, which
  20153. ;; doesn't contain the item. Therefore, do nothing special,
  20154. ;; just stay here.
  20155. (when (looking-at org-list-full-item-re)
  20156. ;; Set special position at first white space character after
  20157. ;; bullet, and check-box, if any.
  20158. (let ((after-bullet
  20159. (let ((box (match-end 3)))
  20160. (if (not box) (match-end 1)
  20161. (let ((after (char-after box)))
  20162. (if (and after (= after ? )) (1+ box) box))))))
  20163. ;; Special case: Move point to special position when
  20164. ;; currently after it or at beginning of line.
  20165. (if (eq special t)
  20166. (when (or (> pos after-bullet) (= (point) pos))
  20167. (goto-char after-bullet))
  20168. ;; Reversed case: Move point to special position when
  20169. ;; point was already at beginning of line and command is
  20170. ;; repeated.
  20171. (when (and (= (point) pos) (eq last-command this-command))
  20172. (goto-char after-bullet))))))))
  20173. (org-no-warnings
  20174. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20175. (defun org-end-of-line (&optional arg)
  20176. "Go to the end of the line.
  20177. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20178. tags on the first attempt, and only move to after the tags when
  20179. the cursor is already beyond the end of the headline."
  20180. (interactive "P")
  20181. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20182. org-special-ctrl-a/e))
  20183. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20184. 'end-of-visual-line)
  20185. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20186. (t 'end-of-line))))
  20187. (if (or (not special) arg) (call-interactively move-fun)
  20188. (let* ((element (save-excursion (beginning-of-line)
  20189. (org-element-at-point)))
  20190. (type (org-element-type element)))
  20191. (cond
  20192. ((memq type '(headline inlinetask))
  20193. (let ((pos (point)))
  20194. (beginning-of-line 1)
  20195. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20196. (if (eq special t)
  20197. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20198. (goto-char (match-beginning 1))
  20199. (goto-char (match-end 0)))
  20200. (if (or (< pos (match-end 0))
  20201. (not (eq this-command last-command)))
  20202. (goto-char (match-end 0))
  20203. (goto-char (match-beginning 1))))
  20204. (call-interactively move-fun))))
  20205. ((org-element-property :hiddenp element)
  20206. ;; If element is hidden, `move-end-of-line' would put point
  20207. ;; after it. Use `end-of-line' to stay on current line.
  20208. (call-interactively 'end-of-line))
  20209. (t (call-interactively move-fun)))))
  20210. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20211. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20212. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20213. (defun org-backward-sentence (&optional arg)
  20214. "Go to beginning of sentence, or beginning of table field.
  20215. This will call `backward-sentence' or `org-table-beginning-of-field',
  20216. depending on context."
  20217. (interactive "P")
  20218. (cond
  20219. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20220. (t (call-interactively 'backward-sentence))))
  20221. (defun org-forward-sentence (&optional arg)
  20222. "Go to end of sentence, or end of table field.
  20223. This will call `forward-sentence' or `org-table-end-of-field',
  20224. depending on context."
  20225. (interactive "P")
  20226. (cond
  20227. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20228. (t (call-interactively 'forward-sentence))))
  20229. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20230. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20231. (defun org-kill-line (&optional arg)
  20232. "Kill line, to tags or end of line."
  20233. (interactive "P")
  20234. (cond
  20235. ((or (not org-special-ctrl-k)
  20236. (bolp)
  20237. (not (org-at-heading-p)))
  20238. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20239. org-ctrl-k-protect-subtree)
  20240. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20241. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20242. (error "C-k aborted - would kill hidden subtree")))
  20243. (call-interactively
  20244. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20245. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20246. (kill-region (point) (match-beginning 1))
  20247. (org-set-tags nil t))
  20248. (t (kill-region (point) (point-at-eol)))))
  20249. (define-key org-mode-map "\C-k" 'org-kill-line)
  20250. (defun org-yank (&optional arg)
  20251. "Yank. If the kill is a subtree, treat it specially.
  20252. This command will look at the current kill and check if is a single
  20253. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20254. cursor is at the beginning of a line or after the stars of a currently
  20255. empty headline, then the yank is handled specially. How exactly depends
  20256. on the value of the following variables, both set by default.
  20257. org-yank-folded-subtrees
  20258. When set, the subtree(s) will be folded after insertion, but only
  20259. if doing so would now swallow text after the yanked text.
  20260. org-yank-adjusted-subtrees
  20261. When set, the subtree will be promoted or demoted in order to
  20262. fit into the local outline tree structure, which means that the level
  20263. will be adjusted so that it becomes the smaller one of the two
  20264. *visible* surrounding headings.
  20265. Any prefix to this command will cause `yank' to be called directly with
  20266. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20267. will just
  20268. plainly yank the text as it is.
  20269. \[1] The test checks if the first non-white line is a heading
  20270. and if there are no other headings with fewer stars."
  20271. (interactive "P")
  20272. (org-yank-generic 'yank arg))
  20273. (defun org-yank-generic (command arg)
  20274. "Perform some yank-like command.
  20275. This function implements the behavior described in the `org-yank'
  20276. documentation. However, it has been generalized to work for any
  20277. interactive command with similar behavior."
  20278. ;; pretend to be command COMMAND
  20279. (setq this-command command)
  20280. (if arg
  20281. (call-interactively command)
  20282. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20283. (and (org-kill-is-subtree-p)
  20284. (or (bolp)
  20285. (and (looking-at "[ \t]*$")
  20286. (string-match
  20287. "\\`\\*+\\'"
  20288. (buffer-substring (point-at-bol) (point)))))))
  20289. swallowp)
  20290. (cond
  20291. ((and subtreep org-yank-folded-subtrees)
  20292. (let ((beg (point))
  20293. end)
  20294. (if (and subtreep org-yank-adjusted-subtrees)
  20295. (org-paste-subtree nil nil 'for-yank)
  20296. (call-interactively command))
  20297. (setq end (point))
  20298. (goto-char beg)
  20299. (when (and (bolp) subtreep
  20300. (not (setq swallowp
  20301. (org-yank-folding-would-swallow-text beg end))))
  20302. (org-with-limited-levels
  20303. (or (looking-at org-outline-regexp)
  20304. (re-search-forward org-outline-regexp-bol end t))
  20305. (while (and (< (point) end) (looking-at org-outline-regexp))
  20306. (hide-subtree)
  20307. (org-cycle-show-empty-lines 'folded)
  20308. (condition-case nil
  20309. (outline-forward-same-level 1)
  20310. (error (goto-char end))))))
  20311. (when swallowp
  20312. (message
  20313. "Inserted text not folded because that would swallow text"))
  20314. (goto-char end)
  20315. (skip-chars-forward " \t\n\r")
  20316. (beginning-of-line 1)
  20317. (push-mark beg 'nomsg)))
  20318. ((and subtreep org-yank-adjusted-subtrees)
  20319. (let ((beg (point-at-bol)))
  20320. (org-paste-subtree nil nil 'for-yank)
  20321. (push-mark beg 'nomsg)))
  20322. (t
  20323. (call-interactively command))))))
  20324. (defun org-yank-folding-would-swallow-text (beg end)
  20325. "Would hide-subtree at BEG swallow any text after END?"
  20326. (let (level)
  20327. (org-with-limited-levels
  20328. (save-excursion
  20329. (goto-char beg)
  20330. (when (or (looking-at org-outline-regexp)
  20331. (re-search-forward org-outline-regexp-bol end t))
  20332. (setq level (org-outline-level)))
  20333. (goto-char end)
  20334. (skip-chars-forward " \t\r\n\v\f")
  20335. (if (or (eobp)
  20336. (and (bolp) (looking-at org-outline-regexp)
  20337. (<= (org-outline-level) level)))
  20338. nil ; Nothing would be swallowed
  20339. t))))) ; something would swallow
  20340. (define-key org-mode-map "\C-y" 'org-yank)
  20341. (defun org-truely-invisible-p ()
  20342. "Check if point is at a character currently not visible.
  20343. This version does not only check the character property, but also
  20344. `visible-mode'."
  20345. ;; Early versions of noutline don't have `outline-invisible-p'.
  20346. (if (org-bound-and-true-p visible-mode)
  20347. nil
  20348. (outline-invisible-p)))
  20349. (defun org-invisible-p2 ()
  20350. "Check if point is at a character currently not visible."
  20351. (save-excursion
  20352. (if (and (eolp) (not (bobp))) (backward-char 1))
  20353. ;; Early versions of noutline don't have `outline-invisible-p'.
  20354. (outline-invisible-p)))
  20355. (defun org-back-to-heading (&optional invisible-ok)
  20356. "Call `outline-back-to-heading', but provide a better error message."
  20357. (condition-case nil
  20358. (outline-back-to-heading invisible-ok)
  20359. (error (error "Before first headline at position %d in buffer %s"
  20360. (point) (current-buffer)))))
  20361. (defun org-before-first-heading-p ()
  20362. "Before first heading?"
  20363. (save-excursion
  20364. (end-of-line)
  20365. (null (re-search-backward org-outline-regexp-bol nil t))))
  20366. (defun org-at-heading-p (&optional ignored)
  20367. (outline-on-heading-p t))
  20368. ;; Compatibility alias with Org versions < 7.8.03
  20369. (defalias 'org-on-heading-p 'org-at-heading-p)
  20370. (defun org-at-comment-p nil
  20371. "Is cursor in a line starting with a # character?"
  20372. (save-excursion
  20373. (beginning-of-line)
  20374. (looking-at "^#")))
  20375. (defun org-at-drawer-p nil
  20376. "Is cursor at a drawer keyword?"
  20377. (save-excursion
  20378. (move-beginning-of-line 1)
  20379. (looking-at org-drawer-regexp)))
  20380. (defun org-at-block-p nil
  20381. "Is cursor at a block keyword?"
  20382. (save-excursion
  20383. (move-beginning-of-line 1)
  20384. (looking-at org-block-regexp)))
  20385. (defun org-point-at-end-of-empty-headline ()
  20386. "If point is at the end of an empty headline, return t, else nil.
  20387. If the heading only contains a TODO keyword, it is still still considered
  20388. empty."
  20389. (and (looking-at "[ \t]*$")
  20390. (when org-todo-line-regexp
  20391. (save-excursion
  20392. (beginning-of-line 1)
  20393. (let ((case-fold-search nil))
  20394. (looking-at org-todo-line-regexp)
  20395. (string= (match-string 3) ""))))))
  20396. (defun org-at-heading-or-item-p ()
  20397. (or (org-at-heading-p) (org-at-item-p)))
  20398. (defun org-at-target-p ()
  20399. (or (org-in-regexp org-radio-target-regexp)
  20400. (org-in-regexp org-target-regexp)))
  20401. ;; Compatibility alias with Org versions < 7.8.03
  20402. (defalias 'org-on-target-p 'org-at-target-p)
  20403. (defun org-up-heading-all (arg)
  20404. "Move to the heading line of which the present line is a subheading.
  20405. This function considers both visible and invisible heading lines.
  20406. With argument, move up ARG levels."
  20407. (if (fboundp 'outline-up-heading-all)
  20408. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20409. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20410. (defun org-up-heading-safe ()
  20411. "Move to the heading line of which the present line is a subheading.
  20412. This version will not throw an error. It will return the level of the
  20413. headline found, or nil if no higher level is found.
  20414. Also, this function will be a lot faster than `outline-up-heading',
  20415. because it relies on stars being the outline starters. This can really
  20416. make a significant difference in outlines with very many siblings."
  20417. (let (start-level re)
  20418. (org-back-to-heading t)
  20419. (setq start-level (funcall outline-level))
  20420. (if (equal start-level 1)
  20421. nil
  20422. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20423. (if (re-search-backward re nil t)
  20424. (funcall outline-level)))))
  20425. (defun org-first-sibling-p ()
  20426. "Is this heading the first child of its parents?"
  20427. (interactive)
  20428. (let ((re org-outline-regexp-bol)
  20429. level l)
  20430. (unless (org-at-heading-p t)
  20431. (error "Not at a heading"))
  20432. (setq level (funcall outline-level))
  20433. (save-excursion
  20434. (if (not (re-search-backward re nil t))
  20435. t
  20436. (setq l (funcall outline-level))
  20437. (< l level)))))
  20438. (defun org-goto-sibling (&optional previous)
  20439. "Goto the next sibling, even if it is invisible.
  20440. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20441. when a sibling was found. When none is found, return nil and don't
  20442. move point."
  20443. (let ((fun (if previous 're-search-backward 're-search-forward))
  20444. (pos (point))
  20445. (re org-outline-regexp-bol)
  20446. level l)
  20447. (when (condition-case nil (org-back-to-heading t) (error nil))
  20448. (setq level (funcall outline-level))
  20449. (catch 'exit
  20450. (or previous (forward-char 1))
  20451. (while (funcall fun re nil t)
  20452. (setq l (funcall outline-level))
  20453. (when (< l level) (goto-char pos) (throw 'exit nil))
  20454. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20455. (goto-char pos)
  20456. nil))))
  20457. (defun org-show-siblings ()
  20458. "Show all siblings of the current headline."
  20459. (save-excursion
  20460. (while (org-goto-sibling) (org-flag-heading nil)))
  20461. (save-excursion
  20462. (while (org-goto-sibling 'previous)
  20463. (org-flag-heading nil))))
  20464. (defun org-goto-first-child ()
  20465. "Goto the first child, even if it is invisible.
  20466. Return t when a child was found. Otherwise don't move point and
  20467. return nil."
  20468. (let (level (pos (point)) (re org-outline-regexp-bol))
  20469. (when (condition-case nil (org-back-to-heading t) (error nil))
  20470. (setq level (outline-level))
  20471. (forward-char 1)
  20472. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20473. (progn (goto-char (match-beginning 0)) t)
  20474. (goto-char pos) nil))))
  20475. (defun org-show-hidden-entry ()
  20476. "Show an entry where even the heading is hidden."
  20477. (save-excursion
  20478. (org-show-entry)))
  20479. (defun org-flag-heading (flag &optional entry)
  20480. "Flag the current heading. FLAG non-nil means make invisible.
  20481. When ENTRY is non-nil, show the entire entry."
  20482. (save-excursion
  20483. (org-back-to-heading t)
  20484. ;; Check if we should show the entire entry
  20485. (if entry
  20486. (progn
  20487. (org-show-entry)
  20488. (save-excursion
  20489. (and (outline-next-heading)
  20490. (org-flag-heading nil))))
  20491. (outline-flag-region (max (point-min) (1- (point)))
  20492. (save-excursion (outline-end-of-heading) (point))
  20493. flag))))
  20494. (defun org-get-next-sibling ()
  20495. "Move to next heading of the same level, and return point.
  20496. If there is no such heading, return nil.
  20497. This is like outline-next-sibling, but invisible headings are ok."
  20498. (let ((level (funcall outline-level)))
  20499. (outline-next-heading)
  20500. (while (and (not (eobp)) (> (funcall outline-level) level))
  20501. (outline-next-heading))
  20502. (if (or (eobp) (< (funcall outline-level) level))
  20503. nil
  20504. (point))))
  20505. (defun org-get-last-sibling ()
  20506. "Move to previous heading of the same level, and return point.
  20507. If there is no such heading, return nil."
  20508. (let ((opoint (point))
  20509. (level (funcall outline-level)))
  20510. (outline-previous-heading)
  20511. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20512. (while (and (> (funcall outline-level) level)
  20513. (not (bobp)))
  20514. (outline-previous-heading))
  20515. (if (< (funcall outline-level) level)
  20516. nil
  20517. (point)))))
  20518. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20519. "Goto to the end of a subtree."
  20520. ;; This contains an exact copy of the original function, but it uses
  20521. ;; `org-back-to-heading', to make it work also in invisible
  20522. ;; trees. And is uses an invisible-ok argument.
  20523. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20524. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20525. ;; with many children and grandchildren etc, this can be much faster
  20526. ;; than the outline version.
  20527. (org-back-to-heading invisible-ok)
  20528. (let ((first t)
  20529. (level (funcall outline-level)))
  20530. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20531. ;; A true heading (not a plain list item), in Org-mode
  20532. ;; This means we can easily find the end by looking
  20533. ;; only for the right number of stars. Using a regexp to do
  20534. ;; this is so much faster than using a Lisp loop.
  20535. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20536. (forward-char 1)
  20537. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20538. ;; something else, do it the slow way
  20539. (while (and (not (eobp))
  20540. (or first (> (funcall outline-level) level)))
  20541. (setq first nil)
  20542. (outline-next-heading)))
  20543. (unless to-heading
  20544. (if (memq (preceding-char) '(?\n ?\^M))
  20545. (progn
  20546. ;; Go to end of line before heading
  20547. (forward-char -1)
  20548. (if (memq (preceding-char) '(?\n ?\^M))
  20549. ;; leave blank line before heading
  20550. (forward-char -1))))))
  20551. (point))
  20552. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20553. "Use Org version in org-mode, for dramatic speed-up."
  20554. (if (derived-mode-p 'org-mode)
  20555. (progn
  20556. (org-end-of-subtree nil t)
  20557. (unless (eobp) (backward-char 1)))
  20558. ad-do-it))
  20559. (defun org-end-of-meta-data-and-drawers ()
  20560. "Jump to the first text after meta data and drawers in the current entry.
  20561. This will move over empty lines, lines with planning time stamps,
  20562. clocking lines, and drawers."
  20563. (org-back-to-heading t)
  20564. (let ((end (save-excursion (outline-next-heading) (point)))
  20565. (re (concat "\\(" org-drawer-regexp "\\)"
  20566. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20567. (forward-line 1)
  20568. (while (re-search-forward re end t)
  20569. (if (not (match-end 1))
  20570. ;; empty or planning line
  20571. (forward-line 1)
  20572. ;; a drawer, find the end
  20573. (re-search-forward "^[ \t]*:END:" end 'move)
  20574. (forward-line 1)))
  20575. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20576. (point)))
  20577. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20578. "Move forward to the arg'th subheading at same level as this one.
  20579. Stop at the first and last subheadings of a superior heading.
  20580. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20581. non-nil it will also look at invisible ones."
  20582. (interactive "p")
  20583. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  20584. (outline-next-heading)
  20585. (org-at-heading-p)
  20586. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20587. (re (format "^\\*\\{1,%d\\} " level))
  20588. l)
  20589. (forward-char 1)
  20590. (while (> arg 0)
  20591. (while (and (re-search-forward re nil 'move)
  20592. (setq l (- (match-end 0) (match-beginning 0) 1))
  20593. (= l level)
  20594. (not invisible-ok)
  20595. (progn (backward-char 1) (outline-invisible-p)))
  20596. (if (< l level) (setq arg 1)))
  20597. (setq arg (1- arg)))
  20598. (beginning-of-line 1))))
  20599. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20600. "Move backward to the arg'th subheading at same level as this one.
  20601. Stop at the first and last subheadings of a superior heading."
  20602. (interactive "p")
  20603. (if (not (ignore-errors (org-back-to-heading)))
  20604. (goto-char (point-min))
  20605. (org-at-heading-p)
  20606. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20607. (re (format "^\\*\\{1,%d\\} " level))
  20608. l)
  20609. (while (> arg 0)
  20610. (while (and (re-search-backward re nil 'move)
  20611. (setq l (- (match-end 0) (match-beginning 0) 1))
  20612. (= l level)
  20613. (not invisible-ok)
  20614. (outline-invisible-p))
  20615. (if (< l level) (setq arg 1)))
  20616. (setq arg (1- arg))))))
  20617. (defun org-forward-element ()
  20618. "Move forward by one element.
  20619. Move to the next element at the same level, when possible."
  20620. (interactive)
  20621. (cond ((eobp) (error "Cannot move further down"))
  20622. ((org-with-limited-levels (org-at-heading-p))
  20623. (let ((origin (point)))
  20624. (org-forward-heading-same-level 1)
  20625. (unless (org-with-limited-levels (org-at-heading-p))
  20626. (goto-char origin)
  20627. (error "Cannot move further down"))))
  20628. (t
  20629. (let* ((elem (org-element-at-point))
  20630. (end (org-element-property :end elem))
  20631. (parent (org-element-property :parent elem)))
  20632. (if (and parent (= (org-element-property :contents-end parent) end))
  20633. (goto-char (org-element-property :end parent))
  20634. (goto-char end))))))
  20635. (defun org-backward-element ()
  20636. "Move backward by one element.
  20637. Move to the previous element at the same level, when possible."
  20638. (interactive)
  20639. (cond ((bobp) (error "Cannot move further up"))
  20640. ((org-with-limited-levels (org-at-heading-p))
  20641. ;; At an headline, move to the previous one, if any, or stay
  20642. ;; here.
  20643. (let ((origin (point)))
  20644. (org-backward-heading-same-level 1)
  20645. (unless (org-with-limited-levels (org-at-heading-p))
  20646. (goto-char origin)
  20647. (error "Cannot move further up"))))
  20648. (t
  20649. (let* ((trail (org-element-at-point 'keep-trail))
  20650. (elem (car trail))
  20651. (prev-elem (nth 1 trail))
  20652. (beg (org-element-property :begin elem)))
  20653. (cond
  20654. ;; Move to beginning of current element if point isn't
  20655. ;; there already.
  20656. ((/= (point) beg) (goto-char beg))
  20657. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20658. ((org-before-first-heading-p) (goto-char (point-min)))
  20659. (t (org-back-to-heading)))))))
  20660. (defun org-up-element ()
  20661. "Move to upper element."
  20662. (interactive)
  20663. (if (org-with-limited-levels (org-at-heading-p))
  20664. (unless (org-up-heading-safe) (error "No surrounding element"))
  20665. (let* ((elem (org-element-at-point))
  20666. (parent (org-element-property :parent elem)))
  20667. (if parent (goto-char (org-element-property :begin parent))
  20668. (if (org-with-limited-levels (org-before-first-heading-p))
  20669. (error "No surrounding element")
  20670. (org-with-limited-levels (org-back-to-heading)))))))
  20671. (defvar org-element-greater-elements)
  20672. (defun org-down-element ()
  20673. "Move to inner element."
  20674. (interactive)
  20675. (let ((element (org-element-at-point)))
  20676. (cond
  20677. ((memq (org-element-type element) '(plain-list table))
  20678. (goto-char (org-element-property :contents-begin element))
  20679. (forward-char))
  20680. ((memq (org-element-type element) org-element-greater-elements)
  20681. ;; If contents are hidden, first disclose them.
  20682. (when (org-element-property :hiddenp element) (org-cycle))
  20683. (goto-char (or (org-element-property :contents-begin element)
  20684. (error "No content for this element"))))
  20685. (t (error "No inner element")))))
  20686. (defun org-drag-element-backward ()
  20687. "Move backward element at point."
  20688. (interactive)
  20689. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20690. (let* ((trail (org-element-at-point 'keep-trail))
  20691. (elem (car trail))
  20692. (prev-elem (nth 1 trail)))
  20693. ;; Error out if no previous element or previous element is
  20694. ;; a parent of the current one.
  20695. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20696. (error "Cannot drag element backward")
  20697. (let ((pos (point)))
  20698. (org-element-swap-A-B prev-elem elem)
  20699. (goto-char (+ (org-element-property :begin prev-elem)
  20700. (- pos (org-element-property :begin elem)))))))))
  20701. (defun org-drag-element-forward ()
  20702. "Move forward element at point."
  20703. (interactive)
  20704. (let* ((pos (point))
  20705. (elem (org-element-at-point)))
  20706. (when (= (point-max) (org-element-property :end elem))
  20707. (error "Cannot drag element forward"))
  20708. (goto-char (org-element-property :end elem))
  20709. (let ((next-elem (org-element-at-point)))
  20710. (when (or (org-element-nested-p elem next-elem)
  20711. (and (eq (org-element-type next-elem) 'headline)
  20712. (not (eq (org-element-type elem) 'headline))))
  20713. (goto-char pos)
  20714. (error "Cannot drag element forward"))
  20715. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20716. ;; body's length (without final blanks) and by the length of
  20717. ;; blanks between ELEM and NEXT-ELEM.
  20718. (let ((size-next (- (save-excursion
  20719. (goto-char (org-element-property :end next-elem))
  20720. (skip-chars-backward " \r\t\n")
  20721. (forward-line)
  20722. ;; Small correction if buffer doesn't end
  20723. ;; with a newline character.
  20724. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20725. (org-element-property :begin next-elem)))
  20726. (size-blank (- (org-element-property :end elem)
  20727. (save-excursion
  20728. (goto-char (org-element-property :end elem))
  20729. (skip-chars-backward " \r\t\n")
  20730. (forward-line)
  20731. (point)))))
  20732. (org-element-swap-A-B elem next-elem)
  20733. (goto-char (+ pos size-next size-blank))))))
  20734. (defun org-mark-element ()
  20735. "Put point at beginning of this element, mark at end.
  20736. Interactively, if this command is repeated or (in Transient Mark
  20737. mode) if the mark is active, it marks the next element after the
  20738. ones already marked."
  20739. (interactive)
  20740. (let (deactivate-mark)
  20741. (if (and (org-called-interactively-p 'any)
  20742. (or (and (eq last-command this-command) (mark t))
  20743. (and transient-mark-mode mark-active)))
  20744. (set-mark
  20745. (save-excursion
  20746. (goto-char (mark))
  20747. (goto-char (org-element-property :end (org-element-at-point)))))
  20748. (let ((element (org-element-at-point)))
  20749. (end-of-line)
  20750. (push-mark (org-element-property :end element) t t)
  20751. (goto-char (org-element-property :begin element))))))
  20752. (defun org-narrow-to-element ()
  20753. "Narrow buffer to current element."
  20754. (interactive)
  20755. (let ((elem (org-element-at-point)))
  20756. (cond
  20757. ((eq (car elem) 'headline)
  20758. (narrow-to-region
  20759. (org-element-property :begin elem)
  20760. (org-element-property :end elem)))
  20761. ((memq (car elem) org-element-greater-elements)
  20762. (narrow-to-region
  20763. (org-element-property :contents-begin elem)
  20764. (org-element-property :contents-end elem)))
  20765. (t
  20766. (narrow-to-region
  20767. (org-element-property :begin elem)
  20768. (org-element-property :end elem))))))
  20769. (defun org-transpose-words ()
  20770. "Transpose words, using `org-mode' syntax table."
  20771. (interactive)
  20772. (with-syntax-table org-syntax-table
  20773. (call-interactively 'transpose-words)))
  20774. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  20775. (defun org-transpose-element ()
  20776. "Transpose current and previous elements, keeping blank lines between.
  20777. Point is moved after both elements."
  20778. (interactive)
  20779. (org-skip-whitespace)
  20780. (let ((end (org-element-property :end (org-element-at-point))))
  20781. (org-drag-element-backward)
  20782. (goto-char end)))
  20783. (defun org-unindent-buffer ()
  20784. "Un-indent the visible part of the buffer.
  20785. Relative indentation (between items, inside blocks, etc.) isn't
  20786. modified."
  20787. (interactive)
  20788. (unless (eq major-mode 'org-mode)
  20789. (error "Cannot un-indent a buffer not in Org mode"))
  20790. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  20791. unindent-tree ; For byte-compiler.
  20792. (unindent-tree
  20793. (function
  20794. (lambda (contents)
  20795. (mapc
  20796. (lambda (element)
  20797. (if (memq (org-element-type element) '(headline section))
  20798. (funcall unindent-tree (org-element-contents element))
  20799. (save-excursion
  20800. (save-restriction
  20801. (narrow-to-region
  20802. (org-element-property :begin element)
  20803. (org-element-property :end element))
  20804. (org-do-remove-indentation)))))
  20805. (reverse contents))))))
  20806. (funcall unindent-tree (org-element-contents parse-tree))))
  20807. (defun org-show-subtree ()
  20808. "Show everything after this heading at deeper levels."
  20809. (interactive)
  20810. (outline-flag-region
  20811. (point)
  20812. (save-excursion
  20813. (org-end-of-subtree t t))
  20814. nil))
  20815. (defun org-show-entry ()
  20816. "Show the body directly following this heading.
  20817. Show the heading too, if it is currently invisible."
  20818. (interactive)
  20819. (save-excursion
  20820. (condition-case nil
  20821. (progn
  20822. (org-back-to-heading t)
  20823. (outline-flag-region
  20824. (max (point-min) (1- (point)))
  20825. (save-excursion
  20826. (if (re-search-forward
  20827. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  20828. (match-beginning 1)
  20829. (point-max)))
  20830. nil)
  20831. (org-cycle-hide-drawers 'children))
  20832. (error nil))))
  20833. (defun org-make-options-regexp (kwds &optional extra)
  20834. "Make a regular expression for keyword lines."
  20835. (concat
  20836. "^#\\+\\("
  20837. (mapconcat 'regexp-quote kwds "\\|")
  20838. (if extra (concat "\\|" extra))
  20839. "\\):[ \t]*\\(.*\\)"))
  20840. ;; Make isearch reveal the necessary context
  20841. (defun org-isearch-end ()
  20842. "Reveal context after isearch exits."
  20843. (when isearch-success ; only if search was successful
  20844. (if (featurep 'xemacs)
  20845. ;; Under XEmacs, the hook is run in the correct place,
  20846. ;; we directly show the context.
  20847. (org-show-context 'isearch)
  20848. ;; In Emacs the hook runs *before* restoring the overlays.
  20849. ;; So we have to use a one-time post-command-hook to do this.
  20850. ;; (Emacs 22 has a special variable, see function `org-mode')
  20851. (unless (and (boundp 'isearch-mode-end-hook-quit)
  20852. isearch-mode-end-hook-quit)
  20853. ;; Only when the isearch was not quitted.
  20854. (org-add-hook 'post-command-hook 'org-isearch-post-command
  20855. 'append 'local)))))
  20856. (defun org-isearch-post-command ()
  20857. "Remove self from hook, and show context."
  20858. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  20859. (org-show-context 'isearch))
  20860. ;;;; Integration with and fixes for other packages
  20861. ;;; Imenu support
  20862. (defvar org-imenu-markers nil
  20863. "All markers currently used by Imenu.")
  20864. (make-variable-buffer-local 'org-imenu-markers)
  20865. (defun org-imenu-new-marker (&optional pos)
  20866. "Return a new marker for use by Imenu, and remember the marker."
  20867. (let ((m (make-marker)))
  20868. (move-marker m (or pos (point)))
  20869. (push m org-imenu-markers)
  20870. m))
  20871. (defun org-imenu-get-tree ()
  20872. "Produce the index for Imenu."
  20873. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  20874. (setq org-imenu-markers nil)
  20875. (let* ((n org-imenu-depth)
  20876. (re (concat "^" (org-get-limited-outline-regexp)))
  20877. (subs (make-vector (1+ n) nil))
  20878. (last-level 0)
  20879. m level head)
  20880. (save-excursion
  20881. (save-restriction
  20882. (widen)
  20883. (goto-char (point-max))
  20884. (while (re-search-backward re nil t)
  20885. (setq level (org-reduced-level (funcall outline-level)))
  20886. (when (and (<= level n)
  20887. (looking-at org-complex-heading-regexp))
  20888. (setq head (org-link-display-format
  20889. (org-match-string-no-properties 4))
  20890. m (org-imenu-new-marker))
  20891. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  20892. (if (>= level last-level)
  20893. (push (cons head m) (aref subs level))
  20894. (push (cons head (aref subs (1+ level))) (aref subs level))
  20895. (loop for i from (1+ level) to n do (aset subs i nil)))
  20896. (setq last-level level)))))
  20897. (aref subs 1)))
  20898. (eval-after-load "imenu"
  20899. '(progn
  20900. (add-hook 'imenu-after-jump-hook
  20901. (lambda ()
  20902. (if (derived-mode-p 'org-mode)
  20903. (org-show-context 'org-goto))))))
  20904. (defun org-link-display-format (link)
  20905. "Replace a link with either the description, or the link target
  20906. if no description is present"
  20907. (save-match-data
  20908. (if (string-match org-bracket-link-analytic-regexp link)
  20909. (replace-match (if (match-end 5)
  20910. (match-string 5 link)
  20911. (concat (match-string 1 link)
  20912. (match-string 3 link)))
  20913. nil t link)
  20914. link)))
  20915. (defun org-toggle-link-display ()
  20916. "Toggle the literal or descriptive display of links."
  20917. (interactive)
  20918. (if org-descriptive-links
  20919. (progn (org-remove-from-invisibility-spec '(org-link))
  20920. (org-restart-font-lock)
  20921. (setq org-descriptive-links nil))
  20922. (progn (add-to-invisibility-spec '(org-link))
  20923. (org-restart-font-lock)
  20924. (setq org-descriptive-links t))))
  20925. ;; Speedbar support
  20926. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  20927. "Overlay marking the agenda restriction line in speedbar.")
  20928. (overlay-put org-speedbar-restriction-lock-overlay
  20929. 'face 'org-agenda-restriction-lock)
  20930. (overlay-put org-speedbar-restriction-lock-overlay
  20931. 'help-echo "Agendas are currently limited to this item.")
  20932. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20933. (defun org-speedbar-set-agenda-restriction ()
  20934. "Restrict future agenda commands to the location at point in speedbar.
  20935. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  20936. (interactive)
  20937. (require 'org-agenda)
  20938. (let (p m tp np dir txt)
  20939. (cond
  20940. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20941. 'org-imenu t))
  20942. (setq m (get-text-property p 'org-imenu-marker))
  20943. (with-current-buffer (marker-buffer m)
  20944. (goto-char m)
  20945. (org-agenda-set-restriction-lock 'subtree)))
  20946. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20947. 'speedbar-function 'speedbar-find-file))
  20948. (setq tp (previous-single-property-change
  20949. (1+ p) 'speedbar-function)
  20950. np (next-single-property-change
  20951. tp 'speedbar-function)
  20952. dir (speedbar-line-directory)
  20953. txt (buffer-substring-no-properties (or tp (point-min))
  20954. (or np (point-max))))
  20955. (with-current-buffer (find-file-noselect
  20956. (let ((default-directory dir))
  20957. (expand-file-name txt)))
  20958. (unless (derived-mode-p 'org-mode)
  20959. (error "Cannot restrict to non-Org-mode file"))
  20960. (org-agenda-set-restriction-lock 'file)))
  20961. (t (error "Don't know how to restrict Org-mode's agenda")))
  20962. (move-overlay org-speedbar-restriction-lock-overlay
  20963. (point-at-bol) (point-at-eol))
  20964. (setq current-prefix-arg nil)
  20965. (org-agenda-maybe-redo)))
  20966. (eval-after-load "speedbar"
  20967. '(progn
  20968. (speedbar-add-supported-extension ".org")
  20969. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  20970. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  20971. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  20972. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  20973. (add-hook 'speedbar-visiting-tag-hook
  20974. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  20975. ;;; Fixes and Hacks for problems with other packages
  20976. ;; Make flyspell not check words in links, to not mess up our keymap
  20977. (defun org-mode-flyspell-verify ()
  20978. "Don't let flyspell put overlays at active buttons, or on
  20979. {todo,all-time,additional-option-like}-keywords."
  20980. (let ((pos (max (1- (point)) (point-min)))
  20981. (word (thing-at-point 'word)))
  20982. (and (not (get-text-property pos 'keymap))
  20983. (not (get-text-property pos 'org-no-flyspell))
  20984. (not (member word org-todo-keywords-1))
  20985. (not (member word org-all-time-keywords))
  20986. (not (member word org-options-keywords))
  20987. (not (member word (mapcar 'car org-startup-options)))
  20988. (not (member word org-additional-option-like-keywords-for-flyspell)))))
  20989. (defun org-remove-flyspell-overlays-in (beg end)
  20990. "Remove flyspell overlays in region."
  20991. (and (org-bound-and-true-p flyspell-mode)
  20992. (fboundp 'flyspell-delete-region-overlays)
  20993. (flyspell-delete-region-overlays beg end))
  20994. (add-text-properties beg end '(org-no-flyspell t)))
  20995. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  20996. (eval-after-load "bookmark"
  20997. '(if (boundp 'bookmark-after-jump-hook)
  20998. ;; We can use the hook
  20999. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21000. ;; Hook not available, use advice
  21001. (defadvice bookmark-jump (after org-make-visible activate)
  21002. "Make the position visible."
  21003. (org-bookmark-jump-unhide))))
  21004. ;; Make sure saveplace shows the location if it was hidden
  21005. (eval-after-load "saveplace"
  21006. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21007. "Make the position visible."
  21008. (org-bookmark-jump-unhide)))
  21009. ;; Make sure ecb shows the location if it was hidden
  21010. (eval-after-load "ecb"
  21011. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21012. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21013. (if (derived-mode-p 'org-mode)
  21014. (org-show-context))))
  21015. (defun org-bookmark-jump-unhide ()
  21016. "Unhide the current position, to show the bookmark location."
  21017. (and (derived-mode-p 'org-mode)
  21018. (or (outline-invisible-p)
  21019. (save-excursion (goto-char (max (point-min) (1- (point))))
  21020. (outline-invisible-p)))
  21021. (org-show-context 'bookmark-jump)))
  21022. ;; Make session.el ignore our circular variable
  21023. (eval-after-load "session"
  21024. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21025. ;;;; Experimental code
  21026. (defun org-closed-in-range ()
  21027. "Sparse tree of items closed in a certain time range.
  21028. Still experimental, may disappear in the future."
  21029. (interactive)
  21030. ;; Get the time interval from the user.
  21031. (let* ((time1 (org-float-time
  21032. (org-read-date nil 'to-time nil "Starting date: ")))
  21033. (time2 (org-float-time
  21034. (org-read-date nil 'to-time nil "End date:")))
  21035. ;; callback function
  21036. (callback (lambda ()
  21037. (let ((time
  21038. (org-float-time
  21039. (apply 'encode-time
  21040. (org-parse-time-string
  21041. (match-string 1))))))
  21042. ;; check if time in interval
  21043. (and (>= time time1) (<= time time2))))))
  21044. ;; make tree, check each match with the callback
  21045. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21046. ;;;; Finish up
  21047. (provide 'org)
  21048. (run-hooks 'org-load-hook)
  21049. ;;; org.el ends here